org.el 736 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679
  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, 2009, 2010
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.01trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-src)
  94. (require 'org-footnote)
  95. ;; babel
  96. (require 'ob)
  97. (require 'ob-table)
  98. (require 'ob-lob)
  99. (require 'ob-ref)
  100. (require 'ob-tangle)
  101. (require 'ob-comint)
  102. (require 'ob-keys)
  103. ;; load languages based on value of `org-babel-load-languages'
  104. (defvar org-babel-load-languages)
  105. ;;;###autoload
  106. (defun org-babel-do-load-languages (sym value)
  107. "Load the languages defined in `org-babel-load-languages'."
  108. (set-default sym value)
  109. (mapc (lambda (pair)
  110. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  111. (if active
  112. (progn
  113. (require (intern (concat "ob-" lang))))
  114. (progn
  115. (funcall 'fmakunbound
  116. (intern (concat "org-babel-execute:" lang)))
  117. (funcall 'fmakunbound
  118. (intern (concat "org-babel-expand-body:" lang)))))))
  119. org-babel-load-languages))
  120. (defcustom org-babel-load-languages '((emacs-lisp . t))
  121. "Languages which can be evaluated in Org-mode buffers.
  122. This list can be used to load support for any of the languages
  123. below, note that each language will depend on a different set of
  124. system executables and/or Emacs modes. When a language is
  125. \"loaded\", then code blocks in that language can be evaluated
  126. with `org-babel-execute-src-block' bound by default to C-c
  127. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  128. be set to remove code block evaluation from the C-c C-c
  129. keybinding. By default only Emacs Lisp (which has no
  130. requirements) is loaded."
  131. :group 'org-babel
  132. :set 'org-babel-do-load-languages
  133. :type '(alist :tag "Babel Languages"
  134. :key-type
  135. (choice
  136. (const :tag "C" C)
  137. (const :tag "R" R)
  138. (const :tag "Asymptote" asymptote)
  139. (const :tag "Clojure" clojure)
  140. (const :tag "CSS" css)
  141. (const :tag "Ditaa" ditaa)
  142. (const :tag "Dot" dot)
  143. (const :tag "Emacs Lisp" emacs-lisp)
  144. (const :tag "Gnuplot" gnuplot)
  145. (const :tag "Haskell" haskell)
  146. (const :tag "Javascript" js)
  147. (const :tag "Latex" latex)
  148. (const :tag "Ledger" ledger)
  149. (const :tag "Matlab" matlab)
  150. (const :tag "Mscgen" mscgen)
  151. (const :tag "Ocaml" ocaml)
  152. (const :tag "Octave" octave)
  153. (const :tag "Org" org)
  154. (const :tag "Perl" perl)
  155. (const :tag "PlantUML" plantuml)
  156. (const :tag "Python" python)
  157. (const :tag "Ruby" ruby)
  158. (const :tag "Sass" sass)
  159. (const :tag "Scheme" scheme)
  160. (const :tag "Screen" screen)
  161. (const :tag "Shell Script" sh)
  162. (const :tag "Sql" sql)
  163. (const :tag "Sqlite" sqlite))
  164. :value-type (boolean :tag "Activate" :value t)))
  165. ;;;; Customization variables
  166. (defcustom org-clone-delete-id nil
  167. "Remove ID property of clones of a subtree.
  168. When non-nil, clones of a subtree don't inherit the ID property.
  169. Otherwise they inherit the ID property with a new unique
  170. identifier."
  171. :type 'boolean
  172. :group 'org-id)
  173. ;;; Version
  174. (defconst org-version "7.01trans"
  175. "The version number of the file org.el.")
  176. (defun org-version (&optional here)
  177. "Show the org-mode version in the echo area.
  178. With prefix arg HERE, insert it at point."
  179. (interactive "P")
  180. (let* ((origin default-directory)
  181. (version org-version)
  182. (git-version)
  183. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  184. (when (and (file-exists-p (expand-file-name ".git" dir))
  185. (executable-find "git"))
  186. (unwind-protect
  187. (progn
  188. (cd dir)
  189. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  190. (with-current-buffer "*Shell Command Output*"
  191. (goto-char (point-min))
  192. (setq git-version (buffer-substring (point) (point-at-eol))))
  193. (subst-char-in-string ?- ?. git-version t)
  194. (when (string-match "\\S-"
  195. (shell-command-to-string
  196. "git diff-index --name-only HEAD --"))
  197. (setq git-version (concat git-version ".dirty")))
  198. (setq version (concat version " (" git-version ")"))))
  199. (cd origin)))
  200. (setq version (format "Org-mode version %s" version))
  201. (if here (insert version))
  202. (message version)))
  203. ;;; Compatibility constants
  204. ;;; The custom variables
  205. (defgroup org nil
  206. "Outline-based notes management and organizer."
  207. :tag "Org"
  208. :group 'outlines
  209. :group 'calendar)
  210. (defcustom org-mode-hook nil
  211. "Mode hook for Org-mode, run after the mode was turned on."
  212. :group 'org
  213. :type 'hook)
  214. (defcustom org-load-hook nil
  215. "Hook that is run after org.el has been loaded."
  216. :group 'org
  217. :type 'hook)
  218. (defvar org-modules) ; defined below
  219. (defvar org-modules-loaded nil
  220. "Have the modules been loaded already?")
  221. (defun org-load-modules-maybe (&optional force)
  222. "Load all extensions listed in `org-modules'."
  223. (when (or force (not org-modules-loaded))
  224. (mapc (lambda (ext)
  225. (condition-case nil (require ext)
  226. (error (message "Problems while trying to load feature `%s'" ext))))
  227. org-modules)
  228. (setq org-modules-loaded t)))
  229. (defun org-set-modules (var value)
  230. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  231. (set var value)
  232. (when (featurep 'org)
  233. (org-load-modules-maybe 'force)))
  234. (when (org-bound-and-true-p org-modules)
  235. (let ((a (member 'org-infojs org-modules)))
  236. (and a (setcar a 'org-jsinfo))))
  237. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  238. "Modules that should always be loaded together with org.el.
  239. If a description starts with <C>, the file is not part of Emacs
  240. and loading it will require that you have downloaded and properly installed
  241. the org-mode distribution.
  242. You can also use this system to load external packages (i.e. neither Org
  243. core modules, nor modules from the CONTRIB directory). Just add symbols
  244. to the end of the list. If the package is called org-xyz.el, then you need
  245. to add the symbol `xyz', and the package must have a call to
  246. (provide 'org-xyz)"
  247. :group 'org
  248. :set 'org-set-modules
  249. :type
  250. '(set :greedy t
  251. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  252. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  253. (const :tag " crypt: Encryption of subtrees" org-crypt)
  254. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  255. (const :tag " docview: Links to doc-view buffers" org-docview)
  256. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  257. (const :tag " id: Global IDs for identifying entries" org-id)
  258. (const :tag " info: Links to Info nodes" org-info)
  259. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  260. (const :tag " habit: Track your consistency with habits" org-habit)
  261. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  262. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  263. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  264. (const :tag " mew Links to Mew folders/messages" org-mew)
  265. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  266. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  267. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  268. (const :tag " vm: Links to VM folders/messages" org-vm)
  269. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  270. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  271. (const :tag " mouse: Additional mouse support" org-mouse)
  272. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  273. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  274. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  275. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  276. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  277. (const :tag "C collector: Collect properties into tables" org-collector)
  278. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  279. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  280. (const :tag "C eval: Include command output as text" org-eval)
  281. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  282. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  283. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  284. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  285. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  286. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  287. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  288. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  289. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  290. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  291. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  292. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  293. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  294. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  295. (const :tag "C registry: A registry for Org-mode links" org-registry)
  296. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  297. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  298. (const :tag "C secretary: Team management with org-mode" org-secretary)
  299. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  300. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  301. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  302. (const :tag "C track: Keep up with Org-mode development" org-track)
  303. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  304. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  305. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  306. (defcustom org-support-shift-select nil
  307. "Non-nil means make shift-cursor commands select text when possible.
  308. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  309. selecting a region, or enlarge regions started in this way.
  310. In Org-mode, in special contexts, these same keys are used for other
  311. purposes, important enough to compete with shift selection. Org tries
  312. to balance these needs by supporting `shift-select-mode' outside these
  313. special contexts, under control of this variable.
  314. The default of this variable is nil, to avoid confusing behavior. Shifted
  315. cursor keys will then execute Org commands in the following contexts:
  316. - on a headline, changing TODO state (left/right) and priority (up/down)
  317. - on a time stamp, changing the time
  318. - in a plain list item, changing the bullet type
  319. - in a property definition line, switching between allowed values
  320. - in the BEGIN line of a clock table (changing the time block).
  321. Outside these contexts, the commands will throw an error.
  322. When this variable is t and the cursor is not in a special context,
  323. Org-mode will support shift-selection for making and enlarging regions.
  324. To make this more effective, the bullet cycling will no longer happen
  325. anywhere in an item line, but only if the cursor is exactly on the bullet.
  326. If you set this variable to the symbol `always', then the keys
  327. will not be special in headlines, property lines, and item lines, to make
  328. shift selection work there as well. If this is what you want, you can
  329. use the following alternative commands: `C-c C-t' and `C-c ,' to
  330. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  331. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  332. edited by hand or in column view.
  333. However, when the cursor is on a timestamp, shift-cursor commands
  334. will still edit the time stamp - this is just too good to give up.
  335. XEmacs user should have this variable set to nil, because shift-select-mode
  336. is Emacs 23 only."
  337. :group 'org
  338. :type '(choice
  339. (const :tag "Never" nil)
  340. (const :tag "When outside special context" t)
  341. (const :tag "Everywhere except timestamps" always)))
  342. (defgroup org-startup nil
  343. "Options concerning startup of Org-mode."
  344. :tag "Org Startup"
  345. :group 'org)
  346. (defcustom org-startup-folded t
  347. "Non-nil means entering Org-mode will switch to OVERVIEW.
  348. This can also be configured on a per-file basis by adding one of
  349. the following lines anywhere in the buffer:
  350. #+STARTUP: fold (or `overview', this is equivalent)
  351. #+STARTUP: nofold (or `showall', this is equivalent)
  352. #+STARTUP: content
  353. #+STARTUP: showeverything"
  354. :group 'org-startup
  355. :type '(choice
  356. (const :tag "nofold: show all" nil)
  357. (const :tag "fold: overview" t)
  358. (const :tag "content: all headlines" content)
  359. (const :tag "show everything, even drawers" showeverything)))
  360. (defcustom org-startup-truncated t
  361. "Non-nil means entering Org-mode will set `truncate-lines'.
  362. This is useful since some lines containing links can be very long and
  363. uninteresting. Also tables look terrible when wrapped."
  364. :group 'org-startup
  365. :type 'boolean)
  366. (defcustom org-startup-indented nil
  367. "Non-nil means turn on `org-indent-mode' on startup.
  368. This can also be configured on a per-file basis by adding one of
  369. the following lines anywhere in the buffer:
  370. #+STARTUP: indent
  371. #+STARTUP: noindent"
  372. :group 'org-structure
  373. :type '(choice
  374. (const :tag "Not" nil)
  375. (const :tag "Globally (slow on startup in large files)" t)))
  376. (defcustom org-use-sub-superscripts t
  377. "Non-nil means interpret \"_\" and \"^\" for export.
  378. When this option is turned on, you can use TeX-like syntax for sub- and
  379. superscripts. Several characters after \"_\" or \"^\" will be
  380. considered as a single item - so grouping with {} is normally not
  381. needed. For example, the following things will be parsed as single
  382. sub- or superscripts.
  383. 10^24 or 10^tau several digits will be considered 1 item.
  384. 10^-12 or 10^-tau a leading sign with digits or a word
  385. x^2-y^3 will be read as x^2 - y^3, because items are
  386. terminated by almost any nonword/nondigit char.
  387. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  388. Still, ambiguity is possible - so when in doubt use {} to enclose the
  389. sub/superscript. If you set this variable to the symbol `{}',
  390. the braces are *required* in order to trigger interpretations as
  391. sub/superscript. This can be helpful in documents that need \"_\"
  392. frequently in plain text.
  393. Not all export backends support this, but HTML does.
  394. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  395. :group 'org-startup
  396. :group 'org-export-translation
  397. :type '(choice
  398. (const :tag "Always interpret" t)
  399. (const :tag "Only with braces" {})
  400. (const :tag "Never interpret" nil)))
  401. (if (fboundp 'defvaralias)
  402. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  403. (defcustom org-startup-with-beamer-mode nil
  404. "Non-nil means turn on `org-beamer-mode' on startup.
  405. This can also be configured on a per-file basis by adding one of
  406. the following lines anywhere in the buffer:
  407. #+STARTUP: beamer"
  408. :group 'org-startup
  409. :type 'boolean)
  410. (defcustom org-startup-align-all-tables nil
  411. "Non-nil means align all tables when visiting a file.
  412. This is useful when the column width in tables is forced with <N> cookies
  413. in table fields. Such tables will look correct only after the first re-align.
  414. This can also be configured on a per-file basis by adding one of
  415. the following lines anywhere in the buffer:
  416. #+STARTUP: align
  417. #+STARTUP: noalign"
  418. :group 'org-startup
  419. :type 'boolean)
  420. (defcustom org-startup-with-inline-images nil
  421. "Non-nil means show inline images when loading a new Org file.
  422. This can also be configured on a per-file basis by adding one of
  423. the following lines anywhere in the buffer:
  424. #+STARTUP: inlineimages
  425. #+STARTUP: noinlineimages"
  426. :group 'org-startup
  427. :type 'boolean)
  428. (defcustom org-insert-mode-line-in-empty-file nil
  429. "Non-nil means insert the first line setting Org-mode in empty files.
  430. When the function `org-mode' is called interactively in an empty file, this
  431. normally means that the file name does not automatically trigger Org-mode.
  432. To ensure that the file will always be in Org-mode in the future, a
  433. line enforcing Org-mode will be inserted into the buffer, if this option
  434. has been set."
  435. :group 'org-startup
  436. :type 'boolean)
  437. (defcustom org-replace-disputed-keys nil
  438. "Non-nil means use alternative key bindings for some keys.
  439. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  440. These keys are also used by other packages like shift-selection-mode'
  441. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  442. If you want to use Org-mode together with one of these other modes,
  443. or more generally if you would like to move some Org-mode commands to
  444. other keys, set this variable and configure the keys with the variable
  445. `org-disputed-keys'.
  446. This option is only relevant at load-time of Org-mode, and must be set
  447. *before* org.el is loaded. Changing it requires a restart of Emacs to
  448. become effective."
  449. :group 'org-startup
  450. :type 'boolean)
  451. (defcustom org-use-extra-keys nil
  452. "Non-nil means use extra key sequence definitions for certain commands.
  453. This happens automatically if you run XEmacs or if `window-system'
  454. is nil. This variable lets you do the same manually. You must
  455. set it before loading org.
  456. Example: on Carbon Emacs 22 running graphically, with an external
  457. keyboard on a Powerbook, the default way of setting M-left might
  458. not work for either Alt or ESC. Setting this variable will make
  459. it work for ESC."
  460. :group 'org-startup
  461. :type 'boolean)
  462. (if (fboundp 'defvaralias)
  463. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  464. (defcustom org-disputed-keys
  465. '(([(shift up)] . [(meta p)])
  466. ([(shift down)] . [(meta n)])
  467. ([(shift left)] . [(meta -)])
  468. ([(shift right)] . [(meta +)])
  469. ([(control shift right)] . [(meta shift +)])
  470. ([(control shift left)] . [(meta shift -)]))
  471. "Keys for which Org-mode and other modes compete.
  472. This is an alist, cars are the default keys, second element specifies
  473. the alternative to use when `org-replace-disputed-keys' is t.
  474. Keys can be specified in any syntax supported by `define-key'.
  475. The value of this option takes effect only at Org-mode's startup,
  476. therefore you'll have to restart Emacs to apply it after changing."
  477. :group 'org-startup
  478. :type 'alist)
  479. (defun org-key (key)
  480. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  481. Or return the original if not disputed.
  482. Also apply the translations defined in `org-xemacs-key-equivalents'."
  483. (when org-replace-disputed-keys
  484. (let* ((nkey (key-description key))
  485. (x (org-find-if (lambda (x)
  486. (equal (key-description (car x)) nkey))
  487. org-disputed-keys)))
  488. (setq key (if x (cdr x) key))))
  489. (when (featurep 'xemacs)
  490. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  491. key)
  492. (defun org-find-if (predicate seq)
  493. (catch 'exit
  494. (while seq
  495. (if (funcall predicate (car seq))
  496. (throw 'exit (car seq))
  497. (pop seq)))))
  498. (defun org-defkey (keymap key def)
  499. "Define a key, possibly translated, as returned by `org-key'."
  500. (define-key keymap (org-key key) def))
  501. (defcustom org-ellipsis nil
  502. "The ellipsis to use in the Org-mode outline.
  503. When nil, just use the standard three dots. When a string, use that instead,
  504. When a face, use the standard 3 dots, but with the specified face.
  505. The change affects only Org-mode (which will then use its own display table).
  506. Changing this requires executing `M-x org-mode' in a buffer to become
  507. effective."
  508. :group 'org-startup
  509. :type '(choice (const :tag "Default" nil)
  510. (face :tag "Face" :value org-warning)
  511. (string :tag "String" :value "...#")))
  512. (defvar org-display-table nil
  513. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  514. (defgroup org-keywords nil
  515. "Keywords in Org-mode."
  516. :tag "Org Keywords"
  517. :group 'org)
  518. (defcustom org-deadline-string "DEADLINE:"
  519. "String to mark deadline entries.
  520. A deadline is this string, followed by a time stamp. Should be a word,
  521. terminated by a colon. You can insert a schedule keyword and
  522. a timestamp with \\[org-deadline].
  523. Changes become only effective after restarting Emacs."
  524. :group 'org-keywords
  525. :type 'string)
  526. (defcustom org-scheduled-string "SCHEDULED:"
  527. "String to mark scheduled TODO entries.
  528. A schedule is this string, followed by a time stamp. Should be a word,
  529. terminated by a colon. You can insert a schedule keyword and
  530. a timestamp with \\[org-schedule].
  531. Changes become only effective after restarting Emacs."
  532. :group 'org-keywords
  533. :type 'string)
  534. (defcustom org-closed-string "CLOSED:"
  535. "String used as the prefix for timestamps logging closing a TODO entry."
  536. :group 'org-keywords
  537. :type 'string)
  538. (defcustom org-clock-string "CLOCK:"
  539. "String used as prefix for timestamps clocking work hours on an item."
  540. :group 'org-keywords
  541. :type 'string)
  542. (defcustom org-comment-string "COMMENT"
  543. "Entries starting with this keyword will never be exported.
  544. An entry can be toggled between COMMENT and normal with
  545. \\[org-toggle-comment].
  546. Changes become only effective after restarting Emacs."
  547. :group 'org-keywords
  548. :type 'string)
  549. (defcustom org-quote-string "QUOTE"
  550. "Entries starting with this keyword will be exported in fixed-width font.
  551. Quoting applies only to the text in the entry following the headline, and does
  552. not extend beyond the next headline, even if that is lower level.
  553. An entry can be toggled between QUOTE and normal with
  554. \\[org-toggle-fixed-width-section]."
  555. :group 'org-keywords
  556. :type 'string)
  557. (defconst org-repeat-re
  558. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  559. "Regular expression for specifying repeated events.
  560. After a match, group 1 contains the repeat expression.")
  561. (defgroup org-structure nil
  562. "Options concerning the general structure of Org-mode files."
  563. :tag "Org Structure"
  564. :group 'org)
  565. (defgroup org-reveal-location nil
  566. "Options about how to make context of a location visible."
  567. :tag "Org Reveal Location"
  568. :group 'org-structure)
  569. (defconst org-context-choice
  570. '(choice
  571. (const :tag "Always" t)
  572. (const :tag "Never" nil)
  573. (repeat :greedy t :tag "Individual contexts"
  574. (cons
  575. (choice :tag "Context"
  576. (const agenda)
  577. (const org-goto)
  578. (const occur-tree)
  579. (const tags-tree)
  580. (const link-search)
  581. (const mark-goto)
  582. (const bookmark-jump)
  583. (const isearch)
  584. (const default))
  585. (boolean))))
  586. "Contexts for the reveal options.")
  587. (defcustom org-show-hierarchy-above '((default . t))
  588. "Non-nil means show full hierarchy when revealing a location.
  589. Org-mode often shows locations in an org-mode file which might have
  590. been invisible before. When this is set, the hierarchy of headings
  591. above the exposed location is shown.
  592. Turning this off for example for sparse trees makes them very compact.
  593. Instead of t, this can also be an alist specifying this option for different
  594. contexts. Valid contexts are
  595. agenda when exposing an entry from the agenda
  596. org-goto when using the command `org-goto' on key C-c C-j
  597. occur-tree when using the command `org-occur' on key C-c /
  598. tags-tree when constructing a sparse tree based on tags matches
  599. link-search when exposing search matches associated with a link
  600. mark-goto when exposing the jump goal of a mark
  601. bookmark-jump when exposing a bookmark location
  602. isearch when exiting from an incremental search
  603. default default for all contexts not set explicitly"
  604. :group 'org-reveal-location
  605. :type org-context-choice)
  606. (defcustom org-show-following-heading '((default . nil))
  607. "Non-nil means show following heading when revealing a location.
  608. Org-mode often shows locations in an org-mode file which might have
  609. been invisible before. When this is set, the heading following the
  610. match is shown.
  611. Turning this off for example for sparse trees makes them very compact,
  612. but makes it harder to edit the location of the match. In such a case,
  613. use the command \\[org-reveal] to show more context.
  614. Instead of t, this can also be an alist specifying this option for different
  615. contexts. See `org-show-hierarchy-above' for valid contexts."
  616. :group 'org-reveal-location
  617. :type org-context-choice)
  618. (defcustom org-show-siblings '((default . nil) (isearch t))
  619. "Non-nil means show all sibling heading when revealing a location.
  620. Org-mode often shows locations in an org-mode file which might have
  621. been invisible before. When this is set, the sibling of the current entry
  622. heading are all made visible. If `org-show-hierarchy-above' is t,
  623. the same happens on each level of the hierarchy above the current entry.
  624. By default this is on for the isearch context, off for all other contexts.
  625. Turning this off for example for sparse trees makes them very compact,
  626. but makes it harder to edit the location of the match. In such a case,
  627. use the command \\[org-reveal] to show more context.
  628. Instead of t, this can also be an alist specifying this option for different
  629. contexts. See `org-show-hierarchy-above' for valid contexts."
  630. :group 'org-reveal-location
  631. :type org-context-choice)
  632. (defcustom org-show-entry-below '((default . nil))
  633. "Non-nil means show the entry below a headline when revealing a location.
  634. Org-mode often shows locations in an org-mode file which might have
  635. been invisible before. When this is set, the text below the headline that is
  636. exposed is also shown.
  637. By default this is off for all contexts.
  638. Instead of t, this can also be an alist specifying this option for different
  639. contexts. See `org-show-hierarchy-above' for valid contexts."
  640. :group 'org-reveal-location
  641. :type org-context-choice)
  642. (defcustom org-indirect-buffer-display 'other-window
  643. "How should indirect tree buffers be displayed?
  644. This applies to indirect buffers created with the commands
  645. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  646. Valid values are:
  647. current-window Display in the current window
  648. other-window Just display in another window.
  649. dedicated-frame Create one new frame, and re-use it each time.
  650. new-frame Make a new frame each time. Note that in this case
  651. previously-made indirect buffers are kept, and you need to
  652. kill these buffers yourself."
  653. :group 'org-structure
  654. :group 'org-agenda-windows
  655. :type '(choice
  656. (const :tag "In current window" current-window)
  657. (const :tag "In current frame, other window" other-window)
  658. (const :tag "Each time a new frame" new-frame)
  659. (const :tag "One dedicated frame" dedicated-frame)))
  660. (defcustom org-use-speed-commands nil
  661. "Non-nil means activate single letter commands at beginning of a headline.
  662. This may also be a function to test for appropriate locations where speed
  663. commands should be active."
  664. :group 'org-structure
  665. :type '(choice
  666. (const :tag "Never" nil)
  667. (const :tag "At beginning of headline stars" t)
  668. (function)))
  669. (defcustom org-speed-commands-user nil
  670. "Alist of additional speed commands.
  671. This list will be checked before `org-speed-commands-default'
  672. when the variable `org-use-speed-commands' is non-nil
  673. and when the cursor is at the beginning of a headline.
  674. The car if each entry is a string with a single letter, which must
  675. be assigned to `self-insert-command' in the global map.
  676. The cdr is either a command to be called interactively, a function
  677. to be called, or a form to be evaluated.
  678. An entry that is just a list with a single string will be interpreted
  679. as a descriptive headline that will be added when listing the speed
  680. commands in the Help buffer using the `?' speed command."
  681. :group 'org-structure
  682. :type '(repeat :value ("k" . ignore)
  683. (choice :value ("k" . ignore)
  684. (list :tag "Descriptive Headline" (string :tag "Headline"))
  685. (cons :tag "Letter and Command"
  686. (string :tag "Command letter")
  687. (choice
  688. (function)
  689. (sexp))))))
  690. (defgroup org-cycle nil
  691. "Options concerning visibility cycling in Org-mode."
  692. :tag "Org Cycle"
  693. :group 'org-structure)
  694. (defcustom org-cycle-skip-children-state-if-no-children t
  695. "Non-nil means skip CHILDREN state in entries that don't have any."
  696. :group 'org-cycle
  697. :type 'boolean)
  698. (defcustom org-cycle-max-level nil
  699. "Maximum level which should still be subject to visibility cycling.
  700. Levels higher than this will, for cycling, be treated as text, not a headline.
  701. When `org-odd-levels-only' is set, a value of N in this variable actually
  702. means 2N-1 stars as the limiting headline.
  703. When nil, cycle all levels.
  704. Note that the limiting level of cycling is also influenced by
  705. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  706. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  707. than its value."
  708. :group 'org-cycle
  709. :type '(choice
  710. (const :tag "No limit" nil)
  711. (integer :tag "Maximum level")))
  712. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  713. "Names of drawers. Drawers are not opened by cycling on the headline above.
  714. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  715. this:
  716. :DRAWERNAME:
  717. .....
  718. :END:
  719. The drawer \"PROPERTIES\" is special for capturing properties through
  720. the property API.
  721. Drawers can be defined on the per-file basis with a line like:
  722. #+DRAWERS: HIDDEN STATE PROPERTIES"
  723. :group 'org-structure
  724. :group 'org-cycle
  725. :type '(repeat (string :tag "Drawer Name")))
  726. (defcustom org-hide-block-startup nil
  727. "Non-nil means entering Org-mode will fold all blocks.
  728. This can also be set in on a per-file basis with
  729. #+STARTUP: hideblocks
  730. #+STARTUP: showblocks"
  731. :group 'org-startup
  732. :group 'org-cycle
  733. :type 'boolean)
  734. (defcustom org-cycle-global-at-bob nil
  735. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  736. This makes it possible to do global cycling without having to use S-TAB or
  737. \\[universal-argument] TAB. For this special case to work, the first line \
  738. of the buffer
  739. must not be a headline - it may be empty or some other text. When used in
  740. this way, `org-cycle-hook' is disables temporarily, to make sure the
  741. cursor stays at the beginning of the buffer.
  742. When this option is nil, don't do anything special at the beginning
  743. of the buffer."
  744. :group 'org-cycle
  745. :type 'boolean)
  746. (defcustom org-cycle-level-after-item/entry-creation t
  747. "Non-nil means cycle entry level or item indentation in new empty entries.
  748. When the cursor is at the end of an empty headline, i.e with only stars
  749. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  750. and then all possible ancestor states, before returning to the original state.
  751. This makes data entry extremely fast: M-RET to create a new headline,
  752. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  753. When the cursor is at the end of an empty plain list item, one TAB will
  754. make it a subitem, two or more tabs will back up to make this an item
  755. higher up in the item hierarchy."
  756. :group 'org-cycle
  757. :type 'boolean)
  758. (defcustom org-cycle-emulate-tab t
  759. "Where should `org-cycle' emulate TAB.
  760. nil Never
  761. white Only in completely white lines
  762. whitestart Only at the beginning of lines, before the first non-white char
  763. t Everywhere except in headlines
  764. exc-hl-bol Everywhere except at the start of a headline
  765. If TAB is used in a place where it does not emulate TAB, the current subtree
  766. visibility is cycled."
  767. :group 'org-cycle
  768. :type '(choice (const :tag "Never" nil)
  769. (const :tag "Only in completely white lines" white)
  770. (const :tag "Before first char in a line" whitestart)
  771. (const :tag "Everywhere except in headlines" t)
  772. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  773. ))
  774. (defcustom org-cycle-separator-lines 2
  775. "Number of empty lines needed to keep an empty line between collapsed trees.
  776. If you leave an empty line between the end of a subtree and the following
  777. headline, this empty line is hidden when the subtree is folded.
  778. Org-mode will leave (exactly) one empty line visible if the number of
  779. empty lines is equal or larger to the number given in this variable.
  780. So the default 2 means at least 2 empty lines after the end of a subtree
  781. are needed to produce free space between a collapsed subtree and the
  782. following headline.
  783. If the number is negative, and the number of empty lines is at least -N,
  784. all empty lines are shown.
  785. Special case: when 0, never leave empty lines in collapsed view."
  786. :group 'org-cycle
  787. :type 'integer)
  788. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  789. (defcustom org-pre-cycle-hook nil
  790. "Hook that is run before visibility cycling is happening.
  791. The function(s) in this hook must accept a single argument which indicates
  792. the new state that will be set right after running this hook. The
  793. argument is a symbol. Before a global state change, it can have the values
  794. `overview', `content', or `all'. Before a local state change, it can have
  795. the values `folded', `children', or `subtree'."
  796. :group 'org-cycle
  797. :type 'hook)
  798. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  799. org-cycle-hide-drawers
  800. org-cycle-show-empty-lines
  801. org-optimize-window-after-visibility-change)
  802. "Hook that is run after `org-cycle' has changed the buffer visibility.
  803. The function(s) in this hook must accept a single argument which indicates
  804. the new state that was set by the most recent `org-cycle' command. The
  805. argument is a symbol. After a global state change, it can have the values
  806. `overview', `content', or `all'. After a local state change, it can have
  807. the values `folded', `children', or `subtree'."
  808. :group 'org-cycle
  809. :type 'hook)
  810. (defgroup org-edit-structure nil
  811. "Options concerning structure editing in Org-mode."
  812. :tag "Org Edit Structure"
  813. :group 'org-structure)
  814. (defcustom org-odd-levels-only nil
  815. "Non-nil means skip even levels and only use odd levels for the outline.
  816. This has the effect that two stars are being added/taken away in
  817. promotion/demotion commands. It also influences how levels are
  818. handled by the exporters.
  819. Changing it requires restart of `font-lock-mode' to become effective
  820. for fontification also in regions already fontified.
  821. You may also set this on a per-file basis by adding one of the following
  822. lines to the buffer:
  823. #+STARTUP: odd
  824. #+STARTUP: oddeven"
  825. :group 'org-edit-structure
  826. :group 'org-appearance
  827. :type 'boolean)
  828. (defcustom org-adapt-indentation t
  829. "Non-nil means adapt indentation to outline node level.
  830. When this variable is set, Org assumes that you write outlines by
  831. indenting text in each node to align with the headline (after the stars).
  832. The following issues are influenced by this variable:
  833. - When this is set and the *entire* text in an entry is indented, the
  834. indentation is increased by one space in a demotion command, and
  835. decreased by one in a promotion command. If any line in the entry
  836. body starts with text at column 0, indentation is not changed at all.
  837. - Property drawers and planning information is inserted indented when
  838. this variable s set. When nil, they will not be indented.
  839. - TAB indents a line relative to context. The lines below a headline
  840. will be indented when this variable is set.
  841. Note that this is all about true indentation, by adding and removing
  842. space characters. See also `org-indent.el' which does level-dependent
  843. indentation in a virtual way, i.e. at display time in Emacs."
  844. :group 'org-edit-structure
  845. :type 'boolean)
  846. (defcustom org-special-ctrl-a/e nil
  847. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  848. When t, `C-a' will bring back the cursor to the beginning of the
  849. headline text, i.e. after the stars and after a possible TODO keyword.
  850. In an item, this will be the position after the bullet.
  851. When the cursor is already at that position, another `C-a' will bring
  852. it to the beginning of the line.
  853. `C-e' will jump to the end of the headline, ignoring the presence of tags
  854. in the headline. A second `C-e' will then jump to the true end of the
  855. line, after any tags. This also means that, when this variable is
  856. non-nil, `C-e' also will never jump beyond the end of the heading of a
  857. folded section, i.e. not after the ellipses.
  858. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  859. going to the true line boundary first. Only a directly following, identical
  860. keypress will bring the cursor to the special positions.
  861. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  862. set separately."
  863. :group 'org-edit-structure
  864. :type '(choice
  865. (const :tag "off" nil)
  866. (const :tag "on: after stars/bullet and before tags first" t)
  867. (const :tag "reversed: true line boundary first" reversed)
  868. (cons :tag "Set C-a and C-e separately"
  869. (choice :tag "Special C-a"
  870. (const :tag "off" nil)
  871. (const :tag "on: after stars/bullet first" t)
  872. (const :tag "reversed: before stars/bullet first" reversed))
  873. (choice :tag "Special C-e"
  874. (const :tag "off" nil)
  875. (const :tag "on: before tags first" t)
  876. (const :tag "reversed: after tags first" reversed)))))
  877. (if (fboundp 'defvaralias)
  878. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  879. (defcustom org-special-ctrl-k nil
  880. "Non-nil means `C-k' will behave specially in headlines.
  881. When nil, `C-k' will call the default `kill-line' command.
  882. When t, the following will happen while the cursor is in the headline:
  883. - When the cursor is at the beginning of a headline, kill the entire
  884. line and possible the folded subtree below the line.
  885. - When in the middle of the headline text, kill the headline up to the tags.
  886. - When after the headline text, kill the tags."
  887. :group 'org-edit-structure
  888. :type 'boolean)
  889. (defcustom org-ctrl-k-protect-subtree nil
  890. "Non-nil means, do not delete a hidden subtree with C-k.
  891. When set to the symbol `error', simply throw an error when C-k is
  892. used to kill (part-of) a headline that has hidden text behind it.
  893. Any other non-nil value will result in a query to the user, if it is
  894. OK to kill that hidden subtree. When nil, kill without remorse."
  895. :group 'org-edit-structure
  896. :type '(choice
  897. (const :tag "Do not protect hidden subtrees" nil)
  898. (const :tag "Protect hidden subtrees with a security query" t)
  899. (const :tag "Never kill a hidden subtree with C-k" error)))
  900. (defcustom org-yank-folded-subtrees t
  901. "Non-nil means when yanking subtrees, fold them.
  902. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  903. it starts with a heading and all other headings in it are either children
  904. or siblings, then fold all the subtrees. However, do this only if no
  905. text after the yank would be swallowed into a folded tree by this action."
  906. :group 'org-edit-structure
  907. :type 'boolean)
  908. (defcustom org-yank-adjusted-subtrees nil
  909. "Non-nil means when yanking subtrees, adjust the level.
  910. With this setting, `org-paste-subtree' is used to insert the subtree, see
  911. this function for details."
  912. :group 'org-edit-structure
  913. :type 'boolean)
  914. (defcustom org-M-RET-may-split-line '((default . t))
  915. "Non-nil means M-RET will split the line at the cursor position.
  916. When nil, it will go to the end of the line before making a
  917. new line.
  918. You may also set this option in a different way for different
  919. contexts. Valid contexts are:
  920. headline when creating a new headline
  921. item when creating a new item
  922. table in a table field
  923. default the value to be used for all contexts not explicitly
  924. customized"
  925. :group 'org-structure
  926. :group 'org-table
  927. :type '(choice
  928. (const :tag "Always" t)
  929. (const :tag "Never" nil)
  930. (repeat :greedy t :tag "Individual contexts"
  931. (cons
  932. (choice :tag "Context"
  933. (const headline)
  934. (const item)
  935. (const table)
  936. (const default))
  937. (boolean)))))
  938. (defcustom org-insert-heading-respect-content nil
  939. "Non-nil means insert new headings after the current subtree.
  940. When nil, the new heading is created directly after the current line.
  941. The commands \\[org-insert-heading-respect-content] and
  942. \\[org-insert-todo-heading-respect-content] turn this variable on
  943. for the duration of the command."
  944. :group 'org-structure
  945. :type 'boolean)
  946. (defcustom org-blank-before-new-entry '((heading . auto)
  947. (plain-list-item . auto))
  948. "Should `org-insert-heading' leave a blank line before new heading/item?
  949. The value is an alist, with `heading' and `plain-list-item' as car,
  950. and a boolean flag as cdr. The cdr may lso be the symbol `auto', and then
  951. Org will look at the surrounding headings/items and try to make an
  952. intelligent decision wether to insert a blank line or not.
  953. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  954. set, the setting here is ignored and no empty line is inserted, to avoid
  955. breaking the list structure."
  956. :group 'org-edit-structure
  957. :type '(list
  958. (cons (const heading)
  959. (choice (const :tag "Never" nil)
  960. (const :tag "Always" t)
  961. (const :tag "Auto" auto)))
  962. (cons (const plain-list-item)
  963. (choice (const :tag "Never" nil)
  964. (const :tag "Always" t)
  965. (const :tag "Auto" auto)))))
  966. (defcustom org-insert-heading-hook nil
  967. "Hook being run after inserting a new heading."
  968. :group 'org-edit-structure
  969. :type 'hook)
  970. (defcustom org-enable-fixed-width-editor t
  971. "Non-nil means lines starting with \":\" are treated as fixed-width.
  972. This currently only means they are never auto-wrapped.
  973. When nil, such lines will be treated like ordinary lines.
  974. See also the QUOTE keyword."
  975. :group 'org-edit-structure
  976. :type 'boolean)
  977. (defcustom org-goto-auto-isearch t
  978. "Non-nil means typing characters in `org-goto' starts incremental search."
  979. :group 'org-edit-structure
  980. :type 'boolean)
  981. (defgroup org-sparse-trees nil
  982. "Options concerning sparse trees in Org-mode."
  983. :tag "Org Sparse Trees"
  984. :group 'org-structure)
  985. (defcustom org-highlight-sparse-tree-matches t
  986. "Non-nil means highlight all matches that define a sparse tree.
  987. The highlights will automatically disappear the next time the buffer is
  988. changed by an edit command."
  989. :group 'org-sparse-trees
  990. :type 'boolean)
  991. (defcustom org-remove-highlights-with-change t
  992. "Non-nil means any change to the buffer will remove temporary highlights.
  993. Such highlights are created by `org-occur' and `org-clock-display'.
  994. When nil, `C-c C-c needs to be used to get rid of the highlights.
  995. The highlights created by `org-preview-latex-fragment' always need
  996. `C-c C-c' to be removed."
  997. :group 'org-sparse-trees
  998. :group 'org-time
  999. :type 'boolean)
  1000. (defcustom org-occur-hook '(org-first-headline-recenter)
  1001. "Hook that is run after `org-occur' has constructed a sparse tree.
  1002. This can be used to recenter the window to show as much of the structure
  1003. as possible."
  1004. :group 'org-sparse-trees
  1005. :type 'hook)
  1006. (defgroup org-imenu-and-speedbar nil
  1007. "Options concerning imenu and speedbar in Org-mode."
  1008. :tag "Org Imenu and Speedbar"
  1009. :group 'org-structure)
  1010. (defcustom org-imenu-depth 2
  1011. "The maximum level for Imenu access to Org-mode headlines.
  1012. This also applied for speedbar access."
  1013. :group 'org-imenu-and-speedbar
  1014. :type 'integer)
  1015. (defgroup org-table nil
  1016. "Options concerning tables in Org-mode."
  1017. :tag "Org Table"
  1018. :group 'org)
  1019. (defcustom org-enable-table-editor 'optimized
  1020. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1021. When nil, such lines will be treated like ordinary lines.
  1022. When equal to the symbol `optimized', the table editor will be optimized to
  1023. do the following:
  1024. - Automatic overwrite mode in front of whitespace in table fields.
  1025. This makes the structure of the table stay in tact as long as the edited
  1026. field does not exceed the column width.
  1027. - Minimize the number of realigns. Normally, the table is aligned each time
  1028. TAB or RET are pressed to move to another field. With optimization this
  1029. happens only if changes to a field might have changed the column width.
  1030. Optimization requires replacing the functions `self-insert-command',
  1031. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1032. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1033. very good at guessing when a re-align will be necessary, but you can always
  1034. force one with \\[org-ctrl-c-ctrl-c].
  1035. If you would like to use the optimized version in Org-mode, but the
  1036. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1037. This variable can be used to turn on and off the table editor during a session,
  1038. but in order to toggle optimization, a restart is required.
  1039. See also the variable `org-table-auto-blank-field'."
  1040. :group 'org-table
  1041. :type '(choice
  1042. (const :tag "off" nil)
  1043. (const :tag "on" t)
  1044. (const :tag "on, optimized" optimized)))
  1045. (defcustom org-self-insert-cluster-for-undo t
  1046. "Non-nil means cluster self-insert commands for undo when possible.
  1047. If this is set, then, like in the Emacs command loop, 20 consecutive
  1048. characters will be undone together.
  1049. This is configurable, because there is some impact on typing performance."
  1050. :group 'org-table
  1051. :type 'boolean)
  1052. (defcustom org-table-tab-recognizes-table.el t
  1053. "Non-nil means TAB will automatically notice a table.el table.
  1054. When it sees such a table, it moves point into it and - if necessary -
  1055. calls `table-recognize-table'."
  1056. :group 'org-table-editing
  1057. :type 'boolean)
  1058. (defgroup org-link nil
  1059. "Options concerning links in Org-mode."
  1060. :tag "Org Link"
  1061. :group 'org)
  1062. (defvar org-link-abbrev-alist-local nil
  1063. "Buffer-local version of `org-link-abbrev-alist', which see.
  1064. The value of this is taken from the #+LINK lines.")
  1065. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1066. (defcustom org-link-abbrev-alist nil
  1067. "Alist of link abbreviations.
  1068. The car of each element is a string, to be replaced at the start of a link.
  1069. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1070. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1071. [[linkkey:tag][description]]
  1072. The 'linkkey' must be a word word, starting with a letter, followed
  1073. by letters, numbers, '-' or '_'.
  1074. If REPLACE is a string, the tag will simply be appended to create the link.
  1075. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1076. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1077. be inserted at that point (see the function `url-hexify-string').
  1078. REPLACE may also be a function that will be called with the tag as the
  1079. only argument to create the link, which should be returned as a string.
  1080. See the manual for examples."
  1081. :group 'org-link
  1082. :type '(repeat
  1083. (cons
  1084. (string :tag "Protocol")
  1085. (choice
  1086. (string :tag "Format")
  1087. (function)))))
  1088. (defcustom org-descriptive-links t
  1089. "Non-nil means hide link part and only show description of bracket links.
  1090. Bracket links are like [[link][description]]. This variable sets the initial
  1091. state in new org-mode buffers. The setting can then be toggled on a
  1092. per-buffer basis from the Org->Hyperlinks menu."
  1093. :group 'org-link
  1094. :type 'boolean)
  1095. (defcustom org-link-file-path-type 'adaptive
  1096. "How the path name in file links should be stored.
  1097. Valid values are:
  1098. relative Relative to the current directory, i.e. the directory of the file
  1099. into which the link is being inserted.
  1100. absolute Absolute path, if possible with ~ for home directory.
  1101. noabbrev Absolute path, no abbreviation of home directory.
  1102. adaptive Use relative path for files in the current directory and sub-
  1103. directories of it. For other files, use an absolute path."
  1104. :group 'org-link
  1105. :type '(choice
  1106. (const relative)
  1107. (const absolute)
  1108. (const noabbrev)
  1109. (const adaptive)))
  1110. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1111. "Types of links that should be activated in Org-mode files.
  1112. This is a list of symbols, each leading to the activation of a certain link
  1113. type. In principle, it does not hurt to turn on most link types - there may
  1114. be a small gain when turning off unused link types. The types are:
  1115. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1116. angle Links in angular brackets that may contain whitespace like
  1117. <bbdb:Carsten Dominik>.
  1118. plain Plain links in normal text, no whitespace, like http://google.com.
  1119. radio Text that is matched by a radio target, see manual for details.
  1120. tag Tag settings in a headline (link to tag search).
  1121. date Time stamps (link to calendar).
  1122. footnote Footnote labels.
  1123. Changing this variable requires a restart of Emacs to become effective."
  1124. :group 'org-link
  1125. :type '(set :greedy t
  1126. (const :tag "Double bracket links" bracket)
  1127. (const :tag "Angular bracket links" angle)
  1128. (const :tag "Plain text links" plain)
  1129. (const :tag "Radio target matches" radio)
  1130. (const :tag "Tags" tag)
  1131. (const :tag "Timestamps" date)
  1132. (const :tag "Footnotes" footnote)))
  1133. (defcustom org-make-link-description-function nil
  1134. "Function to use to generate link descriptions from links.
  1135. If nil the link location will be used. This function must take
  1136. two parameters; the first is the link and the second the
  1137. description `org-insert-link' has generated, and should return the
  1138. description to use."
  1139. :group 'org-link
  1140. :type 'function)
  1141. (defgroup org-link-store nil
  1142. "Options concerning storing links in Org-mode."
  1143. :tag "Org Store Link"
  1144. :group 'org-link)
  1145. (defcustom org-email-link-description-format "Email %c: %.30s"
  1146. "Format of the description part of a link to an email or usenet message.
  1147. The following %-escapes will be replaced by corresponding information:
  1148. %F full \"From\" field
  1149. %f name, taken from \"From\" field, address if no name
  1150. %T full \"To\" field
  1151. %t first name in \"To\" field, address if no name
  1152. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1153. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1154. %s subject
  1155. %m message-id.
  1156. You may use normal field width specification between the % and the letter.
  1157. This is for example useful to limit the length of the subject.
  1158. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1159. :group 'org-link-store
  1160. :type 'string)
  1161. (defcustom org-from-is-user-regexp
  1162. (let (r1 r2)
  1163. (when (and user-mail-address (not (string= user-mail-address "")))
  1164. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1165. (when (and user-full-name (not (string= user-full-name "")))
  1166. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1167. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1168. "Regexp matched against the \"From:\" header of an email or usenet message.
  1169. It should match if the message is from the user him/herself."
  1170. :group 'org-link-store
  1171. :type 'regexp)
  1172. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1173. "Non-nil means storing a link to an Org file will use entry IDs.
  1174. Note that before this variable is even considered, org-id must be loaded,
  1175. so please customize `org-modules' and turn it on.
  1176. The variable can have the following values:
  1177. t Create an ID if needed to make a link to the current entry.
  1178. create-if-interactive
  1179. If `org-store-link' is called directly (interactively, as a user
  1180. command), do create an ID to support the link. But when doing the
  1181. job for remember, only use the ID if it already exists. The
  1182. purpose of this setting is to avoid proliferation of unwanted
  1183. IDs, just because you happen to be in an Org file when you
  1184. call `org-remember' that automatically and preemptively
  1185. creates a link. If you do want to get an ID link in a remember
  1186. template to an entry not having an ID, create it first by
  1187. explicitly creating a link to it, using `C-c C-l' first.
  1188. create-if-interactive-and-no-custom-id
  1189. Like create-if-interactive, but do not create an ID if there is
  1190. a CUSTOM_ID property defined in the entry. This is the default.
  1191. use-existing
  1192. Use existing ID, do not create one.
  1193. nil Never use an ID to make a link, instead link using a text search for
  1194. the headline text."
  1195. :group 'org-link-store
  1196. :type '(choice
  1197. (const :tag "Create ID to make link" t)
  1198. (const :tag "Create if storing link interactively"
  1199. create-if-interactive)
  1200. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1201. create-if-interactive-and-no-custom-id)
  1202. (const :tag "Only use existing" use-existing)
  1203. (const :tag "Do not use ID to create link" nil)))
  1204. (defcustom org-context-in-file-links t
  1205. "Non-nil means file links from `org-store-link' contain context.
  1206. A search string will be added to the file name with :: as separator and
  1207. used to find the context when the link is activated by the command
  1208. `org-open-at-point'.
  1209. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1210. negates this setting for the duration of the command."
  1211. :group 'org-link-store
  1212. :type 'boolean)
  1213. (defcustom org-keep-stored-link-after-insertion nil
  1214. "Non-nil means keep link in list for entire session.
  1215. The command `org-store-link' adds a link pointing to the current
  1216. location to an internal list. These links accumulate during a session.
  1217. The command `org-insert-link' can be used to insert links into any
  1218. Org-mode file (offering completion for all stored links). When this
  1219. option is nil, every link which has been inserted once using \\[org-insert-link]
  1220. will be removed from the list, to make completing the unused links
  1221. more efficient."
  1222. :group 'org-link-store
  1223. :type 'boolean)
  1224. (defgroup org-link-follow nil
  1225. "Options concerning following links in Org-mode."
  1226. :tag "Org Follow Link"
  1227. :group 'org-link)
  1228. (defcustom org-link-translation-function nil
  1229. "Function to translate links with different syntax to Org syntax.
  1230. This can be used to translate links created for example by the Planner
  1231. or emacs-wiki packages to Org syntax.
  1232. The function must accept two parameters, a TYPE containing the link
  1233. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1234. which is everything after the link protocol. It should return a cons
  1235. with possibly modified values of type and path.
  1236. Org contains a function for this, so if you set this variable to
  1237. `org-translate-link-from-planner', you should be able follow many
  1238. links created by planner."
  1239. :group 'org-link-follow
  1240. :type 'function)
  1241. (defcustom org-follow-link-hook nil
  1242. "Hook that is run after a link has been followed."
  1243. :group 'org-link-follow
  1244. :type 'hook)
  1245. (defcustom org-tab-follows-link nil
  1246. "Non-nil means on links TAB will follow the link.
  1247. Needs to be set before org.el is loaded.
  1248. This really should not be used, it does not make sense, and the
  1249. implementation is bad."
  1250. :group 'org-link-follow
  1251. :type 'boolean)
  1252. (defcustom org-return-follows-link nil
  1253. "Non-nil means on links RET will follow the link."
  1254. :group 'org-link-follow
  1255. :type 'boolean)
  1256. (defcustom org-mouse-1-follows-link
  1257. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1258. "Non-nil means mouse-1 on a link will follow the link.
  1259. A longer mouse click will still set point. Does not work on XEmacs.
  1260. Needs to be set before org.el is loaded."
  1261. :group 'org-link-follow
  1262. :type 'boolean)
  1263. (defcustom org-mark-ring-length 4
  1264. "Number of different positions to be recorded in the ring.
  1265. Changing this requires a restart of Emacs to work correctly."
  1266. :group 'org-link-follow
  1267. :type 'integer)
  1268. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1269. "Non-nil means internal links in Org files must exactly match a headline.
  1270. When nil, the link search tries to match a phrase will all words
  1271. in the search text."
  1272. :group 'org-link-follow
  1273. :type '(choice
  1274. (const :tag "Use fuzy text search" nil)
  1275. (const :tag "Match only exact headline" t)
  1276. (const :tag "Match extact headline or query to create it"
  1277. query-to-create)))
  1278. (defcustom org-link-frame-setup
  1279. '((vm . vm-visit-folder-other-frame)
  1280. (gnus . org-gnus-no-new-news)
  1281. (file . find-file-other-window)
  1282. (wl . wl-other-frame))
  1283. "Setup the frame configuration for following links.
  1284. When following a link with Emacs, it may often be useful to display
  1285. this link in another window or frame. This variable can be used to
  1286. set this up for the different types of links.
  1287. For VM, use any of
  1288. `vm-visit-folder'
  1289. `vm-visit-folder-other-frame'
  1290. For Gnus, use any of
  1291. `gnus'
  1292. `gnus-other-frame'
  1293. `org-gnus-no-new-news'
  1294. For FILE, use any of
  1295. `find-file'
  1296. `find-file-other-window'
  1297. `find-file-other-frame'
  1298. For Wanderlust use any of
  1299. `wl'
  1300. `wl-other-frame'
  1301. For the calendar, use the variable `calendar-setup'.
  1302. For BBDB, it is currently only possible to display the matches in
  1303. another window."
  1304. :group 'org-link-follow
  1305. :type '(list
  1306. (cons (const vm)
  1307. (choice
  1308. (const vm-visit-folder)
  1309. (const vm-visit-folder-other-window)
  1310. (const vm-visit-folder-other-frame)))
  1311. (cons (const gnus)
  1312. (choice
  1313. (const gnus)
  1314. (const gnus-other-frame)
  1315. (const org-gnus-no-new-news)))
  1316. (cons (const file)
  1317. (choice
  1318. (const find-file)
  1319. (const find-file-other-window)
  1320. (const find-file-other-frame)))
  1321. (cons (const wl)
  1322. (choice
  1323. (const wl)
  1324. (const wl-other-frame)))))
  1325. (defcustom org-display-internal-link-with-indirect-buffer nil
  1326. "Non-nil means use indirect buffer to display infile links.
  1327. Activating internal links (from one location in a file to another location
  1328. in the same file) normally just jumps to the location. When the link is
  1329. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1330. is displayed in
  1331. another window. When this option is set, the other window actually displays
  1332. an indirect buffer clone of the current buffer, to avoid any visibility
  1333. changes to the current buffer."
  1334. :group 'org-link-follow
  1335. :type 'boolean)
  1336. (defcustom org-open-non-existing-files nil
  1337. "Non-nil means `org-open-file' will open non-existing files.
  1338. When nil, an error will be generated.
  1339. This variable applies only to external applications because they
  1340. might choke on non-existing files. If the link is to a file that
  1341. will be opened in Emacs, the variable is ignored."
  1342. :group 'org-link-follow
  1343. :type 'boolean)
  1344. (defcustom org-open-directory-means-index-dot-org nil
  1345. "Non-nil means a link to a directory really means to index.org.
  1346. When nil, following a directory link will run dired or open a finder/explorer
  1347. window on that directory."
  1348. :group 'org-link-follow
  1349. :type 'boolean)
  1350. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1351. "Function and arguments to call for following mailto links.
  1352. This is a list with the first element being a Lisp function, and the
  1353. remaining elements being arguments to the function. In string arguments,
  1354. %a will be replaced by the address, and %s will be replaced by the subject
  1355. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1356. :group 'org-link-follow
  1357. :type '(choice
  1358. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1359. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1360. (const :tag "message-mail" (message-mail "%a" "%s"))
  1361. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1362. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1363. "Non-nil means ask for confirmation before executing shell links.
  1364. Shell links can be dangerous: just think about a link
  1365. [[shell:rm -rf ~/*][Google Search]]
  1366. This link would show up in your Org-mode document as \"Google Search\",
  1367. but really it would remove your entire home directory.
  1368. Therefore we advise against setting this variable to nil.
  1369. Just change it to `y-or-n-p' if you want to confirm with a
  1370. single keystroke rather than having to type \"yes\"."
  1371. :group 'org-link-follow
  1372. :type '(choice
  1373. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1374. (const :tag "with y-or-n (faster)" y-or-n-p)
  1375. (const :tag "no confirmation (dangerous)" nil)))
  1376. (put 'org-confirm-shell-link-function
  1377. 'safe-local-variable
  1378. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1379. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1380. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1381. Elisp links can be dangerous: just think about a link
  1382. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1383. This link would show up in your Org-mode document as \"Google Search\",
  1384. but really it would remove your entire home directory.
  1385. Therefore we advise against setting this variable to nil.
  1386. Just change it to `y-or-n-p' if you want to confirm with a
  1387. single keystroke rather than having to type \"yes\"."
  1388. :group 'org-link-follow
  1389. :type '(choice
  1390. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1391. (const :tag "with y-or-n (faster)" y-or-n-p)
  1392. (const :tag "no confirmation (dangerous)" nil)))
  1393. (put 'org-confirm-shell-link-function
  1394. 'safe-local-variable
  1395. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1396. (defconst org-file-apps-defaults-gnu
  1397. '((remote . emacs)
  1398. (system . mailcap)
  1399. (t . mailcap))
  1400. "Default file applications on a UNIX or GNU/Linux system.
  1401. See `org-file-apps'.")
  1402. (defconst org-file-apps-defaults-macosx
  1403. '((remote . emacs)
  1404. (t . "open %s")
  1405. (system . "open %s")
  1406. ("ps.gz" . "gv %s")
  1407. ("eps.gz" . "gv %s")
  1408. ("dvi" . "xdvi %s")
  1409. ("fig" . "xfig %s"))
  1410. "Default file applications on a MacOS X system.
  1411. The system \"open\" is known as a default, but we use X11 applications
  1412. for some files for which the OS does not have a good default.
  1413. See `org-file-apps'.")
  1414. (defconst org-file-apps-defaults-windowsnt
  1415. (list
  1416. '(remote . emacs)
  1417. (cons t
  1418. (list (if (featurep 'xemacs)
  1419. 'mswindows-shell-execute
  1420. 'w32-shell-execute)
  1421. "open" 'file))
  1422. (cons 'system
  1423. (list (if (featurep 'xemacs)
  1424. 'mswindows-shell-execute
  1425. 'w32-shell-execute)
  1426. "open" 'file)))
  1427. "Default file applications on a Windows NT system.
  1428. The system \"open\" is used for most files.
  1429. See `org-file-apps'.")
  1430. (defcustom org-file-apps
  1431. '(
  1432. (auto-mode . emacs)
  1433. ("\\.mm\\'" . default)
  1434. ("\\.x?html?\\'" . default)
  1435. ("\\.pdf\\'" . default)
  1436. )
  1437. "External applications for opening `file:path' items in a document.
  1438. Org-mode uses system defaults for different file types, but
  1439. you can use this variable to set the application for a given file
  1440. extension. The entries in this list are cons cells where the car identifies
  1441. files and the cdr the corresponding command. Possible values for the
  1442. file identifier are
  1443. \"string\" A string as a file identifier can be interpreted in different
  1444. ways, depending on its contents:
  1445. - Alphanumeric characters only:
  1446. Match links with this file extension.
  1447. Example: (\"pdf\" . \"evince %s\")
  1448. to open PDFs with evince.
  1449. - Regular expression: Match links where the
  1450. filename matches the regexp. If you want to
  1451. use groups here, use shy groups.
  1452. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1453. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1454. to open *.html and *.xhtml with firefox.
  1455. - Regular expression which contains (non-shy) groups:
  1456. Match links where the whole link, including \"::\", and
  1457. anything after that, matches the regexp.
  1458. In a custom command string, %1, %2, etc. are replaced with
  1459. the parts of the link that were matched by the groups.
  1460. For backwards compatibility, if a command string is given
  1461. that does not use any of the group matches, this case is
  1462. handled identically to the second one (i.e. match against
  1463. file name only).
  1464. In a custom lisp form, you can access the group matches with
  1465. (match-string n link).
  1466. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1467. to open [[file:document.pdf::5]] with evince at page 5.
  1468. `directory' Matches a directory
  1469. `remote' Matches a remote file, accessible through tramp or efs.
  1470. Remote files most likely should be visited through Emacs
  1471. because external applications cannot handle such paths.
  1472. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1473. so all files Emacs knows how to handle. Using this with
  1474. command `emacs' will open most files in Emacs. Beware that this
  1475. will also open html files inside Emacs, unless you add
  1476. (\"html\" . default) to the list as well.
  1477. t Default for files not matched by any of the other options.
  1478. `system' The system command to open files, like `open' on Windows
  1479. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1480. that will be selected if you call `C-c C-o' with a double
  1481. \\[universal-argument] \\[universal-argument] prefix.
  1482. Possible values for the command are:
  1483. `emacs' The file will be visited by the current Emacs process.
  1484. `default' Use the default application for this file type, which is the
  1485. association for t in the list, most likely in the system-specific
  1486. part.
  1487. This can be used to overrule an unwanted setting in the
  1488. system-specific variable.
  1489. `system' Use the system command for opening files, like \"open\".
  1490. This command is specified by the entry whose car is `system'.
  1491. Most likely, the system-specific version of this variable
  1492. does define this command, but you can overrule/replace it
  1493. here.
  1494. string A command to be executed by a shell; %s will be replaced
  1495. by the path to the file.
  1496. sexp A Lisp form which will be evaluated. The file path will
  1497. be available in the Lisp variable `file'.
  1498. For more examples, see the system specific constants
  1499. `org-file-apps-defaults-macosx'
  1500. `org-file-apps-defaults-windowsnt'
  1501. `org-file-apps-defaults-gnu'."
  1502. :group 'org-link-follow
  1503. :type '(repeat
  1504. (cons (choice :value ""
  1505. (string :tag "Extension")
  1506. (const :tag "System command to open files" system)
  1507. (const :tag "Default for unrecognized files" t)
  1508. (const :tag "Remote file" remote)
  1509. (const :tag "Links to a directory" directory)
  1510. (const :tag "Any files that have Emacs modes"
  1511. auto-mode))
  1512. (choice :value ""
  1513. (const :tag "Visit with Emacs" emacs)
  1514. (const :tag "Use default" default)
  1515. (const :tag "Use the system command" system)
  1516. (string :tag "Command")
  1517. (sexp :tag "Lisp form")))))
  1518. (defgroup org-refile nil
  1519. "Options concerning refiling entries in Org-mode."
  1520. :tag "Org Refile"
  1521. :group 'org)
  1522. (defcustom org-directory "~/org"
  1523. "Directory with org files.
  1524. This is just a default location to look for Org files. There is no need
  1525. at all to put your files into this directory. It is only used in the
  1526. following situations:
  1527. 1. When a remember template specifies a target file that is not an
  1528. absolute path. The path will then be interpreted relative to
  1529. `org-directory'
  1530. 2. When a remember note is filed away in an interactive way (when exiting the
  1531. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1532. with `org-directory' as the default path."
  1533. :group 'org-refile
  1534. :group 'org-remember
  1535. :type 'directory)
  1536. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1537. "Default target for storing notes.
  1538. Used as a fall back file for org-remember.el and org-capture.el, for
  1539. templates that do not specify a target file."
  1540. :group 'org-refile
  1541. :group 'org-remember
  1542. :type '(choice
  1543. (const :tag "Default from remember-data-file" nil)
  1544. file))
  1545. (defcustom org-goto-interface 'outline
  1546. "The default interface to be used for `org-goto'.
  1547. Allowed values are:
  1548. outline The interface shows an outline of the relevant file
  1549. and the correct heading is found by moving through
  1550. the outline or by searching with incremental search.
  1551. outline-path-completion Headlines in the current buffer are offered via
  1552. completion. This is the interface also used by
  1553. the refile command."
  1554. :group 'org-refile
  1555. :type '(choice
  1556. (const :tag "Outline" outline)
  1557. (const :tag "Outline-path-completion" outline-path-completion)))
  1558. (defcustom org-goto-max-level 5
  1559. "Maximum target level when running `org-goto' with refile interface."
  1560. :group 'org-refile
  1561. :type 'integer)
  1562. (defcustom org-reverse-note-order nil
  1563. "Non-nil means store new notes at the beginning of a file or entry.
  1564. When nil, new notes will be filed to the end of a file or entry.
  1565. This can also be a list with cons cells of regular expressions that
  1566. are matched against file names, and values."
  1567. :group 'org-remember
  1568. :group 'org-refile
  1569. :type '(choice
  1570. (const :tag "Reverse always" t)
  1571. (const :tag "Reverse never" nil)
  1572. (repeat :tag "By file name regexp"
  1573. (cons regexp boolean))))
  1574. (defcustom org-log-refile nil
  1575. "Information to record when a task is refiled.
  1576. Possible values are:
  1577. nil Don't add anything
  1578. time Add a time stamp to the task
  1579. note Prompt for a note and add it with template `org-log-note-headings'
  1580. This option can also be set with on a per-file-basis with
  1581. #+STARTUP: nologrefile
  1582. #+STARTUP: logrefile
  1583. #+STARTUP: lognoterefile
  1584. You can have local logging settings for a subtree by setting the LOGGING
  1585. property to one or more of these keywords.
  1586. When bulk-refiling from the agenda, the value `note' is forbidden and
  1587. will temporarily be changed to `time'."
  1588. :group 'org-refile
  1589. :group 'org-progress
  1590. :type '(choice
  1591. (const :tag "No logging" nil)
  1592. (const :tag "Record timestamp" time)
  1593. (const :tag "Record timestamp with note." note)))
  1594. (defcustom org-refile-targets nil
  1595. "Targets for refiling entries with \\[org-refile].
  1596. This is list of cons cells. Each cell contains:
  1597. - a specification of the files to be considered, either a list of files,
  1598. or a symbol whose function or variable value will be used to retrieve
  1599. a file name or a list of file names. If you use `org-agenda-files' for
  1600. that, all agenda files will be scanned for targets. Nil means consider
  1601. headings in the current buffer.
  1602. - A specification of how to find candidate refile targets. This may be
  1603. any of:
  1604. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1605. This tag has to be present in all target headlines, inheritance will
  1606. not be considered.
  1607. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1608. todo keyword.
  1609. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1610. headlines that are refiling targets.
  1611. - a cons cell (:level . N). Any headline of level N is considered a target.
  1612. Note that, when `org-odd-levels-only' is set, level corresponds to
  1613. order in hierarchy, not to the number of stars.
  1614. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1615. Note that, when `org-odd-levels-only' is set, level corresponds to
  1616. order in hierarchy, not to the number of stars.
  1617. You can set the variable `org-refile-target-verify-function' to a function
  1618. to verify each headline found by the simple criteria above.
  1619. When this variable is nil, all top-level headlines in the current buffer
  1620. are used, equivalent to the value `((nil . (:level . 1))'."
  1621. :group 'org-refile
  1622. :type '(repeat
  1623. (cons
  1624. (choice :value org-agenda-files
  1625. (const :tag "All agenda files" org-agenda-files)
  1626. (const :tag "Current buffer" nil)
  1627. (function) (variable) (file))
  1628. (choice :tag "Identify target headline by"
  1629. (cons :tag "Specific tag" (const :value :tag) (string))
  1630. (cons :tag "TODO keyword" (const :value :todo) (string))
  1631. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1632. (cons :tag "Level number" (const :value :level) (integer))
  1633. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1634. (defcustom org-refile-target-verify-function nil
  1635. "Function to verify if the headline at point should be a refile target.
  1636. The function will be called without arguments, with point at the
  1637. beginning of the headline. It should return t and leave point
  1638. where it is if the headline is a valid target for refiling.
  1639. If the target should not be selected, the function must return nil.
  1640. In addition to this, it may move point to a place from where the search
  1641. should be continued. For example, the function may decide that the entire
  1642. subtree of the current entry should be excluded and move point to the end
  1643. of the subtree."
  1644. :group 'org-refile
  1645. :type 'function)
  1646. (defcustom org-refile-use-cache nil
  1647. "Non-nil means cache refile targets to speed up the process.
  1648. The cache for a particular file will be updated automatically when
  1649. the buffer has been killed, or when any of the marker used for flagging
  1650. refile targets no longer points at a live buffer.
  1651. If you have added new entries to a buffer that might themselves be targets,
  1652. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1653. find that easier, `C-u C-u C-u C-c C-w'."
  1654. :group 'org-refile
  1655. :type 'boolean)
  1656. (defcustom org-refile-use-outline-path nil
  1657. "Non-nil means provide refile targets as paths.
  1658. So a level 3 headline will be available as level1/level2/level3.
  1659. When the value is `file', also include the file name (without directory)
  1660. into the path. In this case, you can also stop the completion after
  1661. the file name, to get entries inserted as top level in the file.
  1662. When `full-file-path', include the full file path."
  1663. :group 'org-refile
  1664. :type '(choice
  1665. (const :tag "Not" nil)
  1666. (const :tag "Yes" t)
  1667. (const :tag "Start with file name" file)
  1668. (const :tag "Start with full file path" full-file-path)))
  1669. (defcustom org-outline-path-complete-in-steps t
  1670. "Non-nil means complete the outline path in hierarchical steps.
  1671. When Org-mode uses the refile interface to select an outline path
  1672. \(see variable `org-refile-use-outline-path'), the completion of
  1673. the path can be done is a single go, or if can be done in steps down
  1674. the headline hierarchy. Going in steps is probably the best if you
  1675. do not use a special completion package like `ido' or `icicles'.
  1676. However, when using these packages, going in one step can be very
  1677. fast, while still showing the whole path to the entry."
  1678. :group 'org-refile
  1679. :type 'boolean)
  1680. (defcustom org-refile-allow-creating-parent-nodes nil
  1681. "Non-nil means allow to create new nodes as refile targets.
  1682. New nodes are then created by adding \"/new node name\" to the completion
  1683. of an existing node. When the value of this variable is `confirm',
  1684. new node creation must be confirmed by the user (recommended)
  1685. When nil, the completion must match an existing entry.
  1686. Note that, if the new heading is not seen by the criteria
  1687. listed in `org-refile-targets', multiple instances of the same
  1688. heading would be created by trying again to file under the new
  1689. heading."
  1690. :group 'org-refile
  1691. :type '(choice
  1692. (const :tag "Never" nil)
  1693. (const :tag "Always" t)
  1694. (const :tag "Prompt for confirmation" confirm)))
  1695. (defgroup org-todo nil
  1696. "Options concerning TODO items in Org-mode."
  1697. :tag "Org TODO"
  1698. :group 'org)
  1699. (defgroup org-progress nil
  1700. "Options concerning Progress logging in Org-mode."
  1701. :tag "Org Progress"
  1702. :group 'org-time)
  1703. (defvar org-todo-interpretation-widgets
  1704. '(
  1705. (:tag "Sequence (cycling hits every state)" sequence)
  1706. (:tag "Type (cycling directly to DONE)" type))
  1707. "The available interpretation symbols for customizing `org-todo-keywords'.
  1708. Interested libraries should add to this list.")
  1709. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1710. "List of TODO entry keyword sequences and their interpretation.
  1711. \\<org-mode-map>This is a list of sequences.
  1712. Each sequence starts with a symbol, either `sequence' or `type',
  1713. indicating if the keywords should be interpreted as a sequence of
  1714. action steps, or as different types of TODO items. The first
  1715. keywords are states requiring action - these states will select a headline
  1716. for inclusion into the global TODO list Org-mode produces. If one of
  1717. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1718. signify that no further action is necessary. If \"|\" is not found,
  1719. the last keyword is treated as the only DONE state of the sequence.
  1720. The command \\[org-todo] cycles an entry through these states, and one
  1721. additional state where no keyword is present. For details about this
  1722. cycling, see the manual.
  1723. TODO keywords and interpretation can also be set on a per-file basis with
  1724. the special #+SEQ_TODO and #+TYP_TODO lines.
  1725. Each keyword can optionally specify a character for fast state selection
  1726. \(in combination with the variable `org-use-fast-todo-selection')
  1727. and specifiers for state change logging, using the same syntax
  1728. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1729. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1730. indicates to record a time stamp each time this state is selected.
  1731. Each keyword may also specify if a timestamp or a note should be
  1732. recorded when entering or leaving the state, by adding additional
  1733. characters in the parenthesis after the keyword. This looks like this:
  1734. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1735. record only the time of the state change. With X and Y being either
  1736. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1737. Y when leaving the state if and only if the *target* state does not
  1738. define X. You may omit any of the fast-selection key or X or /Y,
  1739. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1740. For backward compatibility, this variable may also be just a list
  1741. of keywords - in this case the interpretation (sequence or type) will be
  1742. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1743. :group 'org-todo
  1744. :group 'org-keywords
  1745. :type '(choice
  1746. (repeat :tag "Old syntax, just keywords"
  1747. (string :tag "Keyword"))
  1748. (repeat :tag "New syntax"
  1749. (cons
  1750. (choice
  1751. :tag "Interpretation"
  1752. ;;Quick and dirty way to see
  1753. ;;`org-todo-interpretations'. This takes the
  1754. ;;place of item arguments
  1755. :convert-widget
  1756. (lambda (widget)
  1757. (widget-put widget
  1758. :args (mapcar
  1759. #'(lambda (x)
  1760. (widget-convert
  1761. (cons 'const x)))
  1762. org-todo-interpretation-widgets))
  1763. widget))
  1764. (repeat
  1765. (string :tag "Keyword"))))))
  1766. (defvar org-todo-keywords-1 nil
  1767. "All TODO and DONE keywords active in a buffer.")
  1768. (make-variable-buffer-local 'org-todo-keywords-1)
  1769. (defvar org-todo-keywords-for-agenda nil)
  1770. (defvar org-done-keywords-for-agenda nil)
  1771. (defvar org-drawers-for-agenda nil)
  1772. (defvar org-todo-keyword-alist-for-agenda nil)
  1773. (defvar org-tag-alist-for-agenda nil)
  1774. (defvar org-agenda-contributing-files nil)
  1775. (defvar org-not-done-keywords nil)
  1776. (make-variable-buffer-local 'org-not-done-keywords)
  1777. (defvar org-done-keywords nil)
  1778. (make-variable-buffer-local 'org-done-keywords)
  1779. (defvar org-todo-heads nil)
  1780. (make-variable-buffer-local 'org-todo-heads)
  1781. (defvar org-todo-sets nil)
  1782. (make-variable-buffer-local 'org-todo-sets)
  1783. (defvar org-todo-log-states nil)
  1784. (make-variable-buffer-local 'org-todo-log-states)
  1785. (defvar org-todo-kwd-alist nil)
  1786. (make-variable-buffer-local 'org-todo-kwd-alist)
  1787. (defvar org-todo-key-alist nil)
  1788. (make-variable-buffer-local 'org-todo-key-alist)
  1789. (defvar org-todo-key-trigger nil)
  1790. (make-variable-buffer-local 'org-todo-key-trigger)
  1791. (defcustom org-todo-interpretation 'sequence
  1792. "Controls how TODO keywords are interpreted.
  1793. This variable is in principle obsolete and is only used for
  1794. backward compatibility, if the interpretation of todo keywords is
  1795. not given already in `org-todo-keywords'. See that variable for
  1796. more information."
  1797. :group 'org-todo
  1798. :group 'org-keywords
  1799. :type '(choice (const sequence)
  1800. (const type)))
  1801. (defcustom org-use-fast-todo-selection t
  1802. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1803. This variable describes if and under what circumstances the cycling
  1804. mechanism for TODO keywords will be replaced by a single-key, direct
  1805. selection scheme.
  1806. When nil, fast selection is never used.
  1807. When the symbol `prefix', it will be used when `org-todo' is called with
  1808. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1809. in an agenda buffer.
  1810. When t, fast selection is used by default. In this case, the prefix
  1811. argument forces cycling instead.
  1812. In all cases, the special interface is only used if access keys have actually
  1813. been assigned by the user, i.e. if keywords in the configuration are followed
  1814. by a letter in parenthesis, like TODO(t)."
  1815. :group 'org-todo
  1816. :type '(choice
  1817. (const :tag "Never" nil)
  1818. (const :tag "By default" t)
  1819. (const :tag "Only with C-u C-c C-t" prefix)))
  1820. (defcustom org-provide-todo-statistics t
  1821. "Non-nil means update todo statistics after insert and toggle.
  1822. ALL-HEADLINES means update todo statistics by including headlines
  1823. with no TODO keyword as well, counting them as not done.
  1824. A list of TODO keywords means the same, but skip keywords that are
  1825. not in this list.
  1826. When this is set, todo statistics is updated in the parent of the
  1827. current entry each time a todo state is changed."
  1828. :group 'org-todo
  1829. :type '(choice
  1830. (const :tag "Yes, only for TODO entries" t)
  1831. (const :tag "Yes, including all entries" 'all-headlines)
  1832. (repeat :tag "Yes, for TODOs in this list"
  1833. (string :tag "TODO keyword"))
  1834. (other :tag "No TODO statistics" nil)))
  1835. (defcustom org-hierarchical-todo-statistics t
  1836. "Non-nil means TODO statistics covers just direct children.
  1837. When nil, all entries in the subtree are considered.
  1838. This has only an effect if `org-provide-todo-statistics' is set.
  1839. To set this to nil for only a single subtree, use a COOKIE_DATA
  1840. property and include the word \"recursive\" into the value."
  1841. :group 'org-todo
  1842. :type 'boolean)
  1843. (defcustom org-after-todo-state-change-hook nil
  1844. "Hook which is run after the state of a TODO item was changed.
  1845. The new state (a string with a TODO keyword, or nil) is available in the
  1846. Lisp variable `state'."
  1847. :group 'org-todo
  1848. :type 'hook)
  1849. (defvar org-blocker-hook nil
  1850. "Hook for functions that are allowed to block a state change.
  1851. Each function gets as its single argument a property list, see
  1852. `org-trigger-hook' for more information about this list.
  1853. If any of the functions in this hook returns nil, the state change
  1854. is blocked.")
  1855. (defvar org-trigger-hook nil
  1856. "Hook for functions that are triggered by a state change.
  1857. Each function gets as its single argument a property list with at least
  1858. the following elements:
  1859. (:type type-of-change :position pos-at-entry-start
  1860. :from old-state :to new-state)
  1861. Depending on the type, more properties may be present.
  1862. This mechanism is currently implemented for:
  1863. TODO state changes
  1864. ------------------
  1865. :type todo-state-change
  1866. :from previous state (keyword as a string), or nil, or a symbol
  1867. 'todo' or 'done', to indicate the general type of state.
  1868. :to new state, like in :from")
  1869. (defcustom org-enforce-todo-dependencies nil
  1870. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1871. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1872. be blocked if any prior sibling is not yet done.
  1873. Finally, if the parent is blocked because of ordered siblings of its own,
  1874. the child will also be blocked.
  1875. This variable needs to be set before org.el is loaded, and you need to
  1876. restart Emacs after a change to make the change effective. The only way
  1877. to change is while Emacs is running is through the customize interface."
  1878. :set (lambda (var val)
  1879. (set var val)
  1880. (if val
  1881. (add-hook 'org-blocker-hook
  1882. 'org-block-todo-from-children-or-siblings-or-parent)
  1883. (remove-hook 'org-blocker-hook
  1884. 'org-block-todo-from-children-or-siblings-or-parent)))
  1885. :group 'org-todo
  1886. :type 'boolean)
  1887. (defcustom org-enforce-todo-checkbox-dependencies nil
  1888. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1889. When this is nil, checkboxes have no influence on switching TODO states.
  1890. When non-nil, you first need to check off all check boxes before the TODO
  1891. entry can be switched to DONE.
  1892. This variable needs to be set before org.el is loaded, and you need to
  1893. restart Emacs after a change to make the change effective. The only way
  1894. to change is while Emacs is running is through the customize interface."
  1895. :set (lambda (var val)
  1896. (set var val)
  1897. (if val
  1898. (add-hook 'org-blocker-hook
  1899. 'org-block-todo-from-checkboxes)
  1900. (remove-hook 'org-blocker-hook
  1901. 'org-block-todo-from-checkboxes)))
  1902. :group 'org-todo
  1903. :type 'boolean)
  1904. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1905. "Non-nil means inserting a TODO heading is treated as state change.
  1906. So when the command \\[org-insert-todo-heading] is used, state change
  1907. logging will apply if appropriate. When nil, the new TODO item will
  1908. be inserted directly, and no logging will take place."
  1909. :group 'org-todo
  1910. :type 'boolean)
  1911. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1912. "Non-nil means switching TODO states with S-cursor counts as state change.
  1913. This is the default behavior. However, setting this to nil allows a
  1914. convenient way to select a TODO state and bypass any logging associated
  1915. with that."
  1916. :group 'org-todo
  1917. :type 'boolean)
  1918. (defcustom org-todo-state-tags-triggers nil
  1919. "Tag changes that should be triggered by TODO state changes.
  1920. This is a list. Each entry is
  1921. (state-change (tag . flag) .......)
  1922. State-change can be a string with a state, and empty string to indicate the
  1923. state that has no TODO keyword, or it can be one of the symbols `todo'
  1924. or `done', meaning any not-done or done state, respectively."
  1925. :group 'org-todo
  1926. :group 'org-tags
  1927. :type '(repeat
  1928. (cons (choice :tag "When changing to"
  1929. (const :tag "Not-done state" todo)
  1930. (const :tag "Done state" done)
  1931. (string :tag "State"))
  1932. (repeat
  1933. (cons :tag "Tag action"
  1934. (string :tag "Tag")
  1935. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1936. (defcustom org-log-done nil
  1937. "Information to record when a task moves to the DONE state.
  1938. Possible values are:
  1939. nil Don't add anything, just change the keyword
  1940. time Add a time stamp to the task
  1941. note Prompt for a note and add it with template `org-log-note-headings'
  1942. This option can also be set with on a per-file-basis with
  1943. #+STARTUP: nologdone
  1944. #+STARTUP: logdone
  1945. #+STARTUP: lognotedone
  1946. You can have local logging settings for a subtree by setting the LOGGING
  1947. property to one or more of these keywords."
  1948. :group 'org-todo
  1949. :group 'org-progress
  1950. :type '(choice
  1951. (const :tag "No logging" nil)
  1952. (const :tag "Record CLOSED timestamp" time)
  1953. (const :tag "Record CLOSED timestamp with note." note)))
  1954. ;; Normalize old uses of org-log-done.
  1955. (cond
  1956. ((eq org-log-done t) (setq org-log-done 'time))
  1957. ((and (listp org-log-done) (memq 'done org-log-done))
  1958. (setq org-log-done 'note)))
  1959. (defcustom org-log-reschedule nil
  1960. "Information to record when the scheduling date of a tasks is modified.
  1961. Possible values are:
  1962. nil Don't add anything, just change the date
  1963. time Add a time stamp to the task
  1964. note Prompt for a note and add it with template `org-log-note-headings'
  1965. This option can also be set with on a per-file-basis with
  1966. #+STARTUP: nologreschedule
  1967. #+STARTUP: logreschedule
  1968. #+STARTUP: lognotereschedule"
  1969. :group 'org-todo
  1970. :group 'org-progress
  1971. :type '(choice
  1972. (const :tag "No logging" nil)
  1973. (const :tag "Record timestamp" time)
  1974. (const :tag "Record timestamp with note." note)))
  1975. (defcustom org-log-redeadline nil
  1976. "Information to record when the deadline date of a tasks is modified.
  1977. Possible values are:
  1978. nil Don't add anything, just change the date
  1979. time Add a time stamp to the task
  1980. note Prompt for a note and add it with template `org-log-note-headings'
  1981. This option can also be set with on a per-file-basis with
  1982. #+STARTUP: nologredeadline
  1983. #+STARTUP: logredeadline
  1984. #+STARTUP: lognoteredeadline
  1985. You can have local logging settings for a subtree by setting the LOGGING
  1986. property to one or more of these keywords."
  1987. :group 'org-todo
  1988. :group 'org-progress
  1989. :type '(choice
  1990. (const :tag "No logging" nil)
  1991. (const :tag "Record timestamp" time)
  1992. (const :tag "Record timestamp with note." note)))
  1993. (defcustom org-log-note-clock-out nil
  1994. "Non-nil means record a note when clocking out of an item.
  1995. This can also be configured on a per-file basis by adding one of
  1996. the following lines anywhere in the buffer:
  1997. #+STARTUP: lognoteclock-out
  1998. #+STARTUP: nolognoteclock-out"
  1999. :group 'org-todo
  2000. :group 'org-progress
  2001. :type 'boolean)
  2002. (defcustom org-log-done-with-time t
  2003. "Non-nil means the CLOSED time stamp will contain date and time.
  2004. When nil, only the date will be recorded."
  2005. :group 'org-progress
  2006. :type 'boolean)
  2007. (defcustom org-log-note-headings
  2008. '((done . "CLOSING NOTE %t")
  2009. (state . "State %-12s from %-12S %t")
  2010. (note . "Note taken on %t")
  2011. (reschedule . "Rescheduled from %S on %t")
  2012. (delschedule . "Not scheduled, was %S on %t")
  2013. (redeadline . "New deadline from %S on %t")
  2014. (deldeadline . "Removed deadline, was %S on %t")
  2015. (refile . "Refiled on %t")
  2016. (clock-out . ""))
  2017. "Headings for notes added to entries.
  2018. The value is an alist, with the car being a symbol indicating the note
  2019. context, and the cdr is the heading to be used. The heading may also be the
  2020. empty string.
  2021. %t in the heading will be replaced by a time stamp.
  2022. %T will be an active time stamp instead the default inactive one
  2023. %s will be replaced by the new TODO state, in double quotes.
  2024. %S will be replaced by the old TODO state, in double quotes.
  2025. %u will be replaced by the user name.
  2026. %U will be replaced by the full user name.
  2027. In fact, it is not a good idea to change the `state' entry, because
  2028. agenda log mode depends on the format of these entries."
  2029. :group 'org-todo
  2030. :group 'org-progress
  2031. :type '(list :greedy t
  2032. (cons (const :tag "Heading when closing an item" done) string)
  2033. (cons (const :tag
  2034. "Heading when changing todo state (todo sequence only)"
  2035. state) string)
  2036. (cons (const :tag "Heading when just taking a note" note) string)
  2037. (cons (const :tag "Heading when clocking out" clock-out) string)
  2038. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2039. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2040. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2041. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2042. (cons (const :tag "Heading when refiling" refile) string)))
  2043. (unless (assq 'note org-log-note-headings)
  2044. (push '(note . "%t") org-log-note-headings))
  2045. (defcustom org-log-into-drawer nil
  2046. "Non-nil means insert state change notes and time stamps into a drawer.
  2047. When nil, state changes notes will be inserted after the headline and
  2048. any scheduling and clock lines, but not inside a drawer.
  2049. The value of this variable should be the name of the drawer to use.
  2050. LOGBOOK is proposed at the default drawer for this purpose, you can
  2051. also set this to a string to define the drawer of your choice.
  2052. A value of t is also allowed, representing \"LOGBOOK\".
  2053. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2054. will be ignored.
  2055. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2056. a subtree."
  2057. :group 'org-todo
  2058. :group 'org-progress
  2059. :type '(choice
  2060. (const :tag "Not into a drawer" nil)
  2061. (const :tag "LOGBOOK" t)
  2062. (string :tag "Other")))
  2063. (if (fboundp 'defvaralias)
  2064. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2065. (defun org-log-into-drawer ()
  2066. "Return the value of `org-log-into-drawer', but let properties overrule.
  2067. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2068. used instead of the default value."
  2069. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  2070. (cond
  2071. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2072. ((equal p "t") "LOGBOOK")
  2073. (t p))))
  2074. (defcustom org-log-state-notes-insert-after-drawers nil
  2075. "Non-nil means insert state change notes after any drawers in entry.
  2076. Only the drawers that *immediately* follow the headline and the
  2077. deadline/scheduled line are skipped.
  2078. When nil, insert notes right after the heading and perhaps the line
  2079. with deadline/scheduling if present.
  2080. This variable will have no effect if `org-log-into-drawer' is
  2081. set."
  2082. :group 'org-todo
  2083. :group 'org-progress
  2084. :type 'boolean)
  2085. (defcustom org-log-states-order-reversed t
  2086. "Non-nil means the latest state note will be directly after heading.
  2087. When nil, the state change notes will be ordered according to time."
  2088. :group 'org-todo
  2089. :group 'org-progress
  2090. :type 'boolean)
  2091. (defcustom org-todo-repeat-to-state nil
  2092. "The TODO state to which a repeater should return the repeating task.
  2093. By default this is the first task in a TODO sequence, or the previous state
  2094. in a TODO_TYP set. But you can specify another task here.
  2095. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2096. :group 'org-todo
  2097. :type '(choice (const :tag "Head of sequence" nil)
  2098. (string :tag "Specific state")))
  2099. (defcustom org-log-repeat 'time
  2100. "Non-nil means record moving through the DONE state when triggering repeat.
  2101. An auto-repeating task is immediately switched back to TODO when
  2102. marked DONE. If you are not logging state changes (by adding \"@\"
  2103. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2104. record a closing note, there will be no record of the task moving
  2105. through DONE. This variable forces taking a note anyway.
  2106. nil Don't force a record
  2107. time Record a time stamp
  2108. note Record a note
  2109. This option can also be set with on a per-file-basis with
  2110. #+STARTUP: logrepeat
  2111. #+STARTUP: lognoterepeat
  2112. #+STARTUP: nologrepeat
  2113. You can have local logging settings for a subtree by setting the LOGGING
  2114. property to one or more of these keywords."
  2115. :group 'org-todo
  2116. :group 'org-progress
  2117. :type '(choice
  2118. (const :tag "Don't force a record" nil)
  2119. (const :tag "Force recording the DONE state" time)
  2120. (const :tag "Force recording a note with the DONE state" note)))
  2121. (defgroup org-priorities nil
  2122. "Priorities in Org-mode."
  2123. :tag "Org Priorities"
  2124. :group 'org-todo)
  2125. (defcustom org-enable-priority-commands t
  2126. "Non-nil means priority commands are active.
  2127. When nil, these commands will be disabled, so that you never accidentally
  2128. set a priority."
  2129. :group 'org-priorities
  2130. :type 'boolean)
  2131. (defcustom org-highest-priority ?A
  2132. "The highest priority of TODO items. A character like ?A, ?B etc.
  2133. Must have a smaller ASCII number than `org-lowest-priority'."
  2134. :group 'org-priorities
  2135. :type 'character)
  2136. (defcustom org-lowest-priority ?C
  2137. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2138. Must have a larger ASCII number than `org-highest-priority'."
  2139. :group 'org-priorities
  2140. :type 'character)
  2141. (defcustom org-default-priority ?B
  2142. "The default priority of TODO items.
  2143. This is the priority an item get if no explicit priority is given."
  2144. :group 'org-priorities
  2145. :type 'character)
  2146. (defcustom org-priority-start-cycle-with-default t
  2147. "Non-nil means start with default priority when starting to cycle.
  2148. When this is nil, the first step in the cycle will be (depending on the
  2149. command used) one higher or lower that the default priority."
  2150. :group 'org-priorities
  2151. :type 'boolean)
  2152. (defgroup org-time nil
  2153. "Options concerning time stamps and deadlines in Org-mode."
  2154. :tag "Org Time"
  2155. :group 'org)
  2156. (defcustom org-insert-labeled-timestamps-at-point nil
  2157. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2158. When nil, these labeled time stamps are forces into the second line of an
  2159. entry, just after the headline. When scheduling from the global TODO list,
  2160. the time stamp will always be forced into the second line."
  2161. :group 'org-time
  2162. :type 'boolean)
  2163. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2164. "Formats for `format-time-string' which are used for time stamps.
  2165. It is not recommended to change this constant.")
  2166. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2167. "Number of minutes to round time stamps to.
  2168. These are two values, the first applies when first creating a time stamp.
  2169. The second applies when changing it with the commands `S-up' and `S-down'.
  2170. When changing the time stamp, this means that it will change in steps
  2171. of N minutes, as given by the second value.
  2172. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2173. numbers should be factors of 60, so for example 5, 10, 15.
  2174. When this is larger than 1, you can still force an exact time stamp by using
  2175. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2176. and by using a prefix arg to `S-up/down' to specify the exact number
  2177. of minutes to shift."
  2178. :group 'org-time
  2179. :get '(lambda (var) ; Make sure both elements are there
  2180. (if (integerp (default-value var))
  2181. (list (default-value var) 5)
  2182. (default-value var)))
  2183. :type '(list
  2184. (integer :tag "when inserting times")
  2185. (integer :tag "when modifying times")))
  2186. ;; Normalize old customizations of this variable.
  2187. (when (integerp org-time-stamp-rounding-minutes)
  2188. (setq org-time-stamp-rounding-minutes
  2189. (list org-time-stamp-rounding-minutes
  2190. org-time-stamp-rounding-minutes)))
  2191. (defcustom org-display-custom-times nil
  2192. "Non-nil means overlay custom formats over all time stamps.
  2193. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2194. To turn this on on a per-file basis, insert anywhere in the file:
  2195. #+STARTUP: customtime"
  2196. :group 'org-time
  2197. :set 'set-default
  2198. :type 'sexp)
  2199. (make-variable-buffer-local 'org-display-custom-times)
  2200. (defcustom org-time-stamp-custom-formats
  2201. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2202. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2203. These are overlayed over the default ISO format if the variable
  2204. `org-display-custom-times' is set. Time like %H:%M should be at the
  2205. end of the second format. The custom formats are also honored by export
  2206. commands, if custom time display is turned on at the time of export."
  2207. :group 'org-time
  2208. :type 'sexp)
  2209. (defun org-time-stamp-format (&optional long inactive)
  2210. "Get the right format for a time string."
  2211. (let ((f (if long (cdr org-time-stamp-formats)
  2212. (car org-time-stamp-formats))))
  2213. (if inactive
  2214. (concat "[" (substring f 1 -1) "]")
  2215. f)))
  2216. (defcustom org-time-clocksum-format "%d:%02d"
  2217. "The format string used when creating CLOCKSUM lines.
  2218. This is also used when org-mode generates a time duration."
  2219. :group 'org-time
  2220. :type 'string)
  2221. (defcustom org-time-clocksum-use-fractional nil
  2222. "If non-nil, \\[org-clock-display] uses fractional times.
  2223. org-mode generates a time duration."
  2224. :group 'org-time
  2225. :type 'boolean)
  2226. (defcustom org-time-clocksum-fractional-format "%.2f"
  2227. "The format string used when creating CLOCKSUM lines, or when
  2228. org-mode generates a time duration."
  2229. :group 'org-time
  2230. :type 'string)
  2231. (defcustom org-deadline-warning-days 14
  2232. "No. of days before expiration during which a deadline becomes active.
  2233. This variable governs the display in sparse trees and in the agenda.
  2234. When 0 or negative, it means use this number (the absolute value of it)
  2235. even if a deadline has a different individual lead time specified.
  2236. Custom commands can set this variable in the options section."
  2237. :group 'org-time
  2238. :group 'org-agenda-daily/weekly
  2239. :type 'integer)
  2240. (defcustom org-read-date-prefer-future t
  2241. "Non-nil means assume future for incomplete date input from user.
  2242. This affects the following situations:
  2243. 1. The user gives a month but not a year.
  2244. For example, if it is April and you enter \"feb 2\", this will be read
  2245. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2246. 2. The user gives a day, but no month.
  2247. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2248. read this as the third of *next* month. However, if you enter \"17\",
  2249. it will be considered as *this* month.
  2250. If you set this variable to the symbol `time', then also the following
  2251. will work:
  2252. 3. If the user gives a time, but no day. If the time is before now,
  2253. to will be interpreted as tomorrow.
  2254. Currently none of this works for ISO week specifications.
  2255. When this option is nil, the current day, month and year will always be
  2256. used as defaults.
  2257. See also `org-agenda-jump-prefer-future'."
  2258. :group 'org-time
  2259. :type '(choice
  2260. (const :tag "Never" nil)
  2261. (const :tag "Check month and day" t)
  2262. (const :tag "Check month, day, and time" time)))
  2263. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2264. "Should the agenda jump command prefer the future for incomplete dates?
  2265. The default is to do the same as configured in `org-read-date-prefer-future'.
  2266. But you can alse set a deviating value here.
  2267. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2268. :group 'org-agenda
  2269. :group 'org-time
  2270. :type '(choice
  2271. (const :tag "Use org-aread-date-prefer-future"
  2272. org-read-date-prefer-future)
  2273. (const :tag "Never" nil)
  2274. (const :tag "Always" t)))
  2275. (defcustom org-read-date-display-live t
  2276. "Non-nil means display current interpretation of date prompt live.
  2277. This display will be in an overlay, in the minibuffer."
  2278. :group 'org-time
  2279. :type 'boolean)
  2280. (defcustom org-read-date-popup-calendar t
  2281. "Non-nil means pop up a calendar when prompting for a date.
  2282. In the calendar, the date can be selected with mouse-1. However, the
  2283. minibuffer will also be active, and you can simply enter the date as well.
  2284. When nil, only the minibuffer will be available."
  2285. :group 'org-time
  2286. :type 'boolean)
  2287. (if (fboundp 'defvaralias)
  2288. (defvaralias 'org-popup-calendar-for-date-prompt
  2289. 'org-read-date-popup-calendar))
  2290. (defcustom org-read-date-minibuffer-setup-hook nil
  2291. "Hook to be used to set up keys for the date/time interface.
  2292. Add key definitions to `minibuffer-local-map', which will be a temporary
  2293. copy."
  2294. :group 'org-time
  2295. :type 'hook)
  2296. (defcustom org-extend-today-until 0
  2297. "The hour when your day really ends. Must be an integer.
  2298. This has influence for the following applications:
  2299. - When switching the agenda to \"today\". It it is still earlier than
  2300. the time given here, the day recognized as TODAY is actually yesterday.
  2301. - When a date is read from the user and it is still before the time given
  2302. here, the current date and time will be assumed to be yesterday, 23:59.
  2303. Also, timestamps inserted in remember templates follow this rule.
  2304. IMPORTANT: This is a feature whose implementation is and likely will
  2305. remain incomplete. Really, it is only here because past midnight seems to
  2306. be the favorite working time of John Wiegley :-)"
  2307. :group 'org-time
  2308. :type 'integer)
  2309. (defcustom org-edit-timestamp-down-means-later nil
  2310. "Non-nil means S-down will increase the time in a time stamp.
  2311. When nil, S-up will increase."
  2312. :group 'org-time
  2313. :type 'boolean)
  2314. (defcustom org-calendar-follow-timestamp-change t
  2315. "Non-nil means make the calendar window follow timestamp changes.
  2316. When a timestamp is modified and the calendar window is visible, it will be
  2317. moved to the new date."
  2318. :group 'org-time
  2319. :type 'boolean)
  2320. (defgroup org-tags nil
  2321. "Options concerning tags in Org-mode."
  2322. :tag "Org Tags"
  2323. :group 'org)
  2324. (defcustom org-tag-alist nil
  2325. "List of tags allowed in Org-mode files.
  2326. When this list is nil, Org-mode will base TAG input on what is already in the
  2327. buffer.
  2328. The value of this variable is an alist, the car of each entry must be a
  2329. keyword as a string, the cdr may be a character that is used to select
  2330. that tag through the fast-tag-selection interface.
  2331. See the manual for details."
  2332. :group 'org-tags
  2333. :type '(repeat
  2334. (choice
  2335. (cons (string :tag "Tag name")
  2336. (character :tag "Access char"))
  2337. (list :tag "Start radio group"
  2338. (const :startgroup)
  2339. (option (string :tag "Group description")))
  2340. (list :tag "End radio group"
  2341. (const :endgroup)
  2342. (option (string :tag "Group description")))
  2343. (const :tag "New line" (:newline)))))
  2344. (defcustom org-tag-persistent-alist nil
  2345. "List of tags that will always appear in all Org-mode files.
  2346. This is in addition to any in buffer settings or customizations
  2347. of `org-tag-alist'.
  2348. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2349. The value of this variable is an alist, the car of each entry must be a
  2350. keyword as a string, the cdr may be a character that is used to select
  2351. that tag through the fast-tag-selection interface.
  2352. See the manual for details.
  2353. To disable these tags on a per-file basis, insert anywhere in the file:
  2354. #+STARTUP: noptag"
  2355. :group 'org-tags
  2356. :type '(repeat
  2357. (choice
  2358. (cons (string :tag "Tag name")
  2359. (character :tag "Access char"))
  2360. (const :tag "Start radio group" (:startgroup))
  2361. (const :tag "End radio group" (:endgroup))
  2362. (const :tag "New line" (:newline)))))
  2363. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2364. "If non-nil, always offer completion for all tags of all agenda files.
  2365. Instead of customizing this variable directly, you might want to
  2366. set it locally for remember buffers, because there no list of
  2367. tags in that file can be created dynamically (there are none).
  2368. (add-hook 'org-remember-mode-hook
  2369. (lambda ()
  2370. (set (make-local-variable
  2371. 'org-complete-tags-always-offer-all-agenda-tags)
  2372. t)))"
  2373. :group 'org-tags
  2374. :type 'boolean)
  2375. (defvar org-file-tags nil
  2376. "List of tags that can be inherited by all entries in the file.
  2377. The tags will be inherited if the variable `org-use-tag-inheritance'
  2378. says they should be.
  2379. This variable is populated from #+FILETAGS lines.")
  2380. (defcustom org-use-fast-tag-selection 'auto
  2381. "Non-nil means use fast tag selection scheme.
  2382. This is a special interface to select and deselect tags with single keys.
  2383. When nil, fast selection is never used.
  2384. When the symbol `auto', fast selection is used if and only if selection
  2385. characters for tags have been configured, either through the variable
  2386. `org-tag-alist' or through a #+TAGS line in the buffer.
  2387. When t, fast selection is always used and selection keys are assigned
  2388. automatically if necessary."
  2389. :group 'org-tags
  2390. :type '(choice
  2391. (const :tag "Always" t)
  2392. (const :tag "Never" nil)
  2393. (const :tag "When selection characters are configured" 'auto)))
  2394. (defcustom org-fast-tag-selection-single-key nil
  2395. "Non-nil means fast tag selection exits after first change.
  2396. When nil, you have to press RET to exit it.
  2397. During fast tag selection, you can toggle this flag with `C-c'.
  2398. This variable can also have the value `expert'. In this case, the window
  2399. displaying the tags menu is not even shown, until you press C-c again."
  2400. :group 'org-tags
  2401. :type '(choice
  2402. (const :tag "No" nil)
  2403. (const :tag "Yes" t)
  2404. (const :tag "Expert" expert)))
  2405. (defvar org-fast-tag-selection-include-todo nil
  2406. "Non-nil means fast tags selection interface will also offer TODO states.
  2407. This is an undocumented feature, you should not rely on it.")
  2408. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2409. "The column to which tags should be indented in a headline.
  2410. If this number is positive, it specifies the column. If it is negative,
  2411. it means that the tags should be flushright to that column. For example,
  2412. -80 works well for a normal 80 character screen."
  2413. :group 'org-tags
  2414. :type 'integer)
  2415. (defcustom org-auto-align-tags t
  2416. "Non-nil means realign tags after pro/demotion of TODO state change.
  2417. These operations change the length of a headline and therefore shift
  2418. the tags around. With this options turned on, after each such operation
  2419. the tags are again aligned to `org-tags-column'."
  2420. :group 'org-tags
  2421. :type 'boolean)
  2422. (defcustom org-use-tag-inheritance t
  2423. "Non-nil means tags in levels apply also for sublevels.
  2424. When nil, only the tags directly given in a specific line apply there.
  2425. This may also be a list of tags that should be inherited, or a regexp that
  2426. matches tags that should be inherited. Additional control is possible
  2427. with the variable `org-tags-exclude-from-inheritance' which gives an
  2428. explicit list of tags to be excluded from inheritance., even if the value of
  2429. `org-use-tag-inheritance' would select it for inheritance.
  2430. If this option is t, a match early-on in a tree can lead to a large
  2431. number of matches in the subtree when constructing the agenda or creating
  2432. a sparse tree. If you only want to see the first match in a tree during
  2433. a search, check out the variable `org-tags-match-list-sublevels'."
  2434. :group 'org-tags
  2435. :type '(choice
  2436. (const :tag "Not" nil)
  2437. (const :tag "Always" t)
  2438. (repeat :tag "Specific tags" (string :tag "Tag"))
  2439. (regexp :tag "Tags matched by regexp")))
  2440. (defcustom org-tags-exclude-from-inheritance nil
  2441. "List of tags that should never be inherited.
  2442. This is a way to exclude a few tags from inheritance. For way to do
  2443. the opposite, to actively allow inheritance for selected tags,
  2444. see the variable `org-use-tag-inheritance'."
  2445. :group 'org-tags
  2446. :type '(repeat (string :tag "Tag")))
  2447. (defun org-tag-inherit-p (tag)
  2448. "Check if TAG is one that should be inherited."
  2449. (cond
  2450. ((member tag org-tags-exclude-from-inheritance) nil)
  2451. ((eq org-use-tag-inheritance t) t)
  2452. ((not org-use-tag-inheritance) nil)
  2453. ((stringp org-use-tag-inheritance)
  2454. (string-match org-use-tag-inheritance tag))
  2455. ((listp org-use-tag-inheritance)
  2456. (member tag org-use-tag-inheritance))
  2457. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2458. (defcustom org-tags-match-list-sublevels t
  2459. "Non-nil means list also sublevels of headlines matching a search.
  2460. This variable applies to tags/property searches, and also to stuck
  2461. projects because this search is based on a tags match as well.
  2462. When set to the symbol `indented', sublevels are indented with
  2463. leading dots.
  2464. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2465. the sublevels of a headline matching a tag search often also match
  2466. the same search. Listing all of them can create very long lists.
  2467. Setting this variable to nil causes subtrees of a match to be skipped.
  2468. This variable is semi-obsolete and probably should always be true. It
  2469. is better to limit inheritance to certain tags using the variables
  2470. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2471. :group 'org-tags
  2472. :type '(choice
  2473. (const :tag "No, don't list them" nil)
  2474. (const :tag "Yes, do list them" t)
  2475. (const :tag "List them, indented with leading dots" indented)))
  2476. (defcustom org-tags-sort-function nil
  2477. "When set, tags are sorted using this function as a comparator."
  2478. :group 'org-tags
  2479. :type '(choice
  2480. (const :tag "No sorting" nil)
  2481. (const :tag "Alphabetical" string<)
  2482. (const :tag "Reverse alphabetical" string>)
  2483. (function :tag "Custom function" nil)))
  2484. (defvar org-tags-history nil
  2485. "History of minibuffer reads for tags.")
  2486. (defvar org-last-tags-completion-table nil
  2487. "The last used completion table for tags.")
  2488. (defvar org-after-tags-change-hook nil
  2489. "Hook that is run after the tags in a line have changed.")
  2490. (defgroup org-properties nil
  2491. "Options concerning properties in Org-mode."
  2492. :tag "Org Properties"
  2493. :group 'org)
  2494. (defcustom org-property-format "%-10s %s"
  2495. "How property key/value pairs should be formatted by `indent-line'.
  2496. When `indent-line' hits a property definition, it will format the line
  2497. according to this format, mainly to make sure that the values are
  2498. lined-up with respect to each other."
  2499. :group 'org-properties
  2500. :type 'string)
  2501. (defcustom org-use-property-inheritance nil
  2502. "Non-nil means properties apply also for sublevels.
  2503. This setting is chiefly used during property searches. Turning it on can
  2504. cause significant overhead when doing a search, which is why it is not
  2505. on by default.
  2506. When nil, only the properties directly given in the current entry count.
  2507. When t, every property is inherited. The value may also be a list of
  2508. properties that should have inheritance, or a regular expression matching
  2509. properties that should be inherited.
  2510. However, note that some special properties use inheritance under special
  2511. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2512. and the properties ending in \"_ALL\" when they are used as descriptor
  2513. for valid values of a property.
  2514. Note for programmers:
  2515. When querying an entry with `org-entry-get', you can control if inheritance
  2516. should be used. By default, `org-entry-get' looks only at the local
  2517. properties. You can request inheritance by setting the inherit argument
  2518. to t (to force inheritance) or to `selective' (to respect the setting
  2519. in this variable)."
  2520. :group 'org-properties
  2521. :type '(choice
  2522. (const :tag "Not" nil)
  2523. (const :tag "Always" t)
  2524. (repeat :tag "Specific properties" (string :tag "Property"))
  2525. (regexp :tag "Properties matched by regexp")))
  2526. (defun org-property-inherit-p (property)
  2527. "Check if PROPERTY is one that should be inherited."
  2528. (cond
  2529. ((eq org-use-property-inheritance t) t)
  2530. ((not org-use-property-inheritance) nil)
  2531. ((stringp org-use-property-inheritance)
  2532. (string-match org-use-property-inheritance property))
  2533. ((listp org-use-property-inheritance)
  2534. (member property org-use-property-inheritance))
  2535. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2536. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2537. "The default column format, if no other format has been defined.
  2538. This variable can be set on the per-file basis by inserting a line
  2539. #+COLUMNS: %25ITEM ....."
  2540. :group 'org-properties
  2541. :type 'string)
  2542. (defcustom org-columns-ellipses ".."
  2543. "The ellipses to be used when a field in column view is truncated.
  2544. When this is the empty string, as many characters as possible are shown,
  2545. but then there will be no visual indication that the field has been truncated.
  2546. When this is a string of length N, the last N characters of a truncated
  2547. field are replaced by this string. If the column is narrower than the
  2548. ellipses string, only part of the ellipses string will be shown."
  2549. :group 'org-properties
  2550. :type 'string)
  2551. (defcustom org-columns-modify-value-for-display-function nil
  2552. "Function that modifies values for display in column view.
  2553. For example, it can be used to cut out a certain part from a time stamp.
  2554. The function must take 2 arguments:
  2555. column-title The title of the column (*not* the property name)
  2556. value The value that should be modified.
  2557. The function should return the value that should be displayed,
  2558. or nil if the normal value should be used."
  2559. :group 'org-properties
  2560. :type 'function)
  2561. (defcustom org-effort-property "Effort"
  2562. "The property that is being used to keep track of effort estimates.
  2563. Effort estimates given in this property need to have the format H:MM."
  2564. :group 'org-properties
  2565. :group 'org-progress
  2566. :type '(string :tag "Property"))
  2567. (defconst org-global-properties-fixed
  2568. '(("VISIBILITY_ALL" . "folded children content all")
  2569. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2570. "List of property/value pairs that can be inherited by any entry.
  2571. These are fixed values, for the preset properties. The user variable
  2572. that can be used to add to this list is `org-global-properties'.
  2573. The entries in this list are cons cells where the car is a property
  2574. name and cdr is a string with the value. If the value represents
  2575. multiple items like an \"_ALL\" property, separate the items by
  2576. spaces.")
  2577. (defcustom org-global-properties nil
  2578. "List of property/value pairs that can be inherited by any entry.
  2579. This list will be combined with the constant `org-global-properties-fixed'.
  2580. The entries in this list are cons cells where the car is a property
  2581. name and cdr is a string with the value.
  2582. You can set buffer-local values for the same purpose in the variable
  2583. `org-file-properties' this by adding lines like
  2584. #+PROPERTY: NAME VALUE"
  2585. :group 'org-properties
  2586. :type '(repeat
  2587. (cons (string :tag "Property")
  2588. (string :tag "Value"))))
  2589. (defvar org-file-properties nil
  2590. "List of property/value pairs that can be inherited by any entry.
  2591. Valid for the current buffer.
  2592. This variable is populated from #+PROPERTY lines.")
  2593. (make-variable-buffer-local 'org-file-properties)
  2594. (defgroup org-agenda nil
  2595. "Options concerning agenda views in Org-mode."
  2596. :tag "Org Agenda"
  2597. :group 'org)
  2598. (defvar org-category nil
  2599. "Variable used by org files to set a category for agenda display.
  2600. Such files should use a file variable to set it, for example
  2601. # -*- mode: org; org-category: \"ELisp\"
  2602. or contain a special line
  2603. #+CATEGORY: ELisp
  2604. If the file does not specify a category, then file's base name
  2605. is used instead.")
  2606. (make-variable-buffer-local 'org-category)
  2607. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2608. (defcustom org-agenda-files nil
  2609. "The files to be used for agenda display.
  2610. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2611. \\[org-remove-file]. You can also use customize to edit the list.
  2612. If an entry is a directory, all files in that directory that are matched by
  2613. `org-agenda-file-regexp' will be part of the file list.
  2614. If the value of the variable is not a list but a single file name, then
  2615. the list of agenda files is actually stored and maintained in that file, one
  2616. agenda file per line. In this file paths can be given relative to
  2617. `org-directory'. Tilde expansion and environment variable substitution
  2618. are also made."
  2619. :group 'org-agenda
  2620. :type '(choice
  2621. (repeat :tag "List of files and directories" file)
  2622. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2623. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2624. "Regular expression to match files for `org-agenda-files'.
  2625. If any element in the list in that variable contains a directory instead
  2626. of a normal file, all files in that directory that are matched by this
  2627. regular expression will be included."
  2628. :group 'org-agenda
  2629. :type 'regexp)
  2630. (defcustom org-agenda-text-search-extra-files nil
  2631. "List of extra files to be searched by text search commands.
  2632. These files will be search in addition to the agenda files by the
  2633. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2634. Note that these files will only be searched for text search commands,
  2635. not for the other agenda views like todo lists, tag searches or the weekly
  2636. agenda. This variable is intended to list notes and possibly archive files
  2637. that should also be searched by these two commands.
  2638. In fact, if the first element in the list is the symbol `agenda-archives',
  2639. than all archive files of all agenda files will be added to the search
  2640. scope."
  2641. :group 'org-agenda
  2642. :type '(set :greedy t
  2643. (const :tag "Agenda Archives" agenda-archives)
  2644. (repeat :inline t (file))))
  2645. (if (fboundp 'defvaralias)
  2646. (defvaralias 'org-agenda-multi-occur-extra-files
  2647. 'org-agenda-text-search-extra-files))
  2648. (defcustom org-agenda-skip-unavailable-files nil
  2649. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2650. A nil value means to remove them, after a query, from the list."
  2651. :group 'org-agenda
  2652. :type 'boolean)
  2653. (defcustom org-calendar-to-agenda-key [?c]
  2654. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2655. The command `org-calendar-goto-agenda' will be bound to this key. The
  2656. default is the character `c' because then `c' can be used to switch back and
  2657. forth between agenda and calendar."
  2658. :group 'org-agenda
  2659. :type 'sexp)
  2660. (defcustom org-calendar-agenda-action-key [?k]
  2661. "The key to be installed in `calendar-mode-map' for agenda-action.
  2662. The command `org-agenda-action' will be bound to this key. The
  2663. default is the character `k' because we use the same key in the agenda."
  2664. :group 'org-agenda
  2665. :type 'sexp)
  2666. (defcustom org-calendar-insert-diary-entry-key [?i]
  2667. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2668. This option is irrelevant until `org-agenda-diary-file' has been configured
  2669. to point to an Org-mode file. When that is the case, the command
  2670. `org-agenda-diary-entry' will be bound to the key given here, by default
  2671. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2672. if you want to continue doing this, you need to change this to a different
  2673. key."
  2674. :group 'org-agenda
  2675. :type 'sexp)
  2676. (defcustom org-agenda-diary-file 'diary-file
  2677. "File to which to add new entries with the `i' key in agenda and calendar.
  2678. When this is the symbol `diary-file', the functionality in the Emacs
  2679. calendar will be used to add entries to the `diary-file'. But when this
  2680. points to a file, `org-agenda-diary-entry' will be used instead."
  2681. :group 'org-agenda
  2682. :type '(choice
  2683. (const :tag "The standard Emacs diary file" diary-file)
  2684. (file :tag "Special Org file diary entries")))
  2685. (eval-after-load "calendar"
  2686. '(progn
  2687. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2688. 'org-calendar-goto-agenda)
  2689. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2690. 'org-agenda-action)
  2691. (add-hook 'calendar-mode-hook
  2692. (lambda ()
  2693. (unless (eq org-agenda-diary-file 'diary-file)
  2694. (define-key calendar-mode-map
  2695. org-calendar-insert-diary-entry-key
  2696. 'org-agenda-diary-entry))))))
  2697. (defgroup org-latex nil
  2698. "Options for embedding LaTeX code into Org-mode."
  2699. :tag "Org LaTeX"
  2700. :group 'org)
  2701. (defcustom org-format-latex-options
  2702. '(:foreground default :background default :scale 1.0
  2703. :html-foreground "Black" :html-background "Transparent"
  2704. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2705. "Options for creating images from LaTeX fragments.
  2706. This is a property list with the following properties:
  2707. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2708. `default' means use the foreground of the default face.
  2709. :background the background color, or \"Transparent\".
  2710. `default' means use the background of the default face.
  2711. :scale a scaling factor for the size of the images, to get more pixels
  2712. :html-foreground, :html-background, :html-scale
  2713. the same numbers for HTML export.
  2714. :matchers a list indicating which matchers should be used to
  2715. find LaTeX fragments. Valid members of this list are:
  2716. \"begin\" find environments
  2717. \"$1\" find single characters surrounded by $.$
  2718. \"$\" find math expressions surrounded by $...$
  2719. \"$$\" find math expressions surrounded by $$....$$
  2720. \"\\(\" find math expressions surrounded by \\(...\\)
  2721. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2722. :group 'org-latex
  2723. :type 'plist)
  2724. (defcustom org-format-latex-signal-error t
  2725. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2726. When nil, just push out a message."
  2727. :group 'org-latex
  2728. :type 'boolean)
  2729. (defcustom org-format-latex-header "\\documentclass{article}
  2730. \\usepackage[usenames]{color}
  2731. \\usepackage{amsmath}
  2732. \\usepackage[mathscr]{eucal}
  2733. \\pagestyle{empty} % do not remove
  2734. \[PACKAGES]
  2735. \[DEFAULT-PACKAGES]
  2736. % The settings below are copied from fullpage.sty
  2737. \\setlength{\\textwidth}{\\paperwidth}
  2738. \\addtolength{\\textwidth}{-3cm}
  2739. \\setlength{\\oddsidemargin}{1.5cm}
  2740. \\addtolength{\\oddsidemargin}{-2.54cm}
  2741. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2742. \\setlength{\\textheight}{\\paperheight}
  2743. \\addtolength{\\textheight}{-\\headheight}
  2744. \\addtolength{\\textheight}{-\\headsep}
  2745. \\addtolength{\\textheight}{-\\footskip}
  2746. \\addtolength{\\textheight}{-3cm}
  2747. \\setlength{\\topmargin}{1.5cm}
  2748. \\addtolength{\\topmargin}{-2.54cm}"
  2749. "The document header used for processing LaTeX fragments.
  2750. It is imperative that this header make sure that no page number
  2751. appears on the page. The package defined in the variables
  2752. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2753. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2754. will be appended."
  2755. :group 'org-latex
  2756. :type 'string)
  2757. (defvar org-format-latex-header-extra nil)
  2758. (defun org-set-packages-alist (var val)
  2759. "Set the packages alist and make sure it has 3 elements per entry."
  2760. (set var (mapcar (lambda (x)
  2761. (if (and (consp x) (= (length x) 2))
  2762. (list (car x) (nth 1 x) t)
  2763. x))
  2764. val)))
  2765. (defun org-get-packages-alist (var)
  2766. "Get the packages alist and make sure it has 3 elements per entry."
  2767. (mapcar (lambda (x)
  2768. (if (and (consp x) (= (length x) 2))
  2769. (list (car x) (nth 1 x) t)
  2770. x))
  2771. (default-value var)))
  2772. ;; The following variables are defined here because is it also used
  2773. ;; when formatting latex fragments. Originally it was part of the
  2774. ;; LaTeX exporter, which is why the name includes "export".
  2775. (defcustom org-export-latex-default-packages-alist
  2776. '(("AUTO" "inputenc" t)
  2777. ("T1" "fontenc" t)
  2778. ("" "fixltx2e" nil)
  2779. ("" "graphicx" t)
  2780. ("" "longtable" nil)
  2781. ("" "float" nil)
  2782. ("" "wrapfig" nil)
  2783. ("" "soul" t)
  2784. ("" "textcomp" t)
  2785. ("" "marvosym" t)
  2786. ("" "wasysym" t)
  2787. ("" "latexsym" t)
  2788. ("" "amssymb" t)
  2789. ("" "hyperref" nil)
  2790. "\\tolerance=1000"
  2791. )
  2792. "Alist of default packages to be inserted in the header.
  2793. Change this only if one of the packages here causes an incompatibility
  2794. with another package you are using.
  2795. The packages in this list are needed by one part or another of Org-mode
  2796. to function properly.
  2797. - inputenc, fontenc: for basic font and character selection
  2798. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2799. for interpreting the entities in `org-entities'. You can skip some of these
  2800. packages if you don't use any of the symbols in it.
  2801. - graphicx: for including images
  2802. - float, wrapfig: for figure placement
  2803. - longtable: for long tables
  2804. - hyperref: for cross references
  2805. Therefore you should not modify this variable unless you know what you
  2806. are doing. The one reason to change it anyway is that you might be loading
  2807. some other package that conflicts with one of the default packages.
  2808. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2809. If SNIPPET-FLAG is t, the package also needs to be included when
  2810. compiling LaTeX snippets into images for inclusion into HTML."
  2811. :group 'org-export-latex
  2812. :set 'org-set-packages-alist
  2813. :get 'org-get-packages-alist
  2814. :type '(repeat
  2815. (choice
  2816. (list :tag "options/package pair"
  2817. (string :tag "options")
  2818. (string :tag "package")
  2819. (boolean :tag "Snippet"))
  2820. (string :tag "A line of LaTeX"))))
  2821. (defcustom org-export-latex-packages-alist nil
  2822. "Alist of packages to be inserted in every LaTeX header.
  2823. These will be inserted after `org-export-latex-default-packages-alist'.
  2824. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2825. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2826. turning LaTeX snippets into images for inclusion into HTML.
  2827. Make sure that you only list packages here which:
  2828. - you want in every file
  2829. - do not conflict with the default packages in
  2830. `org-export-latex-default-packages-alist'
  2831. - do not conflict with the setup in `org-format-latex-header'."
  2832. :group 'org-export-latex
  2833. :set 'org-set-packages-alist
  2834. :get 'org-get-packages-alist
  2835. :type '(repeat
  2836. (choice
  2837. (list :tag "options/package pair"
  2838. (string :tag "options")
  2839. (string :tag "package")
  2840. (boolean :tag "Snippet"))
  2841. (string :tag "A line of LaTeX"))))
  2842. (defgroup org-appearance nil
  2843. "Settings for Org-mode appearance."
  2844. :tag "Org Appearance"
  2845. :group 'org)
  2846. (defcustom org-level-color-stars-only nil
  2847. "Non-nil means fontify only the stars in each headline.
  2848. When nil, the entire headline is fontified.
  2849. Changing it requires restart of `font-lock-mode' to become effective
  2850. also in regions already fontified."
  2851. :group 'org-appearance
  2852. :type 'boolean)
  2853. (defcustom org-hide-leading-stars nil
  2854. "Non-nil means hide the first N-1 stars in a headline.
  2855. This works by using the face `org-hide' for these stars. This
  2856. face is white for a light background, and black for a dark
  2857. background. You may have to customize the face `org-hide' to
  2858. make this work.
  2859. Changing it requires restart of `font-lock-mode' to become effective
  2860. also in regions already fontified.
  2861. You may also set this on a per-file basis by adding one of the following
  2862. lines to the buffer:
  2863. #+STARTUP: hidestars
  2864. #+STARTUP: showstars"
  2865. :group 'org-appearance
  2866. :type 'boolean)
  2867. (defcustom org-hidden-keywords nil
  2868. "List of keywords that should be hidden when typed in the org buffer.
  2869. For example, add #+TITLE to this list in order to make the
  2870. document title appear in the buffer without the initial #+TITLE:
  2871. keyword."
  2872. :group 'org-appearance
  2873. :type '(set (const :tag "#+AUTHOR" author)
  2874. (const :tag "#+DATE" date)
  2875. (const :tag "#+EMAIL" email)
  2876. (const :tag "#+TITLE" title)))
  2877. (defcustom org-fontify-done-headline nil
  2878. "Non-nil means change the face of a headline if it is marked DONE.
  2879. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2880. When this is non-nil, the headline after the keyword is set to the
  2881. `org-headline-done' as an additional indication."
  2882. :group 'org-appearance
  2883. :type 'boolean)
  2884. (defcustom org-fontify-emphasized-text t
  2885. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2886. Changing this variable requires a restart of Emacs to take effect."
  2887. :group 'org-appearance
  2888. :type 'boolean)
  2889. (defcustom org-fontify-whole-heading-line nil
  2890. "Non-nil means fontify the whole line for headings.
  2891. This is useful when setting a background color for the
  2892. org-level-* faces."
  2893. :group 'org-appearance
  2894. :type 'boolean)
  2895. (defcustom org-highlight-latex-fragments-and-specials nil
  2896. "Non-nil means fontify what is treated specially by the exporters."
  2897. :group 'org-appearance
  2898. :type 'boolean)
  2899. (defcustom org-hide-emphasis-markers nil
  2900. "Non-nil mean font-lock should hide the emphasis marker characters."
  2901. :group 'org-appearance
  2902. :type 'boolean)
  2903. (defcustom org-pretty-entities nil
  2904. "Non-nil means show entities as UTF8 characters.
  2905. When nil, the \\name form remains in the buffer."
  2906. :group 'org-appearance
  2907. :type 'boolean)
  2908. (defcustom org-pretty-entities-include-sub-superscripts t
  2909. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2910. :group 'org-appearance
  2911. :type 'boolean)
  2912. (defvar org-emph-re nil
  2913. "Regular expression for matching emphasis.
  2914. After a match, the match groups contain these elements:
  2915. 0 The match of the full regular expression, including the characters
  2916. before and after the proper match
  2917. 1 The character before the proper match, or empty at beginning of line
  2918. 2 The proper match, including the leading and trailing markers
  2919. 3 The leading marker like * or /, indicating the type of highlighting
  2920. 4 The text between the emphasis markers, not including the markers
  2921. 5 The character after the match, empty at the end of a line")
  2922. (defvar org-verbatim-re nil
  2923. "Regular expression for matching verbatim text.")
  2924. (defvar org-emphasis-regexp-components) ; defined just below
  2925. (defvar org-emphasis-alist) ; defined just below
  2926. (defun org-set-emph-re (var val)
  2927. "Set variable and compute the emphasis regular expression."
  2928. (set var val)
  2929. (when (and (boundp 'org-emphasis-alist)
  2930. (boundp 'org-emphasis-regexp-components)
  2931. org-emphasis-alist org-emphasis-regexp-components)
  2932. (let* ((e org-emphasis-regexp-components)
  2933. (pre (car e))
  2934. (post (nth 1 e))
  2935. (border (nth 2 e))
  2936. (body (nth 3 e))
  2937. (nl (nth 4 e))
  2938. (body1 (concat body "*?"))
  2939. (markers (mapconcat 'car org-emphasis-alist ""))
  2940. (vmarkers (mapconcat
  2941. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2942. org-emphasis-alist "")))
  2943. ;; make sure special characters appear at the right position in the class
  2944. (if (string-match "\\^" markers)
  2945. (setq markers (concat (replace-match "" t t markers) "^")))
  2946. (if (string-match "-" markers)
  2947. (setq markers (concat (replace-match "" t t markers) "-")))
  2948. (if (string-match "\\^" vmarkers)
  2949. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2950. (if (string-match "-" vmarkers)
  2951. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2952. (if (> nl 0)
  2953. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2954. (int-to-string nl) "\\}")))
  2955. ;; Make the regexp
  2956. (setq org-emph-re
  2957. (concat "\\([" pre "]\\|^\\)"
  2958. "\\("
  2959. "\\([" markers "]\\)"
  2960. "\\("
  2961. "[^" border "]\\|"
  2962. "[^" border "]"
  2963. body1
  2964. "[^" border "]"
  2965. "\\)"
  2966. "\\3\\)"
  2967. "\\([" post "]\\|$\\)"))
  2968. (setq org-verbatim-re
  2969. (concat "\\([" pre "]\\|^\\)"
  2970. "\\("
  2971. "\\([" vmarkers "]\\)"
  2972. "\\("
  2973. "[^" border "]\\|"
  2974. "[^" border "]"
  2975. body1
  2976. "[^" border "]"
  2977. "\\)"
  2978. "\\3\\)"
  2979. "\\([" post "]\\|$\\)")))))
  2980. (defcustom org-emphasis-regexp-components
  2981. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2982. "Components used to build the regular expression for emphasis.
  2983. This is a list with 6 entries. Terminology: In an emphasis string
  2984. like \" *strong word* \", we call the initial space PREMATCH, the final
  2985. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2986. and \"trong wor\" is the body. The different components in this variable
  2987. specify what is allowed/forbidden in each part:
  2988. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2989. post Chars allowed as postmatch. End of line will be allowed too.
  2990. border The chars *forbidden* as border characters.
  2991. body-regexp A regexp like \".\" to match a body character. Don't use
  2992. non-shy groups here, and don't allow newline here.
  2993. newline The maximum number of newlines allowed in an emphasis exp.
  2994. Use customize to modify this, or restart Emacs after changing it."
  2995. :group 'org-appearance
  2996. :set 'org-set-emph-re
  2997. :type '(list
  2998. (sexp :tag "Allowed chars in pre ")
  2999. (sexp :tag "Allowed chars in post ")
  3000. (sexp :tag "Forbidden chars in border ")
  3001. (sexp :tag "Regexp for body ")
  3002. (integer :tag "number of newlines allowed")
  3003. (option (boolean :tag "Please ignore this button"))))
  3004. (defcustom org-emphasis-alist
  3005. `(("*" bold "<b>" "</b>")
  3006. ("/" italic "<i>" "</i>")
  3007. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3008. ("=" org-code "<code>" "</code>" verbatim)
  3009. ("~" org-verbatim "<code>" "</code>" verbatim)
  3010. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3011. "<del>" "</del>")
  3012. )
  3013. "Special syntax for emphasized text.
  3014. Text starting and ending with a special character will be emphasized, for
  3015. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3016. characters, the face to be used by font-lock for highlighting in Org-mode
  3017. Emacs buffers, and the HTML tags to be used for this.
  3018. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3019. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3020. Use customize to modify this, or restart Emacs after changing it."
  3021. :group 'org-appearance
  3022. :set 'org-set-emph-re
  3023. :type '(repeat
  3024. (list
  3025. (string :tag "Marker character")
  3026. (choice
  3027. (face :tag "Font-lock-face")
  3028. (plist :tag "Face property list"))
  3029. (string :tag "HTML start tag")
  3030. (string :tag "HTML end tag")
  3031. (option (const verbatim)))))
  3032. (defvar org-protecting-blocks
  3033. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3034. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3035. This is needed for font-lock setup.")
  3036. ;;; Miscellaneous options
  3037. (defgroup org-completion nil
  3038. "Completion in Org-mode."
  3039. :tag "Org Completion"
  3040. :group 'org)
  3041. (defcustom org-completion-use-ido nil
  3042. "Non-nil means use ido completion wherever possible.
  3043. Note that `ido-mode' must be active for this variable to be relevant.
  3044. If you decide to turn this variable on, you might well want to turn off
  3045. `org-outline-path-complete-in-steps'.
  3046. See also `org-completion-use-iswitchb'."
  3047. :group 'org-completion
  3048. :type 'boolean)
  3049. (defcustom org-completion-use-iswitchb nil
  3050. "Non-nil means use iswitchb completion wherever possible.
  3051. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3052. If you decide to turn this variable on, you might well want to turn off
  3053. `org-outline-path-complete-in-steps'.
  3054. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3055. :group 'org-completion
  3056. :type 'boolean)
  3057. (defcustom org-completion-fallback-command 'hippie-expand
  3058. "The expansion command called by \\[org-complete] in normal context.
  3059. Normal means no org-mode-specific context."
  3060. :group 'org-completion
  3061. :type 'function)
  3062. ;;; Functions and variables from their packages
  3063. ;; Declared here to avoid compiler warnings
  3064. ;; XEmacs only
  3065. (defvar outline-mode-menu-heading)
  3066. (defvar outline-mode-menu-show)
  3067. (defvar outline-mode-menu-hide)
  3068. (defvar zmacs-regions) ; XEmacs regions
  3069. ;; Emacs only
  3070. (defvar mark-active)
  3071. ;; Various packages
  3072. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3073. (declare-function calendar-forward-day "cal-move" (arg))
  3074. (declare-function calendar-goto-date "cal-move" (date))
  3075. (declare-function calendar-goto-today "cal-move" ())
  3076. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3077. (defvar calc-embedded-close-formula)
  3078. (defvar calc-embedded-open-formula)
  3079. (declare-function cdlatex-tab "ext:cdlatex" ())
  3080. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3081. (defvar font-lock-unfontify-region-function)
  3082. (declare-function iswitchb-read-buffer "iswitchb"
  3083. (prompt &optional default require-match start matches-set))
  3084. (defvar iswitchb-temp-buflist)
  3085. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3086. (defvar org-agenda-tags-todo-honor-ignore-options)
  3087. (declare-function org-agenda-skip "org-agenda" ())
  3088. (declare-function
  3089. org-format-agenda-item "org-agenda"
  3090. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3091. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3092. (declare-function org-agenda-change-all-lines "org-agenda"
  3093. (newhead hdmarker &optional fixface just-this))
  3094. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3095. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3096. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3097. (beg end))
  3098. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3099. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3100. "org-agenda" (&optional end))
  3101. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3102. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3103. (declare-function org-indent-mode "org-indent" (&optional arg))
  3104. (declare-function parse-time-string "parse-time" (string))
  3105. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3106. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3107. (defvar remember-data-file)
  3108. (defvar texmathp-why)
  3109. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3110. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3111. (defvar w3m-current-url)
  3112. (defvar w3m-current-title)
  3113. (defvar org-latex-regexps)
  3114. ;;; Autoload and prepare some org modules
  3115. ;; Some table stuff that needs to be defined here, because it is used
  3116. ;; by the functions setting up org-mode or checking for table context.
  3117. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3118. "Detect an org-type or table-type table.")
  3119. (defconst org-table-line-regexp "^[ \t]*|"
  3120. "Detect an org-type table line.")
  3121. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3122. "Detect an org-type table line.")
  3123. (defconst org-table-hline-regexp "^[ \t]*|-"
  3124. "Detect an org-type table hline.")
  3125. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3126. "Detect a table-type table hline.")
  3127. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3128. "Detect the first line outside a table when searching from within it.
  3129. This works for both table types.")
  3130. ;; Autoload the functions in org-table.el that are needed by functions here.
  3131. (eval-and-compile
  3132. (org-autoload "org-table"
  3133. '(org-table-align org-table-begin org-table-blank-field
  3134. org-table-convert org-table-convert-region org-table-copy-down
  3135. org-table-copy-region org-table-create
  3136. org-table-create-or-convert-from-region
  3137. org-table-create-with-table.el org-table-current-dline
  3138. org-table-cut-region org-table-delete-column org-table-edit-field
  3139. org-table-edit-formulas org-table-end org-table-eval-formula
  3140. org-table-export org-table-field-info
  3141. org-table-get-stored-formulas org-table-goto-column
  3142. org-table-hline-and-move org-table-import org-table-insert-column
  3143. org-table-insert-hline org-table-insert-row org-table-iterate
  3144. org-table-justify-field-maybe org-table-kill-row
  3145. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3146. org-table-move-column org-table-move-column-left
  3147. org-table-move-column-right org-table-move-row
  3148. org-table-move-row-down org-table-move-row-up
  3149. org-table-next-field org-table-next-row org-table-paste-rectangle
  3150. org-table-previous-field org-table-recalculate
  3151. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3152. org-table-toggle-coordinate-overlays
  3153. org-table-toggle-formula-debugger org-table-wrap-region
  3154. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3155. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3156. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3157. (defun org-at-table-p (&optional table-type)
  3158. "Return t if the cursor is inside an org-type table.
  3159. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3160. (if org-enable-table-editor
  3161. (save-excursion
  3162. (beginning-of-line 1)
  3163. (looking-at (if table-type org-table-any-line-regexp
  3164. org-table-line-regexp)))
  3165. nil))
  3166. (defsubst org-table-p () (org-at-table-p))
  3167. (defun org-at-table.el-p ()
  3168. "Return t if and only if we are at a table.el table."
  3169. (and (org-at-table-p 'any)
  3170. (save-excursion
  3171. (goto-char (org-table-begin 'any))
  3172. (looking-at org-table1-hline-regexp))))
  3173. (defun org-table-recognize-table.el ()
  3174. "If there is a table.el table nearby, recognize it and move into it."
  3175. (if org-table-tab-recognizes-table.el
  3176. (if (org-at-table.el-p)
  3177. (progn
  3178. (beginning-of-line 1)
  3179. (if (looking-at org-table-dataline-regexp)
  3180. nil
  3181. (if (looking-at org-table1-hline-regexp)
  3182. (progn
  3183. (beginning-of-line 2)
  3184. (if (looking-at org-table-any-border-regexp)
  3185. (beginning-of-line -1)))))
  3186. (if (re-search-forward "|" (org-table-end t) t)
  3187. (progn
  3188. (require 'table)
  3189. (if (table--at-cell-p (point))
  3190. t
  3191. (message "recognizing table.el table...")
  3192. (table-recognize-table)
  3193. (message "recognizing table.el table...done")))
  3194. (error "This should not happen"))
  3195. t)
  3196. nil)
  3197. nil))
  3198. (defun org-at-table-hline-p ()
  3199. "Return t if the cursor is inside a hline in a table."
  3200. (if org-enable-table-editor
  3201. (save-excursion
  3202. (beginning-of-line 1)
  3203. (looking-at org-table-hline-regexp))
  3204. nil))
  3205. (defvar org-table-clean-did-remove-column nil)
  3206. (defun org-table-map-tables (function &optional quietly)
  3207. "Apply FUNCTION to the start of all tables in the buffer."
  3208. (save-excursion
  3209. (save-restriction
  3210. (widen)
  3211. (goto-char (point-min))
  3212. (while (re-search-forward org-table-any-line-regexp nil t)
  3213. (unless quietly
  3214. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3215. (beginning-of-line 1)
  3216. (when (looking-at org-table-line-regexp)
  3217. (save-excursion (funcall function))
  3218. (or (looking-at org-table-line-regexp)
  3219. (forward-char 1)))
  3220. (re-search-forward org-table-any-border-regexp nil 1))))
  3221. (unless quietly (message "Mapping tables: done")))
  3222. ;; Declare and autoload functions from org-exp.el & Co
  3223. (declare-function org-default-export-plist "org-exp")
  3224. (declare-function org-infile-export-plist "org-exp")
  3225. (declare-function org-get-current-options "org-exp")
  3226. (eval-and-compile
  3227. (org-autoload "org-exp"
  3228. '(org-export org-export-visible
  3229. org-insert-export-options-template
  3230. org-table-clean-before-export))
  3231. (org-autoload "org-ascii"
  3232. '(org-export-as-ascii org-export-ascii-preprocess
  3233. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3234. org-export-region-as-ascii))
  3235. (org-autoload "org-latex"
  3236. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3237. org-replace-region-by-latex org-export-region-as-latex
  3238. org-export-as-latex org-export-as-pdf
  3239. org-export-as-pdf-and-open))
  3240. (org-autoload "org-html"
  3241. '(org-export-as-html-and-open
  3242. org-export-as-html-batch org-export-as-html-to-buffer
  3243. org-replace-region-by-html org-export-region-as-html
  3244. org-export-as-html))
  3245. (org-autoload "org-docbook"
  3246. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3247. org-replace-region-by-docbook org-export-region-as-docbook
  3248. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3249. org-export-as-docbook))
  3250. (org-autoload "org-icalendar"
  3251. '(org-export-icalendar-this-file
  3252. org-export-icalendar-all-agenda-files
  3253. org-export-icalendar-combine-agenda-files))
  3254. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3255. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3256. ;; Declare and autoload functions from org-agenda.el
  3257. (eval-and-compile
  3258. (org-autoload "org-agenda"
  3259. '(org-agenda org-agenda-list org-search-view
  3260. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3261. org-diary org-agenda-to-appt
  3262. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3263. ;; Autoload org-remember
  3264. (eval-and-compile
  3265. (org-autoload "org-remember"
  3266. '(org-remember-insinuate org-remember-annotation
  3267. org-remember-apply-template org-remember org-remember-handler)))
  3268. (eval-and-compile
  3269. (org-autoload "org-capture"
  3270. '(org-capture org-capture-insert-template-here
  3271. org-capture-import-remember-templates)))
  3272. ;; Autoload org-clock.el
  3273. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3274. (beg end))
  3275. (declare-function org-clock-update-mode-line "org-clock" ())
  3276. (declare-function org-resolve-clocks "org-clock"
  3277. (&optional also-non-dangling-p prompt last-valid))
  3278. (defvar org-clock-start-time)
  3279. (defvar org-clock-marker (make-marker)
  3280. "Marker recording the last clock-in.")
  3281. (defvar org-clock-hd-marker (make-marker)
  3282. "Marker recording the last clock-in, but the headline position.")
  3283. (defvar org-clock-heading ""
  3284. "The heading of the current clock entry.")
  3285. (defun org-clock-is-active ()
  3286. "Return non-nil if clock is currently running.
  3287. The return value is actually the clock marker."
  3288. (marker-buffer org-clock-marker))
  3289. (eval-and-compile
  3290. (org-autoload
  3291. "org-clock"
  3292. '(org-clock-in org-clock-out org-clock-cancel
  3293. org-clock-goto org-clock-sum org-clock-display
  3294. org-clock-remove-overlays org-clock-report
  3295. org-clocktable-shift org-dblock-write:clocktable
  3296. org-get-clocktable org-resolve-clocks)))
  3297. (defun org-clock-update-time-maybe ()
  3298. "If this is a CLOCK line, update it and return t.
  3299. Otherwise, return nil."
  3300. (interactive)
  3301. (save-excursion
  3302. (beginning-of-line 1)
  3303. (skip-chars-forward " \t")
  3304. (when (looking-at org-clock-string)
  3305. (let ((re (concat "[ \t]*" org-clock-string
  3306. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3307. "\\([ \t]*=>.*\\)?\\)?"))
  3308. ts te h m s neg)
  3309. (cond
  3310. ((not (looking-at re))
  3311. nil)
  3312. ((not (match-end 2))
  3313. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3314. (> org-clock-marker (point))
  3315. (<= org-clock-marker (point-at-eol)))
  3316. ;; The clock is running here
  3317. (setq org-clock-start-time
  3318. (apply 'encode-time
  3319. (org-parse-time-string (match-string 1))))
  3320. (org-clock-update-mode-line)))
  3321. (t
  3322. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3323. (end-of-line 1)
  3324. (setq ts (match-string 1)
  3325. te (match-string 3))
  3326. (setq s (- (org-float-time
  3327. (apply 'encode-time (org-parse-time-string te)))
  3328. (org-float-time
  3329. (apply 'encode-time (org-parse-time-string ts))))
  3330. neg (< s 0)
  3331. s (abs s)
  3332. h (floor (/ s 3600))
  3333. s (- s (* 3600 h))
  3334. m (floor (/ s 60))
  3335. s (- s (* 60 s)))
  3336. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3337. t))))))
  3338. (defun org-check-running-clock ()
  3339. "Check if the current buffer contains the running clock.
  3340. If yes, offer to stop it and to save the buffer with the changes."
  3341. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3342. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3343. (buffer-name))))
  3344. (org-clock-out)
  3345. (when (y-or-n-p "Save changed buffer?")
  3346. (save-buffer))))
  3347. (defun org-clocktable-try-shift (dir n)
  3348. "Check if this line starts a clock table, if yes, shift the time block."
  3349. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3350. (org-clocktable-shift dir n)))
  3351. ;; Autoload org-timer.el
  3352. (eval-and-compile
  3353. (org-autoload
  3354. "org-timer"
  3355. '(org-timer-start org-timer org-timer-item
  3356. org-timer-change-times-in-region
  3357. org-timer-set-timer
  3358. org-timer-reset-timers
  3359. org-timer-show-remaining-time)))
  3360. ;; Autoload org-feed.el
  3361. (eval-and-compile
  3362. (org-autoload
  3363. "org-feed"
  3364. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3365. ;; Autoload org-indent.el
  3366. ;; Define the variable already here, to make sure we have it.
  3367. (defvar org-indent-mode nil
  3368. "Non-nil if Org-Indent mode is enabled.
  3369. Use the command `org-indent-mode' to change this variable.")
  3370. (eval-and-compile
  3371. (org-autoload
  3372. "org-indent"
  3373. '(org-indent-mode)))
  3374. ;; Autoload org-mobile.el
  3375. (eval-and-compile
  3376. (org-autoload
  3377. "org-mobile"
  3378. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3379. ;; Autoload archiving code
  3380. ;; The stuff that is needed for cycling and tags has to be defined here.
  3381. (defgroup org-archive nil
  3382. "Options concerning archiving in Org-mode."
  3383. :tag "Org Archive"
  3384. :group 'org-structure)
  3385. (defcustom org-archive-location "%s_archive::"
  3386. "The location where subtrees should be archived.
  3387. The value of this variable is a string, consisting of two parts,
  3388. separated by a double-colon. The first part is a filename and
  3389. the second part is a headline.
  3390. When the filename is omitted, archiving happens in the same file.
  3391. %s in the filename will be replaced by the current file
  3392. name (without the directory part). Archiving to a different file
  3393. is useful to keep archived entries from contributing to the
  3394. Org-mode Agenda.
  3395. The archived entries will be filed as subtrees of the specified
  3396. headline. When the headline is omitted, the subtrees are simply
  3397. filed away at the end of the file, as top-level entries. Also in
  3398. the heading you can use %s to represent the file name, this can be
  3399. useful when using the same archive for a number of different files.
  3400. Here are a few examples:
  3401. \"%s_archive::\"
  3402. If the current file is Projects.org, archive in file
  3403. Projects.org_archive, as top-level trees. This is the default.
  3404. \"::* Archived Tasks\"
  3405. Archive in the current file, under the top-level headline
  3406. \"* Archived Tasks\".
  3407. \"~/org/archive.org::\"
  3408. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3409. \"~/org/archive.org::From %s\"
  3410. Archive in file ~/org/archive.org (absolute path), under headlines
  3411. \"From FILENAME\" where file name is the current file name.
  3412. \"basement::** Finished Tasks\"
  3413. Archive in file ./basement (relative path), as level 3 trees
  3414. below the level 2 heading \"** Finished Tasks\".
  3415. You may set this option on a per-file basis by adding to the buffer a
  3416. line like
  3417. #+ARCHIVE: basement::** Finished Tasks
  3418. You may also define it locally for a subtree by setting an ARCHIVE property
  3419. in the entry. If such a property is found in an entry, or anywhere up
  3420. the hierarchy, it will be used."
  3421. :group 'org-archive
  3422. :type 'string)
  3423. (defcustom org-archive-tag "ARCHIVE"
  3424. "The tag that marks a subtree as archived.
  3425. An archived subtree does not open during visibility cycling, and does
  3426. not contribute to the agenda listings.
  3427. After changing this, font-lock must be restarted in the relevant buffers to
  3428. get the proper fontification."
  3429. :group 'org-archive
  3430. :group 'org-keywords
  3431. :type 'string)
  3432. (defcustom org-agenda-skip-archived-trees t
  3433. "Non-nil means the agenda will skip any items located in archived trees.
  3434. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3435. variable is no longer recommended, you should leave it at the value t.
  3436. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3437. :group 'org-archive
  3438. :group 'org-agenda-skip
  3439. :type 'boolean)
  3440. (defcustom org-columns-skip-archived-trees t
  3441. "Non-nil means ignore archived trees when creating column view."
  3442. :group 'org-archive
  3443. :group 'org-properties
  3444. :type 'boolean)
  3445. (defcustom org-cycle-open-archived-trees nil
  3446. "Non-nil means `org-cycle' will open archived trees.
  3447. An archived tree is a tree marked with the tag ARCHIVE.
  3448. When nil, archived trees will stay folded. You can still open them with
  3449. normal outline commands like `show-all', but not with the cycling commands."
  3450. :group 'org-archive
  3451. :group 'org-cycle
  3452. :type 'boolean)
  3453. (defcustom org-sparse-tree-open-archived-trees nil
  3454. "Non-nil means sparse tree construction shows matches in archived trees.
  3455. When nil, matches in these trees are highlighted, but the trees are kept in
  3456. collapsed state."
  3457. :group 'org-archive
  3458. :group 'org-sparse-trees
  3459. :type 'boolean)
  3460. (defun org-cycle-hide-archived-subtrees (state)
  3461. "Re-hide all archived subtrees after a visibility state change."
  3462. (when (and (not org-cycle-open-archived-trees)
  3463. (not (memq state '(overview folded))))
  3464. (save-excursion
  3465. (let* ((globalp (memq state '(contents all)))
  3466. (beg (if globalp (point-min) (point)))
  3467. (end (if globalp (point-max) (org-end-of-subtree t))))
  3468. (org-hide-archived-subtrees beg end)
  3469. (goto-char beg)
  3470. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3471. (message "%s" (substitute-command-keys
  3472. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3473. (defun org-force-cycle-archived ()
  3474. "Cycle subtree even if it is archived."
  3475. (interactive)
  3476. (setq this-command 'org-cycle)
  3477. (let ((org-cycle-open-archived-trees t))
  3478. (call-interactively 'org-cycle)))
  3479. (defun org-hide-archived-subtrees (beg end)
  3480. "Re-hide all archived subtrees after a visibility state change."
  3481. (save-excursion
  3482. (let* ((re (concat ":" org-archive-tag ":")))
  3483. (goto-char beg)
  3484. (while (re-search-forward re end t)
  3485. (when (org-on-heading-p)
  3486. (org-flag-subtree t)
  3487. (org-end-of-subtree t))))))
  3488. (defun org-flag-subtree (flag)
  3489. (save-excursion
  3490. (org-back-to-heading t)
  3491. (outline-end-of-heading)
  3492. (outline-flag-region (point)
  3493. (progn (org-end-of-subtree t) (point))
  3494. flag)))
  3495. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3496. (eval-and-compile
  3497. (org-autoload "org-archive"
  3498. '(org-add-archive-files org-archive-subtree
  3499. org-archive-to-archive-sibling org-toggle-archive-tag
  3500. org-archive-subtree-default
  3501. org-archive-subtree-default-with-confirmation)))
  3502. ;; Autoload Column View Code
  3503. (declare-function org-columns-number-to-string "org-colview")
  3504. (declare-function org-columns-get-format-and-top-level "org-colview")
  3505. (declare-function org-columns-compute "org-colview")
  3506. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3507. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3508. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3509. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3510. ;; Autoload ID code
  3511. (declare-function org-id-store-link "org-id")
  3512. (declare-function org-id-locations-load "org-id")
  3513. (declare-function org-id-locations-save "org-id")
  3514. (defvar org-id-track-globally)
  3515. (org-autoload "org-id"
  3516. '(org-id-get-create org-id-new org-id-copy org-id-get
  3517. org-id-get-with-outline-path-completion
  3518. org-id-get-with-outline-drilling org-id-store-link
  3519. org-id-goto org-id-find org-id-store-link))
  3520. ;; Autoload Plotting Code
  3521. (org-autoload "org-plot"
  3522. '(org-plot/gnuplot))
  3523. ;;; Variables for pre-computed regular expressions, all buffer local
  3524. (defvar org-drawer-regexp nil
  3525. "Matches first line of a hidden block.")
  3526. (make-variable-buffer-local 'org-drawer-regexp)
  3527. (defvar org-todo-regexp nil
  3528. "Matches any of the TODO state keywords.")
  3529. (make-variable-buffer-local 'org-todo-regexp)
  3530. (defvar org-not-done-regexp nil
  3531. "Matches any of the TODO state keywords except the last one.")
  3532. (make-variable-buffer-local 'org-not-done-regexp)
  3533. (defvar org-not-done-heading-regexp nil
  3534. "Matches a TODO headline that is not done.")
  3535. (make-variable-buffer-local 'org-not-done-regexp)
  3536. (defvar org-todo-line-regexp nil
  3537. "Matches a headline and puts TODO state into group 2 if present.")
  3538. (make-variable-buffer-local 'org-todo-line-regexp)
  3539. (defvar org-complex-heading-regexp nil
  3540. "Matches a headline and puts everything into groups:
  3541. group 1: the stars
  3542. group 2: The todo keyword, maybe
  3543. group 3: Priority cookie
  3544. group 4: True headline
  3545. group 5: Tags")
  3546. (make-variable-buffer-local 'org-complex-heading-regexp)
  3547. (defvar org-complex-heading-regexp-format nil
  3548. "Printf format to make regexp to match an exact headline.
  3549. This regexp will match the headline of any node which hase the exact
  3550. headline text that is put into the format, but may have any TODO state,
  3551. priority and tags.")
  3552. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3553. (defvar org-todo-line-tags-regexp nil
  3554. "Matches a headline and puts TODO state into group 2 if present.
  3555. Also put tags into group 4 if tags are present.")
  3556. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3557. (defvar org-nl-done-regexp nil
  3558. "Matches newline followed by a headline with the DONE keyword.")
  3559. (make-variable-buffer-local 'org-nl-done-regexp)
  3560. (defvar org-looking-at-done-regexp nil
  3561. "Matches the DONE keyword a point.")
  3562. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3563. (defvar org-ds-keyword-length 12
  3564. "Maximum length of the Deadline and SCHEDULED keywords.")
  3565. (make-variable-buffer-local 'org-ds-keyword-length)
  3566. (defvar org-deadline-regexp nil
  3567. "Matches the DEADLINE keyword.")
  3568. (make-variable-buffer-local 'org-deadline-regexp)
  3569. (defvar org-deadline-time-regexp nil
  3570. "Matches the DEADLINE keyword together with a time stamp.")
  3571. (make-variable-buffer-local 'org-deadline-time-regexp)
  3572. (defvar org-deadline-line-regexp nil
  3573. "Matches the DEADLINE keyword and the rest of the line.")
  3574. (make-variable-buffer-local 'org-deadline-line-regexp)
  3575. (defvar org-scheduled-regexp nil
  3576. "Matches the SCHEDULED keyword.")
  3577. (make-variable-buffer-local 'org-scheduled-regexp)
  3578. (defvar org-scheduled-time-regexp nil
  3579. "Matches the SCHEDULED keyword together with a time stamp.")
  3580. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3581. (defvar org-closed-time-regexp nil
  3582. "Matches the CLOSED keyword together with a time stamp.")
  3583. (make-variable-buffer-local 'org-closed-time-regexp)
  3584. (defvar org-keyword-time-regexp nil
  3585. "Matches any of the 4 keywords, together with the time stamp.")
  3586. (make-variable-buffer-local 'org-keyword-time-regexp)
  3587. (defvar org-keyword-time-not-clock-regexp nil
  3588. "Matches any of the 3 keywords, together with the time stamp.")
  3589. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3590. (defvar org-maybe-keyword-time-regexp nil
  3591. "Matches a timestamp, possibly preceded by a keyword.")
  3592. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3593. (defvar org-planning-or-clock-line-re nil
  3594. "Matches a line with planning or clock info.")
  3595. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3596. (defvar org-all-time-keywords nil
  3597. "List of time keywords.")
  3598. (make-variable-buffer-local 'org-all-time-keywords)
  3599. (defconst org-plain-time-of-day-regexp
  3600. (concat
  3601. "\\(\\<[012]?[0-9]"
  3602. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3603. "\\(--?"
  3604. "\\(\\<[012]?[0-9]"
  3605. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3606. "\\)?")
  3607. "Regular expression to match a plain time or time range.
  3608. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3609. groups carry important information:
  3610. 0 the full match
  3611. 1 the first time, range or not
  3612. 8 the second time, if it is a range.")
  3613. (defconst org-plain-time-extension-regexp
  3614. (concat
  3615. "\\(\\<[012]?[0-9]"
  3616. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3617. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3618. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3619. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3620. groups carry important information:
  3621. 0 the full match
  3622. 7 hours of duration
  3623. 9 minutes of duration")
  3624. (defconst org-stamp-time-of-day-regexp
  3625. (concat
  3626. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3627. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3628. "\\(--?"
  3629. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3630. "Regular expression to match a timestamp time or time range.
  3631. After a match, the following groups carry important information:
  3632. 0 the full match
  3633. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3634. 2 the first time, range or not
  3635. 4 the second time, if it is a range.")
  3636. (defconst org-startup-options
  3637. '(("fold" org-startup-folded t)
  3638. ("overview" org-startup-folded t)
  3639. ("nofold" org-startup-folded nil)
  3640. ("showall" org-startup-folded nil)
  3641. ("showeverything" org-startup-folded showeverything)
  3642. ("content" org-startup-folded content)
  3643. ("indent" org-startup-indented t)
  3644. ("noindent" org-startup-indented nil)
  3645. ("hidestars" org-hide-leading-stars t)
  3646. ("showstars" org-hide-leading-stars nil)
  3647. ("odd" org-odd-levels-only t)
  3648. ("oddeven" org-odd-levels-only nil)
  3649. ("align" org-startup-align-all-tables t)
  3650. ("noalign" org-startup-align-all-tables nil)
  3651. ("inlineimages" org-startup-with-inline-images t)
  3652. ("noinlineimages" org-startup-with-inline-images nil)
  3653. ("customtime" org-display-custom-times t)
  3654. ("logdone" org-log-done time)
  3655. ("lognotedone" org-log-done note)
  3656. ("nologdone" org-log-done nil)
  3657. ("lognoteclock-out" org-log-note-clock-out t)
  3658. ("nolognoteclock-out" org-log-note-clock-out nil)
  3659. ("logrepeat" org-log-repeat state)
  3660. ("lognoterepeat" org-log-repeat note)
  3661. ("nologrepeat" org-log-repeat nil)
  3662. ("logreschedule" org-log-reschedule time)
  3663. ("lognotereschedule" org-log-reschedule note)
  3664. ("nologreschedule" org-log-reschedule nil)
  3665. ("logredeadline" org-log-redeadline time)
  3666. ("lognoteredeadline" org-log-redeadline note)
  3667. ("nologredeadline" org-log-redeadline nil)
  3668. ("logrefile" org-log-refile time)
  3669. ("lognoterefile" org-log-refile note)
  3670. ("nologrefile" org-log-refile nil)
  3671. ("fninline" org-footnote-define-inline t)
  3672. ("nofninline" org-footnote-define-inline nil)
  3673. ("fnlocal" org-footnote-section nil)
  3674. ("fnauto" org-footnote-auto-label t)
  3675. ("fnprompt" org-footnote-auto-label nil)
  3676. ("fnconfirm" org-footnote-auto-label confirm)
  3677. ("fnplain" org-footnote-auto-label plain)
  3678. ("fnadjust" org-footnote-auto-adjust t)
  3679. ("nofnadjust" org-footnote-auto-adjust nil)
  3680. ("constcgs" constants-unit-system cgs)
  3681. ("constSI" constants-unit-system SI)
  3682. ("noptag" org-tag-persistent-alist nil)
  3683. ("hideblocks" org-hide-block-startup t)
  3684. ("nohideblocks" org-hide-block-startup nil)
  3685. ("beamer" org-startup-with-beamer-mode t)
  3686. ("entitiespretty" org-pretty-entities t)
  3687. ("entitiesplain" org-pretty-entities nil))
  3688. "Variable associated with STARTUP options for org-mode.
  3689. Each element is a list of three items: The startup options as written
  3690. in the #+STARTUP line, the corresponding variable, and the value to
  3691. set this variable to if the option is found. An optional forth element PUSH
  3692. means to push this value onto the list in the variable.")
  3693. (defun org-set-regexps-and-options ()
  3694. "Precompute regular expressions for current buffer."
  3695. (when (org-mode-p)
  3696. (org-set-local 'org-todo-kwd-alist nil)
  3697. (org-set-local 'org-todo-key-alist nil)
  3698. (org-set-local 'org-todo-key-trigger nil)
  3699. (org-set-local 'org-todo-keywords-1 nil)
  3700. (org-set-local 'org-done-keywords nil)
  3701. (org-set-local 'org-todo-heads nil)
  3702. (org-set-local 'org-todo-sets nil)
  3703. (org-set-local 'org-todo-log-states nil)
  3704. (org-set-local 'org-file-properties nil)
  3705. (org-set-local 'org-file-tags nil)
  3706. (let ((re (org-make-options-regexp
  3707. '("CATEGORY" "TODO" "COLUMNS"
  3708. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3709. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3710. "OPTIONS")
  3711. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3712. (splitre "[ \t]+")
  3713. (scripts org-use-sub-superscripts)
  3714. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3715. tail sep kws1 prio props ftags drawers beamer-p
  3716. ext-setup-or-nil setup-contents (start 0))
  3717. (save-excursion
  3718. (save-restriction
  3719. (widen)
  3720. (goto-char (point-min))
  3721. (while (or (and ext-setup-or-nil
  3722. (string-match re ext-setup-or-nil start)
  3723. (setq start (match-end 0)))
  3724. (and (setq ext-setup-or-nil nil start 0)
  3725. (re-search-forward re nil t)))
  3726. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3727. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3728. (if (stringp value) (setq value (org-trim value)))
  3729. (cond
  3730. ((equal key "CATEGORY")
  3731. (setq cat value))
  3732. ((member key '("SEQ_TODO" "TODO"))
  3733. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3734. ((equal key "TYP_TODO")
  3735. (push (cons 'type (org-split-string value splitre)) kwds))
  3736. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3737. ;; general TODO-like setup
  3738. (push (cons (intern (downcase (match-string 1 key)))
  3739. (org-split-string value splitre)) kwds))
  3740. ((equal key "TAGS")
  3741. (setq tags (append tags (if tags '("\\n") nil)
  3742. (org-split-string value splitre))))
  3743. ((equal key "COLUMNS")
  3744. (org-set-local 'org-columns-default-format value))
  3745. ((equal key "LINK")
  3746. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3747. (push (cons (match-string 1 value)
  3748. (org-trim (match-string 2 value)))
  3749. links)))
  3750. ((equal key "PRIORITIES")
  3751. (setq prio (org-split-string value " +")))
  3752. ((equal key "PROPERTY")
  3753. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3754. (push (cons (match-string 1 value) (match-string 2 value))
  3755. props)))
  3756. ((equal key "FILETAGS")
  3757. (when (string-match "\\S-" value)
  3758. (setq ftags
  3759. (append
  3760. ftags
  3761. (apply 'append
  3762. (mapcar (lambda (x) (org-split-string x ":"))
  3763. (org-split-string value)))))))
  3764. ((equal key "DRAWERS")
  3765. (setq drawers (org-split-string value splitre)))
  3766. ((equal key "CONSTANTS")
  3767. (setq const (append const (org-split-string value splitre))))
  3768. ((equal key "STARTUP")
  3769. (let ((opts (org-split-string value splitre))
  3770. l var val)
  3771. (while (setq l (pop opts))
  3772. (when (setq l (assoc l org-startup-options))
  3773. (setq var (nth 1 l) val (nth 2 l))
  3774. (if (not (nth 3 l))
  3775. (set (make-local-variable var) val)
  3776. (if (not (listp (symbol-value var)))
  3777. (set (make-local-variable var) nil))
  3778. (set (make-local-variable var) (symbol-value var))
  3779. (add-to-list var val))))))
  3780. ((equal key "ARCHIVE")
  3781. (setq arch value)
  3782. (remove-text-properties 0 (length arch)
  3783. '(face t fontified t) arch))
  3784. ((equal key "LATEX_CLASS")
  3785. (setq beamer-p (equal value "beamer")))
  3786. ((equal key "OPTIONS")
  3787. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3788. (setq scripts (read (match-string 2 value)))))
  3789. ((equal key "SETUPFILE")
  3790. (setq setup-contents (org-file-contents
  3791. (expand-file-name
  3792. (org-remove-double-quotes value))
  3793. 'noerror))
  3794. (if (not ext-setup-or-nil)
  3795. (setq ext-setup-or-nil setup-contents start 0)
  3796. (setq ext-setup-or-nil
  3797. (concat (substring ext-setup-or-nil 0 start)
  3798. "\n" setup-contents "\n"
  3799. (substring ext-setup-or-nil start)))))
  3800. ))))
  3801. (org-set-local 'org-use-sub-superscripts scripts)
  3802. (when cat
  3803. (org-set-local 'org-category (intern cat))
  3804. (push (cons "CATEGORY" cat) props))
  3805. (when prio
  3806. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3807. (setq prio (mapcar 'string-to-char prio))
  3808. (org-set-local 'org-highest-priority (nth 0 prio))
  3809. (org-set-local 'org-lowest-priority (nth 1 prio))
  3810. (org-set-local 'org-default-priority (nth 2 prio)))
  3811. (and props (org-set-local 'org-file-properties (nreverse props)))
  3812. (and ftags (org-set-local 'org-file-tags
  3813. (mapcar 'org-add-prop-inherited ftags)))
  3814. (and drawers (org-set-local 'org-drawers drawers))
  3815. (and arch (org-set-local 'org-archive-location arch))
  3816. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3817. ;; Process the TODO keywords
  3818. (unless kwds
  3819. ;; Use the global values as if they had been given locally.
  3820. (setq kwds (default-value 'org-todo-keywords))
  3821. (if (stringp (car kwds))
  3822. (setq kwds (list (cons org-todo-interpretation
  3823. (default-value 'org-todo-keywords)))))
  3824. (setq kwds (reverse kwds)))
  3825. (setq kwds (nreverse kwds))
  3826. (let (inter kws kw)
  3827. (while (setq kws (pop kwds))
  3828. (let ((kws (or
  3829. (run-hook-with-args-until-success
  3830. 'org-todo-setup-filter-hook kws)
  3831. kws)))
  3832. (setq inter (pop kws) sep (member "|" kws)
  3833. kws0 (delete "|" (copy-sequence kws))
  3834. kwsa nil
  3835. kws1 (mapcar
  3836. (lambda (x)
  3837. ;; 1 2
  3838. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3839. (progn
  3840. (setq kw (match-string 1 x)
  3841. key (and (match-end 2) (match-string 2 x))
  3842. log (org-extract-log-state-settings x))
  3843. (push (cons kw (and key (string-to-char key))) kwsa)
  3844. (and log (push log org-todo-log-states))
  3845. kw)
  3846. (error "Invalid TODO keyword %s" x)))
  3847. kws0)
  3848. kwsa (if kwsa (append '((:startgroup))
  3849. (nreverse kwsa)
  3850. '((:endgroup))))
  3851. hw (car kws1)
  3852. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3853. tail (list inter hw (car dws) (org-last dws))))
  3854. (add-to-list 'org-todo-heads hw 'append)
  3855. (push kws1 org-todo-sets)
  3856. (setq org-done-keywords (append org-done-keywords dws nil))
  3857. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3858. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3859. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3860. (setq org-todo-sets (nreverse org-todo-sets)
  3861. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3862. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3863. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3864. ;; Process the constants
  3865. (when const
  3866. (let (e cst)
  3867. (while (setq e (pop const))
  3868. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3869. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3870. (setq org-table-formula-constants-local cst)))
  3871. ;; Process the tags.
  3872. (when tags
  3873. (let (e tgs)
  3874. (while (setq e (pop tags))
  3875. (cond
  3876. ((equal e "{") (push '(:startgroup) tgs))
  3877. ((equal e "}") (push '(:endgroup) tgs))
  3878. ((equal e "\\n") (push '(:newline) tgs))
  3879. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3880. (push (cons (match-string 1 e)
  3881. (string-to-char (match-string 2 e)))
  3882. tgs))
  3883. (t (push (list e) tgs))))
  3884. (org-set-local 'org-tag-alist nil)
  3885. (while (setq e (pop tgs))
  3886. (or (and (stringp (car e))
  3887. (assoc (car e) org-tag-alist))
  3888. (push e org-tag-alist)))))
  3889. ;; Compute the regular expressions and other local variables
  3890. (if (not org-done-keywords)
  3891. (setq org-done-keywords (and org-todo-keywords-1
  3892. (list (org-last org-todo-keywords-1)))))
  3893. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3894. (length org-scheduled-string)
  3895. (length org-clock-string)
  3896. (length org-closed-string)))
  3897. org-drawer-regexp
  3898. (concat "^[ \t]*:\\("
  3899. (mapconcat 'regexp-quote org-drawers "\\|")
  3900. "\\):[ \t]*$")
  3901. org-not-done-keywords
  3902. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3903. org-todo-regexp
  3904. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3905. "\\|") "\\)\\>")
  3906. org-not-done-regexp
  3907. (concat "\\<\\("
  3908. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3909. "\\)\\>")
  3910. org-not-done-heading-regexp
  3911. (concat "^\\(\\*+\\)[ \t]+\\("
  3912. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3913. "\\)\\>")
  3914. org-todo-line-regexp
  3915. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3916. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3917. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3918. org-complex-heading-regexp
  3919. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3920. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3921. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3922. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  3923. org-complex-heading-regexp-format
  3924. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3925. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3926. "\\)\\>\\)?"
  3927. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3928. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3929. "[ \t]*\\(%s\\)"
  3930. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3931. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  3932. org-nl-done-regexp
  3933. (concat "\n\\*+[ \t]+"
  3934. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3935. "\\)" "\\>")
  3936. org-todo-line-tags-regexp
  3937. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3938. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3939. (org-re
  3940. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  3941. org-looking-at-done-regexp
  3942. (concat "^" "\\(?:"
  3943. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3944. "\\>")
  3945. org-deadline-regexp (concat "\\<" org-deadline-string)
  3946. org-deadline-time-regexp
  3947. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3948. org-deadline-line-regexp
  3949. (concat "\\<\\(" org-deadline-string "\\).*")
  3950. org-scheduled-regexp
  3951. (concat "\\<" org-scheduled-string)
  3952. org-scheduled-time-regexp
  3953. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3954. org-closed-time-regexp
  3955. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3956. org-keyword-time-regexp
  3957. (concat "\\<\\(" org-scheduled-string
  3958. "\\|" org-deadline-string
  3959. "\\|" org-closed-string
  3960. "\\|" org-clock-string "\\)"
  3961. " *[[<]\\([^]>]+\\)[]>]")
  3962. org-keyword-time-not-clock-regexp
  3963. (concat "\\<\\(" org-scheduled-string
  3964. "\\|" org-deadline-string
  3965. "\\|" org-closed-string
  3966. "\\)"
  3967. " *[[<]\\([^]>]+\\)[]>]")
  3968. org-maybe-keyword-time-regexp
  3969. (concat "\\(\\<\\(" org-scheduled-string
  3970. "\\|" org-deadline-string
  3971. "\\|" org-closed-string
  3972. "\\|" org-clock-string "\\)\\)?"
  3973. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3974. org-planning-or-clock-line-re
  3975. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3976. "\\|" org-deadline-string
  3977. "\\|" org-closed-string "\\|" org-clock-string
  3978. "\\)\\>\\)")
  3979. org-all-time-keywords
  3980. (mapcar (lambda (w) (substring w 0 -1))
  3981. (list org-scheduled-string org-deadline-string
  3982. org-clock-string org-closed-string))
  3983. )
  3984. (org-compute-latex-and-specials-regexp)
  3985. (org-set-font-lock-defaults))))
  3986. (defun org-file-contents (file &optional noerror)
  3987. "Return the contents of FILE, as a string."
  3988. (if (or (not file)
  3989. (not (file-readable-p file)))
  3990. (if noerror
  3991. (progn
  3992. (message "Cannot read file \"%s\"" file)
  3993. (ding) (sit-for 2)
  3994. "")
  3995. (error "Cannot read file \"%s\"" file))
  3996. (with-temp-buffer
  3997. (insert-file-contents file)
  3998. (buffer-string))))
  3999. (defun org-extract-log-state-settings (x)
  4000. "Extract the log state setting from a TODO keyword string.
  4001. This will extract info from a string like \"WAIT(w@/!)\"."
  4002. (let (kw key log1 log2)
  4003. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4004. (setq kw (match-string 1 x)
  4005. key (and (match-end 2) (match-string 2 x))
  4006. log1 (and (match-end 3) (match-string 3 x))
  4007. log2 (and (match-end 4) (match-string 4 x)))
  4008. (and (or log1 log2)
  4009. (list kw
  4010. (and log1 (if (equal log1 "!") 'time 'note))
  4011. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4012. (defun org-remove-keyword-keys (list)
  4013. "Remove a pair of parenthesis at the end of each string in LIST."
  4014. (mapcar (lambda (x)
  4015. (if (string-match "(.*)$" x)
  4016. (substring x 0 (match-beginning 0))
  4017. x))
  4018. list))
  4019. (defun org-assign-fast-keys (alist)
  4020. "Assign fast keys to a keyword-key alist.
  4021. Respect keys that are already there."
  4022. (let (new e (alt ?0))
  4023. (while (setq e (pop alist))
  4024. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4025. (cdr e)) ;; Key already assigned.
  4026. (push e new)
  4027. (let ((clist (string-to-list (downcase (car e))))
  4028. (used (append new alist)))
  4029. (when (= (car clist) ?@)
  4030. (pop clist))
  4031. (while (and clist (rassoc (car clist) used))
  4032. (pop clist))
  4033. (unless clist
  4034. (while (rassoc alt used)
  4035. (incf alt)))
  4036. (push (cons (car e) (or (car clist) alt)) new))))
  4037. (nreverse new)))
  4038. ;;; Some variables used in various places
  4039. (defvar org-window-configuration nil
  4040. "Used in various places to store a window configuration.")
  4041. (defvar org-selected-window nil
  4042. "Used in various places to store a window configuration.")
  4043. (defvar org-finish-function nil
  4044. "Function to be called when `C-c C-c' is used.
  4045. This is for getting out of special buffers like remember.")
  4046. ;; FIXME: Occasionally check by commenting these, to make sure
  4047. ;; no other functions uses these, forgetting to let-bind them.
  4048. (defvar entry)
  4049. (defvar last-state)
  4050. (defvar date)
  4051. ;; Defined somewhere in this file, but used before definition.
  4052. (defvar org-entities) ;; defined in org-entities.el
  4053. (defvar org-struct-menu)
  4054. (defvar org-org-menu)
  4055. (defvar org-tbl-menu)
  4056. ;;;; Define the Org-mode
  4057. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4058. (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"))
  4059. ;; We use a before-change function to check if a table might need
  4060. ;; an update.
  4061. (defvar org-table-may-need-update t
  4062. "Indicates that a table might need an update.
  4063. This variable is set by `org-before-change-function'.
  4064. `org-table-align' sets it back to nil.")
  4065. (defun org-before-change-function (beg end)
  4066. "Every change indicates that a table might need an update."
  4067. (setq org-table-may-need-update t))
  4068. (defvar org-mode-map)
  4069. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4070. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4071. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4072. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4073. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4074. (defvar org-table-buffer-is-an nil)
  4075. (defconst org-outline-regexp "\\*+ ")
  4076. ;;;###autoload
  4077. (define-derived-mode org-mode outline-mode "Org"
  4078. "Outline-based notes management and organizer, alias
  4079. \"Carsten's outline-mode for keeping track of everything.\"
  4080. Org-mode develops organizational tasks around a NOTES file which
  4081. contains information about projects as plain text. Org-mode is
  4082. implemented on top of outline-mode, which is ideal to keep the content
  4083. of large files well structured. It supports ToDo items, deadlines and
  4084. time stamps, which magically appear in the diary listing of the Emacs
  4085. calendar. Tables are easily created with a built-in table editor.
  4086. Plain text URL-like links connect to websites, emails (VM), Usenet
  4087. messages (Gnus), BBDB entries, and any files related to the project.
  4088. For printing and sharing of notes, an Org-mode file (or a part of it)
  4089. can be exported as a structured ASCII or HTML file.
  4090. The following commands are available:
  4091. \\{org-mode-map}"
  4092. ;; Get rid of Outline menus, they are not needed
  4093. ;; Need to do this here because define-derived-mode sets up
  4094. ;; the keymap so late. Still, it is a waste to call this each time
  4095. ;; we switch another buffer into org-mode.
  4096. (if (featurep 'xemacs)
  4097. (when (boundp 'outline-mode-menu-heading)
  4098. ;; Assume this is Greg's port, it uses easymenu
  4099. (easy-menu-remove outline-mode-menu-heading)
  4100. (easy-menu-remove outline-mode-menu-show)
  4101. (easy-menu-remove outline-mode-menu-hide))
  4102. (define-key org-mode-map [menu-bar headings] 'undefined)
  4103. (define-key org-mode-map [menu-bar hide] 'undefined)
  4104. (define-key org-mode-map [menu-bar show] 'undefined))
  4105. (org-load-modules-maybe)
  4106. (easy-menu-add org-org-menu)
  4107. (easy-menu-add org-tbl-menu)
  4108. (org-install-agenda-files-menu)
  4109. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4110. (add-to-invisibility-spec '(org-cwidth))
  4111. (add-to-invisibility-spec '(org-hide-block . t))
  4112. (when (featurep 'xemacs)
  4113. (org-set-local 'line-move-ignore-invisible t))
  4114. (org-set-local 'outline-regexp org-outline-regexp)
  4115. (org-set-local 'outline-level 'org-outline-level)
  4116. (when (and org-ellipsis
  4117. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4118. (fboundp 'make-glyph-code))
  4119. (unless org-display-table
  4120. (setq org-display-table (make-display-table)))
  4121. (set-display-table-slot
  4122. org-display-table 4
  4123. (vconcat (mapcar
  4124. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4125. org-ellipsis)))
  4126. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4127. (setq buffer-display-table org-display-table))
  4128. (org-set-regexps-and-options)
  4129. (when (and org-tag-faces (not org-tags-special-faces-re))
  4130. ;; tag faces set outside customize.... force initialization.
  4131. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4132. ;; Calc embedded
  4133. (org-set-local 'calc-embedded-open-mode "# ")
  4134. (modify-syntax-entry ?@ "w")
  4135. (if org-startup-truncated (setq truncate-lines t))
  4136. (org-set-local 'font-lock-unfontify-region-function
  4137. 'org-unfontify-region)
  4138. ;; Activate before-change-function
  4139. (org-set-local 'org-table-may-need-update t)
  4140. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4141. 'local)
  4142. ;; Check for running clock before killing a buffer
  4143. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4144. ;; Paragraphs and auto-filling
  4145. (org-set-autofill-regexps)
  4146. (setq indent-line-function 'org-indent-line-function)
  4147. (org-update-radio-target-regexp)
  4148. ;; Beginning/end of defun
  4149. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4150. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4151. ;; Make sure dependence stuff works reliably, even for users who set it
  4152. ;; too late :-(
  4153. (if org-enforce-todo-dependencies
  4154. (add-hook 'org-blocker-hook
  4155. 'org-block-todo-from-children-or-siblings-or-parent)
  4156. (remove-hook 'org-blocker-hook
  4157. 'org-block-todo-from-children-or-siblings-or-parent))
  4158. (if org-enforce-todo-checkbox-dependencies
  4159. (add-hook 'org-blocker-hook
  4160. 'org-block-todo-from-checkboxes)
  4161. (remove-hook 'org-blocker-hook
  4162. 'org-block-todo-from-checkboxes))
  4163. ;; Comment characters
  4164. (org-set-local 'comment-start "#")
  4165. (org-set-local 'comment-padding " ")
  4166. ;; Align options lines
  4167. (org-set-local
  4168. 'align-mode-rules-list
  4169. '((org-in-buffer-settings
  4170. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4171. (modes . '(org-mode)))))
  4172. ;; Imenu
  4173. (org-set-local 'imenu-create-index-function
  4174. 'org-imenu-get-tree)
  4175. ;; Make isearch reveal context
  4176. (if (or (featurep 'xemacs)
  4177. (not (boundp 'outline-isearch-open-invisible-function)))
  4178. ;; Emacs 21 and XEmacs make use of the hook
  4179. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4180. ;; Emacs 22 deals with this through a special variable
  4181. (org-set-local 'outline-isearch-open-invisible-function
  4182. (lambda (&rest ignore) (org-show-context 'isearch))))
  4183. ;; Turn on org-beamer-mode?
  4184. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4185. ;; If empty file that did not turn on org-mode automatically, make it to.
  4186. (if (and org-insert-mode-line-in-empty-file
  4187. (interactive-p)
  4188. (= (point-min) (point-max)))
  4189. (insert "# -*- mode: org -*-\n\n"))
  4190. (unless org-inhibit-startup
  4191. (when org-startup-align-all-tables
  4192. (let ((bmp (buffer-modified-p)))
  4193. (org-table-map-tables 'org-table-align 'quietly)
  4194. (set-buffer-modified-p bmp)))
  4195. (when org-startup-with-inline-images
  4196. (org-display-inline-images))
  4197. (when org-startup-indented
  4198. (require 'org-indent)
  4199. (org-indent-mode 1))
  4200. (unless org-inhibit-startup-visibility-stuff
  4201. (org-set-startup-visibility))))
  4202. (when (fboundp 'abbrev-table-put)
  4203. (abbrev-table-put org-mode-abbrev-table
  4204. :parents (list text-mode-abbrev-table)))
  4205. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4206. (defun org-current-time ()
  4207. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4208. (if (> (car org-time-stamp-rounding-minutes) 1)
  4209. (let ((r (car org-time-stamp-rounding-minutes))
  4210. (time (decode-time)))
  4211. (apply 'encode-time
  4212. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4213. (nthcdr 2 time))))
  4214. (current-time)))
  4215. ;;;; Font-Lock stuff, including the activators
  4216. (defvar org-mouse-map (make-sparse-keymap))
  4217. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4218. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4219. (when org-mouse-1-follows-link
  4220. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4221. (when org-tab-follows-link
  4222. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4223. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4224. (require 'font-lock)
  4225. (defconst org-non-link-chars "]\t\n\r<>")
  4226. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4227. "shell" "elisp" "doi" "message"))
  4228. (defvar org-link-types-re nil
  4229. "Matches a link that has a url-like prefix like \"http:\"")
  4230. (defvar org-link-re-with-space nil
  4231. "Matches a link with spaces, optional angular brackets around it.")
  4232. (defvar org-link-re-with-space2 nil
  4233. "Matches a link with spaces, optional angular brackets around it.")
  4234. (defvar org-link-re-with-space3 nil
  4235. "Matches a link with spaces, only for internal part in bracket links.")
  4236. (defvar org-angle-link-re nil
  4237. "Matches link with angular brackets, spaces are allowed.")
  4238. (defvar org-plain-link-re nil
  4239. "Matches plain link, without spaces.")
  4240. (defvar org-bracket-link-regexp nil
  4241. "Matches a link in double brackets.")
  4242. (defvar org-bracket-link-analytic-regexp nil
  4243. "Regular expression used to analyze links.
  4244. Here is what the match groups contain after a match:
  4245. 1: http:
  4246. 2: http
  4247. 3: path
  4248. 4: [desc]
  4249. 5: desc")
  4250. (defvar org-bracket-link-analytic-regexp++ nil
  4251. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4252. (defvar org-any-link-re nil
  4253. "Regular expression matching any link.")
  4254. (defcustom org-match-sexp-depth 3
  4255. "Number of stacked braces for sub/superscript matching.
  4256. This has to be set before loading org.el to be effective."
  4257. :group 'org-export-translation ; ??????????????????????????/
  4258. :type 'integer)
  4259. (defun org-create-multibrace-regexp (left right n)
  4260. "Create a regular expression which will match a balanced sexp.
  4261. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4262. as single character strings.
  4263. The regexp returned will match the entire expression including the
  4264. delimiters. It will also define a single group which contains the
  4265. match except for the outermost delimiters. The maximum depth of
  4266. stacked delimiters is N. Escaping delimiters is not possible."
  4267. (let* ((nothing (concat "[^" left right "]*?"))
  4268. (or "\\|")
  4269. (re nothing)
  4270. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4271. (while (> n 1)
  4272. (setq n (1- n)
  4273. re (concat re or next)
  4274. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4275. (concat left "\\(" re "\\)" right)))
  4276. (defvar org-match-substring-regexp
  4277. (concat
  4278. "\\([^\\]\\)\\([_^]\\)\\("
  4279. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4280. "\\|"
  4281. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4282. "\\|"
  4283. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4284. "The regular expression matching a sub- or superscript.")
  4285. (defvar org-match-substring-with-braces-regexp
  4286. (concat
  4287. "\\([^\\]\\)\\([_^]\\)\\("
  4288. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4289. "\\)")
  4290. "The regular expression matching a sub- or superscript, forcing braces.")
  4291. (defun org-make-link-regexps ()
  4292. "Update the link regular expressions.
  4293. This should be called after the variable `org-link-types' has changed."
  4294. (setq org-link-types-re
  4295. (concat
  4296. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4297. org-link-re-with-space
  4298. (concat
  4299. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4300. "\\([^" org-non-link-chars " ]"
  4301. "[^" org-non-link-chars "]*"
  4302. "[^" org-non-link-chars " ]\\)>?")
  4303. org-link-re-with-space2
  4304. (concat
  4305. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4306. "\\([^" org-non-link-chars " ]"
  4307. "[^\t\n\r]*"
  4308. "[^" org-non-link-chars " ]\\)>?")
  4309. org-link-re-with-space3
  4310. (concat
  4311. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4312. "\\([^" org-non-link-chars " ]"
  4313. "[^\t\n\r]*\\)")
  4314. org-angle-link-re
  4315. (concat
  4316. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4317. "\\([^" org-non-link-chars " ]"
  4318. "[^" org-non-link-chars "]*"
  4319. "\\)>")
  4320. org-plain-link-re
  4321. (concat
  4322. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4323. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4324. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4325. org-bracket-link-regexp
  4326. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4327. org-bracket-link-analytic-regexp
  4328. (concat
  4329. "\\[\\["
  4330. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4331. "\\([^]]+\\)"
  4332. "\\]"
  4333. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4334. "\\]")
  4335. org-bracket-link-analytic-regexp++
  4336. (concat
  4337. "\\[\\["
  4338. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4339. "\\([^]]+\\)"
  4340. "\\]"
  4341. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4342. "\\]")
  4343. org-any-link-re
  4344. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4345. org-angle-link-re "\\)\\|\\("
  4346. org-plain-link-re "\\)")))
  4347. (org-make-link-regexps)
  4348. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4349. "Regular expression for fast time stamp matching.")
  4350. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4351. "Regular expression for fast time stamp matching.")
  4352. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4353. "Regular expression matching time strings for analysis.
  4354. This one does not require the space after the date, so it can be used
  4355. on a string that terminates immediately after the date.")
  4356. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4357. "Regular expression matching time strings for analysis.")
  4358. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4359. "Regular expression matching time stamps, with groups.")
  4360. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4361. "Regular expression matching time stamps (also [..]), with groups.")
  4362. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4363. "Regular expression matching a time stamp range.")
  4364. (defconst org-tr-regexp-both
  4365. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4366. "Regular expression matching a time stamp range.")
  4367. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4368. org-ts-regexp "\\)?")
  4369. "Regular expression matching a time stamp or time stamp range.")
  4370. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4371. org-ts-regexp-both "\\)?")
  4372. "Regular expression matching a time stamp or time stamp range.
  4373. The time stamps may be either active or inactive.")
  4374. (defvar org-emph-face nil)
  4375. (defun org-do-emphasis-faces (limit)
  4376. "Run through the buffer and add overlays to links."
  4377. (let (rtn a)
  4378. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4379. (if (not (= (char-after (match-beginning 3))
  4380. (char-after (match-beginning 4))))
  4381. (progn
  4382. (setq rtn t)
  4383. (setq a (assoc (match-string 3) org-emphasis-alist))
  4384. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4385. 'face
  4386. (nth 1 a))
  4387. (and (nth 4 a)
  4388. (org-remove-flyspell-overlays-in
  4389. (match-beginning 0) (match-end 0)))
  4390. (add-text-properties (match-beginning 2) (match-end 2)
  4391. '(font-lock-multiline t org-emphasis t))
  4392. (when org-hide-emphasis-markers
  4393. (add-text-properties (match-end 4) (match-beginning 5)
  4394. '(invisible org-link))
  4395. (add-text-properties (match-beginning 3) (match-end 3)
  4396. '(invisible org-link)))))
  4397. (backward-char 1))
  4398. rtn))
  4399. (defun org-emphasize (&optional char)
  4400. "Insert or change an emphasis, i.e. a font like bold or italic.
  4401. If there is an active region, change that region to a new emphasis.
  4402. If there is no region, just insert the marker characters and position
  4403. the cursor between them.
  4404. CHAR should be either the marker character, or the first character of the
  4405. HTML tag associated with that emphasis. If CHAR is a space, the means
  4406. to remove the emphasis of the selected region.
  4407. If char is not given (for example in an interactive call) it
  4408. will be prompted for."
  4409. (interactive)
  4410. (let ((eal org-emphasis-alist) e det
  4411. (erc org-emphasis-regexp-components)
  4412. (prompt "")
  4413. (string "") beg end move tag c s)
  4414. (if (org-region-active-p)
  4415. (setq beg (region-beginning) end (region-end)
  4416. string (buffer-substring beg end))
  4417. (setq move t))
  4418. (while (setq e (pop eal))
  4419. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4420. c (aref tag 0))
  4421. (push (cons c (string-to-char (car e))) det)
  4422. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4423. (substring tag 1)))))
  4424. (setq det (nreverse det))
  4425. (unless char
  4426. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4427. (setq char (read-char-exclusive)))
  4428. (setq char (or (cdr (assoc char det)) char))
  4429. (if (equal char ?\ )
  4430. (setq s "" move nil)
  4431. (unless (assoc (char-to-string char) org-emphasis-alist)
  4432. (error "No such emphasis marker: \"%c\"" char))
  4433. (setq s (char-to-string char)))
  4434. (while (and (> (length string) 1)
  4435. (equal (substring string 0 1) (substring string -1))
  4436. (assoc (substring string 0 1) org-emphasis-alist))
  4437. (setq string (substring string 1 -1)))
  4438. (setq string (concat s string s))
  4439. (if beg (delete-region beg end))
  4440. (unless (or (bolp)
  4441. (string-match (concat "[" (nth 0 erc) "\n]")
  4442. (char-to-string (char-before (point)))))
  4443. (insert " "))
  4444. (unless (or (eobp)
  4445. (string-match (concat "[" (nth 1 erc) "\n]")
  4446. (char-to-string (char-after (point)))))
  4447. (insert " ") (backward-char 1))
  4448. (insert string)
  4449. (and move (backward-char 1))))
  4450. (defconst org-nonsticky-props
  4451. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4452. (defsubst org-rear-nonsticky-at (pos)
  4453. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4454. (defun org-activate-plain-links (limit)
  4455. "Run through the buffer and add overlays to links."
  4456. (catch 'exit
  4457. (let (f)
  4458. (if (re-search-forward org-plain-link-re limit t)
  4459. (progn
  4460. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4461. (setq f (get-text-property (match-beginning 0) 'face))
  4462. (if (or (eq f 'org-tag)
  4463. (and (listp f) (memq 'org-tag f)))
  4464. nil
  4465. (add-text-properties (match-beginning 0) (match-end 0)
  4466. (list 'mouse-face 'highlight
  4467. 'face 'org-link
  4468. 'keymap org-mouse-map))
  4469. (org-rear-nonsticky-at (match-end 0)))
  4470. t)))))
  4471. (defun org-activate-code (limit)
  4472. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4473. (progn
  4474. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4475. (remove-text-properties (match-beginning 0) (match-end 0)
  4476. '(display t invisible t intangible t))
  4477. t)))
  4478. (defcustom org-src-fontify-natively nil
  4479. "When non-nil, fontify code in code blocks."
  4480. :type 'boolean
  4481. :group 'org-appearance
  4482. :group 'org-babel)
  4483. (defun org-fontify-meta-lines-and-blocks (limit)
  4484. "Fontify #+ lines and blocks, in the correct ways."
  4485. (let ((case-fold-search t))
  4486. (if (re-search-forward
  4487. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4488. limit t)
  4489. (let ((beg (match-beginning 0))
  4490. (block-start (match-end 0))
  4491. (block-end nil)
  4492. (lang (match-string 7))
  4493. (beg1 (line-beginning-position 2))
  4494. (dc1 (downcase (match-string 2)))
  4495. (dc3 (downcase (match-string 3)))
  4496. end end1 quoting block-type)
  4497. (cond
  4498. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4499. ;; a single line of backend-specific content
  4500. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4501. (remove-text-properties (match-beginning 0) (match-end 0)
  4502. '(display t invisible t intangible t))
  4503. (add-text-properties (match-beginning 1) (match-end 3)
  4504. '(font-lock-fontified t face org-meta-line))
  4505. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4506. '(font-lock-fontified t face org-block))
  4507. ; for backend-specific code
  4508. t)
  4509. ((and (match-end 4) (equal dc3 "begin"))
  4510. ;; Truly a block
  4511. (setq block-type (downcase (match-string 5))
  4512. quoting (member block-type org-protecting-blocks))
  4513. (when (re-search-forward
  4514. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4515. nil t) ;; on purpose, we look further than LIMIT
  4516. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4517. (setq block-end (match-beginning 0))
  4518. (when quoting
  4519. (remove-text-properties beg end
  4520. '(display t invisible t intangible t)))
  4521. (add-text-properties
  4522. beg end
  4523. '(font-lock-fontified t font-lock-multiline t))
  4524. (add-text-properties beg beg1 '(face org-meta-line))
  4525. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4526. ; for end_src
  4527. (cond
  4528. ((and lang org-src-fontify-natively)
  4529. (org-src-font-lock-fontify-block lang block-start block-end))
  4530. (quoting
  4531. (add-text-properties beg1 (+ end1 1) '(face
  4532. org-block)))
  4533. ; end of source block
  4534. ((not org-fontify-quote-and-verse-blocks))
  4535. ((string= block-type "quote")
  4536. (add-text-properties beg1 end1 '(face org-quote)))
  4537. ((string= block-type "verse")
  4538. (add-text-properties beg1 end1 '(face org-verse))))
  4539. t))
  4540. ((member dc1 '("title:" "author:" "email:" "date:"))
  4541. (add-text-properties
  4542. beg (match-end 3)
  4543. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4544. '(font-lock-fontified t invisible t)
  4545. '(font-lock-fontified t face org-document-info-keyword)))
  4546. (add-text-properties
  4547. (match-beginning 6) (match-end 6)
  4548. (if (string-equal dc1 "title:")
  4549. '(font-lock-fontified t face org-document-title)
  4550. '(font-lock-fontified t face org-document-info))))
  4551. ((not (member (char-after beg) '(?\ ?\t)))
  4552. ;; just any other in-buffer setting, but not indented
  4553. (add-text-properties
  4554. beg (match-end 0)
  4555. '(font-lock-fontified t face org-meta-line))
  4556. t)
  4557. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4558. "orgtbl:" "tblfm:" "tblname:" "result:"
  4559. "results:" "source:" "srcname:" "call:"))
  4560. (and (match-end 4) (equal dc3 "attr")))
  4561. (add-text-properties
  4562. beg (match-end 0)
  4563. '(font-lock-fontified t face org-meta-line))
  4564. t)
  4565. ((member dc3 '(" " ""))
  4566. (add-text-properties
  4567. beg (match-end 0)
  4568. '(font-lock-fontified t face font-lock-comment-face)))
  4569. (t nil))))))
  4570. (defun org-activate-angle-links (limit)
  4571. "Run through the buffer and add overlays to links."
  4572. (if (re-search-forward org-angle-link-re limit t)
  4573. (progn
  4574. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4575. (add-text-properties (match-beginning 0) (match-end 0)
  4576. (list 'mouse-face 'highlight
  4577. 'keymap org-mouse-map))
  4578. (org-rear-nonsticky-at (match-end 0))
  4579. t)))
  4580. (defun org-activate-footnote-links (limit)
  4581. "Run through the buffer and add overlays to links."
  4582. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4583. limit t)
  4584. (progn
  4585. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4586. (add-text-properties (match-beginning 2) (match-end 2)
  4587. (list 'mouse-face 'highlight
  4588. 'keymap org-mouse-map
  4589. 'help-echo
  4590. (if (= (point-at-bol) (match-beginning 2))
  4591. "Footnote definition"
  4592. "Footnote reference")
  4593. ))
  4594. (org-rear-nonsticky-at (match-end 2))
  4595. t)))
  4596. (defun org-activate-bracket-links (limit)
  4597. "Run through the buffer and add overlays to bracketed links."
  4598. (if (re-search-forward org-bracket-link-regexp limit t)
  4599. (let* ((help (concat "LINK: "
  4600. (org-match-string-no-properties 1)))
  4601. ;; FIXME: above we should remove the escapes.
  4602. ;; but that requires another match, protecting match data,
  4603. ;; a lot of overhead for font-lock.
  4604. (ip (org-maybe-intangible
  4605. (list 'invisible 'org-link
  4606. 'keymap org-mouse-map 'mouse-face 'highlight
  4607. 'font-lock-multiline t 'help-echo help)))
  4608. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4609. 'font-lock-multiline t 'help-echo help)))
  4610. ;; We need to remove the invisible property here. Table narrowing
  4611. ;; may have made some of this invisible.
  4612. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4613. (remove-text-properties (match-beginning 0) (match-end 0)
  4614. '(invisible nil))
  4615. (if (match-end 3)
  4616. (progn
  4617. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4618. (org-rear-nonsticky-at (match-beginning 3))
  4619. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4620. (org-rear-nonsticky-at (match-end 3))
  4621. (add-text-properties (match-end 3) (match-end 0) ip)
  4622. (org-rear-nonsticky-at (match-end 0)))
  4623. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4624. (org-rear-nonsticky-at (match-beginning 1))
  4625. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4626. (org-rear-nonsticky-at (match-end 1))
  4627. (add-text-properties (match-end 1) (match-end 0) ip)
  4628. (org-rear-nonsticky-at (match-end 0)))
  4629. t)))
  4630. (defun org-activate-dates (limit)
  4631. "Run through the buffer and add overlays to dates."
  4632. (if (re-search-forward org-tsr-regexp-both limit t)
  4633. (progn
  4634. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4635. (add-text-properties (match-beginning 0) (match-end 0)
  4636. (list 'mouse-face 'highlight
  4637. 'keymap org-mouse-map))
  4638. (org-rear-nonsticky-at (match-end 0))
  4639. (when org-display-custom-times
  4640. (if (match-end 3)
  4641. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4642. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4643. t)))
  4644. (defvar org-target-link-regexp nil
  4645. "Regular expression matching radio targets in plain text.")
  4646. (make-variable-buffer-local 'org-target-link-regexp)
  4647. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4648. "Regular expression matching a link target.")
  4649. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4650. "Regular expression matching a radio target.")
  4651. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4652. "Regular expression matching any target.")
  4653. (defun org-activate-target-links (limit)
  4654. "Run through the buffer and add overlays to target matches."
  4655. (when org-target-link-regexp
  4656. (let ((case-fold-search t))
  4657. (if (re-search-forward org-target-link-regexp limit t)
  4658. (progn
  4659. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4660. (add-text-properties (match-beginning 0) (match-end 0)
  4661. (list 'mouse-face 'highlight
  4662. 'keymap org-mouse-map
  4663. 'help-echo "Radio target link"
  4664. 'org-linked-text t))
  4665. (org-rear-nonsticky-at (match-end 0))
  4666. t)))))
  4667. (defun org-update-radio-target-regexp ()
  4668. "Find all radio targets in this file and update the regular expression."
  4669. (interactive)
  4670. (when (memq 'radio org-activate-links)
  4671. (setq org-target-link-regexp
  4672. (org-make-target-link-regexp (org-all-targets 'radio)))
  4673. (org-restart-font-lock)))
  4674. (defun org-hide-wide-columns (limit)
  4675. (let (s e)
  4676. (setq s (text-property-any (point) (or limit (point-max))
  4677. 'org-cwidth t))
  4678. (when s
  4679. (setq e (next-single-property-change s 'org-cwidth))
  4680. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4681. (goto-char e)
  4682. t)))
  4683. (defvar org-latex-and-specials-regexp nil
  4684. "Regular expression for highlighting export special stuff.")
  4685. (defvar org-match-substring-regexp)
  4686. (defvar org-match-substring-with-braces-regexp)
  4687. ;; This should be with the exporter code, but we also use if for font-locking
  4688. (defconst org-export-html-special-string-regexps
  4689. '(("\\\\-" . "&shy;")
  4690. ("---\\([^-]\\)" . "&mdash;\\1")
  4691. ("--\\([^-]\\)" . "&ndash;\\1")
  4692. ("\\.\\.\\." . "&hellip;"))
  4693. "Regular expressions for special string conversion.")
  4694. (defun org-compute-latex-and-specials-regexp ()
  4695. "Compute regular expression for stuff treated specially by exporters."
  4696. (if (not org-highlight-latex-fragments-and-specials)
  4697. (org-set-local 'org-latex-and-specials-regexp nil)
  4698. (require 'org-exp)
  4699. (let*
  4700. ((matchers (plist-get org-format-latex-options :matchers))
  4701. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4702. org-latex-regexps)))
  4703. (org-export-allow-BIND nil)
  4704. (options (org-combine-plists (org-default-export-plist)
  4705. (org-infile-export-plist)))
  4706. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4707. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4708. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4709. (org-export-html-expand (plist-get options :expand-quoted-html))
  4710. (org-export-with-special-strings (plist-get options :special-strings))
  4711. (re-sub
  4712. (cond
  4713. ((equal org-export-with-sub-superscripts '{})
  4714. (list org-match-substring-with-braces-regexp))
  4715. (org-export-with-sub-superscripts
  4716. (list org-match-substring-regexp))
  4717. (t nil)))
  4718. (re-latex
  4719. (if org-export-with-LaTeX-fragments
  4720. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4721. (re-macros
  4722. (if org-export-with-TeX-macros
  4723. (list (concat "\\\\"
  4724. (regexp-opt
  4725. (append
  4726. (delq nil
  4727. (mapcar 'car-safe
  4728. (append org-entities-user
  4729. org-entities)))
  4730. (if (boundp 'org-latex-entities)
  4731. (mapcar (lambda (x)
  4732. (or (car-safe x) x))
  4733. org-latex-entities)
  4734. nil))
  4735. 'words))) ; FIXME
  4736. ))
  4737. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4738. (re-special (if org-export-with-special-strings
  4739. (mapcar (lambda (x) (car x))
  4740. org-export-html-special-string-regexps)))
  4741. (re-rest
  4742. (delq nil
  4743. (list
  4744. (if org-export-html-expand "@<[^>\n]+>")
  4745. ))))
  4746. (org-set-local
  4747. 'org-latex-and-specials-regexp
  4748. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4749. re-rest) "\\|")))))
  4750. (defun org-do-latex-and-special-faces (limit)
  4751. "Run through the buffer and add overlays to links."
  4752. (when org-latex-and-specials-regexp
  4753. (let (rtn d)
  4754. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4755. limit t))
  4756. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4757. 'face))
  4758. '(org-code org-verbatim underline)))
  4759. (progn
  4760. (setq rtn t
  4761. d (cond ((member (char-after (1+ (match-beginning 0)))
  4762. '(?_ ?^)) 1)
  4763. (t 0)))
  4764. (font-lock-prepend-text-property
  4765. (+ d (match-beginning 0)) (match-end 0)
  4766. 'face 'org-latex-and-export-specials)
  4767. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4768. '(font-lock-multiline t)))))
  4769. rtn)))
  4770. (defun org-restart-font-lock ()
  4771. "Restart `font-lock-mode', to force refontification."
  4772. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4773. (font-lock-mode -1)
  4774. (font-lock-mode 1)))
  4775. (defun org-all-targets (&optional radio)
  4776. "Return a list of all targets in this file.
  4777. With optional argument RADIO, only find radio targets."
  4778. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4779. rtn)
  4780. (save-excursion
  4781. (goto-char (point-min))
  4782. (while (re-search-forward re nil t)
  4783. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4784. rtn)))
  4785. (defun org-make-target-link-regexp (targets)
  4786. "Make regular expression matching all strings in TARGETS.
  4787. The regular expression finds the targets also if there is a line break
  4788. between words."
  4789. (and targets
  4790. (concat
  4791. "\\<\\("
  4792. (mapconcat
  4793. (lambda (x)
  4794. (while (string-match " +" x)
  4795. (setq x (replace-match "\\s-+" t t x)))
  4796. x)
  4797. targets
  4798. "\\|")
  4799. "\\)\\>")))
  4800. (defun org-activate-tags (limit)
  4801. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4802. (progn
  4803. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4804. (add-text-properties (match-beginning 1) (match-end 1)
  4805. (list 'mouse-face 'highlight
  4806. 'keymap org-mouse-map))
  4807. (org-rear-nonsticky-at (match-end 1))
  4808. t)))
  4809. (defun org-outline-level ()
  4810. "Compute the outline level of the heading at point.
  4811. This function assumes that the cursor is at the beginning of a line matched
  4812. by `outline-regexp'. Otherwise it returns garbage.
  4813. If this is called at a normal headline, the level is the number of stars.
  4814. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4815. For plain list items, if they are matched by `outline-regexp', this returns
  4816. 1000 plus the line indentation."
  4817. (save-excursion
  4818. (looking-at outline-regexp)
  4819. (if (match-beginning 1)
  4820. (+ (org-get-string-indentation (match-string 1)) 1000)
  4821. (1- (- (match-end 0) (match-beginning 0))))))
  4822. (defvar org-font-lock-keywords nil)
  4823. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4824. "Regular expression matching a property line.")
  4825. (defvar org-font-lock-hook nil
  4826. "Functions to be called for special font lock stuff.")
  4827. (defvar org-font-lock-set-keywords-hook nil
  4828. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4829. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4830. it is installed to be used by font lock. This can be useful if something
  4831. needs to be inserted at a specific position in the font-lock sequence.")
  4832. (defun org-font-lock-hook (limit)
  4833. (run-hook-with-args 'org-font-lock-hook limit))
  4834. (defun org-set-font-lock-defaults ()
  4835. (let* ((em org-fontify-emphasized-text)
  4836. (lk org-activate-links)
  4837. (org-font-lock-extra-keywords
  4838. (list
  4839. ;; Call the hook
  4840. '(org-font-lock-hook)
  4841. ;; Headlines
  4842. `(,(if org-fontify-whole-heading-line
  4843. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4844. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4845. (1 (org-get-level-face 1))
  4846. (2 (org-get-level-face 2))
  4847. (3 (org-get-level-face 3)))
  4848. ;; Table lines
  4849. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4850. (1 'org-table t))
  4851. ;; Table internals
  4852. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4853. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4854. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4855. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4856. ;; Drawers
  4857. (list org-drawer-regexp '(0 'org-special-keyword t))
  4858. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4859. ;; Properties
  4860. (list org-property-re
  4861. '(1 'org-special-keyword t)
  4862. '(3 'org-property-value t))
  4863. ;; Links
  4864. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4865. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4866. (if (memq 'plain lk) '(org-activate-plain-links))
  4867. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4868. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4869. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4870. (if (memq 'footnote lk) '(org-activate-footnote-links
  4871. (2 'org-footnote t)))
  4872. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4873. '(org-hide-wide-columns (0 nil append))
  4874. ;; TODO lines
  4875. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4876. '(1 (org-get-todo-face 1) t))
  4877. ;; DONE
  4878. (if org-fontify-done-headline
  4879. (list (concat "^[*]+ +\\<\\("
  4880. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4881. "\\)\\(.*\\)")
  4882. '(2 'org-headline-done t))
  4883. nil)
  4884. ;; Priorities
  4885. '(org-font-lock-add-priority-faces)
  4886. ;; Tags
  4887. '(org-font-lock-add-tag-faces)
  4888. ;; Special keywords
  4889. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4890. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4891. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4892. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4893. ;; Emphasis
  4894. (if em
  4895. (if (featurep 'xemacs)
  4896. '(org-do-emphasis-faces (0 nil append))
  4897. '(org-do-emphasis-faces)))
  4898. ;; Checkboxes
  4899. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4900. 1 'org-checkbox prepend)
  4901. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4902. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4903. (0 (org-get-checkbox-statistics-face) t)))
  4904. ;; Description list items
  4905. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(.*? ::\\)"
  4906. 2 'bold prepend)
  4907. ;; ARCHIVEd headings
  4908. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4909. '(1 'org-archived prepend))
  4910. ;; Specials
  4911. '(org-do-latex-and-special-faces)
  4912. '(org-fontify-entities)
  4913. '(org-raise-scripts)
  4914. ;; Code
  4915. '(org-activate-code (1 'org-code t))
  4916. ;; COMMENT
  4917. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4918. "\\|" org-quote-string "\\)\\>")
  4919. '(1 'org-special-keyword t))
  4920. '("^#.*" (0 'font-lock-comment-face t))
  4921. ;; Blocks and meta lines
  4922. '(org-fontify-meta-lines-and-blocks)
  4923. )))
  4924. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4925. (run-hooks 'org-font-lock-set-keywords-hook)
  4926. ;; Now set the full font-lock-keywords
  4927. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4928. (org-set-local 'font-lock-defaults
  4929. '(org-font-lock-keywords t nil nil backward-paragraph))
  4930. (kill-local-variable 'font-lock-keywords) nil))
  4931. (defun org-toggle-pretty-entities ()
  4932. "Toggle the composition display of entities as UTF8 characters."
  4933. (interactive)
  4934. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4935. (org-restart-font-lock)
  4936. (if org-pretty-entities
  4937. (message "Entities are displayed as UTF8 characers")
  4938. (save-restriction
  4939. (widen)
  4940. (org-decompose-region (point-min) (point-max))
  4941. (message "Entities are displayed plain"))))
  4942. (defun org-fontify-entities (limit)
  4943. "Find an entity to fontify."
  4944. (let (ee)
  4945. (when org-pretty-entities
  4946. (catch 'match
  4947. (while (re-search-forward
  4948. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  4949. limit t)
  4950. (if (and (not (org-in-indented-comment-line))
  4951. (setq ee (org-entity-get (match-string 1)))
  4952. (= (length (nth 6 ee)) 1))
  4953. (progn
  4954. (add-text-properties
  4955. (match-beginning 0) (match-end 1)
  4956. (list 'font-lock-fontified t))
  4957. (compose-region (match-beginning 0) (match-end 1)
  4958. (nth 6 ee) nil)
  4959. (backward-char 1)
  4960. (throw 'match t))))
  4961. nil))))
  4962. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4963. "Fontify string S like in Org-mode."
  4964. (with-temp-buffer
  4965. (insert s)
  4966. (let ((org-odd-levels-only odd-levels))
  4967. (org-mode)
  4968. (font-lock-fontify-buffer)
  4969. (buffer-string))))
  4970. (defvar org-m nil)
  4971. (defvar org-l nil)
  4972. (defvar org-f nil)
  4973. (defun org-get-level-face (n)
  4974. "Get the right face for match N in font-lock matching of headlines."
  4975. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4976. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4977. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4978. (cond
  4979. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4980. ((eq n 2) org-f)
  4981. (t (if org-level-color-stars-only nil org-f))))
  4982. (defun org-get-todo-face (kwd)
  4983. "Get the right face for a TODO keyword KWD.
  4984. If KWD is a number, get the corresponding match group."
  4985. (if (numberp kwd) (setq kwd (match-string kwd)))
  4986. (or (org-face-from-face-or-color
  4987. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4988. (and (member kwd org-done-keywords) 'org-done)
  4989. 'org-todo))
  4990. (defun org-face-from-face-or-color (context inherit face-or-color)
  4991. "Create a face list that inherits INHERIT, but sets the foreground color.
  4992. When FACE-OR-COLOR is not a string, just return it."
  4993. (if (stringp face-or-color)
  4994. (list :inherit inherit
  4995. (cdr (assoc context org-faces-easy-properties))
  4996. face-or-color)
  4997. face-or-color))
  4998. (defun org-font-lock-add-tag-faces (limit)
  4999. "Add the special tag faces."
  5000. (when (and org-tag-faces org-tags-special-faces-re)
  5001. (while (re-search-forward org-tags-special-faces-re limit t)
  5002. (add-text-properties (match-beginning 1) (match-end 1)
  5003. (list 'face (org-get-tag-face 1)
  5004. 'font-lock-fontified t))
  5005. (backward-char 1))))
  5006. (defun org-font-lock-add-priority-faces (limit)
  5007. "Add the special priority faces."
  5008. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5009. (add-text-properties
  5010. (match-beginning 0) (match-end 0)
  5011. (list 'face (or (org-face-from-face-or-color
  5012. 'priority 'org-special-keyword
  5013. (cdr (assoc (char-after (match-beginning 1))
  5014. org-priority-faces)))
  5015. 'org-special-keyword)
  5016. 'font-lock-fontified t))))
  5017. (defun org-get-tag-face (kwd)
  5018. "Get the right face for a TODO keyword KWD.
  5019. If KWD is a number, get the corresponding match group."
  5020. (if (numberp kwd) (setq kwd (match-string kwd)))
  5021. (or (org-face-from-face-or-color
  5022. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5023. 'org-tag))
  5024. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5025. "Remove fontification and activation overlays from links."
  5026. (font-lock-default-unfontify-region beg end)
  5027. (let* ((buffer-undo-list t)
  5028. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5029. (inhibit-modification-hooks t)
  5030. deactivate-mark buffer-file-name buffer-file-truename)
  5031. (org-decompose-region beg end)
  5032. (remove-text-properties
  5033. beg end
  5034. (if org-indent-mode
  5035. ;; also remove line-prefix and wrap-prefix properties
  5036. '(mouse-face t keymap t org-linked-text t
  5037. invisible t intangible t
  5038. line-prefix t wrap-prefix t
  5039. org-no-flyspell t org-emphasis t)
  5040. '(mouse-face t keymap t org-linked-text t
  5041. invisible t intangible t
  5042. org-no-flyspell t org-emphasis t)))
  5043. (org-remove-font-lock-display-properties beg end)))
  5044. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5045. ((raise 0.3) (height 0.7))
  5046. ((raise -0.5))
  5047. ((raise 0.5)))
  5048. "Display properties for showing superscripts and subscripts.")
  5049. (defun org-remove-font-lock-display-properties (beg end)
  5050. "Remove specific display properties that have been added by font lock.
  5051. The will remove the raise properties that are used to show superscripts
  5052. and subscripts."
  5053. (let (next prop)
  5054. (while (< beg end)
  5055. (setq next (next-single-property-change beg 'display nil end)
  5056. prop (get-text-property beg 'display))
  5057. (if (member prop org-script-display)
  5058. (put-text-property beg next 'display nil))
  5059. (setq beg next))))
  5060. (defun org-raise-scripts (limit)
  5061. "Add raise properties to sub/superscripts."
  5062. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5063. (if (re-search-forward
  5064. (if (eq org-use-sub-superscripts t)
  5065. org-match-substring-regexp
  5066. org-match-substring-with-braces-regexp)
  5067. limit t)
  5068. (let* ((pos (point)) table-p comment-p
  5069. (mpos (match-beginning 3))
  5070. (emph-p (get-text-property mpos 'org-emphasis))
  5071. (link-p (get-text-property mpos 'mouse-face))
  5072. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5073. (goto-char (point-at-bol))
  5074. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5075. comment-p (org-looking-at-p "[ \t]*#"))
  5076. (goto-char pos)
  5077. ;; FIXME: Should we go back one character here, for a_b^c
  5078. ;; (goto-char (1- pos)) ;????????????????????
  5079. (if (or comment-p emph-p link-p keyw-p)
  5080. t
  5081. (put-text-property (match-beginning 3) (match-end 0)
  5082. 'display
  5083. (if (equal (char-after (match-beginning 2)) ?^)
  5084. (nth (if table-p 3 1) org-script-display)
  5085. (nth (if table-p 2 0) org-script-display)))
  5086. (add-text-properties (match-beginning 2) (match-end 2)
  5087. (list 'invisible t
  5088. 'org-dwidth t 'org-dwidth-n 1))
  5089. (if (and (eq (char-after (match-beginning 3)) ?{)
  5090. (eq (char-before (match-end 3)) ?}))
  5091. (progn
  5092. (add-text-properties
  5093. (match-beginning 3) (1+ (match-beginning 3))
  5094. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5095. (add-text-properties
  5096. (1- (match-end 3)) (match-end 3)
  5097. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5098. t)))))
  5099. ;;;; Visibility cycling, including org-goto and indirect buffer
  5100. ;;; Cycling
  5101. (defvar org-cycle-global-status nil)
  5102. (make-variable-buffer-local 'org-cycle-global-status)
  5103. (defvar org-cycle-subtree-status nil)
  5104. (make-variable-buffer-local 'org-cycle-subtree-status)
  5105. ;;;###autoload
  5106. (defvar org-inlinetask-min-level)
  5107. (defun org-cycle (&optional arg)
  5108. "TAB-action and visibility cycling for Org-mode.
  5109. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5110. is outline visibility cycling, but it also invokes other actions
  5111. in special contexts.
  5112. - When this function is called with a prefix argument, rotate the entire
  5113. buffer through 3 states (global cycling)
  5114. 1. OVERVIEW: Show only top-level headlines.
  5115. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5116. 3. SHOW ALL: Show everything.
  5117. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5118. determined by the variable `org-startup-folded', and by any VISIBILITY
  5119. properties in the buffer.
  5120. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5121. including any drawers.
  5122. - When inside a table, re-align the table and move to the next field.
  5123. - When point is at the beginning of a headline, rotate the subtree started
  5124. by this line through 3 different states (local cycling)
  5125. 1. FOLDED: Only the main headline is shown.
  5126. 2. CHILDREN: The main headline and the direct children are shown.
  5127. From this state, you can move to one of the children
  5128. and zoom in further.
  5129. 3. SUBTREE: Show the entire subtree, including body text.
  5130. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5131. - When point is at the beginning of an empty headline and the variable
  5132. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5133. of the headline by demoting and promoting it to likely levels. This
  5134. speeds up creation document structure by pressing TAB once or several
  5135. times right after creating a new headline.
  5136. - When there is a numeric prefix, go up to a heading with level ARG, do
  5137. a `show-subtree' and return to the previous cursor position. If ARG
  5138. is negative, go up that many levels.
  5139. - When point is not at the beginning of a headline, execute the global
  5140. binding for TAB, which is re-indenting the line. See the option
  5141. `org-cycle-emulate-tab' for details.
  5142. - Special case: if point is at the beginning of the buffer and there is
  5143. no headline in line 1, this function will act as if called with prefix arg.
  5144. But only if also the variable `org-cycle-global-at-bob' is t."
  5145. (interactive "P")
  5146. (org-load-modules-maybe)
  5147. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5148. (and org-cycle-level-after-item/entry-creation
  5149. (or (org-cycle-level)
  5150. (org-cycle-item-indentation))))
  5151. (let* ((limit-level
  5152. (or org-cycle-max-level
  5153. (and (boundp 'org-inlinetask-min-level)
  5154. org-inlinetask-min-level
  5155. (1- org-inlinetask-min-level))))
  5156. (nstars (and limit-level
  5157. (if org-odd-levels-only
  5158. (and limit-level (1- (* limit-level 2)))
  5159. limit-level)))
  5160. (outline-regexp
  5161. (cond
  5162. ((not (org-mode-p)) outline-regexp)
  5163. ((or (eq org-cycle-include-plain-lists 'integrate)
  5164. (and org-cycle-include-plain-lists (org-at-item-p)))
  5165. (concat "\\(?:\\*"
  5166. (if nstars (format "\\{1,%d\\}" nstars) "+")
  5167. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  5168. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  5169. (bob-special (and org-cycle-global-at-bob (bobp)
  5170. (not (looking-at outline-regexp))))
  5171. (org-cycle-hook
  5172. (if bob-special
  5173. (delq 'org-optimize-window-after-visibility-change
  5174. (copy-sequence org-cycle-hook))
  5175. org-cycle-hook))
  5176. (pos (point)))
  5177. (if (or bob-special (equal arg '(4)))
  5178. ;; special case: use global cycling
  5179. (setq arg t))
  5180. (cond
  5181. ((equal arg '(16))
  5182. (org-set-startup-visibility)
  5183. (message "Startup visibility, plus VISIBILITY properties"))
  5184. ((equal arg '(64))
  5185. (show-all)
  5186. (message "Entire buffer visible, including drawers"))
  5187. ((org-at-table-p 'any)
  5188. ;; Enter the table or move to the next field in the table
  5189. (if (org-at-table.el-p)
  5190. (message "Use C-c ' to edit table.el tables")
  5191. (if arg (org-table-edit-field t)
  5192. (org-table-justify-field-maybe)
  5193. (call-interactively 'org-table-next-field))))
  5194. ((run-hook-with-args-until-success
  5195. 'org-tab-after-check-for-table-hook))
  5196. ((eq arg t) ;; Global cycling
  5197. (org-cycle-internal-global))
  5198. ((and org-drawers org-drawer-regexp
  5199. (save-excursion
  5200. (beginning-of-line 1)
  5201. (looking-at org-drawer-regexp)))
  5202. ;; Toggle block visibility
  5203. (org-flag-drawer
  5204. (not (get-char-property (match-end 0) 'invisible))))
  5205. ((integerp arg)
  5206. ;; Show-subtree, ARG levels up from here.
  5207. (save-excursion
  5208. (org-back-to-heading)
  5209. (outline-up-heading (if (< arg 0) (- arg)
  5210. (- (funcall outline-level) arg)))
  5211. (org-show-subtree)))
  5212. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5213. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5214. (org-cycle-internal-local))
  5215. ;; TAB emulation and template completion
  5216. (buffer-read-only (org-back-to-heading))
  5217. ((run-hook-with-args-until-success
  5218. 'org-tab-after-check-for-cycling-hook))
  5219. ((org-try-structure-completion))
  5220. ((org-try-cdlatex-tab))
  5221. ((run-hook-with-args-until-success
  5222. 'org-tab-before-tab-emulation-hook))
  5223. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5224. (or (not (bolp))
  5225. (not (looking-at outline-regexp))))
  5226. (call-interactively (global-key-binding "\t")))
  5227. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5228. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5229. (or (and (eq org-cycle-emulate-tab 'white)
  5230. (= (match-end 0) (point-at-eol)))
  5231. (and (eq org-cycle-emulate-tab 'whitestart)
  5232. (>= (match-end 0) pos))))
  5233. t
  5234. (eq org-cycle-emulate-tab t))
  5235. (call-interactively (global-key-binding "\t")))
  5236. (t (save-excursion
  5237. (org-back-to-heading)
  5238. (org-cycle)))))))
  5239. (defun org-cycle-internal-global ()
  5240. "Do the global cycling action."
  5241. (cond
  5242. ((and (eq last-command this-command)
  5243. (eq org-cycle-global-status 'overview))
  5244. ;; We just created the overview - now do table of contents
  5245. ;; This can be slow in very large buffers, so indicate action
  5246. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5247. (message "CONTENTS...")
  5248. (org-content)
  5249. (message "CONTENTS...done")
  5250. (setq org-cycle-global-status 'contents)
  5251. (run-hook-with-args 'org-cycle-hook 'contents))
  5252. ((and (eq last-command this-command)
  5253. (eq org-cycle-global-status 'contents))
  5254. ;; We just showed the table of contents - now show everything
  5255. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5256. (show-all)
  5257. (message "SHOW ALL")
  5258. (setq org-cycle-global-status 'all)
  5259. (run-hook-with-args 'org-cycle-hook 'all))
  5260. (t
  5261. ;; Default action: go to overview
  5262. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5263. (org-overview)
  5264. (message "OVERVIEW")
  5265. (setq org-cycle-global-status 'overview)
  5266. (run-hook-with-args 'org-cycle-hook 'overview))))
  5267. (defun org-cycle-internal-local ()
  5268. "Do the local cycling action."
  5269. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  5270. ;; First, some boundaries
  5271. (save-excursion
  5272. (org-back-to-heading)
  5273. (setq level (funcall outline-level))
  5274. (save-excursion
  5275. (beginning-of-line 2)
  5276. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  5277. ; XEmacs does not have `next-single-char-property-change'
  5278. ; I'm not sure about Emacs 21.
  5279. (while (and (not (eobp)) ;; this is like `next-line'
  5280. (get-char-property (1- (point)) 'invisible))
  5281. (beginning-of-line 2))
  5282. (while (and (not (eobp)) ;; this is like `next-line'
  5283. (get-char-property (1- (point)) 'invisible))
  5284. (goto-char (next-single-char-property-change (point) 'invisible))
  5285. (and (eolp) (beginning-of-line 2))))
  5286. (setq eol (point)))
  5287. (outline-end-of-heading) (setq eoh (point))
  5288. (save-excursion
  5289. (outline-next-heading)
  5290. (setq has-children (and (org-at-heading-p t)
  5291. (> (funcall outline-level) level))))
  5292. ;; if we're in a list, org-end-of-subtree is in fact org-end-of-item.
  5293. (if (org-at-item-p)
  5294. (setq eos (if (and (org-end-of-item) (bolp))
  5295. (1- (point))
  5296. (point)))
  5297. (org-end-of-subtree t)
  5298. (unless (eobp)
  5299. (skip-chars-forward " \t\n"))
  5300. (setq eos (if (eobp) (point) (1- (point))))))
  5301. ;; Find out what to do next and set `this-command'
  5302. (cond
  5303. ((= eos eoh)
  5304. ;; Nothing is hidden behind this heading
  5305. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5306. (message "EMPTY ENTRY")
  5307. (setq org-cycle-subtree-status nil)
  5308. (save-excursion
  5309. (goto-char eos)
  5310. (outline-next-heading)
  5311. (if (org-invisible-p) (org-flag-heading nil))))
  5312. ((and (or (>= eol eos)
  5313. (not (string-match "\\S-" (buffer-substring eol eos))))
  5314. (or has-children
  5315. (not (setq children-skipped
  5316. org-cycle-skip-children-state-if-no-children))))
  5317. ;; Entire subtree is hidden in one line: children view
  5318. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5319. (org-show-entry)
  5320. (show-children)
  5321. (message "CHILDREN")
  5322. (save-excursion
  5323. (goto-char eos)
  5324. (outline-next-heading)
  5325. (if (org-invisible-p) (org-flag-heading nil)))
  5326. (setq org-cycle-subtree-status 'children)
  5327. (run-hook-with-args 'org-cycle-hook 'children))
  5328. ((or children-skipped
  5329. (and (eq last-command this-command)
  5330. (eq org-cycle-subtree-status 'children)))
  5331. ;; We just showed the children, or no children are there,
  5332. ;; now show everything.
  5333. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5334. (outline-flag-region eoh eos nil)
  5335. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5336. (setq org-cycle-subtree-status 'subtree)
  5337. (run-hook-with-args 'org-cycle-hook 'subtree))
  5338. (t
  5339. ;; Default action: hide the subtree.
  5340. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5341. (outline-flag-region eoh eos t)
  5342. (message "FOLDED")
  5343. (setq org-cycle-subtree-status 'folded)
  5344. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5345. ;;;###autoload
  5346. (defun org-global-cycle (&optional arg)
  5347. "Cycle the global visibility. For details see `org-cycle'.
  5348. With \\[universal-argument] prefix arg, switch to startup visibility.
  5349. With a numeric prefix, show all headlines up to that level."
  5350. (interactive "P")
  5351. (let ((org-cycle-include-plain-lists
  5352. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5353. (cond
  5354. ((integerp arg)
  5355. (show-all)
  5356. (hide-sublevels arg)
  5357. (setq org-cycle-global-status 'contents))
  5358. ((equal arg '(4))
  5359. (org-set-startup-visibility)
  5360. (message "Startup visibility, plus VISIBILITY properties."))
  5361. (t
  5362. (org-cycle '(4))))))
  5363. (defun org-set-startup-visibility ()
  5364. "Set the visibility required by startup options and properties."
  5365. (cond
  5366. ((eq org-startup-folded t)
  5367. (org-cycle '(4)))
  5368. ((eq org-startup-folded 'content)
  5369. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5370. (org-cycle '(4)) (org-cycle '(4)))))
  5371. (unless (eq org-startup-folded 'showeverything)
  5372. (if org-hide-block-startup (org-hide-block-all))
  5373. (org-set-visibility-according-to-property 'no-cleanup)
  5374. (org-cycle-hide-archived-subtrees 'all)
  5375. (org-cycle-hide-drawers 'all)
  5376. (org-cycle-show-empty-lines t)))
  5377. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5378. "Switch subtree visibilities according to :VISIBILITY: property."
  5379. (interactive)
  5380. (let (org-show-entry-below state)
  5381. (save-excursion
  5382. (goto-char (point-max))
  5383. (while (re-search-backward
  5384. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5385. nil t)
  5386. (setq state (match-string 1))
  5387. (save-excursion
  5388. (org-back-to-heading t)
  5389. (hide-subtree)
  5390. (org-reveal)
  5391. (cond
  5392. ((equal state '("fold" "folded"))
  5393. (hide-subtree))
  5394. ((equal state "children")
  5395. (org-show-hidden-entry)
  5396. (show-children))
  5397. ((equal state "content")
  5398. (save-excursion
  5399. (save-restriction
  5400. (org-narrow-to-subtree)
  5401. (org-content))))
  5402. ((member state '("all" "showall"))
  5403. (show-subtree)))))
  5404. (unless no-cleanup
  5405. (org-cycle-hide-archived-subtrees 'all)
  5406. (org-cycle-hide-drawers 'all)
  5407. (org-cycle-show-empty-lines 'all)))))
  5408. (defun org-overview ()
  5409. "Switch to overview mode, showing only top-level headlines.
  5410. Really, this shows all headlines with level equal or greater than the level
  5411. of the first headline in the buffer. This is important, because if the
  5412. first headline is not level one, then (hide-sublevels 1) gives confusing
  5413. results."
  5414. (interactive)
  5415. (let ((level (save-excursion
  5416. (goto-char (point-min))
  5417. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5418. (progn
  5419. (goto-char (match-beginning 0))
  5420. (funcall outline-level))))))
  5421. (and level (hide-sublevels level))))
  5422. (defun org-content (&optional arg)
  5423. "Show all headlines in the buffer, like a table of contents.
  5424. With numerical argument N, show content up to level N."
  5425. (interactive "P")
  5426. (save-excursion
  5427. ;; Visit all headings and show their offspring
  5428. (and (integerp arg) (org-overview))
  5429. (goto-char (point-max))
  5430. (catch 'exit
  5431. (while (and (progn (condition-case nil
  5432. (outline-previous-visible-heading 1)
  5433. (error (goto-char (point-min))))
  5434. t)
  5435. (looking-at outline-regexp))
  5436. (if (integerp arg)
  5437. (show-children (1- arg))
  5438. (show-branches))
  5439. (if (bobp) (throw 'exit nil))))))
  5440. (defun org-optimize-window-after-visibility-change (state)
  5441. "Adjust the window after a change in outline visibility.
  5442. This function is the default value of the hook `org-cycle-hook'."
  5443. (when (get-buffer-window (current-buffer))
  5444. (cond
  5445. ((eq state 'content) nil)
  5446. ((eq state 'all) nil)
  5447. ((eq state 'folded) nil)
  5448. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5449. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5450. (defun org-remove-empty-overlays-at (pos)
  5451. "Remove outline overlays that do not contain non-white stuff."
  5452. (mapc
  5453. (lambda (o)
  5454. (and (eq 'outline (overlay-get o 'invisible))
  5455. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5456. (overlay-end o))))
  5457. (delete-overlay o)))
  5458. (overlays-at pos)))
  5459. (defun org-clean-visibility-after-subtree-move ()
  5460. "Fix visibility issues after moving a subtree."
  5461. ;; First, find a reasonable region to look at:
  5462. ;; Start two siblings above, end three below
  5463. (let* ((beg (save-excursion
  5464. (and (org-get-last-sibling)
  5465. (org-get-last-sibling))
  5466. (point)))
  5467. (end (save-excursion
  5468. (and (org-get-next-sibling)
  5469. (org-get-next-sibling)
  5470. (org-get-next-sibling))
  5471. (if (org-at-heading-p)
  5472. (point-at-eol)
  5473. (point))))
  5474. (level (looking-at "\\*+"))
  5475. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5476. (save-excursion
  5477. (save-restriction
  5478. (narrow-to-region beg end)
  5479. (when re
  5480. ;; Properly fold already folded siblings
  5481. (goto-char (point-min))
  5482. (while (re-search-forward re nil t)
  5483. (if (and (not (org-invisible-p))
  5484. (save-excursion
  5485. (goto-char (point-at-eol)) (org-invisible-p)))
  5486. (hide-entry))))
  5487. (org-cycle-show-empty-lines 'overview)
  5488. (org-cycle-hide-drawers 'overview)))))
  5489. (defun org-cycle-show-empty-lines (state)
  5490. "Show empty lines above all visible headlines.
  5491. The region to be covered depends on STATE when called through
  5492. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5493. entire buffer covered. Note that an empty line is only shown if there
  5494. are at least `org-cycle-separator-lines' empty lines before the headline."
  5495. (when (not (= org-cycle-separator-lines 0))
  5496. (save-excursion
  5497. (let* ((n (abs org-cycle-separator-lines))
  5498. (re (cond
  5499. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5500. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5501. (t (let ((ns (number-to-string (- n 2))))
  5502. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5503. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5504. beg end b e)
  5505. (cond
  5506. ((memq state '(overview contents t))
  5507. (setq beg (point-min) end (point-max)))
  5508. ((memq state '(children folded))
  5509. (setq beg (point) end (progn (org-end-of-subtree t t)
  5510. (beginning-of-line 2)
  5511. (point)))))
  5512. (when beg
  5513. (goto-char beg)
  5514. (while (re-search-forward re end t)
  5515. (unless (get-char-property (match-end 1) 'invisible)
  5516. (setq e (match-end 1))
  5517. (if (< org-cycle-separator-lines 0)
  5518. (setq b (save-excursion
  5519. (goto-char (match-beginning 0))
  5520. (org-back-over-empty-lines)
  5521. (if (save-excursion
  5522. (goto-char (max (point-min) (1- (point))))
  5523. (org-on-heading-p))
  5524. (1- (point))
  5525. (point))))
  5526. (setq b (match-beginning 1)))
  5527. (outline-flag-region b e nil)))))))
  5528. ;; Never hide empty lines at the end of the file.
  5529. (save-excursion
  5530. (goto-char (point-max))
  5531. (outline-previous-heading)
  5532. (outline-end-of-heading)
  5533. (if (and (looking-at "[ \t\n]+")
  5534. (= (match-end 0) (point-max)))
  5535. (outline-flag-region (point) (match-end 0) nil))))
  5536. (defun org-show-empty-lines-in-parent ()
  5537. "Move to the parent and re-show empty lines before visible headlines."
  5538. (save-excursion
  5539. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5540. (org-cycle-show-empty-lines context))))
  5541. (defun org-files-list ()
  5542. "Return `org-agenda-files' list, plus all open org-mode files.
  5543. This is useful for operations that need to scan all of a user's
  5544. open and agenda-wise Org files."
  5545. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5546. (dolist (buf (buffer-list))
  5547. (with-current-buffer buf
  5548. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5549. (let ((file (expand-file-name (buffer-file-name))))
  5550. (unless (member file files)
  5551. (push file files))))))
  5552. files))
  5553. (defsubst org-entry-beginning-position ()
  5554. "Return the beginning position of the current entry."
  5555. (save-excursion (outline-back-to-heading t) (point)))
  5556. (defsubst org-entry-end-position ()
  5557. "Return the end position of the current entry."
  5558. (save-excursion (outline-next-heading) (point)))
  5559. (defun org-cycle-hide-drawers (state)
  5560. "Re-hide all drawers after a visibility state change."
  5561. (when (and (org-mode-p)
  5562. (not (memq state '(overview folded contents))))
  5563. (save-excursion
  5564. (let* ((globalp (memq state '(contents all)))
  5565. (beg (if globalp (point-min) (point)))
  5566. (end (if globalp (point-max)
  5567. (if (eq state 'children)
  5568. (save-excursion (outline-next-heading) (point))
  5569. (org-end-of-subtree t)))))
  5570. (goto-char beg)
  5571. (while (re-search-forward org-drawer-regexp end t)
  5572. (org-flag-drawer t))))))
  5573. (defun org-flag-drawer (flag)
  5574. (save-excursion
  5575. (beginning-of-line 1)
  5576. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5577. (let ((b (match-end 0))
  5578. (outline-regexp org-outline-regexp))
  5579. (if (re-search-forward
  5580. "^[ \t]*:END:"
  5581. (save-excursion (outline-next-heading) (point)) t)
  5582. (outline-flag-region b (point-at-eol) flag)
  5583. (error ":END: line missing at position %s" b))))))
  5584. (defun org-subtree-end-visible-p ()
  5585. "Is the end of the current subtree visible?"
  5586. (pos-visible-in-window-p
  5587. (save-excursion (org-end-of-subtree t) (point))))
  5588. (defun org-first-headline-recenter (&optional N)
  5589. "Move cursor to the first headline and recenter the headline.
  5590. Optional argument N means put the headline into the Nth line of the window."
  5591. (goto-char (point-min))
  5592. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5593. (beginning-of-line)
  5594. (recenter (prefix-numeric-value N))))
  5595. ;;; Saving and restoring visibility
  5596. (defun org-outline-overlay-data (&optional use-markers)
  5597. "Return a list of the locations of all outline overlays.
  5598. These are overlays with the `invisible' property value `outline'.
  5599. The return value is a list of cons cells, with start and stop
  5600. positions for each overlay.
  5601. If USE-MARKERS is set, return the positions as markers."
  5602. (let (beg end)
  5603. (save-excursion
  5604. (save-restriction
  5605. (widen)
  5606. (delq nil
  5607. (mapcar (lambda (o)
  5608. (when (eq (overlay-get o 'invisible) 'outline)
  5609. (setq beg (overlay-start o)
  5610. end (overlay-end o))
  5611. (and beg end (> end beg)
  5612. (if use-markers
  5613. (cons (move-marker (make-marker) beg)
  5614. (move-marker (make-marker) end))
  5615. (cons beg end)))))
  5616. (overlays-in (point-min) (point-max))))))))
  5617. (defun org-set-outline-overlay-data (data)
  5618. "Create visibility overlays for all positions in DATA.
  5619. DATA should have been made by `org-outline-overlay-data'."
  5620. (let (o)
  5621. (save-excursion
  5622. (save-restriction
  5623. (widen)
  5624. (show-all)
  5625. (mapc (lambda (c)
  5626. (setq o (make-overlay (car c) (cdr c)))
  5627. (overlay-put o 'invisible 'outline))
  5628. data)))))
  5629. ;;; Folding of blocks
  5630. (defconst org-block-regexp
  5631. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5632. "Regular expression for hiding blocks.")
  5633. (defvar org-hide-block-overlays nil
  5634. "Overlays hiding blocks.")
  5635. (make-variable-buffer-local 'org-hide-block-overlays)
  5636. (defun org-block-map (function &optional start end)
  5637. "Call FUNCTION at the head of all source blocks in the current buffer.
  5638. Optional arguments START and END can be used to limit the range."
  5639. (let ((start (or start (point-min)))
  5640. (end (or end (point-max))))
  5641. (save-excursion
  5642. (goto-char start)
  5643. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5644. (save-excursion
  5645. (save-match-data
  5646. (goto-char (match-beginning 0))
  5647. (funcall function)))))))
  5648. (defun org-hide-block-toggle-all ()
  5649. "Toggle the visibility of all blocks in the current buffer."
  5650. (org-block-map #'org-hide-block-toggle))
  5651. (defun org-hide-block-all ()
  5652. "Fold all blocks in the current buffer."
  5653. (interactive)
  5654. (org-show-block-all)
  5655. (org-block-map #'org-hide-block-toggle-maybe))
  5656. (defun org-show-block-all ()
  5657. "Unfold all blocks in the current buffer."
  5658. (interactive)
  5659. (mapc 'delete-overlay org-hide-block-overlays)
  5660. (setq org-hide-block-overlays nil))
  5661. (defun org-hide-block-toggle-maybe ()
  5662. "Toggle visibility of block at point."
  5663. (interactive)
  5664. (let ((case-fold-search t))
  5665. (if (save-excursion
  5666. (beginning-of-line 1)
  5667. (looking-at org-block-regexp))
  5668. (progn (org-hide-block-toggle)
  5669. t) ;; to signal that we took action
  5670. nil))) ;; to signal that we did not
  5671. (defun org-hide-block-toggle (&optional force)
  5672. "Toggle the visibility of the current block."
  5673. (interactive)
  5674. (save-excursion
  5675. (beginning-of-line)
  5676. (if (re-search-forward org-block-regexp nil t)
  5677. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5678. (end (match-end 0)) ;; end of entire body
  5679. ov)
  5680. (if (memq t (mapcar (lambda (overlay)
  5681. (eq (overlay-get overlay 'invisible)
  5682. 'org-hide-block))
  5683. (overlays-at start)))
  5684. (if (or (not force) (eq force 'off))
  5685. (mapc (lambda (ov)
  5686. (when (member ov org-hide-block-overlays)
  5687. (setq org-hide-block-overlays
  5688. (delq ov org-hide-block-overlays)))
  5689. (when (eq (overlay-get ov 'invisible)
  5690. 'org-hide-block)
  5691. (delete-overlay ov)))
  5692. (overlays-at start)))
  5693. (setq ov (make-overlay start end))
  5694. (overlay-put ov 'invisible 'org-hide-block)
  5695. ;; make the block accessible to isearch
  5696. (overlay-put
  5697. ov 'isearch-open-invisible
  5698. (lambda (ov)
  5699. (when (member ov org-hide-block-overlays)
  5700. (setq org-hide-block-overlays
  5701. (delq ov org-hide-block-overlays)))
  5702. (when (eq (overlay-get ov 'invisible)
  5703. 'org-hide-block)
  5704. (delete-overlay ov))))
  5705. (push ov org-hide-block-overlays)))
  5706. (error "Not looking at a source block"))))
  5707. ;; org-tab-after-check-for-cycling-hook
  5708. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5709. ;; Remove overlays when changing major mode
  5710. (add-hook 'org-mode-hook
  5711. (lambda () (org-add-hook 'change-major-mode-hook
  5712. 'org-show-block-all 'append 'local)))
  5713. ;;; Org-goto
  5714. (defvar org-goto-window-configuration nil)
  5715. (defvar org-goto-marker nil)
  5716. (defvar org-goto-map
  5717. (let ((map (make-sparse-keymap)))
  5718. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5719. (while (setq cmd (pop cmds))
  5720. (substitute-key-definition cmd cmd map global-map)))
  5721. (suppress-keymap map)
  5722. (org-defkey map "\C-m" 'org-goto-ret)
  5723. (org-defkey map [(return)] 'org-goto-ret)
  5724. (org-defkey map [(left)] 'org-goto-left)
  5725. (org-defkey map [(right)] 'org-goto-right)
  5726. (org-defkey map [(control ?g)] 'org-goto-quit)
  5727. (org-defkey map "\C-i" 'org-cycle)
  5728. (org-defkey map [(tab)] 'org-cycle)
  5729. (org-defkey map [(down)] 'outline-next-visible-heading)
  5730. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5731. (if org-goto-auto-isearch
  5732. (if (fboundp 'define-key-after)
  5733. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5734. nil)
  5735. (org-defkey map "q" 'org-goto-quit)
  5736. (org-defkey map "n" 'outline-next-visible-heading)
  5737. (org-defkey map "p" 'outline-previous-visible-heading)
  5738. (org-defkey map "f" 'outline-forward-same-level)
  5739. (org-defkey map "b" 'outline-backward-same-level)
  5740. (org-defkey map "u" 'outline-up-heading))
  5741. (org-defkey map "/" 'org-occur)
  5742. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5743. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5744. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5745. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5746. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5747. map))
  5748. (defconst org-goto-help
  5749. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5750. RET=jump to location [Q]uit and return to previous location
  5751. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5752. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5753. ;; FIXME: Docstring does not mention both interfaces
  5754. (defun org-goto (&optional alternative-interface)
  5755. "Look up a different location in the current file, keeping current visibility.
  5756. When you want look-up or go to a different location in a document, the
  5757. fastest way is often to fold the entire buffer and then dive into the tree.
  5758. This method has the disadvantage, that the previous location will be folded,
  5759. which may not be what you want.
  5760. This command works around this by showing a copy of the current buffer
  5761. in an indirect buffer, in overview mode. You can dive into the tree in
  5762. that copy, use org-occur and incremental search to find a location.
  5763. When pressing RET or `Q', the command returns to the original buffer in
  5764. which the visibility is still unchanged. After RET is will also jump to
  5765. the location selected in the indirect buffer and expose the
  5766. the headline hierarchy above."
  5767. (interactive "P")
  5768. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5769. (org-refile-use-outline-path t)
  5770. (org-refile-target-verify-function nil)
  5771. (interface
  5772. (if (not alternative-interface)
  5773. org-goto-interface
  5774. (if (eq org-goto-interface 'outline)
  5775. 'outline-path-completion
  5776. 'outline)))
  5777. (org-goto-start-pos (point))
  5778. (selected-point
  5779. (if (eq interface 'outline)
  5780. (car (org-get-location (current-buffer) org-goto-help))
  5781. (let ((pa (org-refile-get-location "Goto: ")))
  5782. (org-refile-check-position pa)
  5783. (nth 3 pa)))))
  5784. (if selected-point
  5785. (progn
  5786. (org-mark-ring-push org-goto-start-pos)
  5787. (goto-char selected-point)
  5788. (if (or (org-invisible-p) (org-invisible-p2))
  5789. (org-show-context 'org-goto)))
  5790. (message "Quit"))))
  5791. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5792. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5793. (defvar org-goto-local-auto-isearch-map) ; defined below
  5794. (defun org-get-location (buf help)
  5795. "Let the user select a location in the Org-mode buffer BUF.
  5796. This function uses a recursive edit. It returns the selected position
  5797. or nil."
  5798. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5799. (isearch-hide-immediately nil)
  5800. (isearch-search-fun-function
  5801. (lambda () 'org-goto-local-search-headings))
  5802. (org-goto-selected-point org-goto-exit-command)
  5803. (pop-up-frames nil)
  5804. (special-display-buffer-names nil)
  5805. (special-display-regexps nil)
  5806. (special-display-function nil))
  5807. (save-excursion
  5808. (save-window-excursion
  5809. (delete-other-windows)
  5810. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5811. (switch-to-buffer
  5812. (condition-case nil
  5813. (make-indirect-buffer (current-buffer) "*org-goto*")
  5814. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5815. (with-output-to-temp-buffer "*Help*"
  5816. (princ help))
  5817. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5818. (setq buffer-read-only nil)
  5819. (let ((org-startup-truncated t)
  5820. (org-startup-folded nil)
  5821. (org-startup-align-all-tables nil))
  5822. (org-mode)
  5823. (org-overview))
  5824. (setq buffer-read-only t)
  5825. (if (and (boundp 'org-goto-start-pos)
  5826. (integer-or-marker-p org-goto-start-pos))
  5827. (let ((org-show-hierarchy-above t)
  5828. (org-show-siblings t)
  5829. (org-show-following-heading t))
  5830. (goto-char org-goto-start-pos)
  5831. (and (org-invisible-p) (org-show-context)))
  5832. (goto-char (point-min)))
  5833. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5834. (message "Select location and press RET")
  5835. (use-local-map org-goto-map)
  5836. (recursive-edit)
  5837. ))
  5838. (kill-buffer "*org-goto*")
  5839. (cons org-goto-selected-point org-goto-exit-command)))
  5840. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5841. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5842. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5843. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5844. (defun org-goto-local-search-headings (string bound noerror)
  5845. "Search and make sure that any matches are in headlines."
  5846. (catch 'return
  5847. (while (if isearch-forward
  5848. (search-forward string bound noerror)
  5849. (search-backward string bound noerror))
  5850. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5851. (and (member :headline context)
  5852. (not (member :tags context))))
  5853. (throw 'return (point))))))
  5854. (defun org-goto-local-auto-isearch ()
  5855. "Start isearch."
  5856. (interactive)
  5857. (goto-char (point-min))
  5858. (let ((keys (this-command-keys)))
  5859. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5860. (isearch-mode t)
  5861. (isearch-process-search-char (string-to-char keys)))))
  5862. (defun org-goto-ret (&optional arg)
  5863. "Finish `org-goto' by going to the new location."
  5864. (interactive "P")
  5865. (setq org-goto-selected-point (point)
  5866. org-goto-exit-command 'return)
  5867. (throw 'exit nil))
  5868. (defun org-goto-left ()
  5869. "Finish `org-goto' by going to the new location."
  5870. (interactive)
  5871. (if (org-on-heading-p)
  5872. (progn
  5873. (beginning-of-line 1)
  5874. (setq org-goto-selected-point (point)
  5875. org-goto-exit-command 'left)
  5876. (throw 'exit nil))
  5877. (error "Not on a heading")))
  5878. (defun org-goto-right ()
  5879. "Finish `org-goto' by going to the new location."
  5880. (interactive)
  5881. (if (org-on-heading-p)
  5882. (progn
  5883. (setq org-goto-selected-point (point)
  5884. org-goto-exit-command 'right)
  5885. (throw 'exit nil))
  5886. (error "Not on a heading")))
  5887. (defun org-goto-quit ()
  5888. "Finish `org-goto' without cursor motion."
  5889. (interactive)
  5890. (setq org-goto-selected-point nil)
  5891. (setq org-goto-exit-command 'quit)
  5892. (throw 'exit nil))
  5893. ;;; Indirect buffer display of subtrees
  5894. (defvar org-indirect-dedicated-frame nil
  5895. "This is the frame being used for indirect tree display.")
  5896. (defvar org-last-indirect-buffer nil)
  5897. (defun org-tree-to-indirect-buffer (&optional arg)
  5898. "Create indirect buffer and narrow it to current subtree.
  5899. With numerical prefix ARG, go up to this level and then take that tree.
  5900. If ARG is negative, go up that many levels.
  5901. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5902. indirect buffer previously made with this command, to avoid proliferation of
  5903. indirect buffers. However, when you call the command with a \
  5904. \\[universal-argument] prefix, or
  5905. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5906. is kept so that you can work with several indirect buffers at the same time.
  5907. If `org-indirect-buffer-display' is `dedicated-frame', the \
  5908. \\[universal-argument] prefix also
  5909. requests that a new frame be made for the new buffer, so that the dedicated
  5910. frame is not changed."
  5911. (interactive "P")
  5912. (let ((cbuf (current-buffer))
  5913. (cwin (selected-window))
  5914. (pos (point))
  5915. beg end level heading ibuf)
  5916. (save-excursion
  5917. (org-back-to-heading t)
  5918. (when (numberp arg)
  5919. (setq level (org-outline-level))
  5920. (if (< arg 0) (setq arg (+ level arg)))
  5921. (while (> (setq level (org-outline-level)) arg)
  5922. (outline-up-heading 1 t)))
  5923. (setq beg (point)
  5924. heading (org-get-heading))
  5925. (org-end-of-subtree t t)
  5926. (if (org-on-heading-p) (backward-char 1))
  5927. (setq end (point)))
  5928. (if (and (buffer-live-p org-last-indirect-buffer)
  5929. (not (eq org-indirect-buffer-display 'new-frame))
  5930. (not arg))
  5931. (kill-buffer org-last-indirect-buffer))
  5932. (setq ibuf (org-get-indirect-buffer cbuf)
  5933. org-last-indirect-buffer ibuf)
  5934. (cond
  5935. ((or (eq org-indirect-buffer-display 'new-frame)
  5936. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5937. (select-frame (make-frame))
  5938. (delete-other-windows)
  5939. (switch-to-buffer ibuf)
  5940. (org-set-frame-title heading))
  5941. ((eq org-indirect-buffer-display 'dedicated-frame)
  5942. (raise-frame
  5943. (select-frame (or (and org-indirect-dedicated-frame
  5944. (frame-live-p org-indirect-dedicated-frame)
  5945. org-indirect-dedicated-frame)
  5946. (setq org-indirect-dedicated-frame (make-frame)))))
  5947. (delete-other-windows)
  5948. (switch-to-buffer ibuf)
  5949. (org-set-frame-title (concat "Indirect: " heading)))
  5950. ((eq org-indirect-buffer-display 'current-window)
  5951. (switch-to-buffer ibuf))
  5952. ((eq org-indirect-buffer-display 'other-window)
  5953. (pop-to-buffer ibuf))
  5954. (t (error "Invalid value")))
  5955. (if (featurep 'xemacs)
  5956. (save-excursion (org-mode) (turn-on-font-lock)))
  5957. (narrow-to-region beg end)
  5958. (show-all)
  5959. (goto-char pos)
  5960. (and (window-live-p cwin) (select-window cwin))))
  5961. (defun org-get-indirect-buffer (&optional buffer)
  5962. (setq buffer (or buffer (current-buffer)))
  5963. (let ((n 1) (base (buffer-name buffer)) bname)
  5964. (while (buffer-live-p
  5965. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5966. (setq n (1+ n)))
  5967. (condition-case nil
  5968. (make-indirect-buffer buffer bname 'clone)
  5969. (error (make-indirect-buffer buffer bname)))))
  5970. (defun org-set-frame-title (title)
  5971. "Set the title of the current frame to the string TITLE."
  5972. ;; FIXME: how to name a single frame in XEmacs???
  5973. (unless (featurep 'xemacs)
  5974. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5975. ;;;; Structure editing
  5976. ;;; Inserting headlines
  5977. (defun org-previous-line-empty-p ()
  5978. (save-excursion
  5979. (and (not (bobp))
  5980. (or (beginning-of-line 0) t)
  5981. (save-match-data
  5982. (looking-at "[ \t]*$")))))
  5983. (defun org-insert-heading (&optional force-heading invisible-ok)
  5984. "Insert a new heading or item with same depth at point.
  5985. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5986. If point is at the beginning of a headline, insert a sibling before the
  5987. current headline. If point is not at the beginning, split the line,
  5988. create the new headline with the text in the current line after point
  5989. \(but see also the variable `org-M-RET-may-split-line').
  5990. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5991. This is important for non-interactive uses of the command."
  5992. (interactive "P")
  5993. (if (or (= (buffer-size) 0)
  5994. (and (not (save-excursion
  5995. (and (ignore-errors (org-back-to-heading invisible-ok))
  5996. (org-on-heading-p))))
  5997. (not (org-in-item-p))))
  5998. (progn
  5999. (insert "\n* ")
  6000. (run-hooks 'org-insert-heading-hook))
  6001. (when (or force-heading (not (org-insert-item)))
  6002. (let* ((empty-line-p nil)
  6003. (level nil)
  6004. (on-heading (org-on-heading-p))
  6005. (head (save-excursion
  6006. (condition-case nil
  6007. (progn
  6008. (org-back-to-heading invisible-ok)
  6009. (when (and (not on-heading)
  6010. (featurep 'org-inlinetask)
  6011. (integerp org-inlinetask-min-level)
  6012. (>= (length (match-string 0))
  6013. org-inlinetask-min-level))
  6014. ;; Find a heading level before the inline task
  6015. (while (and (setq level (org-up-heading-safe))
  6016. (>= level org-inlinetask-min-level)))
  6017. (if (org-on-heading-p)
  6018. (org-back-to-heading invisible-ok)
  6019. (error "This should not happen")))
  6020. (setq empty-line-p (org-previous-line-empty-p))
  6021. (match-string 0))
  6022. (error "*"))))
  6023. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6024. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6025. pos hide-previous previous-pos)
  6026. (cond
  6027. ((and (org-on-heading-p) (bolp)
  6028. (or (bobp)
  6029. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  6030. ;; insert before the current line
  6031. (open-line (if blank 2 1)))
  6032. ((and (bolp)
  6033. (not org-insert-heading-respect-content)
  6034. (or (bobp)
  6035. (save-excursion
  6036. (backward-char 1) (not (org-invisible-p)))))
  6037. ;; insert right here
  6038. nil)
  6039. (t
  6040. ;; somewhere in the line
  6041. (save-excursion
  6042. (setq previous-pos (point-at-bol))
  6043. (end-of-line)
  6044. (setq hide-previous (org-invisible-p)))
  6045. (and org-insert-heading-respect-content (org-show-subtree))
  6046. (let ((split
  6047. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6048. (save-excursion
  6049. (let ((p (point)))
  6050. (goto-char (point-at-bol))
  6051. (and (looking-at org-complex-heading-regexp)
  6052. (> p (match-beginning 4)))))))
  6053. tags pos)
  6054. (cond
  6055. (org-insert-heading-respect-content
  6056. (org-end-of-subtree nil t)
  6057. (when (featurep 'org-inlinetask)
  6058. (while (and (not (eobp))
  6059. (looking-at "\\(\\*+\\)[ \t]+")
  6060. (>= (length (match-string 1))
  6061. org-inlinetask-min-level))
  6062. (org-end-of-subtree nil t)))
  6063. (or (bolp) (newline))
  6064. (or (org-previous-line-empty-p)
  6065. (and blank (newline)))
  6066. (open-line 1))
  6067. ((org-on-heading-p)
  6068. (when hide-previous
  6069. (show-children)
  6070. (org-show-entry))
  6071. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6072. (setq tags (and (match-end 2) (match-string 2)))
  6073. (and (match-end 1)
  6074. (delete-region (match-beginning 1) (match-end 1)))
  6075. (setq pos (point-at-bol))
  6076. (or split (end-of-line 1))
  6077. (delete-horizontal-space)
  6078. (if (string-match "\\`\\*+\\'"
  6079. (buffer-substring (point-at-bol) (point)))
  6080. (insert " "))
  6081. (newline (if blank 2 1))
  6082. (when tags
  6083. (save-excursion
  6084. (goto-char pos)
  6085. (end-of-line 1)
  6086. (insert " " tags)
  6087. (org-set-tags nil 'align))))
  6088. (t
  6089. (or split (end-of-line 1))
  6090. (newline (if blank 2 1)))))))
  6091. (insert head) (just-one-space)
  6092. (setq pos (point))
  6093. (end-of-line 1)
  6094. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6095. (when (and org-insert-heading-respect-content hide-previous)
  6096. (save-excursion
  6097. (goto-char previous-pos)
  6098. (hide-subtree)))
  6099. (run-hooks 'org-insert-heading-hook)))))
  6100. (defun org-get-heading (&optional no-tags)
  6101. "Return the heading of the current entry, without the stars."
  6102. (save-excursion
  6103. (org-back-to-heading t)
  6104. (if (looking-at
  6105. (if no-tags
  6106. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6107. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6108. (match-string 1) "")))
  6109. (defun org-heading-components ()
  6110. "Return the components of the current heading.
  6111. This is a list with the following elements:
  6112. - the level as an integer
  6113. - the reduced level, different if `org-odd-levels-only' is set.
  6114. - the TODO keyword, or nil
  6115. - the priority character, like ?A, or nil if no priority is given
  6116. - the headline text itself, or the tags string if no headline text
  6117. - the tags string, or nil."
  6118. (save-excursion
  6119. (org-back-to-heading t)
  6120. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6121. (list (length (match-string 1))
  6122. (org-reduced-level (length (match-string 1)))
  6123. (org-match-string-no-properties 2)
  6124. (and (match-end 3) (aref (match-string 3) 2))
  6125. (org-match-string-no-properties 4)
  6126. (org-match-string-no-properties 5)))))
  6127. (defun org-get-entry ()
  6128. "Get the entry text, after heading, entire subtree."
  6129. (save-excursion
  6130. (org-back-to-heading t)
  6131. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6132. (defun org-insert-heading-after-current ()
  6133. "Insert a new heading with same level as current, after current subtree."
  6134. (interactive)
  6135. (org-back-to-heading)
  6136. (org-insert-heading)
  6137. (org-move-subtree-down)
  6138. (end-of-line 1))
  6139. (defun org-insert-heading-respect-content ()
  6140. (interactive)
  6141. (let ((org-insert-heading-respect-content t))
  6142. (org-insert-heading t)))
  6143. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6144. (interactive "P")
  6145. (let ((org-insert-heading-respect-content t))
  6146. (org-insert-todo-heading force-state t)))
  6147. (defun org-insert-todo-heading (arg &optional force-heading)
  6148. "Insert a new heading with the same level and TODO state as current heading.
  6149. If the heading has no TODO state, or if the state is DONE, use the first
  6150. state (TODO by default). Also with prefix arg, force first state."
  6151. (interactive "P")
  6152. (when (or force-heading (not (org-insert-item 'checkbox)))
  6153. (org-insert-heading force-heading)
  6154. (save-excursion
  6155. (org-back-to-heading)
  6156. (outline-previous-heading)
  6157. (looking-at org-todo-line-regexp))
  6158. (let*
  6159. ((new-mark-x
  6160. (if (or arg
  6161. (not (match-beginning 2))
  6162. (member (match-string 2) org-done-keywords))
  6163. (car org-todo-keywords-1)
  6164. (match-string 2)))
  6165. (new-mark
  6166. (or
  6167. (run-hook-with-args-until-success
  6168. 'org-todo-get-default-hook new-mark-x nil)
  6169. new-mark-x)))
  6170. (beginning-of-line 1)
  6171. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6172. (if org-treat-insert-todo-heading-as-state-change
  6173. (org-todo new-mark)
  6174. (insert new-mark " "))))
  6175. (when org-provide-todo-statistics
  6176. (org-update-parent-todo-statistics))))
  6177. (defun org-insert-subheading (arg)
  6178. "Insert a new subheading and demote it.
  6179. Works for outline headings and for plain lists alike."
  6180. (interactive "P")
  6181. (org-insert-heading arg)
  6182. (cond
  6183. ((org-on-heading-p) (org-do-demote))
  6184. ((org-at-item-p) (org-indent-item))))
  6185. (defun org-insert-todo-subheading (arg)
  6186. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6187. Works for outline headings and for plain lists alike."
  6188. (interactive "P")
  6189. (org-insert-todo-heading arg)
  6190. (cond
  6191. ((org-on-heading-p) (org-do-demote))
  6192. ((org-at-item-p) (org-indent-item))))
  6193. ;;; Promotion and Demotion
  6194. (defvar org-after-demote-entry-hook nil
  6195. "Hook run after an entry has been demoted.
  6196. The cursor will be at the beginning of the entry.
  6197. When a subtree is being demoted, the hook will be called for each node.")
  6198. (defvar org-after-promote-entry-hook nil
  6199. "Hook run after an entry has been promoted.
  6200. The cursor will be at the beginning of the entry.
  6201. When a subtree is being promoted, the hook will be called for each node.")
  6202. (defun org-promote-subtree ()
  6203. "Promote the entire subtree.
  6204. See also `org-promote'."
  6205. (interactive)
  6206. (save-excursion
  6207. (org-map-tree 'org-promote))
  6208. (org-fix-position-after-promote))
  6209. (defun org-demote-subtree ()
  6210. "Demote the entire subtree. See `org-demote'.
  6211. See also `org-promote'."
  6212. (interactive)
  6213. (save-excursion
  6214. (org-map-tree 'org-demote))
  6215. (org-fix-position-after-promote))
  6216. (defun org-do-promote ()
  6217. "Promote the current heading higher up the tree.
  6218. If the region is active in `transient-mark-mode', promote all headings
  6219. in the region."
  6220. (interactive)
  6221. (save-excursion
  6222. (if (org-region-active-p)
  6223. (org-map-region 'org-promote (region-beginning) (region-end))
  6224. (org-promote)))
  6225. (org-fix-position-after-promote))
  6226. (defun org-do-demote ()
  6227. "Demote the current heading lower down the tree.
  6228. If the region is active in `transient-mark-mode', demote all headings
  6229. in the region."
  6230. (interactive)
  6231. (save-excursion
  6232. (if (org-region-active-p)
  6233. (org-map-region 'org-demote (region-beginning) (region-end))
  6234. (org-demote)))
  6235. (org-fix-position-after-promote))
  6236. (defun org-fix-position-after-promote ()
  6237. "Make sure that after pro/demotion cursor position is right."
  6238. (let ((pos (point)))
  6239. (when (save-excursion
  6240. (beginning-of-line 1)
  6241. (looking-at org-todo-line-regexp)
  6242. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6243. (cond ((eobp) (insert " "))
  6244. ((eolp) (insert " "))
  6245. ((equal (char-after) ?\ ) (forward-char 1))))))
  6246. (defun org-current-level ()
  6247. "Return the level of the current entry, or nil if before the first headline.
  6248. The level is the number of stars at the beginning of the headline."
  6249. (save-excursion
  6250. (condition-case nil
  6251. (progn
  6252. (org-back-to-heading t)
  6253. (funcall outline-level))
  6254. (error nil))))
  6255. (defun org-get-previous-line-level ()
  6256. "Return the outline depth of the last headline before the current line.
  6257. Returns 0 for the first headline in the buffer, and nil if before the
  6258. first headline."
  6259. (let ((current-level (org-current-level))
  6260. (prev-level (when (> (line-number-at-pos) 1)
  6261. (save-excursion
  6262. (beginning-of-line 0)
  6263. (org-current-level)))))
  6264. (cond ((null current-level) nil) ; Before first headline
  6265. ((null prev-level) 0) ; At first headline
  6266. (prev-level))))
  6267. (defun org-reduced-level (l)
  6268. "Compute the effective level of a heading.
  6269. This takes into account the setting of `org-odd-levels-only'."
  6270. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6271. (defun org-level-increment ()
  6272. "Return the number of stars that will be added or removed at a
  6273. time to headlines when structure editing, based on the value of
  6274. `org-odd-levels-only'."
  6275. (if org-odd-levels-only 2 1))
  6276. (defun org-get-valid-level (level &optional change)
  6277. "Rectify a level change under the influence of `org-odd-levels-only'
  6278. LEVEL is a current level, CHANGE is by how much the level should be
  6279. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6280. even level numbers will become the next higher odd number."
  6281. (if org-odd-levels-only
  6282. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6283. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6284. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6285. (max 1 (+ level (or change 0)))))
  6286. (if (boundp 'define-obsolete-function-alias)
  6287. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6288. (define-obsolete-function-alias 'org-get-legal-level
  6289. 'org-get-valid-level)
  6290. (define-obsolete-function-alias 'org-get-legal-level
  6291. 'org-get-valid-level "23.1")))
  6292. (defun org-promote ()
  6293. "Promote the current heading higher up the tree.
  6294. If the region is active in `transient-mark-mode', promote all headings
  6295. in the region."
  6296. (org-back-to-heading t)
  6297. (let* ((level (save-match-data (funcall outline-level)))
  6298. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6299. (diff (abs (- level (length up-head) -1))))
  6300. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6301. (replace-match up-head nil t)
  6302. ;; Fixup tag positioning
  6303. (and org-auto-align-tags (org-set-tags nil t))
  6304. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6305. (run-hooks 'org-after-promote-entry-hook)))
  6306. (defun org-demote ()
  6307. "Demote the current heading lower down the tree.
  6308. If the region is active in `transient-mark-mode', demote all headings
  6309. in the region."
  6310. (org-back-to-heading t)
  6311. (let* ((level (save-match-data (funcall outline-level)))
  6312. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6313. (diff (abs (- level (length down-head) -1))))
  6314. (replace-match down-head nil t)
  6315. ;; Fixup tag positioning
  6316. (and org-auto-align-tags (org-set-tags nil t))
  6317. (if org-adapt-indentation (org-fixup-indentation diff))
  6318. (run-hooks 'org-after-demote-entry-hook)))
  6319. (defun org-cycle-level ()
  6320. "Cycle the level of an empty headline through possible states.
  6321. This goes first to child, then to parent, level, then up the hierarchy.
  6322. After top level, it switches back to sibling level."
  6323. (interactive)
  6324. (let ((org-adapt-indentation nil))
  6325. (when (org-point-at-end-of-empty-headline)
  6326. (setq this-command 'org-cycle-level) ; Only needed for caching
  6327. (let ((cur-level (org-current-level))
  6328. (prev-level (org-get-previous-line-level)))
  6329. (cond
  6330. ;; If first headline in file, promote to top-level.
  6331. ((= prev-level 0)
  6332. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6333. do (org-do-promote)))
  6334. ;; If same level as prev, demote one.
  6335. ((= prev-level cur-level)
  6336. (org-do-demote))
  6337. ;; If parent is top-level, promote to top level if not already.
  6338. ((= prev-level 1)
  6339. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6340. do (org-do-promote)))
  6341. ;; If top-level, return to prev-level.
  6342. ((= cur-level 1)
  6343. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6344. do (org-do-demote)))
  6345. ;; If less than prev-level, promote one.
  6346. ((< cur-level prev-level)
  6347. (org-do-promote))
  6348. ;; If deeper than prev-level, promote until higher than
  6349. ;; prev-level.
  6350. ((> cur-level prev-level)
  6351. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6352. do (org-do-promote))))
  6353. t))))
  6354. (defun org-map-tree (fun)
  6355. "Call FUN for every heading underneath the current one."
  6356. (org-back-to-heading)
  6357. (let ((level (funcall outline-level)))
  6358. (save-excursion
  6359. (funcall fun)
  6360. (while (and (progn
  6361. (outline-next-heading)
  6362. (> (funcall outline-level) level))
  6363. (not (eobp)))
  6364. (funcall fun)))))
  6365. (defun org-map-region (fun beg end)
  6366. "Call FUN for every heading between BEG and END."
  6367. (let ((org-ignore-region t))
  6368. (save-excursion
  6369. (setq end (copy-marker end))
  6370. (goto-char beg)
  6371. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6372. (< (point) end))
  6373. (funcall fun))
  6374. (while (and (progn
  6375. (outline-next-heading)
  6376. (< (point) end))
  6377. (not (eobp)))
  6378. (funcall fun)))))
  6379. (defun org-fixup-indentation (diff)
  6380. "Change the indentation in the current entry by DIFF.
  6381. However, if any line in the current entry has no indentation, or if it
  6382. would end up with no indentation after the change, nothing at all is done."
  6383. (save-excursion
  6384. (let ((end (save-excursion (outline-next-heading)
  6385. (point-marker)))
  6386. (prohibit (if (> diff 0)
  6387. "^\\S-"
  6388. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6389. col)
  6390. (unless (save-excursion (end-of-line 1)
  6391. (re-search-forward prohibit end t))
  6392. (while (and (< (point) end)
  6393. (re-search-forward "^[ \t]+" end t))
  6394. (goto-char (match-end 0))
  6395. (setq col (current-column))
  6396. (if (< diff 0) (replace-match ""))
  6397. (org-indent-to-column (+ diff col))))
  6398. (move-marker end nil))))
  6399. (defun org-convert-to-odd-levels ()
  6400. "Convert an org-mode file with all levels allowed to one with odd levels.
  6401. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6402. level 5 etc."
  6403. (interactive)
  6404. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6405. (let ((outline-regexp org-outline-regexp)
  6406. (outline-level 'org-outline-level)
  6407. (org-odd-levels-only nil) n)
  6408. (save-excursion
  6409. (goto-char (point-min))
  6410. (while (re-search-forward "^\\*\\*+ " nil t)
  6411. (setq n (- (length (match-string 0)) 2))
  6412. (while (>= (setq n (1- n)) 0)
  6413. (org-demote))
  6414. (end-of-line 1))))))
  6415. (defun org-convert-to-oddeven-levels ()
  6416. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6417. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6418. file contains a section with an even level, conversion would
  6419. destroy the structure of the file. An error is signaled in this
  6420. case."
  6421. (interactive)
  6422. (goto-char (point-min))
  6423. ;; First check if there are no even levels
  6424. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6425. (org-show-context t)
  6426. (error "Not all levels are odd in this file. Conversion not possible"))
  6427. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6428. (let ((outline-regexp org-outline-regexp)
  6429. (outline-level 'org-outline-level)
  6430. (org-odd-levels-only nil) n)
  6431. (save-excursion
  6432. (goto-char (point-min))
  6433. (while (re-search-forward "^\\*\\*+ " nil t)
  6434. (setq n (/ (1- (length (match-string 0))) 2))
  6435. (while (>= (setq n (1- n)) 0)
  6436. (org-promote))
  6437. (end-of-line 1))))))
  6438. (defun org-tr-level (n)
  6439. "Make N odd if required."
  6440. (if org-odd-levels-only (1+ (/ n 2)) n))
  6441. ;;; Vertical tree motion, cutting and pasting of subtrees
  6442. (defun org-move-subtree-up (&optional arg)
  6443. "Move the current subtree up past ARG headlines of the same level."
  6444. (interactive "p")
  6445. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6446. (defun org-move-subtree-down (&optional arg)
  6447. "Move the current subtree down past ARG headlines of the same level."
  6448. (interactive "p")
  6449. (setq arg (prefix-numeric-value arg))
  6450. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6451. 'org-get-last-sibling))
  6452. (ins-point (make-marker))
  6453. (cnt (abs arg))
  6454. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6455. ;; Select the tree
  6456. (org-back-to-heading)
  6457. (setq beg0 (point))
  6458. (save-excursion
  6459. (setq ne-beg (org-back-over-empty-lines))
  6460. (setq beg (point)))
  6461. (save-match-data
  6462. (save-excursion (outline-end-of-heading)
  6463. (setq folded (org-invisible-p)))
  6464. (outline-end-of-subtree))
  6465. (outline-next-heading)
  6466. (setq ne-end (org-back-over-empty-lines))
  6467. (setq end (point))
  6468. (goto-char beg0)
  6469. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6470. ;; include less whitespace
  6471. (save-excursion
  6472. (goto-char beg)
  6473. (forward-line (- ne-beg ne-end))
  6474. (setq beg (point))))
  6475. ;; Find insertion point, with error handling
  6476. (while (> cnt 0)
  6477. (or (and (funcall movfunc) (looking-at outline-regexp))
  6478. (progn (goto-char beg0)
  6479. (error "Cannot move past superior level or buffer limit")))
  6480. (setq cnt (1- cnt)))
  6481. (if (> arg 0)
  6482. ;; Moving forward - still need to move over subtree
  6483. (progn (org-end-of-subtree t t)
  6484. (save-excursion
  6485. (org-back-over-empty-lines)
  6486. (or (bolp) (newline)))))
  6487. (setq ne-ins (org-back-over-empty-lines))
  6488. (move-marker ins-point (point))
  6489. (setq txt (buffer-substring beg end))
  6490. (org-save-markers-in-region beg end)
  6491. (delete-region beg end)
  6492. (org-remove-empty-overlays-at beg)
  6493. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6494. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6495. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6496. (let ((bbb (point)))
  6497. (insert-before-markers txt)
  6498. (org-reinstall-markers-in-region bbb)
  6499. (move-marker ins-point bbb))
  6500. (or (bolp) (insert "\n"))
  6501. (setq ins-end (point))
  6502. (goto-char ins-point)
  6503. (org-skip-whitespace)
  6504. (when (and (< arg 0)
  6505. (org-first-sibling-p)
  6506. (> ne-ins ne-beg))
  6507. ;; Move whitespace back to beginning
  6508. (save-excursion
  6509. (goto-char ins-end)
  6510. (let ((kill-whole-line t))
  6511. (kill-line (- ne-ins ne-beg)) (point)))
  6512. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6513. (move-marker ins-point nil)
  6514. (if folded
  6515. (hide-subtree)
  6516. (org-show-entry)
  6517. (show-children)
  6518. (org-cycle-hide-drawers 'children))
  6519. (org-clean-visibility-after-subtree-move)))
  6520. (defvar org-subtree-clip ""
  6521. "Clipboard for cut and paste of subtrees.
  6522. This is actually only a copy of the kill, because we use the normal kill
  6523. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6524. (defvar org-subtree-clip-folded nil
  6525. "Was the last copied subtree folded?
  6526. This is used to fold the tree back after pasting.")
  6527. (defun org-cut-subtree (&optional n)
  6528. "Cut the current subtree into the clipboard.
  6529. With prefix arg N, cut this many sequential subtrees.
  6530. This is a short-hand for marking the subtree and then cutting it."
  6531. (interactive "p")
  6532. (org-copy-subtree n 'cut))
  6533. (defun org-copy-subtree (&optional n cut force-store-markers)
  6534. "Cut the current subtree into the clipboard.
  6535. With prefix arg N, cut this many sequential subtrees.
  6536. This is a short-hand for marking the subtree and then copying it.
  6537. If CUT is non-nil, actually cut the subtree.
  6538. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6539. of some markers in the region, even if CUT is non-nil. This is
  6540. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6541. (interactive "p")
  6542. (let (beg end folded (beg0 (point)))
  6543. (if (interactive-p)
  6544. (org-back-to-heading nil) ; take what looks like a subtree
  6545. (org-back-to-heading t)) ; take what is really there
  6546. (org-back-over-empty-lines)
  6547. (setq beg (point))
  6548. (skip-chars-forward " \t\r\n")
  6549. (save-match-data
  6550. (save-excursion (outline-end-of-heading)
  6551. (setq folded (org-invisible-p)))
  6552. (condition-case nil
  6553. (org-forward-same-level (1- n) t)
  6554. (error nil))
  6555. (org-end-of-subtree t t))
  6556. (org-back-over-empty-lines)
  6557. (setq end (point))
  6558. (goto-char beg0)
  6559. (when (> end beg)
  6560. (setq org-subtree-clip-folded folded)
  6561. (when (or cut force-store-markers)
  6562. (org-save-markers-in-region beg end))
  6563. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6564. (setq org-subtree-clip (current-kill 0))
  6565. (message "%s: Subtree(s) with %d characters"
  6566. (if cut "Cut" "Copied")
  6567. (length org-subtree-clip)))))
  6568. (defun org-paste-subtree (&optional level tree for-yank)
  6569. "Paste the clipboard as a subtree, with modification of headline level.
  6570. The entire subtree is promoted or demoted in order to match a new headline
  6571. level.
  6572. If the cursor is at the beginning of a headline, the same level as
  6573. that headline is used to paste the tree
  6574. If not, the new level is derived from the *visible* headings
  6575. before and after the insertion point, and taken to be the inferior headline
  6576. level of the two. So if the previous visible heading is level 3 and the
  6577. next is level 4 (or vice versa), level 4 will be used for insertion.
  6578. This makes sure that the subtree remains an independent subtree and does
  6579. not swallow low level entries.
  6580. You can also force a different level, either by using a numeric prefix
  6581. argument, or by inserting the heading marker by hand. For example, if the
  6582. cursor is after \"*****\", then the tree will be shifted to level 5.
  6583. If optional TREE is given, use this text instead of the kill ring.
  6584. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6585. move back over whitespace before inserting, and move point to the end of
  6586. the inserted text when done."
  6587. (interactive "P")
  6588. (setq tree (or tree (and kill-ring (current-kill 0))))
  6589. (unless (org-kill-is-subtree-p tree)
  6590. (error "%s"
  6591. (substitute-command-keys
  6592. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6593. (let* ((visp (not (org-invisible-p)))
  6594. (txt tree)
  6595. (^re (concat "^\\(" outline-regexp "\\)"))
  6596. (re (concat "\\(" outline-regexp "\\)"))
  6597. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6598. (old-level (if (string-match ^re txt)
  6599. (- (match-end 0) (match-beginning 0) 1)
  6600. -1))
  6601. (force-level (cond (level (prefix-numeric-value level))
  6602. ((and (looking-at "[ \t]*$")
  6603. (string-match
  6604. ^re_ (buffer-substring
  6605. (point-at-bol) (point))))
  6606. (- (match-end 1) (match-beginning 1)))
  6607. ((and (bolp)
  6608. (looking-at org-outline-regexp))
  6609. (- (match-end 0) (point) 1))
  6610. (t nil)))
  6611. (previous-level (save-excursion
  6612. (condition-case nil
  6613. (progn
  6614. (outline-previous-visible-heading 1)
  6615. (if (looking-at re)
  6616. (- (match-end 0) (match-beginning 0) 1)
  6617. 1))
  6618. (error 1))))
  6619. (next-level (save-excursion
  6620. (condition-case nil
  6621. (progn
  6622. (or (looking-at outline-regexp)
  6623. (outline-next-visible-heading 1))
  6624. (if (looking-at re)
  6625. (- (match-end 0) (match-beginning 0) 1)
  6626. 1))
  6627. (error 1))))
  6628. (new-level (or force-level (max previous-level next-level)))
  6629. (shift (if (or (= old-level -1)
  6630. (= new-level -1)
  6631. (= old-level new-level))
  6632. 0
  6633. (- new-level old-level)))
  6634. (delta (if (> shift 0) -1 1))
  6635. (func (if (> shift 0) 'org-demote 'org-promote))
  6636. (org-odd-levels-only nil)
  6637. beg end newend)
  6638. ;; Remove the forced level indicator
  6639. (if force-level
  6640. (delete-region (point-at-bol) (point)))
  6641. ;; Paste
  6642. (beginning-of-line 1)
  6643. (unless for-yank (org-back-over-empty-lines))
  6644. (setq beg (point))
  6645. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6646. (insert-before-markers txt)
  6647. (unless (string-match "\n\\'" txt) (insert "\n"))
  6648. (setq newend (point))
  6649. (org-reinstall-markers-in-region beg)
  6650. (setq end (point))
  6651. (goto-char beg)
  6652. (skip-chars-forward " \t\n\r")
  6653. (setq beg (point))
  6654. (if (and (org-invisible-p) visp)
  6655. (save-excursion (outline-show-heading)))
  6656. ;; Shift if necessary
  6657. (unless (= shift 0)
  6658. (save-restriction
  6659. (narrow-to-region beg end)
  6660. (while (not (= shift 0))
  6661. (org-map-region func (point-min) (point-max))
  6662. (setq shift (+ delta shift)))
  6663. (goto-char (point-min))
  6664. (setq newend (point-max))))
  6665. (when (or (interactive-p) for-yank)
  6666. (message "Clipboard pasted as level %d subtree" new-level))
  6667. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6668. kill-ring
  6669. (eq org-subtree-clip (current-kill 0))
  6670. org-subtree-clip-folded)
  6671. ;; The tree was folded before it was killed/copied
  6672. (hide-subtree))
  6673. (and for-yank (goto-char newend))))
  6674. (defun org-kill-is-subtree-p (&optional txt)
  6675. "Check if the current kill is an outline subtree, or a set of trees.
  6676. Returns nil if kill does not start with a headline, or if the first
  6677. headline level is not the largest headline level in the tree.
  6678. So this will actually accept several entries of equal levels as well,
  6679. which is OK for `org-paste-subtree'.
  6680. If optional TXT is given, check this string instead of the current kill."
  6681. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6682. (start-level (and kill
  6683. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6684. org-outline-regexp "\\)")
  6685. kill)
  6686. (- (match-end 2) (match-beginning 2) 1)))
  6687. (re (concat "^" org-outline-regexp))
  6688. (start (1+ (or (match-beginning 2) -1))))
  6689. (if (not start-level)
  6690. (progn
  6691. nil) ;; does not even start with a heading
  6692. (catch 'exit
  6693. (while (setq start (string-match re kill (1+ start)))
  6694. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6695. (throw 'exit nil)))
  6696. t))))
  6697. (defvar org-markers-to-move nil
  6698. "Markers that should be moved with a cut-and-paste operation.
  6699. Those markers are stored together with their positions relative to
  6700. the start of the region.")
  6701. (defun org-save-markers-in-region (beg end)
  6702. "Check markers in region.
  6703. If these markers are between BEG and END, record their position relative
  6704. to BEG, so that after moving the block of text, we can put the markers back
  6705. into place.
  6706. This function gets called just before an entry or tree gets cut from the
  6707. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6708. called immediately, to move the markers with the entries."
  6709. (setq org-markers-to-move nil)
  6710. (when (featurep 'org-clock)
  6711. (org-clock-save-markers-for-cut-and-paste beg end))
  6712. (when (featurep 'org-agenda)
  6713. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6714. (defun org-check-and-save-marker (marker beg end)
  6715. "Check if MARKER is between BEG and END.
  6716. If yes, remember the marker and the distance to BEG."
  6717. (when (and (marker-buffer marker)
  6718. (equal (marker-buffer marker) (current-buffer)))
  6719. (if (and (>= marker beg) (< marker end))
  6720. (push (cons marker (- marker beg)) org-markers-to-move))))
  6721. (defun org-reinstall-markers-in-region (beg)
  6722. "Move all remembered markers to their position relative to BEG."
  6723. (mapc (lambda (x)
  6724. (move-marker (car x) (+ beg (cdr x))))
  6725. org-markers-to-move)
  6726. (setq org-markers-to-move nil))
  6727. (defun org-narrow-to-subtree ()
  6728. "Narrow buffer to the current subtree."
  6729. (interactive)
  6730. (save-excursion
  6731. (save-match-data
  6732. (narrow-to-region
  6733. (progn (org-back-to-heading t) (point))
  6734. (progn (org-end-of-subtree t t)
  6735. (if (org-on-heading-p) (backward-char 1))
  6736. (point))))))
  6737. (eval-when-compile
  6738. (defvar org-property-drawer-re))
  6739. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6740. "Clone the task (subtree) at point N times.
  6741. The clones will be inserted as siblings.
  6742. In interactive use, the user will be prompted for the number of
  6743. clones to be produced, and for a time SHIFT, which may be a
  6744. repeater as used in time stamps, for example `+3d'.
  6745. When a valid repeater is given and the entry contains any time
  6746. stamps, the clones will become a sequence in time, with time
  6747. stamps in the subtree shifted for each clone produced. If SHIFT
  6748. is nil or the empty string, time stamps will be left alone. The
  6749. ID property of the original subtree is removed.
  6750. If the original subtree did contain time stamps with a repeater,
  6751. the following will happen:
  6752. - the repeater will be removed in each clone
  6753. - an additional clone will be produced, with the current, unshifted
  6754. date(s) in the entry.
  6755. - the original entry will be placed *after* all the clones, with
  6756. repeater intact.
  6757. - the start days in the repeater in the original entry will be shifted
  6758. to past the last clone.
  6759. I this way you can spell out a number of instances of a repeating task,
  6760. and still retain the repeater to cover future instances of the task."
  6761. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6762. (let (beg end template task idprop
  6763. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6764. (if (not (and (integerp n) (> n 0)))
  6765. (error "Invalid number of replications %s" n))
  6766. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6767. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6768. shift)))
  6769. (error "Invalid shift specification %s" shift))
  6770. (when doshift
  6771. (setq shift-n (string-to-number (match-string 1 shift))
  6772. shift-what (cdr (assoc (match-string 2 shift)
  6773. '(("d" . day) ("w" . week)
  6774. ("m" . month) ("y" . year))))))
  6775. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6776. (setq nmin 1 nmax n)
  6777. (org-back-to-heading t)
  6778. (setq beg (point))
  6779. (setq idprop (org-entry-get nil "ID"))
  6780. (org-end-of-subtree t t)
  6781. (or (bolp) (insert "\n"))
  6782. (setq end (point))
  6783. (setq template (buffer-substring beg end))
  6784. (when (and doshift
  6785. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6786. (delete-region beg end)
  6787. (setq end beg)
  6788. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6789. (goto-char end)
  6790. (loop for n from nmin to nmax do
  6791. ;; prepare clone
  6792. (with-temp-buffer
  6793. (insert template)
  6794. (org-mode)
  6795. (goto-char (point-min))
  6796. (and idprop (if org-clone-delete-id
  6797. (org-entry-delete nil "ID")
  6798. (org-id-get-create t)))
  6799. (while (re-search-forward org-property-drawer-re nil t)
  6800. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6801. (goto-char (point-min))
  6802. (when doshift
  6803. (while (re-search-forward org-ts-regexp-both nil t)
  6804. (org-timestamp-change (* n shift-n) shift-what))
  6805. (unless (= n n-no-remove)
  6806. (goto-char (point-min))
  6807. (while (re-search-forward org-ts-regexp nil t)
  6808. (save-excursion
  6809. (goto-char (match-beginning 0))
  6810. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6811. (delete-region (match-beginning 1) (match-end 1)))))))
  6812. (setq task (buffer-string)))
  6813. (insert task))
  6814. (goto-char beg)))
  6815. ;;; Outline Sorting
  6816. (defun org-sort (with-case)
  6817. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6818. Optional argument WITH-CASE means sort case-sensitively.
  6819. With a double prefix argument, also remove duplicate entries."
  6820. (interactive "P")
  6821. (cond
  6822. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6823. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6824. (t
  6825. (org-call-with-arg 'org-sort-entries with-case))))
  6826. (defun org-sort-remove-invisible (s)
  6827. (remove-text-properties 0 (length s) org-rm-props s)
  6828. (while (string-match org-bracket-link-regexp s)
  6829. (setq s (replace-match (if (match-end 2)
  6830. (match-string 3 s)
  6831. (match-string 1 s)) t t s)))
  6832. s)
  6833. (defvar org-priority-regexp) ; defined later in the file
  6834. (defvar org-after-sorting-entries-or-items-hook nil
  6835. "Hook that is run after a bunch of entries or items have been sorted.
  6836. When children are sorted, the cursor is in the parent line when this
  6837. hook gets called. When a region or a plain list is sorted, the cursor
  6838. will be in the first entry of the sorted region/list.")
  6839. (defun org-sort-entries
  6840. (&optional with-case sorting-type getkey-func compare-func property)
  6841. "Sort entries on a certain level of an outline tree.
  6842. If there is an active region, the entries in the region are sorted.
  6843. Else, if the cursor is before the first entry, sort the top-level items.
  6844. Else, the children of the entry at point are sorted.
  6845. Sorting can be alphabetically, numerically, by date/time as given by
  6846. a time stamp, by a property or by priority.
  6847. The command prompts for the sorting type unless it has been given to the
  6848. function through the SORTING-TYPE argument, which needs to be a character,
  6849. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6850. precise meaning of each character:
  6851. n Numerically, by converting the beginning of the entry/item to a number.
  6852. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6853. t By date/time, either the first active time stamp in the entry, or, if
  6854. none exist, by the first inactive one.
  6855. s By the scheduled date/time.
  6856. d By deadline date/time.
  6857. c By creation time, which is assumed to be the first inactive time stamp
  6858. at the beginning of a line.
  6859. p By priority according to the cookie.
  6860. r By the value of a property.
  6861. Capital letters will reverse the sort order.
  6862. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6863. called with point at the beginning of the record. It must return either
  6864. a string or a number that should serve as the sorting key for that record.
  6865. Comparing entries ignores case by default. However, with an optional argument
  6866. WITH-CASE, the sorting considers case as well."
  6867. (interactive "P")
  6868. (let ((case-func (if with-case 'identity 'downcase))
  6869. start beg end stars re re2
  6870. txt what tmp)
  6871. ;; Find beginning and end of region to sort
  6872. (cond
  6873. ((org-region-active-p)
  6874. ;; we will sort the region
  6875. (setq end (region-end)
  6876. what "region")
  6877. (goto-char (region-beginning))
  6878. (if (not (org-on-heading-p)) (outline-next-heading))
  6879. (setq start (point)))
  6880. ((or (org-on-heading-p)
  6881. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6882. ;; we will sort the children of the current headline
  6883. (org-back-to-heading)
  6884. (setq start (point)
  6885. end (progn (org-end-of-subtree t t)
  6886. (or (bolp) (insert "\n"))
  6887. (org-back-over-empty-lines)
  6888. (point))
  6889. what "children")
  6890. (goto-char start)
  6891. (show-subtree)
  6892. (outline-next-heading))
  6893. (t
  6894. ;; we will sort the top-level entries in this file
  6895. (goto-char (point-min))
  6896. (or (org-on-heading-p) (outline-next-heading))
  6897. (setq start (point))
  6898. (goto-char (point-max))
  6899. (beginning-of-line 1)
  6900. (when (looking-at ".*?\\S-")
  6901. ;; File ends in a non-white line
  6902. (end-of-line 1)
  6903. (insert "\n"))
  6904. (setq end (point-max))
  6905. (setq what "top-level")
  6906. (goto-char start)
  6907. (show-all)))
  6908. (setq beg (point))
  6909. (if (>= beg end) (error "Nothing to sort"))
  6910. (looking-at "\\(\\*+\\)")
  6911. (setq stars (match-string 1)
  6912. re (concat "^" (regexp-quote stars) " +")
  6913. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6914. txt (buffer-substring beg end))
  6915. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6916. (if (and (not (equal stars "*")) (string-match re2 txt))
  6917. (error "Region to sort contains a level above the first entry"))
  6918. (unless sorting-type
  6919. (message
  6920. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6921. [t]ime [s]cheduled [d]eadline [c]reated
  6922. A/N/T/S/D/C/P/O/F means reversed:"
  6923. what)
  6924. (setq sorting-type (read-char-exclusive))
  6925. (and (= (downcase sorting-type) ?f)
  6926. (setq getkey-func
  6927. (org-icompleting-read "Sort using function: "
  6928. obarray 'fboundp t nil nil))
  6929. (setq getkey-func (intern getkey-func)))
  6930. (and (= (downcase sorting-type) ?r)
  6931. (setq property
  6932. (org-icompleting-read "Property: "
  6933. (mapcar 'list (org-buffer-property-keys t))
  6934. nil t))))
  6935. (message "Sorting entries...")
  6936. (save-restriction
  6937. (narrow-to-region start end)
  6938. (let ((dcst (downcase sorting-type))
  6939. (case-fold-search nil)
  6940. (now (current-time)))
  6941. (sort-subr
  6942. (/= dcst sorting-type)
  6943. ;; This function moves to the beginning character of the "record" to
  6944. ;; be sorted.
  6945. (lambda nil
  6946. (if (re-search-forward re nil t)
  6947. (goto-char (match-beginning 0))
  6948. (goto-char (point-max))))
  6949. ;; This function moves to the last character of the "record" being
  6950. ;; sorted.
  6951. (lambda nil
  6952. (save-match-data
  6953. (condition-case nil
  6954. (outline-forward-same-level 1)
  6955. (error
  6956. (goto-char (point-max))))))
  6957. ;; This function returns the value that gets sorted against.
  6958. (lambda nil
  6959. (cond
  6960. ((= dcst ?n)
  6961. (if (looking-at org-complex-heading-regexp)
  6962. (string-to-number (match-string 4))
  6963. nil))
  6964. ((= dcst ?a)
  6965. (if (looking-at org-complex-heading-regexp)
  6966. (funcall case-func (match-string 4))
  6967. nil))
  6968. ((= dcst ?t)
  6969. (let ((end (save-excursion (outline-next-heading) (point))))
  6970. (if (or (re-search-forward org-ts-regexp end t)
  6971. (re-search-forward org-ts-regexp-both end t))
  6972. (org-time-string-to-seconds (match-string 0))
  6973. (org-float-time now))))
  6974. ((= dcst ?c)
  6975. (let ((end (save-excursion (outline-next-heading) (point))))
  6976. (if (re-search-forward
  6977. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6978. end t)
  6979. (org-time-string-to-seconds (match-string 0))
  6980. (org-float-time now))))
  6981. ((= dcst ?s)
  6982. (let ((end (save-excursion (outline-next-heading) (point))))
  6983. (if (re-search-forward org-scheduled-time-regexp end t)
  6984. (org-time-string-to-seconds (match-string 1))
  6985. (org-float-time now))))
  6986. ((= dcst ?d)
  6987. (let ((end (save-excursion (outline-next-heading) (point))))
  6988. (if (re-search-forward org-deadline-time-regexp end t)
  6989. (org-time-string-to-seconds (match-string 1))
  6990. (org-float-time now))))
  6991. ((= dcst ?p)
  6992. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6993. (string-to-char (match-string 2))
  6994. org-default-priority))
  6995. ((= dcst ?r)
  6996. (or (org-entry-get nil property) ""))
  6997. ((= dcst ?o)
  6998. (if (looking-at org-complex-heading-regexp)
  6999. (- 9999 (length (member (match-string 2)
  7000. org-todo-keywords-1)))))
  7001. ((= dcst ?f)
  7002. (if getkey-func
  7003. (progn
  7004. (setq tmp (funcall getkey-func))
  7005. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7006. tmp)
  7007. (error "Invalid key function `%s'" getkey-func)))
  7008. (t (error "Invalid sorting type `%c'" sorting-type))))
  7009. nil
  7010. (cond
  7011. ((= dcst ?a) 'string<)
  7012. ((= dcst ?f) compare-func)
  7013. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7014. (t nil)))))
  7015. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7016. (message "Sorting entries...done")))
  7017. (defun org-do-sort (table what &optional with-case sorting-type)
  7018. "Sort TABLE of WHAT according to SORTING-TYPE.
  7019. The user will be prompted for the SORTING-TYPE if the call to this
  7020. function does not specify it. WHAT is only for the prompt, to indicate
  7021. what is being sorted. The sorting key will be extracted from
  7022. the car of the elements of the table.
  7023. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7024. (unless sorting-type
  7025. (message
  7026. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7027. what)
  7028. (setq sorting-type (read-char-exclusive)))
  7029. (let ((dcst (downcase sorting-type))
  7030. extractfun comparefun)
  7031. ;; Define the appropriate functions
  7032. (cond
  7033. ((= dcst ?n)
  7034. (setq extractfun 'string-to-number
  7035. comparefun (if (= dcst sorting-type) '< '>)))
  7036. ((= dcst ?a)
  7037. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7038. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7039. comparefun (if (= dcst sorting-type)
  7040. 'string<
  7041. (lambda (a b) (and (not (string< a b))
  7042. (not (string= a b)))))))
  7043. ((= dcst ?t)
  7044. (setq extractfun
  7045. (lambda (x)
  7046. (if (or (string-match org-ts-regexp x)
  7047. (string-match org-ts-regexp-both x))
  7048. (org-float-time
  7049. (org-time-string-to-time (match-string 0 x)))
  7050. 0))
  7051. comparefun (if (= dcst sorting-type) '< '>)))
  7052. (t (error "Invalid sorting type `%c'" sorting-type)))
  7053. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7054. table)
  7055. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7056. ;;; The orgstruct minor mode
  7057. ;; Define a minor mode which can be used in other modes in order to
  7058. ;; integrate the org-mode structure editing commands.
  7059. ;; This is really a hack, because the org-mode structure commands use
  7060. ;; keys which normally belong to the major mode. Here is how it
  7061. ;; works: The minor mode defines all the keys necessary to operate the
  7062. ;; structure commands, but wraps the commands into a function which
  7063. ;; tests if the cursor is currently at a headline or a plain list
  7064. ;; item. If that is the case, the structure command is used,
  7065. ;; temporarily setting many Org-mode variables like regular
  7066. ;; expressions for filling etc. However, when any of those keys is
  7067. ;; used at a different location, function uses `key-binding' to look
  7068. ;; up if the key has an associated command in another currently active
  7069. ;; keymap (minor modes, major mode, global), and executes that
  7070. ;; command. There might be problems if any of the keys is otherwise
  7071. ;; used as a prefix key.
  7072. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7073. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7074. ;; addresses this by checking explicitly for both bindings.
  7075. (defvar orgstruct-mode-map (make-sparse-keymap)
  7076. "Keymap for the minor `orgstruct-mode'.")
  7077. (defvar org-local-vars nil
  7078. "List of local variables, for use by `orgstruct-mode'.")
  7079. ;;;###autoload
  7080. (define-minor-mode orgstruct-mode
  7081. "Toggle the minor mode `orgstruct-mode'.
  7082. This mode is for using Org-mode structure commands in other
  7083. modes. The following keys behave as if Org-mode were active, if
  7084. the cursor is on a headline, or on a plain list item (both as
  7085. defined by Org-mode).
  7086. M-up Move entry/item up
  7087. M-down Move entry/item down
  7088. M-left Promote
  7089. M-right Demote
  7090. M-S-up Move entry/item up
  7091. M-S-down Move entry/item down
  7092. M-S-left Promote subtree
  7093. M-S-right Demote subtree
  7094. M-q Fill paragraph and items like in Org-mode
  7095. C-c ^ Sort entries
  7096. C-c - Cycle list bullet
  7097. TAB Cycle item visibility
  7098. M-RET Insert new heading/item
  7099. S-M-RET Insert new TODO heading / Checkbox item
  7100. C-c C-c Set tags / toggle checkbox"
  7101. nil " OrgStruct" nil
  7102. (org-load-modules-maybe)
  7103. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7104. ;;;###autoload
  7105. (defun turn-on-orgstruct ()
  7106. "Unconditionally turn on `orgstruct-mode'."
  7107. (orgstruct-mode 1))
  7108. (defun orgstruct++-mode (&optional arg)
  7109. "Toggle `orgstruct-mode', the enhanced version of it.
  7110. In addition to setting orgstruct-mode, this also exports all indentation
  7111. and autofilling variables from org-mode into the buffer. It will also
  7112. recognize item context in multiline items.
  7113. Note that turning off orgstruct-mode will *not* remove the
  7114. indentation/paragraph settings. This can only be done by refreshing the
  7115. major mode, for example with \\[normal-mode]."
  7116. (interactive "P")
  7117. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7118. (if (< arg 1)
  7119. (orgstruct-mode -1)
  7120. (orgstruct-mode 1)
  7121. (let (var val)
  7122. (mapc
  7123. (lambda (x)
  7124. (when (string-match
  7125. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7126. (symbol-name (car x)))
  7127. (setq var (car x) val (nth 1 x))
  7128. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7129. org-local-vars)
  7130. (org-set-local 'orgstruct-is-++ t))))
  7131. (defvar orgstruct-is-++ nil
  7132. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7133. (make-variable-buffer-local 'orgstruct-is-++)
  7134. ;;;###autoload
  7135. (defun turn-on-orgstruct++ ()
  7136. "Unconditionally turn on `orgstruct++-mode'."
  7137. (orgstruct++-mode 1))
  7138. (defun orgstruct-error ()
  7139. "Error when there is no default binding for a structure key."
  7140. (interactive)
  7141. (error "This key has no function outside structure elements"))
  7142. (defun orgstruct-setup ()
  7143. "Setup orgstruct keymaps."
  7144. (let ((nfunc 0)
  7145. (bindings
  7146. (list
  7147. '([(meta up)] org-metaup)
  7148. '([(meta down)] org-metadown)
  7149. '([(meta left)] org-metaleft)
  7150. '([(meta right)] org-metaright)
  7151. '([(meta shift up)] org-shiftmetaup)
  7152. '([(meta shift down)] org-shiftmetadown)
  7153. '([(meta shift left)] org-shiftmetaleft)
  7154. '([(meta shift right)] org-shiftmetaright)
  7155. '([?\e (up)] org-metaup)
  7156. '([?\e (down)] org-metadown)
  7157. '([?\e (left)] org-metaleft)
  7158. '([?\e (right)] org-metaright)
  7159. '([?\e (shift up)] org-shiftmetaup)
  7160. '([?\e (shift down)] org-shiftmetadown)
  7161. '([?\e (shift left)] org-shiftmetaleft)
  7162. '([?\e (shift right)] org-shiftmetaright)
  7163. '([(shift up)] org-shiftup)
  7164. '([(shift down)] org-shiftdown)
  7165. '([(shift left)] org-shiftleft)
  7166. '([(shift right)] org-shiftright)
  7167. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7168. '("\M-q" fill-paragraph)
  7169. '("\C-c^" org-sort)
  7170. '("\C-c-" org-cycle-list-bullet)))
  7171. elt key fun cmd)
  7172. (while (setq elt (pop bindings))
  7173. (setq nfunc (1+ nfunc))
  7174. (setq key (org-key (car elt))
  7175. fun (nth 1 elt)
  7176. cmd (orgstruct-make-binding fun nfunc key))
  7177. (org-defkey orgstruct-mode-map key cmd))
  7178. ;; Special treatment needed for TAB and RET
  7179. (org-defkey orgstruct-mode-map [(tab)]
  7180. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7181. (org-defkey orgstruct-mode-map "\C-i"
  7182. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7183. (org-defkey orgstruct-mode-map "\M-\C-m"
  7184. (orgstruct-make-binding 'org-insert-heading 105
  7185. "\M-\C-m" [(meta return)]))
  7186. (org-defkey orgstruct-mode-map [(meta return)]
  7187. (orgstruct-make-binding 'org-insert-heading 106
  7188. [(meta return)] "\M-\C-m"))
  7189. (org-defkey orgstruct-mode-map [(shift meta return)]
  7190. (orgstruct-make-binding 'org-insert-todo-heading 107
  7191. [(meta return)] "\M-\C-m"))
  7192. (org-defkey orgstruct-mode-map "\e\C-m"
  7193. (orgstruct-make-binding 'org-insert-heading 108
  7194. "\e\C-m" [?\e (return)]))
  7195. (org-defkey orgstruct-mode-map [?\e (return)]
  7196. (orgstruct-make-binding 'org-insert-heading 109
  7197. [?\e (return)] "\e\C-m"))
  7198. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7199. (orgstruct-make-binding 'org-insert-todo-heading 110
  7200. [?\e (return)] "\e\C-m"))
  7201. (unless org-local-vars
  7202. (setq org-local-vars (org-get-local-variables)))
  7203. t))
  7204. (defun orgstruct-make-binding (fun n &rest keys)
  7205. "Create a function for binding in the structure minor mode.
  7206. FUN is the command to call inside a table. N is used to create a unique
  7207. command name. KEYS are keys that should be checked in for a command
  7208. to execute outside of tables."
  7209. (eval
  7210. (list 'defun
  7211. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7212. '(arg)
  7213. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7214. "Outside of structure, run the binding of `"
  7215. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7216. "'.")
  7217. '(interactive "p")
  7218. (list 'if
  7219. `(org-context-p 'headline 'item
  7220. (and orgstruct-is-++
  7221. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7222. 'item-body))
  7223. (list 'org-run-like-in-org-mode (list 'quote fun))
  7224. (list 'let '(orgstruct-mode)
  7225. (list 'call-interactively
  7226. (append '(or)
  7227. (mapcar (lambda (k)
  7228. (list 'key-binding k))
  7229. keys)
  7230. '('orgstruct-error))))))))
  7231. (defun org-context-p (&rest contexts)
  7232. "Check if local context is any of CONTEXTS.
  7233. Possible values in the list of contexts are `table', `headline', and `item'."
  7234. (let ((pos (point)))
  7235. (goto-char (point-at-bol))
  7236. (prog1 (or (and (memq 'table contexts)
  7237. (looking-at "[ \t]*|"))
  7238. (and (memq 'headline contexts)
  7239. ;;????????? (looking-at "\\*+"))
  7240. (looking-at outline-regexp))
  7241. (and (memq 'item contexts)
  7242. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7243. (and (memq 'item-body contexts)
  7244. (org-in-item-p)))
  7245. (goto-char pos))))
  7246. (defun org-get-local-variables ()
  7247. "Return a list of all local variables in an org-mode buffer."
  7248. (let (varlist)
  7249. (with-current-buffer (get-buffer-create "*Org tmp*")
  7250. (erase-buffer)
  7251. (org-mode)
  7252. (setq varlist (buffer-local-variables)))
  7253. (kill-buffer "*Org tmp*")
  7254. (delq nil
  7255. (mapcar
  7256. (lambda (x)
  7257. (setq x
  7258. (if (symbolp x)
  7259. (list x)
  7260. (list (car x) (list 'quote (cdr x)))))
  7261. (if (string-match
  7262. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7263. (symbol-name (car x)))
  7264. x nil))
  7265. varlist))))
  7266. ;;;###autoload
  7267. (defun org-run-like-in-org-mode (cmd)
  7268. "Run a command, pretending that the current buffer is in Org-mode.
  7269. This will temporarily bind local variables that are typically bound in
  7270. Org-mode to the values they have in Org-mode, and then interactively
  7271. call CMD."
  7272. (org-load-modules-maybe)
  7273. (unless org-local-vars
  7274. (setq org-local-vars (org-get-local-variables)))
  7275. (eval (list 'let org-local-vars
  7276. (list 'call-interactively (list 'quote cmd)))))
  7277. ;;;; Archiving
  7278. (defun org-get-category (&optional pos)
  7279. "Get the category applying to position POS."
  7280. (get-text-property (or pos (point)) 'org-category))
  7281. (defun org-refresh-category-properties ()
  7282. "Refresh category text properties in the buffer."
  7283. (let ((def-cat (cond
  7284. ((null org-category)
  7285. (if buffer-file-name
  7286. (file-name-sans-extension
  7287. (file-name-nondirectory buffer-file-name))
  7288. "???"))
  7289. ((symbolp org-category) (symbol-name org-category))
  7290. (t org-category)))
  7291. beg end cat pos optionp)
  7292. (org-unmodified
  7293. (save-excursion
  7294. (save-restriction
  7295. (widen)
  7296. (goto-char (point-min))
  7297. (put-text-property (point) (point-max) 'org-category def-cat)
  7298. (while (re-search-forward
  7299. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7300. (setq pos (match-end 0)
  7301. optionp (equal (char-after (match-beginning 0)) ?#)
  7302. cat (org-trim (match-string 2)))
  7303. (if optionp
  7304. (setq beg (point-at-bol) end (point-max))
  7305. (org-back-to-heading t)
  7306. (setq beg (point) end (org-end-of-subtree t t)))
  7307. (put-text-property beg end 'org-category cat)
  7308. (goto-char pos)))))))
  7309. ;;;; Link Stuff
  7310. ;;; Link abbreviations
  7311. (defun org-link-expand-abbrev (link)
  7312. "Apply replacements as defined in `org-link-abbrev-alist."
  7313. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7314. (let* ((key (match-string 1 link))
  7315. (as (or (assoc key org-link-abbrev-alist-local)
  7316. (assoc key org-link-abbrev-alist)))
  7317. (tag (and (match-end 2) (match-string 3 link)))
  7318. rpl)
  7319. (if (not as)
  7320. link
  7321. (setq rpl (cdr as))
  7322. (cond
  7323. ((symbolp rpl) (funcall rpl tag))
  7324. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7325. ((string-match "%h" rpl)
  7326. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7327. (t (concat rpl tag)))))
  7328. link))
  7329. ;;; Storing and inserting links
  7330. (defvar org-insert-link-history nil
  7331. "Minibuffer history for links inserted with `org-insert-link'.")
  7332. (defvar org-stored-links nil
  7333. "Contains the links stored with `org-store-link'.")
  7334. (defvar org-store-link-plist nil
  7335. "Plist with info about the most recently link created with `org-store-link'.")
  7336. (defvar org-link-protocols nil
  7337. "Link protocols added to Org-mode using `org-add-link-type'.")
  7338. (defvar org-store-link-functions nil
  7339. "List of functions that are called to create and store a link.
  7340. Each function will be called in turn until one returns a non-nil
  7341. value. Each function should check if it is responsible for creating
  7342. this link (for example by looking at the major mode).
  7343. If not, it must exit and return nil.
  7344. If yes, it should return a non-nil value after a calling
  7345. `org-store-link-props' with a list of properties and values.
  7346. Special properties are:
  7347. :type The link prefix, like \"http\". This must be given.
  7348. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7349. This is obligatory as well.
  7350. :description Optional default description for the second pair
  7351. of brackets in an Org-mode link. The user can still change
  7352. this when inserting this link into an Org-mode buffer.
  7353. In addition to these, any additional properties can be specified
  7354. and then used in remember templates.")
  7355. (defun org-add-link-type (type &optional follow export)
  7356. "Add TYPE to the list of `org-link-types'.
  7357. Re-compute all regular expressions depending on `org-link-types'
  7358. FOLLOW and EXPORT are two functions.
  7359. FOLLOW should take the link path as the single argument and do whatever
  7360. is necessary to follow the link, for example find a file or display
  7361. a mail message.
  7362. EXPORT should format the link path for export to one of the export formats.
  7363. It should be a function accepting three arguments:
  7364. path the path of the link, the text after the prefix (like \"http:\")
  7365. desc the description of the link, if any, nil if there was no description
  7366. format the export format, a symbol like `html' or `latex' or `ascii'..
  7367. The function may use the FORMAT information to return different values
  7368. depending on the format. The return value will be put literally into
  7369. the exported file. If the return value is nil, this means Org should
  7370. do what it normally does with links which do not have EXPORT defined.
  7371. Org-mode has a built-in default for exporting links. If you are happy with
  7372. this default, there is no need to define an export function for the link
  7373. type. For a simple example of an export function, see `org-bbdb.el'."
  7374. (add-to-list 'org-link-types type t)
  7375. (org-make-link-regexps)
  7376. (if (assoc type org-link-protocols)
  7377. (setcdr (assoc type org-link-protocols) (list follow export))
  7378. (push (list type follow export) org-link-protocols)))
  7379. (defvar org-agenda-buffer-name)
  7380. ;;;###autoload
  7381. (defun org-store-link (arg)
  7382. "\\<org-mode-map>Store an org-link to the current location.
  7383. This link is added to `org-stored-links' and can later be inserted
  7384. into an org-buffer with \\[org-insert-link].
  7385. For some link types, a prefix arg is interpreted:
  7386. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7387. For file links, arg negates `org-context-in-file-links'."
  7388. (interactive "P")
  7389. (org-load-modules-maybe)
  7390. (setq org-store-link-plist nil) ; reset
  7391. (let ((outline-regexp (org-get-limited-outline-regexp))
  7392. link cpltxt desc description search txt custom-id agenda-link)
  7393. (cond
  7394. ((run-hook-with-args-until-success 'org-store-link-functions)
  7395. (setq link (plist-get org-store-link-plist :link)
  7396. desc (or (plist-get org-store-link-plist :description) link)))
  7397. ((equal (buffer-name) "*Org Edit Src Example*")
  7398. (let (label gc)
  7399. (while (or (not label)
  7400. (save-excursion
  7401. (save-restriction
  7402. (widen)
  7403. (goto-char (point-min))
  7404. (re-search-forward
  7405. (regexp-quote (format org-coderef-label-format label))
  7406. nil t))))
  7407. (when label (message "Label exists already") (sit-for 2))
  7408. (setq label (read-string "Code line label: " label)))
  7409. (end-of-line 1)
  7410. (setq link (format org-coderef-label-format label))
  7411. (setq gc (- 79 (length link)))
  7412. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7413. (insert link)
  7414. (setq link (concat "(" label ")") desc nil)))
  7415. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7416. ;; We are in the agenda, link to referenced location
  7417. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7418. (get-text-property (point) 'org-marker))))
  7419. (when m
  7420. (org-with-point-at m
  7421. (setq agenda-link
  7422. (if (interactive-p)
  7423. (call-interactively 'org-store-link)
  7424. (org-store-link nil)))))))
  7425. ((eq major-mode 'calendar-mode)
  7426. (let ((cd (calendar-cursor-to-date)))
  7427. (setq link
  7428. (format-time-string
  7429. (car org-time-stamp-formats)
  7430. (apply 'encode-time
  7431. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7432. nil nil nil))))
  7433. (org-store-link-props :type "calendar" :date cd)))
  7434. ((eq major-mode 'w3-mode)
  7435. (setq cpltxt (if (and (buffer-name)
  7436. (not (string-match "Untitled" (buffer-name))))
  7437. (buffer-name)
  7438. (url-view-url t))
  7439. link (org-make-link (url-view-url t)))
  7440. (org-store-link-props :type "w3" :url (url-view-url t)))
  7441. ((eq major-mode 'w3m-mode)
  7442. (setq cpltxt (or w3m-current-title w3m-current-url)
  7443. link (org-make-link w3m-current-url))
  7444. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7445. ((setq search (run-hook-with-args-until-success
  7446. 'org-create-file-search-functions))
  7447. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7448. "::" search))
  7449. (setq cpltxt (or description link)))
  7450. ((eq major-mode 'image-mode)
  7451. (setq cpltxt (concat "file:"
  7452. (abbreviate-file-name buffer-file-name))
  7453. link (org-make-link cpltxt))
  7454. (org-store-link-props :type "image" :file buffer-file-name))
  7455. ((eq major-mode 'dired-mode)
  7456. ;; link to the file in the current line
  7457. (let ((file (dired-get-filename nil t)))
  7458. (setq file (if file
  7459. (abbreviate-file-name
  7460. (expand-file-name (dired-get-filename nil t)))
  7461. ;; otherwise, no file so use current directory.
  7462. default-directory))
  7463. (setq cpltxt (concat "file:" file)
  7464. link (org-make-link cpltxt))))
  7465. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7466. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7467. (cond
  7468. ((org-in-regexp "<<\\(.*?\\)>>")
  7469. (setq cpltxt
  7470. (concat "file:"
  7471. (abbreviate-file-name
  7472. (buffer-file-name (buffer-base-buffer)))
  7473. "::" (match-string 1))
  7474. link (org-make-link cpltxt)))
  7475. ((and (featurep 'org-id)
  7476. (or (eq org-link-to-org-use-id t)
  7477. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7478. (interactive-p))
  7479. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7480. (interactive-p)
  7481. (not custom-id))
  7482. (and org-link-to-org-use-id
  7483. (condition-case nil
  7484. (org-entry-get nil "ID")
  7485. (error nil)))))
  7486. ;; We can make a link using the ID.
  7487. (setq link (condition-case nil
  7488. (prog1 (org-id-store-link)
  7489. (setq desc (plist-get org-store-link-plist
  7490. :description)))
  7491. (error
  7492. ;; probably before first headline, link to file only
  7493. (concat "file:"
  7494. (abbreviate-file-name
  7495. (buffer-file-name (buffer-base-buffer))))))))
  7496. (t
  7497. ;; Just link to current headline
  7498. (setq cpltxt (concat "file:"
  7499. (abbreviate-file-name
  7500. (buffer-file-name (buffer-base-buffer)))))
  7501. ;; Add a context search string
  7502. (when (org-xor org-context-in-file-links arg)
  7503. (setq txt (cond
  7504. ((org-on-heading-p) nil)
  7505. ((org-region-active-p)
  7506. (buffer-substring (region-beginning) (region-end)))
  7507. (t nil)))
  7508. (when (or (null txt) (string-match "\\S-" txt))
  7509. (setq cpltxt
  7510. (concat cpltxt "::"
  7511. (condition-case nil
  7512. (org-make-org-heading-search-string txt)
  7513. (error "")))
  7514. desc (or (nth 4 (ignore-errors
  7515. (org-heading-components))) "NONE"))))
  7516. (if (string-match "::\\'" cpltxt)
  7517. (setq cpltxt (substring cpltxt 0 -2)))
  7518. (setq link (org-make-link cpltxt)))))
  7519. ((buffer-file-name (buffer-base-buffer))
  7520. ;; Just link to this file here.
  7521. (setq cpltxt (concat "file:"
  7522. (abbreviate-file-name
  7523. (buffer-file-name (buffer-base-buffer)))))
  7524. ;; Add a context string
  7525. (when (org-xor org-context-in-file-links arg)
  7526. (setq txt (if (org-region-active-p)
  7527. (buffer-substring (region-beginning) (region-end))
  7528. (buffer-substring (point-at-bol) (point-at-eol))))
  7529. ;; Only use search option if there is some text.
  7530. (when (string-match "\\S-" txt)
  7531. (setq cpltxt
  7532. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7533. desc "NONE")))
  7534. (setq link (org-make-link cpltxt)))
  7535. ((interactive-p)
  7536. (error "Cannot link to a buffer which is not visiting a file"))
  7537. (t (setq link nil)))
  7538. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7539. (setq link (or link cpltxt)
  7540. desc (or desc cpltxt))
  7541. (if (equal desc "NONE") (setq desc nil))
  7542. (if (and (or (interactive-p) executing-kbd-macro) link)
  7543. (progn
  7544. (setq org-stored-links
  7545. (cons (list link desc) org-stored-links))
  7546. (message "Stored: %s" (or desc link))
  7547. (when custom-id
  7548. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7549. "::#" custom-id))
  7550. (setq org-stored-links
  7551. (cons (list link desc) org-stored-links))))
  7552. (or agenda-link (and link (org-make-link-string link desc))))))
  7553. (defun org-store-link-props (&rest plist)
  7554. "Store link properties, extract names and addresses."
  7555. (let (x adr)
  7556. (when (setq x (plist-get plist :from))
  7557. (setq adr (mail-extract-address-components x))
  7558. (setq plist (plist-put plist :fromname (car adr)))
  7559. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7560. (when (setq x (plist-get plist :to))
  7561. (setq adr (mail-extract-address-components x))
  7562. (setq plist (plist-put plist :toname (car adr)))
  7563. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7564. (let ((from (plist-get plist :from))
  7565. (to (plist-get plist :to)))
  7566. (when (and from to org-from-is-user-regexp)
  7567. (setq plist
  7568. (plist-put plist :fromto
  7569. (if (string-match org-from-is-user-regexp from)
  7570. (concat "to %t")
  7571. (concat "from %f"))))))
  7572. (setq org-store-link-plist plist))
  7573. (defun org-add-link-props (&rest plist)
  7574. "Add these properties to the link property list."
  7575. (let (key value)
  7576. (while plist
  7577. (setq key (pop plist) value (pop plist))
  7578. (setq org-store-link-plist
  7579. (plist-put org-store-link-plist key value)))))
  7580. (defun org-email-link-description (&optional fmt)
  7581. "Return the description part of an email link.
  7582. This takes information from `org-store-link-plist' and formats it
  7583. according to FMT (default from `org-email-link-description-format')."
  7584. (setq fmt (or fmt org-email-link-description-format))
  7585. (let* ((p org-store-link-plist)
  7586. (to (plist-get p :toaddress))
  7587. (from (plist-get p :fromaddress))
  7588. (table
  7589. (list
  7590. (cons "%c" (plist-get p :fromto))
  7591. (cons "%F" (plist-get p :from))
  7592. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7593. (cons "%T" (plist-get p :to))
  7594. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7595. (cons "%s" (plist-get p :subject))
  7596. (cons "%m" (plist-get p :message-id)))))
  7597. (when (string-match "%c" fmt)
  7598. ;; Check if the user wrote this message
  7599. (if (and org-from-is-user-regexp from to
  7600. (save-match-data (string-match org-from-is-user-regexp from)))
  7601. (setq fmt (replace-match "to %t" t t fmt))
  7602. (setq fmt (replace-match "from %f" t t fmt))))
  7603. (org-replace-escapes fmt table)))
  7604. (defun org-make-org-heading-search-string (&optional string heading)
  7605. "Make search string for STRING or current headline."
  7606. (interactive)
  7607. (let ((s (or string (org-get-heading))))
  7608. (unless (and string (not heading))
  7609. ;; We are using a headline, clean up garbage in there.
  7610. (if (string-match org-todo-regexp s)
  7611. (setq s (replace-match "" t t s)))
  7612. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7613. (setq s (replace-match "" t t s)))
  7614. (setq s (org-trim s))
  7615. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7616. org-comment-string "\\)") s)
  7617. (setq s (replace-match "" t t s)))
  7618. (while (string-match org-ts-regexp s)
  7619. (setq s (replace-match "" t t s))))
  7620. (or string (setq s (concat "*" s))) ; Add * for headlines
  7621. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7622. (defun org-make-link (&rest strings)
  7623. "Concatenate STRINGS."
  7624. (apply 'concat strings))
  7625. (defun org-make-link-string (link &optional description)
  7626. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7627. (unless (string-match "\\S-" link)
  7628. (error "Empty link"))
  7629. (when (and description
  7630. (stringp description)
  7631. (not (string-match "\\S-" description)))
  7632. (setq description nil))
  7633. (when (stringp description)
  7634. ;; Remove brackets from the description, they are fatal.
  7635. (while (string-match "\\[" description)
  7636. (setq description (replace-match "{" t t description)))
  7637. (while (string-match "\\]" description)
  7638. (setq description (replace-match "}" t t description))))
  7639. (when (equal (org-link-escape link) description)
  7640. ;; No description needed, it is identical
  7641. (setq description nil))
  7642. (when (and (not description)
  7643. (not (equal link (org-link-escape link))))
  7644. (setq description (org-extract-attributes link)))
  7645. (setq link (if (string-match org-link-types-re link)
  7646. (concat (match-string 1 link)
  7647. (org-link-escape (substring link (match-end 1))))
  7648. (org-link-escape link)))
  7649. (concat "[[" link "]"
  7650. (if description (concat "[" description "]") "")
  7651. "]"))
  7652. (defconst org-link-escape-chars
  7653. '((?\ . "%20")
  7654. (?\[ . "%5B")
  7655. (?\] . "%5D")
  7656. (?\340 . "%E0") ; `a
  7657. (?\342 . "%E2") ; ^a
  7658. (?\347 . "%E7") ; ,c
  7659. (?\350 . "%E8") ; `e
  7660. (?\351 . "%E9") ; 'e
  7661. (?\352 . "%EA") ; ^e
  7662. (?\356 . "%EE") ; ^i
  7663. (?\364 . "%F4") ; ^o
  7664. (?\371 . "%F9") ; `u
  7665. (?\373 . "%FB") ; ^u
  7666. (?\; . "%3B")
  7667. ;; (?? . "%3F")
  7668. (?= . "%3D")
  7669. (?+ . "%2B")
  7670. )
  7671. "Association list of escapes for some characters problematic in links.
  7672. This is the list that is used for internal purposes.")
  7673. (defvar org-url-encoding-use-url-hexify nil)
  7674. (defconst org-link-escape-chars-browser
  7675. '((?\ . "%20")) ; 32 for the SPC char
  7676. "Association list of escapes for some characters problematic in links.
  7677. This is the list that is used before handing over to the browser.")
  7678. (defun org-link-escape (text &optional table)
  7679. "Escape characters in TEXT that are problematic for links."
  7680. (if (and org-url-encoding-use-url-hexify (not table))
  7681. (url-hexify-string text)
  7682. (setq table (or table org-link-escape-chars))
  7683. (when text
  7684. (let ((re (mapconcat (lambda (x) (regexp-quote
  7685. (char-to-string (car x))))
  7686. table "\\|")))
  7687. (while (string-match re text)
  7688. (setq text
  7689. (replace-match
  7690. (cdr (assoc (string-to-char (match-string 0 text))
  7691. table))
  7692. t t text)))
  7693. text))))
  7694. (defun org-link-unescape (text &optional table)
  7695. "Reverse the action of `org-link-escape'."
  7696. (if (and org-url-encoding-use-url-hexify (not table))
  7697. (url-unhex-string text)
  7698. (setq table (or table org-link-escape-chars))
  7699. (when text
  7700. (let ((case-fold-search t)
  7701. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7702. table "\\|")))
  7703. (while (string-match re text)
  7704. (setq text
  7705. (replace-match
  7706. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7707. table)))
  7708. t t text)))
  7709. text))))
  7710. (defun org-xor (a b)
  7711. "Exclusive or."
  7712. (if a (not b) b))
  7713. (defun org-fixup-message-id-for-http (s)
  7714. "Replace special characters in a message id, so it can be used in an http query."
  7715. (when (string-match "%" s)
  7716. (setq s (mapconcat (lambda (c)
  7717. (if (eq c ?%)
  7718. "%25"
  7719. (char-to-string c)))
  7720. s "")))
  7721. (while (string-match "<" s)
  7722. (setq s (replace-match "%3C" t t s)))
  7723. (while (string-match ">" s)
  7724. (setq s (replace-match "%3E" t t s)))
  7725. (while (string-match "@" s)
  7726. (setq s (replace-match "%40" t t s)))
  7727. s)
  7728. ;;;###autoload
  7729. (defun org-insert-link-global ()
  7730. "Insert a link like Org-mode does.
  7731. This command can be called in any mode to insert a link in Org-mode syntax."
  7732. (interactive)
  7733. (org-load-modules-maybe)
  7734. (org-run-like-in-org-mode 'org-insert-link))
  7735. (defun org-insert-link (&optional complete-file link-location)
  7736. "Insert a link. At the prompt, enter the link.
  7737. Completion can be used to insert any of the link protocol prefixes like
  7738. http or ftp in use.
  7739. The history can be used to select a link previously stored with
  7740. `org-store-link'. When the empty string is entered (i.e. if you just
  7741. press RET at the prompt), the link defaults to the most recently
  7742. stored link. As SPC triggers completion in the minibuffer, you need to
  7743. use M-SPC or C-q SPC to force the insertion of a space character.
  7744. You will also be prompted for a description, and if one is given, it will
  7745. be displayed in the buffer instead of the link.
  7746. If there is already a link at point, this command will allow you to edit link
  7747. and description parts.
  7748. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7749. be selected using completion. The path to the file will be relative to the
  7750. current directory if the file is in the current directory or a subdirectory.
  7751. Otherwise, the link will be the absolute path as completed in the minibuffer
  7752. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7753. option `org-link-file-path-type'.
  7754. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7755. the current directory or below.
  7756. With three \\[universal-argument] prefixes, negate the meaning of
  7757. `org-keep-stored-link-after-insertion'.
  7758. If `org-make-link-description-function' is non-nil, this function will be
  7759. called with the link target, and the result will be the default
  7760. link description.
  7761. If the LINK-LOCATION parameter is non-nil, this value will be
  7762. used as the link location instead of reading one interactively."
  7763. (interactive "P")
  7764. (let* ((wcf (current-window-configuration))
  7765. (region (if (org-region-active-p)
  7766. (buffer-substring (region-beginning) (region-end))))
  7767. (remove (and region (list (region-beginning) (region-end))))
  7768. (desc region)
  7769. tmphist ; byte-compile incorrectly complains about this
  7770. (link link-location)
  7771. entry file all-prefixes)
  7772. (cond
  7773. (link-location) ; specified by arg, just use it.
  7774. ((org-in-regexp org-bracket-link-regexp 1)
  7775. ;; We do have a link at point, and we are going to edit it.
  7776. (setq remove (list (match-beginning 0) (match-end 0)))
  7777. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7778. (setq link (read-string "Link: "
  7779. (org-link-unescape
  7780. (org-match-string-no-properties 1)))))
  7781. ((or (org-in-regexp org-angle-link-re)
  7782. (org-in-regexp org-plain-link-re))
  7783. ;; Convert to bracket link
  7784. (setq remove (list (match-beginning 0) (match-end 0))
  7785. link (read-string "Link: "
  7786. (org-remove-angle-brackets (match-string 0)))))
  7787. ((member complete-file '((4) (16)))
  7788. ;; Completing read for file names.
  7789. (setq link (org-file-complete-link complete-file)))
  7790. (t
  7791. ;; Read link, with completion for stored links.
  7792. (with-output-to-temp-buffer "*Org Links*"
  7793. (princ "Insert a link.
  7794. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7795. (when org-stored-links
  7796. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7797. (princ (mapconcat
  7798. (lambda (x)
  7799. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7800. (reverse org-stored-links) "\n"))))
  7801. (let ((cw (selected-window)))
  7802. (select-window (get-buffer-window "*Org Links*" 'visible))
  7803. (setq truncate-lines t)
  7804. (unless (pos-visible-in-window-p (point-max))
  7805. (org-fit-window-to-buffer))
  7806. (and (window-live-p cw) (select-window cw)))
  7807. ;; Fake a link history, containing the stored links.
  7808. (setq tmphist (append (mapcar 'car org-stored-links)
  7809. org-insert-link-history))
  7810. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7811. (mapcar 'car org-link-abbrev-alist)
  7812. org-link-types))
  7813. (unwind-protect
  7814. (progn
  7815. (setq link
  7816. (let ((org-completion-use-ido nil)
  7817. (org-completion-use-iswitchb nil))
  7818. (org-completing-read
  7819. "Link: "
  7820. (append
  7821. (mapcar (lambda (x) (list (concat x ":")))
  7822. all-prefixes)
  7823. (mapcar 'car org-stored-links))
  7824. nil nil nil
  7825. 'tmphist
  7826. (car (car org-stored-links)))))
  7827. (if (not (string-match "\\S-" link))
  7828. (error "No link selected"))
  7829. (if (or (member link all-prefixes)
  7830. (and (equal ":" (substring link -1))
  7831. (member (substring link 0 -1) all-prefixes)
  7832. (setq link (substring link 0 -1))))
  7833. (setq link (org-link-try-special-completion link))))
  7834. (set-window-configuration wcf)
  7835. (kill-buffer "*Org Links*"))
  7836. (setq entry (assoc link org-stored-links))
  7837. (or entry (push link org-insert-link-history))
  7838. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7839. (not org-keep-stored-link-after-insertion))
  7840. (setq org-stored-links (delq (assoc link org-stored-links)
  7841. org-stored-links)))
  7842. (setq desc (or desc (nth 1 entry)))))
  7843. (if (string-match org-plain-link-re link)
  7844. ;; URL-like link, normalize the use of angular brackets.
  7845. (setq link (org-make-link (org-remove-angle-brackets link))))
  7846. ;; Check if we are linking to the current file with a search option
  7847. ;; If yes, simplify the link by using only the search option.
  7848. (when (and buffer-file-name
  7849. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7850. (let* ((path (match-string 1 link))
  7851. (case-fold-search nil)
  7852. (search (match-string 2 link)))
  7853. (save-match-data
  7854. (if (equal (file-truename buffer-file-name) (file-truename path))
  7855. ;; We are linking to this same file, with a search option
  7856. (setq link search)))))
  7857. ;; Check if we can/should use a relative path. If yes, simplify the link
  7858. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7859. (let* ((type (match-string 1 link))
  7860. (path (match-string 2 link))
  7861. (origpath path)
  7862. (case-fold-search nil))
  7863. (cond
  7864. ((or (eq org-link-file-path-type 'absolute)
  7865. (equal complete-file '(16)))
  7866. (setq path (abbreviate-file-name (expand-file-name path))))
  7867. ((eq org-link-file-path-type 'noabbrev)
  7868. (setq path (expand-file-name path)))
  7869. ((eq org-link-file-path-type 'relative)
  7870. (setq path (file-relative-name path)))
  7871. (t
  7872. (save-match-data
  7873. (if (string-match (concat "^" (regexp-quote
  7874. (expand-file-name
  7875. (file-name-as-directory
  7876. default-directory))))
  7877. (expand-file-name path))
  7878. ;; We are linking a file with relative path name.
  7879. (setq path (substring (expand-file-name path)
  7880. (match-end 0)))
  7881. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7882. (setq link (concat type path))
  7883. (if (equal desc origpath)
  7884. (setq desc path))))
  7885. (if org-make-link-description-function
  7886. (setq desc (funcall org-make-link-description-function link desc)))
  7887. (setq desc (read-string "Description: " desc))
  7888. (unless (string-match "\\S-" desc) (setq desc nil))
  7889. (if remove (apply 'delete-region remove))
  7890. (insert (org-make-link-string link desc))))
  7891. (defun org-link-try-special-completion (type)
  7892. "If there is completion support for link type TYPE, offer it."
  7893. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7894. (if (functionp fun)
  7895. (funcall fun)
  7896. (read-string "Link (no completion support): " (concat type ":")))))
  7897. (defun org-file-complete-link (&optional arg)
  7898. "Create a file link using completion."
  7899. (let (file link)
  7900. (setq file (read-file-name "File: "))
  7901. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7902. (pwd1 (file-name-as-directory (abbreviate-file-name
  7903. (expand-file-name ".")))))
  7904. (cond
  7905. ((equal arg '(16))
  7906. (setq link (org-make-link
  7907. "file:"
  7908. (abbreviate-file-name (expand-file-name file)))))
  7909. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7910. (setq link (org-make-link "file:" (match-string 1 file))))
  7911. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7912. (expand-file-name file))
  7913. (setq link (org-make-link
  7914. "file:" (match-string 1 (expand-file-name file)))))
  7915. (t (setq link (org-make-link "file:" file)))))
  7916. link))
  7917. (defun org-completing-read (&rest args)
  7918. "Completing-read with SPACE being a normal character."
  7919. (let ((minibuffer-local-completion-map
  7920. (copy-keymap minibuffer-local-completion-map)))
  7921. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7922. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7923. (apply 'org-icompleting-read args)))
  7924. (defun org-completing-read-no-i (&rest args)
  7925. (let (org-completion-use-ido org-completion-use-iswitchb)
  7926. (apply 'org-completing-read args)))
  7927. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7928. "Use iswitch as a completing-read replacement to choose from choices.
  7929. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7930. from."
  7931. (let* ((iswitchb-use-virtual-buffers nil)
  7932. (iswitchb-make-buflist-hook
  7933. (lambda ()
  7934. (setq iswitchb-temp-buflist choices))))
  7935. (iswitchb-read-buffer prompt)))
  7936. (defun org-icompleting-read (&rest args)
  7937. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7938. (org-without-partial-completion
  7939. (if (and org-completion-use-ido
  7940. (fboundp 'ido-completing-read)
  7941. (boundp 'ido-mode) ido-mode
  7942. (listp (second args)))
  7943. (let ((ido-enter-matching-directory nil))
  7944. (apply 'ido-completing-read (concat (car args))
  7945. (if (consp (car (nth 1 args)))
  7946. (mapcar (lambda (x) (car x)) (nth 1 args))
  7947. (nth 1 args))
  7948. (cddr args)))
  7949. (if (and org-completion-use-iswitchb
  7950. (boundp 'iswitchb-mode) iswitchb-mode
  7951. (listp (second args)))
  7952. (apply 'org-iswitchb-completing-read (concat (car args))
  7953. (if (consp (car (nth 1 args)))
  7954. (mapcar (lambda (x) (car x)) (nth 1 args))
  7955. (nth 1 args))
  7956. (cddr args))
  7957. (apply 'completing-read args)))))
  7958. (defun org-extract-attributes (s)
  7959. "Extract the attributes cookie from a string and set as text property."
  7960. (let (a attr (start 0) key value)
  7961. (save-match-data
  7962. (when (string-match "{{\\([^}]+\\)}}$" s)
  7963. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7964. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7965. (setq key (match-string 1 a) value (match-string 2 a)
  7966. start (match-end 0)
  7967. attr (plist-put attr (intern key) value))))
  7968. (org-add-props s nil 'org-attr attr))
  7969. s))
  7970. (defun org-extract-attributes-from-string (tag)
  7971. (let (key value attr)
  7972. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7973. (setq key (match-string 1 tag) value (match-string 2 tag)
  7974. tag (replace-match "" t t tag)
  7975. attr (plist-put attr (intern key) value)))
  7976. (cons tag attr)))
  7977. (defun org-attributes-to-string (plist)
  7978. "Format a property list into an HTML attribute list."
  7979. (let ((s "") key value)
  7980. (while plist
  7981. (setq key (pop plist) value (pop plist))
  7982. (and value
  7983. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7984. s))
  7985. ;;; Opening/following a link
  7986. (defvar org-link-search-failed nil)
  7987. (defvar org-open-link-functions nil
  7988. "Hook for functions finding a plain text link.
  7989. These functions must take a single argument, the link content.
  7990. They will be called for links that look like [[link text][description]]
  7991. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7992. like a filename (e.g. \"./blue.png\").
  7993. These functions will be called *before* Org attempts to resolve the
  7994. link by doing text searches in the current buffer - so if you want a
  7995. link \"[[target]]\" to still find \"<<target>>\", your function should
  7996. handle this as a special case.
  7997. When the function does handle the link, it must return a non-nil value.
  7998. If it decides that it is not responsible for this link, it must return
  7999. nil to indicate that that Org-mode can continue with other options
  8000. like exact and fuzzy text search.")
  8001. (defun org-next-link ()
  8002. "Move forward to the next link.
  8003. If the link is in hidden text, expose it."
  8004. (interactive)
  8005. (when (and org-link-search-failed (eq this-command last-command))
  8006. (goto-char (point-min))
  8007. (message "Link search wrapped back to beginning of buffer"))
  8008. (setq org-link-search-failed nil)
  8009. (let* ((pos (point))
  8010. (ct (org-context))
  8011. (a (assoc :link ct)))
  8012. (if a (goto-char (nth 2 a)))
  8013. (if (re-search-forward org-any-link-re nil t)
  8014. (progn
  8015. (goto-char (match-beginning 0))
  8016. (if (org-invisible-p) (org-show-context)))
  8017. (goto-char pos)
  8018. (setq org-link-search-failed t)
  8019. (error "No further link found"))))
  8020. (defun org-previous-link ()
  8021. "Move backward to the previous link.
  8022. If the link is in hidden text, expose it."
  8023. (interactive)
  8024. (when (and org-link-search-failed (eq this-command last-command))
  8025. (goto-char (point-max))
  8026. (message "Link search wrapped back to end of buffer"))
  8027. (setq org-link-search-failed nil)
  8028. (let* ((pos (point))
  8029. (ct (org-context))
  8030. (a (assoc :link ct)))
  8031. (if a (goto-char (nth 1 a)))
  8032. (if (re-search-backward org-any-link-re nil t)
  8033. (progn
  8034. (goto-char (match-beginning 0))
  8035. (if (org-invisible-p) (org-show-context)))
  8036. (goto-char pos)
  8037. (setq org-link-search-failed t)
  8038. (error "No further link found"))))
  8039. (defun org-translate-link (s)
  8040. "Translate a link string if a translation function has been defined."
  8041. (if (and org-link-translation-function
  8042. (fboundp org-link-translation-function)
  8043. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8044. (progn
  8045. (setq s (funcall org-link-translation-function
  8046. (match-string 1) (match-string 2)))
  8047. (concat (car s) ":" (cdr s)))
  8048. s))
  8049. (defun org-translate-link-from-planner (type path)
  8050. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8051. This is still an experimental function, your mileage may vary."
  8052. (cond
  8053. ((member type '("http" "https" "news" "ftp"))
  8054. ;; standard Internet links are the same.
  8055. nil)
  8056. ((and (equal type "irc") (string-match "^//" path))
  8057. ;; Planner has two / at the beginning of an irc link, we have 1.
  8058. ;; We should have zero, actually....
  8059. (setq path (substring path 1)))
  8060. ((and (equal type "lisp") (string-match "^/" path))
  8061. ;; Planner has a slash, we do not.
  8062. (setq type "elisp" path (substring path 1)))
  8063. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8064. ;; A typical message link. Planner has the id after the final slash,
  8065. ;; we separate it with a hash mark
  8066. (setq path (concat (match-string 1 path) "#"
  8067. (org-remove-angle-brackets (match-string 2 path)))))
  8068. )
  8069. (cons type path))
  8070. (defun org-find-file-at-mouse (ev)
  8071. "Open file link or URL at mouse."
  8072. (interactive "e")
  8073. (mouse-set-point ev)
  8074. (org-open-at-point 'in-emacs))
  8075. (defun org-open-at-mouse (ev)
  8076. "Open file link or URL at mouse."
  8077. (interactive "e")
  8078. (mouse-set-point ev)
  8079. (if (eq major-mode 'org-agenda-mode)
  8080. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8081. (org-open-at-point))
  8082. (defvar org-window-config-before-follow-link nil
  8083. "The window configuration before following a link.
  8084. This is saved in case the need arises to restore it.")
  8085. (defvar org-open-link-marker (make-marker)
  8086. "Marker pointing to the location where `org-open-at-point; was called.")
  8087. ;;;###autoload
  8088. (defun org-open-at-point-global ()
  8089. "Follow a link like Org-mode does.
  8090. This command can be called in any mode to follow a link that has
  8091. Org-mode syntax."
  8092. (interactive)
  8093. (org-run-like-in-org-mode 'org-open-at-point))
  8094. ;;;###autoload
  8095. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8096. "Open a link in the string S, as if it was in Org-mode."
  8097. (interactive "sLink: \nP")
  8098. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8099. (with-temp-buffer
  8100. (let ((org-inhibit-startup t))
  8101. (org-mode)
  8102. (insert s)
  8103. (goto-char (point-min))
  8104. (when reference-buffer
  8105. (setq org-link-abbrev-alist-local
  8106. (with-current-buffer reference-buffer
  8107. org-link-abbrev-alist-local)))
  8108. (org-open-at-point arg reference-buffer)))))
  8109. (defvar org-open-at-point-functions nil
  8110. "Hook that is run when following a link at point.
  8111. Functions in this hook must return t if they identify and follow
  8112. a link at point. If they don't find anything interesting at point,
  8113. they must return nil.")
  8114. (defun org-open-at-point (&optional in-emacs reference-buffer)
  8115. "Open link at or after point.
  8116. If there is no link at point, this function will search forward up to
  8117. the end of the current line.
  8118. Normally, files will be opened by an appropriate application. If the
  8119. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  8120. With a double prefix argument, try to open outside of Emacs, in the
  8121. application the system uses for this file type."
  8122. (interactive "P")
  8123. ;; if in a code block, then open the block's results
  8124. (unless (call-interactively #'org-babel-open-src-block-result)
  8125. (org-load-modules-maybe)
  8126. (move-marker org-open-link-marker (point))
  8127. (setq org-window-config-before-follow-link (current-window-configuration))
  8128. (org-remove-occur-highlights nil nil t)
  8129. (cond
  8130. ((and (org-on-heading-p)
  8131. (not (org-in-regexp
  8132. (concat org-plain-link-re "\\|"
  8133. org-bracket-link-regexp "\\|"
  8134. org-angle-link-re "\\|"
  8135. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8136. (not (get-text-property (point) 'org-linked-text)))
  8137. (or (org-offer-links-in-entry in-emacs)
  8138. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8139. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8140. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8141. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8142. (org-footnote-action))
  8143. (t
  8144. (let (type path link line search (pos (point)))
  8145. (catch 'match
  8146. (save-excursion
  8147. (skip-chars-forward "^]\n\r")
  8148. (when (org-in-regexp org-bracket-link-regexp 1)
  8149. (setq link (org-extract-attributes
  8150. (org-link-unescape (org-match-string-no-properties 1))))
  8151. (while (string-match " *\n *" link)
  8152. (setq link (replace-match " " t t link)))
  8153. (setq link (org-link-expand-abbrev link))
  8154. (cond
  8155. ((or (file-name-absolute-p link)
  8156. (string-match "^\\.\\.?/" link))
  8157. (setq type "file" path link))
  8158. ((string-match org-link-re-with-space3 link)
  8159. (setq type (match-string 1 link) path (match-string 2 link)))
  8160. (t (setq type "thisfile" path link)))
  8161. (throw 'match t)))
  8162. (when (get-text-property (point) 'org-linked-text)
  8163. (setq type "thisfile"
  8164. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8165. (1+ (point)) (point))
  8166. path (buffer-substring
  8167. (previous-single-property-change pos 'org-linked-text)
  8168. (next-single-property-change pos 'org-linked-text)))
  8169. (throw 'match t))
  8170. (save-excursion
  8171. (when (or (org-in-regexp org-angle-link-re)
  8172. (org-in-regexp org-plain-link-re))
  8173. (setq type (match-string 1) path (match-string 2))
  8174. (throw 'match t)))
  8175. (save-excursion
  8176. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8177. (setq type "tags"
  8178. path (match-string 1))
  8179. (while (string-match ":" path)
  8180. (setq path (replace-match "+" t t path)))
  8181. (throw 'match t)))
  8182. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8183. (setq type "tree-match"
  8184. path (match-string 1))
  8185. (throw 'match t)))
  8186. (unless path
  8187. (error "No link found"))
  8188. ;; switch back to reference buffer
  8189. ;; needed when if called in a temporary buffer through
  8190. ;; org-open-link-from-string
  8191. (with-current-buffer (or reference-buffer (current-buffer))
  8192. ;; Remove any trailing spaces in path
  8193. (if (string-match " +\\'" path)
  8194. (setq path (replace-match "" t t path)))
  8195. (if (and org-link-translation-function
  8196. (fboundp org-link-translation-function))
  8197. ;; Check if we need to translate the link
  8198. (let ((tmp (funcall org-link-translation-function type path)))
  8199. (setq type (car tmp) path (cdr tmp))))
  8200. (cond
  8201. ((assoc type org-link-protocols)
  8202. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8203. ((equal type "mailto")
  8204. (let ((cmd (car org-link-mailto-program))
  8205. (args (cdr org-link-mailto-program)) args1
  8206. (address path) (subject "") a)
  8207. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8208. (setq address (match-string 1 path)
  8209. subject (org-link-escape (match-string 2 path))))
  8210. (while args
  8211. (cond
  8212. ((not (stringp (car args))) (push (pop args) args1))
  8213. (t (setq a (pop args))
  8214. (if (string-match "%a" a)
  8215. (setq a (replace-match address t t a)))
  8216. (if (string-match "%s" a)
  8217. (setq a (replace-match subject t t a)))
  8218. (push a args1))))
  8219. (apply cmd (nreverse args1))))
  8220. ((member type '("http" "https" "ftp" "news"))
  8221. (browse-url (concat type ":" (org-link-escape
  8222. path org-link-escape-chars-browser))))
  8223. ((string= type "doi")
  8224. (browse-url (concat "http://dx.doi.org/"
  8225. (org-link-escape
  8226. path org-link-escape-chars-browser))))
  8227. ((member type '("message"))
  8228. (browse-url (concat type ":" path)))
  8229. ((string= type "tags")
  8230. (org-tags-view in-emacs path))
  8231. ((string= type "tree-match")
  8232. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8233. ((string= type "file")
  8234. (if (string-match "::\\([0-9]+\\)\\'" path)
  8235. (setq line (string-to-number (match-string 1 path))
  8236. path (substring path 0 (match-beginning 0)))
  8237. (if (string-match "::\\(.+\\)\\'" path)
  8238. (setq search (match-string 1 path)
  8239. path (substring path 0 (match-beginning 0)))))
  8240. (if (string-match "[*?{]" (file-name-nondirectory path))
  8241. (dired path)
  8242. (org-open-file path in-emacs line search)))
  8243. ((string= type "news")
  8244. (require 'org-gnus)
  8245. (org-gnus-follow-link path))
  8246. ((string= type "shell")
  8247. (let ((cmd path))
  8248. (if (or (not org-confirm-shell-link-function)
  8249. (funcall org-confirm-shell-link-function
  8250. (format "Execute \"%s\" in shell? "
  8251. (org-add-props cmd nil
  8252. 'face 'org-warning))))
  8253. (progn
  8254. (message "Executing %s" cmd)
  8255. (shell-command cmd))
  8256. (error "Abort"))))
  8257. ((string= type "elisp")
  8258. (let ((cmd path))
  8259. (if (or (not org-confirm-elisp-link-function)
  8260. (funcall org-confirm-elisp-link-function
  8261. (format "Execute \"%s\" as elisp? "
  8262. (org-add-props cmd nil
  8263. 'face 'org-warning))))
  8264. (message "%s => %s" cmd
  8265. (if (equal (string-to-char cmd) ?\()
  8266. (eval (read cmd))
  8267. (call-interactively (read cmd))))
  8268. (error "Abort"))))
  8269. ((and (string= type "thisfile")
  8270. (run-hook-with-args-until-success
  8271. 'org-open-link-functions path)))
  8272. ((string= type "thisfile")
  8273. (if in-emacs
  8274. (switch-to-buffer-other-window
  8275. (org-get-buffer-for-internal-link (current-buffer)))
  8276. (org-mark-ring-push))
  8277. (let ((cmd `(org-link-search
  8278. ,path
  8279. ,(cond ((equal in-emacs '(4)) 'occur)
  8280. ((equal in-emacs '(16)) 'org-occur)
  8281. (t nil))
  8282. ,pos)))
  8283. (condition-case nil (eval cmd)
  8284. (error (progn (widen) (eval cmd))))))
  8285. (t
  8286. (browse-url-at-point)))))))
  8287. (move-marker org-open-link-marker nil)
  8288. (run-hook-with-args 'org-follow-link-hook)))
  8289. (defun org-offer-links-in-entry (&optional nth zero)
  8290. "Offer links in the current entry and follow the selected link.
  8291. If there is only one link, follow it immediately as well.
  8292. If NTH is an integer, immediately pick the NTH link found.
  8293. If ZERO is a string, check also this string for a link, and if
  8294. there is one, offer it as link number zero."
  8295. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8296. "\\(" org-angle-link-re "\\)\\|"
  8297. "\\(" org-plain-link-re "\\)"))
  8298. (cnt ?0)
  8299. (in-emacs (if (integerp nth) nil nth))
  8300. have-zero end links link c)
  8301. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8302. (push (match-string 0 zero) links)
  8303. (setq cnt (1- cnt) have-zero t))
  8304. (save-excursion
  8305. (org-back-to-heading t)
  8306. (setq end (save-excursion (outline-next-heading) (point)))
  8307. (while (re-search-forward re end t)
  8308. (push (match-string 0) links))
  8309. (setq links (org-uniquify (reverse links))))
  8310. (cond
  8311. ((null links)
  8312. (message "No links"))
  8313. ((equal (length links) 1)
  8314. (setq link (list (car links))))
  8315. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8316. (setq link (nth (if have-zero nth (1- nth)) links)))
  8317. (t ; we have to select a link
  8318. (save-excursion
  8319. (save-window-excursion
  8320. (delete-other-windows)
  8321. (with-output-to-temp-buffer "*Select Link*"
  8322. (mapc (lambda (l)
  8323. (if (not (string-match org-bracket-link-regexp l))
  8324. (princ (format "[%c] %s\n" (incf cnt)
  8325. (org-remove-angle-brackets l)))
  8326. (if (match-end 3)
  8327. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8328. (match-string 3 l) (match-string 1 l)))
  8329. (princ (format "[%c] %s\n" (incf cnt)
  8330. (match-string 1 l))))))
  8331. links))
  8332. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8333. (message "Select link to open, RET to open all:")
  8334. (setq c (read-char-exclusive))
  8335. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8336. (when (equal c ?q) (error "Abort"))
  8337. (if (equal c ?\C-m)
  8338. (setq link links)
  8339. (setq nth (- c ?0))
  8340. (if have-zero (setq nth (1+ nth)))
  8341. (unless (and (integerp nth) (>= (length links) nth))
  8342. (error "Invalid link selection"))
  8343. (setq link (list (nth (1- nth) links))))))
  8344. (if link
  8345. (let ((buf (current-buffer)))
  8346. (dolist (l link)
  8347. (org-open-link-from-string l in-emacs buf))
  8348. t)
  8349. nil)))
  8350. ;; Add special file links that specify the way of opening
  8351. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8352. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8353. (defun org-open-file-with-system (path)
  8354. "Open file at PATH using the system way of opening it."
  8355. (org-open-file path 'system))
  8356. (defun org-open-file-with-emacs (path)
  8357. "Open file at PATH in Emacs."
  8358. (org-open-file path 'emacs))
  8359. (defun org-remove-file-link-modifiers ()
  8360. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8361. (goto-char (point-min))
  8362. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8363. (org-if-unprotected
  8364. (replace-match "file:" t t))))
  8365. (eval-after-load "org-exp"
  8366. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8367. 'org-remove-file-link-modifiers))
  8368. ;;;; Time estimates
  8369. (defun org-get-effort (&optional pom)
  8370. "Get the effort estimate for the current entry."
  8371. (org-entry-get pom org-effort-property))
  8372. ;;; File search
  8373. (defvar org-create-file-search-functions nil
  8374. "List of functions to construct the right search string for a file link.
  8375. These functions are called in turn with point at the location to
  8376. which the link should point.
  8377. A function in the hook should first test if it would like to
  8378. handle this file type, for example by checking the `major-mode'
  8379. or the file extension. If it decides not to handle this file, it
  8380. should just return nil to give other functions a chance. If it
  8381. does handle the file, it must return the search string to be used
  8382. when following the link. The search string will be part of the
  8383. file link, given after a double colon, and `org-open-at-point'
  8384. will automatically search for it. If special measures must be
  8385. taken to make the search successful, another function should be
  8386. added to the companion hook `org-execute-file-search-functions',
  8387. which see.
  8388. A function in this hook may also use `setq' to set the variable
  8389. `description' to provide a suggestion for the descriptive text to
  8390. be used for this link when it gets inserted into an Org-mode
  8391. buffer with \\[org-insert-link].")
  8392. (defvar org-execute-file-search-functions nil
  8393. "List of functions to execute a file search triggered by a link.
  8394. Functions added to this hook must accept a single argument, the
  8395. search string that was part of the file link, the part after the
  8396. double colon. The function must first check if it would like to
  8397. handle this search, for example by checking the `major-mode' or
  8398. the file extension. If it decides not to handle this search, it
  8399. should just return nil to give other functions a chance. If it
  8400. does handle the search, it must return a non-nil value to keep
  8401. other functions from trying.
  8402. Each function can access the current prefix argument through the
  8403. variable `current-prefix-argument'. Note that a single prefix is
  8404. used to force opening a link in Emacs, so it may be good to only
  8405. use a numeric or double prefix to guide the search function.
  8406. In case this is needed, a function in this hook can also restore
  8407. the window configuration before `org-open-at-point' was called using:
  8408. (set-window-configuration org-window-config-before-follow-link)")
  8409. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8410. (defun org-link-search (s &optional type avoid-pos)
  8411. "Search for a link search option.
  8412. If S is surrounded by forward slashes, it is interpreted as a
  8413. regular expression. In org-mode files, this will create an `org-occur'
  8414. sparse tree. In ordinary files, `occur' will be used to list matches.
  8415. If the current buffer is in `dired-mode', grep will be used to search
  8416. in all files. If AVOID-POS is given, ignore matches near that position."
  8417. (let ((case-fold-search t)
  8418. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8419. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8420. (append '(("") (" ") ("\t") ("\n"))
  8421. org-emphasis-alist)
  8422. "\\|") "\\)"))
  8423. (pos (point))
  8424. (pre nil) (post nil)
  8425. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8426. (cond
  8427. ;; First check if there are any special search functions
  8428. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8429. ;; Now try the builtin stuff
  8430. ((and (equal (string-to-char s0) ?#)
  8431. (> (length s0) 1)
  8432. (save-excursion
  8433. (goto-char (point-min))
  8434. (and
  8435. (re-search-forward
  8436. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8437. (setq type 'dedicated
  8438. pos (match-beginning 0))))
  8439. ;; There is an exact target for this
  8440. (goto-char pos)
  8441. (org-back-to-heading t)))
  8442. ((save-excursion
  8443. (goto-char (point-min))
  8444. (and
  8445. (re-search-forward
  8446. (concat "<<" (regexp-quote s0) ">>") nil t)
  8447. (setq type 'dedicated
  8448. pos (match-beginning 0))))
  8449. ;; There is an exact target for this
  8450. (goto-char pos))
  8451. ((and (string-match "^(\\(.*\\))$" s0)
  8452. (save-excursion
  8453. (goto-char (point-min))
  8454. (and
  8455. (re-search-forward
  8456. (concat "[^[]" (regexp-quote
  8457. (format org-coderef-label-format
  8458. (match-string 1 s0))))
  8459. nil t)
  8460. (setq type 'dedicated
  8461. pos (1+ (match-beginning 0))))))
  8462. ;; There is a coderef target for this
  8463. (goto-char pos))
  8464. ((string-match "^/\\(.*\\)/$" s)
  8465. ;; A regular expression
  8466. (cond
  8467. ((org-mode-p)
  8468. (org-occur (match-string 1 s)))
  8469. ;;((eq major-mode 'dired-mode)
  8470. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8471. (t (org-do-occur (match-string 1 s)))))
  8472. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8473. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8474. (goto-char (point-min))
  8475. (cond
  8476. ((let (case-fold-search)
  8477. (re-search-forward (format org-complex-heading-regexp-format
  8478. (regexp-quote s))
  8479. nil t))
  8480. ;; OK, found a match
  8481. (goto-char (match-beginning 0)))
  8482. ((and (not org-link-search-inhibit-query)
  8483. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8484. (y-or-n-p "No match - create this as a new heading? "))
  8485. (goto-char (point-max))
  8486. (or (bolp) (newline))
  8487. (insert "* " s "\n")
  8488. (beginning-of-line 0))
  8489. (t
  8490. (goto-char pos)
  8491. (error "No match"))))
  8492. (t
  8493. ;; A normal search string
  8494. (when (equal (string-to-char s) ?*)
  8495. ;; Anchor on headlines, post may include tags.
  8496. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8497. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8498. s (substring s 1)))
  8499. (remove-text-properties
  8500. 0 (length s)
  8501. '(face nil mouse-face nil keymap nil fontified nil) s)
  8502. ;; Make a series of regular expressions to find a match
  8503. (setq words (org-split-string s "[ \n\r\t]+")
  8504. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8505. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8506. "\\)" markers)
  8507. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8508. re2a (concat "[ \t\r\n]" re2a_)
  8509. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8510. re4 (concat "[^a-zA-Z_]" re4_)
  8511. re1 (concat pre re2 post)
  8512. re3 (concat pre (if pre re4_ re4) post)
  8513. re5 (concat pre ".*" re4)
  8514. re2 (concat pre re2)
  8515. re2a (concat pre (if pre re2a_ re2a))
  8516. re4 (concat pre (if pre re4_ re4))
  8517. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8518. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8519. re5 "\\)"
  8520. ))
  8521. (cond
  8522. ((eq type 'org-occur) (org-occur reall))
  8523. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8524. (t (goto-char (point-min))
  8525. (setq type 'fuzzy)
  8526. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8527. (org-search-not-self 1 re1 nil t)
  8528. (org-search-not-self 1 re2 nil t)
  8529. (org-search-not-self 1 re2a nil t)
  8530. (org-search-not-self 1 re3 nil t)
  8531. (org-search-not-self 1 re4 nil t)
  8532. (org-search-not-self 1 re5 nil t)
  8533. )
  8534. (goto-char (match-beginning 1))
  8535. (goto-char pos)
  8536. (error "No match"))))))
  8537. (and (org-mode-p) (org-show-context 'link-search))
  8538. type))
  8539. (defun org-search-not-self (group &rest args)
  8540. "Execute `re-search-forward', but only accept matches that do not
  8541. enclose the position of `org-open-link-marker'."
  8542. (let ((m org-open-link-marker))
  8543. (catch 'exit
  8544. (while (apply 're-search-forward args)
  8545. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8546. (goto-char (match-end group))
  8547. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8548. (> (match-beginning 0) (marker-position m))
  8549. (< (match-end 0) (marker-position m)))
  8550. (save-match-data
  8551. (or (not (org-in-regexp
  8552. org-bracket-link-analytic-regexp 1))
  8553. (not (match-end 4)) ; no description
  8554. (and (<= (match-beginning 4) (point))
  8555. (>= (match-end 4) (point))))))
  8556. (throw 'exit (point))))))))
  8557. (defun org-get-buffer-for-internal-link (buffer)
  8558. "Return a buffer to be used for displaying the link target of internal links."
  8559. (cond
  8560. ((not org-display-internal-link-with-indirect-buffer)
  8561. buffer)
  8562. ((string-match "(Clone)$" (buffer-name buffer))
  8563. (message "Buffer is already a clone, not making another one")
  8564. ;; we also do not modify visibility in this case
  8565. buffer)
  8566. (t ; make a new indirect buffer for displaying the link
  8567. (let* ((bn (buffer-name buffer))
  8568. (ibn (concat bn "(Clone)"))
  8569. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8570. (with-current-buffer ib (org-overview))
  8571. ib))))
  8572. (defun org-do-occur (regexp &optional cleanup)
  8573. "Call the Emacs command `occur'.
  8574. If CLEANUP is non-nil, remove the printout of the regular expression
  8575. in the *Occur* buffer. This is useful if the regex is long and not useful
  8576. to read."
  8577. (occur regexp)
  8578. (when cleanup
  8579. (let ((cwin (selected-window)) win beg end)
  8580. (when (setq win (get-buffer-window "*Occur*"))
  8581. (select-window win))
  8582. (goto-char (point-min))
  8583. (when (re-search-forward "match[a-z]+" nil t)
  8584. (setq beg (match-end 0))
  8585. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8586. (setq end (1- (match-beginning 0)))))
  8587. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8588. (goto-char (point-min))
  8589. (select-window cwin))))
  8590. ;;; The mark ring for links jumps
  8591. (defvar org-mark-ring nil
  8592. "Mark ring for positions before jumps in Org-mode.")
  8593. (defvar org-mark-ring-last-goto nil
  8594. "Last position in the mark ring used to go back.")
  8595. ;; Fill and close the ring
  8596. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8597. (loop for i from 1 to org-mark-ring-length do
  8598. (push (make-marker) org-mark-ring))
  8599. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8600. org-mark-ring)
  8601. (defun org-mark-ring-push (&optional pos buffer)
  8602. "Put the current position or POS into the mark ring and rotate it."
  8603. (interactive)
  8604. (setq pos (or pos (point)))
  8605. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8606. (move-marker (car org-mark-ring)
  8607. (or pos (point))
  8608. (or buffer (current-buffer)))
  8609. (message "%s"
  8610. (substitute-command-keys
  8611. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8612. (defun org-mark-ring-goto (&optional n)
  8613. "Jump to the previous position in the mark ring.
  8614. With prefix arg N, jump back that many stored positions. When
  8615. called several times in succession, walk through the entire ring.
  8616. Org-mode commands jumping to a different position in the current file,
  8617. or to another Org-mode file, automatically push the old position
  8618. onto the ring."
  8619. (interactive "p")
  8620. (let (p m)
  8621. (if (eq last-command this-command)
  8622. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8623. (setq p org-mark-ring))
  8624. (setq org-mark-ring-last-goto p)
  8625. (setq m (car p))
  8626. (switch-to-buffer (marker-buffer m))
  8627. (goto-char m)
  8628. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8629. (defun org-remove-angle-brackets (s)
  8630. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8631. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8632. s)
  8633. (defun org-add-angle-brackets (s)
  8634. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8635. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8636. s)
  8637. (defun org-remove-double-quotes (s)
  8638. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8639. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8640. s)
  8641. ;;; Following specific links
  8642. (defun org-follow-timestamp-link ()
  8643. (cond
  8644. ((org-at-date-range-p t)
  8645. (let ((org-agenda-start-on-weekday)
  8646. (t1 (match-string 1))
  8647. (t2 (match-string 2)))
  8648. (setq t1 (time-to-days (org-time-string-to-time t1))
  8649. t2 (time-to-days (org-time-string-to-time t2)))
  8650. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8651. ((org-at-timestamp-p t)
  8652. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8653. (substring (match-string 1) 0 10)))
  8654. 1))
  8655. (t (error "This should not happen"))))
  8656. ;;; Following file links
  8657. (defvar org-wait nil)
  8658. (defun org-open-file (path &optional in-emacs line search)
  8659. "Open the file at PATH.
  8660. First, this expands any special file name abbreviations. Then the
  8661. configuration variable `org-file-apps' is checked if it contains an
  8662. entry for this file type, and if yes, the corresponding command is launched.
  8663. If no application is found, Emacs simply visits the file.
  8664. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8665. With a double \\[universal-argument] \\[universal-argument] \
  8666. prefix arg, Org tries to avoid opening in Emacs
  8667. and to use an external application to visit the file.
  8668. Optional LINE specifies a line to go to, optional SEARCH a string
  8669. to search for. If LINE or SEARCH is given, the file will be
  8670. opened in Emacs, unless an entry from org-file-apps that makes
  8671. use of groups in a regexp matches.
  8672. If the file does not exist, an error is thrown."
  8673. (let* ((file (if (equal path "")
  8674. buffer-file-name
  8675. (substitute-in-file-name (expand-file-name path))))
  8676. (file-apps (append org-file-apps (org-default-apps)))
  8677. (apps (org-remove-if
  8678. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8679. (apps-dlink (org-remove-if-not
  8680. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8681. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8682. (dirp (if remp nil (file-directory-p file)))
  8683. (file (if (and dirp org-open-directory-means-index-dot-org)
  8684. (concat (file-name-as-directory file) "index.org")
  8685. file))
  8686. (a-m-a-p (assq 'auto-mode apps))
  8687. (dfile (downcase file))
  8688. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8689. (link (cond ((and (eq line nil)
  8690. (eq search nil))
  8691. file)
  8692. (line
  8693. (concat file "::" (number-to-string line)))
  8694. (search
  8695. (concat file "::" search))))
  8696. (dlink (downcase link))
  8697. (old-buffer (current-buffer))
  8698. (old-pos (point))
  8699. (old-mode major-mode)
  8700. ext cmd link-match-data)
  8701. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8702. (setq ext (match-string 1 dfile))
  8703. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8704. (setq ext (match-string 1 dfile))))
  8705. (cond
  8706. ((member in-emacs '((16) system))
  8707. (setq cmd (cdr (assoc 'system apps))))
  8708. (in-emacs (setq cmd 'emacs))
  8709. (t
  8710. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8711. (and dirp (cdr (assoc 'directory apps)))
  8712. ; first, try matching against apps-dlink
  8713. ; if we get a match here, store the match data for later
  8714. (let ((match (assoc-default dlink apps-dlink
  8715. 'string-match)))
  8716. (if match
  8717. (progn (setq link-match-data (match-data))
  8718. match)
  8719. (progn (setq in-emacs (or in-emacs line search))
  8720. nil))) ; if we have no match in apps-dlink,
  8721. ; always open the file in emacs if line or search
  8722. ; is given (for backwards compatibility)
  8723. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8724. 'string-match)
  8725. (cdr (assoc ext apps))
  8726. (cdr (assoc t apps))))))
  8727. (when (eq cmd 'system)
  8728. (setq cmd (cdr (assoc 'system apps))))
  8729. (when (eq cmd 'default)
  8730. (setq cmd (cdr (assoc t apps))))
  8731. (when (eq cmd 'mailcap)
  8732. (require 'mailcap)
  8733. (mailcap-parse-mailcaps)
  8734. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8735. (command (mailcap-mime-info mime-type)))
  8736. (if (stringp command)
  8737. (setq cmd command)
  8738. (setq cmd 'emacs))))
  8739. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8740. (not (file-exists-p file))
  8741. (not org-open-non-existing-files))
  8742. (error "No such file: %s" file))
  8743. (cond
  8744. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8745. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8746. (while (string-match "['\"]%s['\"]" cmd)
  8747. (setq cmd (replace-match "%s" t t cmd)))
  8748. (while (string-match "%s" cmd)
  8749. (setq cmd (replace-match
  8750. (save-match-data
  8751. (shell-quote-argument
  8752. (convert-standard-filename file)))
  8753. t t cmd)))
  8754. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8755. (save-match-data
  8756. (let ((match-index 1)
  8757. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8758. (set-match-data link-match-data)
  8759. (while (<= match-index number-of-groups)
  8760. (let ((regex (concat "%" (number-to-string match-index)))
  8761. (replace-with (match-string match-index dlink)))
  8762. (while (string-match regex cmd)
  8763. (setq cmd (replace-match replace-with t t cmd))))
  8764. (setq match-index (+ match-index 1)))))
  8765. (save-window-excursion
  8766. (start-process-shell-command cmd nil cmd)
  8767. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8768. ))
  8769. ((or (stringp cmd)
  8770. (eq cmd 'emacs))
  8771. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8772. (widen)
  8773. (if line (org-goto-line line)
  8774. (if search (org-link-search search))))
  8775. ((consp cmd)
  8776. (let ((file (convert-standard-filename file)))
  8777. (save-match-data
  8778. (set-match-data link-match-data)
  8779. (eval cmd))))
  8780. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8781. (and (org-mode-p) (eq old-mode 'org-mode)
  8782. (or (not (equal old-buffer (current-buffer)))
  8783. (not (equal old-pos (point))))
  8784. (org-mark-ring-push old-pos old-buffer))))
  8785. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8786. "This function returns non-nil if `entry' uses a regular
  8787. expression which should be matched against the whole link by
  8788. org-open-file.
  8789. It assumes that is the case when the entry uses a regular
  8790. expression which has at least one grouping construct and the
  8791. action is either a lisp form or a command string containing
  8792. '%1', i.e. using at least one subexpression match as a
  8793. parameter."
  8794. (let ((selector (car entry))
  8795. (action (cdr entry)))
  8796. (if (stringp selector)
  8797. (and (> (regexp-opt-depth selector) 0)
  8798. (or (and (stringp action)
  8799. (string-match "%[0-9]" action))
  8800. (consp action)))
  8801. nil)))
  8802. (defun org-default-apps ()
  8803. "Return the default applications for this operating system."
  8804. (cond
  8805. ((eq system-type 'darwin)
  8806. org-file-apps-defaults-macosx)
  8807. ((eq system-type 'windows-nt)
  8808. org-file-apps-defaults-windowsnt)
  8809. (t org-file-apps-defaults-gnu)))
  8810. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8811. "Convert extensions to regular expressions in the cars of LIST.
  8812. Also, weed out any non-string entries, because the return value is used
  8813. only for regexp matching.
  8814. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8815. point to the symbol `emacs', indicating that the file should
  8816. be opened in Emacs."
  8817. (append
  8818. (delq nil
  8819. (mapcar (lambda (x)
  8820. (if (not (stringp (car x)))
  8821. nil
  8822. (if (string-match "\\W" (car x))
  8823. x
  8824. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8825. list))
  8826. (if add-auto-mode
  8827. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8828. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8829. (defun org-file-remote-p (file)
  8830. "Test whether FILE specifies a location on a remote system.
  8831. Return non-nil if the location is indeed remote.
  8832. For example, the filename \"/user@host:/foo\" specifies a location
  8833. on the system \"/user@host:\"."
  8834. (cond ((fboundp 'file-remote-p)
  8835. (file-remote-p file))
  8836. ((fboundp 'tramp-handle-file-remote-p)
  8837. (tramp-handle-file-remote-p file))
  8838. ((and (boundp 'ange-ftp-name-format)
  8839. (string-match (car ange-ftp-name-format) file))
  8840. t)
  8841. (t nil)))
  8842. ;;;; Refiling
  8843. (defun org-get-org-file ()
  8844. "Read a filename, with default directory `org-directory'."
  8845. (let ((default (or org-default-notes-file remember-data-file)))
  8846. (read-file-name (format "File name [%s]: " default)
  8847. (file-name-as-directory org-directory)
  8848. default)))
  8849. (defun org-notes-order-reversed-p ()
  8850. "Check if the current file should receive notes in reversed order."
  8851. (cond
  8852. ((not org-reverse-note-order) nil)
  8853. ((eq t org-reverse-note-order) t)
  8854. ((not (listp org-reverse-note-order)) nil)
  8855. (t (catch 'exit
  8856. (let ((all org-reverse-note-order)
  8857. entry)
  8858. (while (setq entry (pop all))
  8859. (if (string-match (car entry) buffer-file-name)
  8860. (throw 'exit (cdr entry))))
  8861. nil)))))
  8862. (defvar org-refile-target-table nil
  8863. "The list of refile targets, created by `org-refile'.")
  8864. (defvar org-agenda-new-buffers nil
  8865. "Buffers created to visit agenda files.")
  8866. (defvar org-refile-cache nil
  8867. "Cache for refile targets.")
  8868. (defvar org-refile-markers nil
  8869. "All the markers used for caching refile locations.")
  8870. (defun org-refile-marker (pos)
  8871. "Get a new refile marker, but only if caching is in use."
  8872. (if (not org-refile-use-cache)
  8873. pos
  8874. (let ((m (make-marker)))
  8875. (move-marker m pos)
  8876. (push m org-refile-markers)
  8877. m)))
  8878. (defun org-refile-cache-clear ()
  8879. "Clear the refile cache and disable all the markers."
  8880. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8881. (setq org-refile-markers nil)
  8882. (setq org-refile-cache nil)
  8883. (message "Refile cache has been cleared"))
  8884. (defun org-refile-cache-check-set (set)
  8885. "Check if all the markers in the cache still have live buffers."
  8886. (let (marker)
  8887. (catch 'exit
  8888. (while (and set (setq marker (nth 3 (pop set))))
  8889. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8890. (when (and marker (null (marker-buffer marker)))
  8891. (message "not found") (sit-for 3)
  8892. (throw 'exit nil)))
  8893. t)))
  8894. (defun org-refile-cache-put (set &rest identifiers)
  8895. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8896. (let* ((key (sha1 (prin1-to-string identifiers)))
  8897. (entry (assoc key org-refile-cache)))
  8898. (if entry
  8899. (setcdr entry set)
  8900. (push (cons key set) org-refile-cache))))
  8901. (defun org-refile-cache-get (&rest identifiers)
  8902. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8903. (cond
  8904. ((not org-refile-cache) nil)
  8905. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8906. (t
  8907. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8908. org-refile-cache))))
  8909. (and set (org-refile-cache-check-set set) set)))))
  8910. (defun org-get-refile-targets (&optional default-buffer)
  8911. "Produce a table with refile targets."
  8912. (let ((case-fold-search nil)
  8913. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8914. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8915. targets tgs txt re files f desc descre fast-path-p level pos0)
  8916. (message "Getting targets...")
  8917. (with-current-buffer (or default-buffer (current-buffer))
  8918. (while (setq entry (pop entries))
  8919. (setq files (car entry) desc (cdr entry))
  8920. (setq fast-path-p nil)
  8921. (cond
  8922. ((null files) (setq files (list (current-buffer))))
  8923. ((eq files 'org-agenda-files)
  8924. (setq files (org-agenda-files 'unrestricted)))
  8925. ((and (symbolp files) (fboundp files))
  8926. (setq files (funcall files)))
  8927. ((and (symbolp files) (boundp files))
  8928. (setq files (symbol-value files))))
  8929. (if (stringp files) (setq files (list files)))
  8930. (cond
  8931. ((eq (car desc) :tag)
  8932. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8933. ((eq (car desc) :todo)
  8934. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8935. ((eq (car desc) :regexp)
  8936. (setq descre (cdr desc)))
  8937. ((eq (car desc) :level)
  8938. (setq descre (concat "^\\*\\{" (number-to-string
  8939. (if org-odd-levels-only
  8940. (1- (* 2 (cdr desc)))
  8941. (cdr desc)))
  8942. "\\}[ \t]")))
  8943. ((eq (car desc) :maxlevel)
  8944. (setq fast-path-p t)
  8945. (setq descre (concat "^\\*\\{1," (number-to-string
  8946. (if org-odd-levels-only
  8947. (1- (* 2 (cdr desc)))
  8948. (cdr desc)))
  8949. "\\}[ \t]")))
  8950. (t (error "Bad refiling target description %s" desc)))
  8951. (while (setq f (pop files))
  8952. (with-current-buffer
  8953. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8954. (or
  8955. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8956. (progn
  8957. (if (bufferp f) (setq f (buffer-file-name
  8958. (buffer-base-buffer f))))
  8959. (setq f (and f (expand-file-name f)))
  8960. (if (eq org-refile-use-outline-path 'file)
  8961. (push (list (file-name-nondirectory f) f nil nil) tgs))
  8962. (save-excursion
  8963. (save-restriction
  8964. (widen)
  8965. (goto-char (point-min))
  8966. (while (re-search-forward descre nil t)
  8967. (goto-char (setq pos0 (point-at-bol)))
  8968. (catch 'next
  8969. (when org-refile-target-verify-function
  8970. (save-match-data
  8971. (or (funcall org-refile-target-verify-function)
  8972. (throw 'next t))))
  8973. (when (looking-at org-complex-heading-regexp)
  8974. (setq level (org-reduced-level
  8975. (- (match-end 1) (match-beginning 1)))
  8976. txt (org-link-display-format (match-string 4))
  8977. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  8978. re (format org-complex-heading-regexp-format
  8979. (regexp-quote (match-string 4))))
  8980. (when org-refile-use-outline-path
  8981. (setq txt (mapconcat
  8982. 'org-protect-slash
  8983. (append
  8984. (if (eq org-refile-use-outline-path
  8985. 'file)
  8986. (list (file-name-nondirectory
  8987. (buffer-file-name
  8988. (buffer-base-buffer))))
  8989. (if (eq org-refile-use-outline-path
  8990. 'full-file-path)
  8991. (list (buffer-file-name
  8992. (buffer-base-buffer)))))
  8993. (org-get-outline-path fast-path-p
  8994. level txt)
  8995. (list txt))
  8996. "/")))
  8997. (push (list txt f re (org-refile-marker (point)))
  8998. tgs)))
  8999. (when (= (point) pos0)
  9000. ;; verification function has not moved point
  9001. (goto-char (point-at-eol))))))))
  9002. (when org-refile-use-cache
  9003. (org-refile-cache-put tgs (buffer-file-name) descre))
  9004. (setq targets (append tgs targets))
  9005. ))))
  9006. (message "Getting targets...done")
  9007. (nreverse targets)))
  9008. (defun org-protect-slash (s)
  9009. (while (string-match "/" s)
  9010. (setq s (replace-match "\\" t t s)))
  9011. s)
  9012. (defvar org-olpa (make-vector 20 nil))
  9013. (defun org-get-outline-path (&optional fastp level heading)
  9014. "Return the outline path to the current entry, as a list.
  9015. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9016. routine which makes outline path derivations for an entire file,
  9017. avoiding backtracing. Refile target collection makes use of that."
  9018. (if fastp
  9019. (progn
  9020. (if (> level 19)
  9021. (error "Outline path failure, more than 19 levels"))
  9022. (loop for i from level upto 19 do
  9023. (aset org-olpa i nil))
  9024. (prog1
  9025. (delq nil (append org-olpa nil))
  9026. (aset org-olpa level heading)))
  9027. (let (rtn case-fold-search)
  9028. (save-excursion
  9029. (save-restriction
  9030. (widen)
  9031. (while (org-up-heading-safe)
  9032. (when (looking-at org-complex-heading-regexp)
  9033. (push (org-match-string-no-properties 4) rtn)))
  9034. rtn)))))
  9035. (defun org-format-outline-path (path &optional width prefix)
  9036. "Format the outline path PATH for display.
  9037. Width is the maximum number of characters that is available.
  9038. Prefix is a prefix to be included in the returned string,
  9039. such as the file name."
  9040. (setq width (or width 79))
  9041. (if prefix (setq width (- width (length prefix))))
  9042. (if (not path)
  9043. (or prefix "")
  9044. (let* ((nsteps (length path))
  9045. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9046. (maxwidth (if (<= total-width width)
  9047. 10000 ;; everything fits
  9048. ;; we need to shorten the level headings
  9049. (/ (- width nsteps) nsteps)))
  9050. (org-odd-levels-only nil)
  9051. (n 0)
  9052. (total (1+ (length prefix))))
  9053. (setq maxwidth (max maxwidth 10))
  9054. (concat prefix
  9055. (mapconcat
  9056. (lambda (h)
  9057. (setq n (1+ n))
  9058. (if (and (= n nsteps) (< maxwidth 10000))
  9059. (setq maxwidth (- total-width total)))
  9060. (if (< (length h) maxwidth)
  9061. (progn (setq total (+ total (length h) 1)) h)
  9062. (setq h (substring h 0 (- maxwidth 2))
  9063. total (+ total maxwidth 1))
  9064. (if (string-match "[ \t]+\\'" h)
  9065. (setq h (substring h 0 (match-beginning 0))))
  9066. (setq h (concat h "..")))
  9067. (org-add-props h nil 'face
  9068. (nth (% (1- n) org-n-level-faces)
  9069. org-level-faces))
  9070. h)
  9071. path "/")))))
  9072. (defun org-display-outline-path (&optional file current)
  9073. "Display the current outline path in the echo area."
  9074. (interactive "P")
  9075. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9076. (case-fold-search nil)
  9077. (path (and (org-mode-p) (org-get-outline-path))))
  9078. (if current (setq path (append path
  9079. (save-excursion
  9080. (org-back-to-heading t)
  9081. (if (looking-at org-complex-heading-regexp)
  9082. (list (match-string 4)))))))
  9083. (message "%s"
  9084. (org-format-outline-path
  9085. path
  9086. (1- (frame-width))
  9087. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9088. (defvar org-refile-history nil
  9089. "History for refiling operations.")
  9090. (defvar org-after-refile-insert-hook nil
  9091. "Hook run after `org-refile' has inserted its stuff at the new location.
  9092. Note that this is still *before* the stuff will be removed from
  9093. the *old* location.")
  9094. (defvar org-capture-last-stored-marker)
  9095. (defun org-refile (&optional goto default-buffer rfloc)
  9096. "Move the entry at point to another heading.
  9097. The list of target headings is compiled using the information in
  9098. `org-refile-targets', which see. This list is created before each use
  9099. and will therefore always be up-to-date.
  9100. At the target location, the entry is filed as a subitem of the target heading.
  9101. Depending on `org-reverse-note-order', the new subitem will either be the
  9102. first or the last subitem.
  9103. If there is an active region, all entries in that region will be moved.
  9104. However, the region must fulfill the requirement that the first heading
  9105. is the first one sets the top-level of the moved text - at most siblings
  9106. below it are allowed.
  9107. With prefix arg GOTO, the command will only visit the target location,
  9108. not actually move anything.
  9109. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9110. go to the location where the last refiling
  9111. operation has put the subtree.
  9112. With a prefix argument of `2', refile to the running clock.
  9113. RFLOC can be a refile location obtained in a different way.
  9114. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9115. If you are using target caching (see `org-refile-use-cache'),
  9116. You have to clear the target cache in order to find new targets.
  9117. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9118. (interactive "P")
  9119. (if (member goto '(0 (64)))
  9120. (org-refile-cache-clear)
  9121. (let* ((cbuf (current-buffer))
  9122. (regionp (org-region-active-p))
  9123. (region-start (and regionp (region-beginning)))
  9124. (region-end (and regionp (region-end)))
  9125. (region-length (and regionp (- region-end region-start)))
  9126. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9127. pos it nbuf file re level reversed)
  9128. (setq last-command nil)
  9129. (when regionp
  9130. (goto-char region-start)
  9131. (or (bolp) (goto-char (point-at-bol)))
  9132. (setq region-start (point))
  9133. (unless (org-kill-is-subtree-p
  9134. (buffer-substring region-start region-end))
  9135. (error "The region is not a (sequence of) subtree(s)")))
  9136. (if (equal goto '(16))
  9137. (org-refile-goto-last-stored)
  9138. (when (or
  9139. (and (equal goto 2)
  9140. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9141. (prog1
  9142. (setq it (list (or org-clock-heading "running clock")
  9143. (buffer-file-name
  9144. (marker-buffer org-clock-hd-marker))
  9145. ""
  9146. (marker-position org-clock-hd-marker)))
  9147. (setq goto nil)))
  9148. (setq it (or rfloc
  9149. (save-excursion
  9150. (org-refile-get-location
  9151. (if goto "Goto: " "Refile to: ") default-buffer
  9152. org-refile-allow-creating-parent-nodes)))))
  9153. (setq file (nth 1 it)
  9154. re (nth 2 it)
  9155. pos (nth 3 it))
  9156. (if (and (not goto)
  9157. pos
  9158. (equal (buffer-file-name) file)
  9159. (if regionp
  9160. (and (>= pos region-start)
  9161. (<= pos region-end))
  9162. (and (>= pos (point))
  9163. (< pos (save-excursion
  9164. (org-end-of-subtree t t))))))
  9165. (error "Cannot refile to position inside the tree or region"))
  9166. (setq nbuf (or (find-buffer-visiting file)
  9167. (find-file-noselect file)))
  9168. (if goto
  9169. (progn
  9170. (switch-to-buffer nbuf)
  9171. (goto-char pos)
  9172. (org-show-context 'org-goto))
  9173. (if regionp
  9174. (progn
  9175. (org-kill-new (buffer-substring region-start region-end))
  9176. (org-save-markers-in-region region-start region-end))
  9177. (org-copy-subtree 1 nil t))
  9178. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9179. (find-file-noselect file)))
  9180. (setq reversed (org-notes-order-reversed-p))
  9181. (save-excursion
  9182. (save-restriction
  9183. (widen)
  9184. (if pos
  9185. (progn
  9186. (goto-char pos)
  9187. (looking-at outline-regexp)
  9188. (setq level (org-get-valid-level (funcall outline-level) 1))
  9189. (goto-char
  9190. (if reversed
  9191. (or (outline-next-heading) (point-max))
  9192. (or (save-excursion (org-get-next-sibling))
  9193. (org-end-of-subtree t t)
  9194. (point-max)))))
  9195. (setq level 1)
  9196. (if (not reversed)
  9197. (goto-char (point-max))
  9198. (goto-char (point-min))
  9199. (or (outline-next-heading) (goto-char (point-max)))))
  9200. (if (not (bolp)) (newline))
  9201. (org-paste-subtree level)
  9202. (when org-log-refile
  9203. (org-add-log-setup 'refile nil nil 'findpos
  9204. org-log-refile)
  9205. (unless (eq org-log-refile 'note)
  9206. (save-excursion (org-add-log-note))))
  9207. (and org-auto-align-tags (org-set-tags nil t))
  9208. (bookmark-set "org-refile-last-stored")
  9209. ;; If we are refiling for capture, make sure that the
  9210. ;; last-capture pointers point here
  9211. (when (org-bound-and-true-p org-refile-for-capture)
  9212. (bookmark-set "org-capture-last-stored-marker")
  9213. (move-marker org-capture-last-stored-marker (point)))
  9214. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9215. (run-hooks 'org-after-refile-insert-hook))))
  9216. (if regionp
  9217. (delete-region (point) (+ (point) region-length))
  9218. (org-cut-subtree))
  9219. (when (featurep 'org-inlinetask)
  9220. (org-inlinetask-remove-END-maybe))
  9221. (setq org-markers-to-move nil)
  9222. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9223. (defun org-refile-goto-last-stored ()
  9224. "Go to the location where the last refile was stored."
  9225. (interactive)
  9226. (bookmark-jump "org-refile-last-stored")
  9227. (message "This is the location of the last refile"))
  9228. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9229. "Prompt the user for a refile location, using PROMPT."
  9230. (let ((org-refile-targets org-refile-targets)
  9231. (org-refile-use-outline-path org-refile-use-outline-path))
  9232. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  9233. (unless org-refile-target-table
  9234. (error "No refile targets"))
  9235. (let* ((cbuf (current-buffer))
  9236. (partial-completion-mode nil)
  9237. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9238. (cfunc (if (and org-refile-use-outline-path
  9239. org-outline-path-complete-in-steps)
  9240. 'org-olpath-completing-read
  9241. 'org-icompleting-read))
  9242. (extra (if org-refile-use-outline-path "/" ""))
  9243. (filename (and cfn (expand-file-name cfn)))
  9244. (tbl (mapcar
  9245. (lambda (x)
  9246. (if (and (not (member org-refile-use-outline-path
  9247. '(file full-file-path)))
  9248. (not (equal filename (nth 1 x))))
  9249. (cons (concat (car x) extra " ("
  9250. (file-name-nondirectory (nth 1 x)) ")")
  9251. (cdr x))
  9252. (cons (concat (car x) extra) (cdr x))))
  9253. org-refile-target-table))
  9254. (completion-ignore-case t)
  9255. pa answ parent-target child parent old-hist)
  9256. (setq old-hist org-refile-history)
  9257. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9258. nil 'org-refile-history))
  9259. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9260. (org-refile-check-position pa)
  9261. (if pa
  9262. (progn
  9263. (when (or (not org-refile-history)
  9264. (not (eq old-hist org-refile-history))
  9265. (not (equal (car pa) (car org-refile-history))))
  9266. (setq org-refile-history
  9267. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9268. org-refile-history
  9269. (cdr org-refile-history))))
  9270. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9271. (pop org-refile-history)))
  9272. pa)
  9273. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9274. (progn
  9275. (setq parent (match-string 1 answ)
  9276. child (match-string 2 answ))
  9277. (setq parent-target (or (assoc parent tbl)
  9278. (assoc (concat parent "/") tbl)))
  9279. (when (and parent-target
  9280. (or (eq new-nodes t)
  9281. (and (eq new-nodes 'confirm)
  9282. (y-or-n-p (format "Create new node \"%s\"? "
  9283. child)))))
  9284. (org-refile-new-child parent-target child)))
  9285. (error "Invalid target location")))))
  9286. (defun org-refile-check-position (refile-pointer)
  9287. "Check if the refile pointer matches the readline to which it points."
  9288. (let* ((file (nth 1 refile-pointer))
  9289. (re (nth 2 refile-pointer))
  9290. (pos (nth 3 refile-pointer))
  9291. buffer)
  9292. (when (org-string-nw-p re)
  9293. (setq buffer (if (markerp pos)
  9294. (marker-buffer pos)
  9295. (or (find-buffer-visiting file)
  9296. (find-file-noselect file))))
  9297. (with-current-buffer buffer
  9298. (save-excursion
  9299. (save-restriction
  9300. (widen)
  9301. (goto-char pos)
  9302. (beginning-of-line 1)
  9303. (unless (org-looking-at-p re)
  9304. (error "Invalid refile position, please rebuild the cache"))))))))
  9305. (defun org-refile-new-child (parent-target child)
  9306. "Use refile target PARENT-TARGET to add new CHILD below it."
  9307. (unless parent-target
  9308. (error "Cannot find parent for new node"))
  9309. (let ((file (nth 1 parent-target))
  9310. (pos (nth 3 parent-target))
  9311. level)
  9312. (with-current-buffer (or (find-buffer-visiting file)
  9313. (find-file-noselect file))
  9314. (save-excursion
  9315. (save-restriction
  9316. (widen)
  9317. (if pos
  9318. (goto-char pos)
  9319. (goto-char (point-max))
  9320. (if (not (bolp)) (newline)))
  9321. (when (looking-at outline-regexp)
  9322. (setq level (funcall outline-level))
  9323. (org-end-of-subtree t t))
  9324. (org-back-over-empty-lines)
  9325. (insert "\n" (make-string
  9326. (if pos (org-get-valid-level level 1) 1) ?*)
  9327. " " child "\n")
  9328. (beginning-of-line 0)
  9329. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9330. (defun org-olpath-completing-read (prompt collection &rest args)
  9331. "Read an outline path like a file name."
  9332. (let ((thetable collection)
  9333. (org-completion-use-ido nil) ; does not work with ido.
  9334. (org-completion-use-iswitchb nil)) ; or iswitchb
  9335. (apply
  9336. 'org-icompleting-read prompt
  9337. (lambda (string predicate &optional flag)
  9338. (let (rtn r f (l (length string)))
  9339. (cond
  9340. ((eq flag nil)
  9341. ;; try completion
  9342. (try-completion string thetable))
  9343. ((eq flag t)
  9344. ;; all-completions
  9345. (setq rtn (all-completions string thetable predicate))
  9346. (mapcar
  9347. (lambda (x)
  9348. (setq r (substring x l))
  9349. (if (string-match " ([^)]*)$" x)
  9350. (setq f (match-string 0 x))
  9351. (setq f ""))
  9352. (if (string-match "/" r)
  9353. (concat string (substring r 0 (match-end 0)) f)
  9354. x))
  9355. rtn))
  9356. ((eq flag 'lambda)
  9357. ;; exact match?
  9358. (assoc string thetable)))
  9359. ))
  9360. args)))
  9361. ;;;; Dynamic blocks
  9362. (defun org-find-dblock (name)
  9363. "Find the first dynamic block with name NAME in the buffer.
  9364. If not found, stay at current position and return nil."
  9365. (let (pos)
  9366. (save-excursion
  9367. (goto-char (point-min))
  9368. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9369. nil t)
  9370. (match-beginning 0))))
  9371. (if pos (goto-char pos))
  9372. pos))
  9373. (defconst org-dblock-start-re
  9374. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9375. "Matches the start line of a dynamic block, with parameters.")
  9376. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9377. "Matches the end of a dynamic block.")
  9378. (defun org-create-dblock (plist)
  9379. "Create a dynamic block section, with parameters taken from PLIST.
  9380. PLIST must contain a :name entry which is used as name of the block."
  9381. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9382. (end-of-line 1)
  9383. (newline))
  9384. (let ((col (current-column))
  9385. (name (plist-get plist :name)))
  9386. (insert "#+BEGIN: " name)
  9387. (while plist
  9388. (if (eq (car plist) :name)
  9389. (setq plist (cddr plist))
  9390. (insert " " (prin1-to-string (pop plist)))))
  9391. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9392. (beginning-of-line -2)))
  9393. (defun org-prepare-dblock ()
  9394. "Prepare dynamic block for refresh.
  9395. This empties the block, puts the cursor at the insert position and returns
  9396. the property list including an extra property :name with the block name."
  9397. (unless (looking-at org-dblock-start-re)
  9398. (error "Not at a dynamic block"))
  9399. (let* ((begdel (1+ (match-end 0)))
  9400. (name (org-no-properties (match-string 1)))
  9401. (params (append (list :name name)
  9402. (read (concat "(" (match-string 3) ")")))))
  9403. (save-excursion
  9404. (beginning-of-line 1)
  9405. (skip-chars-forward " \t")
  9406. (setq params (plist-put params :indentation-column (current-column))))
  9407. (unless (re-search-forward org-dblock-end-re nil t)
  9408. (error "Dynamic block not terminated"))
  9409. (setq params
  9410. (append params
  9411. (list :content (buffer-substring
  9412. begdel (match-beginning 0)))))
  9413. (delete-region begdel (match-beginning 0))
  9414. (goto-char begdel)
  9415. (open-line 1)
  9416. params))
  9417. (defun org-map-dblocks (&optional command)
  9418. "Apply COMMAND to all dynamic blocks in the current buffer.
  9419. If COMMAND is not given, use `org-update-dblock'."
  9420. (let ((cmd (or command 'org-update-dblock)))
  9421. (save-excursion
  9422. (goto-char (point-min))
  9423. (while (re-search-forward org-dblock-start-re nil t)
  9424. (goto-char (match-beginning 0))
  9425. (save-excursion
  9426. (condition-case nil
  9427. (funcall cmd)
  9428. (error (message "Error during update of dynamic block"))))
  9429. (unless (re-search-forward org-dblock-end-re nil t)
  9430. (error "Dynamic block not terminated"))))))
  9431. (defun org-dblock-update (&optional arg)
  9432. "User command for updating dynamic blocks.
  9433. Update the dynamic block at point. With prefix ARG, update all dynamic
  9434. blocks in the buffer."
  9435. (interactive "P")
  9436. (if arg
  9437. (org-update-all-dblocks)
  9438. (or (looking-at org-dblock-start-re)
  9439. (org-beginning-of-dblock))
  9440. (org-update-dblock)))
  9441. (defun org-update-dblock ()
  9442. "Update the dynamic block at point.
  9443. This means to empty the block, parse for parameters and then call
  9444. the correct writing function."
  9445. (save-window-excursion
  9446. (let* ((pos (point))
  9447. (line (org-current-line))
  9448. (params (org-prepare-dblock))
  9449. (name (plist-get params :name))
  9450. (indent (plist-get params :indentation-column))
  9451. (cmd (intern (concat "org-dblock-write:" name))))
  9452. (message "Updating dynamic block `%s' at line %d..." name line)
  9453. (funcall cmd params)
  9454. (message "Updating dynamic block `%s' at line %d...done" name line)
  9455. (goto-char pos)
  9456. (when (and indent (> indent 0))
  9457. (setq indent (make-string indent ?\ ))
  9458. (save-excursion
  9459. (org-beginning-of-dblock)
  9460. (forward-line 1)
  9461. (while (not (looking-at org-dblock-end-re))
  9462. (insert indent)
  9463. (beginning-of-line 2))
  9464. (when (looking-at org-dblock-end-re)
  9465. (and (looking-at "[ \t]+")
  9466. (replace-match ""))
  9467. (insert indent)))))))
  9468. (defun org-beginning-of-dblock ()
  9469. "Find the beginning of the dynamic block at point.
  9470. Error if there is no such block at point."
  9471. (let ((pos (point))
  9472. beg)
  9473. (end-of-line 1)
  9474. (if (and (re-search-backward org-dblock-start-re nil t)
  9475. (setq beg (match-beginning 0))
  9476. (re-search-forward org-dblock-end-re nil t)
  9477. (> (match-end 0) pos))
  9478. (goto-char beg)
  9479. (goto-char pos)
  9480. (error "Not in a dynamic block"))))
  9481. (defun org-update-all-dblocks ()
  9482. "Update all dynamic blocks in the buffer.
  9483. This function can be used in a hook."
  9484. (when (org-mode-p)
  9485. (org-map-dblocks 'org-update-dblock)))
  9486. ;;;; Completion
  9487. (defconst org-additional-option-like-keywords
  9488. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  9489. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  9490. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9491. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  9492. "BEGIN:" "END:"
  9493. "ORGTBL" "TBLFM:" "TBLNAME:"
  9494. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9495. "BEGIN_QUOTE" "END_QUOTE"
  9496. "BEGIN_VERSE" "END_VERSE"
  9497. "BEGIN_CENTER" "END_CENTER"
  9498. "BEGIN_SRC" "END_SRC"
  9499. "CATEGORY" "COLUMNS" "PROPERTY"
  9500. "CAPTION" "LABEL"
  9501. "SETUPFILE"
  9502. "BIND"
  9503. "MACRO"))
  9504. (defcustom org-structure-template-alist
  9505. '(
  9506. ("s" "#+begin_src ?\n\n#+end_src"
  9507. "<src lang=\"?\">\n\n</src>")
  9508. ("e" "#+begin_example\n?\n#+end_example"
  9509. "<example>\n?\n</example>")
  9510. ("q" "#+begin_quote\n?\n#+end_quote"
  9511. "<quote>\n?\n</quote>")
  9512. ("v" "#+begin_verse\n?\n#+end_verse"
  9513. "<verse>\n?\n/verse>")
  9514. ("c" "#+begin_center\n?\n#+end_center"
  9515. "<center>\n?\n/center>")
  9516. ("l" "#+begin_latex\n?\n#+end_latex"
  9517. "<literal style=\"latex\">\n?\n</literal>")
  9518. ("L" "#+latex: "
  9519. "<literal style=\"latex\">?</literal>")
  9520. ("h" "#+begin_html\n?\n#+end_html"
  9521. "<literal style=\"html\">\n?\n</literal>")
  9522. ("H" "#+html: "
  9523. "<literal style=\"html\">?</literal>")
  9524. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9525. ("A" "#+ascii: ")
  9526. ("i" "#+include %file ?"
  9527. "<include file=%file markup=\"?\">")
  9528. )
  9529. "Structure completion elements.
  9530. This is a list of abbreviation keys and values. The value gets inserted
  9531. if you type `<' followed by the key and then press the completion key,
  9532. usually `M-TAB'. %file will be replaced by a file name after prompting
  9533. for the file using completion.
  9534. There are two templates for each key, the first uses the original Org syntax,
  9535. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9536. the default when the /org-mtags.el/ module has been loaded. See also the
  9537. variable `org-mtags-prefer-muse-templates'.
  9538. This is an experimental feature, it is undecided if it is going to stay in."
  9539. :group 'org-completion
  9540. :type '(repeat
  9541. (string :tag "Key")
  9542. (string :tag "Template")
  9543. (string :tag "Muse Template")))
  9544. (defun org-try-structure-completion ()
  9545. "Try to complete a structure template before point.
  9546. This looks for strings like \"<e\" on an otherwise empty line and
  9547. expands them."
  9548. (let ((l (buffer-substring (point-at-bol) (point)))
  9549. a)
  9550. (when (and (looking-at "[ \t]*$")
  9551. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9552. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9553. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9554. (match-beginning 1)) a)
  9555. t)))
  9556. (defun org-complete-expand-structure-template (start cell)
  9557. "Expand a structure template."
  9558. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9559. (rpl (nth (if musep 2 1) cell))
  9560. (ind ""))
  9561. (delete-region start (point))
  9562. (when (string-match "\\`#\\+" rpl)
  9563. (cond
  9564. ((bolp))
  9565. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9566. (setq ind (buffer-substring (point-at-bol) (point))))
  9567. (t (newline))))
  9568. (setq start (point))
  9569. (if (string-match "%file" rpl)
  9570. (setq rpl (replace-match
  9571. (concat
  9572. "\""
  9573. (save-match-data
  9574. (abbreviate-file-name (read-file-name "Include file: ")))
  9575. "\"")
  9576. t t rpl)))
  9577. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9578. (concat "\n" ind)))
  9579. (insert rpl)
  9580. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9581. (defun org-complete (&optional arg)
  9582. "Perform completion on word at point.
  9583. At the beginning of a headline, this completes TODO keywords as given in
  9584. `org-todo-keywords'.
  9585. If the current word is preceded by a backslash, completes the TeX symbols
  9586. that are supported for HTML support.
  9587. If the current word is preceded by \"#+\", completes special words for
  9588. setting file options.
  9589. In the line after \"#+STARTUP:, complete valid keywords.\"
  9590. At all other locations, this simply calls the value of
  9591. `org-completion-fallback-command'."
  9592. (interactive "P")
  9593. (org-without-partial-completion
  9594. (catch 'exit
  9595. (let* ((a nil)
  9596. (end (point))
  9597. (beg1 (save-excursion
  9598. (skip-chars-backward (org-re "[:alnum:]_@#%"))
  9599. (point)))
  9600. (beg (save-excursion
  9601. (skip-chars-backward "a-zA-Z0-9_:$")
  9602. (point)))
  9603. (confirm (lambda (x) (stringp (car x))))
  9604. (searchhead (equal (char-before beg) ?*))
  9605. (struct
  9606. (when (and (member (char-before beg1) '(?. ?<))
  9607. (setq a (assoc (buffer-substring beg1 (point))
  9608. org-structure-template-alist)))
  9609. (org-complete-expand-structure-template (1- beg1) a)
  9610. (throw 'exit t)))
  9611. (tag (and (equal (char-before beg1) ?:)
  9612. (equal (char-after (point-at-bol)) ?*)))
  9613. (prop (or (and (equal (char-before beg1) ?:)
  9614. (not (equal (char-after (point-at-bol)) ?*)))
  9615. (string-match "^#\\+PROPERTY:.*"
  9616. (buffer-substring (point-at-bol) (point)))))
  9617. (texp (equal (char-before beg) ?\\))
  9618. (link (equal (char-before beg) ?\[))
  9619. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9620. beg)
  9621. "#+"))
  9622. (startup (string-match "^#\\+STARTUP:.*"
  9623. (buffer-substring (point-at-bol) (point))))
  9624. (completion-ignore-case opt)
  9625. (type nil)
  9626. (tbl nil)
  9627. (table (cond
  9628. (opt
  9629. (setq type :opt)
  9630. (require 'org-exp)
  9631. (append
  9632. (delq nil
  9633. (mapcar
  9634. (lambda (x)
  9635. (if (string-match
  9636. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9637. (cons (match-string 2 x)
  9638. (match-string 1 x))))
  9639. (org-split-string (org-get-current-options) "\n")))
  9640. (mapcar 'list org-additional-option-like-keywords)))
  9641. (startup
  9642. (setq type :startup)
  9643. org-startup-options)
  9644. (link (append org-link-abbrev-alist-local
  9645. org-link-abbrev-alist))
  9646. (texp
  9647. (setq type :tex)
  9648. (append org-entities-user org-entities))
  9649. ((string-match "\\`\\*+[ \t]+\\'"
  9650. (buffer-substring (point-at-bol) beg))
  9651. (setq type :todo)
  9652. (mapcar 'list org-todo-keywords-1))
  9653. (searchhead
  9654. (setq type :searchhead)
  9655. (save-excursion
  9656. (goto-char (point-min))
  9657. (while (re-search-forward org-todo-line-regexp nil t)
  9658. (push (list
  9659. (org-make-org-heading-search-string
  9660. (match-string 3) t))
  9661. tbl)))
  9662. tbl)
  9663. (tag (setq type :tag beg beg1)
  9664. (or org-tag-alist (org-get-buffer-tags)))
  9665. (prop (setq type :prop beg beg1)
  9666. (mapcar 'list (org-buffer-property-keys nil t t)))
  9667. (t (progn
  9668. (call-interactively org-completion-fallback-command)
  9669. (throw 'exit nil)))))
  9670. (pattern (buffer-substring-no-properties beg end))
  9671. (completion (try-completion pattern table confirm)))
  9672. (cond ((eq completion t)
  9673. (if (not (assoc (upcase pattern) table))
  9674. (message "Already complete")
  9675. (if (and (equal type :opt)
  9676. (not (member (car (assoc (upcase pattern) table))
  9677. org-additional-option-like-keywords)))
  9678. (insert (substring (cdr (assoc (upcase pattern) table))
  9679. (length pattern)))
  9680. (if (memq type '(:tag :prop)) (insert ":")))))
  9681. ((null completion)
  9682. (message "Can't find completion for \"%s\"" pattern)
  9683. (ding))
  9684. ((not (string= pattern completion))
  9685. (delete-region beg end)
  9686. (if (string-match " +$" completion)
  9687. (setq completion (replace-match "" t t completion)))
  9688. (insert completion)
  9689. (if (get-buffer-window "*Completions*")
  9690. (delete-window (get-buffer-window "*Completions*")))
  9691. (if (assoc completion table)
  9692. (if (eq type :todo) (insert " ")
  9693. (if (and (memq type '(:tag :prop))
  9694. (not (string-match "^#[ \t]*\\+property:"
  9695. (org-current-line-string t))))
  9696. (insert ":"))))
  9697. (if (and (equal type :opt) (assoc completion table))
  9698. (message "%s" (substitute-command-keys
  9699. "Press \\[org-complete] again to insert example settings"))))
  9700. (t
  9701. (message "Making completion list...")
  9702. (let ((list (sort (all-completions pattern table confirm)
  9703. 'string<)))
  9704. (with-output-to-temp-buffer "*Completions*"
  9705. (condition-case nil
  9706. ;; Protection needed for XEmacs and emacs 21
  9707. (display-completion-list list pattern)
  9708. (error (display-completion-list list)))))
  9709. (message "Making completion list...%s" "done")))))))
  9710. ;;;; TODO, DEADLINE, Comments
  9711. (defun org-toggle-comment ()
  9712. "Change the COMMENT state of an entry."
  9713. (interactive)
  9714. (save-excursion
  9715. (org-back-to-heading)
  9716. (let (case-fold-search)
  9717. (if (looking-at (concat outline-regexp
  9718. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9719. (replace-match "" t t nil 1)
  9720. (if (looking-at outline-regexp)
  9721. (progn
  9722. (goto-char (match-end 0))
  9723. (insert org-comment-string " ")))))))
  9724. (defvar org-last-todo-state-is-todo nil
  9725. "This is non-nil when the last TODO state change led to a TODO state.
  9726. If the last change removed the TODO tag or switched to DONE, then
  9727. this is nil.")
  9728. (defvar org-setting-tags nil) ; dynamically skipped
  9729. (defvar org-todo-setup-filter-hook nil
  9730. "Hook for functions that pre-filter todo specs.
  9731. Each function takes a todo spec and returns either nil or the spec
  9732. transformed into canonical form." )
  9733. (defvar org-todo-get-default-hook nil
  9734. "Hook for functions that get a default item for todo.
  9735. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9736. nil or a string to be used for the todo mark." )
  9737. (defvar org-agenda-headline-snapshot-before-repeat)
  9738. (defun org-todo (&optional arg)
  9739. "Change the TODO state of an item.
  9740. The state of an item is given by a keyword at the start of the heading,
  9741. like
  9742. *** TODO Write paper
  9743. *** DONE Call mom
  9744. The different keywords are specified in the variable `org-todo-keywords'.
  9745. By default the available states are \"TODO\" and \"DONE\".
  9746. So for this example: when the item starts with TODO, it is changed to DONE.
  9747. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9748. DONE are present, add TODO at the beginning of the heading.
  9749. With \\[universal-argument] prefix arg, use completion to determine the new \
  9750. state.
  9751. With numeric prefix arg, switch to that state.
  9752. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9753. keywords (nextset).
  9754. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9755. For calling through lisp, arg is also interpreted in the following way:
  9756. 'none -> empty state
  9757. \"\"(empty string) -> switch to empty state
  9758. 'done -> switch to DONE
  9759. 'nextset -> switch to the next set of keywords
  9760. 'previousset -> switch to the previous set of keywords
  9761. \"WAITING\" -> switch to the specified keyword, but only if it
  9762. really is a member of `org-todo-keywords'."
  9763. (interactive "P")
  9764. (if (equal arg '(16)) (setq arg 'nextset))
  9765. (let ((org-blocker-hook org-blocker-hook)
  9766. (case-fold-search nil))
  9767. (when (equal arg '(64))
  9768. (setq arg nil org-blocker-hook nil))
  9769. (when (and org-blocker-hook
  9770. (or org-inhibit-blocking
  9771. (org-entry-get nil "NOBLOCKING")))
  9772. (setq org-blocker-hook nil))
  9773. (save-excursion
  9774. (catch 'exit
  9775. (org-back-to-heading t)
  9776. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9777. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9778. (looking-at " *"))
  9779. (let* ((match-data (match-data))
  9780. (startpos (point-at-bol))
  9781. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9782. (org-log-done org-log-done)
  9783. (org-log-repeat org-log-repeat)
  9784. (org-todo-log-states org-todo-log-states)
  9785. (this (match-string 1))
  9786. (hl-pos (match-beginning 0))
  9787. (head (org-get-todo-sequence-head this))
  9788. (ass (assoc head org-todo-kwd-alist))
  9789. (interpret (nth 1 ass))
  9790. (done-word (nth 3 ass))
  9791. (final-done-word (nth 4 ass))
  9792. (last-state (or this ""))
  9793. (completion-ignore-case t)
  9794. (member (member this org-todo-keywords-1))
  9795. (tail (cdr member))
  9796. (state (cond
  9797. ((and org-todo-key-trigger
  9798. (or (and (equal arg '(4))
  9799. (eq org-use-fast-todo-selection 'prefix))
  9800. (and (not arg) org-use-fast-todo-selection
  9801. (not (eq org-use-fast-todo-selection
  9802. 'prefix)))))
  9803. ;; Use fast selection
  9804. (org-fast-todo-selection))
  9805. ((and (equal arg '(4))
  9806. (or (not org-use-fast-todo-selection)
  9807. (not org-todo-key-trigger)))
  9808. ;; Read a state with completion
  9809. (org-icompleting-read
  9810. "State: " (mapcar (lambda(x) (list x))
  9811. org-todo-keywords-1)
  9812. nil t))
  9813. ((eq arg 'right)
  9814. (if this
  9815. (if tail (car tail) nil)
  9816. (car org-todo-keywords-1)))
  9817. ((eq arg 'left)
  9818. (if (equal member org-todo-keywords-1)
  9819. nil
  9820. (if this
  9821. (nth (- (length org-todo-keywords-1)
  9822. (length tail) 2)
  9823. org-todo-keywords-1)
  9824. (org-last org-todo-keywords-1))))
  9825. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9826. (setq arg nil))) ; hack to fall back to cycling
  9827. (arg
  9828. ;; user or caller requests a specific state
  9829. (cond
  9830. ((equal arg "") nil)
  9831. ((eq arg 'none) nil)
  9832. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9833. ((eq arg 'nextset)
  9834. (or (car (cdr (member head org-todo-heads)))
  9835. (car org-todo-heads)))
  9836. ((eq arg 'previousset)
  9837. (let ((org-todo-heads (reverse org-todo-heads)))
  9838. (or (car (cdr (member head org-todo-heads)))
  9839. (car org-todo-heads))))
  9840. ((car (member arg org-todo-keywords-1)))
  9841. ((stringp arg)
  9842. (error "State `%s' not valid in this file" arg))
  9843. ((nth (1- (prefix-numeric-value arg))
  9844. org-todo-keywords-1))))
  9845. ((null member) (or head (car org-todo-keywords-1)))
  9846. ((equal this final-done-word) nil) ;; -> make empty
  9847. ((null tail) nil) ;; -> first entry
  9848. ((memq interpret '(type priority))
  9849. (if (eq this-command last-command)
  9850. (car tail)
  9851. (if (> (length tail) 0)
  9852. (or done-word (car org-done-keywords))
  9853. nil)))
  9854. (t
  9855. (car tail))))
  9856. (state (or
  9857. (run-hook-with-args-until-success
  9858. 'org-todo-get-default-hook state last-state)
  9859. state))
  9860. (next (if state (concat " " state " ") " "))
  9861. (change-plist (list :type 'todo-state-change :from this :to state
  9862. :position startpos))
  9863. dolog now-done-p)
  9864. (when org-blocker-hook
  9865. (setq org-last-todo-state-is-todo
  9866. (not (member this org-done-keywords)))
  9867. (unless (save-excursion
  9868. (save-match-data
  9869. (run-hook-with-args-until-failure
  9870. 'org-blocker-hook change-plist)))
  9871. (if (interactive-p)
  9872. (error "TODO state change from %s to %s blocked" this state)
  9873. ;; fail silently
  9874. (message "TODO state change from %s to %s blocked" this state)
  9875. (throw 'exit nil))))
  9876. (store-match-data match-data)
  9877. (replace-match next t t)
  9878. (unless (pos-visible-in-window-p hl-pos)
  9879. (message "TODO state changed to %s" (org-trim next)))
  9880. (unless head
  9881. (setq head (org-get-todo-sequence-head state)
  9882. ass (assoc head org-todo-kwd-alist)
  9883. interpret (nth 1 ass)
  9884. done-word (nth 3 ass)
  9885. final-done-word (nth 4 ass)))
  9886. (when (memq arg '(nextset previousset))
  9887. (message "Keyword-Set %d/%d: %s"
  9888. (- (length org-todo-sets) -1
  9889. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9890. (length org-todo-sets)
  9891. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9892. (setq org-last-todo-state-is-todo
  9893. (not (member state org-done-keywords)))
  9894. (setq now-done-p (and (member state org-done-keywords)
  9895. (not (member this org-done-keywords))))
  9896. (and logging (org-local-logging logging))
  9897. (when (and (or org-todo-log-states org-log-done)
  9898. (not (eq org-inhibit-logging t))
  9899. (not (memq arg '(nextset previousset))))
  9900. ;; we need to look at recording a time and note
  9901. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9902. (nth 2 (assoc this org-todo-log-states))))
  9903. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9904. (setq dolog 'time))
  9905. (when (and state
  9906. (member state org-not-done-keywords)
  9907. (not (member this org-not-done-keywords)))
  9908. ;; This is now a todo state and was not one before
  9909. ;; If there was a CLOSED time stamp, get rid of it.
  9910. (org-add-planning-info nil nil 'closed))
  9911. (when (and now-done-p org-log-done)
  9912. ;; It is now done, and it was not done before
  9913. (org-add-planning-info 'closed (org-current-time))
  9914. (if (and (not dolog) (eq 'note org-log-done))
  9915. (org-add-log-setup 'done state this 'findpos 'note)))
  9916. (when (and state dolog)
  9917. ;; This is a non-nil state, and we need to log it
  9918. (org-add-log-setup 'state state this 'findpos dolog)))
  9919. ;; Fixup tag positioning
  9920. (org-todo-trigger-tag-changes state)
  9921. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9922. (when org-provide-todo-statistics
  9923. (org-update-parent-todo-statistics))
  9924. (run-hooks 'org-after-todo-state-change-hook)
  9925. (if (and arg (not (member state org-done-keywords)))
  9926. (setq head (org-get-todo-sequence-head state)))
  9927. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9928. ;; Do we need to trigger a repeat?
  9929. (when now-done-p
  9930. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9931. ;; This is for the agenda, take a snapshot of the headline.
  9932. (save-match-data
  9933. (setq org-agenda-headline-snapshot-before-repeat
  9934. (org-get-heading))))
  9935. (org-auto-repeat-maybe state))
  9936. ;; Fixup cursor location if close to the keyword
  9937. (if (and (outline-on-heading-p)
  9938. (not (bolp))
  9939. (save-excursion (beginning-of-line 1)
  9940. (looking-at org-todo-line-regexp))
  9941. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9942. (progn
  9943. (goto-char (or (match-end 2) (match-end 1)))
  9944. (and (looking-at " ") (just-one-space))))
  9945. (when org-trigger-hook
  9946. (save-excursion
  9947. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9948. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9949. "Block turning an entry into a TODO, using the hierarchy.
  9950. This checks whether the current task should be blocked from state
  9951. changes. Such blocking occurs when:
  9952. 1. The task has children which are not all in a completed state.
  9953. 2. A task has a parent with the property :ORDERED:, and there
  9954. are siblings prior to the current task with incomplete
  9955. status.
  9956. 3. The parent of the task is blocked because it has siblings that should
  9957. be done first, or is child of a block grandparent TODO entry."
  9958. (if (not org-enforce-todo-dependencies)
  9959. t ; if locally turned off don't block
  9960. (catch 'dont-block
  9961. ;; If this is not a todo state change, or if this entry is already DONE,
  9962. ;; do not block
  9963. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9964. (member (plist-get change-plist :from)
  9965. (cons 'done org-done-keywords))
  9966. (member (plist-get change-plist :to)
  9967. (cons 'todo org-not-done-keywords))
  9968. (not (plist-get change-plist :to)))
  9969. (throw 'dont-block t))
  9970. ;; If this task has children, and any are undone, it's blocked
  9971. (save-excursion
  9972. (org-back-to-heading t)
  9973. (let ((this-level (funcall outline-level)))
  9974. (outline-next-heading)
  9975. (let ((child-level (funcall outline-level)))
  9976. (while (and (not (eobp))
  9977. (> child-level this-level))
  9978. ;; this todo has children, check whether they are all
  9979. ;; completed
  9980. (if (and (not (org-entry-is-done-p))
  9981. (org-entry-is-todo-p))
  9982. (throw 'dont-block nil))
  9983. (outline-next-heading)
  9984. (setq child-level (funcall outline-level))))))
  9985. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9986. ;; any previous siblings are undone, it's blocked
  9987. (save-excursion
  9988. (org-back-to-heading t)
  9989. (let* ((pos (point))
  9990. (parent-pos (and (org-up-heading-safe) (point))))
  9991. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9992. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9993. (forward-line 1)
  9994. (re-search-forward org-not-done-heading-regexp pos t))
  9995. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9996. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9997. (while t
  9998. (goto-char parent-pos)
  9999. (if (not (looking-at org-not-done-heading-regexp))
  10000. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10001. (setq pos (point))
  10002. (setq parent-pos (and (org-up-heading-safe) (point)))
  10003. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10004. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10005. (forward-line 1)
  10006. (re-search-forward org-not-done-heading-regexp pos t))
  10007. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10008. (defcustom org-track-ordered-property-with-tag nil
  10009. "Should the ORDERED property also be shown as a tag?
  10010. The ORDERED property decides if an entry should require subtasks to be
  10011. completed in sequence. Since a property is not very visible, setting
  10012. this option means that toggling the ORDERED property with the command
  10013. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10014. not relevant for the behavior, but it makes things more visible.
  10015. Note that toggling the tag with tags commands will not change the property
  10016. and therefore not influence behavior!
  10017. This can be t, meaning the tag ORDERED should be used, It can also be a
  10018. string to select a different tag for this task."
  10019. :group 'org-todo
  10020. :type '(choice
  10021. (const :tag "No tracking" nil)
  10022. (const :tag "Track with ORDERED tag" t)
  10023. (string :tag "Use other tag")))
  10024. (defun org-toggle-ordered-property ()
  10025. "Toggle the ORDERED property of the current entry.
  10026. For better visibility, you can track the value of this property with a tag.
  10027. See variable `org-track-ordered-property-with-tag'."
  10028. (interactive)
  10029. (let* ((t1 org-track-ordered-property-with-tag)
  10030. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10031. (save-excursion
  10032. (org-back-to-heading)
  10033. (if (org-entry-get nil "ORDERED")
  10034. (progn
  10035. (org-delete-property "ORDERED")
  10036. (and tag (org-toggle-tag tag 'off))
  10037. (message "Subtasks can be completed in arbitrary order"))
  10038. (org-entry-put nil "ORDERED" "t")
  10039. (and tag (org-toggle-tag tag 'on))
  10040. (message "Subtasks must be completed in sequence")))))
  10041. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10042. (defun org-block-todo-from-checkboxes (change-plist)
  10043. "Block turning an entry into a TODO, using checkboxes.
  10044. This checks whether the current task should be blocked from state
  10045. changes because there are unchecked boxes in this entry."
  10046. (if (not org-enforce-todo-checkbox-dependencies)
  10047. t ; if locally turned off don't block
  10048. (catch 'dont-block
  10049. ;; If this is not a todo state change, or if this entry is already DONE,
  10050. ;; do not block
  10051. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10052. (member (plist-get change-plist :from)
  10053. (cons 'done org-done-keywords))
  10054. (member (plist-get change-plist :to)
  10055. (cons 'todo org-not-done-keywords))
  10056. (not (plist-get change-plist :to)))
  10057. (throw 'dont-block t))
  10058. ;; If this task has checkboxes that are not checked, it's blocked
  10059. (save-excursion
  10060. (org-back-to-heading t)
  10061. (let ((beg (point)) end)
  10062. (outline-next-heading)
  10063. (setq end (point))
  10064. (goto-char beg)
  10065. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10066. end t)
  10067. (progn
  10068. (if (boundp 'org-blocked-by-checkboxes)
  10069. (setq org-blocked-by-checkboxes t))
  10070. (throw 'dont-block nil)))))
  10071. t))) ; do not block
  10072. (defun org-entry-blocked-p ()
  10073. "Is the current entry blocked?"
  10074. (if (org-entry-get nil "NOBLOCKING")
  10075. nil ;; Never block this entry
  10076. (not
  10077. (run-hook-with-args-until-failure
  10078. 'org-blocker-hook
  10079. (list :type 'todo-state-change
  10080. :position (point)
  10081. :from 'todo
  10082. :to 'done)))))
  10083. (defun org-update-statistics-cookies (all)
  10084. "Update the statistics cookie, either from TODO or from checkboxes.
  10085. This should be called with the cursor in a line with a statistics cookie."
  10086. (interactive "P")
  10087. (if all
  10088. (progn
  10089. (org-update-checkbox-count 'all)
  10090. (org-map-entries 'org-update-parent-todo-statistics))
  10091. (if (not (org-on-heading-p))
  10092. (org-update-checkbox-count)
  10093. (let ((pos (move-marker (make-marker) (point)))
  10094. end l1 l2)
  10095. (ignore-errors (org-back-to-heading t))
  10096. (if (not (org-on-heading-p))
  10097. (org-update-checkbox-count)
  10098. (setq l1 (org-outline-level))
  10099. (setq end (save-excursion
  10100. (outline-next-heading)
  10101. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10102. (point)))
  10103. (if (and (save-excursion
  10104. (re-search-forward
  10105. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10106. (not (save-excursion (re-search-forward
  10107. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10108. (org-update-checkbox-count)
  10109. (if (and l2 (> l2 l1))
  10110. (progn
  10111. (goto-char end)
  10112. (org-update-parent-todo-statistics))
  10113. (goto-char pos)
  10114. (beginning-of-line 1)
  10115. (while (re-search-forward
  10116. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10117. (point-at-eol) t)
  10118. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10119. (goto-char pos)
  10120. (move-marker pos nil)))))
  10121. (defvar org-entry-property-inherited-from) ;; defined below
  10122. (defun org-update-parent-todo-statistics ()
  10123. "Update any statistics cookie in the parent of the current headline.
  10124. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10125. the entire subtree and this will travel up the hierarchy and update
  10126. statistics everywhere."
  10127. (interactive)
  10128. (let* ((lim 0) prop
  10129. (recursive (or (not org-hierarchical-todo-statistics)
  10130. (string-match
  10131. "\\<recursive\\>"
  10132. (or (setq prop (org-entry-get
  10133. nil "COOKIE_DATA" 'inherit)) ""))))
  10134. (lim (or (and prop (marker-position
  10135. org-entry-property-inherited-from))
  10136. lim))
  10137. (first t)
  10138. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10139. level ltoggle l1 new ndel
  10140. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  10141. (catch 'exit
  10142. (save-excursion
  10143. (beginning-of-line 1)
  10144. (if (org-at-heading-p)
  10145. (setq ltoggle (funcall outline-level))
  10146. (error "This should not happen"))
  10147. (while (and (setq level (org-up-heading-safe))
  10148. (or recursive first)
  10149. (>= (point) lim))
  10150. (setq first nil cookie-present nil)
  10151. (unless (and level
  10152. (not (string-match
  10153. "\\<checkbox\\>"
  10154. (downcase
  10155. (or (org-entry-get
  10156. nil "COOKIE_DATA")
  10157. "")))))
  10158. (throw 'exit nil))
  10159. (while (re-search-forward box-re (point-at-eol) t)
  10160. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10161. (setq is-percent (match-end 2))
  10162. (save-match-data
  10163. (unless (outline-next-heading) (throw 'exit nil))
  10164. (while (and (looking-at org-complex-heading-regexp)
  10165. (> (setq l1 (length (match-string 1))) level))
  10166. (setq kwd (and (or recursive (= l1 ltoggle))
  10167. (match-string 2)))
  10168. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10169. (and (listp org-provide-todo-statistics)
  10170. (or (member kwd org-provide-todo-statistics)
  10171. (member kwd org-done-keywords))))
  10172. (setq cnt-all (1+ cnt-all))
  10173. (if (eq org-provide-todo-statistics t)
  10174. (and kwd (setq cnt-all (1+ cnt-all)))))
  10175. (and (member kwd org-done-keywords)
  10176. (setq cnt-done (1+ cnt-done)))
  10177. (outline-next-heading)))
  10178. (setq new
  10179. (if is-percent
  10180. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10181. (format "[%d/%d]" cnt-done cnt-all))
  10182. ndel (- (match-end 0) (match-beginning 0)))
  10183. (goto-char (match-beginning 0))
  10184. (insert new)
  10185. (delete-region (point) (+ (point) ndel)))
  10186. (when cookie-present
  10187. (run-hook-with-args 'org-after-todo-statistics-hook
  10188. cnt-done (- cnt-all cnt-done))))))
  10189. (run-hooks 'org-todo-statistics-hook)))
  10190. (defvar org-after-todo-statistics-hook nil
  10191. "Hook that is called after a TODO statistics cookie has been updated.
  10192. Each function is called with two arguments: the number of not-done entries
  10193. and the number of done entries.
  10194. For example, the following function, when added to this hook, will switch
  10195. an entry to DONE when all children are done, and back to TODO when new
  10196. entries are set to a TODO status. Note that this hook is only called
  10197. when there is a statistics cookie in the headline!
  10198. (defun org-summary-todo (n-done n-not-done)
  10199. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10200. (let (org-log-done org-log-states) ; turn off logging
  10201. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10202. ")
  10203. (defvar org-todo-statistics-hook nil
  10204. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10205. This hook runs even if there is no statistics cookie present, in which case
  10206. `org-after-todo-statistics-hook' would not run.")
  10207. (defun org-todo-trigger-tag-changes (state)
  10208. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10209. (let ((l org-todo-state-tags-triggers)
  10210. changes)
  10211. (when (or (not state) (equal state ""))
  10212. (setq changes (append changes (cdr (assoc "" l)))))
  10213. (when (and (stringp state) (> (length state) 0))
  10214. (setq changes (append changes (cdr (assoc state l)))))
  10215. (when (member state org-not-done-keywords)
  10216. (setq changes (append changes (cdr (assoc 'todo l)))))
  10217. (when (member state org-done-keywords)
  10218. (setq changes (append changes (cdr (assoc 'done l)))))
  10219. (dolist (c changes)
  10220. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10221. (defun org-local-logging (value)
  10222. "Get logging settings from a property VALUE."
  10223. (let* (words w a)
  10224. ;; directly set the variables, they are already local.
  10225. (setq org-log-done nil
  10226. org-log-repeat nil
  10227. org-todo-log-states nil)
  10228. (setq words (org-split-string value))
  10229. (while (setq w (pop words))
  10230. (cond
  10231. ((setq a (assoc w org-startup-options))
  10232. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10233. (set (nth 1 a) (nth 2 a))))
  10234. ((setq a (org-extract-log-state-settings w))
  10235. (and (member (car a) org-todo-keywords-1)
  10236. (push a org-todo-log-states)))))))
  10237. (defun org-get-todo-sequence-head (kwd)
  10238. "Return the head of the TODO sequence to which KWD belongs.
  10239. If KWD is not set, check if there is a text property remembering the
  10240. right sequence."
  10241. (let (p)
  10242. (cond
  10243. ((not kwd)
  10244. (or (get-text-property (point-at-bol) 'org-todo-head)
  10245. (progn
  10246. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10247. nil (point-at-eol)))
  10248. (get-text-property p 'org-todo-head))))
  10249. ((not (member kwd org-todo-keywords-1))
  10250. (car org-todo-keywords-1))
  10251. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10252. (defun org-fast-todo-selection ()
  10253. "Fast TODO keyword selection with single keys.
  10254. Returns the new TODO keyword, or nil if no state change should occur."
  10255. (let* ((fulltable org-todo-key-alist)
  10256. (done-keywords org-done-keywords) ;; needed for the faces.
  10257. (maxlen (apply 'max (mapcar
  10258. (lambda (x)
  10259. (if (stringp (car x)) (string-width (car x)) 0))
  10260. fulltable)))
  10261. (expert nil)
  10262. (fwidth (+ maxlen 3 1 3))
  10263. (ncol (/ (- (window-width) 4) fwidth))
  10264. tg cnt e c tbl
  10265. groups ingroup)
  10266. (save-excursion
  10267. (save-window-excursion
  10268. (if expert
  10269. (set-buffer (get-buffer-create " *Org todo*"))
  10270. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10271. (erase-buffer)
  10272. (org-set-local 'org-done-keywords done-keywords)
  10273. (setq tbl fulltable cnt 0)
  10274. (while (setq e (pop tbl))
  10275. (cond
  10276. ((equal e '(:startgroup))
  10277. (push '() groups) (setq ingroup t)
  10278. (when (not (= cnt 0))
  10279. (setq cnt 0)
  10280. (insert "\n"))
  10281. (insert "{ "))
  10282. ((equal e '(:endgroup))
  10283. (setq ingroup nil cnt 0)
  10284. (insert "}\n"))
  10285. ((equal e '(:newline))
  10286. (when (not (= cnt 0))
  10287. (setq cnt 0)
  10288. (insert "\n")
  10289. (setq e (car tbl))
  10290. (while (equal (car tbl) '(:newline))
  10291. (insert "\n")
  10292. (setq tbl (cdr tbl)))))
  10293. (t
  10294. (setq tg (car e) c (cdr e))
  10295. (if ingroup (push tg (car groups)))
  10296. (setq tg (org-add-props tg nil 'face
  10297. (org-get-todo-face tg)))
  10298. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10299. (insert "[" c "] " tg (make-string
  10300. (- fwidth 4 (length tg)) ?\ ))
  10301. (when (= (setq cnt (1+ cnt)) ncol)
  10302. (insert "\n")
  10303. (if ingroup (insert " "))
  10304. (setq cnt 0)))))
  10305. (insert "\n")
  10306. (goto-char (point-min))
  10307. (if (not expert) (org-fit-window-to-buffer))
  10308. (message "[a-z..]:Set [SPC]:clear")
  10309. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10310. (cond
  10311. ((or (= c ?\C-g)
  10312. (and (= c ?q) (not (rassoc c fulltable))))
  10313. (setq quit-flag t))
  10314. ((= c ?\ ) nil)
  10315. ((setq e (rassoc c fulltable) tg (car e))
  10316. tg)
  10317. (t (setq quit-flag t)))))))
  10318. (defun org-entry-is-todo-p ()
  10319. (member (org-get-todo-state) org-not-done-keywords))
  10320. (defun org-entry-is-done-p ()
  10321. (member (org-get-todo-state) org-done-keywords))
  10322. (defun org-get-todo-state ()
  10323. (save-excursion
  10324. (org-back-to-heading t)
  10325. (and (looking-at org-todo-line-regexp)
  10326. (match-end 2)
  10327. (match-string 2))))
  10328. (defun org-at-date-range-p (&optional inactive-ok)
  10329. "Is the cursor inside a date range?"
  10330. (interactive)
  10331. (save-excursion
  10332. (catch 'exit
  10333. (let ((pos (point)))
  10334. (skip-chars-backward "^[<\r\n")
  10335. (skip-chars-backward "<[")
  10336. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10337. (>= (match-end 0) pos)
  10338. (throw 'exit t))
  10339. (skip-chars-backward "^<[\r\n")
  10340. (skip-chars-backward "<[")
  10341. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10342. (>= (match-end 0) pos)
  10343. (throw 'exit t)))
  10344. nil)))
  10345. (defun org-get-repeat (&optional tagline)
  10346. "Check if there is a deadline/schedule with repeater in this entry."
  10347. (save-match-data
  10348. (save-excursion
  10349. (org-back-to-heading t)
  10350. (and (re-search-forward (if tagline
  10351. (concat tagline "\\s-*" org-repeat-re)
  10352. org-repeat-re)
  10353. (org-entry-end-position) t)
  10354. (match-string-no-properties 1)))))
  10355. (defvar org-last-changed-timestamp)
  10356. (defvar org-last-inserted-timestamp)
  10357. (defvar org-log-post-message)
  10358. (defvar org-log-note-purpose)
  10359. (defvar org-log-note-how)
  10360. (defvar org-log-note-extra)
  10361. (defun org-auto-repeat-maybe (done-word)
  10362. "Check if the current headline contains a repeated deadline/schedule.
  10363. If yes, set TODO state back to what it was and change the base date
  10364. of repeating deadline/scheduled time stamps to new date.
  10365. This function is run automatically after each state change to a DONE state."
  10366. ;; last-state is dynamically scoped into this function
  10367. (let* ((repeat (org-get-repeat))
  10368. (aa (assoc last-state org-todo-kwd-alist))
  10369. (interpret (nth 1 aa))
  10370. (head (nth 2 aa))
  10371. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10372. (msg "Entry repeats: ")
  10373. (org-log-done nil)
  10374. (org-todo-log-states nil)
  10375. (nshiftmax 10) (nshift 0)
  10376. re type n what ts time to-state)
  10377. (when repeat
  10378. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10379. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10380. org-todo-repeat-to-state))
  10381. (unless (and to-state (member to-state org-todo-keywords-1))
  10382. (setq to-state (if (eq interpret 'type) last-state head)))
  10383. (org-todo to-state)
  10384. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10385. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10386. (org-time-stamp-format t t))))
  10387. (when org-log-repeat
  10388. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10389. (memq 'org-add-log-note post-command-hook))
  10390. ;; OK, we are already setup for some record
  10391. (if (eq org-log-repeat 'note)
  10392. ;; make sure we take a note, not only a time stamp
  10393. (setq org-log-note-how 'note))
  10394. ;; Set up for taking a record
  10395. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10396. last-state
  10397. 'findpos org-log-repeat)))
  10398. (org-back-to-heading t)
  10399. (org-add-planning-info nil nil 'closed)
  10400. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10401. org-deadline-time-regexp "\\)\\|\\("
  10402. org-ts-regexp "\\)"))
  10403. (while (re-search-forward
  10404. re (save-excursion (outline-next-heading) (point)) t)
  10405. (setq type (if (match-end 1) org-scheduled-string
  10406. (if (match-end 3) org-deadline-string "Plain:"))
  10407. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10408. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10409. (setq n (string-to-number (match-string 2 ts))
  10410. what (match-string 3 ts))
  10411. (if (equal what "w") (setq n (* n 7) what "d"))
  10412. ;; Preparation, see if we need to modify the start date for the change
  10413. (when (match-end 1)
  10414. (setq time (save-match-data (org-time-string-to-time ts)))
  10415. (cond
  10416. ((equal (match-string 1 ts) ".")
  10417. ;; Shift starting date to today
  10418. (org-timestamp-change
  10419. (- (time-to-days (current-time)) (time-to-days time))
  10420. 'day))
  10421. ((equal (match-string 1 ts) "+")
  10422. (while (or (= nshift 0)
  10423. (<= (time-to-days time) (time-to-days (current-time))))
  10424. (when (= (incf nshift) nshiftmax)
  10425. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10426. (error "Abort")))
  10427. (org-timestamp-change n (cdr (assoc what whata)))
  10428. (org-at-timestamp-p t)
  10429. (setq ts (match-string 1))
  10430. (setq time (save-match-data (org-time-string-to-time ts))))
  10431. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10432. ;; rematch, so that we have everything in place for the real shift
  10433. (org-at-timestamp-p t)
  10434. (setq ts (match-string 1))
  10435. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10436. (org-timestamp-change n (cdr (assoc what whata)))
  10437. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10438. (setq org-log-post-message msg)
  10439. (message "%s" msg))))
  10440. (defun org-show-todo-tree (arg)
  10441. "Make a compact tree which shows all headlines marked with TODO.
  10442. The tree will show the lines where the regexp matches, and all higher
  10443. headlines above the match.
  10444. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10445. With a numeric prefix N, construct a sparse tree for the Nth element
  10446. of `org-todo-keywords-1'."
  10447. (interactive "P")
  10448. (let ((case-fold-search nil)
  10449. (kwd-re
  10450. (cond ((null arg) org-not-done-regexp)
  10451. ((equal arg '(4))
  10452. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10453. (mapcar 'list org-todo-keywords-1))))
  10454. (concat "\\("
  10455. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10456. "\\)\\>")))
  10457. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10458. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10459. org-todo-keywords-1)))
  10460. (t (error "Invalid prefix argument: %s" arg)))))
  10461. (message "%d TODO entries found"
  10462. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10463. (defun org-deadline (&optional remove time)
  10464. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10465. With argument REMOVE, remove any deadline from the item.
  10466. When TIME is set, it should be an internal time specification, and the
  10467. scheduling will use the corresponding date."
  10468. (interactive "P")
  10469. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10470. (repeater (and old-date
  10471. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10472. (match-string 1 old-date))))
  10473. (if remove
  10474. (progn
  10475. (when (and old-date org-log-redeadline)
  10476. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10477. org-log-redeadline))
  10478. (org-remove-timestamp-with-keyword org-deadline-string)
  10479. (message "Item no longer has a deadline."))
  10480. (org-add-planning-info 'deadline time 'closed)
  10481. (when (and old-date org-log-redeadline
  10482. (not (equal old-date
  10483. (substring org-last-inserted-timestamp 1 -1))))
  10484. (org-add-log-setup 'redeadline nil old-date 'findpos
  10485. org-log-redeadline))
  10486. (when repeater
  10487. (save-excursion
  10488. (org-back-to-heading t)
  10489. (when (re-search-forward (concat org-deadline-string " "
  10490. org-last-inserted-timestamp)
  10491. (save-excursion
  10492. (outline-next-heading) (point)) t)
  10493. (goto-char (1- (match-end 0)))
  10494. (insert " " repeater)
  10495. (setq org-last-inserted-timestamp
  10496. (concat (substring org-last-inserted-timestamp 0 -1)
  10497. " " repeater
  10498. (substring org-last-inserted-timestamp -1))))))
  10499. (message "Deadline on %s" org-last-inserted-timestamp))))
  10500. (defun org-schedule (&optional remove time)
  10501. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10502. With argument REMOVE, remove any scheduling date from the item.
  10503. When TIME is set, it should be an internal time specification, and the
  10504. scheduling will use the corresponding date."
  10505. (interactive "P")
  10506. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10507. (repeater (and old-date
  10508. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10509. (match-string 1 old-date))))
  10510. (if remove
  10511. (progn
  10512. (when (and old-date org-log-reschedule)
  10513. (org-add-log-setup 'delschedule nil old-date 'findpos
  10514. org-log-reschedule))
  10515. (org-remove-timestamp-with-keyword org-scheduled-string)
  10516. (message "Item is no longer scheduled."))
  10517. (org-add-planning-info 'scheduled time 'closed)
  10518. (when (and old-date org-log-reschedule
  10519. (not (equal old-date
  10520. (substring org-last-inserted-timestamp 1 -1))))
  10521. (org-add-log-setup 'reschedule nil old-date 'findpos
  10522. org-log-reschedule))
  10523. (when repeater
  10524. (save-excursion
  10525. (org-back-to-heading t)
  10526. (when (re-search-forward (concat org-scheduled-string " "
  10527. org-last-inserted-timestamp)
  10528. (save-excursion
  10529. (outline-next-heading) (point)) t)
  10530. (goto-char (1- (match-end 0)))
  10531. (insert " " repeater)
  10532. (setq org-last-inserted-timestamp
  10533. (concat (substring org-last-inserted-timestamp 0 -1)
  10534. " " repeater
  10535. (substring org-last-inserted-timestamp -1))))))
  10536. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10537. (defun org-get-scheduled-time (pom &optional inherit)
  10538. "Get the scheduled time as a time tuple, of a format suitable
  10539. for calling org-schedule with, or if there is no scheduling,
  10540. returns nil."
  10541. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10542. (when time
  10543. (apply 'encode-time (org-parse-time-string time)))))
  10544. (defun org-get-deadline-time (pom &optional inherit)
  10545. "Get the deadline as a time tuple, of a format suitable for
  10546. calling org-deadline with, or if there is no scheduling, returns
  10547. nil."
  10548. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10549. (when time
  10550. (apply 'encode-time (org-parse-time-string time)))))
  10551. (defun org-remove-timestamp-with-keyword (keyword)
  10552. "Remove all time stamps with KEYWORD in the current entry."
  10553. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10554. beg)
  10555. (save-excursion
  10556. (org-back-to-heading t)
  10557. (setq beg (point))
  10558. (outline-next-heading)
  10559. (while (re-search-backward re beg t)
  10560. (replace-match "")
  10561. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10562. (equal (char-before) ?\ ))
  10563. (backward-delete-char 1)
  10564. (if (string-match "^[ \t]*$" (buffer-substring
  10565. (point-at-bol) (point-at-eol)))
  10566. (delete-region (point-at-bol)
  10567. (min (point-max) (1+ (point-at-eol))))))))))
  10568. (defun org-add-planning-info (what &optional time &rest remove)
  10569. "Insert new timestamp with keyword in the line directly after the headline.
  10570. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10571. If non is given, the user is prompted for a date.
  10572. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10573. be removed."
  10574. (interactive)
  10575. (let (org-time-was-given org-end-time-was-given ts
  10576. end default-time default-input)
  10577. (catch 'exit
  10578. (when (and (not time) (memq what '(scheduled deadline)))
  10579. ;; Try to get a default date/time from existing timestamp
  10580. (save-excursion
  10581. (org-back-to-heading t)
  10582. (setq end (save-excursion (outline-next-heading) (point)))
  10583. (when (re-search-forward (if (eq what 'scheduled)
  10584. org-scheduled-time-regexp
  10585. org-deadline-time-regexp)
  10586. end t)
  10587. (setq ts (match-string 1)
  10588. default-time
  10589. (apply 'encode-time (org-parse-time-string ts))
  10590. default-input (and ts (org-get-compact-tod ts))))))
  10591. (when what
  10592. ;; If necessary, get the time from the user
  10593. (setq time (or time (org-read-date nil 'to-time nil nil
  10594. default-time default-input))))
  10595. (when (and org-insert-labeled-timestamps-at-point
  10596. (member what '(scheduled deadline)))
  10597. (insert
  10598. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10599. (org-insert-time-stamp time org-time-was-given
  10600. nil nil nil (list org-end-time-was-given))
  10601. (setq what nil))
  10602. (save-excursion
  10603. (save-restriction
  10604. (let (col list elt ts buffer-invisibility-spec)
  10605. (org-back-to-heading t)
  10606. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10607. (goto-char (match-end 1))
  10608. (setq col (current-column))
  10609. (goto-char (match-end 0))
  10610. (if (eobp) (insert "\n") (forward-char 1))
  10611. (when (and (not what)
  10612. (not (looking-at
  10613. (concat "[ \t]*"
  10614. org-keyword-time-not-clock-regexp))))
  10615. ;; Nothing to add, nothing to remove...... :-)
  10616. (throw 'exit nil))
  10617. (if (and (not (looking-at outline-regexp))
  10618. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10619. "[^\r\n]*"))
  10620. (not (equal (match-string 1) org-clock-string)))
  10621. (narrow-to-region (match-beginning 0) (match-end 0))
  10622. (insert-before-markers "\n")
  10623. (backward-char 1)
  10624. (narrow-to-region (point) (point))
  10625. (and org-adapt-indentation (org-indent-to-column col)))
  10626. ;; Check if we have to remove something.
  10627. (setq list (cons what remove))
  10628. (while list
  10629. (setq elt (pop list))
  10630. (goto-char (point-min))
  10631. (when (or (and (eq elt 'scheduled)
  10632. (re-search-forward org-scheduled-time-regexp nil t))
  10633. (and (eq elt 'deadline)
  10634. (re-search-forward org-deadline-time-regexp nil t))
  10635. (and (eq elt 'closed)
  10636. (re-search-forward org-closed-time-regexp nil t)))
  10637. (replace-match "")
  10638. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10639. (skip-chars-backward " ")
  10640. (if (looking-at " +") (replace-match ""))))
  10641. (goto-char (point-max))
  10642. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10643. (when what
  10644. (insert
  10645. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10646. (cond ((eq what 'scheduled) org-scheduled-string)
  10647. ((eq what 'deadline) org-deadline-string)
  10648. ((eq what 'closed) org-closed-string))
  10649. " ")
  10650. (setq ts (org-insert-time-stamp
  10651. time
  10652. (or org-time-was-given
  10653. (and (eq what 'closed) org-log-done-with-time))
  10654. (eq what 'closed)
  10655. nil nil (list org-end-time-was-given)))
  10656. (end-of-line 1))
  10657. (goto-char (point-min))
  10658. (widen)
  10659. (if (and (looking-at "[ \t]*\n")
  10660. (equal (char-before) ?\n))
  10661. (delete-region (1- (point)) (point-at-eol)))
  10662. ts))))))
  10663. (defvar org-log-note-marker (make-marker))
  10664. (defvar org-log-note-purpose nil)
  10665. (defvar org-log-note-state nil)
  10666. (defvar org-log-note-previous-state nil)
  10667. (defvar org-log-note-how nil)
  10668. (defvar org-log-note-extra nil)
  10669. (defvar org-log-note-window-configuration nil)
  10670. (defvar org-log-note-return-to (make-marker))
  10671. (defvar org-log-post-message nil
  10672. "Message to be displayed after a log note has been stored.
  10673. The auto-repeater uses this.")
  10674. (defun org-add-note ()
  10675. "Add a note to the current entry.
  10676. This is done in the same way as adding a state change note."
  10677. (interactive)
  10678. (org-add-log-setup 'note nil nil 'findpos nil))
  10679. (defvar org-property-end-re)
  10680. (defun org-add-log-setup (&optional purpose state prev-state
  10681. findpos how extra)
  10682. "Set up the post command hook to take a note.
  10683. If this is about to TODO state change, the new state is expected in STATE.
  10684. When FINDPOS is non-nil, find the correct position for the note in
  10685. the current entry. If not, assume that it can be inserted at point.
  10686. HOW is an indicator what kind of note should be created.
  10687. EXTRA is additional text that will be inserted into the notes buffer."
  10688. (let* ((org-log-into-drawer (org-log-into-drawer))
  10689. (drawer (cond ((stringp org-log-into-drawer)
  10690. org-log-into-drawer)
  10691. (org-log-into-drawer "LOGBOOK")
  10692. (t nil))))
  10693. (save-restriction
  10694. (save-excursion
  10695. (when findpos
  10696. (org-back-to-heading t)
  10697. (narrow-to-region (point) (save-excursion
  10698. (outline-next-heading) (point)))
  10699. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10700. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10701. "[^\r\n]*\\)?"))
  10702. (goto-char (match-end 0))
  10703. (cond
  10704. (drawer
  10705. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10706. nil t)
  10707. (progn
  10708. (goto-char (match-end 0))
  10709. (or org-log-states-order-reversed
  10710. (and (re-search-forward org-property-end-re nil t)
  10711. (goto-char (1- (match-beginning 0))))))
  10712. (insert "\n:" drawer ":\n:END:")
  10713. (beginning-of-line 0)
  10714. (org-indent-line-function)
  10715. (beginning-of-line 2)
  10716. (org-indent-line-function)
  10717. (end-of-line 0)))
  10718. ((and org-log-state-notes-insert-after-drawers
  10719. (save-excursion
  10720. (forward-line) (looking-at org-drawer-regexp)))
  10721. (forward-line)
  10722. (while (looking-at org-drawer-regexp)
  10723. (goto-char (match-end 0))
  10724. (re-search-forward org-property-end-re (point-max) t)
  10725. (forward-line))
  10726. (forward-line -1)))
  10727. (unless org-log-states-order-reversed
  10728. (and (= (char-after) ?\n) (forward-char 1))
  10729. (org-skip-over-state-notes)
  10730. (skip-chars-backward " \t\n\r")))
  10731. (move-marker org-log-note-marker (point))
  10732. (setq org-log-note-purpose purpose
  10733. org-log-note-state state
  10734. org-log-note-previous-state prev-state
  10735. org-log-note-how how
  10736. org-log-note-extra extra)
  10737. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10738. (defun org-skip-over-state-notes ()
  10739. "Skip past the list of State notes in an entry."
  10740. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10741. (when (org-in-item-p)
  10742. (let ((limit (org-list-bottom-point)))
  10743. (while (looking-at "[ \t]*- State")
  10744. (goto-char (or (org-get-next-item (point) limit)
  10745. (org-get-end-of-item limit)))))))
  10746. (defun org-add-log-note (&optional purpose)
  10747. "Pop up a window for taking a note, and add this note later at point."
  10748. (remove-hook 'post-command-hook 'org-add-log-note)
  10749. (setq org-log-note-window-configuration (current-window-configuration))
  10750. (delete-other-windows)
  10751. (move-marker org-log-note-return-to (point))
  10752. (switch-to-buffer (marker-buffer org-log-note-marker))
  10753. (goto-char org-log-note-marker)
  10754. (org-switch-to-buffer-other-window "*Org Note*")
  10755. (erase-buffer)
  10756. (if (memq org-log-note-how '(time state))
  10757. (let (current-prefix-arg) (org-store-log-note))
  10758. (let ((org-inhibit-startup t)) (org-mode))
  10759. (insert (format "# Insert note for %s.
  10760. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10761. (cond
  10762. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10763. ((eq org-log-note-purpose 'done) "closed todo item")
  10764. ((eq org-log-note-purpose 'state)
  10765. (format "state change from \"%s\" to \"%s\""
  10766. (or org-log-note-previous-state "")
  10767. (or org-log-note-state "")))
  10768. ((eq org-log-note-purpose 'reschedule)
  10769. "rescheduling")
  10770. ((eq org-log-note-purpose 'delschedule)
  10771. "no longer scheduled")
  10772. ((eq org-log-note-purpose 'redeadline)
  10773. "changing deadline")
  10774. ((eq org-log-note-purpose 'deldeadline)
  10775. "removing deadline")
  10776. ((eq org-log-note-purpose 'refile)
  10777. "refiling")
  10778. ((eq org-log-note-purpose 'note)
  10779. "this entry")
  10780. (t (error "This should not happen")))))
  10781. (if org-log-note-extra (insert org-log-note-extra))
  10782. (org-set-local 'org-finish-function 'org-store-log-note)))
  10783. (defvar org-note-abort nil) ; dynamically scoped
  10784. (defun org-store-log-note ()
  10785. "Finish taking a log note, and insert it to where it belongs."
  10786. (let ((txt (buffer-string))
  10787. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10788. lines ind bul)
  10789. (kill-buffer (current-buffer))
  10790. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10791. (setq txt (replace-match "" t t txt)))
  10792. (if (string-match "\\s-+\\'" txt)
  10793. (setq txt (replace-match "" t t txt)))
  10794. (setq lines (org-split-string txt "\n"))
  10795. (when (and note (string-match "\\S-" note))
  10796. (setq note
  10797. (org-replace-escapes
  10798. note
  10799. (list (cons "%u" (user-login-name))
  10800. (cons "%U" user-full-name)
  10801. (cons "%t" (format-time-string
  10802. (org-time-stamp-format 'long 'inactive)
  10803. (current-time)))
  10804. (cons "%T" (format-time-string
  10805. (org-time-stamp-format 'long nil)
  10806. (current-time)))
  10807. (cons "%s" (if org-log-note-state
  10808. (concat "\"" org-log-note-state "\"")
  10809. ""))
  10810. (cons "%S" (if org-log-note-previous-state
  10811. (concat "\"" org-log-note-previous-state "\"")
  10812. "\"\"")))))
  10813. (if lines (setq note (concat note " \\\\")))
  10814. (push note lines))
  10815. (when (or current-prefix-arg org-note-abort)
  10816. (when org-log-into-drawer
  10817. (org-remove-empty-drawer-at
  10818. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10819. org-log-note-marker))
  10820. (setq lines nil))
  10821. (when lines
  10822. (with-current-buffer (marker-buffer org-log-note-marker)
  10823. (save-excursion
  10824. (goto-char org-log-note-marker)
  10825. (move-marker org-log-note-marker nil)
  10826. (end-of-line 1)
  10827. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10828. (setq ind (save-excursion
  10829. (if (org-in-item-p)
  10830. (progn
  10831. (goto-char (org-list-top-point))
  10832. (org-get-indentation))
  10833. (skip-chars-backward " \r\t\n")
  10834. (cond
  10835. ((and (org-at-heading-p)
  10836. org-adapt-indentation)
  10837. (1+ (org-current-level)))
  10838. ((org-at-heading-p) 0)
  10839. (t (org-get-indentation))))))
  10840. (setq bul (org-list-bullet-string "-"))
  10841. (org-indent-line-to ind)
  10842. (insert bul (pop lines))
  10843. (let ((ind-body (+ (length bul) ind)))
  10844. (while lines
  10845. (insert "\n")
  10846. (org-indent-line-to ind-body)
  10847. (insert (pop lines))))
  10848. (message "Note stored")
  10849. (org-back-to-heading t)
  10850. (org-cycle-hide-drawers 'children)))))
  10851. (set-window-configuration org-log-note-window-configuration)
  10852. (with-current-buffer (marker-buffer org-log-note-return-to)
  10853. (goto-char org-log-note-return-to))
  10854. (move-marker org-log-note-return-to nil)
  10855. (and org-log-post-message (message "%s" org-log-post-message)))
  10856. (defun org-remove-empty-drawer-at (drawer pos)
  10857. "Remove an empty drawer DRAWER at position POS.
  10858. POS may also be a marker."
  10859. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10860. (save-excursion
  10861. (save-restriction
  10862. (widen)
  10863. (goto-char pos)
  10864. (if (org-in-regexp
  10865. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10866. (replace-match ""))))))
  10867. (defun org-sparse-tree (&optional arg)
  10868. "Create a sparse tree, prompt for the details.
  10869. This command can create sparse trees. You first need to select the type
  10870. of match used to create the tree:
  10871. t Show all TODO entries.
  10872. T Show entries with a specific TODO keyword.
  10873. m Show entries selected by a tags/property match.
  10874. p Enter a property name and its value (both with completion on existing
  10875. names/values) and show entries with that property.
  10876. / Show entries matching a regular expression (`r' can be used as well)
  10877. d Show deadlines due within `org-deadline-warning-days'.
  10878. b Show deadlines and scheduled items before a date.
  10879. a Show deadlines and scheduled items after a date."
  10880. (interactive "P")
  10881. (let (ans kwd value)
  10882. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
  10883. (setq ans (read-char-exclusive))
  10884. (cond
  10885. ((equal ans ?d)
  10886. (call-interactively 'org-check-deadlines))
  10887. ((equal ans ?b)
  10888. (call-interactively 'org-check-before-date))
  10889. ((equal ans ?a)
  10890. (call-interactively 'org-check-after-date))
  10891. ((equal ans ?t)
  10892. (org-show-todo-tree nil))
  10893. ((equal ans ?T)
  10894. (org-show-todo-tree '(4)))
  10895. ((member ans '(?T ?m))
  10896. (call-interactively 'org-match-sparse-tree))
  10897. ((member ans '(?p ?P))
  10898. (setq kwd (org-icompleting-read "Property: "
  10899. (mapcar 'list (org-buffer-property-keys))))
  10900. (setq value (org-icompleting-read "Value: "
  10901. (mapcar 'list (org-property-values kwd))))
  10902. (unless (string-match "\\`{.*}\\'" value)
  10903. (setq value (concat "\"" value "\"")))
  10904. (org-match-sparse-tree arg (concat kwd "=" value)))
  10905. ((member ans '(?r ?R ?/))
  10906. (call-interactively 'org-occur))
  10907. (t (error "No such sparse tree command \"%c\"" ans)))))
  10908. (defvar org-occur-highlights nil
  10909. "List of overlays used for occur matches.")
  10910. (make-variable-buffer-local 'org-occur-highlights)
  10911. (defvar org-occur-parameters nil
  10912. "Parameters of the active org-occur calls.
  10913. This is a list, each call to org-occur pushes as cons cell,
  10914. containing the regular expression and the callback, onto the list.
  10915. The list can contain several entries if `org-occur' has been called
  10916. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10917. will only contain one set of parameters. When the highlights are
  10918. removed (for example with `C-c C-c', or with the next edit (depending
  10919. on `org-remove-highlights-with-change'), this variable is emptied
  10920. as well.")
  10921. (make-variable-buffer-local 'org-occur-parameters)
  10922. (defun org-occur (regexp &optional keep-previous callback)
  10923. "Make a compact tree which shows all matches of REGEXP.
  10924. The tree will show the lines where the regexp matches, and all higher
  10925. headlines above the match. It will also show the heading after the match,
  10926. to make sure editing the matching entry is easy.
  10927. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10928. call to `org-occur' will be kept, to allow stacking of calls to this
  10929. command.
  10930. If CALLBACK is non-nil, it is a function which is called to confirm
  10931. that the match should indeed be shown."
  10932. (interactive "sRegexp: \nP")
  10933. (when (equal regexp "")
  10934. (error "Regexp cannot be empty"))
  10935. (unless keep-previous
  10936. (org-remove-occur-highlights nil nil t))
  10937. (push (cons regexp callback) org-occur-parameters)
  10938. (let ((cnt 0))
  10939. (save-excursion
  10940. (goto-char (point-min))
  10941. (if (or (not keep-previous) ; do not want to keep
  10942. (not org-occur-highlights)) ; no previous matches
  10943. ;; hide everything
  10944. (org-overview))
  10945. (while (re-search-forward regexp nil t)
  10946. (when (or (not callback)
  10947. (save-match-data (funcall callback)))
  10948. (setq cnt (1+ cnt))
  10949. (when org-highlight-sparse-tree-matches
  10950. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10951. (org-show-context 'occur-tree))))
  10952. (when org-remove-highlights-with-change
  10953. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10954. nil 'local))
  10955. (unless org-sparse-tree-open-archived-trees
  10956. (org-hide-archived-subtrees (point-min) (point-max)))
  10957. (run-hooks 'org-occur-hook)
  10958. (if (interactive-p)
  10959. (message "%d match(es) for regexp %s" cnt regexp))
  10960. cnt))
  10961. (defun org-show-context (&optional key)
  10962. "Make sure point and context are visible.
  10963. How much context is shown depends upon the variables
  10964. `org-show-hierarchy-above', `org-show-following-heading'. and
  10965. `org-show-siblings'."
  10966. (let ((heading-p (org-on-heading-p t))
  10967. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10968. (following-p (org-get-alist-option org-show-following-heading key))
  10969. (entry-p (org-get-alist-option org-show-entry-below key))
  10970. (siblings-p (org-get-alist-option org-show-siblings key)))
  10971. (catch 'exit
  10972. ;; Show heading or entry text
  10973. (if (and heading-p (not entry-p))
  10974. (org-flag-heading nil) ; only show the heading
  10975. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10976. (org-show-hidden-entry))) ; show entire entry
  10977. (when following-p
  10978. ;; Show next sibling, or heading below text
  10979. (save-excursion
  10980. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10981. (org-flag-heading nil))))
  10982. (when siblings-p (org-show-siblings))
  10983. (when hierarchy-p
  10984. ;; show all higher headings, possibly with siblings
  10985. (save-excursion
  10986. (while (and (condition-case nil
  10987. (progn (org-up-heading-all 1) t)
  10988. (error nil))
  10989. (not (bobp)))
  10990. (org-flag-heading nil)
  10991. (when siblings-p (org-show-siblings))))))))
  10992. (defvar org-reveal-start-hook nil
  10993. "Hook run before revealing a location.")
  10994. (defun org-reveal (&optional siblings)
  10995. "Show current entry, hierarchy above it, and the following headline.
  10996. This can be used to show a consistent set of context around locations
  10997. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10998. not t for the search context.
  10999. With optional argument SIBLINGS, on each level of the hierarchy all
  11000. siblings are shown. This repairs the tree structure to what it would
  11001. look like when opened with hierarchical calls to `org-cycle'.
  11002. With double optional argument \\[universal-argument] \\[universal-argument], \
  11003. go to the parent and show the
  11004. entire tree."
  11005. (interactive "P")
  11006. (run-hooks 'org-reveal-start-hook)
  11007. (let ((org-show-hierarchy-above t)
  11008. (org-show-following-heading t)
  11009. (org-show-siblings (if siblings t org-show-siblings)))
  11010. (org-show-context nil))
  11011. (when (equal siblings '(16))
  11012. (save-excursion
  11013. (when (org-up-heading-safe)
  11014. (org-show-subtree)
  11015. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11016. (defun org-highlight-new-match (beg end)
  11017. "Highlight from BEG to END and mark the highlight is an occur headline."
  11018. (let ((ov (make-overlay beg end)))
  11019. (overlay-put ov 'face 'secondary-selection)
  11020. (push ov org-occur-highlights)))
  11021. (defun org-remove-occur-highlights (&optional beg end noremove)
  11022. "Remove the occur highlights from the buffer.
  11023. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11024. from the `before-change-functions' in the current buffer."
  11025. (interactive)
  11026. (unless org-inhibit-highlight-removal
  11027. (mapc 'delete-overlay org-occur-highlights)
  11028. (setq org-occur-highlights nil)
  11029. (setq org-occur-parameters nil)
  11030. (unless noremove
  11031. (remove-hook 'before-change-functions
  11032. 'org-remove-occur-highlights 'local))))
  11033. ;;;; Priorities
  11034. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11035. "Regular expression matching the priority indicator.")
  11036. (defvar org-remove-priority-next-time nil)
  11037. (defun org-priority-up ()
  11038. "Increase the priority of the current item."
  11039. (interactive)
  11040. (org-priority 'up))
  11041. (defun org-priority-down ()
  11042. "Decrease the priority of the current item."
  11043. (interactive)
  11044. (org-priority 'down))
  11045. (defun org-priority (&optional action)
  11046. "Change the priority of an item by ARG.
  11047. ACTION can be `set', `up', `down', or a character."
  11048. (interactive)
  11049. (unless org-enable-priority-commands
  11050. (error "Priority commands are disabled"))
  11051. (setq action (or action 'set))
  11052. (let (current new news have remove)
  11053. (save-excursion
  11054. (org-back-to-heading t)
  11055. (if (looking-at org-priority-regexp)
  11056. (setq current (string-to-char (match-string 2))
  11057. have t)
  11058. (setq current org-default-priority))
  11059. (cond
  11060. ((eq action 'remove)
  11061. (setq remove t new ?\ ))
  11062. ((or (eq action 'set)
  11063. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11064. (if (not (eq action 'set))
  11065. (setq new action)
  11066. (message "Priority %c-%c, SPC to remove: "
  11067. org-highest-priority org-lowest-priority)
  11068. (save-match-data
  11069. (setq new (read-char-exclusive))))
  11070. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11071. (= (upcase org-lowest-priority) org-lowest-priority))
  11072. (setq new (upcase new)))
  11073. (cond ((equal new ?\ ) (setq remove t))
  11074. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11075. (error "Priority must be between `%c' and `%c'"
  11076. org-highest-priority org-lowest-priority))))
  11077. ((eq action 'up)
  11078. (if (and (not have) (eq last-command this-command))
  11079. (setq new org-lowest-priority)
  11080. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11081. org-default-priority (1- current)))))
  11082. ((eq action 'down)
  11083. (if (and (not have) (eq last-command this-command))
  11084. (setq new org-highest-priority)
  11085. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11086. org-default-priority (1+ current)))))
  11087. (t (error "Invalid action")))
  11088. (if (or (< (upcase new) org-highest-priority)
  11089. (> (upcase new) org-lowest-priority))
  11090. (setq remove t))
  11091. (setq news (format "%c" new))
  11092. (if have
  11093. (if remove
  11094. (replace-match "" t t nil 1)
  11095. (replace-match news t t nil 2))
  11096. (if remove
  11097. (error "No priority cookie found in line")
  11098. (let ((case-fold-search nil))
  11099. (looking-at org-todo-line-regexp))
  11100. (if (match-end 2)
  11101. (progn
  11102. (goto-char (match-end 2))
  11103. (insert " [#" news "]"))
  11104. (goto-char (match-beginning 3))
  11105. (insert "[#" news "] "))))
  11106. (org-preserve-lc (org-set-tags nil 'align)))
  11107. (if remove
  11108. (message "Priority removed")
  11109. (message "Priority of current item set to %s" news))))
  11110. (defun org-get-priority (s)
  11111. "Find priority cookie and return priority."
  11112. (save-match-data
  11113. (if (not (string-match org-priority-regexp s))
  11114. (* 1000 (- org-lowest-priority org-default-priority))
  11115. (* 1000 (- org-lowest-priority
  11116. (string-to-char (match-string 2 s)))))))
  11117. ;;;; Tags
  11118. (defvar org-agenda-archives-mode)
  11119. (defvar org-map-continue-from nil
  11120. "Position from where mapping should continue.
  11121. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11122. (defvar org-scanner-tags nil
  11123. "The current tag list while the tags scanner is running.")
  11124. (defvar org-trust-scanner-tags nil
  11125. "Should `org-get-tags-at' use the tags fro the scanner.
  11126. This is for internal dynamical scoping only.
  11127. When this is non-nil, the function `org-get-tags-at' will return the value
  11128. of `org-scanner-tags' instead of building the list by itself. This
  11129. can lead to large speed-ups when the tags scanner is used in a file with
  11130. many entries, and when the list of tags is retrieved, for example to
  11131. obtain a list of properties. Building the tags list for each entry in such
  11132. a file becomes an N^2 operation - but with this variable set, it scales
  11133. as N.")
  11134. (defun org-scan-tags (action matcher &optional todo-only)
  11135. "Scan headline tags with inheritance and produce output ACTION.
  11136. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11137. or `agenda' to produce an entry list for an agenda view. It can also be
  11138. a Lisp form or a function that should be called at each matched headline, in
  11139. this case the return value is a list of all return values from these calls.
  11140. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11141. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11142. only lines with a TODO keyword are included in the output."
  11143. (require 'org-agenda)
  11144. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11145. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11146. (org-re
  11147. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11148. (props (list 'face 'default
  11149. 'done-face 'org-agenda-done
  11150. 'undone-face 'default
  11151. 'mouse-face 'highlight
  11152. 'org-not-done-regexp org-not-done-regexp
  11153. 'org-todo-regexp org-todo-regexp
  11154. 'help-echo
  11155. (format "mouse-2 or RET jump to org file %s"
  11156. (abbreviate-file-name
  11157. (or (buffer-file-name (buffer-base-buffer))
  11158. (buffer-name (buffer-base-buffer)))))))
  11159. (case-fold-search nil)
  11160. (org-map-continue-from nil)
  11161. lspos tags tags-list
  11162. (tags-alist (list (cons 0 org-file-tags)))
  11163. (llast 0) rtn rtn1 level category i txt
  11164. todo marker entry priority)
  11165. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11166. (setq action (list 'lambda nil action)))
  11167. (save-excursion
  11168. (goto-char (point-min))
  11169. (when (eq action 'sparse-tree)
  11170. (org-overview)
  11171. (org-remove-occur-highlights))
  11172. (while (re-search-forward re nil t)
  11173. (catch :skip
  11174. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11175. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11176. (goto-char (setq lspos (match-beginning 0)))
  11177. (setq level (org-reduced-level (funcall outline-level))
  11178. category (org-get-category))
  11179. (setq i llast llast level)
  11180. ;; remove tag lists from same and sublevels
  11181. (while (>= i level)
  11182. (when (setq entry (assoc i tags-alist))
  11183. (setq tags-alist (delete entry tags-alist)))
  11184. (setq i (1- i)))
  11185. ;; add the next tags
  11186. (when tags
  11187. (setq tags (org-split-string tags ":")
  11188. tags-alist
  11189. (cons (cons level tags) tags-alist)))
  11190. ;; compile tags for current headline
  11191. (setq tags-list
  11192. (if org-use-tag-inheritance
  11193. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11194. tags)
  11195. org-scanner-tags tags-list)
  11196. (when org-use-tag-inheritance
  11197. (setcdr (car tags-alist)
  11198. (mapcar (lambda (x)
  11199. (setq x (copy-sequence x))
  11200. (org-add-prop-inherited x))
  11201. (cdar tags-alist))))
  11202. (when (and tags org-use-tag-inheritance
  11203. (or (not (eq t org-use-tag-inheritance))
  11204. org-tags-exclude-from-inheritance))
  11205. ;; selective inheritance, remove uninherited ones
  11206. (setcdr (car tags-alist)
  11207. (org-remove-uniherited-tags (cdar tags-alist))))
  11208. (when (and (or (not todo-only)
  11209. (and (member todo org-not-done-keywords)
  11210. (or (not org-agenda-tags-todo-honor-ignore-options)
  11211. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11212. (let ((case-fold-search t)) (eval matcher))
  11213. (or
  11214. (not (member org-archive-tag tags-list))
  11215. ;; we have an archive tag, should we use this anyway?
  11216. (or (not org-agenda-skip-archived-trees)
  11217. (and (eq action 'agenda) org-agenda-archives-mode))))
  11218. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11219. ;; select this headline
  11220. (cond
  11221. ((eq action 'sparse-tree)
  11222. (and org-highlight-sparse-tree-matches
  11223. (org-get-heading) (match-end 0)
  11224. (org-highlight-new-match
  11225. (match-beginning 0) (match-beginning 1)))
  11226. (org-show-context 'tags-tree))
  11227. ((eq action 'agenda)
  11228. (setq txt (org-format-agenda-item
  11229. ""
  11230. (concat
  11231. (if (eq org-tags-match-list-sublevels 'indented)
  11232. (make-string (1- level) ?.) "")
  11233. (org-get-heading))
  11234. category
  11235. tags-list
  11236. )
  11237. priority (org-get-priority txt))
  11238. (goto-char lspos)
  11239. (setq marker (org-agenda-new-marker))
  11240. (org-add-props txt props
  11241. 'org-marker marker 'org-hd-marker marker 'org-category category
  11242. 'todo-state todo
  11243. 'priority priority 'type "tagsmatch")
  11244. (push txt rtn))
  11245. ((functionp action)
  11246. (setq org-map-continue-from nil)
  11247. (save-excursion
  11248. (setq rtn1 (funcall action))
  11249. (push rtn1 rtn)))
  11250. (t (error "Invalid action")))
  11251. ;; if we are to skip sublevels, jump to end of subtree
  11252. (unless org-tags-match-list-sublevels
  11253. (org-end-of-subtree t)
  11254. (backward-char 1))))
  11255. ;; Get the correct position from where to continue
  11256. (if org-map-continue-from
  11257. (goto-char org-map-continue-from)
  11258. (and (= (point) lspos) (end-of-line 1)))))
  11259. (when (and (eq action 'sparse-tree)
  11260. (not org-sparse-tree-open-archived-trees))
  11261. (org-hide-archived-subtrees (point-min) (point-max)))
  11262. (nreverse rtn)))
  11263. (defun org-remove-uniherited-tags (tags)
  11264. "Remove all tags that are not inherited from the list TAGS."
  11265. (cond
  11266. ((eq org-use-tag-inheritance t)
  11267. (if org-tags-exclude-from-inheritance
  11268. (org-delete-all org-tags-exclude-from-inheritance tags)
  11269. tags))
  11270. ((not org-use-tag-inheritance) nil)
  11271. ((stringp org-use-tag-inheritance)
  11272. (delq nil (mapcar
  11273. (lambda (x)
  11274. (if (and (string-match org-use-tag-inheritance x)
  11275. (not (member x org-tags-exclude-from-inheritance)))
  11276. x nil))
  11277. tags)))
  11278. ((listp org-use-tag-inheritance)
  11279. (delq nil (mapcar
  11280. (lambda (x)
  11281. (if (member x org-use-tag-inheritance) x nil))
  11282. tags)))))
  11283. (defvar todo-only) ;; dynamically scoped
  11284. (defun org-match-sparse-tree (&optional todo-only match)
  11285. "Create a sparse tree according to tags string MATCH.
  11286. MATCH can contain positive and negative selection of tags, like
  11287. \"+WORK+URGENT-WITHBOSS\".
  11288. If optional argument TODO-ONLY is non-nil, only select lines that are
  11289. also TODO lines."
  11290. (interactive "P")
  11291. (org-prepare-agenda-buffers (list (current-buffer)))
  11292. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11293. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11294. (defvar org-cached-props nil)
  11295. (defun org-cached-entry-get (pom property)
  11296. (if (or (eq t org-use-property-inheritance)
  11297. (and (stringp org-use-property-inheritance)
  11298. (string-match org-use-property-inheritance property))
  11299. (and (listp org-use-property-inheritance)
  11300. (member property org-use-property-inheritance)))
  11301. ;; Caching is not possible, check it directly
  11302. (org-entry-get pom property 'inherit)
  11303. ;; Get all properties, so that we can do complicated checks easily
  11304. (cdr (assoc property (or org-cached-props
  11305. (setq org-cached-props
  11306. (org-entry-properties pom)))))))
  11307. (defun org-global-tags-completion-table (&optional files)
  11308. "Return the list of all tags in all agenda buffer/files."
  11309. (save-excursion
  11310. (org-uniquify
  11311. (delq nil
  11312. (apply 'append
  11313. (mapcar
  11314. (lambda (file)
  11315. (set-buffer (find-file-noselect file))
  11316. (append (org-get-buffer-tags)
  11317. (mapcar (lambda (x) (if (stringp (car-safe x))
  11318. (list (car-safe x)) nil))
  11319. org-tag-alist)))
  11320. (if (and files (car files))
  11321. files
  11322. (org-agenda-files))))))))
  11323. (defun org-make-tags-matcher (match)
  11324. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11325. ;; todo-only is scoped dynamically into this function, and the function
  11326. ;; may change it if the matcher asks for it.
  11327. (unless match
  11328. ;; Get a new match request, with completion
  11329. (let ((org-last-tags-completion-table
  11330. (org-global-tags-completion-table)))
  11331. (setq match (org-completing-read-no-i
  11332. "Match: " 'org-tags-completion-function nil nil nil
  11333. 'org-tags-history))))
  11334. ;; Parse the string and create a lisp form
  11335. (let ((match0 match)
  11336. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11337. minus tag mm
  11338. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11339. orterms term orlist re-p str-p level-p level-op time-p
  11340. prop-p pn pv po cat-p gv rest)
  11341. (if (string-match "/+" match)
  11342. ;; match contains also a todo-matching request
  11343. (progn
  11344. (setq tagsmatch (substring match 0 (match-beginning 0))
  11345. todomatch (substring match (match-end 0)))
  11346. (if (string-match "^!" todomatch)
  11347. (setq todo-only t todomatch (substring todomatch 1)))
  11348. (if (string-match "^\\s-*$" todomatch)
  11349. (setq todomatch nil)))
  11350. ;; only matching tags
  11351. (setq tagsmatch match todomatch nil))
  11352. ;; Make the tags matcher
  11353. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11354. (setq tagsmatcher t)
  11355. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11356. (while (setq term (pop orterms))
  11357. (while (and (equal (substring term -1) "\\") orterms)
  11358. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11359. (while (string-match re term)
  11360. (setq rest (substring term (match-end 0))
  11361. minus (and (match-end 1)
  11362. (equal (match-string 1 term) "-"))
  11363. tag (save-match-data (replace-regexp-in-string
  11364. "\\\\-" "-"
  11365. (match-string 2 term)))
  11366. re-p (equal (string-to-char tag) ?{)
  11367. level-p (match-end 4)
  11368. prop-p (match-end 5)
  11369. mm (cond
  11370. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11371. (level-p
  11372. (setq level-op (org-op-to-function (match-string 3 term)))
  11373. `(,level-op level ,(string-to-number
  11374. (match-string 4 term))))
  11375. (prop-p
  11376. (setq pn (match-string 5 term)
  11377. po (match-string 6 term)
  11378. pv (match-string 7 term)
  11379. cat-p (equal pn "CATEGORY")
  11380. re-p (equal (string-to-char pv) ?{)
  11381. str-p (equal (string-to-char pv) ?\")
  11382. time-p (save-match-data
  11383. (string-match "^\"[[<].*[]>]\"$" pv))
  11384. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11385. (if time-p (setq pv (org-matcher-time pv)))
  11386. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11387. (cond
  11388. ((equal pn "CATEGORY")
  11389. (setq gv '(get-text-property (point) 'org-category)))
  11390. ((equal pn "TODO")
  11391. (setq gv 'todo))
  11392. (t
  11393. (setq gv `(org-cached-entry-get nil ,pn))))
  11394. (if re-p
  11395. (if (eq po 'org<>)
  11396. `(not (string-match ,pv (or ,gv "")))
  11397. `(string-match ,pv (or ,gv "")))
  11398. (if str-p
  11399. `(,po (or ,gv "") ,pv)
  11400. `(,po (string-to-number (or ,gv ""))
  11401. ,(string-to-number pv) ))))
  11402. (t `(member ,tag tags-list)))
  11403. mm (if minus (list 'not mm) mm)
  11404. term rest)
  11405. (push mm tagsmatcher))
  11406. (push (if (> (length tagsmatcher) 1)
  11407. (cons 'and tagsmatcher)
  11408. (car tagsmatcher))
  11409. orlist)
  11410. (setq tagsmatcher nil))
  11411. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11412. (setq tagsmatcher
  11413. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11414. ;; Make the todo matcher
  11415. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11416. (setq todomatcher t)
  11417. (setq orterms (org-split-string todomatch "|") orlist nil)
  11418. (while (setq term (pop orterms))
  11419. (while (string-match re term)
  11420. (setq minus (and (match-end 1)
  11421. (equal (match-string 1 term) "-"))
  11422. kwd (match-string 2 term)
  11423. re-p (equal (string-to-char kwd) ?{)
  11424. term (substring term (match-end 0))
  11425. mm (if re-p
  11426. `(string-match ,(substring kwd 1 -1) todo)
  11427. (list 'equal 'todo kwd))
  11428. mm (if minus (list 'not mm) mm))
  11429. (push mm todomatcher))
  11430. (push (if (> (length todomatcher) 1)
  11431. (cons 'and todomatcher)
  11432. (car todomatcher))
  11433. orlist)
  11434. (setq todomatcher nil))
  11435. (setq todomatcher (if (> (length orlist) 1)
  11436. (cons 'or orlist) (car orlist))))
  11437. ;; Return the string and lisp forms of the matcher
  11438. (setq matcher (if todomatcher
  11439. (list 'and tagsmatcher todomatcher)
  11440. tagsmatcher))
  11441. (cons match0 matcher)))
  11442. (defun org-op-to-function (op &optional stringp)
  11443. "Turn an operator into the appropriate function."
  11444. (setq op
  11445. (cond
  11446. ((equal op "<" ) '(< string< org-time<))
  11447. ((equal op ">" ) '(> org-string> org-time>))
  11448. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11449. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11450. ((member op '("=" "==")) '(= string= org-time=))
  11451. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11452. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11453. (defun org<> (a b) (not (= a b)))
  11454. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11455. (defun org-string>= (a b) (not (string< a b)))
  11456. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11457. (defun org-string<> (a b) (not (string= a b)))
  11458. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11459. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11460. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11461. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11462. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11463. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11464. (defun org-2ft (s)
  11465. "Convert S to a floating point time.
  11466. If S is already a number, just return it. If it is a string, parse
  11467. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11468. (cond
  11469. ((numberp s) s)
  11470. ((stringp s)
  11471. (condition-case nil
  11472. (float-time (apply 'encode-time (org-parse-time-string s)))
  11473. (error 0.)))
  11474. (t 0.)))
  11475. (defun org-time-today ()
  11476. "Time in seconds today at 0:00.
  11477. Returns the float number of seconds since the beginning of the
  11478. epoch to the beginning of today (00:00)."
  11479. (float-time (apply 'encode-time
  11480. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11481. (defun org-matcher-time (s)
  11482. "Interpret a time comparison value."
  11483. (save-match-data
  11484. (cond
  11485. ((string= s "<now>") (float-time))
  11486. ((string= s "<today>") (org-time-today))
  11487. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11488. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11489. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11490. (+ (org-time-today)
  11491. (* (string-to-number (match-string 1 s))
  11492. (cdr (assoc (match-string 2 s)
  11493. '(("d" . 86400.0) ("w" . 604800.0)
  11494. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11495. (t (org-2ft s)))))
  11496. (defun org-match-any-p (re list)
  11497. "Does re match any element of list?"
  11498. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11499. (delq nil list))
  11500. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11501. (defvar org-tags-overlay (make-overlay 1 1))
  11502. (org-detach-overlay org-tags-overlay)
  11503. (defun org-get-local-tags-at (&optional pos)
  11504. "Get a list of tags defined in the current headline."
  11505. (org-get-tags-at pos 'local))
  11506. (defun org-get-local-tags ()
  11507. "Get a list of tags defined in the current headline."
  11508. (org-get-tags-at nil 'local))
  11509. (defun org-get-tags-at (&optional pos local)
  11510. "Get a list of all headline tags applicable at POS.
  11511. POS defaults to point. If tags are inherited, the list contains
  11512. the targets in the same sequence as the headlines appear, i.e.
  11513. the tags of the current headline come last.
  11514. When LOCAL is non-nil, only return tags from the current headline,
  11515. ignore inherited ones."
  11516. (interactive)
  11517. (if (and org-trust-scanner-tags
  11518. (or (not pos) (equal pos (point)))
  11519. (not local))
  11520. org-scanner-tags
  11521. (let (tags ltags lastpos parent)
  11522. (save-excursion
  11523. (save-restriction
  11524. (widen)
  11525. (goto-char (or pos (point)))
  11526. (save-match-data
  11527. (catch 'done
  11528. (condition-case nil
  11529. (progn
  11530. (org-back-to-heading t)
  11531. (while (not (equal lastpos (point)))
  11532. (setq lastpos (point))
  11533. (when (looking-at
  11534. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11535. (setq ltags (org-split-string
  11536. (org-match-string-no-properties 1) ":"))
  11537. (when parent
  11538. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11539. (setq tags (append
  11540. (if parent
  11541. (org-remove-uniherited-tags ltags)
  11542. ltags)
  11543. tags)))
  11544. (or org-use-tag-inheritance (throw 'done t))
  11545. (if local (throw 'done t))
  11546. (or (org-up-heading-safe) (error nil))
  11547. (setq parent t)))
  11548. (error nil)))))
  11549. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11550. (defun org-add-prop-inherited (s)
  11551. (add-text-properties 0 (length s) '(inherited t) s)
  11552. s)
  11553. (defun org-toggle-tag (tag &optional onoff)
  11554. "Toggle the tag TAG for the current line.
  11555. If ONOFF is `on' or `off', don't toggle but set to this state."
  11556. (let (res current)
  11557. (save-excursion
  11558. (org-back-to-heading t)
  11559. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11560. (point-at-eol) t)
  11561. (progn
  11562. (setq current (match-string 1))
  11563. (replace-match ""))
  11564. (setq current ""))
  11565. (setq current (nreverse (org-split-string current ":")))
  11566. (cond
  11567. ((eq onoff 'on)
  11568. (setq res t)
  11569. (or (member tag current) (push tag current)))
  11570. ((eq onoff 'off)
  11571. (or (not (member tag current)) (setq current (delete tag current))))
  11572. (t (if (member tag current)
  11573. (setq current (delete tag current))
  11574. (setq res t)
  11575. (push tag current))))
  11576. (end-of-line 1)
  11577. (if current
  11578. (progn
  11579. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11580. (org-set-tags nil t))
  11581. (delete-horizontal-space))
  11582. (run-hooks 'org-after-tags-change-hook))
  11583. res))
  11584. (defun org-align-tags-here (to-col)
  11585. ;; Assumes that this is a headline
  11586. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11587. (beginning-of-line 1)
  11588. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11589. (< pos (match-beginning 2)))
  11590. (progn
  11591. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11592. (goto-char (match-beginning 1))
  11593. (insert " ")
  11594. (delete-region (point) (1+ (match-beginning 2)))
  11595. (setq ncol (max (1+ (current-column))
  11596. (1+ col)
  11597. (if (> to-col 0)
  11598. to-col
  11599. (- (abs to-col) tags-l))))
  11600. (setq p (point))
  11601. (insert (make-string (- ncol (current-column)) ?\ ))
  11602. (setq ncol (current-column))
  11603. (when indent-tabs-mode (tabify p (point-at-eol)))
  11604. (org-move-to-column (min ncol col) t))
  11605. (goto-char pos))))
  11606. (defun org-set-tags-command (&optional arg just-align)
  11607. "Call the set-tags command for the current entry."
  11608. (interactive "P")
  11609. (if (org-on-heading-p)
  11610. (org-set-tags arg just-align)
  11611. (save-excursion
  11612. (org-back-to-heading t)
  11613. (org-set-tags arg just-align))))
  11614. (defun org-set-tags-to (data)
  11615. "Set the tags of the current entry to DATA, replacing the current tags.
  11616. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11617. If DATA is nil or the empty string, any tags will be removed."
  11618. (interactive "sTags: ")
  11619. (setq data
  11620. (cond
  11621. ((eq data nil) "")
  11622. ((equal data "") "")
  11623. ((stringp data)
  11624. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11625. ":"))
  11626. ((listp data)
  11627. (concat ":" (mapconcat 'identity data ":") ":"))
  11628. (t nil)))
  11629. (when data
  11630. (save-excursion
  11631. (org-back-to-heading t)
  11632. (when (looking-at org-complex-heading-regexp)
  11633. (if (match-end 5)
  11634. (progn
  11635. (goto-char (match-beginning 5))
  11636. (insert data)
  11637. (delete-region (point) (point-at-eol))
  11638. (org-set-tags nil 'align))
  11639. (goto-char (point-at-eol))
  11640. (insert " " data)
  11641. (org-set-tags nil 'align)))
  11642. (beginning-of-line 1)
  11643. (if (looking-at ".*?\\([ \t]+\\)$")
  11644. (delete-region (match-beginning 1) (match-end 1))))))
  11645. (defun org-align-all-tags ()
  11646. "Align the tags i all headings."
  11647. (interactive)
  11648. (save-excursion
  11649. (or (ignore-errors (org-back-to-heading t))
  11650. (outline-next-heading))
  11651. (if (org-on-heading-p)
  11652. (org-set-tags t)
  11653. (message "No headings"))))
  11654. (defvar org-indent-indentation-per-level)
  11655. (defun org-set-tags (&optional arg just-align)
  11656. "Set the tags for the current headline.
  11657. With prefix ARG, realign all tags in headings in the current buffer."
  11658. (interactive "P")
  11659. (let* ((re (concat "^" outline-regexp))
  11660. (current (org-get-tags-string))
  11661. (col (current-column))
  11662. (org-setting-tags t)
  11663. table current-tags inherited-tags ; computed below when needed
  11664. tags p0 c0 c1 rpl di tc level)
  11665. (if arg
  11666. (save-excursion
  11667. (goto-char (point-min))
  11668. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11669. (while (re-search-forward re nil t)
  11670. (org-set-tags nil t)
  11671. (end-of-line 1)))
  11672. (message "All tags realigned to column %d" org-tags-column))
  11673. (if just-align
  11674. (setq tags current)
  11675. ;; Get a new set of tags from the user
  11676. (save-excursion
  11677. (setq table (append org-tag-persistent-alist
  11678. (or org-tag-alist (org-get-buffer-tags))
  11679. (and
  11680. org-complete-tags-always-offer-all-agenda-tags
  11681. (org-global-tags-completion-table
  11682. (org-agenda-files))))
  11683. org-last-tags-completion-table table
  11684. current-tags (org-split-string current ":")
  11685. inherited-tags (nreverse
  11686. (nthcdr (length current-tags)
  11687. (nreverse (org-get-tags-at))))
  11688. tags
  11689. (if (or (eq t org-use-fast-tag-selection)
  11690. (and org-use-fast-tag-selection
  11691. (delq nil (mapcar 'cdr table))))
  11692. (org-fast-tag-selection
  11693. current-tags inherited-tags table
  11694. (if org-fast-tag-selection-include-todo
  11695. org-todo-key-alist))
  11696. (let ((org-add-colon-after-tag-completion t))
  11697. (org-trim
  11698. (org-without-partial-completion
  11699. (org-icompleting-read "Tags: "
  11700. 'org-tags-completion-function
  11701. nil nil current 'org-tags-history)))))))
  11702. (while (string-match "[-+&]+" tags)
  11703. ;; No boolean logic, just a list
  11704. (setq tags (replace-match ":" t t tags))))
  11705. (setq tags (replace-regexp-in-string "[ ,]" ":" tags))
  11706. (if org-tags-sort-function
  11707. (setq tags (mapconcat 'identity
  11708. (sort (org-split-string
  11709. tags (org-re "[^[:alnum:]_@#%]+"))
  11710. org-tags-sort-function) ":")))
  11711. (if (string-match "\\`[\t ]*\\'" tags)
  11712. (setq tags "")
  11713. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11714. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11715. ;; Insert new tags at the correct column
  11716. (beginning-of-line 1)
  11717. (setq level (or (and (looking-at org-outline-regexp)
  11718. (- (match-end 0) (point) 1))
  11719. 1))
  11720. (cond
  11721. ((and (equal current "") (equal tags "")))
  11722. ((re-search-forward
  11723. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11724. (point-at-eol) t)
  11725. (if (equal tags "")
  11726. (setq rpl "")
  11727. (goto-char (match-beginning 0))
  11728. (setq c0 (current-column)
  11729. ;; compute offset for the case of org-indent-mode active
  11730. di (if org-indent-mode
  11731. (* (1- org-indent-indentation-per-level) (1- level))
  11732. 0)
  11733. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11734. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11735. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11736. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11737. (replace-match rpl t t)
  11738. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11739. tags)
  11740. (t (error "Tags alignment failed")))
  11741. (org-move-to-column col)
  11742. (unless just-align
  11743. (run-hooks 'org-after-tags-change-hook)))))
  11744. (defun org-change-tag-in-region (beg end tag off)
  11745. "Add or remove TAG for each entry in the region.
  11746. This works in the agenda, and also in an org-mode buffer."
  11747. (interactive
  11748. (list (region-beginning) (region-end)
  11749. (let ((org-last-tags-completion-table
  11750. (if (org-mode-p)
  11751. (org-get-buffer-tags)
  11752. (org-global-tags-completion-table))))
  11753. (org-icompleting-read
  11754. "Tag: " 'org-tags-completion-function nil nil nil
  11755. 'org-tags-history))
  11756. (progn
  11757. (message "[s]et or [r]emove? ")
  11758. (equal (read-char-exclusive) ?r))))
  11759. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11760. (let ((agendap (equal major-mode 'org-agenda-mode))
  11761. l1 l2 m buf pos newhead (cnt 0))
  11762. (goto-char end)
  11763. (setq l2 (1- (org-current-line)))
  11764. (goto-char beg)
  11765. (setq l1 (org-current-line))
  11766. (loop for l from l1 to l2 do
  11767. (org-goto-line l)
  11768. (setq m (get-text-property (point) 'org-hd-marker))
  11769. (when (or (and (org-mode-p) (org-on-heading-p))
  11770. (and agendap m))
  11771. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11772. pos (if agendap m (point)))
  11773. (with-current-buffer buf
  11774. (save-excursion
  11775. (save-restriction
  11776. (goto-char pos)
  11777. (setq cnt (1+ cnt))
  11778. (org-toggle-tag tag (if off 'off 'on))
  11779. (setq newhead (org-get-heading)))))
  11780. (and agendap (org-agenda-change-all-lines newhead m))))
  11781. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11782. (defun org-tags-completion-function (string predicate &optional flag)
  11783. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11784. (confirm (lambda (x) (stringp (car x)))))
  11785. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11786. (setq s1 (match-string 1 string)
  11787. s2 (match-string 2 string))
  11788. (setq s1 "" s2 string))
  11789. (cond
  11790. ((eq flag nil)
  11791. ;; try completion
  11792. (setq rtn (try-completion s2 ctable confirm))
  11793. (if (stringp rtn)
  11794. (setq rtn
  11795. (concat s1 s2 (substring rtn (length s2))
  11796. (if (and org-add-colon-after-tag-completion
  11797. (assoc rtn ctable))
  11798. ":" ""))))
  11799. rtn)
  11800. ((eq flag t)
  11801. ;; all-completions
  11802. (all-completions s2 ctable confirm)
  11803. )
  11804. ((eq flag 'lambda)
  11805. ;; exact match?
  11806. (assoc s2 ctable)))
  11807. ))
  11808. (defun org-fast-tag-insert (kwd tags face &optional end)
  11809. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11810. (insert (format "%-12s" (concat kwd ":"))
  11811. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11812. (or end "")))
  11813. (defun org-fast-tag-show-exit (flag)
  11814. (save-excursion
  11815. (org-goto-line 3)
  11816. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11817. (replace-match ""))
  11818. (when flag
  11819. (end-of-line 1)
  11820. (org-move-to-column (- (window-width) 19) t)
  11821. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11822. (defun org-set-current-tags-overlay (current prefix)
  11823. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11824. (if (featurep 'xemacs)
  11825. (org-overlay-display org-tags-overlay (concat prefix s)
  11826. 'secondary-selection)
  11827. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11828. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11829. (defvar org-last-tag-selection-key nil)
  11830. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11831. "Fast tag selection with single keys.
  11832. CURRENT is the current list of tags in the headline, INHERITED is the
  11833. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11834. possibly with grouping information. TODO-TABLE is a similar table with
  11835. TODO keywords, should these have keys assigned to them.
  11836. If the keys are nil, a-z are automatically assigned.
  11837. Returns the new tags string, or nil to not change the current settings."
  11838. (let* ((fulltable (append table todo-table))
  11839. (maxlen (apply 'max (mapcar
  11840. (lambda (x)
  11841. (if (stringp (car x)) (string-width (car x)) 0))
  11842. fulltable)))
  11843. (buf (current-buffer))
  11844. (expert (eq org-fast-tag-selection-single-key 'expert))
  11845. (buffer-tags nil)
  11846. (fwidth (+ maxlen 3 1 3))
  11847. (ncol (/ (- (window-width) 4) fwidth))
  11848. (i-face 'org-done)
  11849. (c-face 'org-todo)
  11850. tg cnt e c char c1 c2 ntable tbl rtn
  11851. ov-start ov-end ov-prefix
  11852. (exit-after-next org-fast-tag-selection-single-key)
  11853. (done-keywords org-done-keywords)
  11854. groups ingroup)
  11855. (save-excursion
  11856. (beginning-of-line 1)
  11857. (if (looking-at
  11858. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11859. (setq ov-start (match-beginning 1)
  11860. ov-end (match-end 1)
  11861. ov-prefix "")
  11862. (setq ov-start (1- (point-at-eol))
  11863. ov-end (1+ ov-start))
  11864. (skip-chars-forward "^\n\r")
  11865. (setq ov-prefix
  11866. (concat
  11867. (buffer-substring (1- (point)) (point))
  11868. (if (> (current-column) org-tags-column)
  11869. " "
  11870. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11871. (move-overlay org-tags-overlay ov-start ov-end)
  11872. (save-window-excursion
  11873. (if expert
  11874. (set-buffer (get-buffer-create " *Org tags*"))
  11875. (delete-other-windows)
  11876. (split-window-vertically)
  11877. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11878. (erase-buffer)
  11879. (org-set-local 'org-done-keywords done-keywords)
  11880. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11881. (org-fast-tag-insert "Current" current c-face "\n\n")
  11882. (org-fast-tag-show-exit exit-after-next)
  11883. (org-set-current-tags-overlay current ov-prefix)
  11884. (setq tbl fulltable char ?a cnt 0)
  11885. (while (setq e (pop tbl))
  11886. (cond
  11887. ((equal (car e) :startgroup)
  11888. (push '() groups) (setq ingroup t)
  11889. (when (not (= cnt 0))
  11890. (setq cnt 0)
  11891. (insert "\n"))
  11892. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11893. ((equal (car e) :endgroup)
  11894. (setq ingroup nil cnt 0)
  11895. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11896. ((equal e '(:newline))
  11897. (when (not (= cnt 0))
  11898. (setq cnt 0)
  11899. (insert "\n")
  11900. (setq e (car tbl))
  11901. (while (equal (car tbl) '(:newline))
  11902. (insert "\n")
  11903. (setq tbl (cdr tbl)))))
  11904. (t
  11905. (setq tg (copy-sequence (car e)) c2 nil)
  11906. (if (cdr e)
  11907. (setq c (cdr e))
  11908. ;; automatically assign a character.
  11909. (setq c1 (string-to-char
  11910. (downcase (substring
  11911. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11912. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11913. (while (or (rassoc char ntable) (rassoc char table))
  11914. (setq char (1+ char)))
  11915. (setq c2 c1))
  11916. (setq c (or c2 char)))
  11917. (if ingroup (push tg (car groups)))
  11918. (setq tg (org-add-props tg nil 'face
  11919. (cond
  11920. ((not (assoc tg table))
  11921. (org-get-todo-face tg))
  11922. ((member tg current) c-face)
  11923. ((member tg inherited) i-face)
  11924. (t nil))))
  11925. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11926. (insert "[" c "] " tg (make-string
  11927. (- fwidth 4 (length tg)) ?\ ))
  11928. (push (cons tg c) ntable)
  11929. (when (= (setq cnt (1+ cnt)) ncol)
  11930. (insert "\n")
  11931. (if ingroup (insert " "))
  11932. (setq cnt 0)))))
  11933. (setq ntable (nreverse ntable))
  11934. (insert "\n")
  11935. (goto-char (point-min))
  11936. (if (not expert) (org-fit-window-to-buffer))
  11937. (setq rtn
  11938. (catch 'exit
  11939. (while t
  11940. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11941. (if (not groups) "no " "")
  11942. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11943. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11944. (setq org-last-tag-selection-key c)
  11945. (cond
  11946. ((= c ?\r) (throw 'exit t))
  11947. ((= c ?!)
  11948. (setq groups (not groups))
  11949. (goto-char (point-min))
  11950. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11951. ((= c ?\C-c)
  11952. (if (not expert)
  11953. (org-fast-tag-show-exit
  11954. (setq exit-after-next (not exit-after-next)))
  11955. (setq expert nil)
  11956. (delete-other-windows)
  11957. (split-window-vertically)
  11958. (org-switch-to-buffer-other-window " *Org tags*")
  11959. (org-fit-window-to-buffer)))
  11960. ((or (= c ?\C-g)
  11961. (and (= c ?q) (not (rassoc c ntable))))
  11962. (org-detach-overlay org-tags-overlay)
  11963. (setq quit-flag t))
  11964. ((= c ?\ )
  11965. (setq current nil)
  11966. (if exit-after-next (setq exit-after-next 'now)))
  11967. ((= c ?\t)
  11968. (condition-case nil
  11969. (setq tg (org-icompleting-read
  11970. "Tag: "
  11971. (or buffer-tags
  11972. (with-current-buffer buf
  11973. (org-get-buffer-tags)))))
  11974. (quit (setq tg "")))
  11975. (when (string-match "\\S-" tg)
  11976. (add-to-list 'buffer-tags (list tg))
  11977. (if (member tg current)
  11978. (setq current (delete tg current))
  11979. (push tg current)))
  11980. (if exit-after-next (setq exit-after-next 'now)))
  11981. ((setq e (rassoc c todo-table) tg (car e))
  11982. (with-current-buffer buf
  11983. (save-excursion (org-todo tg)))
  11984. (if exit-after-next (setq exit-after-next 'now)))
  11985. ((setq e (rassoc c ntable) tg (car e))
  11986. (if (member tg current)
  11987. (setq current (delete tg current))
  11988. (loop for g in groups do
  11989. (if (member tg g)
  11990. (mapc (lambda (x)
  11991. (setq current (delete x current)))
  11992. g)))
  11993. (push tg current))
  11994. (if exit-after-next (setq exit-after-next 'now))))
  11995. ;; Create a sorted list
  11996. (setq current
  11997. (sort current
  11998. (lambda (a b)
  11999. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12000. (if (eq exit-after-next 'now) (throw 'exit t))
  12001. (goto-char (point-min))
  12002. (beginning-of-line 2)
  12003. (delete-region (point) (point-at-eol))
  12004. (org-fast-tag-insert "Current" current c-face)
  12005. (org-set-current-tags-overlay current ov-prefix)
  12006. (while (re-search-forward
  12007. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12008. (setq tg (match-string 1))
  12009. (add-text-properties
  12010. (match-beginning 1) (match-end 1)
  12011. (list 'face
  12012. (cond
  12013. ((member tg current) c-face)
  12014. ((member tg inherited) i-face)
  12015. (t (get-text-property (match-beginning 1) 'face))))))
  12016. (goto-char (point-min)))))
  12017. (org-detach-overlay org-tags-overlay)
  12018. (if rtn
  12019. (mapconcat 'identity current ":")
  12020. nil))))
  12021. (defun org-get-tags-string ()
  12022. "Get the TAGS string in the current headline."
  12023. (unless (org-on-heading-p t)
  12024. (error "Not on a heading"))
  12025. (save-excursion
  12026. (beginning-of-line 1)
  12027. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12028. (org-match-string-no-properties 1)
  12029. "")))
  12030. (defun org-get-tags ()
  12031. "Get the list of tags specified in the current headline."
  12032. (org-split-string (org-get-tags-string) ":"))
  12033. (defun org-get-buffer-tags ()
  12034. "Get a table of all tags used in the buffer, for completion."
  12035. (let (tags)
  12036. (save-excursion
  12037. (goto-char (point-min))
  12038. (while (re-search-forward
  12039. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12040. (when (equal (char-after (point-at-bol 0)) ?*)
  12041. (mapc (lambda (x) (add-to-list 'tags x))
  12042. (org-split-string (org-match-string-no-properties 1) ":")))))
  12043. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12044. (mapcar 'list tags)))
  12045. ;;;; The mapping API
  12046. ;;;###autoload
  12047. (defun org-map-entries (func &optional match scope &rest skip)
  12048. "Call FUNC at each headline selected by MATCH in SCOPE.
  12049. FUNC is a function or a lisp form. The function will be called without
  12050. arguments, with the cursor positioned at the beginning of the headline.
  12051. The return values of all calls to the function will be collected and
  12052. returned as a list.
  12053. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12054. does not need to preserve point. After evaluation, the cursor will be
  12055. moved to the end of the line (presumably of the headline of the
  12056. processed entry) and search continues from there. Under some
  12057. circumstances, this may not produce the wanted results. For example,
  12058. if you have removed (e.g. archived) the current (sub)tree it could
  12059. mean that the next entry will be skipped entirely. In such cases, you
  12060. can specify the position from where search should continue by making
  12061. FUNC set the variable `org-map-continue-from' to the desired buffer
  12062. position.
  12063. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12064. Only headlines that are matched by this query will be considered during
  12065. the iteration. When MATCH is nil or t, all headlines will be
  12066. visited by the iteration.
  12067. SCOPE determines the scope of this command. It can be any of:
  12068. nil The current buffer, respecting the restriction if any
  12069. tree The subtree started with the entry at point
  12070. file The current buffer, without restriction
  12071. file-with-archives
  12072. The current buffer, and any archives associated with it
  12073. agenda All agenda files
  12074. agenda-with-archives
  12075. All agenda files with any archive files associated with them
  12076. \(file1 file2 ...)
  12077. If this is a list, all files in the list will be scanned
  12078. The remaining args are treated as settings for the skipping facilities of
  12079. the scanner. The following items can be given here:
  12080. archive skip trees with the archive tag.
  12081. comment skip trees with the COMMENT keyword
  12082. function or Emacs Lisp form:
  12083. will be used as value for `org-agenda-skip-function', so whenever
  12084. the function returns t, FUNC will not be called for that
  12085. entry and search will continue from the point where the
  12086. function leaves it.
  12087. If your function needs to retrieve the tags including inherited tags
  12088. at the *current* entry, you can use the value of the variable
  12089. `org-scanner-tags' which will be much faster than getting the value
  12090. with `org-get-tags-at'. If your function gets properties with
  12091. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12092. to t around the call to `org-entry-properties' to get the same speedup.
  12093. Note that if your function moves around to retrieve tags and properties at
  12094. a *different* entry, you cannot use these techniques."
  12095. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12096. (org-agenda-skip-archived-trees (memq 'archive skip))
  12097. (org-agenda-skip-comment-trees (memq 'comment skip))
  12098. (org-agenda-skip-function
  12099. (car (org-delete-all '(comment archive) skip)))
  12100. (org-tags-match-list-sublevels t)
  12101. matcher file res
  12102. org-todo-keywords-for-agenda
  12103. org-done-keywords-for-agenda
  12104. org-todo-keyword-alist-for-agenda
  12105. org-drawers-for-agenda
  12106. org-tag-alist-for-agenda)
  12107. (cond
  12108. ((eq match t) (setq matcher t))
  12109. ((eq match nil) (setq matcher t))
  12110. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12111. (save-excursion
  12112. (save-restriction
  12113. (when (eq scope 'tree)
  12114. (org-back-to-heading t)
  12115. (org-narrow-to-subtree)
  12116. (setq scope nil))
  12117. (if (not scope)
  12118. (progn
  12119. (org-prepare-agenda-buffers
  12120. (list (buffer-file-name (current-buffer))))
  12121. (setq res (org-scan-tags func matcher)))
  12122. ;; Get the right scope
  12123. (cond
  12124. ((and scope (listp scope) (symbolp (car scope)))
  12125. (setq scope (eval scope)))
  12126. ((eq scope 'agenda)
  12127. (setq scope (org-agenda-files t)))
  12128. ((eq scope 'agenda-with-archives)
  12129. (setq scope (org-agenda-files t))
  12130. (setq scope (org-add-archive-files scope)))
  12131. ((eq scope 'file)
  12132. (setq scope (list (buffer-file-name))))
  12133. ((eq scope 'file-with-archives)
  12134. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12135. (org-prepare-agenda-buffers scope)
  12136. (while (setq file (pop scope))
  12137. (with-current-buffer (org-find-base-buffer-visiting file)
  12138. (save-excursion
  12139. (save-restriction
  12140. (widen)
  12141. (goto-char (point-min))
  12142. (setq res (append res (org-scan-tags func matcher))))))))))
  12143. res))
  12144. ;;;; Properties
  12145. ;;; Setting and retrieving properties
  12146. (defconst org-special-properties
  12147. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12148. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  12149. "The special properties valid in Org-mode.
  12150. These are properties that are not defined in the property drawer,
  12151. but in some other way.")
  12152. (defconst org-default-properties
  12153. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12154. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12155. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12156. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12157. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12158. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12159. "Some properties that are used by Org-mode for various purposes.
  12160. Being in this list makes sure that they are offered for completion.")
  12161. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12162. "Regular expression matching the first line of a property drawer.")
  12163. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12164. "Regular expression matching the last line of a property drawer.")
  12165. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12166. "Regular expression matching the first line of a property drawer.")
  12167. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12168. "Regular expression matching the first line of a property drawer.")
  12169. (defconst org-property-drawer-re
  12170. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12171. org-property-end-re "\\)\n?")
  12172. "Matches an entire property drawer.")
  12173. (defconst org-clock-drawer-re
  12174. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12175. org-property-end-re "\\)\n?")
  12176. "Matches an entire clock drawer.")
  12177. (defun org-property-action ()
  12178. "Do an action on properties."
  12179. (interactive)
  12180. (let (c)
  12181. (org-at-property-p)
  12182. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12183. (setq c (read-char-exclusive))
  12184. (cond
  12185. ((equal c ?s)
  12186. (call-interactively 'org-set-property))
  12187. ((equal c ?d)
  12188. (call-interactively 'org-delete-property))
  12189. ((equal c ?D)
  12190. (call-interactively 'org-delete-property-globally))
  12191. ((equal c ?c)
  12192. (call-interactively 'org-compute-property-at-point))
  12193. (t (error "No such property action %c" c)))))
  12194. (defun org-set-effort (&optional value)
  12195. "Set the effort property of the current entry.
  12196. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12197. allowed value."
  12198. (interactive "P")
  12199. (if (equal value 0) (setq value 10))
  12200. (let* ((completion-ignore-case t)
  12201. (prop org-effort-property)
  12202. (cur (org-entry-get nil prop))
  12203. (allowed (org-property-get-allowed-values nil prop 'table))
  12204. (existing (mapcar 'list (org-property-values prop)))
  12205. rpl
  12206. (val (cond
  12207. ((stringp value) value)
  12208. ((and allowed (integerp value))
  12209. (or (car (nth (1- value) allowed))
  12210. (car (org-last allowed))))
  12211. (allowed
  12212. (message "Select 1-9,0, [RET%s]: %s"
  12213. (if cur (concat "=" cur) "")
  12214. (mapconcat 'car allowed " "))
  12215. (setq rpl (read-char-exclusive))
  12216. (if (equal rpl ?\r)
  12217. cur
  12218. (setq rpl (- rpl ?0))
  12219. (if (equal rpl 0) (setq rpl 10))
  12220. (if (and (> rpl 0) (<= rpl (length allowed)))
  12221. (car (nth (1- rpl) allowed))
  12222. (org-completing-read "Effort: " allowed nil))))
  12223. (t
  12224. (let (org-completion-use-ido org-completion-use-iswitchb)
  12225. (org-completing-read
  12226. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12227. (concat "[" cur "]") "")
  12228. ": ")
  12229. existing nil nil "" nil cur))))))
  12230. (unless (equal (org-entry-get nil prop) val)
  12231. (org-entry-put nil prop val))
  12232. (message "%s is now %s" prop val)))
  12233. (defun org-at-property-p ()
  12234. "Is cursor inside a property drawer?"
  12235. (save-excursion
  12236. (beginning-of-line 1)
  12237. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12238. (save-match-data ;; Used by calling procedures
  12239. (let ((p (point))
  12240. (range (unless (org-before-first-heading-p)
  12241. (org-get-property-block))))
  12242. (and range (<= (car range) p) (< p (cdr range))))))))
  12243. (defun org-get-property-block (&optional beg end force)
  12244. "Return the (beg . end) range of the body of the property drawer.
  12245. BEG and END can be beginning and end of subtree, if not given
  12246. they will be found.
  12247. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12248. (catch 'exit
  12249. (save-excursion
  12250. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12251. (end (or end (progn (outline-next-heading) (point)))))
  12252. (goto-char beg)
  12253. (if (re-search-forward org-property-start-re end t)
  12254. (setq beg (1+ (match-end 0)))
  12255. (if force
  12256. (save-excursion
  12257. (org-insert-property-drawer)
  12258. (setq end (progn (outline-next-heading) (point))))
  12259. (throw 'exit nil))
  12260. (goto-char beg)
  12261. (if (re-search-forward org-property-start-re end t)
  12262. (setq beg (1+ (match-end 0)))))
  12263. (if (re-search-forward org-property-end-re end t)
  12264. (setq end (match-beginning 0))
  12265. (or force (throw 'exit nil))
  12266. (goto-char beg)
  12267. (setq end beg)
  12268. (org-indent-line-function)
  12269. (insert ":END:\n"))
  12270. (cons beg end)))))
  12271. (defun org-entry-properties (&optional pom which specific)
  12272. "Get all properties of the entry at point-or-marker POM.
  12273. This includes the TODO keyword, the tags, time strings for deadline,
  12274. scheduled, and clocking, and any additional properties defined in the
  12275. entry. The return value is an alist, keys may occur multiple times
  12276. if the property key was used several times.
  12277. POM may also be nil, in which case the current entry is used.
  12278. If WHICH is nil or `all', get all properties. If WHICH is
  12279. `special' or `standard', only get that subclass. If WHICH
  12280. is a string only get exactly this property. Specific can be a string, the
  12281. specific property we are interested in. Specifying it can speed
  12282. things up because then unnecessary parsing is avoided."
  12283. (setq which (or which 'all))
  12284. (org-with-point-at pom
  12285. (let ((clockstr (substring org-clock-string 0 -1))
  12286. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12287. (case-fold-search nil)
  12288. beg end range props sum-props key key1 value string clocksum)
  12289. (save-excursion
  12290. (when (condition-case nil
  12291. (and (org-mode-p) (org-back-to-heading t))
  12292. (error nil))
  12293. (setq beg (point))
  12294. (setq sum-props (get-text-property (point) 'org-summaries))
  12295. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12296. (outline-next-heading)
  12297. (setq end (point))
  12298. (when (memq which '(all special))
  12299. ;; Get the special properties, like TODO and tags
  12300. (goto-char beg)
  12301. (when (and (or (not specific) (string= specific "TODO"))
  12302. (looking-at org-todo-line-regexp) (match-end 2))
  12303. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12304. (when (and (or (not specific) (string= specific "PRIORITY"))
  12305. (looking-at org-priority-regexp))
  12306. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12307. (when (and (or (not specific) (string= specific "TAGS"))
  12308. (setq value (org-get-tags-string))
  12309. (string-match "\\S-" value))
  12310. (push (cons "TAGS" value) props))
  12311. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12312. (setq value (org-get-tags-at)))
  12313. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12314. ":"))
  12315. props))
  12316. (when (or (not specific) (string= specific "BLOCKED"))
  12317. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12318. (when (or (not specific)
  12319. (member specific
  12320. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12321. "TIMESTAMP" "TIMESTAMP_IA")))
  12322. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12323. (setq key (if (match-end 1)
  12324. (substring (org-match-string-no-properties 1)
  12325. 0 -1))
  12326. string (if (equal key clockstr)
  12327. (org-no-properties
  12328. (org-trim
  12329. (buffer-substring
  12330. (match-beginning 3) (goto-char
  12331. (point-at-eol)))))
  12332. (substring (org-match-string-no-properties 3)
  12333. 1 -1)))
  12334. ;; Get the correct property name from the key. This is
  12335. ;; necessary if the user has configured time keywords.
  12336. (setq key1 (concat key ":"))
  12337. (cond
  12338. ((not key)
  12339. (setq key
  12340. (if (= (char-after (match-beginning 3)) ?\[)
  12341. "TIMESTAMP_IA" "TIMESTAMP")))
  12342. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12343. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12344. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12345. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12346. (when (or (equal key "CLOCK") (not (assoc key props)))
  12347. (push (cons key string) props))))
  12348. )
  12349. (when (memq which '(all standard))
  12350. ;; Get the standard properties, like :PROP: ...
  12351. (setq range (org-get-property-block beg end))
  12352. (when range
  12353. (goto-char (car range))
  12354. (while (re-search-forward
  12355. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12356. (cdr range) t)
  12357. (setq key (org-match-string-no-properties 1)
  12358. value (org-trim (or (org-match-string-no-properties 2) "")))
  12359. (unless (member key excluded)
  12360. (push (cons key (or value "")) props)))))
  12361. (if clocksum
  12362. (push (cons "CLOCKSUM"
  12363. (org-columns-number-to-string (/ (float clocksum) 60.)
  12364. 'add_times))
  12365. props))
  12366. (unless (assoc "CATEGORY" props)
  12367. (setq value (or (org-get-category)
  12368. (progn (org-refresh-category-properties)
  12369. (org-get-category))))
  12370. (push (cons "CATEGORY" value) props))
  12371. (append sum-props (nreverse props)))))))
  12372. (defun org-entry-get (pom property &optional inherit literal-nil)
  12373. "Get value of PROPERTY for entry at point-or-marker POM.
  12374. If INHERIT is non-nil and the entry does not have the property,
  12375. then also check higher levels of the hierarchy.
  12376. If INHERIT is the symbol `selective', use inheritance only if the setting
  12377. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12378. If the property is present but empty, the return value is the empty string.
  12379. If the property is not present at all, nil is returned.
  12380. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12381. do not interpret it as the list atom nil. This is used for inheritance
  12382. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12383. (org-with-point-at pom
  12384. (if (and inherit (if (eq inherit 'selective)
  12385. (org-property-inherit-p property)
  12386. t))
  12387. (org-entry-get-with-inheritance property literal-nil)
  12388. (if (member property org-special-properties)
  12389. ;; We need a special property. Use `org-entry-properties' to
  12390. ;; retrieve it, but specify the wanted property
  12391. (cdr (assoc property (org-entry-properties nil 'special property)))
  12392. (let ((range (org-get-property-block)))
  12393. (if (and range
  12394. (goto-char (car range))
  12395. (re-search-forward
  12396. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12397. (cdr range) t))
  12398. ;; Found the property, return it.
  12399. (if (match-end 1)
  12400. (if literal-nil
  12401. (org-match-string-no-properties 1)
  12402. (org-not-nil (org-match-string-no-properties 1)))
  12403. "")))))))
  12404. (defun org-property-or-variable-value (var &optional inherit)
  12405. "Check if there is a property fixing the value of VAR.
  12406. If yes, return this value. If not, return the current value of the variable."
  12407. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12408. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12409. (read prop)
  12410. (symbol-value var))))
  12411. (defun org-entry-delete (pom property)
  12412. "Delete the property PROPERTY from entry at point-or-marker POM."
  12413. (org-with-point-at pom
  12414. (if (member property org-special-properties)
  12415. nil ; cannot delete these properties.
  12416. (let ((range (org-get-property-block)))
  12417. (if (and range
  12418. (goto-char (car range))
  12419. (re-search-forward
  12420. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12421. (cdr range) t))
  12422. (progn
  12423. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12424. t)
  12425. nil)))))
  12426. ;; Multi-values properties are properties that contain multiple values
  12427. ;; These values are assumed to be single words, separated by whitespace.
  12428. (defun org-entry-add-to-multivalued-property (pom property value)
  12429. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12430. (let* ((old (org-entry-get pom property))
  12431. (values (and old (org-split-string old "[ \t]"))))
  12432. (setq value (org-entry-protect-space value))
  12433. (unless (member value values)
  12434. (setq values (cons value values))
  12435. (org-entry-put pom property
  12436. (mapconcat 'identity values " ")))))
  12437. (defun org-entry-remove-from-multivalued-property (pom property value)
  12438. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12439. (let* ((old (org-entry-get pom property))
  12440. (values (and old (org-split-string old "[ \t]"))))
  12441. (setq value (org-entry-protect-space value))
  12442. (when (member value values)
  12443. (setq values (delete value values))
  12444. (org-entry-put pom property
  12445. (mapconcat 'identity values " ")))))
  12446. (defun org-entry-member-in-multivalued-property (pom property value)
  12447. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12448. (let* ((old (org-entry-get pom property))
  12449. (values (and old (org-split-string old "[ \t]"))))
  12450. (setq value (org-entry-protect-space value))
  12451. (member value values)))
  12452. (defun org-entry-get-multivalued-property (pom property)
  12453. "Return a list of values in a multivalued property."
  12454. (let* ((value (org-entry-get pom property))
  12455. (values (and value (org-split-string value "[ \t]"))))
  12456. (mapcar 'org-entry-restore-space values)))
  12457. (defun org-entry-put-multivalued-property (pom property &rest values)
  12458. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12459. VALUES should be a list of strings. Spaces will be protected."
  12460. (org-entry-put pom property
  12461. (mapconcat 'org-entry-protect-space values " "))
  12462. (let* ((value (org-entry-get pom property))
  12463. (values (and value (org-split-string value "[ \t]"))))
  12464. (mapcar 'org-entry-restore-space values)))
  12465. (defun org-entry-protect-space (s)
  12466. "Protect spaces and newline in string S."
  12467. (while (string-match " " s)
  12468. (setq s (replace-match "%20" t t s)))
  12469. (while (string-match "\n" s)
  12470. (setq s (replace-match "%0A" t t s)))
  12471. s)
  12472. (defun org-entry-restore-space (s)
  12473. "Restore spaces and newline in string S."
  12474. (while (string-match "%20" s)
  12475. (setq s (replace-match " " t t s)))
  12476. (while (string-match "%0A" s)
  12477. (setq s (replace-match "\n" t t s)))
  12478. s)
  12479. (defvar org-entry-property-inherited-from (make-marker)
  12480. "Marker pointing to the entry from where a property was inherited.
  12481. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12482. location of the entry where the inheritance search matched. If there was
  12483. no match, the marker will point nowhere.
  12484. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12485. is set.")
  12486. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12487. "Get entry property, and search higher levels if not present.
  12488. The search will stop at the first ancestor which has the property defined.
  12489. If the value found is \"nil\", return nil to show that the property
  12490. should be considered as undefined (this is the meaning of nil here).
  12491. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12492. (move-marker org-entry-property-inherited-from nil)
  12493. (let (tmp)
  12494. (save-excursion
  12495. (save-restriction
  12496. (widen)
  12497. (catch 'ex
  12498. (while t
  12499. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12500. (org-back-to-heading t)
  12501. (move-marker org-entry-property-inherited-from (point))
  12502. (throw 'ex tmp))
  12503. (or (org-up-heading-safe) (throw 'ex nil)))))
  12504. (setq tmp (or tmp
  12505. (cdr (assoc property org-file-properties))
  12506. (cdr (assoc property org-global-properties))
  12507. (cdr (assoc property org-global-properties-fixed))))
  12508. (if literal-nil tmp (org-not-nil tmp)))))
  12509. (defvar org-property-changed-functions nil
  12510. "Hook called when the value of a property has changed.
  12511. Each hook function should accept two arguments, the name of the property
  12512. and the new value.")
  12513. (defun org-entry-put (pom property value)
  12514. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12515. (org-with-point-at pom
  12516. (org-back-to-heading t)
  12517. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12518. range)
  12519. (cond
  12520. ((equal property "TODO")
  12521. (when (and (stringp value) (string-match "\\S-" value)
  12522. (not (member value org-todo-keywords-1)))
  12523. (error "\"%s\" is not a valid TODO state" value))
  12524. (if (or (not value)
  12525. (not (string-match "\\S-" value)))
  12526. (setq value 'none))
  12527. (org-todo value)
  12528. (org-set-tags nil 'align))
  12529. ((equal property "PRIORITY")
  12530. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12531. (string-to-char value) ?\ ))
  12532. (org-set-tags nil 'align))
  12533. ((equal property "SCHEDULED")
  12534. (if (re-search-forward org-scheduled-time-regexp end t)
  12535. (cond
  12536. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12537. ((eq value 'later) (org-timestamp-change 1 'day))
  12538. (t (call-interactively 'org-schedule)))
  12539. (call-interactively 'org-schedule)))
  12540. ((equal property "DEADLINE")
  12541. (if (re-search-forward org-deadline-time-regexp end t)
  12542. (cond
  12543. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12544. ((eq value 'later) (org-timestamp-change 1 'day))
  12545. (t (call-interactively 'org-deadline)))
  12546. (call-interactively 'org-deadline)))
  12547. ((member property org-special-properties)
  12548. (error "The %s property can not yet be set with `org-entry-put'"
  12549. property))
  12550. (t ; a non-special property
  12551. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12552. (setq range (org-get-property-block beg end 'force))
  12553. (goto-char (car range))
  12554. (if (re-search-forward
  12555. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12556. (progn
  12557. (delete-region (match-beginning 1) (match-end 1))
  12558. (goto-char (match-beginning 1)))
  12559. (goto-char (cdr range))
  12560. (insert "\n")
  12561. (backward-char 1)
  12562. (org-indent-line-function)
  12563. (insert ":" property ":"))
  12564. (and value (insert " " value))
  12565. (org-indent-line-function)))))
  12566. (run-hook-with-args 'org-property-changed-functions property value)))
  12567. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12568. "Get all property keys in the current buffer.
  12569. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12570. like tags and TODO state.
  12571. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12572. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12573. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12574. formats in the current buffer."
  12575. (let (rtn range cfmt s p)
  12576. (save-excursion
  12577. (save-restriction
  12578. (widen)
  12579. (goto-char (point-min))
  12580. (while (re-search-forward org-property-start-re nil t)
  12581. (setq range (org-get-property-block))
  12582. (goto-char (car range))
  12583. (while (re-search-forward
  12584. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12585. (cdr range) t)
  12586. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12587. (outline-next-heading))))
  12588. (when include-specials
  12589. (setq rtn (append org-special-properties rtn)))
  12590. (when include-defaults
  12591. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12592. (add-to-list 'rtn org-effort-property))
  12593. (when include-columns
  12594. (save-excursion
  12595. (save-restriction
  12596. (widen)
  12597. (goto-char (point-min))
  12598. (while (re-search-forward
  12599. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12600. nil t)
  12601. (setq cfmt (match-string 2) s 0)
  12602. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12603. cfmt s)
  12604. (setq s (match-end 0)
  12605. p (match-string 1 cfmt))
  12606. (unless (or (equal p "ITEM")
  12607. (member p org-special-properties))
  12608. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12609. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12610. (defun org-property-values (key)
  12611. "Return a list of all values of property KEY."
  12612. (save-excursion
  12613. (save-restriction
  12614. (widen)
  12615. (goto-char (point-min))
  12616. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12617. values)
  12618. (while (re-search-forward re nil t)
  12619. (add-to-list 'values (org-trim (match-string 1))))
  12620. (delete "" values)))))
  12621. (defun org-insert-property-drawer ()
  12622. "Insert a property drawer into the current entry."
  12623. (interactive)
  12624. (org-back-to-heading t)
  12625. (looking-at outline-regexp)
  12626. (let ((indent (if org-adapt-indentation
  12627. (- (match-end 0)(match-beginning 0))
  12628. 0))
  12629. (beg (point))
  12630. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12631. end hiddenp)
  12632. (outline-next-heading)
  12633. (setq end (point))
  12634. (goto-char beg)
  12635. (while (re-search-forward re end t))
  12636. (setq hiddenp (org-invisible-p))
  12637. (end-of-line 1)
  12638. (and (equal (char-after) ?\n) (forward-char 1))
  12639. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12640. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12641. ;; just skip this line
  12642. (beginning-of-line 2)
  12643. ;; Drawer start, find the end
  12644. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12645. (beginning-of-line 1)))
  12646. (org-skip-over-state-notes)
  12647. (skip-chars-backward " \t\n\r")
  12648. (if (eq (char-before) ?*) (forward-char 1))
  12649. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12650. (beginning-of-line 0)
  12651. (org-indent-to-column indent)
  12652. (beginning-of-line 2)
  12653. (org-indent-to-column indent)
  12654. (beginning-of-line 0)
  12655. (if hiddenp
  12656. (save-excursion
  12657. (org-back-to-heading t)
  12658. (hide-entry))
  12659. (org-flag-drawer t))))
  12660. (defun org-set-property (property value)
  12661. "In the current entry, set PROPERTY to VALUE.
  12662. When called interactively, this will prompt for a property name, offering
  12663. completion on existing and default properties. And then it will prompt
  12664. for a value, offering completion either on allowed values (via an inherited
  12665. xxx_ALL property) or on existing values in other instances of this property
  12666. in the current file."
  12667. (interactive
  12668. (let* ((completion-ignore-case t)
  12669. (keys (org-buffer-property-keys nil t t))
  12670. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12671. (prop (if (member prop0 keys)
  12672. prop0
  12673. (or (cdr (assoc (downcase prop0)
  12674. (mapcar (lambda (x) (cons (downcase x) x))
  12675. keys)))
  12676. prop0)))
  12677. (cur (org-entry-get nil prop))
  12678. (prompt (concat prop " value"
  12679. (if (and cur (string-match "\\S-" cur))
  12680. (concat " [" cur "]") "") ": "))
  12681. (allowed (org-property-get-allowed-values nil prop 'table))
  12682. (existing (mapcar 'list (org-property-values prop)))
  12683. (val (if allowed
  12684. (org-completing-read prompt allowed nil
  12685. (not (get-text-property 0 'org-unrestricted
  12686. (caar allowed))))
  12687. (let (org-completion-use-ido org-completion-use-iswitchb)
  12688. (org-completing-read prompt existing nil nil "" nil cur)))))
  12689. (list prop (if (equal val "") cur val))))
  12690. (unless (equal (org-entry-get nil property) value)
  12691. (org-entry-put nil property value)))
  12692. (defun org-delete-property (property)
  12693. "In the current entry, delete PROPERTY."
  12694. (interactive
  12695. (let* ((completion-ignore-case t)
  12696. (prop (org-icompleting-read "Property: "
  12697. (org-entry-properties nil 'standard))))
  12698. (list prop)))
  12699. (message "Property %s %s" property
  12700. (if (org-entry-delete nil property)
  12701. "deleted"
  12702. "was not present in the entry")))
  12703. (defun org-delete-property-globally (property)
  12704. "Remove PROPERTY globally, from all entries."
  12705. (interactive
  12706. (let* ((completion-ignore-case t)
  12707. (prop (org-icompleting-read
  12708. "Globally remove property: "
  12709. (mapcar 'list (org-buffer-property-keys)))))
  12710. (list prop)))
  12711. (save-excursion
  12712. (save-restriction
  12713. (widen)
  12714. (goto-char (point-min))
  12715. (let ((cnt 0))
  12716. (while (re-search-forward
  12717. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12718. nil t)
  12719. (setq cnt (1+ cnt))
  12720. (replace-match ""))
  12721. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12722. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12723. (defun org-compute-property-at-point ()
  12724. "Compute the property at point.
  12725. This looks for an enclosing column format, extracts the operator and
  12726. then applies it to the property in the column format's scope."
  12727. (interactive)
  12728. (unless (org-at-property-p)
  12729. (error "Not at a property"))
  12730. (let ((prop (org-match-string-no-properties 2)))
  12731. (org-columns-get-format-and-top-level)
  12732. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12733. (error "No operator defined for property %s" prop))
  12734. (org-columns-compute prop)))
  12735. (defvar org-property-allowed-value-functions nil
  12736. "Hook for functions supplying allowed values for a specific property.
  12737. The functions must take a single argument, the name of the property, and
  12738. return a flat list of allowed values. If \":ETC\" is one of
  12739. the values, this means that these values are intended as defaults for
  12740. completion, but that other values should be allowed too.
  12741. The functions must return nil if they are not responsible for this
  12742. property.")
  12743. (defun org-property-get-allowed-values (pom property &optional table)
  12744. "Get allowed values for the property PROPERTY.
  12745. When TABLE is non-nil, return an alist that can directly be used for
  12746. completion."
  12747. (let (vals)
  12748. (cond
  12749. ((equal property "TODO")
  12750. (setq vals (org-with-point-at pom
  12751. (append org-todo-keywords-1 '("")))))
  12752. ((equal property "PRIORITY")
  12753. (let ((n org-lowest-priority))
  12754. (while (>= n org-highest-priority)
  12755. (push (char-to-string n) vals)
  12756. (setq n (1- n)))))
  12757. ((member property org-special-properties))
  12758. ((setq vals (run-hook-with-args-until-success
  12759. 'org-property-allowed-value-functions property)))
  12760. (t
  12761. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12762. (when (and vals (string-match "\\S-" vals))
  12763. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12764. (setq vals (mapcar (lambda (x)
  12765. (cond ((stringp x) x)
  12766. ((numberp x) (number-to-string x))
  12767. ((symbolp x) (symbol-name x))
  12768. (t "???")))
  12769. vals)))))
  12770. (when (member ":ETC" vals)
  12771. (setq vals (remove ":ETC" vals))
  12772. (org-add-props (car vals) '(org-unrestricted t)))
  12773. (if table (mapcar 'list vals) vals)))
  12774. (defun org-property-previous-allowed-value (&optional previous)
  12775. "Switch to the next allowed value for this property."
  12776. (interactive)
  12777. (org-property-next-allowed-value t))
  12778. (defun org-property-next-allowed-value (&optional previous)
  12779. "Switch to the next allowed value for this property."
  12780. (interactive)
  12781. (unless (org-at-property-p)
  12782. (error "Not at a property"))
  12783. (let* ((key (match-string 2))
  12784. (value (match-string 3))
  12785. (allowed (or (org-property-get-allowed-values (point) key)
  12786. (and (member value '("[ ]" "[-]" "[X]"))
  12787. '("[ ]" "[X]"))))
  12788. nval)
  12789. (unless allowed
  12790. (error "Allowed values for this property have not been defined"))
  12791. (if previous (setq allowed (reverse allowed)))
  12792. (if (member value allowed)
  12793. (setq nval (car (cdr (member value allowed)))))
  12794. (setq nval (or nval (car allowed)))
  12795. (if (equal nval value)
  12796. (error "Only one allowed value for this property"))
  12797. (org-at-property-p)
  12798. (replace-match (concat " :" key ": " nval) t t)
  12799. (org-indent-line-function)
  12800. (beginning-of-line 1)
  12801. (skip-chars-forward " \t")
  12802. (run-hook-with-args 'org-property-changed-functions key nval)))
  12803. (defun org-find-olp (path &optional this-buffer)
  12804. "Return a marker pointing to the entry at outline path OLP.
  12805. If anything goes wrong, throw an error.
  12806. You can wrap this call to catch the error like this:
  12807. (condition-case msg
  12808. (org-mobile-locate-entry (match-string 4))
  12809. (error (nth 1 msg)))
  12810. The return value will then be either a string with the error message,
  12811. or a marker if everything is OK.
  12812. If THIS-BUFFER is set, the outline path does not contain a file,
  12813. only headings."
  12814. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12815. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12816. (level 1)
  12817. (lmin 1)
  12818. (lmax 1)
  12819. limit re end found pos heading cnt)
  12820. (unless buffer (error "File not found :%s" file))
  12821. (with-current-buffer buffer
  12822. (save-excursion
  12823. (save-restriction
  12824. (widen)
  12825. (setq limit (point-max))
  12826. (goto-char (point-min))
  12827. (while (setq heading (pop path))
  12828. (setq re (format org-complex-heading-regexp-format
  12829. (regexp-quote heading)))
  12830. (setq cnt 0 pos (point))
  12831. (while (re-search-forward re end t)
  12832. (setq level (- (match-end 1) (match-beginning 1)))
  12833. (if (and (>= level lmin) (<= level lmax))
  12834. (setq found (match-beginning 0) cnt (1+ cnt))))
  12835. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12836. lmax heading))
  12837. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12838. lmax heading))
  12839. (goto-char found)
  12840. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12841. (setq end (save-excursion (org-end-of-subtree t t))))
  12842. (when (org-on-heading-p)
  12843. (move-marker (make-marker) (point))))))))
  12844. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  12845. "Find node HEADING in BUFFER.
  12846. Return a marker to the heading if it was found, or nil if not.
  12847. If POS-ONLY is set, return just the position instead of a marker.
  12848. The heading text must match exact, but it may have a TODO keyword,
  12849. a priority cookie and tags in the standard locations."
  12850. (with-current-buffer (or buffer (current-buffer))
  12851. (save-excursion
  12852. (save-restriction
  12853. (widen)
  12854. (goto-char (point-min))
  12855. (let (case-fold-search)
  12856. (if (re-search-forward
  12857. (format org-complex-heading-regexp-format
  12858. (regexp-quote heading)) nil t)
  12859. (if pos-only
  12860. (match-beginning 0)
  12861. (move-marker (make-marker) (match-beginning 0)))))))))
  12862. (defun org-find-exact-heading-in-directory (heading &optional dir)
  12863. "Find Org node headline HEADING in all .org files in directory DIR.
  12864. When the target headline is found, return a marker to this location."
  12865. (let ((files (directory-files (or dir default-directory)
  12866. nil "\\`[^.#].*\\.org\\'"))
  12867. file visiting m buffer)
  12868. (catch 'found
  12869. (while (setq file (pop files))
  12870. (message "trying %s" file)
  12871. (setq visiting (org-find-base-buffer-visiting file))
  12872. (setq buffer (or visiting (find-file-noselect file)))
  12873. (setq m (org-find-exact-headline-in-buffer
  12874. heading buffer))
  12875. (when (and (not m) (not visiting)) (kill-buffer buffer))
  12876. (and m (throw 'found m))))))
  12877. (defun org-find-entry-with-id (ident)
  12878. "Locate the entry that contains the ID property with exact value IDENT.
  12879. IDENT can be a string, a symbol or a number, this function will search for
  12880. the string representation of it.
  12881. Return the position where this entry starts, or nil if there is no such entry."
  12882. (interactive "sID: ")
  12883. (let ((id (cond
  12884. ((stringp ident) ident)
  12885. ((symbol-name ident) (symbol-name ident))
  12886. ((numberp ident) (number-to-string ident))
  12887. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12888. (case-fold-search nil))
  12889. (save-excursion
  12890. (save-restriction
  12891. (widen)
  12892. (goto-char (point-min))
  12893. (when (re-search-forward
  12894. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12895. nil t)
  12896. (org-back-to-heading t)
  12897. (point))))))
  12898. ;;;; Timestamps
  12899. (defvar org-last-changed-timestamp nil)
  12900. (defvar org-last-inserted-timestamp nil
  12901. "The last time stamp inserted with `org-insert-time-stamp'.")
  12902. (defvar org-time-was-given) ; dynamically scoped parameter
  12903. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12904. (defvar org-ts-what) ; dynamically scoped parameter
  12905. (defun org-time-stamp (arg &optional inactive)
  12906. "Prompt for a date/time and insert a time stamp.
  12907. If the user specifies a time like HH:MM, or if this command is called
  12908. with a prefix argument, the time stamp will contain date and time.
  12909. Otherwise, only the date will be included. All parts of a date not
  12910. specified by the user will be filled in from the current date/time.
  12911. So if you press just return without typing anything, the time stamp
  12912. will represent the current date/time. If there is already a timestamp
  12913. at the cursor, it will be modified."
  12914. (interactive "P")
  12915. (let* ((ts nil)
  12916. (default-time
  12917. ;; Default time is either today, or, when entering a range,
  12918. ;; the range start.
  12919. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12920. (save-excursion
  12921. (re-search-backward
  12922. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12923. (- (point) 20) t)))
  12924. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12925. (current-time)))
  12926. (default-input (and ts (org-get-compact-tod ts)))
  12927. org-time-was-given org-end-time-was-given time)
  12928. (cond
  12929. ((and (org-at-timestamp-p t)
  12930. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12931. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12932. (insert "--")
  12933. (setq time (let ((this-command this-command))
  12934. (org-read-date arg 'totime nil nil
  12935. default-time default-input)))
  12936. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12937. ((org-at-timestamp-p t)
  12938. (setq time (let ((this-command this-command))
  12939. (org-read-date arg 'totime nil nil default-time default-input)))
  12940. (when (org-at-timestamp-p t) ; just to get the match data
  12941. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12942. (replace-match "")
  12943. (setq org-last-changed-timestamp
  12944. (org-insert-time-stamp
  12945. time (or org-time-was-given arg)
  12946. inactive nil nil (list org-end-time-was-given))))
  12947. (message "Timestamp updated"))
  12948. (t
  12949. (setq time (let ((this-command this-command))
  12950. (org-read-date arg 'totime nil nil default-time default-input)))
  12951. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12952. nil nil (list org-end-time-was-given))))))
  12953. ;; FIXME: can we use this for something else, like computing time differences?
  12954. (defun org-get-compact-tod (s)
  12955. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12956. (let* ((t1 (match-string 1 s))
  12957. (h1 (string-to-number (match-string 2 s)))
  12958. (m1 (string-to-number (match-string 3 s)))
  12959. (t2 (and (match-end 4) (match-string 5 s)))
  12960. (h2 (and t2 (string-to-number (match-string 6 s))))
  12961. (m2 (and t2 (string-to-number (match-string 7 s))))
  12962. dh dm)
  12963. (if (not t2)
  12964. t1
  12965. (setq dh (- h2 h1) dm (- m2 m1))
  12966. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12967. (concat t1 "+" (number-to-string dh)
  12968. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12969. (defun org-time-stamp-inactive (&optional arg)
  12970. "Insert an inactive time stamp.
  12971. An inactive time stamp is enclosed in square brackets instead of angle
  12972. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12973. does not link to the calendar and cannot be changed with the S-cursor keys.
  12974. So these are more for recording a certain time/date."
  12975. (interactive "P")
  12976. (org-time-stamp arg 'inactive))
  12977. (defvar org-date-ovl (make-overlay 1 1))
  12978. (overlay-put org-date-ovl 'face 'org-warning)
  12979. (org-detach-overlay org-date-ovl)
  12980. (defvar org-ans1) ; dynamically scoped parameter
  12981. (defvar org-ans2) ; dynamically scoped parameter
  12982. (defvar org-plain-time-of-day-regexp) ; defined below
  12983. (defvar org-overriding-default-time nil) ; dynamically scoped
  12984. (defvar org-read-date-overlay nil)
  12985. (defvar org-dcst nil) ; dynamically scoped
  12986. (defvar org-read-date-history nil)
  12987. (defvar org-read-date-final-answer nil)
  12988. (defun org-read-date (&optional with-time to-time from-string prompt
  12989. default-time default-input)
  12990. "Read a date, possibly a time, and make things smooth for the user.
  12991. The prompt will suggest to enter an ISO date, but you can also enter anything
  12992. which will at least partially be understood by `parse-time-string'.
  12993. Unrecognized parts of the date will default to the current day, month, year,
  12994. hour and minute. If this command is called to replace a timestamp at point,
  12995. of to enter the second timestamp of a range, the default time is taken
  12996. from the existing stamp. Furthermore, the command prefers the future,
  12997. so if you are giving a date where the year is not given, and the day-month
  12998. combination is already past in the current year, it will assume you
  12999. mean next year. For details, see the manual. A few examples:
  13000. 3-2-5 --> 2003-02-05
  13001. feb 15 --> currentyear-02-15
  13002. 2/15 --> currentyear-02-15
  13003. sep 12 9 --> 2009-09-12
  13004. 12:45 --> today 12:45
  13005. 22 sept 0:34 --> currentyear-09-22 0:34
  13006. 12 --> currentyear-currentmonth-12
  13007. Fri --> nearest Friday (today or later)
  13008. etc.
  13009. Furthermore you can specify a relative date by giving, as the *first* thing
  13010. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13011. change in days weeks, months, years.
  13012. With a single plus or minus, the date is relative to today. With a double
  13013. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13014. +4d --> four days from today
  13015. +4 --> same as above
  13016. +2w --> two weeks from today
  13017. ++5 --> five days from default date
  13018. The function understands only English month and weekday abbreviations,
  13019. but this can be configured with the variables `parse-time-months' and
  13020. `parse-time-weekdays'.
  13021. While prompting, a calendar is popped up - you can also select the
  13022. date with the mouse (button 1). The calendar shows a period of three
  13023. months. To scroll it to other months, use the keys `>' and `<'.
  13024. If you don't like the calendar, turn it off with
  13025. \(setq org-read-date-popup-calendar nil)
  13026. With optional argument TO-TIME, the date will immediately be converted
  13027. to an internal time.
  13028. With an optional argument WITH-TIME, the prompt will suggest to also
  13029. insert a time. Note that when WITH-TIME is not set, you can still
  13030. enter a time, and this function will inform the calling routine about
  13031. this change. The calling routine may then choose to change the format
  13032. used to insert the time stamp into the buffer to include the time.
  13033. With optional argument FROM-STRING, read from this string instead from
  13034. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13035. the time/date that is used for everything that is not specified by the
  13036. user."
  13037. (require 'parse-time)
  13038. (let* ((org-time-stamp-rounding-minutes
  13039. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13040. (org-dcst org-display-custom-times)
  13041. (ct (org-current-time))
  13042. (def (or org-overriding-default-time default-time ct))
  13043. (defdecode (decode-time def))
  13044. (dummy (progn
  13045. (when (< (nth 2 defdecode) org-extend-today-until)
  13046. (setcar (nthcdr 2 defdecode) -1)
  13047. (setcar (nthcdr 1 defdecode) 59)
  13048. (setq def (apply 'encode-time defdecode)
  13049. defdecode (decode-time def)))))
  13050. (calendar-frame-setup nil)
  13051. (calendar-setup nil)
  13052. (calendar-move-hook nil)
  13053. (calendar-view-diary-initially-flag nil)
  13054. (calendar-view-holidays-initially-flag nil)
  13055. (timestr (format-time-string
  13056. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13057. (prompt (concat (if prompt (concat prompt " ") "")
  13058. (format "Date+time [%s]: " timestr)))
  13059. ans (org-ans0 "") org-ans1 org-ans2 final)
  13060. (cond
  13061. (from-string (setq ans from-string))
  13062. (org-read-date-popup-calendar
  13063. (save-excursion
  13064. (save-window-excursion
  13065. (calendar)
  13066. (calendar-forward-day (- (time-to-days def)
  13067. (calendar-absolute-from-gregorian
  13068. (calendar-current-date))))
  13069. (org-eval-in-calendar nil t)
  13070. (let* ((old-map (current-local-map))
  13071. (map (copy-keymap calendar-mode-map))
  13072. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13073. (org-defkey map (kbd "RET") 'org-calendar-select)
  13074. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13075. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13076. (org-defkey minibuffer-local-map [(meta shift left)]
  13077. (lambda () (interactive)
  13078. (org-eval-in-calendar '(calendar-backward-month 1))))
  13079. (org-defkey minibuffer-local-map [(meta shift right)]
  13080. (lambda () (interactive)
  13081. (org-eval-in-calendar '(calendar-forward-month 1))))
  13082. (org-defkey minibuffer-local-map [(meta shift up)]
  13083. (lambda () (interactive)
  13084. (org-eval-in-calendar '(calendar-backward-year 1))))
  13085. (org-defkey minibuffer-local-map [(meta shift down)]
  13086. (lambda () (interactive)
  13087. (org-eval-in-calendar '(calendar-forward-year 1))))
  13088. (org-defkey minibuffer-local-map [?\e (shift left)]
  13089. (lambda () (interactive)
  13090. (org-eval-in-calendar '(calendar-backward-month 1))))
  13091. (org-defkey minibuffer-local-map [?\e (shift right)]
  13092. (lambda () (interactive)
  13093. (org-eval-in-calendar '(calendar-forward-month 1))))
  13094. (org-defkey minibuffer-local-map [?\e (shift up)]
  13095. (lambda () (interactive)
  13096. (org-eval-in-calendar '(calendar-backward-year 1))))
  13097. (org-defkey minibuffer-local-map [?\e (shift down)]
  13098. (lambda () (interactive)
  13099. (org-eval-in-calendar '(calendar-forward-year 1))))
  13100. (org-defkey minibuffer-local-map [(shift up)]
  13101. (lambda () (interactive)
  13102. (org-eval-in-calendar '(calendar-backward-week 1))))
  13103. (org-defkey minibuffer-local-map [(shift down)]
  13104. (lambda () (interactive)
  13105. (org-eval-in-calendar '(calendar-forward-week 1))))
  13106. (org-defkey minibuffer-local-map [(shift left)]
  13107. (lambda () (interactive)
  13108. (org-eval-in-calendar '(calendar-backward-day 1))))
  13109. (org-defkey minibuffer-local-map [(shift right)]
  13110. (lambda () (interactive)
  13111. (org-eval-in-calendar '(calendar-forward-day 1))))
  13112. (org-defkey minibuffer-local-map ">"
  13113. (lambda () (interactive)
  13114. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13115. (org-defkey minibuffer-local-map "<"
  13116. (lambda () (interactive)
  13117. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13118. (org-defkey minibuffer-local-map "\C-v"
  13119. (lambda () (interactive)
  13120. (org-eval-in-calendar
  13121. '(calendar-scroll-left-three-months 1))))
  13122. (org-defkey minibuffer-local-map "\M-v"
  13123. (lambda () (interactive)
  13124. (org-eval-in-calendar
  13125. '(calendar-scroll-right-three-months 1))))
  13126. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13127. (unwind-protect
  13128. (progn
  13129. (use-local-map map)
  13130. (add-hook 'post-command-hook 'org-read-date-display)
  13131. (setq org-ans0 (read-string prompt default-input
  13132. 'org-read-date-history nil))
  13133. ;; org-ans0: from prompt
  13134. ;; org-ans1: from mouse click
  13135. ;; org-ans2: from calendar motion
  13136. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13137. (remove-hook 'post-command-hook 'org-read-date-display)
  13138. (use-local-map old-map)
  13139. (when org-read-date-overlay
  13140. (delete-overlay org-read-date-overlay)
  13141. (setq org-read-date-overlay nil)))))))
  13142. (t ; Naked prompt only
  13143. (unwind-protect
  13144. (setq ans (read-string prompt default-input
  13145. 'org-read-date-history timestr))
  13146. (when org-read-date-overlay
  13147. (delete-overlay org-read-date-overlay)
  13148. (setq org-read-date-overlay nil)))))
  13149. (setq final (org-read-date-analyze ans def defdecode))
  13150. ;; One round trip to get rid of 34th of August and stuff like that....
  13151. (setq final (decode-time (apply 'encode-time final)))
  13152. (setq org-read-date-final-answer ans)
  13153. (if to-time
  13154. (apply 'encode-time final)
  13155. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13156. (format "%04d-%02d-%02d %02d:%02d"
  13157. (nth 5 final) (nth 4 final) (nth 3 final)
  13158. (nth 2 final) (nth 1 final))
  13159. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13160. (defvar def)
  13161. (defvar defdecode)
  13162. (defvar with-time)
  13163. (defvar org-read-date-analyze-futurep nil)
  13164. (defun org-read-date-display ()
  13165. "Display the current date prompt interpretation in the minibuffer."
  13166. (when org-read-date-display-live
  13167. (when org-read-date-overlay
  13168. (delete-overlay org-read-date-overlay))
  13169. (let ((p (point)))
  13170. (end-of-line 1)
  13171. (while (not (equal (buffer-substring
  13172. (max (point-min) (- (point) 4)) (point))
  13173. " "))
  13174. (insert " "))
  13175. (goto-char p))
  13176. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13177. " " (or org-ans1 org-ans2)))
  13178. (org-end-time-was-given nil)
  13179. (f (org-read-date-analyze ans def defdecode))
  13180. (fmts (if org-dcst
  13181. org-time-stamp-custom-formats
  13182. org-time-stamp-formats))
  13183. (fmt (if (or with-time
  13184. (and (boundp 'org-time-was-given) org-time-was-given))
  13185. (cdr fmts)
  13186. (car fmts)))
  13187. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13188. (when (and org-end-time-was-given
  13189. (string-match org-plain-time-of-day-regexp txt))
  13190. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13191. org-end-time-was-given
  13192. (substring txt (match-end 0)))))
  13193. (when org-read-date-analyze-futurep
  13194. (setq txt (concat txt " (=>F)")))
  13195. (setq org-read-date-overlay
  13196. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13197. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13198. (defun org-read-date-analyze (ans def defdecode)
  13199. "Analyze the combined answer of the date prompt."
  13200. ;; FIXME: cleanup and comment
  13201. (let ((nowdecode (decode-time (current-time)))
  13202. delta deltan deltaw deltadef year month day
  13203. hour minute second wday pm h2 m2 tl wday1
  13204. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13205. (setq org-read-date-analyze-futurep nil)
  13206. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13207. (setq ans "+0"))
  13208. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13209. (setq ans (replace-match "" t t ans)
  13210. deltan (car delta)
  13211. deltaw (nth 1 delta)
  13212. deltadef (nth 2 delta)))
  13213. ;; Check if there is an iso week date in there
  13214. ;; If yes, store the info and postpone interpreting it until the rest
  13215. ;; of the parsing is done
  13216. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13217. (setq iso-year (if (match-end 1)
  13218. (org-small-year-to-year
  13219. (string-to-number (match-string 1 ans))))
  13220. iso-weekday (if (match-end 3)
  13221. (string-to-number (match-string 3 ans)))
  13222. iso-week (string-to-number (match-string 2 ans)))
  13223. (setq ans (replace-match "" t t ans)))
  13224. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13225. (when (string-match
  13226. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13227. (setq year (if (match-end 2)
  13228. (string-to-number (match-string 2 ans))
  13229. (progn (setq kill-year t)
  13230. (string-to-number (format-time-string "%Y"))))
  13231. month (string-to-number (match-string 3 ans))
  13232. day (string-to-number (match-string 4 ans)))
  13233. (if (< year 100) (setq year (+ 2000 year)))
  13234. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13235. t nil ans)))
  13236. ;; Help matching american dates, like 5/30 or 5/30/7
  13237. (when (string-match
  13238. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13239. (setq year (if (match-end 4)
  13240. (string-to-number (match-string 4 ans))
  13241. (progn (setq kill-year t)
  13242. (string-to-number (format-time-string "%Y"))))
  13243. month (string-to-number (match-string 1 ans))
  13244. day (string-to-number (match-string 2 ans)))
  13245. (if (< year 100) (setq year (+ 2000 year)))
  13246. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13247. t nil ans)))
  13248. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13249. ;; If there is a time with am/pm, and *no* time without it, we convert
  13250. ;; so that matching will be successful.
  13251. (loop for i from 1 to 2 do ; twice, for end time as well
  13252. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13253. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13254. (setq hour (string-to-number (match-string 1 ans))
  13255. minute (if (match-end 3)
  13256. (string-to-number (match-string 3 ans))
  13257. 0)
  13258. pm (equal ?p
  13259. (string-to-char (downcase (match-string 4 ans)))))
  13260. (if (and (= hour 12) (not pm))
  13261. (setq hour 0)
  13262. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13263. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13264. t t ans))))
  13265. ;; Check if a time range is given as a duration
  13266. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13267. (setq hour (string-to-number (match-string 1 ans))
  13268. h2 (+ hour (string-to-number (match-string 3 ans)))
  13269. minute (string-to-number (match-string 2 ans))
  13270. m2 (+ minute (if (match-end 5) (string-to-number
  13271. (match-string 5 ans))0)))
  13272. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13273. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13274. t t ans)))
  13275. ;; Check if there is a time range
  13276. (when (boundp 'org-end-time-was-given)
  13277. (setq org-time-was-given nil)
  13278. (when (and (string-match org-plain-time-of-day-regexp ans)
  13279. (match-end 8))
  13280. (setq org-end-time-was-given (match-string 8 ans))
  13281. (setq ans (concat (substring ans 0 (match-beginning 7))
  13282. (substring ans (match-end 7))))))
  13283. (setq tl (parse-time-string ans)
  13284. day (or (nth 3 tl) (nth 3 defdecode))
  13285. month (or (nth 4 tl)
  13286. (if (and org-read-date-prefer-future
  13287. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13288. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13289. (nth 4 defdecode)))
  13290. year (or (and (not kill-year) (nth 5 tl))
  13291. (if (and org-read-date-prefer-future
  13292. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13293. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13294. (nth 5 defdecode)))
  13295. hour (or (nth 2 tl) (nth 2 defdecode))
  13296. minute (or (nth 1 tl) (nth 1 defdecode))
  13297. second (or (nth 0 tl) 0)
  13298. wday (nth 6 tl))
  13299. (when (and (eq org-read-date-prefer-future 'time)
  13300. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13301. (equal day (nth 3 nowdecode))
  13302. (equal month (nth 4 nowdecode))
  13303. (equal year (nth 5 nowdecode))
  13304. (nth 2 tl)
  13305. (or (< (nth 2 tl) (nth 2 nowdecode))
  13306. (and (= (nth 2 tl) (nth 2 nowdecode))
  13307. (nth 1 tl)
  13308. (< (nth 1 tl) (nth 1 nowdecode)))))
  13309. (setq day (1+ day)
  13310. futurep t))
  13311. ;; Special date definitions below
  13312. (cond
  13313. (iso-week
  13314. ;; There was an iso week
  13315. (require 'cal-iso)
  13316. (setq futurep nil)
  13317. (setq year (or iso-year year)
  13318. day (or iso-weekday wday 1)
  13319. wday nil ; to make sure that the trigger below does not match
  13320. iso-date (calendar-gregorian-from-absolute
  13321. (calendar-absolute-from-iso
  13322. (list iso-week day year))))
  13323. ; FIXME: Should we also push ISO weeks into the future?
  13324. ; (when (and org-read-date-prefer-future
  13325. ; (not iso-year)
  13326. ; (< (calendar-absolute-from-gregorian iso-date)
  13327. ; (time-to-days (current-time))))
  13328. ; (setq year (1+ year)
  13329. ; iso-date (calendar-gregorian-from-absolute
  13330. ; (calendar-absolute-from-iso
  13331. ; (list iso-week day year)))))
  13332. (setq month (car iso-date)
  13333. year (nth 2 iso-date)
  13334. day (nth 1 iso-date)))
  13335. (deltan
  13336. (setq futurep nil)
  13337. (unless deltadef
  13338. (let ((now (decode-time (current-time))))
  13339. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13340. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13341. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13342. ((equal deltaw "m") (setq month (+ month deltan)))
  13343. ((equal deltaw "y") (setq year (+ year deltan)))))
  13344. ((and wday (not (nth 3 tl)))
  13345. (setq futurep nil)
  13346. ;; Weekday was given, but no day, so pick that day in the week
  13347. ;; on or after the derived date.
  13348. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13349. (unless (equal wday wday1)
  13350. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13351. (if (and (boundp 'org-time-was-given)
  13352. (nth 2 tl))
  13353. (setq org-time-was-given t))
  13354. (if (< year 100) (setq year (+ 2000 year)))
  13355. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  13356. (setq org-read-date-analyze-futurep futurep)
  13357. (list second minute hour day month year)))
  13358. (defvar parse-time-weekdays)
  13359. (defun org-read-date-get-relative (s today default)
  13360. "Check string S for special relative date string.
  13361. TODAY and DEFAULT are internal times, for today and for a default.
  13362. Return shift list (N what def-flag)
  13363. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13364. N is the number of WHATs to shift.
  13365. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13366. the DEFAULT date rather than TODAY."
  13367. (when (and
  13368. (string-match
  13369. (concat
  13370. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13371. "\\([0-9]+\\)?"
  13372. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13373. "\\([ \t]\\|$\\)") s)
  13374. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13375. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13376. (string-to-char (substring (match-string 1 s) -1))
  13377. ?+))
  13378. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13379. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13380. (what (if (match-end 3) (match-string 3 s) "d"))
  13381. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13382. (date (if rel default today))
  13383. (wday (nth 6 (decode-time date)))
  13384. delta)
  13385. (if wday1
  13386. (progn
  13387. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13388. (if (= dir ?-) (setq delta (- delta 7)))
  13389. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13390. (list delta "d" rel))
  13391. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13392. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13393. "Turn a user-specified date into the internal representation.
  13394. The internal representation needed by the calendar is (month day year).
  13395. This is a wrapper to handle the brain-dead convention in calendar that
  13396. user function argument order change dependent on argument order."
  13397. (if (boundp 'calendar-date-style)
  13398. (cond
  13399. ((eq calendar-date-style 'american)
  13400. (list arg1 arg2 arg3))
  13401. ((eq calendar-date-style 'european)
  13402. (list arg2 arg1 arg3))
  13403. ((eq calendar-date-style 'iso)
  13404. (list arg2 arg3 arg1)))
  13405. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13406. (if (org-bound-and-true-p european-calendar-style)
  13407. (list arg2 arg1 arg3)
  13408. (list arg1 arg2 arg3)))))
  13409. (defun org-eval-in-calendar (form &optional keepdate)
  13410. "Eval FORM in the calendar window and return to current window.
  13411. Also, store the cursor date in variable org-ans2."
  13412. (let ((sf (selected-frame))
  13413. (sw (selected-window)))
  13414. (select-window (get-buffer-window "*Calendar*" t))
  13415. (eval form)
  13416. (when (and (not keepdate) (calendar-cursor-to-date))
  13417. (let* ((date (calendar-cursor-to-date))
  13418. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13419. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13420. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13421. (select-window sw)
  13422. (org-select-frame-set-input-focus sf)))
  13423. (defun org-calendar-select ()
  13424. "Return to `org-read-date' with the date currently selected.
  13425. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13426. (interactive)
  13427. (when (calendar-cursor-to-date)
  13428. (let* ((date (calendar-cursor-to-date))
  13429. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13430. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13431. (if (active-minibuffer-window) (exit-minibuffer))))
  13432. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13433. "Insert a date stamp for the date given by the internal TIME.
  13434. WITH-HM means use the stamp format that includes the time of the day.
  13435. INACTIVE means use square brackets instead of angular ones, so that the
  13436. stamp will not contribute to the agenda.
  13437. PRE and POST are optional strings to be inserted before and after the
  13438. stamp.
  13439. The command returns the inserted time stamp."
  13440. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13441. stamp)
  13442. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13443. (insert-before-markers (or pre ""))
  13444. (when (listp extra)
  13445. (setq extra (car extra))
  13446. (if (and (stringp extra)
  13447. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13448. (setq extra (format "-%02d:%02d"
  13449. (string-to-number (match-string 1 extra))
  13450. (string-to-number (match-string 2 extra))))
  13451. (setq extra nil)))
  13452. (when extra
  13453. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13454. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13455. (insert-before-markers (or post ""))
  13456. (setq org-last-inserted-timestamp stamp)))
  13457. (defun org-toggle-time-stamp-overlays ()
  13458. "Toggle the use of custom time stamp formats."
  13459. (interactive)
  13460. (setq org-display-custom-times (not org-display-custom-times))
  13461. (unless org-display-custom-times
  13462. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13463. (while (setq p (next-single-property-change p 'display))
  13464. (if (and (get-text-property p 'display)
  13465. (eq (get-text-property p 'face) 'org-date))
  13466. (remove-text-properties
  13467. p (setq p (next-single-property-change p 'display))
  13468. '(display t))))
  13469. (set-buffer-modified-p bmp)))
  13470. (if (featurep 'xemacs)
  13471. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13472. (org-restart-font-lock)
  13473. (setq org-table-may-need-update t)
  13474. (if org-display-custom-times
  13475. (message "Time stamps are overlayed with custom format")
  13476. (message "Time stamp overlays removed")))
  13477. (defun org-display-custom-time (beg end)
  13478. "Overlay modified time stamp format over timestamp between BEG and END."
  13479. (let* ((ts (buffer-substring beg end))
  13480. t1 w1 with-hm tf time str w2 (off 0))
  13481. (save-match-data
  13482. (setq t1 (org-parse-time-string ts t))
  13483. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13484. (setq off (- (match-end 0) (match-beginning 0)))))
  13485. (setq end (- end off))
  13486. (setq w1 (- end beg)
  13487. with-hm (and (nth 1 t1) (nth 2 t1))
  13488. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13489. time (org-fix-decoded-time t1)
  13490. str (org-add-props
  13491. (format-time-string
  13492. (substring tf 1 -1) (apply 'encode-time time))
  13493. nil 'mouse-face 'highlight)
  13494. w2 (length str))
  13495. (if (not (= w2 w1))
  13496. (add-text-properties (1+ beg) (+ 2 beg)
  13497. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13498. (if (featurep 'xemacs)
  13499. (progn
  13500. (put-text-property beg end 'invisible t)
  13501. (put-text-property beg end 'end-glyph (make-glyph str)))
  13502. (put-text-property beg end 'display str))))
  13503. (defun org-translate-time (string)
  13504. "Translate all timestamps in STRING to custom format.
  13505. But do this only if the variable `org-display-custom-times' is set."
  13506. (when org-display-custom-times
  13507. (save-match-data
  13508. (let* ((start 0)
  13509. (re org-ts-regexp-both)
  13510. t1 with-hm inactive tf time str beg end)
  13511. (while (setq start (string-match re string start))
  13512. (setq beg (match-beginning 0)
  13513. end (match-end 0)
  13514. t1 (save-match-data
  13515. (org-parse-time-string (substring string beg end) t))
  13516. with-hm (and (nth 1 t1) (nth 2 t1))
  13517. inactive (equal (substring string beg (1+ beg)) "[")
  13518. tf (funcall (if with-hm 'cdr 'car)
  13519. org-time-stamp-custom-formats)
  13520. time (org-fix-decoded-time t1)
  13521. str (format-time-string
  13522. (concat
  13523. (if inactive "[" "<") (substring tf 1 -1)
  13524. (if inactive "]" ">"))
  13525. (apply 'encode-time time))
  13526. string (replace-match str t t string)
  13527. start (+ start (length str)))))))
  13528. string)
  13529. (defun org-fix-decoded-time (time)
  13530. "Set 0 instead of nil for the first 6 elements of time.
  13531. Don't touch the rest."
  13532. (let ((n 0))
  13533. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13534. (defun org-days-to-time (timestamp-string)
  13535. "Difference between TIMESTAMP-STRING and now in days."
  13536. (- (time-to-days (org-time-string-to-time timestamp-string))
  13537. (time-to-days (current-time))))
  13538. (defun org-deadline-close (timestamp-string &optional ndays)
  13539. "Is the time in TIMESTAMP-STRING close to the current date?"
  13540. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13541. (and (< (org-days-to-time timestamp-string) ndays)
  13542. (not (org-entry-is-done-p))))
  13543. (defun org-get-wdays (ts)
  13544. "Get the deadline lead time appropriate for timestring TS."
  13545. (cond
  13546. ((<= org-deadline-warning-days 0)
  13547. ;; 0 or negative, enforce this value no matter what
  13548. (- org-deadline-warning-days))
  13549. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13550. ;; lead time is specified.
  13551. (floor (* (string-to-number (match-string 1 ts))
  13552. (cdr (assoc (match-string 2 ts)
  13553. '(("d" . 1) ("w" . 7)
  13554. ("m" . 30.4) ("y" . 365.25)))))))
  13555. ;; go for the default.
  13556. (t org-deadline-warning-days)))
  13557. (defun org-calendar-select-mouse (ev)
  13558. "Return to `org-read-date' with the date currently selected.
  13559. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13560. (interactive "e")
  13561. (mouse-set-point ev)
  13562. (when (calendar-cursor-to-date)
  13563. (let* ((date (calendar-cursor-to-date))
  13564. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13565. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13566. (if (active-minibuffer-window) (exit-minibuffer))))
  13567. (defun org-check-deadlines (ndays)
  13568. "Check if there are any deadlines due or past due.
  13569. A deadline is considered due if it happens within `org-deadline-warning-days'
  13570. days from today's date. If the deadline appears in an entry marked DONE,
  13571. it is not shown. The prefix arg NDAYS can be used to test that many
  13572. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13573. (interactive "P")
  13574. (let* ((org-warn-days
  13575. (cond
  13576. ((equal ndays '(4)) 100000)
  13577. (ndays (prefix-numeric-value ndays))
  13578. (t (abs org-deadline-warning-days))))
  13579. (case-fold-search nil)
  13580. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13581. (callback
  13582. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13583. (message "%d deadlines past-due or due within %d days"
  13584. (org-occur regexp nil callback)
  13585. org-warn-days)))
  13586. (defun org-check-before-date (date)
  13587. "Check if there are deadlines or scheduled entries before DATE."
  13588. (interactive (list (org-read-date)))
  13589. (let ((case-fold-search nil)
  13590. (regexp (concat "\\<\\(" org-deadline-string
  13591. "\\|" org-scheduled-string
  13592. "\\) *<\\([^>]+\\)>"))
  13593. (callback
  13594. (lambda () (time-less-p
  13595. (org-time-string-to-time (match-string 2))
  13596. (org-time-string-to-time date)))))
  13597. (message "%d entries before %s"
  13598. (org-occur regexp nil callback) date)))
  13599. (defun org-check-after-date (date)
  13600. "Check if there are deadlines or scheduled entries after DATE."
  13601. (interactive (list (org-read-date)))
  13602. (let ((case-fold-search nil)
  13603. (regexp (concat "\\<\\(" org-deadline-string
  13604. "\\|" org-scheduled-string
  13605. "\\) *<\\([^>]+\\)>"))
  13606. (callback
  13607. (lambda () (not
  13608. (time-less-p
  13609. (org-time-string-to-time (match-string 2))
  13610. (org-time-string-to-time date))))))
  13611. (message "%d entries after %s"
  13612. (org-occur regexp nil callback) date)))
  13613. (defun org-evaluate-time-range (&optional to-buffer)
  13614. "Evaluate a time range by computing the difference between start and end.
  13615. Normally the result is just printed in the echo area, but with prefix arg
  13616. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13617. If the time range is actually in a table, the result is inserted into the
  13618. next column.
  13619. For time difference computation, a year is assumed to be exactly 365
  13620. days in order to avoid rounding problems."
  13621. (interactive "P")
  13622. (or
  13623. (org-clock-update-time-maybe)
  13624. (save-excursion
  13625. (unless (org-at-date-range-p t)
  13626. (goto-char (point-at-bol))
  13627. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13628. (if (not (org-at-date-range-p t))
  13629. (error "Not at a time-stamp range, and none found in current line")))
  13630. (let* ((ts1 (match-string 1))
  13631. (ts2 (match-string 2))
  13632. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13633. (match-end (match-end 0))
  13634. (time1 (org-time-string-to-time ts1))
  13635. (time2 (org-time-string-to-time ts2))
  13636. (t1 (org-float-time time1))
  13637. (t2 (org-float-time time2))
  13638. (diff (abs (- t2 t1)))
  13639. (negative (< (- t2 t1) 0))
  13640. ;; (ys (floor (* 365 24 60 60)))
  13641. (ds (* 24 60 60))
  13642. (hs (* 60 60))
  13643. (fy "%dy %dd %02d:%02d")
  13644. (fy1 "%dy %dd")
  13645. (fd "%dd %02d:%02d")
  13646. (fd1 "%dd")
  13647. (fh "%02d:%02d")
  13648. y d h m align)
  13649. (if havetime
  13650. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13651. y 0
  13652. d (floor (/ diff ds)) diff (mod diff ds)
  13653. h (floor (/ diff hs)) diff (mod diff hs)
  13654. m (floor (/ diff 60)))
  13655. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13656. y 0
  13657. d (floor (+ (/ diff ds) 0.5))
  13658. h 0 m 0))
  13659. (if (not to-buffer)
  13660. (message "%s" (org-make-tdiff-string y d h m))
  13661. (if (org-at-table-p)
  13662. (progn
  13663. (goto-char match-end)
  13664. (setq align t)
  13665. (and (looking-at " *|") (goto-char (match-end 0))))
  13666. (goto-char match-end))
  13667. (if (looking-at
  13668. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13669. (replace-match ""))
  13670. (if negative (insert " -"))
  13671. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13672. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13673. (insert " " (format fh h m))))
  13674. (if align (org-table-align))
  13675. (message "Time difference inserted")))))
  13676. (defun org-make-tdiff-string (y d h m)
  13677. (let ((fmt "")
  13678. (l nil))
  13679. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13680. l (push y l)))
  13681. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13682. l (push d l)))
  13683. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13684. l (push h l)))
  13685. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13686. l (push m l)))
  13687. (apply 'format fmt (nreverse l))))
  13688. (defun org-time-string-to-time (s)
  13689. (apply 'encode-time (org-parse-time-string s)))
  13690. (defun org-time-string-to-seconds (s)
  13691. (org-float-time (org-time-string-to-time s)))
  13692. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13693. "Convert a time stamp to an absolute day number.
  13694. If there is a specifier for a cyclic time stamp, get the closest date to
  13695. DAYNR.
  13696. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13697. the variable date is bound by the calendar when this is called."
  13698. (cond
  13699. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13700. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13701. daynr
  13702. (+ daynr 1000)))
  13703. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13704. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13705. (time-to-days (current-time))) (match-string 0 s)
  13706. prefer show-all))
  13707. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13708. (defun org-days-to-iso-week (days)
  13709. "Return the iso week number."
  13710. (require 'cal-iso)
  13711. (car (calendar-iso-from-absolute days)))
  13712. (defun org-small-year-to-year (year)
  13713. "Convert 2-digit years into 4-digit years.
  13714. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13715. The year 2000 cannot be abbreviated. Any year larger than 99
  13716. is returned unchanged."
  13717. (if (< year 38)
  13718. (setq year (+ 2000 year))
  13719. (if (< year 100)
  13720. (setq year (+ 1900 year))))
  13721. year)
  13722. (defun org-time-from-absolute (d)
  13723. "Return the time corresponding to date D.
  13724. D may be an absolute day number, or a calendar-type list (month day year)."
  13725. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13726. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13727. (defun org-calendar-holiday ()
  13728. "List of holidays, for Diary display in Org-mode."
  13729. (require 'holidays)
  13730. (let ((hl (funcall
  13731. (if (fboundp 'calendar-check-holidays)
  13732. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13733. (if hl (mapconcat 'identity hl "; "))))
  13734. (defun org-diary-sexp-entry (sexp entry date)
  13735. "Process a SEXP diary ENTRY for DATE."
  13736. (require 'diary-lib)
  13737. (let ((result (if calendar-debug-sexp
  13738. (let ((stack-trace-on-error t))
  13739. (eval (car (read-from-string sexp))))
  13740. (condition-case nil
  13741. (eval (car (read-from-string sexp)))
  13742. (error
  13743. (beep)
  13744. (message "Bad sexp at line %d in %s: %s"
  13745. (org-current-line)
  13746. (buffer-file-name) sexp)
  13747. (sleep-for 2))))))
  13748. (cond ((stringp result) result)
  13749. ((and (consp result)
  13750. (stringp (cdr result))) (cdr result))
  13751. (result entry)
  13752. (t nil))))
  13753. (defun org-diary-to-ical-string (frombuf)
  13754. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13755. This uses the icalendar.el library."
  13756. (let* ((tmpdir (if (featurep 'xemacs)
  13757. (temp-directory)
  13758. temporary-file-directory))
  13759. (tmpfile (make-temp-name
  13760. (expand-file-name "orgics" tmpdir)))
  13761. buf rtn b e)
  13762. (with-current-buffer frombuf
  13763. (icalendar-export-region (point-min) (point-max) tmpfile)
  13764. (setq buf (find-buffer-visiting tmpfile))
  13765. (set-buffer buf)
  13766. (goto-char (point-min))
  13767. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13768. (setq b (match-beginning 0)))
  13769. (goto-char (point-max))
  13770. (if (re-search-backward "^END:VEVENT" nil t)
  13771. (setq e (match-end 0)))
  13772. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13773. (kill-buffer buf)
  13774. (delete-file tmpfile)
  13775. rtn))
  13776. (defun org-closest-date (start current change prefer show-all)
  13777. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13778. When PREFER is `past' return a date that is either CURRENT or past.
  13779. When PREFER is `future', return a date that is either CURRENT or future.
  13780. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13781. ;; Make the proper lists from the dates
  13782. (catch 'exit
  13783. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13784. dn dw sday cday n1 n2 n0
  13785. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13786. (setq start (org-date-to-gregorian start)
  13787. current (org-date-to-gregorian
  13788. (if show-all
  13789. current
  13790. (time-to-days (current-time))))
  13791. sday (calendar-absolute-from-gregorian start)
  13792. cday (calendar-absolute-from-gregorian current))
  13793. (if (<= cday sday) (throw 'exit sday))
  13794. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13795. (setq dn (string-to-number (match-string 1 change))
  13796. dw (cdr (assoc (match-string 2 change) a1)))
  13797. (error "Invalid change specifier: %s" change))
  13798. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13799. (cond
  13800. ((eq dw 'day)
  13801. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13802. n2 (+ n1 dn)))
  13803. ((eq dw 'year)
  13804. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13805. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13806. (setq date1 (list m d y1)
  13807. n1 (calendar-absolute-from-gregorian date1)
  13808. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13809. n2 (calendar-absolute-from-gregorian date2)))
  13810. ((eq dw 'month)
  13811. ;; approx number of month between the two dates
  13812. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13813. ;; How often does dn fit in there?
  13814. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13815. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13816. m (+ m nm)
  13817. ny (floor (/ m 12))
  13818. y (+ y ny)
  13819. m (- m (* ny 12)))
  13820. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13821. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13822. (setq m2 (+ m dn) y2 y)
  13823. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13824. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13825. (while (<= n2 cday)
  13826. (setq n1 n2 m m2 y y2)
  13827. (setq m2 (+ m dn) y2 y)
  13828. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13829. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13830. ;; Make sure n1 is the earlier date
  13831. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13832. (if show-all
  13833. (cond
  13834. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13835. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13836. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13837. (cond
  13838. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13839. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13840. (t (if (= cday n1) n1 n2)))))))
  13841. (defun org-date-to-gregorian (date)
  13842. "Turn any specification of DATE into a Gregorian date for the calendar."
  13843. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13844. ((and (listp date) (= (length date) 3)) date)
  13845. ((stringp date)
  13846. (setq date (org-parse-time-string date))
  13847. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13848. ((listp date)
  13849. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13850. (defun org-parse-time-string (s &optional nodefault)
  13851. "Parse the standard Org-mode time string.
  13852. This should be a lot faster than the normal `parse-time-string'.
  13853. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13854. hour and minute fields will be nil if not given."
  13855. (if (string-match org-ts-regexp0 s)
  13856. (list 0
  13857. (if (or (match-beginning 8) (not nodefault))
  13858. (string-to-number (or (match-string 8 s) "0")))
  13859. (if (or (match-beginning 7) (not nodefault))
  13860. (string-to-number (or (match-string 7 s) "0")))
  13861. (string-to-number (match-string 4 s))
  13862. (string-to-number (match-string 3 s))
  13863. (string-to-number (match-string 2 s))
  13864. nil nil nil)
  13865. (error "Not a standard Org-mode time string: %s" s)))
  13866. (defun org-timestamp-up (&optional arg)
  13867. "Increase the date item at the cursor by one.
  13868. If the cursor is on the year, change the year. If it is on the month or
  13869. the day, change that.
  13870. With prefix ARG, change by that many units."
  13871. (interactive "p")
  13872. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  13873. (defun org-timestamp-down (&optional arg)
  13874. "Decrease the date item at the cursor by one.
  13875. If the cursor is on the year, change the year. If it is on the month or
  13876. the day, change that.
  13877. With prefix ARG, change by that many units."
  13878. (interactive "p")
  13879. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  13880. (defun org-timestamp-up-day (&optional arg)
  13881. "Increase the date in the time stamp by one day.
  13882. With prefix ARG, change that many days."
  13883. (interactive "p")
  13884. (if (and (not (org-at-timestamp-p t))
  13885. (org-on-heading-p))
  13886. (org-todo 'up)
  13887. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  13888. (defun org-timestamp-down-day (&optional arg)
  13889. "Decrease the date in the time stamp by one day.
  13890. With prefix ARG, change that many days."
  13891. (interactive "p")
  13892. (if (and (not (org-at-timestamp-p t))
  13893. (org-on-heading-p))
  13894. (org-todo 'down)
  13895. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  13896. (defun org-at-timestamp-p (&optional inactive-ok)
  13897. "Determine if the cursor is in or at a timestamp."
  13898. (interactive)
  13899. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13900. (pos (point))
  13901. (ans (or (looking-at tsr)
  13902. (save-excursion
  13903. (skip-chars-backward "^[<\n\r\t")
  13904. (if (> (point) (point-min)) (backward-char 1))
  13905. (and (looking-at tsr)
  13906. (> (- (match-end 0) pos) -1))))))
  13907. (and ans
  13908. (boundp 'org-ts-what)
  13909. (setq org-ts-what
  13910. (cond
  13911. ((= pos (match-beginning 0)) 'bracket)
  13912. ((= pos (1- (match-end 0))) 'bracket)
  13913. ((org-pos-in-match-range pos 2) 'year)
  13914. ((org-pos-in-match-range pos 3) 'month)
  13915. ((org-pos-in-match-range pos 7) 'hour)
  13916. ((org-pos-in-match-range pos 8) 'minute)
  13917. ((or (org-pos-in-match-range pos 4)
  13918. (org-pos-in-match-range pos 5)) 'day)
  13919. ((and (> pos (or (match-end 8) (match-end 5)))
  13920. (< pos (match-end 0)))
  13921. (- pos (or (match-end 8) (match-end 5))))
  13922. (t 'day))))
  13923. ans))
  13924. (defun org-toggle-timestamp-type ()
  13925. "Toggle the type (<active> or [inactive]) of a time stamp."
  13926. (interactive)
  13927. (when (org-at-timestamp-p t)
  13928. (let ((beg (match-beginning 0)) (end (match-end 0))
  13929. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13930. (save-excursion
  13931. (goto-char beg)
  13932. (while (re-search-forward "[][<>]" end t)
  13933. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13934. t t)))
  13935. (message "Timestamp is now %sactive"
  13936. (if (equal (char-after beg) ?<) "" "in")))))
  13937. (defun org-timestamp-change (n &optional what updown)
  13938. "Change the date in the time stamp at point.
  13939. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13940. `year', `minute', `second'. If WHAT is not given, the cursor position
  13941. in the timestamp determines what will be changed."
  13942. (let ((pos (point))
  13943. with-hm inactive
  13944. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13945. org-ts-what
  13946. extra rem
  13947. ts time time0)
  13948. (if (not (org-at-timestamp-p t))
  13949. (error "Not at a timestamp"))
  13950. (if (and (not what) (eq org-ts-what 'bracket))
  13951. (org-toggle-timestamp-type)
  13952. (if (and (not what) (not (eq org-ts-what 'day))
  13953. org-display-custom-times
  13954. (get-text-property (point) 'display)
  13955. (not (get-text-property (1- (point)) 'display)))
  13956. (setq org-ts-what 'day))
  13957. (setq org-ts-what (or what org-ts-what)
  13958. inactive (= (char-after (match-beginning 0)) ?\[)
  13959. ts (match-string 0))
  13960. (replace-match "")
  13961. (if (string-match
  13962. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13963. ts)
  13964. (setq extra (match-string 1 ts)))
  13965. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13966. (setq with-hm t))
  13967. (setq time0 (org-parse-time-string ts))
  13968. (when (and updown
  13969. (eq org-ts-what 'minute)
  13970. (not current-prefix-arg))
  13971. ;; This looks like s-up and s-down. Change by one rounding step.
  13972. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13973. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13974. (setcar (cdr time0) (+ (nth 1 time0)
  13975. (if (> n 0) (- rem) (- dm rem))))))
  13976. (setq time
  13977. (encode-time (or (car time0) 0)
  13978. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13979. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13980. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13981. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13982. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13983. (nthcdr 6 time0)))
  13984. (when (and (member org-ts-what '(hour minute))
  13985. extra
  13986. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13987. (setq extra (org-modify-ts-extra
  13988. extra
  13989. (if (eq org-ts-what 'hour) 2 5)
  13990. n dm)))
  13991. (when (integerp org-ts-what)
  13992. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13993. (if (eq what 'calendar)
  13994. (let ((cal-date (org-get-date-from-calendar)))
  13995. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13996. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13997. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13998. (setcar time0 (or (car time0) 0))
  13999. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14000. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14001. (setq time (apply 'encode-time time0))))
  14002. (setq org-last-changed-timestamp
  14003. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14004. (org-clock-update-time-maybe)
  14005. (goto-char pos)
  14006. ;; Try to recenter the calendar window, if any
  14007. (if (and org-calendar-follow-timestamp-change
  14008. (get-buffer-window "*Calendar*" t)
  14009. (memq org-ts-what '(day month year)))
  14010. (org-recenter-calendar (time-to-days time))))))
  14011. (defun org-modify-ts-extra (s pos n dm)
  14012. "Change the different parts of the lead-time and repeat fields in timestamp."
  14013. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14014. ng h m new rem)
  14015. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14016. (cond
  14017. ((or (org-pos-in-match-range pos 2)
  14018. (org-pos-in-match-range pos 3))
  14019. (setq m (string-to-number (match-string 3 s))
  14020. h (string-to-number (match-string 2 s)))
  14021. (if (org-pos-in-match-range pos 2)
  14022. (setq h (+ h n))
  14023. (setq n (* dm (org-no-warnings (signum n))))
  14024. (when (not (= 0 (setq rem (% m dm))))
  14025. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14026. (setq m (+ m n)))
  14027. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14028. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14029. (setq h (min 24 (max 0 h)))
  14030. (setq ng 1 new (format "-%02d:%02d" h m)))
  14031. ((org-pos-in-match-range pos 6)
  14032. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14033. ((org-pos-in-match-range pos 5)
  14034. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14035. ((org-pos-in-match-range pos 9)
  14036. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14037. ((org-pos-in-match-range pos 8)
  14038. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14039. (when ng
  14040. (setq s (concat
  14041. (substring s 0 (match-beginning ng))
  14042. new
  14043. (substring s (match-end ng))))))
  14044. s))
  14045. (defun org-recenter-calendar (date)
  14046. "If the calendar is visible, recenter it to DATE."
  14047. (let* ((win (selected-window))
  14048. (cwin (get-buffer-window "*Calendar*" t))
  14049. (calendar-move-hook nil))
  14050. (when cwin
  14051. (select-window cwin)
  14052. (calendar-goto-date (if (listp date) date
  14053. (calendar-gregorian-from-absolute date)))
  14054. (select-window win))))
  14055. (defun org-goto-calendar (&optional arg)
  14056. "Go to the Emacs calendar at the current date.
  14057. If there is a time stamp in the current line, go to that date.
  14058. A prefix ARG can be used to force the current date."
  14059. (interactive "P")
  14060. (let ((tsr org-ts-regexp) diff
  14061. (calendar-move-hook nil)
  14062. (calendar-view-holidays-initially-flag nil)
  14063. (calendar-view-diary-initially-flag nil))
  14064. (if (or (org-at-timestamp-p)
  14065. (save-excursion
  14066. (beginning-of-line 1)
  14067. (looking-at (concat ".*" tsr))))
  14068. (let ((d1 (time-to-days (current-time)))
  14069. (d2 (time-to-days
  14070. (org-time-string-to-time (match-string 1)))))
  14071. (setq diff (- d2 d1))))
  14072. (calendar)
  14073. (calendar-goto-today)
  14074. (if (and diff (not arg)) (calendar-forward-day diff))))
  14075. (defun org-get-date-from-calendar ()
  14076. "Return a list (month day year) of date at point in calendar."
  14077. (with-current-buffer "*Calendar*"
  14078. (save-match-data
  14079. (calendar-cursor-to-date))))
  14080. (defun org-date-from-calendar ()
  14081. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14082. If there is already a time stamp at the cursor position, update it."
  14083. (interactive)
  14084. (if (org-at-timestamp-p t)
  14085. (org-timestamp-change 0 'calendar)
  14086. (let ((cal-date (org-get-date-from-calendar)))
  14087. (org-insert-time-stamp
  14088. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14089. (defun org-minutes-to-hh:mm-string (m)
  14090. "Compute H:MM from a number of minutes."
  14091. (let ((h (/ m 60)))
  14092. (setq m (- m (* 60 h)))
  14093. (format org-time-clocksum-format h m)))
  14094. (defun org-hh:mm-string-to-minutes (s)
  14095. "Convert a string H:MM to a number of minutes.
  14096. If the string is just a number, interpret it as minutes.
  14097. In fact, the first hh:mm or number in the string will be taken,
  14098. there can be extra stuff in the string.
  14099. If no number is found, the return value is 0."
  14100. (cond
  14101. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14102. (+ (* (string-to-number (match-string 1 s)) 60)
  14103. (string-to-number (match-string 2 s))))
  14104. ((string-match "\\([0-9]+\\)" s)
  14105. (string-to-number (match-string 1 s)))
  14106. (t 0)))
  14107. ;;;; Files
  14108. (defun org-save-all-org-buffers ()
  14109. "Save all Org-mode buffers without user confirmation."
  14110. (interactive)
  14111. (message "Saving all Org-mode buffers...")
  14112. (save-some-buffers t 'org-mode-p)
  14113. (when (featurep 'org-id) (org-id-locations-save))
  14114. (message "Saving all Org-mode buffers... done"))
  14115. (defun org-revert-all-org-buffers ()
  14116. "Revert all Org-mode buffers.
  14117. Prompt for confirmation when there are unsaved changes.
  14118. Be sure you know what you are doing before letting this function
  14119. overwrite your changes.
  14120. This function is useful in a setup where one tracks org files
  14121. with a version control system, to revert on one machine after pulling
  14122. changes from another. I believe the procedure must be like this:
  14123. 1. M-x org-save-all-org-buffers
  14124. 2. Pull changes from the other machine, resolve conflicts
  14125. 3. M-x org-revert-all-org-buffers"
  14126. (interactive)
  14127. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14128. (error "Abort"))
  14129. (save-excursion
  14130. (save-window-excursion
  14131. (mapc
  14132. (lambda (b)
  14133. (when (and (with-current-buffer b (org-mode-p))
  14134. (with-current-buffer b buffer-file-name))
  14135. (switch-to-buffer b)
  14136. (revert-buffer t 'no-confirm)))
  14137. (buffer-list))
  14138. (when (and (featurep 'org-id) org-id-track-globally)
  14139. (org-id-locations-load)))))
  14140. ;;;; Agenda files
  14141. ;;;###autoload
  14142. (defun org-switchb (&optional arg)
  14143. "Switch between Org buffers.
  14144. With a prefix argument, restrict available to files.
  14145. With two prefix arguments, restrict available buffers to agenda files.
  14146. Defaults to `iswitchb' for buffer name completion.
  14147. Set `org-completion-use-ido' to make it use ido instead."
  14148. (interactive "P")
  14149. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14150. ((equal arg '(16)) (org-buffer-list 'agenda))
  14151. (t (org-buffer-list))))
  14152. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14153. (org-completion-use-ido org-completion-use-ido))
  14154. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14155. (setq org-completion-use-iswitchb t))
  14156. (switch-to-buffer
  14157. (org-icompleting-read "Org buffer: "
  14158. (mapcar 'list (mapcar 'buffer-name blist))
  14159. nil t))))
  14160. ;;; Define some older names previously used for this functionality
  14161. ;;;###autoload
  14162. (defalias 'org-ido-switchb 'org-switchb)
  14163. ;;;###autoload
  14164. (defalias 'org-iswitchb 'org-switchb)
  14165. (defun org-buffer-list (&optional predicate exclude-tmp)
  14166. "Return a list of Org buffers.
  14167. PREDICATE can be `export', `files' or `agenda'.
  14168. export restrict the list to Export buffers.
  14169. files restrict the list to buffers visiting Org files.
  14170. agenda restrict the list to buffers visiting agenda files.
  14171. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14172. (let* ((bfn nil)
  14173. (agenda-files (and (eq predicate 'agenda)
  14174. (mapcar 'file-truename (org-agenda-files t))))
  14175. (filter
  14176. (cond
  14177. ((eq predicate 'files)
  14178. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14179. ((eq predicate 'export)
  14180. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14181. ((eq predicate 'agenda)
  14182. (lambda (b)
  14183. (with-current-buffer b
  14184. (and (eq major-mode 'org-mode)
  14185. (setq bfn (buffer-file-name b))
  14186. (member (file-truename bfn) agenda-files)))))
  14187. (t (lambda (b) (with-current-buffer b
  14188. (or (eq major-mode 'org-mode)
  14189. (string-match "\*Org .*Export"
  14190. (buffer-name b)))))))))
  14191. (delq nil
  14192. (mapcar
  14193. (lambda(b)
  14194. (if (and (funcall filter b)
  14195. (or (not exclude-tmp)
  14196. (not (string-match "tmp" (buffer-name b)))))
  14197. b
  14198. nil))
  14199. (buffer-list)))))
  14200. (defun org-agenda-files (&optional unrestricted archives)
  14201. "Get the list of agenda files.
  14202. Optional UNRESTRICTED means return the full list even if a restriction
  14203. is currently in place.
  14204. When ARCHIVES is t, include all archive files that are really being
  14205. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14206. `org-agenda-archives-mode' is t."
  14207. (let ((files
  14208. (cond
  14209. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14210. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14211. ((listp org-agenda-files) org-agenda-files)
  14212. (t (error "Invalid value of `org-agenda-files'")))))
  14213. (setq files (apply 'append
  14214. (mapcar (lambda (f)
  14215. (if (file-directory-p f)
  14216. (directory-files
  14217. f t org-agenda-file-regexp)
  14218. (list f)))
  14219. files)))
  14220. (when org-agenda-skip-unavailable-files
  14221. (setq files (delq nil
  14222. (mapcar (function
  14223. (lambda (file)
  14224. (and (file-readable-p file) file)))
  14225. files))))
  14226. (when (or (eq archives t)
  14227. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14228. (setq files (org-add-archive-files files)))
  14229. files))
  14230. (defun org-agenda-file-p (&optional file)
  14231. "Return non-nil, if FILE is an agenda file.
  14232. If FILE is omitted, use the file associated with the current
  14233. buffer."
  14234. (member (or file (buffer-file-name))
  14235. (org-agenda-files t)))
  14236. (defun org-edit-agenda-file-list ()
  14237. "Edit the list of agenda files.
  14238. Depending on setup, this either uses customize to edit the variable
  14239. `org-agenda-files', or it visits the file that is holding the list. In the
  14240. latter case, the buffer is set up in a way that saving it automatically kills
  14241. the buffer and restores the previous window configuration."
  14242. (interactive)
  14243. (if (stringp org-agenda-files)
  14244. (let ((cw (current-window-configuration)))
  14245. (find-file org-agenda-files)
  14246. (org-set-local 'org-window-configuration cw)
  14247. (org-add-hook 'after-save-hook
  14248. (lambda ()
  14249. (set-window-configuration
  14250. (prog1 org-window-configuration
  14251. (kill-buffer (current-buffer))))
  14252. (org-install-agenda-files-menu)
  14253. (message "New agenda file list installed"))
  14254. nil 'local)
  14255. (message "%s" (substitute-command-keys
  14256. "Edit list and finish with \\[save-buffer]")))
  14257. (customize-variable 'org-agenda-files)))
  14258. (defun org-store-new-agenda-file-list (list)
  14259. "Set new value for the agenda file list and save it correctly."
  14260. (if (stringp org-agenda-files)
  14261. (let ((fe (org-read-agenda-file-list t)) b u)
  14262. (while (setq b (find-buffer-visiting org-agenda-files))
  14263. (kill-buffer b))
  14264. (with-temp-file org-agenda-files
  14265. (insert
  14266. (mapconcat
  14267. (lambda (f) ;; Keep un-expanded entries.
  14268. (if (setq u (assoc f fe))
  14269. (cdr u)
  14270. f))
  14271. list "\n")
  14272. "\n")))
  14273. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14274. (org-insert-mode-line-in-empty-file nil))
  14275. (setq org-agenda-files list)
  14276. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14277. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14278. "Read the list of agenda files from a file.
  14279. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14280. filenames, used by `org-store-new-agenda-file-list' to write back
  14281. un-expanded file names."
  14282. (when (file-directory-p org-agenda-files)
  14283. (error "`org-agenda-files' cannot be a single directory"))
  14284. (when (stringp org-agenda-files)
  14285. (with-temp-buffer
  14286. (insert-file-contents org-agenda-files)
  14287. (mapcar
  14288. (lambda (f)
  14289. (let ((e (expand-file-name (substitute-in-file-name f)
  14290. org-directory)))
  14291. (if pair-with-expansion
  14292. (cons e f)
  14293. e)))
  14294. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14295. ;;;###autoload
  14296. (defun org-cycle-agenda-files ()
  14297. "Cycle through the files in `org-agenda-files'.
  14298. If the current buffer visits an agenda file, find the next one in the list.
  14299. If the current buffer does not, find the first agenda file."
  14300. (interactive)
  14301. (let* ((fs (org-agenda-files t))
  14302. (files (append fs (list (car fs))))
  14303. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14304. file)
  14305. (unless files (error "No agenda files"))
  14306. (catch 'exit
  14307. (while (setq file (pop files))
  14308. (if (equal (file-truename file) tcf)
  14309. (when (car files)
  14310. (find-file (car files))
  14311. (throw 'exit t))))
  14312. (find-file (car fs)))
  14313. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14314. (defun org-agenda-file-to-front (&optional to-end)
  14315. "Move/add the current file to the top of the agenda file list.
  14316. If the file is not present in the list, it is added to the front. If it is
  14317. present, it is moved there. With optional argument TO-END, add/move to the
  14318. end of the list."
  14319. (interactive "P")
  14320. (let ((org-agenda-skip-unavailable-files nil)
  14321. (file-alist (mapcar (lambda (x)
  14322. (cons (file-truename x) x))
  14323. (org-agenda-files t)))
  14324. (ctf (file-truename buffer-file-name))
  14325. x had)
  14326. (setq x (assoc ctf file-alist) had x)
  14327. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14328. (if to-end
  14329. (setq file-alist (append (delq x file-alist) (list x)))
  14330. (setq file-alist (cons x (delq x file-alist))))
  14331. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14332. (org-install-agenda-files-menu)
  14333. (message "File %s to %s of agenda file list"
  14334. (if had "moved" "added") (if to-end "end" "front"))))
  14335. (defun org-remove-file (&optional file)
  14336. "Remove current file from the list of files in variable `org-agenda-files'.
  14337. These are the files which are being checked for agenda entries.
  14338. Optional argument FILE means use this file instead of the current."
  14339. (interactive)
  14340. (let* ((org-agenda-skip-unavailable-files nil)
  14341. (file (or file buffer-file-name))
  14342. (true-file (file-truename file))
  14343. (afile (abbreviate-file-name file))
  14344. (files (delq nil (mapcar
  14345. (lambda (x)
  14346. (if (equal true-file
  14347. (file-truename x))
  14348. nil x))
  14349. (org-agenda-files t)))))
  14350. (if (not (= (length files) (length (org-agenda-files t))))
  14351. (progn
  14352. (org-store-new-agenda-file-list files)
  14353. (org-install-agenda-files-menu)
  14354. (message "Removed file: %s" afile))
  14355. (message "File was not in list: %s (not removed)" afile))))
  14356. (defun org-file-menu-entry (file)
  14357. (vector file (list 'find-file file) t))
  14358. (defun org-check-agenda-file (file)
  14359. "Make sure FILE exists. If not, ask user what to do."
  14360. (when (not (file-exists-p file))
  14361. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14362. (abbreviate-file-name file))
  14363. (let ((r (downcase (read-char-exclusive))))
  14364. (cond
  14365. ((equal r ?r)
  14366. (org-remove-file file)
  14367. (throw 'nextfile t))
  14368. (t (error "Abort"))))))
  14369. (defun org-get-agenda-file-buffer (file)
  14370. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14371. it to the list of buffers which might be released later."
  14372. (let ((buf (org-find-base-buffer-visiting file)))
  14373. (if buf
  14374. buf ; just return it
  14375. ;; Make a new buffer and remember it
  14376. (setq buf (find-file-noselect file))
  14377. (if buf (push buf org-agenda-new-buffers))
  14378. buf)))
  14379. (defun org-release-buffers (blist)
  14380. "Release all buffers in list, asking the user for confirmation when needed.
  14381. When a buffer is unmodified, it is just killed. When modified, it is saved
  14382. \(if the user agrees) and then killed."
  14383. (let (buf file)
  14384. (while (setq buf (pop blist))
  14385. (setq file (buffer-file-name buf))
  14386. (when (and (buffer-modified-p buf)
  14387. file
  14388. (y-or-n-p (format "Save file %s? " file)))
  14389. (with-current-buffer buf (save-buffer)))
  14390. (kill-buffer buf))))
  14391. (defun org-prepare-agenda-buffers (files)
  14392. "Create buffers for all agenda files, protect archived trees and comments."
  14393. (interactive)
  14394. (let ((pa '(:org-archived t))
  14395. (pc '(:org-comment t))
  14396. (pall '(:org-archived t :org-comment t))
  14397. (inhibit-read-only t)
  14398. (rea (concat ":" org-archive-tag ":"))
  14399. bmp file re)
  14400. (save-excursion
  14401. (save-restriction
  14402. (while (setq file (pop files))
  14403. (catch 'nextfile
  14404. (if (bufferp file)
  14405. (set-buffer file)
  14406. (org-check-agenda-file file)
  14407. (set-buffer (org-get-agenda-file-buffer file)))
  14408. (widen)
  14409. (setq bmp (buffer-modified-p))
  14410. (org-refresh-category-properties)
  14411. (setq org-todo-keywords-for-agenda
  14412. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14413. (setq org-done-keywords-for-agenda
  14414. (append org-done-keywords-for-agenda org-done-keywords))
  14415. (setq org-todo-keyword-alist-for-agenda
  14416. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14417. (setq org-drawers-for-agenda
  14418. (append org-drawers-for-agenda org-drawers))
  14419. (setq org-tag-alist-for-agenda
  14420. (append org-tag-alist-for-agenda org-tag-alist))
  14421. (save-excursion
  14422. (remove-text-properties (point-min) (point-max) pall)
  14423. (when org-agenda-skip-archived-trees
  14424. (goto-char (point-min))
  14425. (while (re-search-forward rea nil t)
  14426. (if (org-on-heading-p t)
  14427. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14428. (goto-char (point-min))
  14429. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14430. (while (re-search-forward re nil t)
  14431. (add-text-properties
  14432. (match-beginning 0) (org-end-of-subtree t) pc)))
  14433. (set-buffer-modified-p bmp)))))
  14434. (setq org-todo-keywords-for-agenda
  14435. (org-uniquify org-todo-keywords-for-agenda))
  14436. (setq org-todo-keyword-alist-for-agenda
  14437. (org-uniquify org-todo-keyword-alist-for-agenda)
  14438. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14439. ;;;; Embedded LaTeX
  14440. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14441. "Keymap for the minor `org-cdlatex-mode'.")
  14442. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14443. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14444. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14445. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14446. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14447. (defvar org-cdlatex-texmathp-advice-is-done nil
  14448. "Flag remembering if we have applied the advice to texmathp already.")
  14449. (define-minor-mode org-cdlatex-mode
  14450. "Toggle the minor `org-cdlatex-mode'.
  14451. This mode supports entering LaTeX environment and math in LaTeX fragments
  14452. in Org-mode.
  14453. \\{org-cdlatex-mode-map}"
  14454. nil " OCDL" nil
  14455. (when org-cdlatex-mode (require 'cdlatex))
  14456. (unless org-cdlatex-texmathp-advice-is-done
  14457. (setq org-cdlatex-texmathp-advice-is-done t)
  14458. (defadvice texmathp (around org-math-always-on activate)
  14459. "Always return t in org-mode buffers.
  14460. This is because we want to insert math symbols without dollars even outside
  14461. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14462. an embedded LaTeX fragment, let texmathp do its job.
  14463. \\[org-cdlatex-mode-map]"
  14464. (interactive)
  14465. (let (p)
  14466. (cond
  14467. ((not (org-mode-p)) ad-do-it)
  14468. ((eq this-command 'cdlatex-math-symbol)
  14469. (setq ad-return-value t
  14470. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14471. (t
  14472. (let ((p (org-inside-LaTeX-fragment-p)))
  14473. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14474. (setq ad-return-value t
  14475. texmathp-why '("Org-mode embedded math" . 0))
  14476. (if p ad-do-it)))))))))
  14477. (defun turn-on-org-cdlatex ()
  14478. "Unconditionally turn on `org-cdlatex-mode'."
  14479. (org-cdlatex-mode 1))
  14480. (defun org-inside-LaTeX-fragment-p ()
  14481. "Test if point is inside a LaTeX fragment.
  14482. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14483. sequence appearing also before point.
  14484. Even though the matchers for math are configurable, this function assumes
  14485. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14486. delimiters are skipped when they have been removed by customization.
  14487. The return value is nil, or a cons cell with the delimiter and
  14488. and the position of this delimiter.
  14489. This function does a reasonably good job, but can locally be fooled by
  14490. for example currency specifications. For example it will assume being in
  14491. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14492. fragments that are properly closed, but during editing, we have to live
  14493. with the uncertainty caused by missing closing delimiters. This function
  14494. looks only before point, not after."
  14495. (catch 'exit
  14496. (let ((pos (point))
  14497. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14498. (lim (progn
  14499. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14500. (point)))
  14501. dd-on str (start 0) m re)
  14502. (goto-char pos)
  14503. (when dodollar
  14504. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14505. re (nth 1 (assoc "$" org-latex-regexps)))
  14506. (while (string-match re str start)
  14507. (cond
  14508. ((= (match-end 0) (length str))
  14509. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14510. ((= (match-end 0) (- (length str) 5))
  14511. (throw 'exit nil))
  14512. (t (setq start (match-end 0))))))
  14513. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14514. (goto-char pos)
  14515. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14516. (and (match-beginning 2) (throw 'exit nil))
  14517. ;; count $$
  14518. (while (re-search-backward "\\$\\$" lim t)
  14519. (setq dd-on (not dd-on)))
  14520. (goto-char pos)
  14521. (if dd-on (cons "$$" m))))))
  14522. (defun org-inside-latex-macro-p ()
  14523. "Is point inside a LaTeX macro or its arguments?"
  14524. (save-match-data
  14525. (org-in-regexp
  14526. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14527. (defun org-try-cdlatex-tab ()
  14528. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14529. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14530. - inside a LaTeX fragment, or
  14531. - after the first word in a line, where an abbreviation expansion could
  14532. insert a LaTeX environment."
  14533. (when org-cdlatex-mode
  14534. (cond
  14535. ((save-excursion
  14536. (skip-chars-backward "a-zA-Z0-9*")
  14537. (skip-chars-backward " \t")
  14538. (bolp))
  14539. (cdlatex-tab) t)
  14540. ((org-inside-LaTeX-fragment-p)
  14541. (cdlatex-tab) t)
  14542. (t nil))))
  14543. (defun org-cdlatex-underscore-caret (&optional arg)
  14544. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14545. Revert to the normal definition outside of these fragments."
  14546. (interactive "P")
  14547. (if (org-inside-LaTeX-fragment-p)
  14548. (call-interactively 'cdlatex-sub-superscript)
  14549. (let (org-cdlatex-mode)
  14550. (call-interactively (key-binding (vector last-input-event))))))
  14551. (defun org-cdlatex-math-modify (&optional arg)
  14552. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14553. Revert to the normal definition outside of these fragments."
  14554. (interactive "P")
  14555. (if (org-inside-LaTeX-fragment-p)
  14556. (call-interactively 'cdlatex-math-modify)
  14557. (let (org-cdlatex-mode)
  14558. (call-interactively (key-binding (vector last-input-event))))))
  14559. (defvar org-latex-fragment-image-overlays nil
  14560. "List of overlays carrying the images of latex fragments.")
  14561. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14562. (defun org-remove-latex-fragment-image-overlays ()
  14563. "Remove all overlays with LaTeX fragment images in current buffer."
  14564. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14565. (setq org-latex-fragment-image-overlays nil))
  14566. (defun org-preview-latex-fragment (&optional subtree)
  14567. "Preview the LaTeX fragment at point, or all locally or globally.
  14568. If the cursor is in a LaTeX fragment, create the image and overlay
  14569. it over the source code. If there is no fragment at point, display
  14570. all fragments in the current text, from one headline to the next. With
  14571. prefix SUBTREE, display all fragments in the current subtree. With a
  14572. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14573. the cursor is before the first headline,
  14574. display all fragments in the buffer.
  14575. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14576. (interactive "P")
  14577. (org-remove-latex-fragment-image-overlays)
  14578. (save-excursion
  14579. (save-restriction
  14580. (let (beg end at msg)
  14581. (cond
  14582. ((or (equal subtree '(16))
  14583. (not (save-excursion
  14584. (re-search-backward (concat "^" outline-regexp) nil t))))
  14585. (setq beg (point-min) end (point-max)
  14586. msg "Creating images for buffer...%s"))
  14587. ((equal subtree '(4))
  14588. (org-back-to-heading)
  14589. (setq beg (point) end (org-end-of-subtree t)
  14590. msg "Creating images for subtree...%s"))
  14591. (t
  14592. (if (setq at (org-inside-LaTeX-fragment-p))
  14593. (goto-char (max (point-min) (- (cdr at) 2)))
  14594. (org-back-to-heading))
  14595. (setq beg (point) end (progn (outline-next-heading) (point))
  14596. msg (if at "Creating image...%s"
  14597. "Creating images for entry...%s"))))
  14598. (message msg "")
  14599. (narrow-to-region beg end)
  14600. (goto-char beg)
  14601. (org-format-latex
  14602. (concat "ltxpng/" (file-name-sans-extension
  14603. (file-name-nondirectory
  14604. buffer-file-name)))
  14605. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14606. (message msg "done. Use `C-c C-c' to remove images.")))))
  14607. (defvar org-latex-regexps
  14608. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14609. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14610. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14611. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14612. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14613. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14614. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14615. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14616. "Regular expressions for matching embedded LaTeX.")
  14617. (defvar org-export-have-math nil) ;; dynamic scoping
  14618. (defun org-format-latex (prefix &optional dir overlays msg at
  14619. forbuffer processing-type)
  14620. "Replace LaTeX fragments with links to an image, and produce images.
  14621. Some of the options can be changed using the variable
  14622. `org-format-latex-options'."
  14623. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14624. (let* ((prefixnodir (file-name-nondirectory prefix))
  14625. (absprefix (expand-file-name prefix dir))
  14626. (todir (file-name-directory absprefix))
  14627. (opt org-format-latex-options)
  14628. (matchers (plist-get opt :matchers))
  14629. (re-list org-latex-regexps)
  14630. (org-format-latex-header-extra
  14631. (plist-get (org-infile-export-plist) :latex-header-extra))
  14632. (cnt 0) txt hash link beg end re e checkdir
  14633. executables-checked string
  14634. m n block linkfile movefile ov)
  14635. ;; Check the different regular expressions
  14636. (while (setq e (pop re-list))
  14637. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14638. block (if (nth 3 e) "\n\n" ""))
  14639. (when (member m matchers)
  14640. (goto-char (point-min))
  14641. (while (re-search-forward re nil t)
  14642. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14643. (not (get-text-property (match-beginning n)
  14644. 'org-protected))
  14645. (or (not overlays)
  14646. (not (eq (get-char-property (match-beginning n)
  14647. 'org-overlay-type)
  14648. 'org-latex-overlay))))
  14649. (setq org-export-have-math t)
  14650. (cond
  14651. ((eq processing-type 'verbatim)
  14652. ;; Leave the text verbatim, just protect it
  14653. (add-text-properties (match-beginning n) (match-end n)
  14654. '(org-protected t)))
  14655. ((eq processing-type 'mathjax)
  14656. ;; Prepare for MathJax processing
  14657. (setq string (match-string n))
  14658. (if (member m '("$" "$1"))
  14659. (save-excursion
  14660. (delete-region (match-beginning n) (match-end n))
  14661. (goto-char (match-beginning n))
  14662. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14663. "\\)")
  14664. '(org-protected t))))
  14665. (add-text-properties (match-beginning n) (match-end n)
  14666. '(org-protected t))))
  14667. ((or (eq processing-type 'dvipng) t)
  14668. ;; Process to an image
  14669. (setq txt (match-string n)
  14670. beg (match-beginning n) end (match-end n)
  14671. cnt (1+ cnt))
  14672. (let (print-length print-level) ; make sure full list is printed
  14673. (setq hash (sha1 (prin1-to-string
  14674. (list org-format-latex-header
  14675. org-format-latex-header-extra
  14676. org-export-latex-default-packages-alist
  14677. org-export-latex-packages-alist
  14678. org-format-latex-options
  14679. forbuffer txt)))
  14680. linkfile (format "%s_%s.png" prefix hash)
  14681. movefile (format "%s_%s.png" absprefix hash)))
  14682. (setq link (concat block "[[file:" linkfile "]]" block))
  14683. (if msg (message msg cnt))
  14684. (goto-char beg)
  14685. (unless checkdir ; make sure the directory exists
  14686. (setq checkdir t)
  14687. (or (file-directory-p todir) (make-directory todir t)))
  14688. (unless executables-checked
  14689. (org-check-external-command
  14690. "latex" "needed to convert LaTeX fragments to images")
  14691. (org-check-external-command
  14692. "dvipng" "needed to convert LaTeX fragments to images")
  14693. (setq executables-checked t))
  14694. (unless (file-exists-p movefile)
  14695. (org-create-formula-image
  14696. txt movefile opt forbuffer))
  14697. (if overlays
  14698. (progn
  14699. (mapc (lambda (o)
  14700. (if (eq (overlay-get o 'org-overlay-type)
  14701. 'org-latex-overlay)
  14702. (delete-overlay o)))
  14703. (overlays-in beg end))
  14704. (setq ov (make-overlay beg end))
  14705. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14706. (if (featurep 'xemacs)
  14707. (progn
  14708. (overlay-put ov 'invisible t)
  14709. (overlay-put
  14710. ov 'end-glyph
  14711. (make-glyph (vector 'png :file movefile))))
  14712. (overlay-put
  14713. ov 'display
  14714. (list 'image :type 'png :file movefile :ascent 'center)))
  14715. (push ov org-latex-fragment-image-overlays)
  14716. (goto-char end))
  14717. (delete-region beg end)
  14718. (insert (org-add-props link
  14719. (list 'org-latex-src
  14720. (replace-regexp-in-string
  14721. "\"" "" txt)))))))))))))
  14722. ;; This function borrows from Ganesh Swami's latex2png.el
  14723. (defun org-create-formula-image (string tofile options buffer)
  14724. "This calls dvipng."
  14725. (require 'org-latex)
  14726. (let* ((tmpdir (if (featurep 'xemacs)
  14727. (temp-directory)
  14728. temporary-file-directory))
  14729. (texfilebase (make-temp-name
  14730. (expand-file-name "orgtex" tmpdir)))
  14731. (texfile (concat texfilebase ".tex"))
  14732. (dvifile (concat texfilebase ".dvi"))
  14733. (pngfile (concat texfilebase ".png"))
  14734. (fnh (if (featurep 'xemacs)
  14735. (font-height (get-face-font 'default))
  14736. (face-attribute 'default :height nil)))
  14737. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14738. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14739. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14740. "Black"))
  14741. (bg (or (plist-get options (if buffer :background :html-background))
  14742. "Transparent")))
  14743. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14744. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14745. (with-temp-file texfile
  14746. (insert (org-splice-latex-header
  14747. org-format-latex-header
  14748. org-export-latex-default-packages-alist
  14749. org-export-latex-packages-alist t
  14750. org-format-latex-header-extra))
  14751. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14752. (require 'org-latex)
  14753. (org-export-latex-fix-inputenc))
  14754. (let ((dir default-directory))
  14755. (condition-case nil
  14756. (progn
  14757. (cd tmpdir)
  14758. (call-process "latex" nil nil nil texfile))
  14759. (error nil))
  14760. (cd dir))
  14761. (if (not (file-exists-p dvifile))
  14762. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14763. (condition-case nil
  14764. (call-process "dvipng" nil nil nil
  14765. "-fg" fg "-bg" bg
  14766. "-D" dpi
  14767. ;;"-x" scale "-y" scale
  14768. "-T" "tight"
  14769. "-o" pngfile
  14770. dvifile)
  14771. (error nil))
  14772. (if (not (file-exists-p pngfile))
  14773. (if org-format-latex-signal-error
  14774. (error "Failed to create png file from %s" texfile)
  14775. (message "Failed to create png file from %s" texfile)
  14776. nil)
  14777. ;; Use the requested file name and clean up
  14778. (copy-file pngfile tofile 'replace)
  14779. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14780. (delete-file (concat texfilebase e)))
  14781. pngfile))))
  14782. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14783. "Fill a LaTeX header template TPL.
  14784. In the template, the following place holders will be recognized:
  14785. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14786. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14787. [PACKAGES] \\usepackage statements for PKG
  14788. [NO-PACKAGES] do not include PKG
  14789. [EXTRA] the string EXTRA
  14790. [NO-EXTRA] do not include EXTRA
  14791. For backward compatibility, if both the positive and the negative place
  14792. holder is missing, the positive one (without the \"NO-\") will be
  14793. assumed to be present at the end of the template.
  14794. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14795. EXTRA is a string.
  14796. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14797. (let (rpl (end ""))
  14798. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14799. (setq rpl (if (or (match-end 1) (not def-pkg))
  14800. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14801. tpl (replace-match rpl t t tpl))
  14802. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14803. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14804. (setq rpl (if (or (match-end 1) (not pkg))
  14805. "" (org-latex-packages-to-string pkg snippets-p t))
  14806. tpl (replace-match rpl t t tpl))
  14807. (if pkg (setq end
  14808. (concat end "\n"
  14809. (org-latex-packages-to-string pkg snippets-p)))))
  14810. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14811. (setq rpl (if (or (match-end 1) (not extra))
  14812. "" (concat extra "\n"))
  14813. tpl (replace-match rpl t t tpl))
  14814. (if (and extra (string-match "\\S-" extra))
  14815. (setq end (concat end "\n" extra))))
  14816. (if (string-match "\\S-" end)
  14817. (concat tpl "\n" end)
  14818. tpl)))
  14819. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14820. "Turn an alist of packages into a string with the \\usepackage macros."
  14821. (setq pkg (mapconcat (lambda(p)
  14822. (cond
  14823. ((stringp p) p)
  14824. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14825. (format "%% Package %s omitted" (cadr p)))
  14826. ((equal "" (car p))
  14827. (format "\\usepackage{%s}" (cadr p)))
  14828. (t
  14829. (format "\\usepackage[%s]{%s}"
  14830. (car p) (cadr p)))))
  14831. pkg
  14832. "\n"))
  14833. (if newline (concat pkg "\n") pkg))
  14834. (defun org-dvipng-color (attr)
  14835. "Return an rgb color specification for dvipng."
  14836. (apply 'format "rgb %s %s %s"
  14837. (mapcar 'org-normalize-color
  14838. (color-values (face-attribute 'default attr nil)))))
  14839. (defun org-normalize-color (value)
  14840. "Return string to be used as color value for an RGB component."
  14841. (format "%g" (/ value 65535.0)))
  14842. ;; Image display
  14843. (defvar org-inline-image-overlays nil)
  14844. (make-variable-buffer-local 'org-inline-image-overlays)
  14845. (defun org-toggle-inline-images (&optional include-linked)
  14846. "Toggle the display of inline images.
  14847. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14848. (interactive "P")
  14849. (if org-inline-image-overlays
  14850. (progn
  14851. (org-remove-inline-images)
  14852. (message "Inline image display turned off"))
  14853. (org-display-inline-images include-linked)
  14854. (if org-inline-image-overlays
  14855. (message "%d images displayed inline"
  14856. (length org-inline-image-overlays))
  14857. (message "No images to display inline"))))
  14858. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14859. "Display inline images.
  14860. Normally only links without a description part are inlined, because this
  14861. is how it will work for export. When INCLUDE-LINKED is set, also links
  14862. with a description part will be inlined. This can be nice for a quick
  14863. look at those images, but it does not reflect what exported files will look
  14864. like.
  14865. When REFRESH is set, refresh existing images between BEG and END.
  14866. This will create new image displays only if necessary.
  14867. BEG and END default to the buffer boundaries."
  14868. (interactive "P")
  14869. (unless refresh
  14870. (org-remove-inline-images)
  14871. (clear-image-cache))
  14872. (save-excursion
  14873. (save-restriction
  14874. (widen)
  14875. (setq beg (or beg (point-min)) end (or end (point-max)))
  14876. (goto-char (point-min))
  14877. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~.:/\\_0-9a-zA-Z ]+"
  14878. (substring (org-image-file-name-regexp) 0 -2)
  14879. "\\)\\]" (if include-linked "" "\\]")))
  14880. old file ov img)
  14881. (while (re-search-forward re end t)
  14882. (setq old (get-char-property-and-overlay (match-beginning 1)
  14883. 'org-image-overlay))
  14884. (setq file (expand-file-name
  14885. (concat (or (match-string 3) "") (match-string 4))))
  14886. (when (file-exists-p file)
  14887. (if (and (car-safe old) refresh)
  14888. (image-refresh (overlay-get (cdr old) 'display))
  14889. (setq img (create-image file))
  14890. (when img
  14891. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14892. (overlay-put ov 'display img)
  14893. (overlay-put ov 'face 'default)
  14894. (overlay-put ov 'org-image-overlay t)
  14895. (overlay-put ov 'modification-hooks
  14896. (list 'org-display-inline-modification-hook))
  14897. (push ov org-inline-image-overlays)))))))))
  14898. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  14899. "Remove inline-display overlay if a corresponding region is modified."
  14900. (let ((inhibit-modification-hooks t))
  14901. (when (and ov after)
  14902. (delete ov org-inline-image-overlays)
  14903. (delete-overlay ov))))
  14904. (defun org-remove-inline-images ()
  14905. "Remove inline display of images."
  14906. (interactive)
  14907. (mapc 'delete-overlay org-inline-image-overlays)
  14908. (setq org-inline-image-overlays nil))
  14909. ;;;; Key bindings
  14910. ;; Make `C-c C-x' a prefix key
  14911. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14912. ;; TAB key with modifiers
  14913. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14914. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14915. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14916. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14917. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14918. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14919. ;; The following line is necessary under Suse GNU/Linux
  14920. (unless (featurep 'xemacs)
  14921. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14922. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14923. (define-key org-mode-map [backtab] 'org-shifttab)
  14924. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14925. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14926. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14927. ;; Cursor keys with modifiers
  14928. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14929. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14930. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14931. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14932. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14933. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14934. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14935. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14936. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14937. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14938. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14939. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14940. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14941. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14942. ;; Babel keys
  14943. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  14944. (mapc (lambda (pair)
  14945. (define-key org-babel-map (car pair) (cdr pair)))
  14946. org-babel-key-bindings)
  14947. ;;; Extra keys for tty access.
  14948. ;; We only set them when really needed because otherwise the
  14949. ;; menus don't show the simple keys
  14950. (when (or org-use-extra-keys
  14951. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14952. (not window-system))
  14953. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14954. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14955. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14956. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14957. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14958. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14959. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14960. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14961. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14962. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14963. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14964. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14965. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14966. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14967. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14968. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14969. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14970. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14971. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14972. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14973. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14974. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14975. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14976. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14977. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14978. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14979. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14980. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14981. ;; All the other keys
  14982. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14983. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14984. (if (boundp 'narrow-map)
  14985. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14986. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14987. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14988. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14989. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14990. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14991. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14992. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14993. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14994. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14995. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14996. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14997. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14998. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14999. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15000. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15001. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15002. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15003. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15004. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15005. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15006. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15007. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15008. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15009. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15010. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15011. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15012. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15013. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15014. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15015. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15016. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15017. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15018. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15019. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15020. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15021. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15022. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15023. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15024. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15025. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15026. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15027. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15028. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15029. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15030. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15031. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15032. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15033. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15034. (org-defkey org-mode-map "\C-m" 'org-return)
  15035. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15036. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15037. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15038. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15039. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15040. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15041. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15042. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15043. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15044. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15045. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15046. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15047. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15048. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15049. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15050. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15051. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15052. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15053. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15054. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15055. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15056. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15057. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15058. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15059. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15060. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15061. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15062. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15063. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15064. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15065. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15066. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15067. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15068. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15069. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15070. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15071. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15072. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15073. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15074. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15075. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15076. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15077. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15078. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15079. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15080. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15081. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15082. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15083. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15084. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15085. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15086. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15087. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15088. (when (featurep 'xemacs)
  15089. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15090. (defconst org-speed-commands-default
  15091. '(
  15092. ("Outline Navigation")
  15093. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15094. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15095. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15096. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15097. ("u" . (org-speed-move-safe 'outline-up-heading))
  15098. ("j" . org-goto)
  15099. ("g" . (org-refile t))
  15100. ("Outline Visibility")
  15101. ("c" . org-cycle)
  15102. ("C" . org-shifttab)
  15103. (" " . org-display-outline-path)
  15104. ("Outline Structure Editing")
  15105. ("U" . org-shiftmetaup)
  15106. ("D" . org-shiftmetadown)
  15107. ("r" . org-metaright)
  15108. ("l" . org-metaleft)
  15109. ("R" . org-shiftmetaright)
  15110. ("L" . org-shiftmetaleft)
  15111. ("i" . (progn (forward-char 1) (call-interactively
  15112. 'org-insert-heading-respect-content)))
  15113. ("^" . org-sort)
  15114. ("w" . org-refile)
  15115. ("a" . org-archive-subtree-default-with-confirmation)
  15116. ("." . outline-mark-subtree)
  15117. ("Clock Commands")
  15118. ("I" . org-clock-in)
  15119. ("O" . org-clock-out)
  15120. ("Meta Data Editing")
  15121. ("t" . org-todo)
  15122. ("0" . (org-priority ?\ ))
  15123. ("1" . (org-priority ?A))
  15124. ("2" . (org-priority ?B))
  15125. ("3" . (org-priority ?C))
  15126. (";" . org-set-tags-command)
  15127. ("e" . org-set-effort)
  15128. ("Agenda Views etc")
  15129. ("v" . org-agenda)
  15130. ("/" . org-sparse-tree)
  15131. ("Misc")
  15132. ("o" . org-open-at-point)
  15133. ("?" . org-speed-command-help)
  15134. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15135. (">" . (org-agenda-remove-restriction-lock))
  15136. )
  15137. "The default speed commands.")
  15138. (defun org-print-speed-command (e)
  15139. (if (> (length (car e)) 1)
  15140. (progn
  15141. (princ "\n")
  15142. (princ (car e))
  15143. (princ "\n")
  15144. (princ (make-string (length (car e)) ?-))
  15145. (princ "\n"))
  15146. (princ (car e))
  15147. (princ " ")
  15148. (if (symbolp (cdr e))
  15149. (princ (symbol-name (cdr e)))
  15150. (prin1 (cdr e)))
  15151. (princ "\n")))
  15152. (defun org-speed-command-help ()
  15153. "Show the available speed commands."
  15154. (interactive)
  15155. (if (not org-use-speed-commands)
  15156. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15157. (with-output-to-temp-buffer "*Help*"
  15158. (princ "User-defined Speed commands\n===========================\n")
  15159. (mapc 'org-print-speed-command org-speed-commands-user)
  15160. (princ "\n")
  15161. (princ "Built-in Speed commands\n=======================\n")
  15162. (mapc 'org-print-speed-command org-speed-commands-default))
  15163. (with-current-buffer "*Help*"
  15164. (setq truncate-lines t))))
  15165. (defun org-speed-move-safe (cmd)
  15166. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15167. If not, return to the original position and throw an error."
  15168. (interactive)
  15169. (let ((pos (point)))
  15170. (call-interactively cmd)
  15171. (unless (and (bolp) (org-on-heading-p))
  15172. (goto-char pos)
  15173. (error "Boundary reached while executing %s" cmd))))
  15174. (defvar org-self-insert-command-undo-counter 0)
  15175. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15176. (defvar org-speed-command nil)
  15177. (defun org-speed-command-default-hook (keys)
  15178. "Hook for activating single-letter speed commands.
  15179. `org-speed-commands-default' specifies a minimal command set. Use
  15180. `org-speed-commands-user' for further customization."
  15181. (when (or (and (bolp) (looking-at outline-regexp))
  15182. (and (functionp org-use-speed-commands)
  15183. (funcall org-use-speed-commands)))
  15184. (cdr (assoc keys (append org-speed-commands-user
  15185. org-speed-commands-default)))))
  15186. (defun org-babel-speed-command-hook (keys)
  15187. "Hook for activating single-letter code block commands."
  15188. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15189. (cdr (assoc keys org-babel-key-bindings))))
  15190. (defcustom org-speed-command-hook
  15191. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15192. "Hook for activating speed commands at strategic locations.
  15193. Hook functions are called in sequence until a valid handler is
  15194. found.
  15195. Each hook takes a single argument, a user-pressed command key
  15196. which is also a `self-insert-command' from the global map.
  15197. Within the hook, examine the cursor position and the command key
  15198. and return nil or a valid handler as appropriate. Handler could
  15199. be one of an interactive command, a function, or a form.
  15200. Set `org-use-speed-commands' to non-nil value to enable this
  15201. hook. The default setting is `org-speed-command-default-hook'."
  15202. :group 'org-structure
  15203. :type 'hook)
  15204. (defun org-self-insert-command (N)
  15205. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15206. If the cursor is in a table looking at whitespace, the whitespace is
  15207. overwritten, and the table is not marked as requiring realignment."
  15208. (interactive "p")
  15209. (cond
  15210. ((and org-use-speed-commands
  15211. (setq org-speed-command
  15212. (run-hook-with-args-until-success
  15213. 'org-speed-command-hook (this-command-keys))))
  15214. (cond
  15215. ((commandp org-speed-command)
  15216. (setq this-command org-speed-command)
  15217. (call-interactively org-speed-command))
  15218. ((functionp org-speed-command)
  15219. (funcall org-speed-command))
  15220. ((and org-speed-command (listp org-speed-command))
  15221. (eval org-speed-command))
  15222. (t (let (org-use-speed-commands)
  15223. (call-interactively 'org-self-insert-command)))))
  15224. ((and
  15225. (org-table-p)
  15226. (progn
  15227. ;; check if we blank the field, and if that triggers align
  15228. (and (featurep 'org-table) org-table-auto-blank-field
  15229. (member last-command
  15230. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15231. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15232. ;; got extra space, this field does not determine column width
  15233. (let (org-table-may-need-update) (org-table-blank-field))
  15234. ;; no extra space, this field may determine column width
  15235. (org-table-blank-field)))
  15236. t)
  15237. (eq N 1)
  15238. (looking-at "[^|\n]* |"))
  15239. (let (org-table-may-need-update)
  15240. (goto-char (1- (match-end 0)))
  15241. (delete-backward-char 1)
  15242. (goto-char (match-beginning 0))
  15243. (self-insert-command N)))
  15244. (t
  15245. (setq org-table-may-need-update t)
  15246. (self-insert-command N)
  15247. (org-fix-tags-on-the-fly)
  15248. (if org-self-insert-cluster-for-undo
  15249. (if (not (eq last-command 'org-self-insert-command))
  15250. (setq org-self-insert-command-undo-counter 1)
  15251. (if (>= org-self-insert-command-undo-counter 20)
  15252. (setq org-self-insert-command-undo-counter 1)
  15253. (and (> org-self-insert-command-undo-counter 0)
  15254. buffer-undo-list
  15255. (not (cadr buffer-undo-list)) ; remove nil entry
  15256. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15257. (setq org-self-insert-command-undo-counter
  15258. (1+ org-self-insert-command-undo-counter))))))))
  15259. (defun org-fix-tags-on-the-fly ()
  15260. (when (and (equal (char-after (point-at-bol)) ?*)
  15261. (org-on-heading-p))
  15262. (org-align-tags-here org-tags-column)))
  15263. (defun org-delete-backward-char (N)
  15264. "Like `delete-backward-char', insert whitespace at field end in tables.
  15265. When deleting backwards, in tables this function will insert whitespace in
  15266. front of the next \"|\" separator, to keep the table aligned. The table will
  15267. still be marked for re-alignment if the field did fill the entire column,
  15268. because, in this case the deletion might narrow the column."
  15269. (interactive "p")
  15270. (if (and (org-table-p)
  15271. (eq N 1)
  15272. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15273. (looking-at ".*?|"))
  15274. (let ((pos (point))
  15275. (noalign (looking-at "[^|\n\r]* |"))
  15276. (c org-table-may-need-update))
  15277. (backward-delete-char N)
  15278. (if (not overwrite-mode)
  15279. (progn
  15280. (skip-chars-forward "^|")
  15281. (insert " ")
  15282. (goto-char (1- pos))))
  15283. ;; noalign: if there were two spaces at the end, this field
  15284. ;; does not determine the width of the column.
  15285. (if noalign (setq org-table-may-need-update c)))
  15286. (backward-delete-char N)
  15287. (org-fix-tags-on-the-fly)))
  15288. (defun org-delete-char (N)
  15289. "Like `delete-char', but insert whitespace at field end in tables.
  15290. When deleting characters, in tables this function will insert whitespace in
  15291. front of the next \"|\" separator, to keep the table aligned. The table will
  15292. still be marked for re-alignment if the field did fill the entire column,
  15293. because, in this case the deletion might narrow the column."
  15294. (interactive "p")
  15295. (if (and (org-table-p)
  15296. (not (bolp))
  15297. (not (= (char-after) ?|))
  15298. (eq N 1))
  15299. (if (looking-at ".*?|")
  15300. (let ((pos (point))
  15301. (noalign (looking-at "[^|\n\r]* |"))
  15302. (c org-table-may-need-update))
  15303. (replace-match (concat
  15304. (substring (match-string 0) 1 -1)
  15305. " |"))
  15306. (goto-char pos)
  15307. ;; noalign: if there were two spaces at the end, this field
  15308. ;; does not determine the width of the column.
  15309. (if noalign (setq org-table-may-need-update c)))
  15310. (delete-char N))
  15311. (delete-char N)
  15312. (org-fix-tags-on-the-fly)))
  15313. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15314. (put 'org-self-insert-command 'delete-selection t)
  15315. (put 'orgtbl-self-insert-command 'delete-selection t)
  15316. (put 'org-delete-char 'delete-selection 'supersede)
  15317. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15318. (put 'org-yank 'delete-selection 'yank)
  15319. ;; Make `flyspell-mode' delay after some commands
  15320. (put 'org-self-insert-command 'flyspell-delayed t)
  15321. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15322. (put 'org-delete-char 'flyspell-delayed t)
  15323. (put 'org-delete-backward-char 'flyspell-delayed t)
  15324. ;; Make pabbrev-mode expand after org-mode commands
  15325. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15326. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15327. ;; How to do this: Measure non-white length of current string
  15328. ;; If equal to column width, we should realign.
  15329. (defun org-remap (map &rest commands)
  15330. "In MAP, remap the functions given in COMMANDS.
  15331. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15332. (let (new old)
  15333. (while commands
  15334. (setq old (pop commands) new (pop commands))
  15335. (if (fboundp 'command-remapping)
  15336. (org-defkey map (vector 'remap old) new)
  15337. (substitute-key-definition old new map global-map)))))
  15338. (when (eq org-enable-table-editor 'optimized)
  15339. ;; If the user wants maximum table support, we need to hijack
  15340. ;; some standard editing functions
  15341. (org-remap org-mode-map
  15342. 'self-insert-command 'org-self-insert-command
  15343. 'delete-char 'org-delete-char
  15344. 'delete-backward-char 'org-delete-backward-char)
  15345. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15346. (defvar org-ctrl-c-ctrl-c-hook nil
  15347. "Hook for functions attaching themselves to `C-c C-c'.
  15348. This can be used to add additional functionality to the C-c C-c key which
  15349. executes context-dependent commands.
  15350. Each function will be called with no arguments. The function must check
  15351. if the context is appropriate for it to act. If yes, it should do its
  15352. thing and then return a non-nil value. If the context is wrong,
  15353. just do nothing and return nil.")
  15354. (defvar org-tab-first-hook nil
  15355. "Hook for functions to attach themselves to TAB.
  15356. See `org-ctrl-c-ctrl-c-hook' for more information.
  15357. This hook runs as the first action when TAB is pressed, even before
  15358. `org-cycle' messes around with the `outline-regexp' to cater for
  15359. inline tasks and plain list item folding.
  15360. If any function in this hook returns t, any other actions that
  15361. would have been caused by TAB (such as table field motion or visibility
  15362. cycling) will not occur.")
  15363. (defvar org-tab-after-check-for-table-hook nil
  15364. "Hook for functions to attach themselves to TAB.
  15365. See `org-ctrl-c-ctrl-c-hook' for more information.
  15366. This hook runs after it has been established that the cursor is not in a
  15367. table, but before checking if the cursor is in a headline or if global cycling
  15368. should be done.
  15369. If any function in this hook returns t, not other actions like visibility
  15370. cycling will be done.")
  15371. (defvar org-tab-after-check-for-cycling-hook nil
  15372. "Hook for functions to attach themselves to TAB.
  15373. See `org-ctrl-c-ctrl-c-hook' for more information.
  15374. This hook runs after it has been established that not table field motion and
  15375. not visibility should be done because of current context. This is probably
  15376. the place where a package like yasnippets can hook in.")
  15377. (defvar org-tab-before-tab-emulation-hook nil
  15378. "Hook for functions to attach themselves to TAB.
  15379. See `org-ctrl-c-ctrl-c-hook' for more information.
  15380. This hook runs after every other options for TAB have been exhausted, but
  15381. before indentation and \t insertion takes place.")
  15382. (defvar org-metaleft-hook nil
  15383. "Hook for functions attaching themselves to `M-left'.
  15384. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15385. (defvar org-metaright-hook nil
  15386. "Hook for functions attaching themselves to `M-right'.
  15387. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15388. (defvar org-metaup-hook nil
  15389. "Hook for functions attaching themselves to `M-up'.
  15390. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15391. (defvar org-metadown-hook nil
  15392. "Hook for functions attaching themselves to `M-down'.
  15393. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15394. (defvar org-shiftmetaleft-hook nil
  15395. "Hook for functions attaching themselves to `M-S-left'.
  15396. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15397. (defvar org-shiftmetaright-hook nil
  15398. "Hook for functions attaching themselves to `M-S-right'.
  15399. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15400. (defvar org-shiftmetaup-hook nil
  15401. "Hook for functions attaching themselves to `M-S-up'.
  15402. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15403. (defvar org-shiftmetadown-hook nil
  15404. "Hook for functions attaching themselves to `M-S-down'.
  15405. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15406. (defvar org-metareturn-hook nil
  15407. "Hook for functions attaching themselves to `M-RET'.
  15408. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15409. (defvar org-shiftup-hook nil
  15410. "Hook for functions attaching themselves to `S-up'.
  15411. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15412. (defvar org-shiftup-final-hook nil
  15413. "Hook for functions attaching themselves to `S-up'.
  15414. This one runs after all other options except shift-select have been excluded.
  15415. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15416. (defvar org-shiftdown-hook nil
  15417. "Hook for functions attaching themselves to `S-down'.
  15418. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15419. (defvar org-shiftdown-final-hook nil
  15420. "Hook for functions attaching themselves to `S-down'.
  15421. This one runs after all other options except shift-select have been excluded.
  15422. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15423. (defvar org-shiftleft-hook nil
  15424. "Hook for functions attaching themselves to `S-left'.
  15425. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15426. (defvar org-shiftleft-final-hook nil
  15427. "Hook for functions attaching themselves to `S-left'.
  15428. This one runs after all other options except shift-select have been excluded.
  15429. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15430. (defvar org-shiftright-hook nil
  15431. "Hook for functions attaching themselves to `S-right'.
  15432. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15433. (defvar org-shiftright-final-hook nil
  15434. "Hook for functions attaching themselves to `S-right'.
  15435. This one runs after all other options except shift-select have been excluded.
  15436. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15437. (defun org-modifier-cursor-error ()
  15438. "Throw an error, a modified cursor command was applied in wrong context."
  15439. (error "This command is active in special context like tables, headlines or items"))
  15440. (defun org-shiftselect-error ()
  15441. "Throw an error because Shift-Cursor command was applied in wrong context."
  15442. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15443. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15444. (error "This command works only in special context like headlines or timestamps")))
  15445. (defun org-call-for-shift-select (cmd)
  15446. (let ((this-command-keys-shift-translated t))
  15447. (call-interactively cmd)))
  15448. (defun org-shifttab (&optional arg)
  15449. "Global visibility cycling or move to previous table field.
  15450. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15451. on context.
  15452. See the individual commands for more information."
  15453. (interactive "P")
  15454. (cond
  15455. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15456. ((integerp arg)
  15457. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15458. (message "Content view to level: %d" arg)
  15459. (org-content (prefix-numeric-value arg2))
  15460. (setq org-cycle-global-status 'overview)))
  15461. (t (call-interactively 'org-global-cycle))))
  15462. (defun org-shiftmetaleft ()
  15463. "Promote subtree or delete table column.
  15464. Calls `org-promote-subtree', `org-outdent-item',
  15465. or `org-table-delete-column', depending on context.
  15466. See the individual commands for more information."
  15467. (interactive)
  15468. (cond
  15469. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15470. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15471. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15472. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15473. (t (org-modifier-cursor-error))))
  15474. (defun org-shiftmetaright ()
  15475. "Demote subtree or insert table column.
  15476. Calls `org-demote-subtree', `org-indent-item',
  15477. or `org-table-insert-column', depending on context.
  15478. See the individual commands for more information."
  15479. (interactive)
  15480. (cond
  15481. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15482. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15483. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15484. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15485. (t (org-modifier-cursor-error))))
  15486. (defun org-shiftmetaup (&optional arg)
  15487. "Move subtree up or kill table row.
  15488. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15489. `org-move-item-up' depending on context. See the individual commands
  15490. for more information."
  15491. (interactive "P")
  15492. (cond
  15493. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15494. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15495. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15496. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15497. (t (org-modifier-cursor-error))))
  15498. (defun org-shiftmetadown (&optional arg)
  15499. "Move subtree down or insert table row.
  15500. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15501. `org-move-item-down', depending on context. See the individual
  15502. commands for more information."
  15503. (interactive "P")
  15504. (cond
  15505. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15506. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15507. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15508. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15509. (t (org-modifier-cursor-error))))
  15510. (defsubst org-hidden-tree-error ()
  15511. (error
  15512. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15513. (defun org-metaleft (&optional arg)
  15514. "Promote heading or move table column to left.
  15515. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15516. With no specific context, calls the Emacs default `backward-word'.
  15517. See the individual commands for more information."
  15518. (interactive "P")
  15519. (cond
  15520. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15521. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15522. ((or (org-on-heading-p)
  15523. (and (org-region-active-p)
  15524. (save-excursion
  15525. (goto-char (region-beginning))
  15526. (org-on-heading-p))))
  15527. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15528. (call-interactively 'org-do-promote))
  15529. ((or (org-at-item-p)
  15530. (and (org-region-active-p)
  15531. (save-excursion
  15532. (goto-char (region-beginning))
  15533. (org-at-item-p))))
  15534. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15535. (call-interactively 'org-outdent-item))
  15536. (t (call-interactively 'backward-word))))
  15537. (defun org-metaright (&optional arg)
  15538. "Demote subtree or move table column to right.
  15539. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15540. With no specific context, calls the Emacs default `forward-word'.
  15541. See the individual commands for more information."
  15542. (interactive "P")
  15543. (cond
  15544. ((run-hook-with-args-until-success 'org-metaright-hook))
  15545. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15546. ((or (org-on-heading-p)
  15547. (and (org-region-active-p)
  15548. (save-excursion
  15549. (goto-char (region-beginning))
  15550. (org-on-heading-p))))
  15551. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15552. (call-interactively 'org-do-demote))
  15553. ((or (org-at-item-p)
  15554. (and (org-region-active-p)
  15555. (save-excursion
  15556. (goto-char (region-beginning))
  15557. (org-at-item-p))))
  15558. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15559. (call-interactively 'org-indent-item))
  15560. (t (call-interactively 'forward-word))))
  15561. (defun org-check-for-hidden (what)
  15562. "Check if there are hidden headlines/items in the current visual line.
  15563. WHAT can be either `headlines' or `items'. If the current line is
  15564. an outline or item heading and it has a folded subtree below it,
  15565. this function returns t, nil otherwise."
  15566. (let ((re (cond
  15567. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15568. ((eq what 'items) (concat "^" (org-item-re t)))
  15569. (t (error "This should not happen"))))
  15570. beg end)
  15571. (save-excursion
  15572. (catch 'exit
  15573. (unless (org-region-active-p)
  15574. (setq beg (point-at-bol))
  15575. (beginning-of-line 2)
  15576. (while (and (not (eobp)) ;; this is like `next-line'
  15577. (get-char-property (1- (point)) 'invisible))
  15578. (beginning-of-line 2))
  15579. (setq end (point))
  15580. (goto-char beg)
  15581. (goto-char (point-at-eol))
  15582. (setq end (max end (point)))
  15583. (while (re-search-forward re end t)
  15584. (if (get-char-property (match-beginning 0) 'invisible)
  15585. (throw 'exit t))))
  15586. nil))))
  15587. (defun org-metaup (&optional arg)
  15588. "Move subtree up or move table row up.
  15589. Calls `org-move-subtree-up' or `org-table-move-row' or
  15590. `org-move-item-up', depending on context. See the individual commands
  15591. for more information."
  15592. (interactive "P")
  15593. (cond
  15594. ((run-hook-with-args-until-success 'org-metaup-hook))
  15595. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15596. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15597. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15598. (t (transpose-lines 1) (beginning-of-line -1))))
  15599. (defun org-metadown (&optional arg)
  15600. "Move subtree down or move table row down.
  15601. Calls `org-move-subtree-down' or `org-table-move-row' or
  15602. `org-move-item-down', depending on context. See the individual
  15603. commands for more information."
  15604. (interactive "P")
  15605. (cond
  15606. ((run-hook-with-args-until-success 'org-metadown-hook))
  15607. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15608. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15609. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15610. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15611. (defun org-shiftup (&optional arg)
  15612. "Increase item in timestamp or increase priority of current headline.
  15613. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15614. depending on context. See the individual commands for more information."
  15615. (interactive "P")
  15616. (cond
  15617. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15618. ((and org-support-shift-select (org-region-active-p))
  15619. (org-call-for-shift-select 'previous-line))
  15620. ((org-at-timestamp-p t)
  15621. (call-interactively (if org-edit-timestamp-down-means-later
  15622. 'org-timestamp-down 'org-timestamp-up)))
  15623. ((and (not (eq org-support-shift-select 'always))
  15624. org-enable-priority-commands
  15625. (org-on-heading-p))
  15626. (call-interactively 'org-priority-up))
  15627. ((and (not org-support-shift-select) (org-at-item-p))
  15628. (call-interactively 'org-previous-item))
  15629. ((org-clocktable-try-shift 'up arg))
  15630. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15631. (org-support-shift-select
  15632. (org-call-for-shift-select 'previous-line))
  15633. (t (org-shiftselect-error))))
  15634. (defun org-shiftdown (&optional arg)
  15635. "Decrease item in timestamp or decrease priority of current headline.
  15636. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15637. depending on context. See the individual commands for more information."
  15638. (interactive "P")
  15639. (cond
  15640. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15641. ((and org-support-shift-select (org-region-active-p))
  15642. (org-call-for-shift-select 'next-line))
  15643. ((org-at-timestamp-p t)
  15644. (call-interactively (if org-edit-timestamp-down-means-later
  15645. 'org-timestamp-up 'org-timestamp-down)))
  15646. ((and (not (eq org-support-shift-select 'always))
  15647. org-enable-priority-commands
  15648. (org-on-heading-p))
  15649. (call-interactively 'org-priority-down))
  15650. ((and (not org-support-shift-select) (org-at-item-p))
  15651. (call-interactively 'org-next-item))
  15652. ((org-clocktable-try-shift 'down arg))
  15653. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15654. (org-support-shift-select
  15655. (org-call-for-shift-select 'next-line))
  15656. (t (org-shiftselect-error))))
  15657. (defun org-shiftright (&optional arg)
  15658. "Cycle the thing at point or in the current line, depending on context.
  15659. Depending on context, this does one of the following:
  15660. - switch a timestamp at point one day into the future
  15661. - on a headline, switch to the next TODO keyword.
  15662. - on an item, switch entire list to the next bullet type
  15663. - on a property line, switch to the next allowed value
  15664. - on a clocktable definition line, move time block into the future"
  15665. (interactive "P")
  15666. (cond
  15667. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15668. ((and org-support-shift-select (org-region-active-p))
  15669. (org-call-for-shift-select 'forward-char))
  15670. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15671. ((and (not (eq org-support-shift-select 'always))
  15672. (org-on-heading-p))
  15673. (let ((org-inhibit-logging
  15674. (not org-treat-S-cursor-todo-selection-as-state-change))
  15675. (org-inhibit-blocking
  15676. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15677. (org-call-with-arg 'org-todo 'right)))
  15678. ((or (and org-support-shift-select
  15679. (not (eq org-support-shift-select 'always))
  15680. (org-at-item-bullet-p))
  15681. (and (not org-support-shift-select) (org-at-item-p)))
  15682. (org-call-with-arg 'org-cycle-list-bullet nil))
  15683. ((and (not (eq org-support-shift-select 'always))
  15684. (org-at-property-p))
  15685. (call-interactively 'org-property-next-allowed-value))
  15686. ((org-clocktable-try-shift 'right arg))
  15687. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15688. (org-support-shift-select
  15689. (org-call-for-shift-select 'forward-char))
  15690. (t (org-shiftselect-error))))
  15691. (defun org-shiftleft (&optional arg)
  15692. "Cycle the thing at point or in the current line, depending on context.
  15693. Depending on context, this does one of the following:
  15694. - switch a timestamp at point one day into the past
  15695. - on a headline, switch to the previous TODO keyword.
  15696. - on an item, switch entire list to the previous bullet type
  15697. - on a property line, switch to the previous allowed value
  15698. - on a clocktable definition line, move time block into the past"
  15699. (interactive "P")
  15700. (cond
  15701. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15702. ((and org-support-shift-select (org-region-active-p))
  15703. (org-call-for-shift-select 'backward-char))
  15704. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15705. ((and (not (eq org-support-shift-select 'always))
  15706. (org-on-heading-p))
  15707. (let ((org-inhibit-logging
  15708. (not org-treat-S-cursor-todo-selection-as-state-change))
  15709. (org-inhibit-blocking
  15710. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15711. (org-call-with-arg 'org-todo 'left)))
  15712. ((or (and org-support-shift-select
  15713. (not (eq org-support-shift-select 'always))
  15714. (org-at-item-bullet-p))
  15715. (and (not org-support-shift-select) (org-at-item-p)))
  15716. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15717. ((and (not (eq org-support-shift-select 'always))
  15718. (org-at-property-p))
  15719. (call-interactively 'org-property-previous-allowed-value))
  15720. ((org-clocktable-try-shift 'left arg))
  15721. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15722. (org-support-shift-select
  15723. (org-call-for-shift-select 'backward-char))
  15724. (t (org-shiftselect-error))))
  15725. (defun org-shiftcontrolright ()
  15726. "Switch to next TODO set."
  15727. (interactive)
  15728. (cond
  15729. ((and org-support-shift-select (org-region-active-p))
  15730. (org-call-for-shift-select 'forward-word))
  15731. ((and (not (eq org-support-shift-select 'always))
  15732. (org-on-heading-p))
  15733. (org-call-with-arg 'org-todo 'nextset))
  15734. (org-support-shift-select
  15735. (org-call-for-shift-select 'forward-word))
  15736. (t (org-shiftselect-error))))
  15737. (defun org-shiftcontrolleft ()
  15738. "Switch to previous TODO set."
  15739. (interactive)
  15740. (cond
  15741. ((and org-support-shift-select (org-region-active-p))
  15742. (org-call-for-shift-select 'backward-word))
  15743. ((and (not (eq org-support-shift-select 'always))
  15744. (org-on-heading-p))
  15745. (org-call-with-arg 'org-todo 'previousset))
  15746. (org-support-shift-select
  15747. (org-call-for-shift-select 'backward-word))
  15748. (t (org-shiftselect-error))))
  15749. (defun org-ctrl-c-ret ()
  15750. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15751. (interactive)
  15752. (cond
  15753. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15754. (t (call-interactively 'org-insert-heading))))
  15755. (defun org-copy-special ()
  15756. "Copy region in table or copy current subtree.
  15757. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15758. See the individual commands for more information."
  15759. (interactive)
  15760. (call-interactively
  15761. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15762. (defun org-cut-special ()
  15763. "Cut region in table or cut current subtree.
  15764. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15765. See the individual commands for more information."
  15766. (interactive)
  15767. (call-interactively
  15768. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15769. (defun org-paste-special (arg)
  15770. "Paste rectangular region into table, or past subtree relative to level.
  15771. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15772. See the individual commands for more information."
  15773. (interactive "P")
  15774. (if (org-at-table-p)
  15775. (org-table-paste-rectangle)
  15776. (org-paste-subtree arg)))
  15777. (defun org-edit-special (&optional arg)
  15778. "Call a special editor for the stuff at point.
  15779. When at a table, call the formula editor with `org-table-edit-formulas'.
  15780. When at the first line of an src example, call `org-edit-src-code'.
  15781. When in an #+include line, visit the include file. Otherwise call
  15782. `ffap' to visit the file at point."
  15783. (interactive)
  15784. ;; possibly prep session before editing source
  15785. (when arg
  15786. (let* ((info (org-babel-get-src-block-info))
  15787. (lang (nth 0 info))
  15788. (params (nth 2 info))
  15789. (session (cdr (assoc :session params))))
  15790. (when (and info session) ;; we are in a source-code block with a session
  15791. (funcall
  15792. (intern (concat "org-babel-prep-session:" lang)) session params))))
  15793. (cond ;; proceed with `org-edit-special'
  15794. ((save-excursion
  15795. (beginning-of-line 1)
  15796. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15797. (find-file (org-trim (match-string 1))))
  15798. ((org-edit-src-code))
  15799. ((org-edit-fixed-width-region))
  15800. ((org-at-table.el-p)
  15801. (org-edit-src-code))
  15802. ((org-at-table-p)
  15803. (call-interactively 'org-table-edit-formulas))
  15804. (t (call-interactively 'ffap))))
  15805. (defun org-ctrl-c-ctrl-c (&optional arg)
  15806. "Set tags in headline, or update according to changed information at point.
  15807. This command does many different things, depending on context:
  15808. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15809. this is what we do.
  15810. - If the cursor is on a statistics cookie, update it.
  15811. - If the cursor is in a headline, prompt for tags and insert them
  15812. into the current line, aligned to `org-tags-column'. When called
  15813. with prefix arg, realign all tags in the current buffer.
  15814. - If the cursor is in one of the special #+KEYWORD lines, this
  15815. triggers scanning the buffer for these lines and updating the
  15816. information.
  15817. - If the cursor is inside a table, realign the table. This command
  15818. works even if the automatic table editor has been turned off.
  15819. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15820. the entire table.
  15821. - If the cursor is at a footnote reference or definition, jump to
  15822. the corresponding definition or references, respectively.
  15823. - If the cursor is a the beginning of a dynamic block, update it.
  15824. - If the current buffer is a capture buffer, close note and file it.
  15825. - If the cursor is on a <<<target>>>, update radio targets and
  15826. corresponding links in this buffer.
  15827. - If the cursor is on a numbered item in a plain list, renumber the
  15828. ordered list.
  15829. - If the cursor is on a checkbox, toggle it.
  15830. - If the cursor is on a code block, evaluate it. The variable
  15831. `org-confirm-babel-evaluate' can be used to control prompting
  15832. before code block evaluation, by default every code block
  15833. evaluation requires confirmation. Code block evaluation can be
  15834. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  15835. (interactive "P")
  15836. (let ((org-enable-table-editor t))
  15837. (cond
  15838. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15839. org-occur-highlights
  15840. org-latex-fragment-image-overlays)
  15841. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15842. (org-remove-occur-highlights)
  15843. (org-remove-latex-fragment-image-overlays)
  15844. (message "Temporary highlights/overlays removed from current buffer"))
  15845. ((and (local-variable-p 'org-finish-function (current-buffer))
  15846. (fboundp org-finish-function))
  15847. (funcall org-finish-function))
  15848. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15849. ((or (looking-at org-property-start-re)
  15850. (org-at-property-p))
  15851. (call-interactively 'org-property-action))
  15852. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15853. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15854. (or (org-on-heading-p) (org-at-item-p)))
  15855. (call-interactively 'org-update-statistics-cookies))
  15856. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15857. ((org-at-table.el-p)
  15858. (message "Use C-c ' to edit table.el tables"))
  15859. ((org-at-table-p)
  15860. (org-table-maybe-eval-formula)
  15861. (if arg
  15862. (call-interactively 'org-table-recalculate)
  15863. (org-table-maybe-recalculate-line))
  15864. (call-interactively 'org-table-align))
  15865. ((or (org-footnote-at-reference-p)
  15866. (org-footnote-at-definition-p))
  15867. (call-interactively 'org-footnote-action))
  15868. ((org-at-item-checkbox-p)
  15869. (call-interactively 'org-list-repair)
  15870. (call-interactively 'org-toggle-checkbox)
  15871. (org-list-send-list 'maybe))
  15872. ((org-at-item-p)
  15873. (call-interactively 'org-list-repair)
  15874. (when arg (call-interactively 'org-toggle-checkbox))
  15875. (org-list-send-list 'maybe))
  15876. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15877. ;; Dynamic block
  15878. (beginning-of-line 1)
  15879. (save-excursion (org-update-dblock)))
  15880. ((save-excursion
  15881. (beginning-of-line 1)
  15882. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15883. (cond
  15884. ((equal (match-string 1) "TBLFM")
  15885. ;; Recalculate the table before this line
  15886. (save-excursion
  15887. (beginning-of-line 1)
  15888. (skip-chars-backward " \r\n\t")
  15889. (if (org-at-table-p)
  15890. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15891. (t
  15892. (let ((org-inhibit-startup-visibility-stuff t)
  15893. (org-startup-align-all-tables nil))
  15894. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15895. (message "Local setup has been refreshed"))))
  15896. ((org-clock-update-time-maybe))
  15897. (t (error "C-c C-c can do nothing useful at this location")))))
  15898. (defun org-mode-restart ()
  15899. "Restart Org-mode, to scan again for special lines.
  15900. Also updates the keyword regular expressions."
  15901. (interactive)
  15902. (org-mode)
  15903. (message "Org-mode restarted"))
  15904. (defun org-kill-note-or-show-branches ()
  15905. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15906. (interactive)
  15907. (if (not org-finish-function)
  15908. (progn
  15909. (hide-subtree)
  15910. (call-interactively 'show-branches))
  15911. (let ((org-note-abort t))
  15912. (funcall org-finish-function))))
  15913. (defun org-return (&optional indent)
  15914. "Goto next table row or insert a newline.
  15915. Calls `org-table-next-row' or `newline', depending on context.
  15916. See the individual commands for more information."
  15917. (interactive)
  15918. (cond
  15919. ((bobp) (if indent (newline-and-indent) (newline)))
  15920. ((org-at-table-p)
  15921. (org-table-justify-field-maybe)
  15922. (call-interactively 'org-table-next-row))
  15923. ((and org-return-follows-link
  15924. (eq (get-text-property (point) 'face) 'org-link))
  15925. (call-interactively 'org-open-at-point))
  15926. ((and (org-at-heading-p)
  15927. (looking-at
  15928. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  15929. (org-show-entry)
  15930. (end-of-line 1)
  15931. (newline))
  15932. (t (if indent (newline-and-indent) (newline)))))
  15933. (defun org-return-indent ()
  15934. "Goto next table row or insert a newline and indent.
  15935. Calls `org-table-next-row' or `newline-and-indent', depending on
  15936. context. See the individual commands for more information."
  15937. (interactive)
  15938. (org-return t))
  15939. (defun org-ctrl-c-star ()
  15940. "Compute table, or change heading status of lines.
  15941. Calls `org-table-recalculate' or `org-toggle-heading',
  15942. depending on context."
  15943. (interactive)
  15944. (cond
  15945. ((org-at-table-p)
  15946. (call-interactively 'org-table-recalculate))
  15947. (t
  15948. ;; Convert all lines in region to list items
  15949. (call-interactively 'org-toggle-heading))))
  15950. (defun org-ctrl-c-minus ()
  15951. "Insert separator line in table or modify bullet status of line.
  15952. Also turns a plain line or a region of lines into list items.
  15953. Calls `org-table-insert-hline', `org-toggle-item', or
  15954. `org-cycle-list-bullet', depending on context."
  15955. (interactive)
  15956. (cond
  15957. ((org-at-table-p)
  15958. (call-interactively 'org-table-insert-hline))
  15959. ((org-region-active-p)
  15960. (call-interactively 'org-toggle-item))
  15961. ((org-in-item-p)
  15962. (call-interactively 'org-cycle-list-bullet))
  15963. (t
  15964. (call-interactively 'org-toggle-item))))
  15965. (defun org-toggle-item ()
  15966. "Convert headings or normal lines to items, items to normal lines.
  15967. If there is no active region, only the current line is considered.
  15968. If the first line in the region is a headline, convert all headlines to items.
  15969. If the first line in the region is an item, convert all items to normal lines.
  15970. If the first line is normal text, add an item bullet to each line."
  15971. (interactive)
  15972. (let (l2 l beg end)
  15973. (if (org-region-active-p)
  15974. (setq beg (region-beginning) end (region-end))
  15975. (setq beg (point-at-bol)
  15976. end (min (1+ (point-at-eol)) (point-max))))
  15977. (save-excursion
  15978. (goto-char end)
  15979. (setq l2 (org-current-line))
  15980. (goto-char beg)
  15981. (beginning-of-line 1)
  15982. (setq l (1- (org-current-line)))
  15983. (if (org-at-item-p)
  15984. ;; We already have items, de-itemize
  15985. (while (< (setq l (1+ l)) l2)
  15986. (when (org-at-item-p)
  15987. (skip-chars-forward " \t")
  15988. (delete-region (point) (match-end 0)))
  15989. (beginning-of-line 2))
  15990. (if (org-on-heading-p)
  15991. ;; Headings, convert to items
  15992. (while (< (setq l (1+ l)) l2)
  15993. (if (looking-at org-outline-regexp)
  15994. (replace-match (org-list-bullet-string "-") t t))
  15995. (beginning-of-line 2))
  15996. ;; normal lines, turn them into items
  15997. (while (< (setq l (1+ l)) l2)
  15998. (unless (org-at-item-p)
  15999. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16000. (replace-match
  16001. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  16002. (beginning-of-line 2)))))))
  16003. (defun org-toggle-heading (&optional nstars)
  16004. "Convert headings to normal text, or items or text to headings.
  16005. If there is no active region, only the current line is considered.
  16006. If the first line is a heading, remove the stars from all headlines
  16007. in the region.
  16008. If the first line is a plain list item, turn all plain list items
  16009. into headings.
  16010. If the first line is a normal line, turn each and every line in the
  16011. region into a heading.
  16012. When converting a line into a heading, the number of stars is chosen
  16013. such that the lines become children of the current entry. However,
  16014. when a prefix argument is given, its value determines the number of
  16015. stars to add."
  16016. (interactive "P")
  16017. (let (l2 l itemp beg end)
  16018. (if (org-region-active-p)
  16019. (setq beg (region-beginning) end (region-end))
  16020. (setq beg (point-at-bol)
  16021. end (min (1+ (point-at-eol)) (point-max))))
  16022. (save-excursion
  16023. (goto-char end)
  16024. (setq l2 (org-current-line))
  16025. (goto-char beg)
  16026. (beginning-of-line 1)
  16027. (setq l (1- (org-current-line)))
  16028. (if (org-on-heading-p)
  16029. ;; We already have headlines, de-star them
  16030. (while (< (setq l (1+ l)) l2)
  16031. (when (org-on-heading-p t)
  16032. (and (looking-at outline-regexp) (replace-match "")))
  16033. (beginning-of-line 2))
  16034. (setq itemp (org-at-item-p))
  16035. (let* ((stars
  16036. (if nstars
  16037. (make-string (prefix-numeric-value current-prefix-arg)
  16038. ?*)
  16039. (save-excursion
  16040. (if (re-search-backward org-complex-heading-regexp nil t)
  16041. (match-string 1) ""))))
  16042. (add-stars (cond (nstars "")
  16043. ((equal stars "") "*")
  16044. (org-odd-levels-only "**")
  16045. (t "*")))
  16046. (rpl (concat stars add-stars " ")))
  16047. (while (< (setq l (1+ l)) l2)
  16048. (if itemp
  16049. (and (org-at-item-p) (replace-match rpl t t))
  16050. (unless (org-on-heading-p)
  16051. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16052. (replace-match (concat rpl (match-string 2))))))
  16053. (beginning-of-line 2)))))))
  16054. (defun org-meta-return (&optional arg)
  16055. "Insert a new heading or wrap a region in a table.
  16056. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16057. See the individual commands for more information."
  16058. (interactive "P")
  16059. (cond
  16060. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16061. ((org-at-table-p)
  16062. (call-interactively 'org-table-wrap-region))
  16063. (t (call-interactively 'org-insert-heading))))
  16064. ;;; Menu entries
  16065. ;; Define the Org-mode menus
  16066. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16067. '("Tbl"
  16068. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16069. ["Next Field" org-cycle (org-at-table-p)]
  16070. ["Previous Field" org-shifttab (org-at-table-p)]
  16071. ["Next Row" org-return (org-at-table-p)]
  16072. "--"
  16073. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16074. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16075. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16076. "--"
  16077. ("Column"
  16078. ["Move Column Left" org-metaleft (org-at-table-p)]
  16079. ["Move Column Right" org-metaright (org-at-table-p)]
  16080. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16081. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16082. ("Row"
  16083. ["Move Row Up" org-metaup (org-at-table-p)]
  16084. ["Move Row Down" org-metadown (org-at-table-p)]
  16085. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16086. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16087. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16088. "--"
  16089. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16090. ("Rectangle"
  16091. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16092. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16093. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16094. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16095. "--"
  16096. ("Calculate"
  16097. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16098. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16099. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16100. "--"
  16101. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16102. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16103. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16104. "--"
  16105. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16106. "--"
  16107. ["Sum Column/Rectangle" org-table-sum
  16108. (or (org-at-table-p) (org-region-active-p))]
  16109. ["Which Column?" org-table-current-column (org-at-table-p)])
  16110. ["Debug Formulas"
  16111. org-table-toggle-formula-debugger
  16112. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16113. ["Show Col/Row Numbers"
  16114. org-table-toggle-coordinate-overlays
  16115. :style toggle
  16116. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16117. "--"
  16118. ["Create" org-table-create (and (not (org-at-table-p))
  16119. org-enable-table-editor)]
  16120. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16121. ["Import from File" org-table-import (not (org-at-table-p))]
  16122. ["Export to File" org-table-export (org-at-table-p)]
  16123. "--"
  16124. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16125. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16126. '("Org"
  16127. ("Show/Hide"
  16128. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16129. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16130. ["Sparse Tree..." org-sparse-tree t]
  16131. ["Reveal Context" org-reveal t]
  16132. ["Show All" show-all t]
  16133. "--"
  16134. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16135. "--"
  16136. ["New Heading" org-insert-heading t]
  16137. ("Navigate Headings"
  16138. ["Up" outline-up-heading t]
  16139. ["Next" outline-next-visible-heading t]
  16140. ["Previous" outline-previous-visible-heading t]
  16141. ["Next Same Level" outline-forward-same-level t]
  16142. ["Previous Same Level" outline-backward-same-level t]
  16143. "--"
  16144. ["Jump" org-goto t])
  16145. ("Edit Structure"
  16146. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16147. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16148. "--"
  16149. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16150. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16151. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16152. "--"
  16153. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16154. "--"
  16155. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16156. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16157. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16158. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16159. "--"
  16160. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16161. "--"
  16162. ["Convert to odd levels" org-convert-to-odd-levels t]
  16163. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16164. ("Editing"
  16165. ["Emphasis..." org-emphasize t]
  16166. ["Edit Source Example" org-edit-special t]
  16167. "--"
  16168. ["Footnote new/jump" org-footnote-action t]
  16169. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16170. ("Archive"
  16171. ["Archive (default method)" org-archive-subtree-default t]
  16172. "--"
  16173. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16174. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16175. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16176. )
  16177. "--"
  16178. ("Hyperlinks"
  16179. ["Store Link (Global)" org-store-link t]
  16180. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16181. ["Insert Link" org-insert-link t]
  16182. ["Follow Link" org-open-at-point t]
  16183. "--"
  16184. ["Next link" org-next-link t]
  16185. ["Previous link" org-previous-link t]
  16186. "--"
  16187. ["Descriptive Links"
  16188. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16189. :style radio
  16190. :selected (member '(org-link) buffer-invisibility-spec)]
  16191. ["Literal Links"
  16192. (progn
  16193. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16194. :style radio
  16195. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16196. "--"
  16197. ("TODO Lists"
  16198. ["TODO/DONE/-" org-todo t]
  16199. ("Select keyword"
  16200. ["Next keyword" org-shiftright (org-on-heading-p)]
  16201. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16202. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  16203. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16204. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16205. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16206. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16207. "--"
  16208. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16209. :selected org-enforce-todo-dependencies :style toggle :active t]
  16210. "Settings for tree at point"
  16211. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16212. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  16213. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16214. ["Do Children parallel" org-toggle-ordered-property :style radio
  16215. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  16216. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16217. "--"
  16218. ["Set Priority" org-priority t]
  16219. ["Priority Up" org-shiftup t]
  16220. ["Priority Down" org-shiftdown t]
  16221. "--"
  16222. ["Get news from all feeds" org-feed-update-all t]
  16223. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16224. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16225. ("TAGS and Properties"
  16226. ["Set Tags" org-set-tags-command t]
  16227. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16228. "--"
  16229. ["Set property" org-set-property t]
  16230. ["Column view of properties" org-columns t]
  16231. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16232. ("Dates and Scheduling"
  16233. ["Timestamp" org-time-stamp t]
  16234. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16235. ("Change Date"
  16236. ["1 Day Later" org-shiftright t]
  16237. ["1 Day Earlier" org-shiftleft t]
  16238. ["1 ... Later" org-shiftup t]
  16239. ["1 ... Earlier" org-shiftdown t])
  16240. ["Compute Time Range" org-evaluate-time-range t]
  16241. ["Schedule Item" org-schedule t]
  16242. ["Deadline" org-deadline t]
  16243. "--"
  16244. ["Custom time format" org-toggle-time-stamp-overlays
  16245. :style radio :selected org-display-custom-times]
  16246. "--"
  16247. ["Goto Calendar" org-goto-calendar t]
  16248. ["Date from Calendar" org-date-from-calendar t]
  16249. "--"
  16250. ["Start/Restart Timer" org-timer-start t]
  16251. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16252. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16253. ["Insert Timer String" org-timer t]
  16254. ["Insert Timer Item" org-timer-item t])
  16255. ("Logging work"
  16256. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16257. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16258. ["Clock out" org-clock-out t]
  16259. ["Clock cancel" org-clock-cancel t]
  16260. "--"
  16261. ["Mark as default task" org-clock-mark-default-task t]
  16262. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16263. ["Goto running clock" org-clock-goto t]
  16264. "--"
  16265. ["Display times" org-clock-display t]
  16266. ["Create clock table" org-clock-report t]
  16267. "--"
  16268. ["Record DONE time"
  16269. (progn (setq org-log-done (not org-log-done))
  16270. (message "Switching to %s will %s record a timestamp"
  16271. (car org-done-keywords)
  16272. (if org-log-done "automatically" "not")))
  16273. :style toggle :selected org-log-done])
  16274. "--"
  16275. ["Agenda Command..." org-agenda t]
  16276. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16277. ("File List for Agenda")
  16278. ("Special views current file"
  16279. ["TODO Tree" org-show-todo-tree t]
  16280. ["Check Deadlines" org-check-deadlines t]
  16281. ["Timeline" org-timeline t]
  16282. ["Tags/Property tree" org-match-sparse-tree t])
  16283. "--"
  16284. ["Export/Publish..." org-export t]
  16285. ("LaTeX"
  16286. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16287. :selected org-cdlatex-mode]
  16288. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16289. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16290. ["Modify math symbol" org-cdlatex-math-modify
  16291. (org-inside-LaTeX-fragment-p)]
  16292. ["Insert citation" org-reftex-citation t]
  16293. "--"
  16294. ["Template for BEAMER" org-insert-beamer-options-template t])
  16295. "--"
  16296. ("MobileOrg"
  16297. ["Push Files and Views" org-mobile-push t]
  16298. ["Get Captured and Flagged" org-mobile-pull t]
  16299. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16300. "--"
  16301. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16302. "--"
  16303. ("Documentation"
  16304. ["Show Version" org-version t]
  16305. ["Info Documentation" org-info t])
  16306. ("Customize"
  16307. ["Browse Org Group" org-customize t]
  16308. "--"
  16309. ["Expand This Menu" org-create-customize-menu
  16310. (fboundp 'customize-menu-create)])
  16311. ["Send bug report" org-submit-bug-report t]
  16312. "--"
  16313. ("Refresh/Reload"
  16314. ["Refresh setup current buffer" org-mode-restart t]
  16315. ["Reload Org (after update)" org-reload t]
  16316. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16317. ))
  16318. (defun org-info (&optional node)
  16319. "Read documentation for Org-mode in the info system.
  16320. With optional NODE, go directly to that node."
  16321. (interactive)
  16322. (info (format "(org)%s" (or node ""))))
  16323. ;;;###autoload
  16324. (defun org-submit-bug-report ()
  16325. "Submit a bug report on Org-mode via mail.
  16326. Don't hesitate to report any problems or inaccurate documentation.
  16327. If you don't have setup sending mail from (X)Emacs, please copy the
  16328. output buffer into your mail program, as it gives us important
  16329. information about your Org-mode version and configuration."
  16330. (interactive)
  16331. (require 'reporter)
  16332. (org-load-modules-maybe)
  16333. (org-require-autoloaded-modules)
  16334. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16335. (reporter-submit-bug-report
  16336. "emacs-orgmode@gnu.org"
  16337. (org-version)
  16338. (let (list)
  16339. (save-window-excursion
  16340. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16341. (delete-other-windows)
  16342. (erase-buffer)
  16343. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16344. We would like to add your full Org-mode and Outline configuration to the
  16345. bug report. This greatly simplifies the work of the maintainer and
  16346. other experts on the mailing list.
  16347. HOWEVER, some variables you have customized may contain private
  16348. information. The names of customers, colleagues, or friends, might
  16349. appear in the form of file names, tags, todo states, or search strings.
  16350. If you answer yes to the prompt, you might want to check and remove
  16351. such private information before sending the email.")
  16352. (add-text-properties (point-min) (point-max) '(face org-warning))
  16353. (when (yes-or-no-p "Include your Org-mode configuration ")
  16354. (mapatoms
  16355. (lambda (v)
  16356. (and (boundp v)
  16357. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16358. (or (and (symbol-value v)
  16359. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16360. (and
  16361. (get v 'custom-type) (get v 'standard-value)
  16362. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16363. (push v list)))))
  16364. (kill-buffer (get-buffer "*Warn about privacy*"))
  16365. list))
  16366. nil nil
  16367. "Remember to cover the basics, that is, what you expected to happen and
  16368. what in fact did happen. You don't know how to make a good report? See
  16369. http://orgmode.org/manual/Feedback.html#Feedback
  16370. Your bug report will be posted to the Org-mode mailing list.
  16371. ------------------------------------------------------------------------")
  16372. (save-excursion
  16373. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16374. (replace-match "\\1Bug: \\3 [\\2]")))))
  16375. (defun org-install-agenda-files-menu ()
  16376. (let ((bl (buffer-list)))
  16377. (save-excursion
  16378. (while bl
  16379. (set-buffer (pop bl))
  16380. (if (org-mode-p) (setq bl nil)))
  16381. (when (org-mode-p)
  16382. (easy-menu-change
  16383. '("Org") "File List for Agenda"
  16384. (append
  16385. (list
  16386. ["Edit File List" (org-edit-agenda-file-list) t]
  16387. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16388. ["Remove Current File from List" org-remove-file t]
  16389. ["Cycle through agenda files" org-cycle-agenda-files t]
  16390. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16391. "--")
  16392. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16393. ;;;; Documentation
  16394. ;;;###autoload
  16395. (defun org-require-autoloaded-modules ()
  16396. (interactive)
  16397. (mapc 'require
  16398. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16399. org-docbook org-exp org-html org-icalendar
  16400. org-id org-latex
  16401. org-publish org-remember org-table
  16402. org-timer org-xoxo)))
  16403. ;;;###autoload
  16404. (defun org-reload (&optional uncompiled)
  16405. "Reload all org lisp files.
  16406. With prefix arg UNCOMPILED, load the uncompiled versions."
  16407. (interactive "P")
  16408. (require 'find-func)
  16409. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16410. (dir-org (file-name-directory (org-find-library-name "org")))
  16411. (dir-org-contrib (ignore-errors
  16412. (file-name-directory
  16413. (org-find-library-name "org-contribdir"))))
  16414. (babel-files
  16415. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16416. (append (list nil "comint" "eval" "exp" "keys"
  16417. "lob" "ref" "table" "tangle")
  16418. (delq nil
  16419. (mapcar
  16420. (lambda (lang)
  16421. (when (cdr lang) (symbol-name (car lang))))
  16422. org-babel-load-languages)))))
  16423. (files
  16424. (append (directory-files dir-org t file-re)
  16425. babel-files
  16426. (and dir-org-contrib
  16427. (directory-files dir-org-contrib t file-re))))
  16428. (remove-re (concat (if (featurep 'xemacs)
  16429. "org-colview" "org-colview-xemacs")
  16430. "\\'")))
  16431. (setq files (mapcar 'file-name-sans-extension files))
  16432. (setq files (mapcar
  16433. (lambda (x) (if (string-match remove-re x) nil x))
  16434. files))
  16435. (setq files (delq nil files))
  16436. (mapc
  16437. (lambda (f)
  16438. (when (featurep (intern (file-name-nondirectory f)))
  16439. (if (and (not uncompiled)
  16440. (file-exists-p (concat f ".elc")))
  16441. (load (concat f ".elc") nil nil t)
  16442. (load (concat f ".el") nil nil t))))
  16443. files))
  16444. (org-version))
  16445. ;;;###autoload
  16446. (defun org-customize ()
  16447. "Call the customize function with org as argument."
  16448. (interactive)
  16449. (org-load-modules-maybe)
  16450. (org-require-autoloaded-modules)
  16451. (customize-browse 'org))
  16452. (defun org-create-customize-menu ()
  16453. "Create a full customization menu for Org-mode, insert it into the menu."
  16454. (interactive)
  16455. (org-load-modules-maybe)
  16456. (org-require-autoloaded-modules)
  16457. (if (fboundp 'customize-menu-create)
  16458. (progn
  16459. (easy-menu-change
  16460. '("Org") "Customize"
  16461. `(["Browse Org group" org-customize t]
  16462. "--"
  16463. ,(customize-menu-create 'org)
  16464. ["Set" Custom-set t]
  16465. ["Save" Custom-save t]
  16466. ["Reset to Current" Custom-reset-current t]
  16467. ["Reset to Saved" Custom-reset-saved t]
  16468. ["Reset to Standard Settings" Custom-reset-standard t]))
  16469. (message "\"Org\"-menu now contains full customization menu"))
  16470. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16471. ;;;; Miscellaneous stuff
  16472. ;;; Generally useful functions
  16473. (defun org-get-at-bol (property)
  16474. "Get text property PROPERTY at beginning of line."
  16475. (get-text-property (point-at-bol) property))
  16476. (defun org-find-text-property-in-string (prop s)
  16477. "Return the first non-nil value of property PROP in string S."
  16478. (or (get-text-property 0 prop s)
  16479. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16480. prop s)))
  16481. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16482. "Display the given MESSAGE as a warning."
  16483. (if (fboundp 'display-warning)
  16484. (display-warning 'org message
  16485. (if (featurep 'xemacs) 'warning :warning))
  16486. (let ((buf (get-buffer-create "*Org warnings*")))
  16487. (with-current-buffer buf
  16488. (goto-char (point-max))
  16489. (insert "Warning (Org): " message)
  16490. (unless (bolp)
  16491. (newline)))
  16492. (display-buffer buf)
  16493. (sit-for 0))))
  16494. (defun org-in-commented-line ()
  16495. "Is point in a line starting with `#'?"
  16496. (equal (char-after (point-at-bol)) ?#))
  16497. (defun org-in-indented-comment-line ()
  16498. "Is point in a line starting with `#' after some white space?"
  16499. (save-excursion
  16500. (save-match-data
  16501. (goto-char (point-at-bol))
  16502. (looking-at "[ \t]*#"))))
  16503. (defun org-in-verbatim-emphasis ()
  16504. (save-match-data
  16505. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16506. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16507. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16508. (if (and marker (marker-buffer marker)
  16509. (buffer-live-p (marker-buffer marker)))
  16510. (progn
  16511. (switch-to-buffer (marker-buffer marker))
  16512. (if (or (> marker (point-max)) (< marker (point-min)))
  16513. (widen))
  16514. (goto-char marker)
  16515. (org-show-context 'org-goto))
  16516. (if bookmark
  16517. (bookmark-jump bookmark)
  16518. (error "Cannot find location"))))
  16519. (defun org-quote-csv-field (s)
  16520. "Quote field for inclusion in CSV material."
  16521. (if (string-match "[\",]" s)
  16522. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16523. s))
  16524. (defun org-plist-delete (plist property)
  16525. "Delete PROPERTY from PLIST.
  16526. This is in contrast to merely setting it to 0."
  16527. (let (p)
  16528. (while plist
  16529. (if (not (eq property (car plist)))
  16530. (setq p (plist-put p (car plist) (nth 1 plist))))
  16531. (setq plist (cddr plist)))
  16532. p))
  16533. (defun org-force-self-insert (N)
  16534. "Needed to enforce self-insert under remapping."
  16535. (interactive "p")
  16536. (self-insert-command N))
  16537. (defun org-string-width (s)
  16538. "Compute width of string, ignoring invisible characters.
  16539. This ignores character with invisibility property `org-link', and also
  16540. characters with property `org-cwidth', because these will become invisible
  16541. upon the next fontification round."
  16542. (let (b l)
  16543. (when (or (eq t buffer-invisibility-spec)
  16544. (assq 'org-link buffer-invisibility-spec))
  16545. (while (setq b (text-property-any 0 (length s)
  16546. 'invisible 'org-link s))
  16547. (setq s (concat (substring s 0 b)
  16548. (substring s (or (next-single-property-change
  16549. b 'invisible s) (length s)))))))
  16550. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16551. (setq s (concat (substring s 0 b)
  16552. (substring s (or (next-single-property-change
  16553. b 'org-cwidth s) (length s))))))
  16554. (setq l (string-width s) b -1)
  16555. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16556. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16557. l))
  16558. (defun org-get-indentation (&optional line)
  16559. "Get the indentation of the current line, interpreting tabs.
  16560. When LINE is given, assume it represents a line and compute its indentation."
  16561. (if line
  16562. (if (string-match "^ *" (org-remove-tabs line))
  16563. (match-end 0))
  16564. (save-excursion
  16565. (beginning-of-line 1)
  16566. (skip-chars-forward " \t")
  16567. (current-column))))
  16568. (defun org-remove-tabs (s &optional width)
  16569. "Replace tabulators in S with spaces.
  16570. Assumes that s is a single line, starting in column 0."
  16571. (setq width (or width tab-width))
  16572. (while (string-match "\t" s)
  16573. (setq s (replace-match
  16574. (make-string
  16575. (- (* width (/ (+ (match-beginning 0) width) width))
  16576. (match-beginning 0)) ?\ )
  16577. t t s)))
  16578. s)
  16579. (defun org-fix-indentation (line ind)
  16580. "Fix indentation in LINE.
  16581. IND is a cons cell with target and minimum indentation.
  16582. If the current indentation in LINE is smaller than the minimum,
  16583. leave it alone. If it is larger than ind, set it to the target."
  16584. (let* ((l (org-remove-tabs line))
  16585. (i (org-get-indentation l))
  16586. (i1 (car ind)) (i2 (cdr ind)))
  16587. (if (>= i i2) (setq l (substring line i2)))
  16588. (if (> i1 0)
  16589. (concat (make-string i1 ?\ ) l)
  16590. l)))
  16591. (defun org-remove-indentation (code &optional n)
  16592. "Remove the maximum common indentation from the lines in CODE.
  16593. N may optionally be the number of spaces to remove."
  16594. (with-temp-buffer
  16595. (insert code)
  16596. (org-do-remove-indentation n)
  16597. (buffer-string)))
  16598. (defun org-do-remove-indentation (&optional n)
  16599. "Remove the maximum common indentation from the buffer."
  16600. (untabify (point-min) (point-max))
  16601. (let ((min 10000) re)
  16602. (if n
  16603. (setq min n)
  16604. (goto-char (point-min))
  16605. (while (re-search-forward "^ *[^ \n]" nil t)
  16606. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16607. (unless (or (= min 0) (= min 10000))
  16608. (setq re (format "^ \\{%d\\}" min))
  16609. (goto-char (point-min))
  16610. (while (re-search-forward re nil t)
  16611. (replace-match "")
  16612. (end-of-line 1))
  16613. min)))
  16614. (defun org-fill-template (template alist)
  16615. "Find each %key of ALIST in TEMPLATE and replace it."
  16616. (let ((case-fold-search nil)
  16617. entry key value)
  16618. (setq alist (sort (copy-sequence alist)
  16619. (lambda (a b) (< (length (car a)) (length (car b))))))
  16620. (while (setq entry (pop alist))
  16621. (setq template
  16622. (replace-regexp-in-string
  16623. (concat "%" (regexp-quote (car entry)))
  16624. (cdr entry) template t t)))
  16625. template))
  16626. (defun org-base-buffer (buffer)
  16627. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16628. (if (not buffer)
  16629. buffer
  16630. (or (buffer-base-buffer buffer)
  16631. buffer)))
  16632. (defun org-trim (s)
  16633. "Remove whitespace at beginning and end of string."
  16634. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16635. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16636. s)
  16637. (defun org-wrap (string &optional width lines)
  16638. "Wrap string to either a number of lines, or a width in characters.
  16639. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16640. that costs. If there is a word longer than WIDTH, the text is actually
  16641. wrapped to the length of that word.
  16642. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16643. many lines, whatever width that takes.
  16644. The return value is a list of lines, without newlines at the end."
  16645. (let* ((words (org-split-string string "[ \t\n]+"))
  16646. (maxword (apply 'max (mapcar 'org-string-width words)))
  16647. w ll)
  16648. (cond (width
  16649. (org-do-wrap words (max maxword width)))
  16650. (lines
  16651. (setq w maxword)
  16652. (setq ll (org-do-wrap words maxword))
  16653. (if (<= (length ll) lines)
  16654. ll
  16655. (setq ll words)
  16656. (while (> (length ll) lines)
  16657. (setq w (1+ w))
  16658. (setq ll (org-do-wrap words w)))
  16659. ll))
  16660. (t (error "Cannot wrap this")))))
  16661. (defun org-do-wrap (words width)
  16662. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16663. (let (lines line)
  16664. (while words
  16665. (setq line (pop words))
  16666. (while (and words (< (+ (length line) (length (car words))) width))
  16667. (setq line (concat line " " (pop words))))
  16668. (setq lines (push line lines)))
  16669. (nreverse lines)))
  16670. (defun org-split-string (string &optional separators)
  16671. "Splits STRING into substrings at SEPARATORS.
  16672. No empty strings are returned if there are matches at the beginning
  16673. and end of string."
  16674. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16675. (start 0)
  16676. notfirst
  16677. (list nil))
  16678. (while (and (string-match rexp string
  16679. (if (and notfirst
  16680. (= start (match-beginning 0))
  16681. (< start (length string)))
  16682. (1+ start) start))
  16683. (< (match-beginning 0) (length string)))
  16684. (setq notfirst t)
  16685. (or (eq (match-beginning 0) 0)
  16686. (and (eq (match-beginning 0) (match-end 0))
  16687. (eq (match-beginning 0) start))
  16688. (setq list
  16689. (cons (substring string start (match-beginning 0))
  16690. list)))
  16691. (setq start (match-end 0)))
  16692. (or (eq start (length string))
  16693. (setq list
  16694. (cons (substring string start)
  16695. list)))
  16696. (nreverse list)))
  16697. (defun org-quote-vert (s)
  16698. "Replace \"|\" with \"\\vert\"."
  16699. (while (string-match "|" s)
  16700. (setq s (replace-match "\\vert" t t s)))
  16701. s)
  16702. (defun org-uuidgen-p (s)
  16703. "Is S an ID created by UUIDGEN?"
  16704. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16705. (defun org-context ()
  16706. "Return a list of contexts of the current cursor position.
  16707. If several contexts apply, all are returned.
  16708. Each context entry is a list with a symbol naming the context, and
  16709. two positions indicating start and end of the context. Possible
  16710. contexts are:
  16711. :headline anywhere in a headline
  16712. :headline-stars on the leading stars in a headline
  16713. :todo-keyword on a TODO keyword (including DONE) in a headline
  16714. :tags on the TAGS in a headline
  16715. :priority on the priority cookie in a headline
  16716. :item on the first line of a plain list item
  16717. :item-bullet on the bullet/number of a plain list item
  16718. :checkbox on the checkbox in a plain list item
  16719. :table in an org-mode table
  16720. :table-special on a special filed in a table
  16721. :table-table in a table.el table
  16722. :link on a hyperlink
  16723. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16724. :target on a <<target>>
  16725. :radio-target on a <<<radio-target>>>
  16726. :latex-fragment on a LaTeX fragment
  16727. :latex-preview on a LaTeX fragment with overlayed preview image
  16728. This function expects the position to be visible because it uses font-lock
  16729. faces as a help to recognize the following contexts: :table-special, :link,
  16730. and :keyword."
  16731. (let* ((f (get-text-property (point) 'face))
  16732. (faces (if (listp f) f (list f)))
  16733. (p (point)) clist o)
  16734. ;; First the large context
  16735. (cond
  16736. ((org-on-heading-p t)
  16737. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16738. (when (progn
  16739. (beginning-of-line 1)
  16740. (looking-at org-todo-line-tags-regexp))
  16741. (push (org-point-in-group p 1 :headline-stars) clist)
  16742. (push (org-point-in-group p 2 :todo-keyword) clist)
  16743. (push (org-point-in-group p 4 :tags) clist))
  16744. (goto-char p)
  16745. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16746. (if (looking-at "\\[#[A-Z0-9]\\]")
  16747. (push (org-point-in-group p 0 :priority) clist)))
  16748. ((org-at-item-p)
  16749. (push (org-point-in-group p 2 :item-bullet) clist)
  16750. (push (list :item (point-at-bol)
  16751. (save-excursion (org-end-of-item) (point)))
  16752. clist)
  16753. (and (org-at-item-checkbox-p)
  16754. (push (org-point-in-group p 0 :checkbox) clist)))
  16755. ((org-at-table-p)
  16756. (push (list :table (org-table-begin) (org-table-end)) clist)
  16757. (if (memq 'org-formula faces)
  16758. (push (list :table-special
  16759. (previous-single-property-change p 'face)
  16760. (next-single-property-change p 'face)) clist)))
  16761. ((org-at-table-p 'any)
  16762. (push (list :table-table) clist)))
  16763. (goto-char p)
  16764. ;; Now the small context
  16765. (cond
  16766. ((org-at-timestamp-p)
  16767. (push (org-point-in-group p 0 :timestamp) clist))
  16768. ((memq 'org-link faces)
  16769. (push (list :link
  16770. (previous-single-property-change p 'face)
  16771. (next-single-property-change p 'face)) clist))
  16772. ((memq 'org-special-keyword faces)
  16773. (push (list :keyword
  16774. (previous-single-property-change p 'face)
  16775. (next-single-property-change p 'face)) clist))
  16776. ((org-on-target-p)
  16777. (push (org-point-in-group p 0 :target) clist)
  16778. (goto-char (1- (match-beginning 0)))
  16779. (if (looking-at org-radio-target-regexp)
  16780. (push (org-point-in-group p 0 :radio-target) clist))
  16781. (goto-char p))
  16782. ((setq o (car (delq nil
  16783. (mapcar
  16784. (lambda (x)
  16785. (if (memq x org-latex-fragment-image-overlays) x))
  16786. (overlays-at (point))))))
  16787. (push (list :latex-fragment
  16788. (overlay-start o) (overlay-end o)) clist)
  16789. (push (list :latex-preview
  16790. (overlay-start o) (overlay-end o)) clist))
  16791. ((org-inside-LaTeX-fragment-p)
  16792. ;; FIXME: positions wrong.
  16793. (push (list :latex-fragment (point) (point)) clist)))
  16794. (setq clist (nreverse (delq nil clist)))
  16795. clist))
  16796. ;; FIXME: Compare with at-regexp-p Do we need both?
  16797. (defun org-in-regexp (re &optional nlines visually)
  16798. "Check if point is inside a match of regexp.
  16799. Normally only the current line is checked, but you can include NLINES extra
  16800. lines both before and after point into the search.
  16801. If VISUALLY is set, require that the cursor is not after the match but
  16802. really on, so that the block visually is on the match."
  16803. (catch 'exit
  16804. (let ((pos (point))
  16805. (eol (point-at-eol (+ 1 (or nlines 0))))
  16806. (inc (if visually 1 0)))
  16807. (save-excursion
  16808. (beginning-of-line (- 1 (or nlines 0)))
  16809. (while (re-search-forward re eol t)
  16810. (if (and (<= (match-beginning 0) pos)
  16811. (>= (+ inc (match-end 0)) pos))
  16812. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16813. (defun org-at-regexp-p (regexp)
  16814. "Is point inside a match of REGEXP in the current line?"
  16815. (catch 'exit
  16816. (save-excursion
  16817. (let ((pos (point)) (end (point-at-eol)))
  16818. (beginning-of-line 1)
  16819. (while (re-search-forward regexp end t)
  16820. (if (and (<= (match-beginning 0) pos)
  16821. (>= (match-end 0) pos))
  16822. (throw 'exit t)))
  16823. nil))))
  16824. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  16825. "Return t if the current point is between matches of START-RE and END-RE.
  16826. This will also return t if point is on one of the two matches or
  16827. in an unfinished block. END-RE can be a string or a form
  16828. returning a string.
  16829. An optional third argument bounds the search for START-RE. It
  16830. defaults to previous heading or `point-min'."
  16831. (let ((pos (point))
  16832. (limit (or bound (save-excursion (outline-previous-heading)))))
  16833. (save-excursion
  16834. ;; we're on a block when point is on start-re...
  16835. (or (org-at-regexp-p start-re)
  16836. ;; ... or start-re can be found above...
  16837. (and (re-search-backward start-re limit t)
  16838. ;; ... but no end-re between start-re and point.
  16839. (not (re-search-forward (eval end-re) pos t)))))))
  16840. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16841. "Call `multi-occur' with buffers for all agenda files."
  16842. (interactive "sOrg-files matching: \np")
  16843. (let* ((files (org-agenda-files))
  16844. (tnames (mapcar 'file-truename files))
  16845. (extra org-agenda-text-search-extra-files)
  16846. f)
  16847. (when (eq (car extra) 'agenda-archives)
  16848. (setq extra (cdr extra))
  16849. (setq files (org-add-archive-files files)))
  16850. (while (setq f (pop extra))
  16851. (unless (member (file-truename f) tnames)
  16852. (add-to-list 'files f 'append)
  16853. (add-to-list 'tnames (file-truename f) 'append)))
  16854. (multi-occur
  16855. (mapcar (lambda (x)
  16856. (with-current-buffer
  16857. (or (get-file-buffer x) (find-file-noselect x))
  16858. (widen)
  16859. (current-buffer)))
  16860. files)
  16861. regexp)))
  16862. (if (boundp 'occur-mode-find-occurrence-hook)
  16863. ;; Emacs 23
  16864. (add-hook 'occur-mode-find-occurrence-hook
  16865. (lambda ()
  16866. (when (org-mode-p)
  16867. (org-reveal))))
  16868. ;; Emacs 22
  16869. (defadvice occur-mode-goto-occurrence
  16870. (after org-occur-reveal activate)
  16871. (and (org-mode-p) (org-reveal)))
  16872. (defadvice occur-mode-goto-occurrence-other-window
  16873. (after org-occur-reveal activate)
  16874. (and (org-mode-p) (org-reveal)))
  16875. (defadvice occur-mode-display-occurrence
  16876. (after org-occur-reveal activate)
  16877. (when (org-mode-p)
  16878. (let ((pos (occur-mode-find-occurrence)))
  16879. (with-current-buffer (marker-buffer pos)
  16880. (save-excursion
  16881. (goto-char pos)
  16882. (org-reveal)))))))
  16883. (defun org-occur-link-in-agenda-files ()
  16884. "Create a link and search for it in the agendas.
  16885. The link is not stored in `org-stored-links', it is just created
  16886. for the search purpose."
  16887. (interactive)
  16888. (let ((link (condition-case nil
  16889. (org-store-link nil)
  16890. (error "Unable to create a link to here"))))
  16891. (org-occur-in-agenda-files (regexp-quote link))))
  16892. (defun org-uniquify (list)
  16893. "Remove duplicate elements from LIST."
  16894. (let (res)
  16895. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16896. res))
  16897. (defun org-delete-all (elts list)
  16898. "Remove all elements in ELTS from LIST."
  16899. (while elts
  16900. (setq list (delete (pop elts) list)))
  16901. list)
  16902. (defun org-count (cl-item cl-seq)
  16903. "Count the number of occurrences of ITEM in SEQ.
  16904. Taken from `count' in cl-seq.el with all keyword arguments removed."
  16905. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  16906. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  16907. (while (< cl-start cl-end)
  16908. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  16909. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  16910. (setq cl-start (1+ cl-start)))
  16911. cl-count))
  16912. (defun org-remove-if (predicate seq)
  16913. "Remove everything from SEQ that fulfills PREDICATE."
  16914. (let (res e)
  16915. (while seq
  16916. (setq e (pop seq))
  16917. (if (not (funcall predicate e)) (push e res)))
  16918. (nreverse res)))
  16919. (defun org-remove-if-not (predicate seq)
  16920. "Remove everything from SEQ that does not fulfill PREDICATE."
  16921. (let (res e)
  16922. (while seq
  16923. (setq e (pop seq))
  16924. (if (funcall predicate e) (push e res)))
  16925. (nreverse res)))
  16926. (defun org-back-over-empty-lines ()
  16927. "Move backwards over whitespace, to the beginning of the first empty line.
  16928. Returns the number of empty lines passed."
  16929. (let ((pos (point)))
  16930. (skip-chars-backward " \t\n\r")
  16931. (beginning-of-line 2)
  16932. (goto-char (min (point) pos))
  16933. (count-lines (point) pos)))
  16934. (defun org-skip-whitespace ()
  16935. (skip-chars-forward " \t\n\r"))
  16936. (defun org-point-in-group (point group &optional context)
  16937. "Check if POINT is in match-group GROUP.
  16938. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16939. match. If the match group does not exist or point is not inside it,
  16940. return nil."
  16941. (and (match-beginning group)
  16942. (>= point (match-beginning group))
  16943. (<= point (match-end group))
  16944. (if context
  16945. (list context (match-beginning group) (match-end group))
  16946. t)))
  16947. (defun org-switch-to-buffer-other-window (&rest args)
  16948. "Switch to buffer in a second window on the current frame.
  16949. In particular, do not allow pop-up frames.
  16950. Returns the newly created buffer."
  16951. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16952. special-display-function)
  16953. (apply 'switch-to-buffer-other-window args)))
  16954. (defun org-combine-plists (&rest plists)
  16955. "Create a single property list from all plists in PLISTS.
  16956. The process starts by copying the first list, and then setting properties
  16957. from the other lists. Settings in the last list are the most significant
  16958. ones and overrule settings in the other lists."
  16959. (let ((rtn (copy-sequence (pop plists)))
  16960. p v ls)
  16961. (while plists
  16962. (setq ls (pop plists))
  16963. (while ls
  16964. (setq p (pop ls) v (pop ls))
  16965. (setq rtn (plist-put rtn p v))))
  16966. rtn))
  16967. (defun org-move-line-down (arg)
  16968. "Move the current line down. With prefix argument, move it past ARG lines."
  16969. (interactive "p")
  16970. (let ((col (current-column))
  16971. beg end pos)
  16972. (beginning-of-line 1) (setq beg (point))
  16973. (beginning-of-line 2) (setq end (point))
  16974. (beginning-of-line (+ 1 arg))
  16975. (setq pos (move-marker (make-marker) (point)))
  16976. (insert (delete-and-extract-region beg end))
  16977. (goto-char pos)
  16978. (org-move-to-column col)))
  16979. (defun org-move-line-up (arg)
  16980. "Move the current line up. With prefix argument, move it past ARG lines."
  16981. (interactive "p")
  16982. (let ((col (current-column))
  16983. beg end pos)
  16984. (beginning-of-line 1) (setq beg (point))
  16985. (beginning-of-line 2) (setq end (point))
  16986. (beginning-of-line (- arg))
  16987. (setq pos (move-marker (make-marker) (point)))
  16988. (insert (delete-and-extract-region beg end))
  16989. (goto-char pos)
  16990. (org-move-to-column col)))
  16991. (defun org-replace-escapes (string table)
  16992. "Replace %-escapes in STRING with values in TABLE.
  16993. TABLE is an association list with keys like \"%a\" and string values.
  16994. The sequences in STRING may contain normal field width and padding information,
  16995. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16996. so values can contain further %-escapes if they are define later in TABLE."
  16997. (let ((tbl (copy-alist table))
  16998. (case-fold-search nil)
  16999. (pchg 0)
  17000. e re rpl)
  17001. (while (setq e (pop tbl))
  17002. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  17003. (when (and (cdr e) (string-match re (cdr e)))
  17004. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  17005. (safe "SREF"))
  17006. (add-text-properties 0 3 (list 'sref sref) safe)
  17007. (setcdr e (replace-match safe t t (cdr e)))))
  17008. (while (string-match re string)
  17009. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  17010. (cdr e)))
  17011. (setq string (replace-match rpl t t string))))
  17012. (while (setq pchg (next-property-change pchg string))
  17013. (let ((sref (get-text-property pchg 'sref string)))
  17014. (when (and sref (string-match "SREF" string pchg))
  17015. (setq string (replace-match sref t t string)))))
  17016. string))
  17017. (defun org-sublist (list start end)
  17018. "Return a section of LIST, from START to END.
  17019. Counting starts at 1."
  17020. (let (rtn (c start))
  17021. (setq list (nthcdr (1- start) list))
  17022. (while (and list (<= c end))
  17023. (push (pop list) rtn)
  17024. (setq c (1+ c)))
  17025. (nreverse rtn)))
  17026. (defun org-find-base-buffer-visiting (file)
  17027. "Like `find-buffer-visiting' but always return the base buffer and
  17028. not an indirect buffer."
  17029. (let ((buf (or (get-file-buffer file)
  17030. (find-buffer-visiting file))))
  17031. (if buf
  17032. (or (buffer-base-buffer buf) buf)
  17033. nil)))
  17034. (defun org-image-file-name-regexp (&optional extensions)
  17035. "Return regexp matching the file names of images.
  17036. If EXTENSIONS is given, only match these."
  17037. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  17038. (image-file-name-regexp)
  17039. (let ((image-file-name-extensions
  17040. (or extensions
  17041. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17042. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  17043. (concat "\\."
  17044. (regexp-opt (nconc (mapcar 'upcase
  17045. image-file-name-extensions)
  17046. image-file-name-extensions)
  17047. t)
  17048. "\\'"))))
  17049. (defun org-file-image-p (file &optional extensions)
  17050. "Return non-nil if FILE is an image."
  17051. (save-match-data
  17052. (string-match (org-image-file-name-regexp extensions) file)))
  17053. (defun org-get-cursor-date ()
  17054. "Return the date at cursor in as a time.
  17055. This works in the calendar and in the agenda, anywhere else it just
  17056. returns the current time."
  17057. (let (date day defd)
  17058. (cond
  17059. ((eq major-mode 'calendar-mode)
  17060. (setq date (calendar-cursor-to-date)
  17061. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17062. ((eq major-mode 'org-agenda-mode)
  17063. (setq day (get-text-property (point) 'day))
  17064. (if day
  17065. (setq date (calendar-gregorian-from-absolute day)
  17066. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17067. (nth 2 date))))))
  17068. (or defd (current-time))))
  17069. (defvar org-agenda-action-marker (make-marker)
  17070. "Marker pointing to the entry for the next agenda action.")
  17071. (defun org-mark-entry-for-agenda-action ()
  17072. "Mark the current entry as target of an agenda action.
  17073. Agenda actions are actions executed from the agenda with the key `k',
  17074. which make use of the date at the cursor."
  17075. (interactive)
  17076. (move-marker org-agenda-action-marker
  17077. (save-excursion (org-back-to-heading t) (point))
  17078. (current-buffer))
  17079. (message
  17080. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17081. ;;; Paragraph filling stuff.
  17082. ;; We want this to be just right, so use the full arsenal.
  17083. (defun org-indent-line-function ()
  17084. "Indent line like previous, but further if previous was headline or item."
  17085. (interactive)
  17086. (let* ((pos (point))
  17087. (itemp (org-at-item-p))
  17088. (case-fold-search t)
  17089. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17090. (inline-task-p (and (featurep 'org-inlinetask)
  17091. (org-inlinetask-in-task-p)))
  17092. column bpos bcol tpos tcol bullet btype bullet-type)
  17093. ;; Find the previous relevant line
  17094. (beginning-of-line 1)
  17095. (cond
  17096. ;; Comments
  17097. ((looking-at "#") (setq column 0))
  17098. ;; Headings
  17099. ((looking-at "\\*+ ") (setq column 0))
  17100. ;; Drawers
  17101. ((and (looking-at "[ \t]*:END:")
  17102. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17103. (save-excursion
  17104. (goto-char (1- (match-beginning 1)))
  17105. (setq column (current-column))))
  17106. ;; Special blocks
  17107. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17108. (save-excursion
  17109. (re-search-backward
  17110. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17111. (setq column (org-get-indentation (match-string 0))))
  17112. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17113. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17114. (save-excursion
  17115. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17116. (setq column
  17117. (if (equal (downcase (match-string 1)) "src")
  17118. ;; src blocks: let `org-edit-src-exit' handle them
  17119. (org-get-indentation)
  17120. (org-get-indentation (match-string 0)))))
  17121. ;; Lists
  17122. ((org-in-item-p)
  17123. (org-beginning-of-item)
  17124. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\(:?\\[@\\(:?start:\\)?[0-9]+\\][ \t]*\\)?\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  17125. (setq bpos (match-beginning 1) tpos (match-end 0)
  17126. bcol (progn (goto-char bpos) (current-column))
  17127. tcol (progn (goto-char tpos) (current-column))
  17128. bullet (match-string 1)
  17129. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  17130. (if (> tcol (+ bcol org-description-max-indent))
  17131. (setq tcol (+ bcol 5)))
  17132. (if (not itemp)
  17133. (setq column tcol)
  17134. (beginning-of-line 1)
  17135. (goto-char pos)
  17136. (if (looking-at "\\S-")
  17137. (progn
  17138. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  17139. (setq bullet (match-string 1)
  17140. btype (if (string-match "[0-9]" bullet) "n" bullet))
  17141. (setq column (if (equal btype bullet-type) bcol tcol)))
  17142. (setq column (org-get-indentation)))))
  17143. ;; This line has nothing special, look upside to get a clue about
  17144. ;; what to do.
  17145. (t
  17146. (beginning-of-line 0)
  17147. (while (and (not (bobp))
  17148. ;; skip comments, verbatim, empty lines, tables,
  17149. ;; inline tasks
  17150. (or (looking-at "[ \t]*[\n:#|]")
  17151. (and (org-in-item-p) (goto-char (org-list-top-point)))
  17152. (and (not inline-task-p)
  17153. (featurep 'org-inlinetask)
  17154. (org-inlinetask-in-task-p)))
  17155. (not (looking-at "[ \t]*:END:"))
  17156. (not (looking-at org-drawer-regexp)))
  17157. (beginning-of-line 0))
  17158. (cond
  17159. ;; There was an heading above.
  17160. ((looking-at "\\*+[ \t]+")
  17161. (if (not org-adapt-indentation)
  17162. (setq column 0)
  17163. (goto-char (match-end 0))
  17164. (setq column (current-column))))
  17165. ;; A drawer had started and is unfinished: indent consequently.
  17166. ((looking-at org-drawer-regexp)
  17167. (goto-char (1- (match-beginning 1)))
  17168. (setq column (current-column)))
  17169. ;; The drawer had ended: indent like its :END: line.
  17170. ((looking-at "\\([ \t]*\\):END:")
  17171. (goto-char (match-end 1))
  17172. (setq column (current-column)))
  17173. ;; Else, nothing noticeable found: get indentation and go on.
  17174. (t (setq column (org-get-indentation))))))
  17175. (goto-char pos)
  17176. (if (<= (current-column) (current-indentation))
  17177. (org-indent-line-to column)
  17178. (save-excursion (org-indent-line-to column)))
  17179. (setq column (current-column))
  17180. (beginning-of-line 1)
  17181. (if (looking-at
  17182. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17183. (replace-match (concat (match-string 1)
  17184. (format org-property-format
  17185. (match-string 2) (match-string 3)))
  17186. t t))
  17187. (org-move-to-column column)))
  17188. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17189. "Variable to store copy of `adaptive-fill-regexp'.
  17190. Since `adaptive-fill-regexp' is set to never match, we need to
  17191. store a backup of its value before entering `org-mode' so that
  17192. the functionality can be provided as a fall-back.")
  17193. (defun org-set-autofill-regexps ()
  17194. (interactive)
  17195. ;; In the paragraph separator we include headlines, because filling
  17196. ;; text in a line directly attached to a headline would otherwise
  17197. ;; fill the headline as well.
  17198. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17199. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17200. ;; The paragraph starter includes hand-formatted lists.
  17201. (org-set-local
  17202. 'paragraph-start
  17203. (concat
  17204. "\f" "\\|"
  17205. "[ ]*$" "\\|"
  17206. "\\*+ " "\\|"
  17207. "[ \t]*#" "\\|"
  17208. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  17209. "[ \t]*[:|]" "\\|"
  17210. "\\$\\$" "\\|"
  17211. "\\\\\\(begin\\|end\\|[][]\\)"))
  17212. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17213. ;; But only if the user has not turned off tables or fixed-width regions
  17214. (org-set-local
  17215. 'auto-fill-inhibit-regexp
  17216. (concat "\\*+ \\|#\\+"
  17217. "\\|[ \t]*" org-keyword-time-regexp
  17218. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17219. (concat
  17220. "\\|[ \t]*["
  17221. (if org-enable-table-editor "|" "")
  17222. (if org-enable-fixed-width-editor ":" "")
  17223. "]"))))
  17224. ;; We use our own fill-paragraph function, to make sure that tables
  17225. ;; and fixed-width regions are not wrapped. That function will pass
  17226. ;; through to `fill-paragraph' when appropriate.
  17227. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17228. ;; Adaptive filling: To get full control, first make sure that
  17229. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17230. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17231. (org-set-local 'org-adaptive-fill-regexp-backup
  17232. adaptive-fill-regexp))
  17233. (org-set-local 'adaptive-fill-regexp "\000")
  17234. (org-set-local 'adaptive-fill-function
  17235. 'org-adaptive-fill-function)
  17236. (org-set-local
  17237. 'align-mode-rules-list
  17238. '((org-in-buffer-settings
  17239. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17240. (modes . '(org-mode))))))
  17241. (defun org-fill-paragraph (&optional justify)
  17242. "Re-align a table, pass through to fill-paragraph if no table."
  17243. (let ((table-p (org-at-table-p))
  17244. (table.el-p (org-at-table.el-p)))
  17245. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17246. (save-excursion (goto-char (point-at-bol))
  17247. (looking-at outline-regexp)))
  17248. t) ; skip headlines
  17249. (table.el-p t) ; skip table.el tables
  17250. (table-p (org-table-align) t) ; align org-mode tables
  17251. (t nil)))) ; call paragraph-fill
  17252. ;; For reference, this is the default value of adaptive-fill-regexp
  17253. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17254. (defun org-adaptive-fill-function ()
  17255. "Return a fill prefix for org-mode files.
  17256. In particular, this makes sure hanging paragraphs for hand-formatted lists
  17257. work correctly."
  17258. (cond
  17259. ;; Comment line
  17260. ((looking-at "#[ \t]+")
  17261. (match-string-no-properties 0))
  17262. ;; Description list
  17263. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  17264. (save-excursion
  17265. (if (> (match-end 1) (+ (match-beginning 1)
  17266. org-description-max-indent))
  17267. (goto-char (+ (match-beginning 1) 5))
  17268. (goto-char (match-end 0)))
  17269. (make-string (current-column) ?\ )))
  17270. ;; Ordered or unordered list
  17271. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  17272. (save-excursion
  17273. (goto-char (match-end 0))
  17274. (make-string (current-column) ?\ )))
  17275. ;; Other text
  17276. ((looking-at org-adaptive-fill-regexp-backup)
  17277. (match-string-no-properties 0))))
  17278. ;;; Other stuff.
  17279. (defun org-toggle-fixed-width-section (arg)
  17280. "Toggle the fixed-width export.
  17281. If there is no active region, the QUOTE keyword at the current headline is
  17282. inserted or removed. When present, it causes the text between this headline
  17283. and the next to be exported as fixed-width text, and unmodified.
  17284. If there is an active region, this command adds or removes a colon as the
  17285. first character of this line. If the first character of a line is a colon,
  17286. this line is also exported in fixed-width font."
  17287. (interactive "P")
  17288. (let* ((cc 0)
  17289. (regionp (org-region-active-p))
  17290. (beg (if regionp (region-beginning) (point)))
  17291. (end (if regionp (region-end)))
  17292. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17293. (case-fold-search nil)
  17294. (re "[ \t]*\\(: \\)")
  17295. off)
  17296. (if regionp
  17297. (save-excursion
  17298. (goto-char beg)
  17299. (setq cc (current-column))
  17300. (beginning-of-line 1)
  17301. (setq off (looking-at re))
  17302. (while (> nlines 0)
  17303. (setq nlines (1- nlines))
  17304. (beginning-of-line 1)
  17305. (cond
  17306. (arg
  17307. (org-move-to-column cc t)
  17308. (insert ": \n")
  17309. (forward-line -1))
  17310. ((and off (looking-at re))
  17311. (replace-match "" t t nil 1))
  17312. ((not off) (org-move-to-column cc t) (insert ": ")))
  17313. (forward-line 1)))
  17314. (save-excursion
  17315. (org-back-to-heading)
  17316. (if (looking-at (concat outline-regexp
  17317. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17318. (replace-match "" t t nil 1)
  17319. (if (looking-at outline-regexp)
  17320. (progn
  17321. (goto-char (match-end 0))
  17322. (insert org-quote-string " "))))))))
  17323. (defun org-reftex-citation ()
  17324. "Use reftex-citation to insert a citation into the buffer.
  17325. This looks for a line like
  17326. #+BIBLIOGRAPHY: foo plain option:-d
  17327. and derives from it that foo.bib is the bibliography file relevant
  17328. for this document. It then installs the necessary environment for RefTeX
  17329. to work in this buffer and calls `reftex-citation' to insert a citation
  17330. into the buffer.
  17331. Export of such citations to both LaTeX and HTML is handled by the contributed
  17332. package org-exp-bibtex by Taru Karttunen."
  17333. (interactive)
  17334. (let ((reftex-docstruct-symbol 'rds)
  17335. (reftex-cite-format "\\cite{%l}")
  17336. rds bib)
  17337. (save-excursion
  17338. (save-restriction
  17339. (widen)
  17340. (let ((case-fold-search t)
  17341. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17342. (if (not (save-excursion
  17343. (or (re-search-forward re nil t)
  17344. (re-search-backward re nil t))))
  17345. (error "No bibliography defined in file")
  17346. (setq bib (concat (match-string 1) ".bib")
  17347. rds (list (list 'bib bib)))))))
  17348. (call-interactively 'reftex-citation)))
  17349. ;;;; Functions extending outline functionality
  17350. (defun org-beginning-of-line (&optional arg)
  17351. "Go to the beginning of the current line. If that is invisible, continue
  17352. to a visible line beginning. This makes the function of C-a more intuitive.
  17353. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17354. first attempt, and only move to after the tags when the cursor is already
  17355. beyond the end of the headline."
  17356. (interactive "P")
  17357. (let ((pos (point))
  17358. (special (if (consp org-special-ctrl-a/e)
  17359. (car org-special-ctrl-a/e)
  17360. org-special-ctrl-a/e))
  17361. refpos)
  17362. (if (org-bound-and-true-p line-move-visual)
  17363. (beginning-of-visual-line 1)
  17364. (beginning-of-line 1))
  17365. (if (and arg (fboundp 'move-beginning-of-line))
  17366. (call-interactively 'move-beginning-of-line)
  17367. (if (bobp)
  17368. nil
  17369. (backward-char 1)
  17370. (if (org-truely-invisible-p)
  17371. (while (and (not (bobp)) (org-truely-invisible-p))
  17372. (backward-char 1)
  17373. (beginning-of-line 1))
  17374. (forward-char 1))))
  17375. (when special
  17376. (cond
  17377. ((and (looking-at org-complex-heading-regexp)
  17378. (= (char-after (match-end 1)) ?\ ))
  17379. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17380. (point-at-eol)))
  17381. (goto-char
  17382. (if (eq special t)
  17383. (cond ((> pos refpos) refpos)
  17384. ((= pos (point)) refpos)
  17385. (t (point)))
  17386. (cond ((> pos (point)) (point))
  17387. ((not (eq last-command this-command)) (point))
  17388. (t refpos)))))
  17389. ((org-at-item-p)
  17390. (goto-char
  17391. (if (eq special t)
  17392. (cond ((> pos (match-end 4)) (match-end 4))
  17393. ((= pos (point)) (match-end 4))
  17394. (t (point)))
  17395. (cond ((> pos (point)) (point))
  17396. ((not (eq last-command this-command)) (point))
  17397. (t (match-end 4))))))))
  17398. (org-no-warnings
  17399. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17400. (defun org-end-of-line (&optional arg)
  17401. "Go to the end of the line.
  17402. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17403. first attempt, and only move to after the tags when the cursor is already
  17404. beyond the end of the headline."
  17405. (interactive "P")
  17406. (let ((special (if (consp org-special-ctrl-a/e)
  17407. (cdr org-special-ctrl-a/e)
  17408. org-special-ctrl-a/e)))
  17409. (if (or (not special)
  17410. (not (org-on-heading-p))
  17411. arg)
  17412. (call-interactively
  17413. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17414. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17415. (t 'end-of-line)))
  17416. (let ((pos (point)))
  17417. (beginning-of-line 1)
  17418. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  17419. (if (eq special t)
  17420. (if (or (< pos (match-beginning 1))
  17421. (= pos (match-end 0)))
  17422. (goto-char (match-beginning 1))
  17423. (goto-char (match-end 0)))
  17424. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17425. (goto-char (match-end 0))
  17426. (goto-char (match-beginning 1))))
  17427. (call-interactively (if (fboundp 'move-end-of-line)
  17428. 'move-end-of-line
  17429. 'end-of-line)))))
  17430. (org-no-warnings
  17431. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17432. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17433. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17434. (define-key org-mode-map [home] 'org-beginning-of-line)
  17435. (define-key org-mode-map [end] 'org-end-of-line)
  17436. (defun org-backward-sentence (&optional arg)
  17437. "Go to beginning of sentence, or beginning of table field.
  17438. This will call `backward-sentence' or `org-table-beginning-of-field',
  17439. depending on context."
  17440. (interactive "P")
  17441. (cond
  17442. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17443. (t (call-interactively 'backward-sentence))))
  17444. (defun org-forward-sentence (&optional arg)
  17445. "Go to end of sentence, or end of table field.
  17446. This will call `forward-sentence' or `org-table-end-of-field',
  17447. depending on context."
  17448. (interactive "P")
  17449. (cond
  17450. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17451. (t (call-interactively 'forward-sentence))))
  17452. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17453. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17454. (defun org-kill-line (&optional arg)
  17455. "Kill line, to tags or end of line."
  17456. (interactive "P")
  17457. (cond
  17458. ((or (not org-special-ctrl-k)
  17459. (bolp)
  17460. (not (org-on-heading-p)))
  17461. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17462. org-ctrl-k-protect-subtree)
  17463. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17464. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17465. (error "C-k aborted - would kill hidden subtree")))
  17466. (call-interactively 'kill-line))
  17467. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  17468. (kill-region (point) (match-beginning 1))
  17469. (org-set-tags nil t))
  17470. (t (kill-region (point) (point-at-eol)))))
  17471. (define-key org-mode-map "\C-k" 'org-kill-line)
  17472. (defun org-yank (&optional arg)
  17473. "Yank. If the kill is a subtree, treat it specially.
  17474. This command will look at the current kill and check if is a single
  17475. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17476. cursor is at the beginning of a line or after the stars of a currently
  17477. empty headline, then the yank is handled specially. How exactly depends
  17478. on the value of the following variables, both set by default.
  17479. org-yank-folded-subtrees
  17480. When set, the subtree(s) will be folded after insertion, but only
  17481. if doing so would now swallow text after the yanked text.
  17482. org-yank-adjusted-subtrees
  17483. When set, the subtree will be promoted or demoted in order to
  17484. fit into the local outline tree structure, which means that the level
  17485. will be adjusted so that it becomes the smaller one of the two
  17486. *visible* surrounding headings.
  17487. Any prefix to this command will cause `yank' to be called directly with
  17488. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17489. will just
  17490. plainly yank the text as it is.
  17491. \[1] The test checks if the first non-white line is a heading
  17492. and if there are no other headings with fewer stars."
  17493. (interactive "P")
  17494. (org-yank-generic 'yank arg))
  17495. (defun org-yank-generic (command arg)
  17496. "Perform some yank-like command.
  17497. This function implements the behavior described in the `org-yank'
  17498. documentation. However, it has been generalized to work for any
  17499. interactive command with similar behavior."
  17500. ;; pretend to be command COMMAND
  17501. (setq this-command command)
  17502. (if arg
  17503. (call-interactively command)
  17504. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17505. (and (org-kill-is-subtree-p)
  17506. (or (bolp)
  17507. (and (looking-at "[ \t]*$")
  17508. (string-match
  17509. "\\`\\*+\\'"
  17510. (buffer-substring (point-at-bol) (point)))))))
  17511. swallowp)
  17512. (cond
  17513. ((and subtreep org-yank-folded-subtrees)
  17514. (let ((beg (point))
  17515. end)
  17516. (if (and subtreep org-yank-adjusted-subtrees)
  17517. (org-paste-subtree nil nil 'for-yank)
  17518. (call-interactively command))
  17519. (setq end (point))
  17520. (goto-char beg)
  17521. (when (and (bolp) subtreep
  17522. (not (setq swallowp
  17523. (org-yank-folding-would-swallow-text beg end))))
  17524. (or (looking-at outline-regexp)
  17525. (re-search-forward (concat "^" outline-regexp) end t))
  17526. (while (and (< (point) end) (looking-at outline-regexp))
  17527. (hide-subtree)
  17528. (org-cycle-show-empty-lines 'folded)
  17529. (condition-case nil
  17530. (outline-forward-same-level 1)
  17531. (error (goto-char end)))))
  17532. (when swallowp
  17533. (message
  17534. "Inserted text not folded because that would swallow text"))
  17535. (goto-char end)
  17536. (skip-chars-forward " \t\n\r")
  17537. (beginning-of-line 1)
  17538. (push-mark beg 'nomsg)))
  17539. ((and subtreep org-yank-adjusted-subtrees)
  17540. (let ((beg (point-at-bol)))
  17541. (org-paste-subtree nil nil 'for-yank)
  17542. (push-mark beg 'nomsg)))
  17543. (t
  17544. (call-interactively command))))))
  17545. (defun org-yank-folding-would-swallow-text (beg end)
  17546. "Would hide-subtree at BEG swallow any text after END?"
  17547. (let (level)
  17548. (save-excursion
  17549. (goto-char beg)
  17550. (when (or (looking-at outline-regexp)
  17551. (re-search-forward (concat "^" outline-regexp) end t))
  17552. (setq level (org-outline-level)))
  17553. (goto-char end)
  17554. (skip-chars-forward " \t\r\n\v\f")
  17555. (if (or (eobp)
  17556. (and (bolp) (looking-at org-outline-regexp)
  17557. (<= (org-outline-level) level)))
  17558. nil ; Nothing would be swallowed
  17559. t)))) ; something would swallow
  17560. (define-key org-mode-map "\C-y" 'org-yank)
  17561. (defun org-invisible-p ()
  17562. "Check if point is at a character currently not visible."
  17563. ;; Early versions of noutline don't have `outline-invisible-p'.
  17564. (if (fboundp 'outline-invisible-p)
  17565. (outline-invisible-p)
  17566. (get-char-property (point) 'invisible)))
  17567. (defun org-truely-invisible-p ()
  17568. "Check if point is at a character currently not visible.
  17569. This version does not only check the character property, but also
  17570. `visible-mode'."
  17571. ;; Early versions of noutline don't have `outline-invisible-p'.
  17572. (if (org-bound-and-true-p visible-mode)
  17573. nil
  17574. (if (fboundp 'outline-invisible-p)
  17575. (outline-invisible-p)
  17576. (get-char-property (point) 'invisible))))
  17577. (defun org-invisible-p2 ()
  17578. "Check if point is at a character currently not visible."
  17579. (save-excursion
  17580. (if (and (eolp) (not (bobp))) (backward-char 1))
  17581. ;; Early versions of noutline don't have `outline-invisible-p'.
  17582. (if (fboundp 'outline-invisible-p)
  17583. (outline-invisible-p)
  17584. (get-char-property (point) 'invisible))))
  17585. (defun org-back-to-heading (&optional invisible-ok)
  17586. "Call `outline-back-to-heading', but provide a better error message."
  17587. (condition-case nil
  17588. (outline-back-to-heading invisible-ok)
  17589. (error (error "Before first headline at position %d in buffer %s"
  17590. (point) (current-buffer)))))
  17591. (defun org-beginning-of-defun ()
  17592. "Go to the beginning of the subtree, i.e. back to the heading."
  17593. (org-back-to-heading))
  17594. (defun org-end-of-defun ()
  17595. "Go to the end of the subtree."
  17596. (org-end-of-subtree nil t))
  17597. (defun org-before-first-heading-p ()
  17598. "Before first heading?"
  17599. (save-excursion
  17600. (null (re-search-backward "^\\*+ " nil t))))
  17601. (defun org-on-heading-p (&optional ignored)
  17602. (outline-on-heading-p t))
  17603. (defun org-at-heading-p (&optional ignored)
  17604. (outline-on-heading-p t))
  17605. (defun org-point-at-end-of-empty-headline ()
  17606. "If point is at the end of an empty headline, return t, else nil.
  17607. If the heading only contains a TODO keyword, it is still still considered
  17608. empty."
  17609. (and (looking-at "[ \t]*$")
  17610. (save-excursion
  17611. (beginning-of-line 1)
  17612. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17613. "\\)?[ \t]*$")))))
  17614. (defun org-at-heading-or-item-p ()
  17615. (or (org-on-heading-p) (org-at-item-p)))
  17616. (defun org-on-target-p ()
  17617. (or (org-in-regexp org-radio-target-regexp)
  17618. (org-in-regexp org-target-regexp)))
  17619. (defun org-up-heading-all (arg)
  17620. "Move to the heading line of which the present line is a subheading.
  17621. This function considers both visible and invisible heading lines.
  17622. With argument, move up ARG levels."
  17623. (if (fboundp 'outline-up-heading-all)
  17624. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17625. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17626. (defun org-up-heading-safe ()
  17627. "Move to the heading line of which the present line is a subheading.
  17628. This version will not throw an error. It will return the level of the
  17629. headline found, or nil if no higher level is found.
  17630. Also, this function will be a lot faster than `outline-up-heading',
  17631. because it relies on stars being the outline starters. This can really
  17632. make a significant difference in outlines with very many siblings."
  17633. (let (start-level re)
  17634. (org-back-to-heading t)
  17635. (setq start-level (funcall outline-level))
  17636. (if (equal start-level 1)
  17637. nil
  17638. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17639. (if (re-search-backward re nil t)
  17640. (funcall outline-level)))))
  17641. (defun org-first-sibling-p ()
  17642. "Is this heading the first child of its parents?"
  17643. (interactive)
  17644. (let ((re (concat "^" outline-regexp))
  17645. level l)
  17646. (unless (org-at-heading-p t)
  17647. (error "Not at a heading"))
  17648. (setq level (funcall outline-level))
  17649. (save-excursion
  17650. (if (not (re-search-backward re nil t))
  17651. t
  17652. (setq l (funcall outline-level))
  17653. (< l level)))))
  17654. (defun org-goto-sibling (&optional previous)
  17655. "Goto the next sibling, even if it is invisible.
  17656. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17657. when a sibling was found. When none is found, return nil and don't
  17658. move point."
  17659. (let ((fun (if previous 're-search-backward 're-search-forward))
  17660. (pos (point))
  17661. (re (concat "^" outline-regexp))
  17662. level l)
  17663. (when (condition-case nil (org-back-to-heading t) (error nil))
  17664. (setq level (funcall outline-level))
  17665. (catch 'exit
  17666. (or previous (forward-char 1))
  17667. (while (funcall fun re nil t)
  17668. (setq l (funcall outline-level))
  17669. (when (< l level) (goto-char pos) (throw 'exit nil))
  17670. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17671. (goto-char pos)
  17672. nil))))
  17673. (defun org-show-siblings ()
  17674. "Show all siblings of the current headline."
  17675. (save-excursion
  17676. (while (org-goto-sibling) (org-flag-heading nil)))
  17677. (save-excursion
  17678. (while (org-goto-sibling 'previous)
  17679. (org-flag-heading nil))))
  17680. (defun org-goto-first-child ()
  17681. "Goto the first child, even if it is invisible.
  17682. Return t when a child was found. Otherwise don't move point and
  17683. return nil."
  17684. (let (level (pos (point)) (re (concat "^" outline-regexp)))
  17685. (when (condition-case nil (org-back-to-heading t) (error nil))
  17686. (setq level (outline-level))
  17687. (forward-char 1)
  17688. (if (and (re-search-forward re nil t) (> (outline-level) level))
  17689. (progn (goto-char (match-beginning 0)) t)
  17690. (goto-char pos) nil))))
  17691. (defun org-show-hidden-entry ()
  17692. "Show an entry where even the heading is hidden."
  17693. (save-excursion
  17694. (org-show-entry)))
  17695. (defun org-flag-heading (flag &optional entry)
  17696. "Flag the current heading. FLAG non-nil means make invisible.
  17697. When ENTRY is non-nil, show the entire entry."
  17698. (save-excursion
  17699. (org-back-to-heading t)
  17700. ;; Check if we should show the entire entry
  17701. (if entry
  17702. (progn
  17703. (org-show-entry)
  17704. (save-excursion
  17705. (and (outline-next-heading)
  17706. (org-flag-heading nil))))
  17707. (outline-flag-region (max (point-min) (1- (point)))
  17708. (save-excursion (outline-end-of-heading) (point))
  17709. flag))))
  17710. (defun org-get-next-sibling ()
  17711. "Move to next heading of the same level, and return point.
  17712. If there is no such heading, return nil.
  17713. This is like outline-next-sibling, but invisible headings are ok."
  17714. (let ((level (funcall outline-level)))
  17715. (outline-next-heading)
  17716. (while (and (not (eobp)) (> (funcall outline-level) level))
  17717. (outline-next-heading))
  17718. (if (or (eobp) (< (funcall outline-level) level))
  17719. nil
  17720. (point))))
  17721. (defun org-get-last-sibling ()
  17722. "Move to previous heading of the same level, and return point.
  17723. If there is no such heading, return nil."
  17724. (let ((opoint (point))
  17725. (level (funcall outline-level)))
  17726. (outline-previous-heading)
  17727. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17728. (while (and (> (funcall outline-level) level)
  17729. (not (bobp)))
  17730. (outline-previous-heading))
  17731. (if (< (funcall outline-level) level)
  17732. nil
  17733. (point)))))
  17734. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17735. ;; This contains an exact copy of the original function, but it uses
  17736. ;; `org-back-to-heading', to make it work also in invisible
  17737. ;; trees. And is uses an invisible-OK argument.
  17738. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17739. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17740. ;; with many children and grandchildren etc, this can be much faster
  17741. ;; than the outline version.
  17742. (org-back-to-heading invisible-OK)
  17743. (let ((first t)
  17744. (level (funcall outline-level)))
  17745. (if (and (org-mode-p) (< level 1000))
  17746. ;; A true heading (not a plain list item), in Org-mode
  17747. ;; This means we can easily find the end by looking
  17748. ;; only for the right number of stars. Using a regexp to do
  17749. ;; this is so much faster than using a Lisp loop.
  17750. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17751. (forward-char 1)
  17752. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17753. ;; something else, do it the slow way
  17754. (while (and (not (eobp))
  17755. (or first (> (funcall outline-level) level)))
  17756. (setq first nil)
  17757. (outline-next-heading)))
  17758. (unless to-heading
  17759. (if (memq (preceding-char) '(?\n ?\^M))
  17760. (progn
  17761. ;; Go to end of line before heading
  17762. (forward-char -1)
  17763. (if (memq (preceding-char) '(?\n ?\^M))
  17764. ;; leave blank line before heading
  17765. (forward-char -1))))))
  17766. (point))
  17767. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17768. "Use Org version in org-mode, for dramatic speed-up."
  17769. (if (eq major-mode 'org-mode)
  17770. (progn
  17771. (org-end-of-subtree nil t)
  17772. (unless (eobp) (backward-char 1)))
  17773. ad-do-it))
  17774. (defun org-forward-same-level (arg &optional invisible-ok)
  17775. "Move forward to the arg'th subheading at same level as this one.
  17776. Stop at the first and last subheadings of a superior heading.
  17777. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  17778. it wil also look at invisible ones."
  17779. (interactive "p")
  17780. (org-back-to-heading invisible-ok)
  17781. (org-on-heading-p)
  17782. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17783. (re (format "^\\*\\{1,%d\\} " level))
  17784. l)
  17785. (forward-char 1)
  17786. (while (> arg 0)
  17787. (while (and (re-search-forward re nil 'move)
  17788. (setq l (- (match-end 0) (match-beginning 0) 1))
  17789. (= l level)
  17790. (not invisible-ok)
  17791. (progn (backward-char 1) (org-invisible-p)))
  17792. (if (< l level) (setq arg 1)))
  17793. (setq arg (1- arg)))
  17794. (beginning-of-line 1)))
  17795. (defun org-backward-same-level (arg &optional invisible-ok)
  17796. "Move backward to the arg'th subheading at same level as this one.
  17797. Stop at the first and last subheadings of a superior heading."
  17798. (interactive "p")
  17799. (org-back-to-heading)
  17800. (org-on-heading-p)
  17801. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17802. (re (format "^\\*\\{1,%d\\} " level))
  17803. l)
  17804. (while (> arg 0)
  17805. (while (and (re-search-backward re nil 'move)
  17806. (setq l (- (match-end 0) (match-beginning 0) 1))
  17807. (= l level)
  17808. (not invisible-ok)
  17809. (org-invisible-p))
  17810. (if (< l level) (setq arg 1)))
  17811. (setq arg (1- arg)))))
  17812. (defun org-show-subtree ()
  17813. "Show everything after this heading at deeper levels."
  17814. (outline-flag-region
  17815. (point)
  17816. (save-excursion
  17817. (org-end-of-subtree t t))
  17818. nil))
  17819. (defun org-show-entry ()
  17820. "Show the body directly following this heading.
  17821. Show the heading too, if it is currently invisible."
  17822. (interactive)
  17823. (save-excursion
  17824. (condition-case nil
  17825. (progn
  17826. (org-back-to-heading t)
  17827. (outline-flag-region
  17828. (max (point-min) (1- (point)))
  17829. (save-excursion
  17830. (if (re-search-forward
  17831. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17832. (match-beginning 1)
  17833. (point-max)))
  17834. nil)
  17835. (org-cycle-hide-drawers 'children))
  17836. (error nil))))
  17837. (defun org-make-options-regexp (kwds &optional extra)
  17838. "Make a regular expression for keyword lines."
  17839. (concat
  17840. "^"
  17841. "#?[ \t]*\\+\\("
  17842. (mapconcat 'regexp-quote kwds "\\|")
  17843. (if extra (concat "\\|" extra))
  17844. "\\):[ \t]*"
  17845. "\\(.*\\)"))
  17846. ;; Make isearch reveal the necessary context
  17847. (defun org-isearch-end ()
  17848. "Reveal context after isearch exits."
  17849. (when isearch-success ; only if search was successful
  17850. (if (featurep 'xemacs)
  17851. ;; Under XEmacs, the hook is run in the correct place,
  17852. ;; we directly show the context.
  17853. (org-show-context 'isearch)
  17854. ;; In Emacs the hook runs *before* restoring the overlays.
  17855. ;; So we have to use a one-time post-command-hook to do this.
  17856. ;; (Emacs 22 has a special variable, see function `org-mode')
  17857. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17858. isearch-mode-end-hook-quit)
  17859. ;; Only when the isearch was not quitted.
  17860. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17861. 'append 'local)))))
  17862. (defun org-isearch-post-command ()
  17863. "Remove self from hook, and show context."
  17864. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17865. (org-show-context 'isearch))
  17866. ;;;; Integration with and fixes for other packages
  17867. ;;; Imenu support
  17868. (defvar org-imenu-markers nil
  17869. "All markers currently used by Imenu.")
  17870. (make-variable-buffer-local 'org-imenu-markers)
  17871. (defun org-imenu-new-marker (&optional pos)
  17872. "Return a new marker for use by Imenu, and remember the marker."
  17873. (let ((m (make-marker)))
  17874. (move-marker m (or pos (point)))
  17875. (push m org-imenu-markers)
  17876. m))
  17877. (defun org-imenu-get-tree ()
  17878. "Produce the index for Imenu."
  17879. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17880. (setq org-imenu-markers nil)
  17881. (let* ((n org-imenu-depth)
  17882. (re (concat "^" outline-regexp))
  17883. (subs (make-vector (1+ n) nil))
  17884. (last-level 0)
  17885. m level head)
  17886. (save-excursion
  17887. (save-restriction
  17888. (widen)
  17889. (goto-char (point-max))
  17890. (while (re-search-backward re nil t)
  17891. (setq level (org-reduced-level (funcall outline-level)))
  17892. (when (<= level n)
  17893. (looking-at org-complex-heading-regexp)
  17894. (setq head (org-link-display-format
  17895. (org-match-string-no-properties 4))
  17896. m (org-imenu-new-marker))
  17897. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17898. (if (>= level last-level)
  17899. (push (cons head m) (aref subs level))
  17900. (push (cons head (aref subs (1+ level))) (aref subs level))
  17901. (loop for i from (1+ level) to n do (aset subs i nil)))
  17902. (setq last-level level)))))
  17903. (aref subs 1)))
  17904. (eval-after-load "imenu"
  17905. '(progn
  17906. (add-hook 'imenu-after-jump-hook
  17907. (lambda ()
  17908. (if (eq major-mode 'org-mode)
  17909. (org-show-context 'org-goto))))))
  17910. (defun org-link-display-format (link)
  17911. "Replace a link with either the description, or the link target
  17912. if no description is present"
  17913. (save-match-data
  17914. (if (string-match org-bracket-link-analytic-regexp link)
  17915. (replace-match (if (match-end 5)
  17916. (match-string 5 link)
  17917. (concat (match-string 1 link)
  17918. (match-string 3 link)))
  17919. nil t link)
  17920. link)))
  17921. ;; Speedbar support
  17922. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17923. "Overlay marking the agenda restriction line in speedbar.")
  17924. (overlay-put org-speedbar-restriction-lock-overlay
  17925. 'face 'org-agenda-restriction-lock)
  17926. (overlay-put org-speedbar-restriction-lock-overlay
  17927. 'help-echo "Agendas are currently limited to this item.")
  17928. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17929. (defun org-speedbar-set-agenda-restriction ()
  17930. "Restrict future agenda commands to the location at point in speedbar.
  17931. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17932. (interactive)
  17933. (require 'org-agenda)
  17934. (let (p m tp np dir txt)
  17935. (cond
  17936. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17937. 'org-imenu t))
  17938. (setq m (get-text-property p 'org-imenu-marker))
  17939. (with-current-buffer (marker-buffer m)
  17940. (goto-char m)
  17941. (org-agenda-set-restriction-lock 'subtree)))
  17942. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17943. 'speedbar-function 'speedbar-find-file))
  17944. (setq tp (previous-single-property-change
  17945. (1+ p) 'speedbar-function)
  17946. np (next-single-property-change
  17947. tp 'speedbar-function)
  17948. dir (speedbar-line-directory)
  17949. txt (buffer-substring-no-properties (or tp (point-min))
  17950. (or np (point-max))))
  17951. (with-current-buffer (find-file-noselect
  17952. (let ((default-directory dir))
  17953. (expand-file-name txt)))
  17954. (unless (org-mode-p)
  17955. (error "Cannot restrict to non-Org-mode file"))
  17956. (org-agenda-set-restriction-lock 'file)))
  17957. (t (error "Don't know how to restrict Org-mode's agenda")))
  17958. (move-overlay org-speedbar-restriction-lock-overlay
  17959. (point-at-bol) (point-at-eol))
  17960. (setq current-prefix-arg nil)
  17961. (org-agenda-maybe-redo)))
  17962. (eval-after-load "speedbar"
  17963. '(progn
  17964. (speedbar-add-supported-extension ".org")
  17965. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17966. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17967. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17968. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17969. (add-hook 'speedbar-visiting-tag-hook
  17970. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17971. ;;; Fixes and Hacks for problems with other packages
  17972. ;; Make flyspell not check words in links, to not mess up our keymap
  17973. (defun org-mode-flyspell-verify ()
  17974. "Don't let flyspell put overlays at active buttons."
  17975. (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
  17976. (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
  17977. (defun org-remove-flyspell-overlays-in (beg end)
  17978. "Remove flyspell overlays in region."
  17979. (and (org-bound-and-true-p flyspell-mode)
  17980. (fboundp 'flyspell-delete-region-overlays)
  17981. (flyspell-delete-region-overlays beg end))
  17982. (add-text-properties beg end '(org-no-flyspell t)))
  17983. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17984. (eval-after-load "bookmark"
  17985. '(if (boundp 'bookmark-after-jump-hook)
  17986. ;; We can use the hook
  17987. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17988. ;; Hook not available, use advice
  17989. (defadvice bookmark-jump (after org-make-visible activate)
  17990. "Make the position visible."
  17991. (org-bookmark-jump-unhide))))
  17992. ;; Make sure saveplace shows the location if it was hidden
  17993. (eval-after-load "saveplace"
  17994. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17995. "Make the position visible."
  17996. (org-bookmark-jump-unhide)))
  17997. ;; Make sure ecb shows the location if it was hidden
  17998. (eval-after-load "ecb"
  17999. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  18000. "Make hierarchy visible when jumping into location from ECB tree buffer."
  18001. (if (eq major-mode 'org-mode)
  18002. (org-show-context))))
  18003. (defun org-bookmark-jump-unhide ()
  18004. "Unhide the current position, to show the bookmark location."
  18005. (and (org-mode-p)
  18006. (or (org-invisible-p)
  18007. (save-excursion (goto-char (max (point-min) (1- (point))))
  18008. (org-invisible-p)))
  18009. (org-show-context 'bookmark-jump)))
  18010. ;; Make session.el ignore our circular variable
  18011. (eval-after-load "session"
  18012. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  18013. ;;;; Experimental code
  18014. (defun org-closed-in-range ()
  18015. "Sparse tree of items closed in a certain time range.
  18016. Still experimental, may disappear in the future."
  18017. (interactive)
  18018. ;; Get the time interval from the user.
  18019. (let* ((time1 (org-float-time
  18020. (org-read-date nil 'to-time nil "Starting date: ")))
  18021. (time2 (org-float-time
  18022. (org-read-date nil 'to-time nil "End date:")))
  18023. ;; callback function
  18024. (callback (lambda ()
  18025. (let ((time
  18026. (org-float-time
  18027. (apply 'encode-time
  18028. (org-parse-time-string
  18029. (match-string 1))))))
  18030. ;; check if time in interval
  18031. (and (>= time time1) (<= time time2))))))
  18032. ;; make tree, check each match with the callback
  18033. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  18034. ;;;; Finish up
  18035. (provide 'org)
  18036. (run-hooks 'org-load-hook)
  18037. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  18038. ;;; org.el ends here