org.el 733 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587
  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.3
  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. (require 'pcomplete)
  73. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  74. (when (fboundp 'defvaralias)
  75. (unless (boundp 'calendar-view-holidays-initially-flag)
  76. (defvaralias 'calendar-view-holidays-initially-flag
  77. 'view-calendar-holidays-initially))
  78. (unless (boundp 'calendar-view-diary-initially-flag)
  79. (defvaralias 'calendar-view-diary-initially-flag
  80. 'view-diary-entries-initially))
  81. (unless (boundp 'diary-fancy-buffer)
  82. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  83. (require 'outline) (require 'noutline)
  84. ;; Other stuff we need.
  85. (require 'time-date)
  86. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  87. (require 'easymenu)
  88. (require 'overlay)
  89. (require 'org-macs)
  90. (require 'org-entities)
  91. (require 'org-compat)
  92. (require 'org-faces)
  93. (require 'org-list)
  94. (require 'org-complete)
  95. (require 'org-src)
  96. (require 'org-footnote)
  97. ;; babel
  98. (require 'ob)
  99. (require 'ob-table)
  100. (require 'ob-lob)
  101. (require 'ob-ref)
  102. (require 'ob-tangle)
  103. (require 'ob-comint)
  104. (require 'ob-keys)
  105. ;; load languages based on value of `org-babel-load-languages'
  106. (defvar org-babel-load-languages)
  107. ;;;###autoload
  108. (defun org-babel-do-load-languages (sym value)
  109. "Load the languages defined in `org-babel-load-languages'."
  110. (set-default sym value)
  111. (mapc (lambda (pair)
  112. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  113. (if active
  114. (progn
  115. (require (intern (concat "ob-" lang))))
  116. (progn
  117. (funcall 'fmakunbound
  118. (intern (concat "org-babel-execute:" lang)))
  119. (funcall 'fmakunbound
  120. (intern (concat "org-babel-expand-body:" lang)))))))
  121. org-babel-load-languages))
  122. (defcustom org-babel-load-languages '((emacs-lisp . t))
  123. "Languages which can be evaluated in Org-mode buffers.
  124. This list can be used to load support for any of the languages
  125. below, note that each language will depend on a different set of
  126. system executables and/or Emacs modes. When a language is
  127. \"loaded\", then code blocks in that language can be evaluated
  128. with `org-babel-execute-src-block' bound by default to C-c
  129. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  130. be set to remove code block evaluation from the C-c C-c
  131. keybinding. By default only Emacs Lisp (which has no
  132. requirements) is loaded."
  133. :group 'org-babel
  134. :set 'org-babel-do-load-languages
  135. :type '(alist :tag "Babel Languages"
  136. :key-type
  137. (choice
  138. (const :tag "C" C)
  139. (const :tag "R" R)
  140. (const :tag "Asymptote" asymptote)
  141. (const :tag "Calc" calc)
  142. (const :tag "Clojure" clojure)
  143. (const :tag "CSS" css)
  144. (const :tag "Ditaa" ditaa)
  145. (const :tag "Dot" dot)
  146. (const :tag "Emacs Lisp" emacs-lisp)
  147. (const :tag "Gnuplot" gnuplot)
  148. (const :tag "Haskell" haskell)
  149. (const :tag "Javascript" js)
  150. (const :tag "Latex" latex)
  151. (const :tag "Ledger" ledger)
  152. (const :tag "Matlab" matlab)
  153. (const :tag "Mscgen" mscgen)
  154. (const :tag "Ocaml" ocaml)
  155. (const :tag "Octave" octave)
  156. (const :tag "Org" org)
  157. (const :tag "Perl" perl)
  158. (const :tag "PlantUML" plantuml)
  159. (const :tag "Python" python)
  160. (const :tag "Ruby" ruby)
  161. (const :tag "Sass" sass)
  162. (const :tag "Scheme" scheme)
  163. (const :tag "Screen" screen)
  164. (const :tag "Shell Script" sh)
  165. (const :tag "Sql" sql)
  166. (const :tag "Sqlite" sqlite))
  167. :value-type (boolean :tag "Activate" :value t)))
  168. ;;;; Customization variables
  169. (defcustom org-clone-delete-id nil
  170. "Remove ID property of clones of a subtree.
  171. When non-nil, clones of a subtree don't inherit the ID property.
  172. Otherwise they inherit the ID property with a new unique
  173. identifier."
  174. :type 'boolean
  175. :group 'org-id)
  176. ;;; Version
  177. (defconst org-version "7.3"
  178. "The version number of the file org.el.")
  179. (defun org-version (&optional here)
  180. "Show the org-mode version in the echo area.
  181. With prefix arg HERE, insert it at point."
  182. (interactive "P")
  183. (let* ((origin default-directory)
  184. (version org-version)
  185. (git-version)
  186. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  187. (when (and (file-exists-p (expand-file-name ".git" dir))
  188. (executable-find "git"))
  189. (unwind-protect
  190. (progn
  191. (cd dir)
  192. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  193. (with-current-buffer "*Shell Command Output*"
  194. (goto-char (point-min))
  195. (setq git-version (buffer-substring (point) (point-at-eol))))
  196. (subst-char-in-string ?- ?. git-version t)
  197. (when (string-match "\\S-"
  198. (shell-command-to-string
  199. "git diff-index --name-only HEAD --"))
  200. (setq git-version (concat git-version ".dirty")))
  201. (setq version (concat version " (" git-version ")"))))
  202. (cd origin)))
  203. (setq version (format "Org-mode version %s" version))
  204. (if here (insert version))
  205. (message version)))
  206. ;;; Compatibility constants
  207. ;;; The custom variables
  208. (defgroup org nil
  209. "Outline-based notes management and organizer."
  210. :tag "Org"
  211. :group 'outlines
  212. :group 'calendar)
  213. (defcustom org-mode-hook nil
  214. "Mode hook for Org-mode, run after the mode was turned on."
  215. :group 'org
  216. :type 'hook)
  217. (defcustom org-load-hook nil
  218. "Hook that is run after org.el has been loaded."
  219. :group 'org
  220. :type 'hook)
  221. (defvar org-modules) ; defined below
  222. (defvar org-modules-loaded nil
  223. "Have the modules been loaded already?")
  224. (defun org-load-modules-maybe (&optional force)
  225. "Load all extensions listed in `org-modules'."
  226. (when (or force (not org-modules-loaded))
  227. (mapc (lambda (ext)
  228. (condition-case nil (require ext)
  229. (error (message "Problems while trying to load feature `%s'" ext))))
  230. org-modules)
  231. (setq org-modules-loaded t)))
  232. (defun org-set-modules (var value)
  233. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  234. (set var value)
  235. (when (featurep 'org)
  236. (org-load-modules-maybe 'force)))
  237. (when (org-bound-and-true-p org-modules)
  238. (let ((a (member 'org-infojs org-modules)))
  239. (and a (setcar a 'org-jsinfo))))
  240. (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)
  241. "Modules that should always be loaded together with org.el.
  242. If a description starts with <C>, the file is not part of Emacs
  243. and loading it will require that you have downloaded and properly installed
  244. the org-mode distribution.
  245. You can also use this system to load external packages (i.e. neither Org
  246. core modules, nor modules from the CONTRIB directory). Just add symbols
  247. to the end of the list. If the package is called org-xyz.el, then you need
  248. to add the symbol `xyz', and the package must have a call to
  249. (provide 'org-xyz)"
  250. :group 'org
  251. :set 'org-set-modules
  252. :type
  253. '(set :greedy t
  254. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  255. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  256. (const :tag " crypt: Encryption of subtrees" org-crypt)
  257. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  258. (const :tag " docview: Links to doc-view buffers" org-docview)
  259. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  260. (const :tag " id: Global IDs for identifying entries" org-id)
  261. (const :tag " info: Links to Info nodes" org-info)
  262. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  263. (const :tag " habit: Track your consistency with habits" org-habit)
  264. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  265. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  266. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  267. (const :tag " mew Links to Mew folders/messages" org-mew)
  268. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  269. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  270. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  271. (const :tag " vm: Links to VM folders/messages" org-vm)
  272. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  273. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  274. (const :tag " mouse: Additional mouse support" org-mouse)
  275. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  276. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  277. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  278. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  279. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  280. (const :tag "C collector: Collect properties into tables" org-collector)
  281. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  282. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  283. (const :tag "C eval: Include command output as text" org-eval)
  284. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  285. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  286. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  287. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  288. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  289. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  290. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  291. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  292. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  293. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  294. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  295. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  296. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  297. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  298. (const :tag "C registry: A registry for Org-mode links" org-registry)
  299. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  300. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  301. (const :tag "C secretary: Team management with org-mode" org-secretary)
  302. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  303. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  304. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  305. (const :tag "C track: Keep up with Org-mode development" org-track)
  306. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  307. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  308. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  309. (defcustom org-support-shift-select nil
  310. "Non-nil means make shift-cursor commands select text when possible.
  311. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  312. selecting a region, or enlarge regions started in this way.
  313. In Org-mode, in special contexts, these same keys are used for other
  314. purposes, important enough to compete with shift selection. Org tries
  315. to balance these needs by supporting `shift-select-mode' outside these
  316. special contexts, under control of this variable.
  317. The default of this variable is nil, to avoid confusing behavior. Shifted
  318. cursor keys will then execute Org commands in the following contexts:
  319. - on a headline, changing TODO state (left/right) and priority (up/down)
  320. - on a time stamp, changing the time
  321. - in a plain list item, changing the bullet type
  322. - in a property definition line, switching between allowed values
  323. - in the BEGIN line of a clock table (changing the time block).
  324. Outside these contexts, the commands will throw an error.
  325. When this variable is t and the cursor is not in a special context,
  326. Org-mode will support shift-selection for making and enlarging regions.
  327. To make this more effective, the bullet cycling will no longer happen
  328. anywhere in an item line, but only if the cursor is exactly on the bullet.
  329. If you set this variable to the symbol `always', then the keys
  330. will not be special in headlines, property lines, and item lines, to make
  331. shift selection work there as well. If this is what you want, you can
  332. use the following alternative commands: `C-c C-t' and `C-c ,' to
  333. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  334. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  335. edited by hand or in column view.
  336. However, when the cursor is on a timestamp, shift-cursor commands
  337. will still edit the time stamp - this is just too good to give up.
  338. XEmacs user should have this variable set to nil, because shift-select-mode
  339. is Emacs 23 only."
  340. :group 'org
  341. :type '(choice
  342. (const :tag "Never" nil)
  343. (const :tag "When outside special context" t)
  344. (const :tag "Everywhere except timestamps" always)))
  345. (defgroup org-startup nil
  346. "Options concerning startup of Org-mode."
  347. :tag "Org Startup"
  348. :group 'org)
  349. (defcustom org-startup-folded t
  350. "Non-nil means entering Org-mode will switch to OVERVIEW.
  351. This can also be configured on a per-file basis by adding one of
  352. the following lines anywhere in the buffer:
  353. #+STARTUP: fold (or `overview', this is equivalent)
  354. #+STARTUP: nofold (or `showall', this is equivalent)
  355. #+STARTUP: content
  356. #+STARTUP: showeverything"
  357. :group 'org-startup
  358. :type '(choice
  359. (const :tag "nofold: show all" nil)
  360. (const :tag "fold: overview" t)
  361. (const :tag "content: all headlines" content)
  362. (const :tag "show everything, even drawers" showeverything)))
  363. (defcustom org-startup-truncated t
  364. "Non-nil means entering Org-mode will set `truncate-lines'.
  365. This is useful since some lines containing links can be very long and
  366. uninteresting. Also tables look terrible when wrapped."
  367. :group 'org-startup
  368. :type 'boolean)
  369. (defcustom org-startup-indented nil
  370. "Non-nil means turn on `org-indent-mode' on startup.
  371. This can also be configured on a per-file basis by adding one of
  372. the following lines anywhere in the buffer:
  373. #+STARTUP: indent
  374. #+STARTUP: noindent"
  375. :group 'org-structure
  376. :type '(choice
  377. (const :tag "Not" nil)
  378. (const :tag "Globally (slow on startup in large files)" t)))
  379. (defcustom org-use-sub-superscripts t
  380. "Non-nil means interpret \"_\" and \"^\" for export.
  381. When this option is turned on, you can use TeX-like syntax for sub- and
  382. superscripts. Several characters after \"_\" or \"^\" will be
  383. considered as a single item - so grouping with {} is normally not
  384. needed. For example, the following things will be parsed as single
  385. sub- or superscripts.
  386. 10^24 or 10^tau several digits will be considered 1 item.
  387. 10^-12 or 10^-tau a leading sign with digits or a word
  388. x^2-y^3 will be read as x^2 - y^3, because items are
  389. terminated by almost any nonword/nondigit char.
  390. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  391. Still, ambiguity is possible - so when in doubt use {} to enclose the
  392. sub/superscript. If you set this variable to the symbol `{}',
  393. the braces are *required* in order to trigger interpretations as
  394. sub/superscript. This can be helpful in documents that need \"_\"
  395. frequently in plain text.
  396. Not all export backends support this, but HTML does.
  397. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  398. :group 'org-startup
  399. :group 'org-export-translation
  400. :type '(choice
  401. (const :tag "Always interpret" t)
  402. (const :tag "Only with braces" {})
  403. (const :tag "Never interpret" nil)))
  404. (if (fboundp 'defvaralias)
  405. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  406. (defcustom org-startup-with-beamer-mode nil
  407. "Non-nil means turn on `org-beamer-mode' on startup.
  408. This can also be configured on a per-file basis by adding one of
  409. the following lines anywhere in the buffer:
  410. #+STARTUP: beamer"
  411. :group 'org-startup
  412. :type 'boolean)
  413. (defcustom org-startup-align-all-tables nil
  414. "Non-nil means align all tables when visiting a file.
  415. This is useful when the column width in tables is forced with <N> cookies
  416. in table fields. Such tables will look correct only after the first re-align.
  417. This can also be configured on a per-file basis by adding one of
  418. the following lines anywhere in the buffer:
  419. #+STARTUP: align
  420. #+STARTUP: noalign"
  421. :group 'org-startup
  422. :type 'boolean)
  423. (defcustom org-startup-with-inline-images nil
  424. "Non-nil means show inline images when loading a new Org file.
  425. This can also be configured on a per-file basis by adding one of
  426. the following lines anywhere in the buffer:
  427. #+STARTUP: inlineimages
  428. #+STARTUP: noinlineimages"
  429. :group 'org-startup
  430. :type 'boolean)
  431. (defcustom org-insert-mode-line-in-empty-file nil
  432. "Non-nil means insert the first line setting Org-mode in empty files.
  433. When the function `org-mode' is called interactively in an empty file, this
  434. normally means that the file name does not automatically trigger Org-mode.
  435. To ensure that the file will always be in Org-mode in the future, a
  436. line enforcing Org-mode will be inserted into the buffer, if this option
  437. has been set."
  438. :group 'org-startup
  439. :type 'boolean)
  440. (defcustom org-replace-disputed-keys nil
  441. "Non-nil means use alternative key bindings for some keys.
  442. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  443. These keys are also used by other packages like shift-selection-mode'
  444. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  445. If you want to use Org-mode together with one of these other modes,
  446. or more generally if you would like to move some Org-mode commands to
  447. other keys, set this variable and configure the keys with the variable
  448. `org-disputed-keys'.
  449. This option is only relevant at load-time of Org-mode, and must be set
  450. *before* org.el is loaded. Changing it requires a restart of Emacs to
  451. become effective."
  452. :group 'org-startup
  453. :type 'boolean)
  454. (defcustom org-use-extra-keys nil
  455. "Non-nil means use extra key sequence definitions for certain commands.
  456. This happens automatically if you run XEmacs or if `window-system'
  457. is nil. This variable lets you do the same manually. You must
  458. set it before loading org.
  459. Example: on Carbon Emacs 22 running graphically, with an external
  460. keyboard on a Powerbook, the default way of setting M-left might
  461. not work for either Alt or ESC. Setting this variable will make
  462. it work for ESC."
  463. :group 'org-startup
  464. :type 'boolean)
  465. (if (fboundp 'defvaralias)
  466. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  467. (defcustom org-disputed-keys
  468. '(([(shift up)] . [(meta p)])
  469. ([(shift down)] . [(meta n)])
  470. ([(shift left)] . [(meta -)])
  471. ([(shift right)] . [(meta +)])
  472. ([(control shift right)] . [(meta shift +)])
  473. ([(control shift left)] . [(meta shift -)]))
  474. "Keys for which Org-mode and other modes compete.
  475. This is an alist, cars are the default keys, second element specifies
  476. the alternative to use when `org-replace-disputed-keys' is t.
  477. Keys can be specified in any syntax supported by `define-key'.
  478. The value of this option takes effect only at Org-mode's startup,
  479. therefore you'll have to restart Emacs to apply it after changing."
  480. :group 'org-startup
  481. :type 'alist)
  482. (defun org-key (key)
  483. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  484. Or return the original if not disputed.
  485. Also apply the translations defined in `org-xemacs-key-equivalents'."
  486. (when org-replace-disputed-keys
  487. (let* ((nkey (key-description key))
  488. (x (org-find-if (lambda (x)
  489. (equal (key-description (car x)) nkey))
  490. org-disputed-keys)))
  491. (setq key (if x (cdr x) key))))
  492. (when (featurep 'xemacs)
  493. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  494. key)
  495. (defun org-find-if (predicate seq)
  496. (catch 'exit
  497. (while seq
  498. (if (funcall predicate (car seq))
  499. (throw 'exit (car seq))
  500. (pop seq)))))
  501. (defun org-defkey (keymap key def)
  502. "Define a key, possibly translated, as returned by `org-key'."
  503. (define-key keymap (org-key key) def))
  504. (defcustom org-ellipsis nil
  505. "The ellipsis to use in the Org-mode outline.
  506. When nil, just use the standard three dots. When a string, use that instead,
  507. When a face, use the standard 3 dots, but with the specified face.
  508. The change affects only Org-mode (which will then use its own display table).
  509. Changing this requires executing `M-x org-mode' in a buffer to become
  510. effective."
  511. :group 'org-startup
  512. :type '(choice (const :tag "Default" nil)
  513. (face :tag "Face" :value org-warning)
  514. (string :tag "String" :value "...#")))
  515. (defvar org-display-table nil
  516. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  517. (defgroup org-keywords nil
  518. "Keywords in Org-mode."
  519. :tag "Org Keywords"
  520. :group 'org)
  521. (defcustom org-deadline-string "DEADLINE:"
  522. "String to mark deadline entries.
  523. A deadline is this string, followed by a time stamp. Should be a word,
  524. terminated by a colon. You can insert a schedule keyword and
  525. a timestamp with \\[org-deadline].
  526. Changes become only effective after restarting Emacs."
  527. :group 'org-keywords
  528. :type 'string)
  529. (defcustom org-scheduled-string "SCHEDULED:"
  530. "String to mark scheduled TODO entries.
  531. A schedule is this string, followed by a time stamp. Should be a word,
  532. terminated by a colon. You can insert a schedule keyword and
  533. a timestamp with \\[org-schedule].
  534. Changes become only effective after restarting Emacs."
  535. :group 'org-keywords
  536. :type 'string)
  537. (defcustom org-closed-string "CLOSED:"
  538. "String used as the prefix for timestamps logging closing a TODO entry."
  539. :group 'org-keywords
  540. :type 'string)
  541. (defcustom org-clock-string "CLOCK:"
  542. "String used as prefix for timestamps clocking work hours on an item."
  543. :group 'org-keywords
  544. :type 'string)
  545. (defcustom org-comment-string "COMMENT"
  546. "Entries starting with this keyword will never be exported.
  547. An entry can be toggled between COMMENT and normal with
  548. \\[org-toggle-comment].
  549. Changes become only effective after restarting Emacs."
  550. :group 'org-keywords
  551. :type 'string)
  552. (defcustom org-quote-string "QUOTE"
  553. "Entries starting with this keyword will be exported in fixed-width font.
  554. Quoting applies only to the text in the entry following the headline, and does
  555. not extend beyond the next headline, even if that is lower level.
  556. An entry can be toggled between QUOTE and normal with
  557. \\[org-toggle-fixed-width-section]."
  558. :group 'org-keywords
  559. :type 'string)
  560. (defconst org-repeat-re
  561. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  562. "Regular expression for specifying repeated events.
  563. After a match, group 1 contains the repeat expression.")
  564. (defgroup org-structure nil
  565. "Options concerning the general structure of Org-mode files."
  566. :tag "Org Structure"
  567. :group 'org)
  568. (defgroup org-reveal-location nil
  569. "Options about how to make context of a location visible."
  570. :tag "Org Reveal Location"
  571. :group 'org-structure)
  572. (defconst org-context-choice
  573. '(choice
  574. (const :tag "Always" t)
  575. (const :tag "Never" nil)
  576. (repeat :greedy t :tag "Individual contexts"
  577. (cons
  578. (choice :tag "Context"
  579. (const agenda)
  580. (const org-goto)
  581. (const occur-tree)
  582. (const tags-tree)
  583. (const link-search)
  584. (const mark-goto)
  585. (const bookmark-jump)
  586. (const isearch)
  587. (const default))
  588. (boolean))))
  589. "Contexts for the reveal options.")
  590. (defcustom org-show-hierarchy-above '((default . t))
  591. "Non-nil means show full hierarchy when revealing a location.
  592. Org-mode often shows locations in an org-mode file which might have
  593. been invisible before. When this is set, the hierarchy of headings
  594. above the exposed location is shown.
  595. Turning this off for example for sparse trees makes them very compact.
  596. Instead of t, this can also be an alist specifying this option for different
  597. contexts. Valid contexts are
  598. agenda when exposing an entry from the agenda
  599. org-goto when using the command `org-goto' on key C-c C-j
  600. occur-tree when using the command `org-occur' on key C-c /
  601. tags-tree when constructing a sparse tree based on tags matches
  602. link-search when exposing search matches associated with a link
  603. mark-goto when exposing the jump goal of a mark
  604. bookmark-jump when exposing a bookmark location
  605. isearch when exiting from an incremental search
  606. default default for all contexts not set explicitly"
  607. :group 'org-reveal-location
  608. :type org-context-choice)
  609. (defcustom org-show-following-heading '((default . nil))
  610. "Non-nil means show following heading when revealing a location.
  611. Org-mode often shows locations in an org-mode file which might have
  612. been invisible before. When this is set, the heading following the
  613. match is shown.
  614. Turning this off for example for sparse trees makes them very compact,
  615. but makes it harder to edit the location of the match. In such a case,
  616. use the command \\[org-reveal] to show more context.
  617. Instead of t, this can also be an alist specifying this option for different
  618. contexts. See `org-show-hierarchy-above' for valid contexts."
  619. :group 'org-reveal-location
  620. :type org-context-choice)
  621. (defcustom org-show-siblings '((default . nil) (isearch t))
  622. "Non-nil means show all sibling heading when revealing a location.
  623. Org-mode often shows locations in an org-mode file which might have
  624. been invisible before. When this is set, the sibling of the current entry
  625. heading are all made visible. If `org-show-hierarchy-above' is t,
  626. the same happens on each level of the hierarchy above the current entry.
  627. By default this is on for the isearch context, off for all other contexts.
  628. Turning this off for example for sparse trees makes them very compact,
  629. but makes it harder to edit the location of the match. In such a case,
  630. use the command \\[org-reveal] to show more context.
  631. Instead of t, this can also be an alist specifying this option for different
  632. contexts. See `org-show-hierarchy-above' for valid contexts."
  633. :group 'org-reveal-location
  634. :type org-context-choice)
  635. (defcustom org-show-entry-below '((default . nil))
  636. "Non-nil means show the entry below a headline when revealing a location.
  637. Org-mode often shows locations in an org-mode file which might have
  638. been invisible before. When this is set, the text below the headline that is
  639. exposed is also shown.
  640. By default this is off for all contexts.
  641. Instead of t, this can also be an alist specifying this option for different
  642. contexts. See `org-show-hierarchy-above' for valid contexts."
  643. :group 'org-reveal-location
  644. :type org-context-choice)
  645. (defcustom org-indirect-buffer-display 'other-window
  646. "How should indirect tree buffers be displayed?
  647. This applies to indirect buffers created with the commands
  648. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  649. Valid values are:
  650. current-window Display in the current window
  651. other-window Just display in another window.
  652. dedicated-frame Create one new frame, and re-use it each time.
  653. new-frame Make a new frame each time. Note that in this case
  654. previously-made indirect buffers are kept, and you need to
  655. kill these buffers yourself."
  656. :group 'org-structure
  657. :group 'org-agenda-windows
  658. :type '(choice
  659. (const :tag "In current window" current-window)
  660. (const :tag "In current frame, other window" other-window)
  661. (const :tag "Each time a new frame" new-frame)
  662. (const :tag "One dedicated frame" dedicated-frame)))
  663. (defcustom org-use-speed-commands nil
  664. "Non-nil means activate single letter commands at beginning of a headline.
  665. This may also be a function to test for appropriate locations where speed
  666. commands should be active."
  667. :group 'org-structure
  668. :type '(choice
  669. (const :tag "Never" nil)
  670. (const :tag "At beginning of headline stars" t)
  671. (function)))
  672. (defcustom org-speed-commands-user nil
  673. "Alist of additional speed commands.
  674. This list will be checked before `org-speed-commands-default'
  675. when the variable `org-use-speed-commands' is non-nil
  676. and when the cursor is at the beginning of a headline.
  677. The car if each entry is a string with a single letter, which must
  678. be assigned to `self-insert-command' in the global map.
  679. The cdr is either a command to be called interactively, a function
  680. to be called, or a form to be evaluated.
  681. An entry that is just a list with a single string will be interpreted
  682. as a descriptive headline that will be added when listing the speed
  683. commands in the Help buffer using the `?' speed command."
  684. :group 'org-structure
  685. :type '(repeat :value ("k" . ignore)
  686. (choice :value ("k" . ignore)
  687. (list :tag "Descriptive Headline" (string :tag "Headline"))
  688. (cons :tag "Letter and Command"
  689. (string :tag "Command letter")
  690. (choice
  691. (function)
  692. (sexp))))))
  693. (defgroup org-cycle nil
  694. "Options concerning visibility cycling in Org-mode."
  695. :tag "Org Cycle"
  696. :group 'org-structure)
  697. (defcustom org-cycle-skip-children-state-if-no-children t
  698. "Non-nil means skip CHILDREN state in entries that don't have any."
  699. :group 'org-cycle
  700. :type 'boolean)
  701. (defcustom org-cycle-max-level nil
  702. "Maximum level which should still be subject to visibility cycling.
  703. Levels higher than this will, for cycling, be treated as text, not a headline.
  704. When `org-odd-levels-only' is set, a value of N in this variable actually
  705. means 2N-1 stars as the limiting headline.
  706. When nil, cycle all levels.
  707. Note that the limiting level of cycling is also influenced by
  708. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  709. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  710. than its value."
  711. :group 'org-cycle
  712. :type '(choice
  713. (const :tag "No limit" nil)
  714. (integer :tag "Maximum level")))
  715. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  716. "Names of drawers. Drawers are not opened by cycling on the headline above.
  717. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  718. this:
  719. :DRAWERNAME:
  720. .....
  721. :END:
  722. The drawer \"PROPERTIES\" is special for capturing properties through
  723. the property API.
  724. Drawers can be defined on the per-file basis with a line like:
  725. #+DRAWERS: HIDDEN STATE PROPERTIES"
  726. :group 'org-structure
  727. :group 'org-cycle
  728. :type '(repeat (string :tag "Drawer Name")))
  729. (defcustom org-hide-block-startup nil
  730. "Non-nil means entering Org-mode will fold all blocks.
  731. This can also be set in on a per-file basis with
  732. #+STARTUP: hideblocks
  733. #+STARTUP: showblocks"
  734. :group 'org-startup
  735. :group 'org-cycle
  736. :type 'boolean)
  737. (defcustom org-cycle-global-at-bob nil
  738. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  739. This makes it possible to do global cycling without having to use S-TAB or
  740. \\[universal-argument] TAB. For this special case to work, the first line \
  741. of the buffer
  742. must not be a headline - it may be empty or some other text. When used in
  743. this way, `org-cycle-hook' is disables temporarily, to make sure the
  744. cursor stays at the beginning of the buffer.
  745. When this option is nil, don't do anything special at the beginning
  746. of the buffer."
  747. :group 'org-cycle
  748. :type 'boolean)
  749. (defcustom org-cycle-level-after-item/entry-creation t
  750. "Non-nil means cycle entry level or item indentation in new empty entries.
  751. When the cursor is at the end of an empty headline, i.e with only stars
  752. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  753. and then all possible ancestor states, before returning to the original state.
  754. This makes data entry extremely fast: M-RET to create a new headline,
  755. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  756. When the cursor is at the end of an empty plain list item, one TAB will
  757. make it a subitem, two or more tabs will back up to make this an item
  758. higher up in the item hierarchy."
  759. :group 'org-cycle
  760. :type 'boolean)
  761. (defcustom org-cycle-emulate-tab t
  762. "Where should `org-cycle' emulate TAB.
  763. nil Never
  764. white Only in completely white lines
  765. whitestart Only at the beginning of lines, before the first non-white char
  766. t Everywhere except in headlines
  767. exc-hl-bol Everywhere except at the start of a headline
  768. If TAB is used in a place where it does not emulate TAB, the current subtree
  769. visibility is cycled."
  770. :group 'org-cycle
  771. :type '(choice (const :tag "Never" nil)
  772. (const :tag "Only in completely white lines" white)
  773. (const :tag "Before first char in a line" whitestart)
  774. (const :tag "Everywhere except in headlines" t)
  775. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  776. ))
  777. (defcustom org-cycle-separator-lines 2
  778. "Number of empty lines needed to keep an empty line between collapsed trees.
  779. If you leave an empty line between the end of a subtree and the following
  780. headline, this empty line is hidden when the subtree is folded.
  781. Org-mode will leave (exactly) one empty line visible if the number of
  782. empty lines is equal or larger to the number given in this variable.
  783. So the default 2 means at least 2 empty lines after the end of a subtree
  784. are needed to produce free space between a collapsed subtree and the
  785. following headline.
  786. If the number is negative, and the number of empty lines is at least -N,
  787. all empty lines are shown.
  788. Special case: when 0, never leave empty lines in collapsed view."
  789. :group 'org-cycle
  790. :type 'integer)
  791. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  792. (defcustom org-pre-cycle-hook nil
  793. "Hook that is run before visibility cycling is happening.
  794. The function(s) in this hook must accept a single argument which indicates
  795. the new state that will be set right after running this hook. The
  796. argument is a symbol. Before a global state change, it can have the values
  797. `overview', `content', or `all'. Before a local state change, it can have
  798. the values `folded', `children', or `subtree'."
  799. :group 'org-cycle
  800. :type 'hook)
  801. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  802. org-cycle-hide-drawers
  803. org-cycle-show-empty-lines
  804. org-optimize-window-after-visibility-change)
  805. "Hook that is run after `org-cycle' has changed the buffer visibility.
  806. The function(s) in this hook must accept a single argument which indicates
  807. the new state that was set by the most recent `org-cycle' command. The
  808. argument is a symbol. After a global state change, it can have the values
  809. `overview', `content', or `all'. After a local state change, it can have
  810. the values `folded', `children', or `subtree'."
  811. :group 'org-cycle
  812. :type 'hook)
  813. (defgroup org-edit-structure nil
  814. "Options concerning structure editing in Org-mode."
  815. :tag "Org Edit Structure"
  816. :group 'org-structure)
  817. (defcustom org-odd-levels-only nil
  818. "Non-nil means skip even levels and only use odd levels for the outline.
  819. This has the effect that two stars are being added/taken away in
  820. promotion/demotion commands. It also influences how levels are
  821. handled by the exporters.
  822. Changing it requires restart of `font-lock-mode' to become effective
  823. for fontification also in regions already fontified.
  824. You may also set this on a per-file basis by adding one of the following
  825. lines to the buffer:
  826. #+STARTUP: odd
  827. #+STARTUP: oddeven"
  828. :group 'org-edit-structure
  829. :group 'org-appearance
  830. :type 'boolean)
  831. (defcustom org-adapt-indentation t
  832. "Non-nil means adapt indentation to outline node level.
  833. When this variable is set, Org assumes that you write outlines by
  834. indenting text in each node to align with the headline (after the stars).
  835. The following issues are influenced by this variable:
  836. - When this is set and the *entire* text in an entry is indented, the
  837. indentation is increased by one space in a demotion command, and
  838. decreased by one in a promotion command. If any line in the entry
  839. body starts with text at column 0, indentation is not changed at all.
  840. - Property drawers and planning information is inserted indented when
  841. this variable s set. When nil, they will not be indented.
  842. - TAB indents a line relative to context. The lines below a headline
  843. will be indented when this variable is set.
  844. Note that this is all about true indentation, by adding and removing
  845. space characters. See also `org-indent.el' which does level-dependent
  846. indentation in a virtual way, i.e. at display time in Emacs."
  847. :group 'org-edit-structure
  848. :type 'boolean)
  849. (defcustom org-special-ctrl-a/e nil
  850. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  851. When t, `C-a' will bring back the cursor to the beginning of the
  852. headline text, i.e. after the stars and after a possible TODO keyword.
  853. In an item, this will be the position after the bullet.
  854. When the cursor is already at that position, another `C-a' will bring
  855. it to the beginning of the line.
  856. `C-e' will jump to the end of the headline, ignoring the presence of tags
  857. in the headline. A second `C-e' will then jump to the true end of the
  858. line, after any tags. This also means that, when this variable is
  859. non-nil, `C-e' also will never jump beyond the end of the heading of a
  860. folded section, i.e. not after the ellipses.
  861. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  862. going to the true line boundary first. Only a directly following, identical
  863. keypress will bring the cursor to the special positions.
  864. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  865. set separately."
  866. :group 'org-edit-structure
  867. :type '(choice
  868. (const :tag "off" nil)
  869. (const :tag "on: after stars/bullet and before tags first" t)
  870. (const :tag "reversed: true line boundary first" reversed)
  871. (cons :tag "Set C-a and C-e separately"
  872. (choice :tag "Special C-a"
  873. (const :tag "off" nil)
  874. (const :tag "on: after stars/bullet first" t)
  875. (const :tag "reversed: before stars/bullet first" reversed))
  876. (choice :tag "Special C-e"
  877. (const :tag "off" nil)
  878. (const :tag "on: before tags first" t)
  879. (const :tag "reversed: after tags first" reversed)))))
  880. (if (fboundp 'defvaralias)
  881. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  882. (defcustom org-special-ctrl-k nil
  883. "Non-nil means `C-k' will behave specially in headlines.
  884. When nil, `C-k' will call the default `kill-line' command.
  885. When t, the following will happen while the cursor is in the headline:
  886. - When the cursor is at the beginning of a headline, kill the entire
  887. line and possible the folded subtree below the line.
  888. - When in the middle of the headline text, kill the headline up to the tags.
  889. - When after the headline text, kill the tags."
  890. :group 'org-edit-structure
  891. :type 'boolean)
  892. (defcustom org-ctrl-k-protect-subtree nil
  893. "Non-nil means, do not delete a hidden subtree with C-k.
  894. When set to the symbol `error', simply throw an error when C-k is
  895. used to kill (part-of) a headline that has hidden text behind it.
  896. Any other non-nil value will result in a query to the user, if it is
  897. OK to kill that hidden subtree. When nil, kill without remorse."
  898. :group 'org-edit-structure
  899. :type '(choice
  900. (const :tag "Do not protect hidden subtrees" nil)
  901. (const :tag "Protect hidden subtrees with a security query" t)
  902. (const :tag "Never kill a hidden subtree with C-k" error)))
  903. (defcustom org-yank-folded-subtrees t
  904. "Non-nil means when yanking subtrees, fold them.
  905. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  906. it starts with a heading and all other headings in it are either children
  907. or siblings, then fold all the subtrees. However, do this only if no
  908. text after the yank would be swallowed into a folded tree by this action."
  909. :group 'org-edit-structure
  910. :type 'boolean)
  911. (defcustom org-yank-adjusted-subtrees nil
  912. "Non-nil means when yanking subtrees, adjust the level.
  913. With this setting, `org-paste-subtree' is used to insert the subtree, see
  914. this function for details."
  915. :group 'org-edit-structure
  916. :type 'boolean)
  917. (defcustom org-M-RET-may-split-line '((default . t))
  918. "Non-nil means M-RET will split the line at the cursor position.
  919. When nil, it will go to the end of the line before making a
  920. new line.
  921. You may also set this option in a different way for different
  922. contexts. Valid contexts are:
  923. headline when creating a new headline
  924. item when creating a new item
  925. table in a table field
  926. default the value to be used for all contexts not explicitly
  927. customized"
  928. :group 'org-structure
  929. :group 'org-table
  930. :type '(choice
  931. (const :tag "Always" t)
  932. (const :tag "Never" nil)
  933. (repeat :greedy t :tag "Individual contexts"
  934. (cons
  935. (choice :tag "Context"
  936. (const headline)
  937. (const item)
  938. (const table)
  939. (const default))
  940. (boolean)))))
  941. (defcustom org-insert-heading-respect-content nil
  942. "Non-nil means insert new headings after the current subtree.
  943. When nil, the new heading is created directly after the current line.
  944. The commands \\[org-insert-heading-respect-content] and
  945. \\[org-insert-todo-heading-respect-content] turn this variable on
  946. for the duration of the command."
  947. :group 'org-structure
  948. :type 'boolean)
  949. (defcustom org-blank-before-new-entry '((heading . auto)
  950. (plain-list-item . auto))
  951. "Should `org-insert-heading' leave a blank line before new heading/item?
  952. The value is an alist, with `heading' and `plain-list-item' as car,
  953. and a boolean flag as cdr. The cdr may lso be the symbol `auto', and then
  954. Org will look at the surrounding headings/items and try to make an
  955. intelligent decision wether to insert a blank line or not.
  956. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  957. set, the setting here is ignored and no empty line is inserted, to avoid
  958. breaking the list structure."
  959. :group 'org-edit-structure
  960. :type '(list
  961. (cons (const heading)
  962. (choice (const :tag "Never" nil)
  963. (const :tag "Always" t)
  964. (const :tag "Auto" auto)))
  965. (cons (const plain-list-item)
  966. (choice (const :tag "Never" nil)
  967. (const :tag "Always" t)
  968. (const :tag "Auto" auto)))))
  969. (defcustom org-insert-heading-hook nil
  970. "Hook being run after inserting a new heading."
  971. :group 'org-edit-structure
  972. :type 'hook)
  973. (defcustom org-enable-fixed-width-editor t
  974. "Non-nil means lines starting with \":\" are treated as fixed-width.
  975. This currently only means they are never auto-wrapped.
  976. When nil, such lines will be treated like ordinary lines.
  977. See also the QUOTE keyword."
  978. :group 'org-edit-structure
  979. :type 'boolean)
  980. (defcustom org-goto-auto-isearch t
  981. "Non-nil means typing characters in `org-goto' starts incremental search."
  982. :group 'org-edit-structure
  983. :type 'boolean)
  984. (defgroup org-sparse-trees nil
  985. "Options concerning sparse trees in Org-mode."
  986. :tag "Org Sparse Trees"
  987. :group 'org-structure)
  988. (defcustom org-highlight-sparse-tree-matches t
  989. "Non-nil means highlight all matches that define a sparse tree.
  990. The highlights will automatically disappear the next time the buffer is
  991. changed by an edit command."
  992. :group 'org-sparse-trees
  993. :type 'boolean)
  994. (defcustom org-remove-highlights-with-change t
  995. "Non-nil means any change to the buffer will remove temporary highlights.
  996. Such highlights are created by `org-occur' and `org-clock-display'.
  997. When nil, `C-c C-c needs to be used to get rid of the highlights.
  998. The highlights created by `org-preview-latex-fragment' always need
  999. `C-c C-c' to be removed."
  1000. :group 'org-sparse-trees
  1001. :group 'org-time
  1002. :type 'boolean)
  1003. (defcustom org-occur-hook '(org-first-headline-recenter)
  1004. "Hook that is run after `org-occur' has constructed a sparse tree.
  1005. This can be used to recenter the window to show as much of the structure
  1006. as possible."
  1007. :group 'org-sparse-trees
  1008. :type 'hook)
  1009. (defgroup org-imenu-and-speedbar nil
  1010. "Options concerning imenu and speedbar in Org-mode."
  1011. :tag "Org Imenu and Speedbar"
  1012. :group 'org-structure)
  1013. (defcustom org-imenu-depth 2
  1014. "The maximum level for Imenu access to Org-mode headlines.
  1015. This also applied for speedbar access."
  1016. :group 'org-imenu-and-speedbar
  1017. :type 'integer)
  1018. (defgroup org-table nil
  1019. "Options concerning tables in Org-mode."
  1020. :tag "Org Table"
  1021. :group 'org)
  1022. (defcustom org-enable-table-editor 'optimized
  1023. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1024. When nil, such lines will be treated like ordinary lines.
  1025. When equal to the symbol `optimized', the table editor will be optimized to
  1026. do the following:
  1027. - Automatic overwrite mode in front of whitespace in table fields.
  1028. This makes the structure of the table stay in tact as long as the edited
  1029. field does not exceed the column width.
  1030. - Minimize the number of realigns. Normally, the table is aligned each time
  1031. TAB or RET are pressed to move to another field. With optimization this
  1032. happens only if changes to a field might have changed the column width.
  1033. Optimization requires replacing the functions `self-insert-command',
  1034. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1035. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1036. very good at guessing when a re-align will be necessary, but you can always
  1037. force one with \\[org-ctrl-c-ctrl-c].
  1038. If you would like to use the optimized version in Org-mode, but the
  1039. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1040. This variable can be used to turn on and off the table editor during a session,
  1041. but in order to toggle optimization, a restart is required.
  1042. See also the variable `org-table-auto-blank-field'."
  1043. :group 'org-table
  1044. :type '(choice
  1045. (const :tag "off" nil)
  1046. (const :tag "on" t)
  1047. (const :tag "on, optimized" optimized)))
  1048. (defcustom org-self-insert-cluster-for-undo t
  1049. "Non-nil means cluster self-insert commands for undo when possible.
  1050. If this is set, then, like in the Emacs command loop, 20 consecutive
  1051. characters will be undone together.
  1052. This is configurable, because there is some impact on typing performance."
  1053. :group 'org-table
  1054. :type 'boolean)
  1055. (defcustom org-table-tab-recognizes-table.el t
  1056. "Non-nil means TAB will automatically notice a table.el table.
  1057. When it sees such a table, it moves point into it and - if necessary -
  1058. calls `table-recognize-table'."
  1059. :group 'org-table-editing
  1060. :type 'boolean)
  1061. (defgroup org-link nil
  1062. "Options concerning links in Org-mode."
  1063. :tag "Org Link"
  1064. :group 'org)
  1065. (defvar org-link-abbrev-alist-local nil
  1066. "Buffer-local version of `org-link-abbrev-alist', which see.
  1067. The value of this is taken from the #+LINK lines.")
  1068. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1069. (defcustom org-link-abbrev-alist nil
  1070. "Alist of link abbreviations.
  1071. The car of each element is a string, to be replaced at the start of a link.
  1072. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1073. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1074. [[linkkey:tag][description]]
  1075. The 'linkkey' must be a word word, starting with a letter, followed
  1076. by letters, numbers, '-' or '_'.
  1077. If REPLACE is a string, the tag will simply be appended to create the link.
  1078. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1079. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1080. be inserted at that point (see the function `url-hexify-string').
  1081. REPLACE may also be a function that will be called with the tag as the
  1082. only argument to create the link, which should be returned as a string.
  1083. See the manual for examples."
  1084. :group 'org-link
  1085. :type '(repeat
  1086. (cons
  1087. (string :tag "Protocol")
  1088. (choice
  1089. (string :tag "Format")
  1090. (function)))))
  1091. (defcustom org-descriptive-links t
  1092. "Non-nil means hide link part and only show description of bracket links.
  1093. Bracket links are like [[link][description]]. This variable sets the initial
  1094. state in new org-mode buffers. The setting can then be toggled on a
  1095. per-buffer basis from the Org->Hyperlinks menu."
  1096. :group 'org-link
  1097. :type 'boolean)
  1098. (defcustom org-link-file-path-type 'adaptive
  1099. "How the path name in file links should be stored.
  1100. Valid values are:
  1101. relative Relative to the current directory, i.e. the directory of the file
  1102. into which the link is being inserted.
  1103. absolute Absolute path, if possible with ~ for home directory.
  1104. noabbrev Absolute path, no abbreviation of home directory.
  1105. adaptive Use relative path for files in the current directory and sub-
  1106. directories of it. For other files, use an absolute path."
  1107. :group 'org-link
  1108. :type '(choice
  1109. (const relative)
  1110. (const absolute)
  1111. (const noabbrev)
  1112. (const adaptive)))
  1113. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1114. "Types of links that should be activated in Org-mode files.
  1115. This is a list of symbols, each leading to the activation of a certain link
  1116. type. In principle, it does not hurt to turn on most link types - there may
  1117. be a small gain when turning off unused link types. The types are:
  1118. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1119. angle Links in angular brackets that may contain whitespace like
  1120. <bbdb:Carsten Dominik>.
  1121. plain Plain links in normal text, no whitespace, like http://google.com.
  1122. radio Text that is matched by a radio target, see manual for details.
  1123. tag Tag settings in a headline (link to tag search).
  1124. date Time stamps (link to calendar).
  1125. footnote Footnote labels.
  1126. Changing this variable requires a restart of Emacs to become effective."
  1127. :group 'org-link
  1128. :type '(set :greedy t
  1129. (const :tag "Double bracket links" bracket)
  1130. (const :tag "Angular bracket links" angle)
  1131. (const :tag "Plain text links" plain)
  1132. (const :tag "Radio target matches" radio)
  1133. (const :tag "Tags" tag)
  1134. (const :tag "Timestamps" date)
  1135. (const :tag "Footnotes" footnote)))
  1136. (defcustom org-make-link-description-function nil
  1137. "Function to use to generate link descriptions from links.
  1138. If nil the link location will be used. This function must take
  1139. two parameters; the first is the link and the second the
  1140. description `org-insert-link' has generated, and should return the
  1141. description to use."
  1142. :group 'org-link
  1143. :type 'function)
  1144. (defgroup org-link-store nil
  1145. "Options concerning storing links in Org-mode."
  1146. :tag "Org Store Link"
  1147. :group 'org-link)
  1148. (defcustom org-email-link-description-format "Email %c: %.30s"
  1149. "Format of the description part of a link to an email or usenet message.
  1150. The following %-escapes will be replaced by corresponding information:
  1151. %F full \"From\" field
  1152. %f name, taken from \"From\" field, address if no name
  1153. %T full \"To\" field
  1154. %t first name in \"To\" field, address if no name
  1155. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1156. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1157. %s subject
  1158. %m message-id.
  1159. You may use normal field width specification between the % and the letter.
  1160. This is for example useful to limit the length of the subject.
  1161. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1162. :group 'org-link-store
  1163. :type 'string)
  1164. (defcustom org-from-is-user-regexp
  1165. (let (r1 r2)
  1166. (when (and user-mail-address (not (string= user-mail-address "")))
  1167. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1168. (when (and user-full-name (not (string= user-full-name "")))
  1169. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1170. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1171. "Regexp matched against the \"From:\" header of an email or usenet message.
  1172. It should match if the message is from the user him/herself."
  1173. :group 'org-link-store
  1174. :type 'regexp)
  1175. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1176. "Non-nil means storing a link to an Org file will use entry IDs.
  1177. Note that before this variable is even considered, org-id must be loaded,
  1178. so please customize `org-modules' and turn it on.
  1179. The variable can have the following values:
  1180. t Create an ID if needed to make a link to the current entry.
  1181. create-if-interactive
  1182. If `org-store-link' is called directly (interactively, as a user
  1183. command), do create an ID to support the link. But when doing the
  1184. job for remember, only use the ID if it already exists. The
  1185. purpose of this setting is to avoid proliferation of unwanted
  1186. IDs, just because you happen to be in an Org file when you
  1187. call `org-remember' that automatically and preemptively
  1188. creates a link. If you do want to get an ID link in a remember
  1189. template to an entry not having an ID, create it first by
  1190. explicitly creating a link to it, using `C-c C-l' first.
  1191. create-if-interactive-and-no-custom-id
  1192. Like create-if-interactive, but do not create an ID if there is
  1193. a CUSTOM_ID property defined in the entry. This is the default.
  1194. use-existing
  1195. Use existing ID, do not create one.
  1196. nil Never use an ID to make a link, instead link using a text search for
  1197. the headline text."
  1198. :group 'org-link-store
  1199. :type '(choice
  1200. (const :tag "Create ID to make link" t)
  1201. (const :tag "Create if storing link interactively"
  1202. create-if-interactive)
  1203. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1204. create-if-interactive-and-no-custom-id)
  1205. (const :tag "Only use existing" use-existing)
  1206. (const :tag "Do not use ID to create link" nil)))
  1207. (defcustom org-context-in-file-links t
  1208. "Non-nil means file links from `org-store-link' contain context.
  1209. A search string will be added to the file name with :: as separator and
  1210. used to find the context when the link is activated by the command
  1211. `org-open-at-point'.
  1212. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1213. negates this setting for the duration of the command."
  1214. :group 'org-link-store
  1215. :type 'boolean)
  1216. (defcustom org-keep-stored-link-after-insertion nil
  1217. "Non-nil means keep link in list for entire session.
  1218. The command `org-store-link' adds a link pointing to the current
  1219. location to an internal list. These links accumulate during a session.
  1220. The command `org-insert-link' can be used to insert links into any
  1221. Org-mode file (offering completion for all stored links). When this
  1222. option is nil, every link which has been inserted once using \\[org-insert-link]
  1223. will be removed from the list, to make completing the unused links
  1224. more efficient."
  1225. :group 'org-link-store
  1226. :type 'boolean)
  1227. (defgroup org-link-follow nil
  1228. "Options concerning following links in Org-mode."
  1229. :tag "Org Follow Link"
  1230. :group 'org-link)
  1231. (defcustom org-link-translation-function nil
  1232. "Function to translate links with different syntax to Org syntax.
  1233. This can be used to translate links created for example by the Planner
  1234. or emacs-wiki packages to Org syntax.
  1235. The function must accept two parameters, a TYPE containing the link
  1236. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1237. which is everything after the link protocol. It should return a cons
  1238. with possibly modified values of type and path.
  1239. Org contains a function for this, so if you set this variable to
  1240. `org-translate-link-from-planner', you should be able follow many
  1241. links created by planner."
  1242. :group 'org-link-follow
  1243. :type 'function)
  1244. (defcustom org-follow-link-hook nil
  1245. "Hook that is run after a link has been followed."
  1246. :group 'org-link-follow
  1247. :type 'hook)
  1248. (defcustom org-tab-follows-link nil
  1249. "Non-nil means on links TAB will follow the link.
  1250. Needs to be set before org.el is loaded.
  1251. This really should not be used, it does not make sense, and the
  1252. implementation is bad."
  1253. :group 'org-link-follow
  1254. :type 'boolean)
  1255. (defcustom org-return-follows-link nil
  1256. "Non-nil means on links RET will follow the link."
  1257. :group 'org-link-follow
  1258. :type 'boolean)
  1259. (defcustom org-mouse-1-follows-link
  1260. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1261. "Non-nil means mouse-1 on a link will follow the link.
  1262. A longer mouse click will still set point. Does not work on XEmacs.
  1263. Needs to be set before org.el is loaded."
  1264. :group 'org-link-follow
  1265. :type 'boolean)
  1266. (defcustom org-mark-ring-length 4
  1267. "Number of different positions to be recorded in the ring.
  1268. Changing this requires a restart of Emacs to work correctly."
  1269. :group 'org-link-follow
  1270. :type 'integer)
  1271. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1272. "Non-nil means internal links in Org files must exactly match a headline.
  1273. When nil, the link search tries to match a phrase will all words
  1274. in the search text."
  1275. :group 'org-link-follow
  1276. :type '(choice
  1277. (const :tag "Use fuzy text search" nil)
  1278. (const :tag "Match only exact headline" t)
  1279. (const :tag "Match extact headline or query to create it"
  1280. query-to-create)))
  1281. (defcustom org-link-frame-setup
  1282. '((vm . vm-visit-folder-other-frame)
  1283. (gnus . org-gnus-no-new-news)
  1284. (file . find-file-other-window)
  1285. (wl . wl-other-frame))
  1286. "Setup the frame configuration for following links.
  1287. When following a link with Emacs, it may often be useful to display
  1288. this link in another window or frame. This variable can be used to
  1289. set this up for the different types of links.
  1290. For VM, use any of
  1291. `vm-visit-folder'
  1292. `vm-visit-folder-other-frame'
  1293. For Gnus, use any of
  1294. `gnus'
  1295. `gnus-other-frame'
  1296. `org-gnus-no-new-news'
  1297. For FILE, use any of
  1298. `find-file'
  1299. `find-file-other-window'
  1300. `find-file-other-frame'
  1301. For Wanderlust use any of
  1302. `wl'
  1303. `wl-other-frame'
  1304. For the calendar, use the variable `calendar-setup'.
  1305. For BBDB, it is currently only possible to display the matches in
  1306. another window."
  1307. :group 'org-link-follow
  1308. :type '(list
  1309. (cons (const vm)
  1310. (choice
  1311. (const vm-visit-folder)
  1312. (const vm-visit-folder-other-window)
  1313. (const vm-visit-folder-other-frame)))
  1314. (cons (const gnus)
  1315. (choice
  1316. (const gnus)
  1317. (const gnus-other-frame)
  1318. (const org-gnus-no-new-news)))
  1319. (cons (const file)
  1320. (choice
  1321. (const find-file)
  1322. (const find-file-other-window)
  1323. (const find-file-other-frame)))
  1324. (cons (const wl)
  1325. (choice
  1326. (const wl)
  1327. (const wl-other-frame)))))
  1328. (defcustom org-display-internal-link-with-indirect-buffer nil
  1329. "Non-nil means use indirect buffer to display infile links.
  1330. Activating internal links (from one location in a file to another location
  1331. in the same file) normally just jumps to the location. When the link is
  1332. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1333. is displayed in
  1334. another window. When this option is set, the other window actually displays
  1335. an indirect buffer clone of the current buffer, to avoid any visibility
  1336. changes to the current buffer."
  1337. :group 'org-link-follow
  1338. :type 'boolean)
  1339. (defcustom org-open-non-existing-files nil
  1340. "Non-nil means `org-open-file' will open non-existing files.
  1341. When nil, an error will be generated.
  1342. This variable applies only to external applications because they
  1343. might choke on non-existing files. If the link is to a file that
  1344. will be opened in Emacs, the variable is ignored."
  1345. :group 'org-link-follow
  1346. :type 'boolean)
  1347. (defcustom org-open-directory-means-index-dot-org nil
  1348. "Non-nil means a link to a directory really means to index.org.
  1349. When nil, following a directory link will run dired or open a finder/explorer
  1350. window on that directory."
  1351. :group 'org-link-follow
  1352. :type 'boolean)
  1353. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1354. "Function and arguments to call for following mailto links.
  1355. This is a list with the first element being a Lisp function, and the
  1356. remaining elements being arguments to the function. In string arguments,
  1357. %a will be replaced by the address, and %s will be replaced by the subject
  1358. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1359. :group 'org-link-follow
  1360. :type '(choice
  1361. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1362. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1363. (const :tag "message-mail" (message-mail "%a" "%s"))
  1364. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1365. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1366. "Non-nil means ask for confirmation before executing shell links.
  1367. Shell links can be dangerous: just think about a link
  1368. [[shell:rm -rf ~/*][Google Search]]
  1369. This link would show up in your Org-mode document as \"Google Search\",
  1370. but really it would remove your entire home directory.
  1371. Therefore we advise against setting this variable to nil.
  1372. Just change it to `y-or-n-p' if you want to confirm with a
  1373. single keystroke rather than having to type \"yes\"."
  1374. :group 'org-link-follow
  1375. :type '(choice
  1376. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1377. (const :tag "with y-or-n (faster)" y-or-n-p)
  1378. (const :tag "no confirmation (dangerous)" nil)))
  1379. (put 'org-confirm-shell-link-function
  1380. 'safe-local-variable
  1381. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1382. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1383. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1384. Elisp links can be dangerous: just think about a link
  1385. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1386. This link would show up in your Org-mode document as \"Google Search\",
  1387. but really it would remove your entire home directory.
  1388. Therefore we advise against setting this variable to nil.
  1389. Just change it to `y-or-n-p' if you want to confirm with a
  1390. single keystroke rather than having to type \"yes\"."
  1391. :group 'org-link-follow
  1392. :type '(choice
  1393. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1394. (const :tag "with y-or-n (faster)" y-or-n-p)
  1395. (const :tag "no confirmation (dangerous)" nil)))
  1396. (put 'org-confirm-shell-link-function
  1397. 'safe-local-variable
  1398. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1399. (defconst org-file-apps-defaults-gnu
  1400. '((remote . emacs)
  1401. (system . mailcap)
  1402. (t . mailcap))
  1403. "Default file applications on a UNIX or GNU/Linux system.
  1404. See `org-file-apps'.")
  1405. (defconst org-file-apps-defaults-macosx
  1406. '((remote . emacs)
  1407. (t . "open %s")
  1408. (system . "open %s")
  1409. ("ps.gz" . "gv %s")
  1410. ("eps.gz" . "gv %s")
  1411. ("dvi" . "xdvi %s")
  1412. ("fig" . "xfig %s"))
  1413. "Default file applications on a MacOS X system.
  1414. The system \"open\" is known as a default, but we use X11 applications
  1415. for some files for which the OS does not have a good default.
  1416. See `org-file-apps'.")
  1417. (defconst org-file-apps-defaults-windowsnt
  1418. (list
  1419. '(remote . emacs)
  1420. (cons t
  1421. (list (if (featurep 'xemacs)
  1422. 'mswindows-shell-execute
  1423. 'w32-shell-execute)
  1424. "open" 'file))
  1425. (cons 'system
  1426. (list (if (featurep 'xemacs)
  1427. 'mswindows-shell-execute
  1428. 'w32-shell-execute)
  1429. "open" 'file)))
  1430. "Default file applications on a Windows NT system.
  1431. The system \"open\" is used for most files.
  1432. See `org-file-apps'.")
  1433. (defcustom org-file-apps
  1434. '(
  1435. (auto-mode . emacs)
  1436. ("\\.mm\\'" . default)
  1437. ("\\.x?html?\\'" . default)
  1438. ("\\.pdf\\'" . default)
  1439. )
  1440. "External applications for opening `file:path' items in a document.
  1441. Org-mode uses system defaults for different file types, but
  1442. you can use this variable to set the application for a given file
  1443. extension. The entries in this list are cons cells where the car identifies
  1444. files and the cdr the corresponding command. Possible values for the
  1445. file identifier are
  1446. \"string\" A string as a file identifier can be interpreted in different
  1447. ways, depending on its contents:
  1448. - Alphanumeric characters only:
  1449. Match links with this file extension.
  1450. Example: (\"pdf\" . \"evince %s\")
  1451. to open PDFs with evince.
  1452. - Regular expression: Match links where the
  1453. filename matches the regexp. If you want to
  1454. use groups here, use shy groups.
  1455. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1456. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1457. to open *.html and *.xhtml with firefox.
  1458. - Regular expression which contains (non-shy) groups:
  1459. Match links where the whole link, including \"::\", and
  1460. anything after that, matches the regexp.
  1461. In a custom command string, %1, %2, etc. are replaced with
  1462. the parts of the link that were matched by the groups.
  1463. For backwards compatibility, if a command string is given
  1464. that does not use any of the group matches, this case is
  1465. handled identically to the second one (i.e. match against
  1466. file name only).
  1467. In a custom lisp form, you can access the group matches with
  1468. (match-string n link).
  1469. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1470. to open [[file:document.pdf::5]] with evince at page 5.
  1471. `directory' Matches a directory
  1472. `remote' Matches a remote file, accessible through tramp or efs.
  1473. Remote files most likely should be visited through Emacs
  1474. because external applications cannot handle such paths.
  1475. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1476. so all files Emacs knows how to handle. Using this with
  1477. command `emacs' will open most files in Emacs. Beware that this
  1478. will also open html files inside Emacs, unless you add
  1479. (\"html\" . default) to the list as well.
  1480. t Default for files not matched by any of the other options.
  1481. `system' The system command to open files, like `open' on Windows
  1482. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1483. that will be selected if you call `C-c C-o' with a double
  1484. \\[universal-argument] \\[universal-argument] prefix.
  1485. Possible values for the command are:
  1486. `emacs' The file will be visited by the current Emacs process.
  1487. `default' Use the default application for this file type, which is the
  1488. association for t in the list, most likely in the system-specific
  1489. part.
  1490. This can be used to overrule an unwanted setting in the
  1491. system-specific variable.
  1492. `system' Use the system command for opening files, like \"open\".
  1493. This command is specified by the entry whose car is `system'.
  1494. Most likely, the system-specific version of this variable
  1495. does define this command, but you can overrule/replace it
  1496. here.
  1497. string A command to be executed by a shell; %s will be replaced
  1498. by the path to the file.
  1499. sexp A Lisp form which will be evaluated. The file path will
  1500. be available in the Lisp variable `file'.
  1501. For more examples, see the system specific constants
  1502. `org-file-apps-defaults-macosx'
  1503. `org-file-apps-defaults-windowsnt'
  1504. `org-file-apps-defaults-gnu'."
  1505. :group 'org-link-follow
  1506. :type '(repeat
  1507. (cons (choice :value ""
  1508. (string :tag "Extension")
  1509. (const :tag "System command to open files" system)
  1510. (const :tag "Default for unrecognized files" t)
  1511. (const :tag "Remote file" remote)
  1512. (const :tag "Links to a directory" directory)
  1513. (const :tag "Any files that have Emacs modes"
  1514. auto-mode))
  1515. (choice :value ""
  1516. (const :tag "Visit with Emacs" emacs)
  1517. (const :tag "Use default" default)
  1518. (const :tag "Use the system command" system)
  1519. (string :tag "Command")
  1520. (sexp :tag "Lisp form")))))
  1521. (defgroup org-refile nil
  1522. "Options concerning refiling entries in Org-mode."
  1523. :tag "Org Refile"
  1524. :group 'org)
  1525. (defcustom org-directory "~/org"
  1526. "Directory with org files.
  1527. This is just a default location to look for Org files. There is no need
  1528. at all to put your files into this directory. It is only used in the
  1529. following situations:
  1530. 1. When a remember template specifies a target file that is not an
  1531. absolute path. The path will then be interpreted relative to
  1532. `org-directory'
  1533. 2. When a remember note is filed away in an interactive way (when exiting the
  1534. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1535. with `org-directory' as the default path."
  1536. :group 'org-refile
  1537. :group 'org-remember
  1538. :type 'directory)
  1539. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1540. "Default target for storing notes.
  1541. Used as a fall back file for org-remember.el and org-capture.el, for
  1542. templates that do not specify a target file."
  1543. :group 'org-refile
  1544. :group 'org-remember
  1545. :type '(choice
  1546. (const :tag "Default from remember-data-file" nil)
  1547. file))
  1548. (defcustom org-goto-interface 'outline
  1549. "The default interface to be used for `org-goto'.
  1550. Allowed values are:
  1551. outline The interface shows an outline of the relevant file
  1552. and the correct heading is found by moving through
  1553. the outline or by searching with incremental search.
  1554. outline-path-completion Headlines in the current buffer are offered via
  1555. completion. This is the interface also used by
  1556. the refile command."
  1557. :group 'org-refile
  1558. :type '(choice
  1559. (const :tag "Outline" outline)
  1560. (const :tag "Outline-path-completion" outline-path-completion)))
  1561. (defcustom org-goto-max-level 5
  1562. "Maximum target level when running `org-goto' with refile interface."
  1563. :group 'org-refile
  1564. :type 'integer)
  1565. (defcustom org-reverse-note-order nil
  1566. "Non-nil means store new notes at the beginning of a file or entry.
  1567. When nil, new notes will be filed to the end of a file or entry.
  1568. This can also be a list with cons cells of regular expressions that
  1569. are matched against file names, and values."
  1570. :group 'org-remember
  1571. :group 'org-refile
  1572. :type '(choice
  1573. (const :tag "Reverse always" t)
  1574. (const :tag "Reverse never" nil)
  1575. (repeat :tag "By file name regexp"
  1576. (cons regexp boolean))))
  1577. (defcustom org-log-refile nil
  1578. "Information to record when a task is refiled.
  1579. Possible values are:
  1580. nil Don't add anything
  1581. time Add a time stamp to the task
  1582. note Prompt for a note and add it with template `org-log-note-headings'
  1583. This option can also be set with on a per-file-basis with
  1584. #+STARTUP: nologrefile
  1585. #+STARTUP: logrefile
  1586. #+STARTUP: lognoterefile
  1587. You can have local logging settings for a subtree by setting the LOGGING
  1588. property to one or more of these keywords.
  1589. When bulk-refiling from the agenda, the value `note' is forbidden and
  1590. will temporarily be changed to `time'."
  1591. :group 'org-refile
  1592. :group 'org-progress
  1593. :type '(choice
  1594. (const :tag "No logging" nil)
  1595. (const :tag "Record timestamp" time)
  1596. (const :tag "Record timestamp with note." note)))
  1597. (defcustom org-refile-targets nil
  1598. "Targets for refiling entries with \\[org-refile].
  1599. This is list of cons cells. Each cell contains:
  1600. - a specification of the files to be considered, either a list of files,
  1601. or a symbol whose function or variable value will be used to retrieve
  1602. a file name or a list of file names. If you use `org-agenda-files' for
  1603. that, all agenda files will be scanned for targets. Nil means consider
  1604. headings in the current buffer.
  1605. - A specification of how to find candidate refile targets. This may be
  1606. any of:
  1607. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1608. This tag has to be present in all target headlines, inheritance will
  1609. not be considered.
  1610. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1611. todo keyword.
  1612. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1613. headlines that are refiling targets.
  1614. - a cons cell (:level . N). Any headline of level N is considered 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. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1618. Note that, when `org-odd-levels-only' is set, level corresponds to
  1619. order in hierarchy, not to the number of stars.
  1620. You can set the variable `org-refile-target-verify-function' to a function
  1621. to verify each headline found by the simple criteria above.
  1622. When this variable is nil, all top-level headlines in the current buffer
  1623. are used, equivalent to the value `((nil . (:level . 1))'."
  1624. :group 'org-refile
  1625. :type '(repeat
  1626. (cons
  1627. (choice :value org-agenda-files
  1628. (const :tag "All agenda files" org-agenda-files)
  1629. (const :tag "Current buffer" nil)
  1630. (function) (variable) (file))
  1631. (choice :tag "Identify target headline by"
  1632. (cons :tag "Specific tag" (const :value :tag) (string))
  1633. (cons :tag "TODO keyword" (const :value :todo) (string))
  1634. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1635. (cons :tag "Level number" (const :value :level) (integer))
  1636. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1637. (defcustom org-refile-target-verify-function nil
  1638. "Function to verify if the headline at point should be a refile target.
  1639. The function will be called without arguments, with point at the
  1640. beginning of the headline. It should return t and leave point
  1641. where it is if the headline is a valid target for refiling.
  1642. If the target should not be selected, the function must return nil.
  1643. In addition to this, it may move point to a place from where the search
  1644. should be continued. For example, the function may decide that the entire
  1645. subtree of the current entry should be excluded and move point to the end
  1646. of the subtree."
  1647. :group 'org-refile
  1648. :type 'function)
  1649. (defcustom org-refile-use-cache nil
  1650. "Non-nil means cache refile targets to speed up the process.
  1651. The cache for a particular file will be updated automatically when
  1652. the buffer has been killed, or when any of the marker used for flagging
  1653. refile targets no longer points at a live buffer.
  1654. If you have added new entries to a buffer that might themselves be targets,
  1655. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1656. find that easier, `C-u C-u C-u C-c C-w'."
  1657. :group 'org-refile
  1658. :type 'boolean)
  1659. (defcustom org-refile-use-outline-path nil
  1660. "Non-nil means provide refile targets as paths.
  1661. So a level 3 headline will be available as level1/level2/level3.
  1662. When the value is `file', also include the file name (without directory)
  1663. into the path. In this case, you can also stop the completion after
  1664. the file name, to get entries inserted as top level in the file.
  1665. When `full-file-path', include the full file path."
  1666. :group 'org-refile
  1667. :type '(choice
  1668. (const :tag "Not" nil)
  1669. (const :tag "Yes" t)
  1670. (const :tag "Start with file name" file)
  1671. (const :tag "Start with full file path" full-file-path)))
  1672. (defcustom org-outline-path-complete-in-steps t
  1673. "Non-nil means complete the outline path in hierarchical steps.
  1674. When Org-mode uses the refile interface to select an outline path
  1675. \(see variable `org-refile-use-outline-path'), the completion of
  1676. the path can be done is a single go, or if can be done in steps down
  1677. the headline hierarchy. Going in steps is probably the best if you
  1678. do not use a special completion package like `ido' or `icicles'.
  1679. However, when using these packages, going in one step can be very
  1680. fast, while still showing the whole path to the entry."
  1681. :group 'org-refile
  1682. :type 'boolean)
  1683. (defcustom org-refile-allow-creating-parent-nodes nil
  1684. "Non-nil means allow to create new nodes as refile targets.
  1685. New nodes are then created by adding \"/new node name\" to the completion
  1686. of an existing node. When the value of this variable is `confirm',
  1687. new node creation must be confirmed by the user (recommended)
  1688. When nil, the completion must match an existing entry.
  1689. Note that, if the new heading is not seen by the criteria
  1690. listed in `org-refile-targets', multiple instances of the same
  1691. heading would be created by trying again to file under the new
  1692. heading."
  1693. :group 'org-refile
  1694. :type '(choice
  1695. (const :tag "Never" nil)
  1696. (const :tag "Always" t)
  1697. (const :tag "Prompt for confirmation" confirm)))
  1698. (defgroup org-todo nil
  1699. "Options concerning TODO items in Org-mode."
  1700. :tag "Org TODO"
  1701. :group 'org)
  1702. (defgroup org-progress nil
  1703. "Options concerning Progress logging in Org-mode."
  1704. :tag "Org Progress"
  1705. :group 'org-time)
  1706. (defvar org-todo-interpretation-widgets
  1707. '(
  1708. (:tag "Sequence (cycling hits every state)" sequence)
  1709. (:tag "Type (cycling directly to DONE)" type))
  1710. "The available interpretation symbols for customizing `org-todo-keywords'.
  1711. Interested libraries should add to this list.")
  1712. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1713. "List of TODO entry keyword sequences and their interpretation.
  1714. \\<org-mode-map>This is a list of sequences.
  1715. Each sequence starts with a symbol, either `sequence' or `type',
  1716. indicating if the keywords should be interpreted as a sequence of
  1717. action steps, or as different types of TODO items. The first
  1718. keywords are states requiring action - these states will select a headline
  1719. for inclusion into the global TODO list Org-mode produces. If one of
  1720. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1721. signify that no further action is necessary. If \"|\" is not found,
  1722. the last keyword is treated as the only DONE state of the sequence.
  1723. The command \\[org-todo] cycles an entry through these states, and one
  1724. additional state where no keyword is present. For details about this
  1725. cycling, see the manual.
  1726. TODO keywords and interpretation can also be set on a per-file basis with
  1727. the special #+SEQ_TODO and #+TYP_TODO lines.
  1728. Each keyword can optionally specify a character for fast state selection
  1729. \(in combination with the variable `org-use-fast-todo-selection')
  1730. and specifiers for state change logging, using the same syntax
  1731. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1732. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1733. indicates to record a time stamp each time this state is selected.
  1734. Each keyword may also specify if a timestamp or a note should be
  1735. recorded when entering or leaving the state, by adding additional
  1736. characters in the parenthesis after the keyword. This looks like this:
  1737. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1738. record only the time of the state change. With X and Y being either
  1739. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1740. Y when leaving the state if and only if the *target* state does not
  1741. define X. You may omit any of the fast-selection key or X or /Y,
  1742. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1743. For backward compatibility, this variable may also be just a list
  1744. of keywords - in this case the interpretation (sequence or type) will be
  1745. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1746. :group 'org-todo
  1747. :group 'org-keywords
  1748. :type '(choice
  1749. (repeat :tag "Old syntax, just keywords"
  1750. (string :tag "Keyword"))
  1751. (repeat :tag "New syntax"
  1752. (cons
  1753. (choice
  1754. :tag "Interpretation"
  1755. ;;Quick and dirty way to see
  1756. ;;`org-todo-interpretations'. This takes the
  1757. ;;place of item arguments
  1758. :convert-widget
  1759. (lambda (widget)
  1760. (widget-put widget
  1761. :args (mapcar
  1762. #'(lambda (x)
  1763. (widget-convert
  1764. (cons 'const x)))
  1765. org-todo-interpretation-widgets))
  1766. widget))
  1767. (repeat
  1768. (string :tag "Keyword"))))))
  1769. (defvar org-todo-keywords-1 nil
  1770. "All TODO and DONE keywords active in a buffer.")
  1771. (make-variable-buffer-local 'org-todo-keywords-1)
  1772. (defvar org-todo-keywords-for-agenda nil)
  1773. (defvar org-done-keywords-for-agenda nil)
  1774. (defvar org-drawers-for-agenda nil)
  1775. (defvar org-todo-keyword-alist-for-agenda nil)
  1776. (defvar org-tag-alist-for-agenda nil)
  1777. (defvar org-agenda-contributing-files nil)
  1778. (defvar org-not-done-keywords nil)
  1779. (make-variable-buffer-local 'org-not-done-keywords)
  1780. (defvar org-done-keywords nil)
  1781. (make-variable-buffer-local 'org-done-keywords)
  1782. (defvar org-todo-heads nil)
  1783. (make-variable-buffer-local 'org-todo-heads)
  1784. (defvar org-todo-sets nil)
  1785. (make-variable-buffer-local 'org-todo-sets)
  1786. (defvar org-todo-log-states nil)
  1787. (make-variable-buffer-local 'org-todo-log-states)
  1788. (defvar org-todo-kwd-alist nil)
  1789. (make-variable-buffer-local 'org-todo-kwd-alist)
  1790. (defvar org-todo-key-alist nil)
  1791. (make-variable-buffer-local 'org-todo-key-alist)
  1792. (defvar org-todo-key-trigger nil)
  1793. (make-variable-buffer-local 'org-todo-key-trigger)
  1794. (defcustom org-todo-interpretation 'sequence
  1795. "Controls how TODO keywords are interpreted.
  1796. This variable is in principle obsolete and is only used for
  1797. backward compatibility, if the interpretation of todo keywords is
  1798. not given already in `org-todo-keywords'. See that variable for
  1799. more information."
  1800. :group 'org-todo
  1801. :group 'org-keywords
  1802. :type '(choice (const sequence)
  1803. (const type)))
  1804. (defcustom org-use-fast-todo-selection t
  1805. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1806. This variable describes if and under what circumstances the cycling
  1807. mechanism for TODO keywords will be replaced by a single-key, direct
  1808. selection scheme.
  1809. When nil, fast selection is never used.
  1810. When the symbol `prefix', it will be used when `org-todo' is called with
  1811. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1812. in an agenda buffer.
  1813. When t, fast selection is used by default. In this case, the prefix
  1814. argument forces cycling instead.
  1815. In all cases, the special interface is only used if access keys have actually
  1816. been assigned by the user, i.e. if keywords in the configuration are followed
  1817. by a letter in parenthesis, like TODO(t)."
  1818. :group 'org-todo
  1819. :type '(choice
  1820. (const :tag "Never" nil)
  1821. (const :tag "By default" t)
  1822. (const :tag "Only with C-u C-c C-t" prefix)))
  1823. (defcustom org-provide-todo-statistics t
  1824. "Non-nil means update todo statistics after insert and toggle.
  1825. ALL-HEADLINES means update todo statistics by including headlines
  1826. with no TODO keyword as well, counting them as not done.
  1827. A list of TODO keywords means the same, but skip keywords that are
  1828. not in this list.
  1829. When this is set, todo statistics is updated in the parent of the
  1830. current entry each time a todo state is changed."
  1831. :group 'org-todo
  1832. :type '(choice
  1833. (const :tag "Yes, only for TODO entries" t)
  1834. (const :tag "Yes, including all entries" 'all-headlines)
  1835. (repeat :tag "Yes, for TODOs in this list"
  1836. (string :tag "TODO keyword"))
  1837. (other :tag "No TODO statistics" nil)))
  1838. (defcustom org-hierarchical-todo-statistics t
  1839. "Non-nil means TODO statistics covers just direct children.
  1840. When nil, all entries in the subtree are considered.
  1841. This has only an effect if `org-provide-todo-statistics' is set.
  1842. To set this to nil for only a single subtree, use a COOKIE_DATA
  1843. property and include the word \"recursive\" into the value."
  1844. :group 'org-todo
  1845. :type 'boolean)
  1846. (defcustom org-after-todo-state-change-hook nil
  1847. "Hook which is run after the state of a TODO item was changed.
  1848. The new state (a string with a TODO keyword, or nil) is available in the
  1849. Lisp variable `state'."
  1850. :group 'org-todo
  1851. :type 'hook)
  1852. (defvar org-blocker-hook nil
  1853. "Hook for functions that are allowed to block a state change.
  1854. Each function gets as its single argument a property list, see
  1855. `org-trigger-hook' for more information about this list.
  1856. If any of the functions in this hook returns nil, the state change
  1857. is blocked.")
  1858. (defvar org-trigger-hook nil
  1859. "Hook for functions that are triggered by a state change.
  1860. Each function gets as its single argument a property list with at least
  1861. the following elements:
  1862. (:type type-of-change :position pos-at-entry-start
  1863. :from old-state :to new-state)
  1864. Depending on the type, more properties may be present.
  1865. This mechanism is currently implemented for:
  1866. TODO state changes
  1867. ------------------
  1868. :type todo-state-change
  1869. :from previous state (keyword as a string), or nil, or a symbol
  1870. 'todo' or 'done', to indicate the general type of state.
  1871. :to new state, like in :from")
  1872. (defcustom org-enforce-todo-dependencies nil
  1873. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1874. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1875. be blocked if any prior sibling is not yet done.
  1876. Finally, if the parent is blocked because of ordered siblings of its own,
  1877. the child will also be blocked.
  1878. This variable needs to be set before org.el is loaded, and you need to
  1879. restart Emacs after a change to make the change effective. The only way
  1880. to change is while Emacs is running is through the customize interface."
  1881. :set (lambda (var val)
  1882. (set var val)
  1883. (if val
  1884. (add-hook 'org-blocker-hook
  1885. 'org-block-todo-from-children-or-siblings-or-parent)
  1886. (remove-hook 'org-blocker-hook
  1887. 'org-block-todo-from-children-or-siblings-or-parent)))
  1888. :group 'org-todo
  1889. :type 'boolean)
  1890. (defcustom org-enforce-todo-checkbox-dependencies nil
  1891. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1892. When this is nil, checkboxes have no influence on switching TODO states.
  1893. When non-nil, you first need to check off all check boxes before the TODO
  1894. entry can be switched to DONE.
  1895. This variable needs to be set before org.el is loaded, and you need to
  1896. restart Emacs after a change to make the change effective. The only way
  1897. to change is while Emacs is running is through the customize interface."
  1898. :set (lambda (var val)
  1899. (set var val)
  1900. (if val
  1901. (add-hook 'org-blocker-hook
  1902. 'org-block-todo-from-checkboxes)
  1903. (remove-hook 'org-blocker-hook
  1904. 'org-block-todo-from-checkboxes)))
  1905. :group 'org-todo
  1906. :type 'boolean)
  1907. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1908. "Non-nil means inserting a TODO heading is treated as state change.
  1909. So when the command \\[org-insert-todo-heading] is used, state change
  1910. logging will apply if appropriate. When nil, the new TODO item will
  1911. be inserted directly, and no logging will take place."
  1912. :group 'org-todo
  1913. :type 'boolean)
  1914. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1915. "Non-nil means switching TODO states with S-cursor counts as state change.
  1916. This is the default behavior. However, setting this to nil allows a
  1917. convenient way to select a TODO state and bypass any logging associated
  1918. with that."
  1919. :group 'org-todo
  1920. :type 'boolean)
  1921. (defcustom org-todo-state-tags-triggers nil
  1922. "Tag changes that should be triggered by TODO state changes.
  1923. This is a list. Each entry is
  1924. (state-change (tag . flag) .......)
  1925. State-change can be a string with a state, and empty string to indicate the
  1926. state that has no TODO keyword, or it can be one of the symbols `todo'
  1927. or `done', meaning any not-done or done state, respectively."
  1928. :group 'org-todo
  1929. :group 'org-tags
  1930. :type '(repeat
  1931. (cons (choice :tag "When changing to"
  1932. (const :tag "Not-done state" todo)
  1933. (const :tag "Done state" done)
  1934. (string :tag "State"))
  1935. (repeat
  1936. (cons :tag "Tag action"
  1937. (string :tag "Tag")
  1938. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1939. (defcustom org-log-done nil
  1940. "Information to record when a task moves to the DONE state.
  1941. Possible values are:
  1942. nil Don't add anything, just change the keyword
  1943. time Add a time stamp to the task
  1944. note Prompt for a note and add it with template `org-log-note-headings'
  1945. This option can also be set with on a per-file-basis with
  1946. #+STARTUP: nologdone
  1947. #+STARTUP: logdone
  1948. #+STARTUP: lognotedone
  1949. You can have local logging settings for a subtree by setting the LOGGING
  1950. property to one or more of these keywords."
  1951. :group 'org-todo
  1952. :group 'org-progress
  1953. :type '(choice
  1954. (const :tag "No logging" nil)
  1955. (const :tag "Record CLOSED timestamp" time)
  1956. (const :tag "Record CLOSED timestamp with note." note)))
  1957. ;; Normalize old uses of org-log-done.
  1958. (cond
  1959. ((eq org-log-done t) (setq org-log-done 'time))
  1960. ((and (listp org-log-done) (memq 'done org-log-done))
  1961. (setq org-log-done 'note)))
  1962. (defcustom org-log-reschedule nil
  1963. "Information to record when the scheduling date of a tasks is modified.
  1964. Possible values are:
  1965. nil Don't add anything, just change the date
  1966. time Add a time stamp to the task
  1967. note Prompt for a note and add it with template `org-log-note-headings'
  1968. This option can also be set with on a per-file-basis with
  1969. #+STARTUP: nologreschedule
  1970. #+STARTUP: logreschedule
  1971. #+STARTUP: lognotereschedule"
  1972. :group 'org-todo
  1973. :group 'org-progress
  1974. :type '(choice
  1975. (const :tag "No logging" nil)
  1976. (const :tag "Record timestamp" time)
  1977. (const :tag "Record timestamp with note." note)))
  1978. (defcustom org-log-redeadline nil
  1979. "Information to record when the deadline date of a tasks is modified.
  1980. Possible values are:
  1981. nil Don't add anything, just change the date
  1982. time Add a time stamp to the task
  1983. note Prompt for a note and add it with template `org-log-note-headings'
  1984. This option can also be set with on a per-file-basis with
  1985. #+STARTUP: nologredeadline
  1986. #+STARTUP: logredeadline
  1987. #+STARTUP: lognoteredeadline
  1988. You can have local logging settings for a subtree by setting the LOGGING
  1989. property to one or more of these keywords."
  1990. :group 'org-todo
  1991. :group 'org-progress
  1992. :type '(choice
  1993. (const :tag "No logging" nil)
  1994. (const :tag "Record timestamp" time)
  1995. (const :tag "Record timestamp with note." note)))
  1996. (defcustom org-log-note-clock-out nil
  1997. "Non-nil means record a note when clocking out of an item.
  1998. This can also be configured on a per-file basis by adding one of
  1999. the following lines anywhere in the buffer:
  2000. #+STARTUP: lognoteclock-out
  2001. #+STARTUP: nolognoteclock-out"
  2002. :group 'org-todo
  2003. :group 'org-progress
  2004. :type 'boolean)
  2005. (defcustom org-log-done-with-time t
  2006. "Non-nil means the CLOSED time stamp will contain date and time.
  2007. When nil, only the date will be recorded."
  2008. :group 'org-progress
  2009. :type 'boolean)
  2010. (defcustom org-log-note-headings
  2011. '((done . "CLOSING NOTE %t")
  2012. (state . "State %-12s from %-12S %t")
  2013. (note . "Note taken on %t")
  2014. (reschedule . "Rescheduled from %S on %t")
  2015. (delschedule . "Not scheduled, was %S on %t")
  2016. (redeadline . "New deadline from %S on %t")
  2017. (deldeadline . "Removed deadline, was %S on %t")
  2018. (refile . "Refiled on %t")
  2019. (clock-out . ""))
  2020. "Headings for notes added to entries.
  2021. The value is an alist, with the car being a symbol indicating the note
  2022. context, and the cdr is the heading to be used. The heading may also be the
  2023. empty string.
  2024. %t in the heading will be replaced by a time stamp.
  2025. %T will be an active time stamp instead the default inactive one
  2026. %s will be replaced by the new TODO state, in double quotes.
  2027. %S will be replaced by the old TODO state, in double quotes.
  2028. %u will be replaced by the user name.
  2029. %U will be replaced by the full user name.
  2030. In fact, it is not a good idea to change the `state' entry, because
  2031. agenda log mode depends on the format of these entries."
  2032. :group 'org-todo
  2033. :group 'org-progress
  2034. :type '(list :greedy t
  2035. (cons (const :tag "Heading when closing an item" done) string)
  2036. (cons (const :tag
  2037. "Heading when changing todo state (todo sequence only)"
  2038. state) string)
  2039. (cons (const :tag "Heading when just taking a note" note) string)
  2040. (cons (const :tag "Heading when clocking out" clock-out) string)
  2041. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2042. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2043. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2044. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2045. (cons (const :tag "Heading when refiling" refile) string)))
  2046. (unless (assq 'note org-log-note-headings)
  2047. (push '(note . "%t") org-log-note-headings))
  2048. (defcustom org-log-into-drawer nil
  2049. "Non-nil means insert state change notes and time stamps into a drawer.
  2050. When nil, state changes notes will be inserted after the headline and
  2051. any scheduling and clock lines, but not inside a drawer.
  2052. The value of this variable should be the name of the drawer to use.
  2053. LOGBOOK is proposed at the default drawer for this purpose, you can
  2054. also set this to a string to define the drawer of your choice.
  2055. A value of t is also allowed, representing \"LOGBOOK\".
  2056. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2057. will be ignored.
  2058. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2059. a subtree."
  2060. :group 'org-todo
  2061. :group 'org-progress
  2062. :type '(choice
  2063. (const :tag "Not into a drawer" nil)
  2064. (const :tag "LOGBOOK" t)
  2065. (string :tag "Other")))
  2066. (if (fboundp 'defvaralias)
  2067. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2068. (defun org-log-into-drawer ()
  2069. "Return the value of `org-log-into-drawer', but let properties overrule.
  2070. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2071. used instead of the default value."
  2072. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  2073. (cond
  2074. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2075. ((equal p "t") "LOGBOOK")
  2076. (t p))))
  2077. (defcustom org-log-state-notes-insert-after-drawers nil
  2078. "Non-nil means insert state change notes after any drawers in entry.
  2079. Only the drawers that *immediately* follow the headline and the
  2080. deadline/scheduled line are skipped.
  2081. When nil, insert notes right after the heading and perhaps the line
  2082. with deadline/scheduling if present.
  2083. This variable will have no effect if `org-log-into-drawer' is
  2084. set."
  2085. :group 'org-todo
  2086. :group 'org-progress
  2087. :type 'boolean)
  2088. (defcustom org-log-states-order-reversed t
  2089. "Non-nil means the latest state note will be directly after heading.
  2090. When nil, the state change notes will be ordered according to time."
  2091. :group 'org-todo
  2092. :group 'org-progress
  2093. :type 'boolean)
  2094. (defcustom org-todo-repeat-to-state nil
  2095. "The TODO state to which a repeater should return the repeating task.
  2096. By default this is the first task in a TODO sequence, or the previous state
  2097. in a TODO_TYP set. But you can specify another task here.
  2098. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2099. :group 'org-todo
  2100. :type '(choice (const :tag "Head of sequence" nil)
  2101. (string :tag "Specific state")))
  2102. (defcustom org-log-repeat 'time
  2103. "Non-nil means record moving through the DONE state when triggering repeat.
  2104. An auto-repeating task is immediately switched back to TODO when
  2105. marked DONE. If you are not logging state changes (by adding \"@\"
  2106. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2107. record a closing note, there will be no record of the task moving
  2108. through DONE. This variable forces taking a note anyway.
  2109. nil Don't force a record
  2110. time Record a time stamp
  2111. note Record a note
  2112. This option can also be set with on a per-file-basis with
  2113. #+STARTUP: logrepeat
  2114. #+STARTUP: lognoterepeat
  2115. #+STARTUP: nologrepeat
  2116. You can have local logging settings for a subtree by setting the LOGGING
  2117. property to one or more of these keywords."
  2118. :group 'org-todo
  2119. :group 'org-progress
  2120. :type '(choice
  2121. (const :tag "Don't force a record" nil)
  2122. (const :tag "Force recording the DONE state" time)
  2123. (const :tag "Force recording a note with the DONE state" note)))
  2124. (defgroup org-priorities nil
  2125. "Priorities in Org-mode."
  2126. :tag "Org Priorities"
  2127. :group 'org-todo)
  2128. (defcustom org-enable-priority-commands t
  2129. "Non-nil means priority commands are active.
  2130. When nil, these commands will be disabled, so that you never accidentally
  2131. set a priority."
  2132. :group 'org-priorities
  2133. :type 'boolean)
  2134. (defcustom org-highest-priority ?A
  2135. "The highest priority of TODO items. A character like ?A, ?B etc.
  2136. Must have a smaller ASCII number than `org-lowest-priority'."
  2137. :group 'org-priorities
  2138. :type 'character)
  2139. (defcustom org-lowest-priority ?C
  2140. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2141. Must have a larger ASCII number than `org-highest-priority'."
  2142. :group 'org-priorities
  2143. :type 'character)
  2144. (defcustom org-default-priority ?B
  2145. "The default priority of TODO items.
  2146. This is the priority an item get if no explicit priority is given."
  2147. :group 'org-priorities
  2148. :type 'character)
  2149. (defcustom org-priority-start-cycle-with-default t
  2150. "Non-nil means start with default priority when starting to cycle.
  2151. When this is nil, the first step in the cycle will be (depending on the
  2152. command used) one higher or lower that the default priority."
  2153. :group 'org-priorities
  2154. :type 'boolean)
  2155. (defcustom org-get-priority-function nil
  2156. "Function to extract the priority from a string.
  2157. The string is normally the headline. If this is nil Org computes the
  2158. priority from the priority cookie like [#A] in the headline. It returns
  2159. an integer, increasing by 1000 for each priority level.
  2160. The user can set a different function here, which should take a string
  2161. as an argument and return the numeric priority."
  2162. :group 'org-priorities
  2163. :type 'function)
  2164. (defgroup org-time nil
  2165. "Options concerning time stamps and deadlines in Org-mode."
  2166. :tag "Org Time"
  2167. :group 'org)
  2168. (defcustom org-insert-labeled-timestamps-at-point nil
  2169. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2170. When nil, these labeled time stamps are forces into the second line of an
  2171. entry, just after the headline. When scheduling from the global TODO list,
  2172. the time stamp will always be forced into the second line."
  2173. :group 'org-time
  2174. :type 'boolean)
  2175. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2176. "Formats for `format-time-string' which are used for time stamps.
  2177. It is not recommended to change this constant.")
  2178. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2179. "Number of minutes to round time stamps to.
  2180. These are two values, the first applies when first creating a time stamp.
  2181. The second applies when changing it with the commands `S-up' and `S-down'.
  2182. When changing the time stamp, this means that it will change in steps
  2183. of N minutes, as given by the second value.
  2184. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2185. numbers should be factors of 60, so for example 5, 10, 15.
  2186. When this is larger than 1, you can still force an exact time stamp by using
  2187. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2188. and by using a prefix arg to `S-up/down' to specify the exact number
  2189. of minutes to shift."
  2190. :group 'org-time
  2191. :get '(lambda (var) ; Make sure both elements are there
  2192. (if (integerp (default-value var))
  2193. (list (default-value var) 5)
  2194. (default-value var)))
  2195. :type '(list
  2196. (integer :tag "when inserting times")
  2197. (integer :tag "when modifying times")))
  2198. ;; Normalize old customizations of this variable.
  2199. (when (integerp org-time-stamp-rounding-minutes)
  2200. (setq org-time-stamp-rounding-minutes
  2201. (list org-time-stamp-rounding-minutes
  2202. org-time-stamp-rounding-minutes)))
  2203. (defcustom org-display-custom-times nil
  2204. "Non-nil means overlay custom formats over all time stamps.
  2205. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2206. To turn this on on a per-file basis, insert anywhere in the file:
  2207. #+STARTUP: customtime"
  2208. :group 'org-time
  2209. :set 'set-default
  2210. :type 'sexp)
  2211. (make-variable-buffer-local 'org-display-custom-times)
  2212. (defcustom org-time-stamp-custom-formats
  2213. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2214. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2215. These are overlayed over the default ISO format if the variable
  2216. `org-display-custom-times' is set. Time like %H:%M should be at the
  2217. end of the second format. The custom formats are also honored by export
  2218. commands, if custom time display is turned on at the time of export."
  2219. :group 'org-time
  2220. :type 'sexp)
  2221. (defun org-time-stamp-format (&optional long inactive)
  2222. "Get the right format for a time string."
  2223. (let ((f (if long (cdr org-time-stamp-formats)
  2224. (car org-time-stamp-formats))))
  2225. (if inactive
  2226. (concat "[" (substring f 1 -1) "]")
  2227. f)))
  2228. (defcustom org-time-clocksum-format "%d:%02d"
  2229. "The format string used when creating CLOCKSUM lines.
  2230. This is also used when org-mode generates a time duration."
  2231. :group 'org-time
  2232. :type 'string)
  2233. (defcustom org-time-clocksum-use-fractional nil
  2234. "If non-nil, \\[org-clock-display] uses fractional times.
  2235. org-mode generates a time duration."
  2236. :group 'org-time
  2237. :type 'boolean)
  2238. (defcustom org-time-clocksum-fractional-format "%.2f"
  2239. "The format string used when creating CLOCKSUM lines, or when
  2240. org-mode generates a time duration."
  2241. :group 'org-time
  2242. :type 'string)
  2243. (defcustom org-deadline-warning-days 14
  2244. "No. of days before expiration during which a deadline becomes active.
  2245. This variable governs the display in sparse trees and in the agenda.
  2246. When 0 or negative, it means use this number (the absolute value of it)
  2247. even if a deadline has a different individual lead time specified.
  2248. Custom commands can set this variable in the options section."
  2249. :group 'org-time
  2250. :group 'org-agenda-daily/weekly
  2251. :type 'integer)
  2252. (defcustom org-read-date-prefer-future t
  2253. "Non-nil means assume future for incomplete date input from user.
  2254. This affects the following situations:
  2255. 1. The user gives a month but not a year.
  2256. For example, if it is April and you enter \"feb 2\", this will be read
  2257. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2258. 2. The user gives a day, but no month.
  2259. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2260. read this as the third of *next* month. However, if you enter \"17\",
  2261. it will be considered as *this* month.
  2262. If you set this variable to the symbol `time', then also the following
  2263. will work:
  2264. 3. If the user gives a time, but no day. If the time is before now,
  2265. to will be interpreted as tomorrow.
  2266. Currently none of this works for ISO week specifications.
  2267. When this option is nil, the current day, month and year will always be
  2268. used as defaults.
  2269. See also `org-agenda-jump-prefer-future'."
  2270. :group 'org-time
  2271. :type '(choice
  2272. (const :tag "Never" nil)
  2273. (const :tag "Check month and day" t)
  2274. (const :tag "Check month, day, and time" time)))
  2275. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2276. "Should the agenda jump command prefer the future for incomplete dates?
  2277. The default is to do the same as configured in `org-read-date-prefer-future'.
  2278. But you can alse set a deviating value here.
  2279. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2280. :group 'org-agenda
  2281. :group 'org-time
  2282. :type '(choice
  2283. (const :tag "Use org-aread-date-prefer-future"
  2284. org-read-date-prefer-future)
  2285. (const :tag "Never" nil)
  2286. (const :tag "Always" t)))
  2287. (defcustom org-read-date-display-live t
  2288. "Non-nil means display current interpretation of date prompt live.
  2289. This display will be in an overlay, in the minibuffer."
  2290. :group 'org-time
  2291. :type 'boolean)
  2292. (defcustom org-read-date-popup-calendar t
  2293. "Non-nil means pop up a calendar when prompting for a date.
  2294. In the calendar, the date can be selected with mouse-1. However, the
  2295. minibuffer will also be active, and you can simply enter the date as well.
  2296. When nil, only the minibuffer will be available."
  2297. :group 'org-time
  2298. :type 'boolean)
  2299. (if (fboundp 'defvaralias)
  2300. (defvaralias 'org-popup-calendar-for-date-prompt
  2301. 'org-read-date-popup-calendar))
  2302. (defcustom org-read-date-minibuffer-setup-hook nil
  2303. "Hook to be used to set up keys for the date/time interface.
  2304. Add key definitions to `minibuffer-local-map', which will be a temporary
  2305. copy."
  2306. :group 'org-time
  2307. :type 'hook)
  2308. (defcustom org-extend-today-until 0
  2309. "The hour when your day really ends. Must be an integer.
  2310. This has influence for the following applications:
  2311. - When switching the agenda to \"today\". It it is still earlier than
  2312. the time given here, the day recognized as TODAY is actually yesterday.
  2313. - When a date is read from the user and it is still before the time given
  2314. here, the current date and time will be assumed to be yesterday, 23:59.
  2315. Also, timestamps inserted in remember templates follow this rule.
  2316. IMPORTANT: This is a feature whose implementation is and likely will
  2317. remain incomplete. Really, it is only here because past midnight seems to
  2318. be the favorite working time of John Wiegley :-)"
  2319. :group 'org-time
  2320. :type 'integer)
  2321. (defcustom org-edit-timestamp-down-means-later nil
  2322. "Non-nil means S-down will increase the time in a time stamp.
  2323. When nil, S-up will increase."
  2324. :group 'org-time
  2325. :type 'boolean)
  2326. (defcustom org-calendar-follow-timestamp-change t
  2327. "Non-nil means make the calendar window follow timestamp changes.
  2328. When a timestamp is modified and the calendar window is visible, it will be
  2329. moved to the new date."
  2330. :group 'org-time
  2331. :type 'boolean)
  2332. (defgroup org-tags nil
  2333. "Options concerning tags in Org-mode."
  2334. :tag "Org Tags"
  2335. :group 'org)
  2336. (defcustom org-tag-alist nil
  2337. "List of tags allowed in Org-mode files.
  2338. When this list is nil, Org-mode will base TAG input on what is already in the
  2339. buffer.
  2340. The value of this variable is an alist, the car of each entry must be a
  2341. keyword as a string, the cdr may be a character that is used to select
  2342. that tag through the fast-tag-selection interface.
  2343. See the manual for details."
  2344. :group 'org-tags
  2345. :type '(repeat
  2346. (choice
  2347. (cons (string :tag "Tag name")
  2348. (character :tag "Access char"))
  2349. (list :tag "Start radio group"
  2350. (const :startgroup)
  2351. (option (string :tag "Group description")))
  2352. (list :tag "End radio group"
  2353. (const :endgroup)
  2354. (option (string :tag "Group description")))
  2355. (const :tag "New line" (:newline)))))
  2356. (defcustom org-tag-persistent-alist nil
  2357. "List of tags that will always appear in all Org-mode files.
  2358. This is in addition to any in buffer settings or customizations
  2359. of `org-tag-alist'.
  2360. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2361. The value of this variable is an alist, the car of each entry must be a
  2362. keyword as a string, the cdr may be a character that is used to select
  2363. that tag through the fast-tag-selection interface.
  2364. See the manual for details.
  2365. To disable these tags on a per-file basis, insert anywhere in the file:
  2366. #+STARTUP: noptag"
  2367. :group 'org-tags
  2368. :type '(repeat
  2369. (choice
  2370. (cons (string :tag "Tag name")
  2371. (character :tag "Access char"))
  2372. (const :tag "Start radio group" (:startgroup))
  2373. (const :tag "End radio group" (:endgroup))
  2374. (const :tag "New line" (:newline)))))
  2375. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2376. "If non-nil, always offer completion for all tags of all agenda files.
  2377. Instead of customizing this variable directly, you might want to
  2378. set it locally for remember buffers, because there no list of
  2379. tags in that file can be created dynamically (there are none).
  2380. (add-hook 'org-remember-mode-hook
  2381. (lambda ()
  2382. (set (make-local-variable
  2383. 'org-complete-tags-always-offer-all-agenda-tags)
  2384. t)))"
  2385. :group 'org-tags
  2386. :type 'boolean)
  2387. (defvar org-file-tags nil
  2388. "List of tags that can be inherited by all entries in the file.
  2389. The tags will be inherited if the variable `org-use-tag-inheritance'
  2390. says they should be.
  2391. This variable is populated from #+FILETAGS lines.")
  2392. (defcustom org-use-fast-tag-selection 'auto
  2393. "Non-nil means use fast tag selection scheme.
  2394. This is a special interface to select and deselect tags with single keys.
  2395. When nil, fast selection is never used.
  2396. When the symbol `auto', fast selection is used if and only if selection
  2397. characters for tags have been configured, either through the variable
  2398. `org-tag-alist' or through a #+TAGS line in the buffer.
  2399. When t, fast selection is always used and selection keys are assigned
  2400. automatically if necessary."
  2401. :group 'org-tags
  2402. :type '(choice
  2403. (const :tag "Always" t)
  2404. (const :tag "Never" nil)
  2405. (const :tag "When selection characters are configured" 'auto)))
  2406. (defcustom org-fast-tag-selection-single-key nil
  2407. "Non-nil means fast tag selection exits after first change.
  2408. When nil, you have to press RET to exit it.
  2409. During fast tag selection, you can toggle this flag with `C-c'.
  2410. This variable can also have the value `expert'. In this case, the window
  2411. displaying the tags menu is not even shown, until you press C-c again."
  2412. :group 'org-tags
  2413. :type '(choice
  2414. (const :tag "No" nil)
  2415. (const :tag "Yes" t)
  2416. (const :tag "Expert" expert)))
  2417. (defvar org-fast-tag-selection-include-todo nil
  2418. "Non-nil means fast tags selection interface will also offer TODO states.
  2419. This is an undocumented feature, you should not rely on it.")
  2420. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2421. "The column to which tags should be indented in a headline.
  2422. If this number is positive, it specifies the column. If it is negative,
  2423. it means that the tags should be flushright to that column. For example,
  2424. -80 works well for a normal 80 character screen."
  2425. :group 'org-tags
  2426. :type 'integer)
  2427. (defcustom org-auto-align-tags t
  2428. "Non-nil means realign tags after pro/demotion of TODO state change.
  2429. These operations change the length of a headline and therefore shift
  2430. the tags around. With this options turned on, after each such operation
  2431. the tags are again aligned to `org-tags-column'."
  2432. :group 'org-tags
  2433. :type 'boolean)
  2434. (defcustom org-use-tag-inheritance t
  2435. "Non-nil means tags in levels apply also for sublevels.
  2436. When nil, only the tags directly given in a specific line apply there.
  2437. This may also be a list of tags that should be inherited, or a regexp that
  2438. matches tags that should be inherited. Additional control is possible
  2439. with the variable `org-tags-exclude-from-inheritance' which gives an
  2440. explicit list of tags to be excluded from inheritance., even if the value of
  2441. `org-use-tag-inheritance' would select it for inheritance.
  2442. If this option is t, a match early-on in a tree can lead to a large
  2443. number of matches in the subtree when constructing the agenda or creating
  2444. a sparse tree. If you only want to see the first match in a tree during
  2445. a search, check out the variable `org-tags-match-list-sublevels'."
  2446. :group 'org-tags
  2447. :type '(choice
  2448. (const :tag "Not" nil)
  2449. (const :tag "Always" t)
  2450. (repeat :tag "Specific tags" (string :tag "Tag"))
  2451. (regexp :tag "Tags matched by regexp")))
  2452. (defcustom org-tags-exclude-from-inheritance nil
  2453. "List of tags that should never be inherited.
  2454. This is a way to exclude a few tags from inheritance. For way to do
  2455. the opposite, to actively allow inheritance for selected tags,
  2456. see the variable `org-use-tag-inheritance'."
  2457. :group 'org-tags
  2458. :type '(repeat (string :tag "Tag")))
  2459. (defun org-tag-inherit-p (tag)
  2460. "Check if TAG is one that should be inherited."
  2461. (cond
  2462. ((member tag org-tags-exclude-from-inheritance) nil)
  2463. ((eq org-use-tag-inheritance t) t)
  2464. ((not org-use-tag-inheritance) nil)
  2465. ((stringp org-use-tag-inheritance)
  2466. (string-match org-use-tag-inheritance tag))
  2467. ((listp org-use-tag-inheritance)
  2468. (member tag org-use-tag-inheritance))
  2469. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2470. (defcustom org-tags-match-list-sublevels t
  2471. "Non-nil means list also sublevels of headlines matching a search.
  2472. This variable applies to tags/property searches, and also to stuck
  2473. projects because this search is based on a tags match as well.
  2474. When set to the symbol `indented', sublevels are indented with
  2475. leading dots.
  2476. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2477. the sublevels of a headline matching a tag search often also match
  2478. the same search. Listing all of them can create very long lists.
  2479. Setting this variable to nil causes subtrees of a match to be skipped.
  2480. This variable is semi-obsolete and probably should always be true. It
  2481. is better to limit inheritance to certain tags using the variables
  2482. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2483. :group 'org-tags
  2484. :type '(choice
  2485. (const :tag "No, don't list them" nil)
  2486. (const :tag "Yes, do list them" t)
  2487. (const :tag "List them, indented with leading dots" indented)))
  2488. (defcustom org-tags-sort-function nil
  2489. "When set, tags are sorted using this function as a comparator."
  2490. :group 'org-tags
  2491. :type '(choice
  2492. (const :tag "No sorting" nil)
  2493. (const :tag "Alphabetical" string<)
  2494. (const :tag "Reverse alphabetical" string>)
  2495. (function :tag "Custom function" nil)))
  2496. (defvar org-tags-history nil
  2497. "History of minibuffer reads for tags.")
  2498. (defvar org-last-tags-completion-table nil
  2499. "The last used completion table for tags.")
  2500. (defvar org-after-tags-change-hook nil
  2501. "Hook that is run after the tags in a line have changed.")
  2502. (defgroup org-properties nil
  2503. "Options concerning properties in Org-mode."
  2504. :tag "Org Properties"
  2505. :group 'org)
  2506. (defcustom org-property-format "%-10s %s"
  2507. "How property key/value pairs should be formatted by `indent-line'.
  2508. When `indent-line' hits a property definition, it will format the line
  2509. according to this format, mainly to make sure that the values are
  2510. lined-up with respect to each other."
  2511. :group 'org-properties
  2512. :type 'string)
  2513. (defcustom org-use-property-inheritance nil
  2514. "Non-nil means properties apply also for sublevels.
  2515. This setting is chiefly used during property searches. Turning it on can
  2516. cause significant overhead when doing a search, which is why it is not
  2517. on by default.
  2518. When nil, only the properties directly given in the current entry count.
  2519. When t, every property is inherited. The value may also be a list of
  2520. properties that should have inheritance, or a regular expression matching
  2521. properties that should be inherited.
  2522. However, note that some special properties use inheritance under special
  2523. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2524. and the properties ending in \"_ALL\" when they are used as descriptor
  2525. for valid values of a property.
  2526. Note for programmers:
  2527. When querying an entry with `org-entry-get', you can control if inheritance
  2528. should be used. By default, `org-entry-get' looks only at the local
  2529. properties. You can request inheritance by setting the inherit argument
  2530. to t (to force inheritance) or to `selective' (to respect the setting
  2531. in this variable)."
  2532. :group 'org-properties
  2533. :type '(choice
  2534. (const :tag "Not" nil)
  2535. (const :tag "Always" t)
  2536. (repeat :tag "Specific properties" (string :tag "Property"))
  2537. (regexp :tag "Properties matched by regexp")))
  2538. (defun org-property-inherit-p (property)
  2539. "Check if PROPERTY is one that should be inherited."
  2540. (cond
  2541. ((eq org-use-property-inheritance t) t)
  2542. ((not org-use-property-inheritance) nil)
  2543. ((stringp org-use-property-inheritance)
  2544. (string-match org-use-property-inheritance property))
  2545. ((listp org-use-property-inheritance)
  2546. (member property org-use-property-inheritance))
  2547. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2548. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2549. "The default column format, if no other format has been defined.
  2550. This variable can be set on the per-file basis by inserting a line
  2551. #+COLUMNS: %25ITEM ....."
  2552. :group 'org-properties
  2553. :type 'string)
  2554. (defcustom org-columns-ellipses ".."
  2555. "The ellipses to be used when a field in column view is truncated.
  2556. When this is the empty string, as many characters as possible are shown,
  2557. but then there will be no visual indication that the field has been truncated.
  2558. When this is a string of length N, the last N characters of a truncated
  2559. field are replaced by this string. If the column is narrower than the
  2560. ellipses string, only part of the ellipses string will be shown."
  2561. :group 'org-properties
  2562. :type 'string)
  2563. (defcustom org-columns-modify-value-for-display-function nil
  2564. "Function that modifies values for display in column view.
  2565. For example, it can be used to cut out a certain part from a time stamp.
  2566. The function must take 2 arguments:
  2567. column-title The title of the column (*not* the property name)
  2568. value The value that should be modified.
  2569. The function should return the value that should be displayed,
  2570. or nil if the normal value should be used."
  2571. :group 'org-properties
  2572. :type 'function)
  2573. (defcustom org-effort-property "Effort"
  2574. "The property that is being used to keep track of effort estimates.
  2575. Effort estimates given in this property need to have the format H:MM."
  2576. :group 'org-properties
  2577. :group 'org-progress
  2578. :type '(string :tag "Property"))
  2579. (defconst org-global-properties-fixed
  2580. '(("VISIBILITY_ALL" . "folded children content all")
  2581. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2582. "List of property/value pairs that can be inherited by any entry.
  2583. These are fixed values, for the preset properties. The user variable
  2584. that can be used to add to this list is `org-global-properties'.
  2585. The entries in this list are cons cells where the car is a property
  2586. name and cdr is a string with the value. If the value represents
  2587. multiple items like an \"_ALL\" property, separate the items by
  2588. spaces.")
  2589. (defcustom org-global-properties nil
  2590. "List of property/value pairs that can be inherited by any entry.
  2591. This list will be combined with the constant `org-global-properties-fixed'.
  2592. The entries in this list are cons cells where the car is a property
  2593. name and cdr is a string with the value.
  2594. You can set buffer-local values for the same purpose in the variable
  2595. `org-file-properties' this by adding lines like
  2596. #+PROPERTY: NAME VALUE"
  2597. :group 'org-properties
  2598. :type '(repeat
  2599. (cons (string :tag "Property")
  2600. (string :tag "Value"))))
  2601. (defvar org-file-properties nil
  2602. "List of property/value pairs that can be inherited by any entry.
  2603. Valid for the current buffer.
  2604. This variable is populated from #+PROPERTY lines.")
  2605. (make-variable-buffer-local 'org-file-properties)
  2606. (defgroup org-agenda nil
  2607. "Options concerning agenda views in Org-mode."
  2608. :tag "Org Agenda"
  2609. :group 'org)
  2610. (defvar org-category nil
  2611. "Variable used by org files to set a category for agenda display.
  2612. Such files should use a file variable to set it, for example
  2613. # -*- mode: org; org-category: \"ELisp\"
  2614. or contain a special line
  2615. #+CATEGORY: ELisp
  2616. If the file does not specify a category, then file's base name
  2617. is used instead.")
  2618. (make-variable-buffer-local 'org-category)
  2619. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2620. (defcustom org-agenda-files nil
  2621. "The files to be used for agenda display.
  2622. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2623. \\[org-remove-file]. You can also use customize to edit the list.
  2624. If an entry is a directory, all files in that directory that are matched by
  2625. `org-agenda-file-regexp' will be part of the file list.
  2626. If the value of the variable is not a list but a single file name, then
  2627. the list of agenda files is actually stored and maintained in that file, one
  2628. agenda file per line. In this file paths can be given relative to
  2629. `org-directory'. Tilde expansion and environment variable substitution
  2630. are also made."
  2631. :group 'org-agenda
  2632. :type '(choice
  2633. (repeat :tag "List of files and directories" file)
  2634. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2635. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2636. "Regular expression to match files for `org-agenda-files'.
  2637. If any element in the list in that variable contains a directory instead
  2638. of a normal file, all files in that directory that are matched by this
  2639. regular expression will be included."
  2640. :group 'org-agenda
  2641. :type 'regexp)
  2642. (defcustom org-agenda-text-search-extra-files nil
  2643. "List of extra files to be searched by text search commands.
  2644. These files will be search in addition to the agenda files by the
  2645. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2646. Note that these files will only be searched for text search commands,
  2647. not for the other agenda views like todo lists, tag searches or the weekly
  2648. agenda. This variable is intended to list notes and possibly archive files
  2649. that should also be searched by these two commands.
  2650. In fact, if the first element in the list is the symbol `agenda-archives',
  2651. than all archive files of all agenda files will be added to the search
  2652. scope."
  2653. :group 'org-agenda
  2654. :type '(set :greedy t
  2655. (const :tag "Agenda Archives" agenda-archives)
  2656. (repeat :inline t (file))))
  2657. (if (fboundp 'defvaralias)
  2658. (defvaralias 'org-agenda-multi-occur-extra-files
  2659. 'org-agenda-text-search-extra-files))
  2660. (defcustom org-agenda-skip-unavailable-files nil
  2661. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2662. A nil value means to remove them, after a query, from the list."
  2663. :group 'org-agenda
  2664. :type 'boolean)
  2665. (defcustom org-calendar-to-agenda-key [?c]
  2666. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2667. The command `org-calendar-goto-agenda' will be bound to this key. The
  2668. default is the character `c' because then `c' can be used to switch back and
  2669. forth between agenda and calendar."
  2670. :group 'org-agenda
  2671. :type 'sexp)
  2672. (defcustom org-calendar-agenda-action-key [?k]
  2673. "The key to be installed in `calendar-mode-map' for agenda-action.
  2674. The command `org-agenda-action' will be bound to this key. The
  2675. default is the character `k' because we use the same key in the agenda."
  2676. :group 'org-agenda
  2677. :type 'sexp)
  2678. (defcustom org-calendar-insert-diary-entry-key [?i]
  2679. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2680. This option is irrelevant until `org-agenda-diary-file' has been configured
  2681. to point to an Org-mode file. When that is the case, the command
  2682. `org-agenda-diary-entry' will be bound to the key given here, by default
  2683. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2684. if you want to continue doing this, you need to change this to a different
  2685. key."
  2686. :group 'org-agenda
  2687. :type 'sexp)
  2688. (defcustom org-agenda-diary-file 'diary-file
  2689. "File to which to add new entries with the `i' key in agenda and calendar.
  2690. When this is the symbol `diary-file', the functionality in the Emacs
  2691. calendar will be used to add entries to the `diary-file'. But when this
  2692. points to a file, `org-agenda-diary-entry' will be used instead."
  2693. :group 'org-agenda
  2694. :type '(choice
  2695. (const :tag "The standard Emacs diary file" diary-file)
  2696. (file :tag "Special Org file diary entries")))
  2697. (eval-after-load "calendar"
  2698. '(progn
  2699. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2700. 'org-calendar-goto-agenda)
  2701. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2702. 'org-agenda-action)
  2703. (add-hook 'calendar-mode-hook
  2704. (lambda ()
  2705. (unless (eq org-agenda-diary-file 'diary-file)
  2706. (define-key calendar-mode-map
  2707. org-calendar-insert-diary-entry-key
  2708. 'org-agenda-diary-entry))))))
  2709. (defgroup org-latex nil
  2710. "Options for embedding LaTeX code into Org-mode."
  2711. :tag "Org LaTeX"
  2712. :group 'org)
  2713. (defcustom org-format-latex-options
  2714. '(:foreground default :background default :scale 1.0
  2715. :html-foreground "Black" :html-background "Transparent"
  2716. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2717. "Options for creating images from LaTeX fragments.
  2718. This is a property list with the following properties:
  2719. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2720. `default' means use the foreground of the default face.
  2721. :background the background color, or \"Transparent\".
  2722. `default' means use the background of the default face.
  2723. :scale a scaling factor for the size of the images, to get more pixels
  2724. :html-foreground, :html-background, :html-scale
  2725. the same numbers for HTML export.
  2726. :matchers a list indicating which matchers should be used to
  2727. find LaTeX fragments. Valid members of this list are:
  2728. \"begin\" find environments
  2729. \"$1\" find single characters surrounded by $.$
  2730. \"$\" find math expressions surrounded by $...$
  2731. \"$$\" find math expressions surrounded by $$....$$
  2732. \"\\(\" find math expressions surrounded by \\(...\\)
  2733. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2734. :group 'org-latex
  2735. :type 'plist)
  2736. (defcustom org-format-latex-signal-error t
  2737. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2738. When nil, just push out a message."
  2739. :group 'org-latex
  2740. :type 'boolean)
  2741. (defcustom org-format-latex-header "\\documentclass{article}
  2742. \\usepackage[usenames]{color}
  2743. \\usepackage{amsmath}
  2744. \\usepackage[mathscr]{eucal}
  2745. \\pagestyle{empty} % do not remove
  2746. \[PACKAGES]
  2747. \[DEFAULT-PACKAGES]
  2748. % The settings below are copied from fullpage.sty
  2749. \\setlength{\\textwidth}{\\paperwidth}
  2750. \\addtolength{\\textwidth}{-3cm}
  2751. \\setlength{\\oddsidemargin}{1.5cm}
  2752. \\addtolength{\\oddsidemargin}{-2.54cm}
  2753. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2754. \\setlength{\\textheight}{\\paperheight}
  2755. \\addtolength{\\textheight}{-\\headheight}
  2756. \\addtolength{\\textheight}{-\\headsep}
  2757. \\addtolength{\\textheight}{-\\footskip}
  2758. \\addtolength{\\textheight}{-3cm}
  2759. \\setlength{\\topmargin}{1.5cm}
  2760. \\addtolength{\\topmargin}{-2.54cm}"
  2761. "The document header used for processing LaTeX fragments.
  2762. It is imperative that this header make sure that no page number
  2763. appears on the page. The package defined in the variables
  2764. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2765. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2766. will be appended."
  2767. :group 'org-latex
  2768. :type 'string)
  2769. (defvar org-format-latex-header-extra nil)
  2770. (defun org-set-packages-alist (var val)
  2771. "Set the packages alist and make sure it has 3 elements per entry."
  2772. (set var (mapcar (lambda (x)
  2773. (if (and (consp x) (= (length x) 2))
  2774. (list (car x) (nth 1 x) t)
  2775. x))
  2776. val)))
  2777. (defun org-get-packages-alist (var)
  2778. "Get the packages alist and make sure it has 3 elements per entry."
  2779. (mapcar (lambda (x)
  2780. (if (and (consp x) (= (length x) 2))
  2781. (list (car x) (nth 1 x) t)
  2782. x))
  2783. (default-value var)))
  2784. ;; The following variables are defined here because is it also used
  2785. ;; when formatting latex fragments. Originally it was part of the
  2786. ;; LaTeX exporter, which is why the name includes "export".
  2787. (defcustom org-export-latex-default-packages-alist
  2788. '(("AUTO" "inputenc" t)
  2789. ("T1" "fontenc" t)
  2790. ("" "fixltx2e" nil)
  2791. ("" "graphicx" t)
  2792. ("" "longtable" nil)
  2793. ("" "float" nil)
  2794. ("" "wrapfig" nil)
  2795. ("" "soul" t)
  2796. ("" "textcomp" t)
  2797. ("" "marvosym" t)
  2798. ("" "wasysym" t)
  2799. ("" "latexsym" t)
  2800. ("" "amssymb" t)
  2801. ("" "hyperref" nil)
  2802. "\\tolerance=1000"
  2803. )
  2804. "Alist of default packages to be inserted in the header.
  2805. Change this only if one of the packages here causes an incompatibility
  2806. with another package you are using.
  2807. The packages in this list are needed by one part or another of Org-mode
  2808. to function properly.
  2809. - inputenc, fontenc: for basic font and character selection
  2810. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2811. for interpreting the entities in `org-entities'. You can skip some of these
  2812. packages if you don't use any of the symbols in it.
  2813. - graphicx: for including images
  2814. - float, wrapfig: for figure placement
  2815. - longtable: for long tables
  2816. - hyperref: for cross references
  2817. Therefore you should not modify this variable unless you know what you
  2818. are doing. The one reason to change it anyway is that you might be loading
  2819. some other package that conflicts with one of the default packages.
  2820. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2821. If SNIPPET-FLAG is t, the package also needs to be included when
  2822. compiling LaTeX snippets into images for inclusion into HTML."
  2823. :group 'org-export-latex
  2824. :set 'org-set-packages-alist
  2825. :get 'org-get-packages-alist
  2826. :type '(repeat
  2827. (choice
  2828. (list :tag "options/package pair"
  2829. (string :tag "options")
  2830. (string :tag "package")
  2831. (boolean :tag "Snippet"))
  2832. (string :tag "A line of LaTeX"))))
  2833. (defcustom org-export-latex-packages-alist nil
  2834. "Alist of packages to be inserted in every LaTeX header.
  2835. These will be inserted after `org-export-latex-default-packages-alist'.
  2836. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2837. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2838. turning LaTeX snippets into images for inclusion into HTML.
  2839. Make sure that you only list packages here which:
  2840. - you want in every file
  2841. - do not conflict with the default packages in
  2842. `org-export-latex-default-packages-alist'
  2843. - do not conflict with the setup in `org-format-latex-header'."
  2844. :group 'org-export-latex
  2845. :set 'org-set-packages-alist
  2846. :get 'org-get-packages-alist
  2847. :type '(repeat
  2848. (choice
  2849. (list :tag "options/package pair"
  2850. (string :tag "options")
  2851. (string :tag "package")
  2852. (boolean :tag "Snippet"))
  2853. (string :tag "A line of LaTeX"))))
  2854. (defgroup org-appearance nil
  2855. "Settings for Org-mode appearance."
  2856. :tag "Org Appearance"
  2857. :group 'org)
  2858. (defcustom org-level-color-stars-only nil
  2859. "Non-nil means fontify only the stars in each headline.
  2860. When nil, the entire headline is fontified.
  2861. Changing it requires restart of `font-lock-mode' to become effective
  2862. also in regions already fontified."
  2863. :group 'org-appearance
  2864. :type 'boolean)
  2865. (defcustom org-hide-leading-stars nil
  2866. "Non-nil means hide the first N-1 stars in a headline.
  2867. This works by using the face `org-hide' for these stars. This
  2868. face is white for a light background, and black for a dark
  2869. background. You may have to customize the face `org-hide' to
  2870. make this work.
  2871. Changing it requires restart of `font-lock-mode' to become effective
  2872. also in regions already fontified.
  2873. You may also set this on a per-file basis by adding one of the following
  2874. lines to the buffer:
  2875. #+STARTUP: hidestars
  2876. #+STARTUP: showstars"
  2877. :group 'org-appearance
  2878. :type 'boolean)
  2879. (defcustom org-hidden-keywords nil
  2880. "List of keywords that should be hidden when typed in the org buffer.
  2881. For example, add #+TITLE to this list in order to make the
  2882. document title appear in the buffer without the initial #+TITLE:
  2883. keyword."
  2884. :group 'org-appearance
  2885. :type '(set (const :tag "#+AUTHOR" author)
  2886. (const :tag "#+DATE" date)
  2887. (const :tag "#+EMAIL" email)
  2888. (const :tag "#+TITLE" title)))
  2889. (defcustom org-fontify-done-headline nil
  2890. "Non-nil means change the face of a headline if it is marked DONE.
  2891. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2892. When this is non-nil, the headline after the keyword is set to the
  2893. `org-headline-done' as an additional indication."
  2894. :group 'org-appearance
  2895. :type 'boolean)
  2896. (defcustom org-fontify-emphasized-text t
  2897. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2898. Changing this variable requires a restart of Emacs to take effect."
  2899. :group 'org-appearance
  2900. :type 'boolean)
  2901. (defcustom org-fontify-whole-heading-line nil
  2902. "Non-nil means fontify the whole line for headings.
  2903. This is useful when setting a background color for the
  2904. org-level-* faces."
  2905. :group 'org-appearance
  2906. :type 'boolean)
  2907. (defcustom org-highlight-latex-fragments-and-specials nil
  2908. "Non-nil means fontify what is treated specially by the exporters."
  2909. :group 'org-appearance
  2910. :type 'boolean)
  2911. (defcustom org-hide-emphasis-markers nil
  2912. "Non-nil mean font-lock should hide the emphasis marker characters."
  2913. :group 'org-appearance
  2914. :type 'boolean)
  2915. (defcustom org-pretty-entities nil
  2916. "Non-nil means show entities as UTF8 characters.
  2917. When nil, the \\name form remains in the buffer."
  2918. :group 'org-appearance
  2919. :type 'boolean)
  2920. (defcustom org-pretty-entities-include-sub-superscripts t
  2921. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2922. :group 'org-appearance
  2923. :type 'boolean)
  2924. (defvar org-emph-re nil
  2925. "Regular expression for matching emphasis.
  2926. After a match, the match groups contain these elements:
  2927. 0 The match of the full regular expression, including the characters
  2928. before and after the proper match
  2929. 1 The character before the proper match, or empty at beginning of line
  2930. 2 The proper match, including the leading and trailing markers
  2931. 3 The leading marker like * or /, indicating the type of highlighting
  2932. 4 The text between the emphasis markers, not including the markers
  2933. 5 The character after the match, empty at the end of a line")
  2934. (defvar org-verbatim-re nil
  2935. "Regular expression for matching verbatim text.")
  2936. (defvar org-emphasis-regexp-components) ; defined just below
  2937. (defvar org-emphasis-alist) ; defined just below
  2938. (defun org-set-emph-re (var val)
  2939. "Set variable and compute the emphasis regular expression."
  2940. (set var val)
  2941. (when (and (boundp 'org-emphasis-alist)
  2942. (boundp 'org-emphasis-regexp-components)
  2943. org-emphasis-alist org-emphasis-regexp-components)
  2944. (let* ((e org-emphasis-regexp-components)
  2945. (pre (car e))
  2946. (post (nth 1 e))
  2947. (border (nth 2 e))
  2948. (body (nth 3 e))
  2949. (nl (nth 4 e))
  2950. (body1 (concat body "*?"))
  2951. (markers (mapconcat 'car org-emphasis-alist ""))
  2952. (vmarkers (mapconcat
  2953. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2954. org-emphasis-alist "")))
  2955. ;; make sure special characters appear at the right position in the class
  2956. (if (string-match "\\^" markers)
  2957. (setq markers (concat (replace-match "" t t markers) "^")))
  2958. (if (string-match "-" markers)
  2959. (setq markers (concat (replace-match "" t t markers) "-")))
  2960. (if (string-match "\\^" vmarkers)
  2961. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2962. (if (string-match "-" vmarkers)
  2963. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2964. (if (> nl 0)
  2965. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2966. (int-to-string nl) "\\}")))
  2967. ;; Make the regexp
  2968. (setq org-emph-re
  2969. (concat "\\([" pre "]\\|^\\)"
  2970. "\\("
  2971. "\\([" markers "]\\)"
  2972. "\\("
  2973. "[^" border "]\\|"
  2974. "[^" border "]"
  2975. body1
  2976. "[^" border "]"
  2977. "\\)"
  2978. "\\3\\)"
  2979. "\\([" post "]\\|$\\)"))
  2980. (setq org-verbatim-re
  2981. (concat "\\([" pre "]\\|^\\)"
  2982. "\\("
  2983. "\\([" vmarkers "]\\)"
  2984. "\\("
  2985. "[^" border "]\\|"
  2986. "[^" border "]"
  2987. body1
  2988. "[^" border "]"
  2989. "\\)"
  2990. "\\3\\)"
  2991. "\\([" post "]\\|$\\)")))))
  2992. (defcustom org-emphasis-regexp-components
  2993. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2994. "Components used to build the regular expression for emphasis.
  2995. This is a list with five entries. Terminology: In an emphasis string
  2996. like \" *strong word* \", we call the initial space PREMATCH, the final
  2997. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2998. and \"trong wor\" is the body. The different components in this variable
  2999. specify what is allowed/forbidden in each part:
  3000. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3001. post Chars allowed as postmatch. End of line will be allowed too.
  3002. border The chars *forbidden* as border characters.
  3003. body-regexp A regexp like \".\" to match a body character. Don't use
  3004. non-shy groups here, and don't allow newline here.
  3005. newline The maximum number of newlines allowed in an emphasis exp.
  3006. Use customize to modify this, or restart Emacs after changing it."
  3007. :group 'org-appearance
  3008. :set 'org-set-emph-re
  3009. :type '(list
  3010. (sexp :tag "Allowed chars in pre ")
  3011. (sexp :tag "Allowed chars in post ")
  3012. (sexp :tag "Forbidden chars in border ")
  3013. (sexp :tag "Regexp for body ")
  3014. (integer :tag "number of newlines allowed")
  3015. (option (boolean :tag "Please ignore this button"))))
  3016. (defcustom org-emphasis-alist
  3017. `(("*" bold "<b>" "</b>")
  3018. ("/" italic "<i>" "</i>")
  3019. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3020. ("=" org-code "<code>" "</code>" verbatim)
  3021. ("~" org-verbatim "<code>" "</code>" verbatim)
  3022. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3023. "<del>" "</del>")
  3024. )
  3025. "Special syntax for emphasized text.
  3026. Text starting and ending with a special character will be emphasized, for
  3027. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3028. characters, the face to be used by font-lock for highlighting in Org-mode
  3029. Emacs buffers, and the HTML tags to be used for this.
  3030. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3031. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3032. Use customize to modify this, or restart Emacs after changing it."
  3033. :group 'org-appearance
  3034. :set 'org-set-emph-re
  3035. :type '(repeat
  3036. (list
  3037. (string :tag "Marker character")
  3038. (choice
  3039. (face :tag "Font-lock-face")
  3040. (plist :tag "Face property list"))
  3041. (string :tag "HTML start tag")
  3042. (string :tag "HTML end tag")
  3043. (option (const verbatim)))))
  3044. (defvar org-protecting-blocks
  3045. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3046. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3047. This is needed for font-lock setup.")
  3048. ;;; Miscellaneous options
  3049. (defgroup org-completion nil
  3050. "Completion in Org-mode."
  3051. :tag "Org Completion"
  3052. :group 'org)
  3053. (defcustom org-completion-use-ido nil
  3054. "Non-nil means use ido completion wherever possible.
  3055. Note that `ido-mode' must be active for this variable to be relevant.
  3056. If you decide to turn this variable on, you might well want to turn off
  3057. `org-outline-path-complete-in-steps'.
  3058. See also `org-completion-use-iswitchb'."
  3059. :group 'org-completion
  3060. :type 'boolean)
  3061. (defcustom org-completion-use-iswitchb nil
  3062. "Non-nil means use iswitchb completion wherever possible.
  3063. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3064. If you decide to turn this variable on, you might well want to turn off
  3065. `org-outline-path-complete-in-steps'.
  3066. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3067. :group 'org-completion
  3068. :type 'boolean)
  3069. (defcustom org-completion-fallback-command 'hippie-expand
  3070. "The expansion command called by \\[pcomplete] in normal context.
  3071. Normal means, no org-mode-specific context."
  3072. :group 'org-completion
  3073. :type 'function)
  3074. ;;; Functions and variables from their packages
  3075. ;; Declared here to avoid compiler warnings
  3076. ;; XEmacs only
  3077. (defvar outline-mode-menu-heading)
  3078. (defvar outline-mode-menu-show)
  3079. (defvar outline-mode-menu-hide)
  3080. (defvar zmacs-regions) ; XEmacs regions
  3081. ;; Emacs only
  3082. (defvar mark-active)
  3083. ;; Various packages
  3084. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3085. (declare-function calendar-forward-day "cal-move" (arg))
  3086. (declare-function calendar-goto-date "cal-move" (date))
  3087. (declare-function calendar-goto-today "cal-move" ())
  3088. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3089. (defvar calc-embedded-close-formula)
  3090. (defvar calc-embedded-open-formula)
  3091. (declare-function cdlatex-tab "ext:cdlatex" ())
  3092. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3093. (defvar font-lock-unfontify-region-function)
  3094. (declare-function iswitchb-read-buffer "iswitchb"
  3095. (prompt &optional default require-match start matches-set))
  3096. (defvar iswitchb-temp-buflist)
  3097. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3098. (defvar org-agenda-tags-todo-honor-ignore-options)
  3099. (declare-function org-agenda-skip "org-agenda" ())
  3100. (declare-function
  3101. org-format-agenda-item "org-agenda"
  3102. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3103. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3104. (declare-function org-agenda-change-all-lines "org-agenda"
  3105. (newhead hdmarker &optional fixface just-this))
  3106. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3107. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3108. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3109. (beg end))
  3110. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3111. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3112. "org-agenda" (&optional end))
  3113. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3114. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3115. (declare-function org-indent-mode "org-indent" (&optional arg))
  3116. (declare-function parse-time-string "parse-time" (string))
  3117. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3118. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3119. (defvar remember-data-file)
  3120. (defvar texmathp-why)
  3121. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3122. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3123. (defvar w3m-current-url)
  3124. (defvar w3m-current-title)
  3125. (defvar org-latex-regexps)
  3126. ;;; Autoload and prepare some org modules
  3127. ;; Some table stuff that needs to be defined here, because it is used
  3128. ;; by the functions setting up org-mode or checking for table context.
  3129. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3130. "Detect an org-type or table-type table.")
  3131. (defconst org-table-line-regexp "^[ \t]*|"
  3132. "Detect an org-type table line.")
  3133. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3134. "Detect an org-type table line.")
  3135. (defconst org-table-hline-regexp "^[ \t]*|-"
  3136. "Detect an org-type table hline.")
  3137. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3138. "Detect a table-type table hline.")
  3139. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3140. "Detect the first line outside a table when searching from within it.
  3141. This works for both table types.")
  3142. ;; Autoload the functions in org-table.el that are needed by functions here.
  3143. (eval-and-compile
  3144. (org-autoload "org-table"
  3145. '(org-table-align org-table-begin org-table-blank-field
  3146. org-table-convert org-table-convert-region org-table-copy-down
  3147. org-table-copy-region org-table-create
  3148. org-table-create-or-convert-from-region
  3149. org-table-create-with-table.el org-table-current-dline
  3150. org-table-cut-region org-table-delete-column org-table-edit-field
  3151. org-table-edit-formulas org-table-end org-table-eval-formula
  3152. org-table-export org-table-field-info
  3153. org-table-get-stored-formulas org-table-goto-column
  3154. org-table-hline-and-move org-table-import org-table-insert-column
  3155. org-table-insert-hline org-table-insert-row org-table-iterate
  3156. org-table-justify-field-maybe org-table-kill-row
  3157. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3158. org-table-move-column org-table-move-column-left
  3159. org-table-move-column-right org-table-move-row
  3160. org-table-move-row-down org-table-move-row-up
  3161. org-table-next-field org-table-next-row org-table-paste-rectangle
  3162. org-table-previous-field org-table-recalculate
  3163. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3164. org-table-toggle-coordinate-overlays
  3165. org-table-toggle-formula-debugger org-table-wrap-region
  3166. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3167. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3168. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3169. (defun org-at-table-p (&optional table-type)
  3170. "Return t if the cursor is inside an org-type table.
  3171. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3172. (if org-enable-table-editor
  3173. (save-excursion
  3174. (beginning-of-line 1)
  3175. (looking-at (if table-type org-table-any-line-regexp
  3176. org-table-line-regexp)))
  3177. nil))
  3178. (defsubst org-table-p () (org-at-table-p))
  3179. (defun org-at-table.el-p ()
  3180. "Return t if and only if we are at a table.el table."
  3181. (and (org-at-table-p 'any)
  3182. (save-excursion
  3183. (goto-char (org-table-begin 'any))
  3184. (looking-at org-table1-hline-regexp))))
  3185. (defun org-table-recognize-table.el ()
  3186. "If there is a table.el table nearby, recognize it and move into it."
  3187. (if org-table-tab-recognizes-table.el
  3188. (if (org-at-table.el-p)
  3189. (progn
  3190. (beginning-of-line 1)
  3191. (if (looking-at org-table-dataline-regexp)
  3192. nil
  3193. (if (looking-at org-table1-hline-regexp)
  3194. (progn
  3195. (beginning-of-line 2)
  3196. (if (looking-at org-table-any-border-regexp)
  3197. (beginning-of-line -1)))))
  3198. (if (re-search-forward "|" (org-table-end t) t)
  3199. (progn
  3200. (require 'table)
  3201. (if (table--at-cell-p (point))
  3202. t
  3203. (message "recognizing table.el table...")
  3204. (table-recognize-table)
  3205. (message "recognizing table.el table...done")))
  3206. (error "This should not happen"))
  3207. t)
  3208. nil)
  3209. nil))
  3210. (defun org-at-table-hline-p ()
  3211. "Return t if the cursor is inside a hline in a table."
  3212. (if org-enable-table-editor
  3213. (save-excursion
  3214. (beginning-of-line 1)
  3215. (looking-at org-table-hline-regexp))
  3216. nil))
  3217. (defvar org-table-clean-did-remove-column nil)
  3218. (defun org-table-map-tables (function &optional quietly)
  3219. "Apply FUNCTION to the start of all tables in the buffer."
  3220. (save-excursion
  3221. (save-restriction
  3222. (widen)
  3223. (goto-char (point-min))
  3224. (while (re-search-forward org-table-any-line-regexp nil t)
  3225. (unless quietly
  3226. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3227. (beginning-of-line 1)
  3228. (when (looking-at org-table-line-regexp)
  3229. (save-excursion (funcall function))
  3230. (or (looking-at org-table-line-regexp)
  3231. (forward-char 1)))
  3232. (re-search-forward org-table-any-border-regexp nil 1))))
  3233. (unless quietly (message "Mapping tables: done")))
  3234. ;; Declare and autoload functions from org-exp.el & Co
  3235. (declare-function org-default-export-plist "org-exp")
  3236. (declare-function org-infile-export-plist "org-exp")
  3237. (declare-function org-get-current-options "org-exp")
  3238. (eval-and-compile
  3239. (org-autoload "org-exp"
  3240. '(org-export org-export-visible
  3241. org-insert-export-options-template
  3242. org-table-clean-before-export))
  3243. (org-autoload "org-ascii"
  3244. '(org-export-as-ascii org-export-ascii-preprocess
  3245. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3246. org-export-region-as-ascii))
  3247. (org-autoload "org-latex"
  3248. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3249. org-replace-region-by-latex org-export-region-as-latex
  3250. org-export-as-latex org-export-as-pdf
  3251. org-export-as-pdf-and-open))
  3252. (org-autoload "org-html"
  3253. '(org-export-as-html-and-open
  3254. org-export-as-html-batch org-export-as-html-to-buffer
  3255. org-replace-region-by-html org-export-region-as-html
  3256. org-export-as-html))
  3257. (org-autoload "org-docbook"
  3258. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3259. org-replace-region-by-docbook org-export-region-as-docbook
  3260. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3261. org-export-as-docbook))
  3262. (org-autoload "org-icalendar"
  3263. '(org-export-icalendar-this-file
  3264. org-export-icalendar-all-agenda-files
  3265. org-export-icalendar-combine-agenda-files))
  3266. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3267. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3268. ;; Declare and autoload functions from org-agenda.el
  3269. (eval-and-compile
  3270. (org-autoload "org-agenda"
  3271. '(org-agenda org-agenda-list org-search-view
  3272. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3273. org-diary org-agenda-to-appt
  3274. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3275. ;; Autoload org-remember
  3276. (eval-and-compile
  3277. (org-autoload "org-remember"
  3278. '(org-remember-insinuate org-remember-annotation
  3279. org-remember-apply-template org-remember org-remember-handler)))
  3280. (eval-and-compile
  3281. (org-autoload "org-capture"
  3282. '(org-capture org-capture-insert-template-here
  3283. org-capture-import-remember-templates)))
  3284. ;; Autoload org-clock.el
  3285. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3286. (beg end))
  3287. (declare-function org-clock-update-mode-line "org-clock" ())
  3288. (declare-function org-resolve-clocks "org-clock"
  3289. (&optional also-non-dangling-p prompt last-valid))
  3290. (defvar org-clock-start-time)
  3291. (defvar org-clock-marker (make-marker)
  3292. "Marker recording the last clock-in.")
  3293. (defvar org-clock-hd-marker (make-marker)
  3294. "Marker recording the last clock-in, but the headline position.")
  3295. (defvar org-clock-heading ""
  3296. "The heading of the current clock entry.")
  3297. (defun org-clock-is-active ()
  3298. "Return non-nil if clock is currently running.
  3299. The return value is actually the clock marker."
  3300. (marker-buffer org-clock-marker))
  3301. (eval-and-compile
  3302. (org-autoload
  3303. "org-clock"
  3304. '(org-clock-in org-clock-out org-clock-cancel
  3305. org-clock-goto org-clock-sum org-clock-display
  3306. org-clock-remove-overlays org-clock-report
  3307. org-clocktable-shift org-dblock-write:clocktable
  3308. org-get-clocktable org-resolve-clocks)))
  3309. (defun org-clock-update-time-maybe ()
  3310. "If this is a CLOCK line, update it and return t.
  3311. Otherwise, return nil."
  3312. (interactive)
  3313. (save-excursion
  3314. (beginning-of-line 1)
  3315. (skip-chars-forward " \t")
  3316. (when (looking-at org-clock-string)
  3317. (let ((re (concat "[ \t]*" org-clock-string
  3318. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3319. "\\([ \t]*=>.*\\)?\\)?"))
  3320. ts te h m s neg)
  3321. (cond
  3322. ((not (looking-at re))
  3323. nil)
  3324. ((not (match-end 2))
  3325. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3326. (> org-clock-marker (point))
  3327. (<= org-clock-marker (point-at-eol)))
  3328. ;; The clock is running here
  3329. (setq org-clock-start-time
  3330. (apply 'encode-time
  3331. (org-parse-time-string (match-string 1))))
  3332. (org-clock-update-mode-line)))
  3333. (t
  3334. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3335. (end-of-line 1)
  3336. (setq ts (match-string 1)
  3337. te (match-string 3))
  3338. (setq s (- (org-float-time
  3339. (apply 'encode-time (org-parse-time-string te)))
  3340. (org-float-time
  3341. (apply 'encode-time (org-parse-time-string ts))))
  3342. neg (< s 0)
  3343. s (abs s)
  3344. h (floor (/ s 3600))
  3345. s (- s (* 3600 h))
  3346. m (floor (/ s 60))
  3347. s (- s (* 60 s)))
  3348. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3349. t))))))
  3350. (defun org-check-running-clock ()
  3351. "Check if the current buffer contains the running clock.
  3352. If yes, offer to stop it and to save the buffer with the changes."
  3353. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3354. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3355. (buffer-name))))
  3356. (org-clock-out)
  3357. (when (y-or-n-p "Save changed buffer?")
  3358. (save-buffer))))
  3359. (defun org-clocktable-try-shift (dir n)
  3360. "Check if this line starts a clock table, if yes, shift the time block."
  3361. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3362. (org-clocktable-shift dir n)))
  3363. ;; Autoload org-timer.el
  3364. (eval-and-compile
  3365. (org-autoload
  3366. "org-timer"
  3367. '(org-timer-start org-timer org-timer-item
  3368. org-timer-change-times-in-region
  3369. org-timer-set-timer
  3370. org-timer-reset-timers
  3371. org-timer-show-remaining-time)))
  3372. ;; Autoload org-feed.el
  3373. (eval-and-compile
  3374. (org-autoload
  3375. "org-feed"
  3376. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3377. ;; Autoload org-indent.el
  3378. ;; Define the variable already here, to make sure we have it.
  3379. (defvar org-indent-mode nil
  3380. "Non-nil if Org-Indent mode is enabled.
  3381. Use the command `org-indent-mode' to change this variable.")
  3382. (eval-and-compile
  3383. (org-autoload
  3384. "org-indent"
  3385. '(org-indent-mode)))
  3386. ;; Autoload org-mobile.el
  3387. (eval-and-compile
  3388. (org-autoload
  3389. "org-mobile"
  3390. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3391. ;; Autoload archiving code
  3392. ;; The stuff that is needed for cycling and tags has to be defined here.
  3393. (defgroup org-archive nil
  3394. "Options concerning archiving in Org-mode."
  3395. :tag "Org Archive"
  3396. :group 'org-structure)
  3397. (defcustom org-archive-location "%s_archive::"
  3398. "The location where subtrees should be archived.
  3399. The value of this variable is a string, consisting of two parts,
  3400. separated by a double-colon. The first part is a filename and
  3401. the second part is a headline.
  3402. When the filename is omitted, archiving happens in the same file.
  3403. %s in the filename will be replaced by the current file
  3404. name (without the directory part). Archiving to a different file
  3405. is useful to keep archived entries from contributing to the
  3406. Org-mode Agenda.
  3407. The archived entries will be filed as subtrees of the specified
  3408. headline. When the headline is omitted, the subtrees are simply
  3409. filed away at the end of the file, as top-level entries. Also in
  3410. the heading you can use %s to represent the file name, this can be
  3411. useful when using the same archive for a number of different files.
  3412. Here are a few examples:
  3413. \"%s_archive::\"
  3414. If the current file is Projects.org, archive in file
  3415. Projects.org_archive, as top-level trees. This is the default.
  3416. \"::* Archived Tasks\"
  3417. Archive in the current file, under the top-level headline
  3418. \"* Archived Tasks\".
  3419. \"~/org/archive.org::\"
  3420. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3421. \"~/org/archive.org::From %s\"
  3422. Archive in file ~/org/archive.org (absolute path), under headlines
  3423. \"From FILENAME\" where file name is the current file name.
  3424. \"basement::** Finished Tasks\"
  3425. Archive in file ./basement (relative path), as level 3 trees
  3426. below the level 2 heading \"** Finished Tasks\".
  3427. You may set this option on a per-file basis by adding to the buffer a
  3428. line like
  3429. #+ARCHIVE: basement::** Finished Tasks
  3430. You may also define it locally for a subtree by setting an ARCHIVE property
  3431. in the entry. If such a property is found in an entry, or anywhere up
  3432. the hierarchy, it will be used."
  3433. :group 'org-archive
  3434. :type 'string)
  3435. (defcustom org-archive-tag "ARCHIVE"
  3436. "The tag that marks a subtree as archived.
  3437. An archived subtree does not open during visibility cycling, and does
  3438. not contribute to the agenda listings.
  3439. After changing this, font-lock must be restarted in the relevant buffers to
  3440. get the proper fontification."
  3441. :group 'org-archive
  3442. :group 'org-keywords
  3443. :type 'string)
  3444. (defcustom org-agenda-skip-archived-trees t
  3445. "Non-nil means the agenda will skip any items located in archived trees.
  3446. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3447. variable is no longer recommended, you should leave it at the value t.
  3448. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3449. :group 'org-archive
  3450. :group 'org-agenda-skip
  3451. :type 'boolean)
  3452. (defcustom org-columns-skip-archived-trees t
  3453. "Non-nil means ignore archived trees when creating column view."
  3454. :group 'org-archive
  3455. :group 'org-properties
  3456. :type 'boolean)
  3457. (defcustom org-cycle-open-archived-trees nil
  3458. "Non-nil means `org-cycle' will open archived trees.
  3459. An archived tree is a tree marked with the tag ARCHIVE.
  3460. When nil, archived trees will stay folded. You can still open them with
  3461. normal outline commands like `show-all', but not with the cycling commands."
  3462. :group 'org-archive
  3463. :group 'org-cycle
  3464. :type 'boolean)
  3465. (defcustom org-sparse-tree-open-archived-trees nil
  3466. "Non-nil means sparse tree construction shows matches in archived trees.
  3467. When nil, matches in these trees are highlighted, but the trees are kept in
  3468. collapsed state."
  3469. :group 'org-archive
  3470. :group 'org-sparse-trees
  3471. :type 'boolean)
  3472. (defun org-cycle-hide-archived-subtrees (state)
  3473. "Re-hide all archived subtrees after a visibility state change."
  3474. (when (and (not org-cycle-open-archived-trees)
  3475. (not (memq state '(overview folded))))
  3476. (save-excursion
  3477. (let* ((globalp (memq state '(contents all)))
  3478. (beg (if globalp (point-min) (point)))
  3479. (end (if globalp (point-max) (org-end-of-subtree t))))
  3480. (org-hide-archived-subtrees beg end)
  3481. (goto-char beg)
  3482. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3483. (message "%s" (substitute-command-keys
  3484. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3485. (defun org-force-cycle-archived ()
  3486. "Cycle subtree even if it is archived."
  3487. (interactive)
  3488. (setq this-command 'org-cycle)
  3489. (let ((org-cycle-open-archived-trees t))
  3490. (call-interactively 'org-cycle)))
  3491. (defun org-hide-archived-subtrees (beg end)
  3492. "Re-hide all archived subtrees after a visibility state change."
  3493. (save-excursion
  3494. (let* ((re (concat ":" org-archive-tag ":")))
  3495. (goto-char beg)
  3496. (while (re-search-forward re end t)
  3497. (when (org-on-heading-p)
  3498. (org-flag-subtree t)
  3499. (org-end-of-subtree t))))))
  3500. (defun org-flag-subtree (flag)
  3501. (save-excursion
  3502. (org-back-to-heading t)
  3503. (outline-end-of-heading)
  3504. (outline-flag-region (point)
  3505. (progn (org-end-of-subtree t) (point))
  3506. flag)))
  3507. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3508. (eval-and-compile
  3509. (org-autoload "org-archive"
  3510. '(org-add-archive-files org-archive-subtree
  3511. org-archive-to-archive-sibling org-toggle-archive-tag
  3512. org-archive-subtree-default
  3513. org-archive-subtree-default-with-confirmation)))
  3514. ;; Autoload Column View Code
  3515. (declare-function org-columns-number-to-string "org-colview")
  3516. (declare-function org-columns-get-format-and-top-level "org-colview")
  3517. (declare-function org-columns-compute "org-colview")
  3518. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3519. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3520. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3521. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3522. ;; Autoload ID code
  3523. (declare-function org-id-store-link "org-id")
  3524. (declare-function org-id-locations-load "org-id")
  3525. (declare-function org-id-locations-save "org-id")
  3526. (defvar org-id-track-globally)
  3527. (org-autoload "org-id"
  3528. '(org-id-get-create org-id-new org-id-copy org-id-get
  3529. org-id-get-with-outline-path-completion
  3530. org-id-get-with-outline-drilling org-id-store-link
  3531. org-id-goto org-id-find org-id-store-link))
  3532. ;; Autoload Plotting Code
  3533. (org-autoload "org-plot"
  3534. '(org-plot/gnuplot))
  3535. ;;; Variables for pre-computed regular expressions, all buffer local
  3536. (defvar org-drawer-regexp nil
  3537. "Matches first line of a hidden block.")
  3538. (make-variable-buffer-local 'org-drawer-regexp)
  3539. (defvar org-todo-regexp nil
  3540. "Matches any of the TODO state keywords.")
  3541. (make-variable-buffer-local 'org-todo-regexp)
  3542. (defvar org-not-done-regexp nil
  3543. "Matches any of the TODO state keywords except the last one.")
  3544. (make-variable-buffer-local 'org-not-done-regexp)
  3545. (defvar org-not-done-heading-regexp nil
  3546. "Matches a TODO headline that is not done.")
  3547. (make-variable-buffer-local 'org-not-done-regexp)
  3548. (defvar org-todo-line-regexp nil
  3549. "Matches a headline and puts TODO state into group 2 if present.")
  3550. (make-variable-buffer-local 'org-todo-line-regexp)
  3551. (defvar org-complex-heading-regexp nil
  3552. "Matches a headline and puts everything into groups:
  3553. group 1: the stars
  3554. group 2: The todo keyword, maybe
  3555. group 3: Priority cookie
  3556. group 4: True headline
  3557. group 5: Tags")
  3558. (make-variable-buffer-local 'org-complex-heading-regexp)
  3559. (defvar org-complex-heading-regexp-format nil
  3560. "Printf format to make regexp to match an exact headline.
  3561. This regexp will match the headline of any node which hase the exact
  3562. headline text that is put into the format, but may have any TODO state,
  3563. priority and tags.")
  3564. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3565. (defvar org-todo-line-tags-regexp nil
  3566. "Matches a headline and puts TODO state into group 2 if present.
  3567. Also put tags into group 4 if tags are present.")
  3568. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3569. (defvar org-nl-done-regexp nil
  3570. "Matches newline followed by a headline with the DONE keyword.")
  3571. (make-variable-buffer-local 'org-nl-done-regexp)
  3572. (defvar org-looking-at-done-regexp nil
  3573. "Matches the DONE keyword a point.")
  3574. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3575. (defvar org-ds-keyword-length 12
  3576. "Maximum length of the Deadline and SCHEDULED keywords.")
  3577. (make-variable-buffer-local 'org-ds-keyword-length)
  3578. (defvar org-deadline-regexp nil
  3579. "Matches the DEADLINE keyword.")
  3580. (make-variable-buffer-local 'org-deadline-regexp)
  3581. (defvar org-deadline-time-regexp nil
  3582. "Matches the DEADLINE keyword together with a time stamp.")
  3583. (make-variable-buffer-local 'org-deadline-time-regexp)
  3584. (defvar org-deadline-line-regexp nil
  3585. "Matches the DEADLINE keyword and the rest of the line.")
  3586. (make-variable-buffer-local 'org-deadline-line-regexp)
  3587. (defvar org-scheduled-regexp nil
  3588. "Matches the SCHEDULED keyword.")
  3589. (make-variable-buffer-local 'org-scheduled-regexp)
  3590. (defvar org-scheduled-time-regexp nil
  3591. "Matches the SCHEDULED keyword together with a time stamp.")
  3592. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3593. (defvar org-closed-time-regexp nil
  3594. "Matches the CLOSED keyword together with a time stamp.")
  3595. (make-variable-buffer-local 'org-closed-time-regexp)
  3596. (defvar org-keyword-time-regexp nil
  3597. "Matches any of the 4 keywords, together with the time stamp.")
  3598. (make-variable-buffer-local 'org-keyword-time-regexp)
  3599. (defvar org-keyword-time-not-clock-regexp nil
  3600. "Matches any of the 3 keywords, together with the time stamp.")
  3601. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3602. (defvar org-maybe-keyword-time-regexp nil
  3603. "Matches a timestamp, possibly preceded by a keyword.")
  3604. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3605. (defvar org-planning-or-clock-line-re nil
  3606. "Matches a line with planning or clock info.")
  3607. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3608. (defvar org-all-time-keywords nil
  3609. "List of time keywords.")
  3610. (make-variable-buffer-local 'org-all-time-keywords)
  3611. (defconst org-plain-time-of-day-regexp
  3612. (concat
  3613. "\\(\\<[012]?[0-9]"
  3614. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3615. "\\(--?"
  3616. "\\(\\<[012]?[0-9]"
  3617. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3618. "\\)?")
  3619. "Regular expression to match a plain time or time range.
  3620. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3621. groups carry important information:
  3622. 0 the full match
  3623. 1 the first time, range or not
  3624. 8 the second time, if it is a range.")
  3625. (defconst org-plain-time-extension-regexp
  3626. (concat
  3627. "\\(\\<[012]?[0-9]"
  3628. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3629. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3630. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3631. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3632. groups carry important information:
  3633. 0 the full match
  3634. 7 hours of duration
  3635. 9 minutes of duration")
  3636. (defconst org-stamp-time-of-day-regexp
  3637. (concat
  3638. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3639. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3640. "\\(--?"
  3641. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3642. "Regular expression to match a timestamp time or time range.
  3643. After a match, the following groups carry important information:
  3644. 0 the full match
  3645. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3646. 2 the first time, range or not
  3647. 4 the second time, if it is a range.")
  3648. (defconst org-startup-options
  3649. '(("fold" org-startup-folded t)
  3650. ("overview" org-startup-folded t)
  3651. ("nofold" org-startup-folded nil)
  3652. ("showall" org-startup-folded nil)
  3653. ("showeverything" org-startup-folded showeverything)
  3654. ("content" org-startup-folded content)
  3655. ("indent" org-startup-indented t)
  3656. ("noindent" org-startup-indented nil)
  3657. ("hidestars" org-hide-leading-stars t)
  3658. ("showstars" org-hide-leading-stars nil)
  3659. ("odd" org-odd-levels-only t)
  3660. ("oddeven" org-odd-levels-only nil)
  3661. ("align" org-startup-align-all-tables t)
  3662. ("noalign" org-startup-align-all-tables nil)
  3663. ("inlineimages" org-startup-with-inline-images t)
  3664. ("noinlineimages" org-startup-with-inline-images nil)
  3665. ("customtime" org-display-custom-times t)
  3666. ("logdone" org-log-done time)
  3667. ("lognotedone" org-log-done note)
  3668. ("nologdone" org-log-done nil)
  3669. ("lognoteclock-out" org-log-note-clock-out t)
  3670. ("nolognoteclock-out" org-log-note-clock-out nil)
  3671. ("logrepeat" org-log-repeat state)
  3672. ("lognoterepeat" org-log-repeat note)
  3673. ("nologrepeat" org-log-repeat nil)
  3674. ("logreschedule" org-log-reschedule time)
  3675. ("lognotereschedule" org-log-reschedule note)
  3676. ("nologreschedule" org-log-reschedule nil)
  3677. ("logredeadline" org-log-redeadline time)
  3678. ("lognoteredeadline" org-log-redeadline note)
  3679. ("nologredeadline" org-log-redeadline nil)
  3680. ("logrefile" org-log-refile time)
  3681. ("lognoterefile" org-log-refile note)
  3682. ("nologrefile" org-log-refile nil)
  3683. ("fninline" org-footnote-define-inline t)
  3684. ("nofninline" org-footnote-define-inline nil)
  3685. ("fnlocal" org-footnote-section nil)
  3686. ("fnauto" org-footnote-auto-label t)
  3687. ("fnprompt" org-footnote-auto-label nil)
  3688. ("fnconfirm" org-footnote-auto-label confirm)
  3689. ("fnplain" org-footnote-auto-label plain)
  3690. ("fnadjust" org-footnote-auto-adjust t)
  3691. ("nofnadjust" org-footnote-auto-adjust nil)
  3692. ("constcgs" constants-unit-system cgs)
  3693. ("constSI" constants-unit-system SI)
  3694. ("noptag" org-tag-persistent-alist nil)
  3695. ("hideblocks" org-hide-block-startup t)
  3696. ("nohideblocks" org-hide-block-startup nil)
  3697. ("beamer" org-startup-with-beamer-mode t)
  3698. ("entitiespretty" org-pretty-entities t)
  3699. ("entitiesplain" org-pretty-entities nil))
  3700. "Variable associated with STARTUP options for org-mode.
  3701. Each element is a list of three items: The startup options as written
  3702. in the #+STARTUP line, the corresponding variable, and the value to
  3703. set this variable to if the option is found. An optional forth element PUSH
  3704. means to push this value onto the list in the variable.")
  3705. (defun org-set-regexps-and-options ()
  3706. "Precompute regular expressions for current buffer."
  3707. (when (org-mode-p)
  3708. (org-set-local 'org-todo-kwd-alist nil)
  3709. (org-set-local 'org-todo-key-alist nil)
  3710. (org-set-local 'org-todo-key-trigger nil)
  3711. (org-set-local 'org-todo-keywords-1 nil)
  3712. (org-set-local 'org-done-keywords nil)
  3713. (org-set-local 'org-todo-heads nil)
  3714. (org-set-local 'org-todo-sets nil)
  3715. (org-set-local 'org-todo-log-states nil)
  3716. (org-set-local 'org-file-properties nil)
  3717. (org-set-local 'org-file-tags nil)
  3718. (let ((re (org-make-options-regexp
  3719. '("CATEGORY" "TODO" "COLUMNS"
  3720. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3721. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3722. "OPTIONS")
  3723. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3724. (splitre "[ \t]+")
  3725. (scripts org-use-sub-superscripts)
  3726. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3727. tail sep kws1 prio props ftags drawers beamer-p
  3728. ext-setup-or-nil setup-contents (start 0))
  3729. (save-excursion
  3730. (save-restriction
  3731. (widen)
  3732. (goto-char (point-min))
  3733. (while (or (and ext-setup-or-nil
  3734. (string-match re ext-setup-or-nil start)
  3735. (setq start (match-end 0)))
  3736. (and (setq ext-setup-or-nil nil start 0)
  3737. (re-search-forward re nil t)))
  3738. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3739. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3740. (if (stringp value) (setq value (org-trim value)))
  3741. (cond
  3742. ((equal key "CATEGORY")
  3743. (setq cat value))
  3744. ((member key '("SEQ_TODO" "TODO"))
  3745. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3746. ((equal key "TYP_TODO")
  3747. (push (cons 'type (org-split-string value splitre)) kwds))
  3748. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3749. ;; general TODO-like setup
  3750. (push (cons (intern (downcase (match-string 1 key)))
  3751. (org-split-string value splitre)) kwds))
  3752. ((equal key "TAGS")
  3753. (setq tags (append tags (if tags '("\\n") nil)
  3754. (org-split-string value splitre))))
  3755. ((equal key "COLUMNS")
  3756. (org-set-local 'org-columns-default-format value))
  3757. ((equal key "LINK")
  3758. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3759. (push (cons (match-string 1 value)
  3760. (org-trim (match-string 2 value)))
  3761. links)))
  3762. ((equal key "PRIORITIES")
  3763. (setq prio (org-split-string value " +")))
  3764. ((equal key "PROPERTY")
  3765. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3766. (push (cons (match-string 1 value) (match-string 2 value))
  3767. props)))
  3768. ((equal key "FILETAGS")
  3769. (when (string-match "\\S-" value)
  3770. (setq ftags
  3771. (append
  3772. ftags
  3773. (apply 'append
  3774. (mapcar (lambda (x) (org-split-string x ":"))
  3775. (org-split-string value)))))))
  3776. ((equal key "DRAWERS")
  3777. (setq drawers (org-split-string value splitre)))
  3778. ((equal key "CONSTANTS")
  3779. (setq const (append const (org-split-string value splitre))))
  3780. ((equal key "STARTUP")
  3781. (let ((opts (org-split-string value splitre))
  3782. l var val)
  3783. (while (setq l (pop opts))
  3784. (when (setq l (assoc l org-startup-options))
  3785. (setq var (nth 1 l) val (nth 2 l))
  3786. (if (not (nth 3 l))
  3787. (set (make-local-variable var) val)
  3788. (if (not (listp (symbol-value var)))
  3789. (set (make-local-variable var) nil))
  3790. (set (make-local-variable var) (symbol-value var))
  3791. (add-to-list var val))))))
  3792. ((equal key "ARCHIVE")
  3793. (setq arch value)
  3794. (remove-text-properties 0 (length arch)
  3795. '(face t fontified t) arch))
  3796. ((equal key "LATEX_CLASS")
  3797. (setq beamer-p (equal value "beamer")))
  3798. ((equal key "OPTIONS")
  3799. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3800. (setq scripts (read (match-string 2 value)))))
  3801. ((equal key "SETUPFILE")
  3802. (setq setup-contents (org-file-contents
  3803. (expand-file-name
  3804. (org-remove-double-quotes value))
  3805. 'noerror))
  3806. (if (not ext-setup-or-nil)
  3807. (setq ext-setup-or-nil setup-contents start 0)
  3808. (setq ext-setup-or-nil
  3809. (concat (substring ext-setup-or-nil 0 start)
  3810. "\n" setup-contents "\n"
  3811. (substring ext-setup-or-nil start)))))
  3812. ))))
  3813. (org-set-local 'org-use-sub-superscripts scripts)
  3814. (when cat
  3815. (org-set-local 'org-category (intern cat))
  3816. (push (cons "CATEGORY" cat) props))
  3817. (when prio
  3818. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3819. (setq prio (mapcar 'string-to-char prio))
  3820. (org-set-local 'org-highest-priority (nth 0 prio))
  3821. (org-set-local 'org-lowest-priority (nth 1 prio))
  3822. (org-set-local 'org-default-priority (nth 2 prio)))
  3823. (and props (org-set-local 'org-file-properties (nreverse props)))
  3824. (and ftags (org-set-local 'org-file-tags
  3825. (mapcar 'org-add-prop-inherited ftags)))
  3826. (and drawers (org-set-local 'org-drawers drawers))
  3827. (and arch (org-set-local 'org-archive-location arch))
  3828. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3829. ;; Process the TODO keywords
  3830. (unless kwds
  3831. ;; Use the global values as if they had been given locally.
  3832. (setq kwds (default-value 'org-todo-keywords))
  3833. (if (stringp (car kwds))
  3834. (setq kwds (list (cons org-todo-interpretation
  3835. (default-value 'org-todo-keywords)))))
  3836. (setq kwds (reverse kwds)))
  3837. (setq kwds (nreverse kwds))
  3838. (let (inter kws kw)
  3839. (while (setq kws (pop kwds))
  3840. (let ((kws (or
  3841. (run-hook-with-args-until-success
  3842. 'org-todo-setup-filter-hook kws)
  3843. kws)))
  3844. (setq inter (pop kws) sep (member "|" kws)
  3845. kws0 (delete "|" (copy-sequence kws))
  3846. kwsa nil
  3847. kws1 (mapcar
  3848. (lambda (x)
  3849. ;; 1 2
  3850. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3851. (progn
  3852. (setq kw (match-string 1 x)
  3853. key (and (match-end 2) (match-string 2 x))
  3854. log (org-extract-log-state-settings x))
  3855. (push (cons kw (and key (string-to-char key))) kwsa)
  3856. (and log (push log org-todo-log-states))
  3857. kw)
  3858. (error "Invalid TODO keyword %s" x)))
  3859. kws0)
  3860. kwsa (if kwsa (append '((:startgroup))
  3861. (nreverse kwsa)
  3862. '((:endgroup))))
  3863. hw (car kws1)
  3864. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3865. tail (list inter hw (car dws) (org-last dws))))
  3866. (add-to-list 'org-todo-heads hw 'append)
  3867. (push kws1 org-todo-sets)
  3868. (setq org-done-keywords (append org-done-keywords dws nil))
  3869. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3870. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3871. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3872. (setq org-todo-sets (nreverse org-todo-sets)
  3873. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3874. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3875. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3876. ;; Process the constants
  3877. (when const
  3878. (let (e cst)
  3879. (while (setq e (pop const))
  3880. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3881. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3882. (setq org-table-formula-constants-local cst)))
  3883. ;; Process the tags.
  3884. (when tags
  3885. (let (e tgs)
  3886. (while (setq e (pop tags))
  3887. (cond
  3888. ((equal e "{") (push '(:startgroup) tgs))
  3889. ((equal e "}") (push '(:endgroup) tgs))
  3890. ((equal e "\\n") (push '(:newline) tgs))
  3891. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3892. (push (cons (match-string 1 e)
  3893. (string-to-char (match-string 2 e)))
  3894. tgs))
  3895. (t (push (list e) tgs))))
  3896. (org-set-local 'org-tag-alist nil)
  3897. (while (setq e (pop tgs))
  3898. (or (and (stringp (car e))
  3899. (assoc (car e) org-tag-alist))
  3900. (push e org-tag-alist)))))
  3901. ;; Compute the regular expressions and other local variables
  3902. (if (not org-done-keywords)
  3903. (setq org-done-keywords (and org-todo-keywords-1
  3904. (list (org-last org-todo-keywords-1)))))
  3905. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3906. (length org-scheduled-string)
  3907. (length org-clock-string)
  3908. (length org-closed-string)))
  3909. org-drawer-regexp
  3910. (concat "^[ \t]*:\\("
  3911. (mapconcat 'regexp-quote org-drawers "\\|")
  3912. "\\):[ \t]*$")
  3913. org-not-done-keywords
  3914. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3915. org-todo-regexp
  3916. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3917. "\\|") "\\)\\>")
  3918. org-not-done-regexp
  3919. (concat "\\<\\("
  3920. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3921. "\\)\\>")
  3922. org-not-done-heading-regexp
  3923. (concat "^\\(\\*+\\)[ \t]+\\("
  3924. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3925. "\\)\\>")
  3926. org-todo-line-regexp
  3927. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3928. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3929. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3930. org-complex-heading-regexp
  3931. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3932. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3933. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3934. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  3935. org-complex-heading-regexp-format
  3936. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3937. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3938. "\\)\\>\\)?"
  3939. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3940. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3941. "[ \t]*\\(%s\\)"
  3942. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3943. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  3944. org-nl-done-regexp
  3945. (concat "\n\\*+[ \t]+"
  3946. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3947. "\\)" "\\>")
  3948. org-todo-line-tags-regexp
  3949. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3950. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3951. (org-re
  3952. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  3953. org-looking-at-done-regexp
  3954. (concat "^" "\\(?:"
  3955. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3956. "\\>")
  3957. org-deadline-regexp (concat "\\<" org-deadline-string)
  3958. org-deadline-time-regexp
  3959. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3960. org-deadline-line-regexp
  3961. (concat "\\<\\(" org-deadline-string "\\).*")
  3962. org-scheduled-regexp
  3963. (concat "\\<" org-scheduled-string)
  3964. org-scheduled-time-regexp
  3965. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3966. org-closed-time-regexp
  3967. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3968. org-keyword-time-regexp
  3969. (concat "\\<\\(" org-scheduled-string
  3970. "\\|" org-deadline-string
  3971. "\\|" org-closed-string
  3972. "\\|" org-clock-string "\\)"
  3973. " *[[<]\\([^]>]+\\)[]>]")
  3974. org-keyword-time-not-clock-regexp
  3975. (concat "\\<\\(" org-scheduled-string
  3976. "\\|" org-deadline-string
  3977. "\\|" org-closed-string
  3978. "\\)"
  3979. " *[[<]\\([^]>]+\\)[]>]")
  3980. org-maybe-keyword-time-regexp
  3981. (concat "\\(\\<\\(" org-scheduled-string
  3982. "\\|" org-deadline-string
  3983. "\\|" org-closed-string
  3984. "\\|" org-clock-string "\\)\\)?"
  3985. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3986. org-planning-or-clock-line-re
  3987. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3988. "\\|" org-deadline-string
  3989. "\\|" org-closed-string "\\|" org-clock-string
  3990. "\\)\\>\\)")
  3991. org-all-time-keywords
  3992. (mapcar (lambda (w) (substring w 0 -1))
  3993. (list org-scheduled-string org-deadline-string
  3994. org-clock-string org-closed-string))
  3995. )
  3996. (org-compute-latex-and-specials-regexp)
  3997. (org-set-font-lock-defaults))))
  3998. (defun org-file-contents (file &optional noerror)
  3999. "Return the contents of FILE, as a string."
  4000. (if (or (not file)
  4001. (not (file-readable-p file)))
  4002. (if noerror
  4003. (progn
  4004. (message "Cannot read file \"%s\"" file)
  4005. (ding) (sit-for 2)
  4006. "")
  4007. (error "Cannot read file \"%s\"" file))
  4008. (with-temp-buffer
  4009. (insert-file-contents file)
  4010. (buffer-string))))
  4011. (defun org-extract-log-state-settings (x)
  4012. "Extract the log state setting from a TODO keyword string.
  4013. This will extract info from a string like \"WAIT(w@/!)\"."
  4014. (let (kw key log1 log2)
  4015. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4016. (setq kw (match-string 1 x)
  4017. key (and (match-end 2) (match-string 2 x))
  4018. log1 (and (match-end 3) (match-string 3 x))
  4019. log2 (and (match-end 4) (match-string 4 x)))
  4020. (and (or log1 log2)
  4021. (list kw
  4022. (and log1 (if (equal log1 "!") 'time 'note))
  4023. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4024. (defun org-remove-keyword-keys (list)
  4025. "Remove a pair of parenthesis at the end of each string in LIST."
  4026. (mapcar (lambda (x)
  4027. (if (string-match "(.*)$" x)
  4028. (substring x 0 (match-beginning 0))
  4029. x))
  4030. list))
  4031. (defun org-assign-fast-keys (alist)
  4032. "Assign fast keys to a keyword-key alist.
  4033. Respect keys that are already there."
  4034. (let (new e (alt ?0))
  4035. (while (setq e (pop alist))
  4036. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4037. (cdr e)) ;; Key already assigned.
  4038. (push e new)
  4039. (let ((clist (string-to-list (downcase (car e))))
  4040. (used (append new alist)))
  4041. (when (= (car clist) ?@)
  4042. (pop clist))
  4043. (while (and clist (rassoc (car clist) used))
  4044. (pop clist))
  4045. (unless clist
  4046. (while (rassoc alt used)
  4047. (incf alt)))
  4048. (push (cons (car e) (or (car clist) alt)) new))))
  4049. (nreverse new)))
  4050. ;;; Some variables used in various places
  4051. (defvar org-window-configuration nil
  4052. "Used in various places to store a window configuration.")
  4053. (defvar org-selected-window nil
  4054. "Used in various places to store a window configuration.")
  4055. (defvar org-finish-function nil
  4056. "Function to be called when `C-c C-c' is used.
  4057. This is for getting out of special buffers like remember.")
  4058. ;; FIXME: Occasionally check by commenting these, to make sure
  4059. ;; no other functions uses these, forgetting to let-bind them.
  4060. (defvar entry)
  4061. (defvar last-state)
  4062. (defvar date)
  4063. ;; Defined somewhere in this file, but used before definition.
  4064. (defvar org-entities) ;; defined in org-entities.el
  4065. (defvar org-struct-menu)
  4066. (defvar org-org-menu)
  4067. (defvar org-tbl-menu)
  4068. ;;;; Define the Org-mode
  4069. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4070. (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"))
  4071. ;; We use a before-change function to check if a table might need
  4072. ;; an update.
  4073. (defvar org-table-may-need-update t
  4074. "Indicates that a table might need an update.
  4075. This variable is set by `org-before-change-function'.
  4076. `org-table-align' sets it back to nil.")
  4077. (defun org-before-change-function (beg end)
  4078. "Every change indicates that a table might need an update."
  4079. (setq org-table-may-need-update t))
  4080. (defvar org-mode-map)
  4081. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4082. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4083. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4084. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4085. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4086. (defvar org-table-buffer-is-an nil)
  4087. (defconst org-outline-regexp "\\*+ ")
  4088. ;;;###autoload
  4089. (define-derived-mode org-mode outline-mode "Org"
  4090. "Outline-based notes management and organizer, alias
  4091. \"Carsten's outline-mode for keeping track of everything.\"
  4092. Org-mode develops organizational tasks around a NOTES file which
  4093. contains information about projects as plain text. Org-mode is
  4094. implemented on top of outline-mode, which is ideal to keep the content
  4095. of large files well structured. It supports ToDo items, deadlines and
  4096. time stamps, which magically appear in the diary listing of the Emacs
  4097. calendar. Tables are easily created with a built-in table editor.
  4098. Plain text URL-like links connect to websites, emails (VM), Usenet
  4099. messages (Gnus), BBDB entries, and any files related to the project.
  4100. For printing and sharing of notes, an Org-mode file (or a part of it)
  4101. can be exported as a structured ASCII or HTML file.
  4102. The following commands are available:
  4103. \\{org-mode-map}"
  4104. ;; Get rid of Outline menus, they are not needed
  4105. ;; Need to do this here because define-derived-mode sets up
  4106. ;; the keymap so late. Still, it is a waste to call this each time
  4107. ;; we switch another buffer into org-mode.
  4108. (if (featurep 'xemacs)
  4109. (when (boundp 'outline-mode-menu-heading)
  4110. ;; Assume this is Greg's port, it uses easymenu
  4111. (easy-menu-remove outline-mode-menu-heading)
  4112. (easy-menu-remove outline-mode-menu-show)
  4113. (easy-menu-remove outline-mode-menu-hide))
  4114. (define-key org-mode-map [menu-bar headings] 'undefined)
  4115. (define-key org-mode-map [menu-bar hide] 'undefined)
  4116. (define-key org-mode-map [menu-bar show] 'undefined))
  4117. (org-load-modules-maybe)
  4118. (easy-menu-add org-org-menu)
  4119. (easy-menu-add org-tbl-menu)
  4120. (org-install-agenda-files-menu)
  4121. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4122. (add-to-invisibility-spec '(org-cwidth))
  4123. (add-to-invisibility-spec '(org-hide-block . t))
  4124. (when (featurep 'xemacs)
  4125. (org-set-local 'line-move-ignore-invisible t))
  4126. (org-set-local 'outline-regexp org-outline-regexp)
  4127. (org-set-local 'outline-level 'org-outline-level)
  4128. (when (and org-ellipsis
  4129. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4130. (fboundp 'make-glyph-code))
  4131. (unless org-display-table
  4132. (setq org-display-table (make-display-table)))
  4133. (set-display-table-slot
  4134. org-display-table 4
  4135. (vconcat (mapcar
  4136. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4137. org-ellipsis)))
  4138. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4139. (setq buffer-display-table org-display-table))
  4140. (org-set-regexps-and-options)
  4141. (when (and org-tag-faces (not org-tags-special-faces-re))
  4142. ;; tag faces set outside customize.... force initialization.
  4143. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4144. ;; Calc embedded
  4145. (org-set-local 'calc-embedded-open-mode "# ")
  4146. (modify-syntax-entry ?@ "w")
  4147. (if org-startup-truncated (setq truncate-lines t))
  4148. (org-set-local 'font-lock-unfontify-region-function
  4149. 'org-unfontify-region)
  4150. ;; Activate before-change-function
  4151. (org-set-local 'org-table-may-need-update t)
  4152. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4153. 'local)
  4154. ;; Check for running clock before killing a buffer
  4155. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4156. ;; Paragraphs and auto-filling
  4157. (org-set-autofill-regexps)
  4158. (setq indent-line-function 'org-indent-line-function)
  4159. (org-update-radio-target-regexp)
  4160. ;; Beginning/end of defun
  4161. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4162. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4163. ;; Make sure dependence stuff works reliably, even for users who set it
  4164. ;; too late :-(
  4165. (if org-enforce-todo-dependencies
  4166. (add-hook 'org-blocker-hook
  4167. 'org-block-todo-from-children-or-siblings-or-parent)
  4168. (remove-hook 'org-blocker-hook
  4169. 'org-block-todo-from-children-or-siblings-or-parent))
  4170. (if org-enforce-todo-checkbox-dependencies
  4171. (add-hook 'org-blocker-hook
  4172. 'org-block-todo-from-checkboxes)
  4173. (remove-hook 'org-blocker-hook
  4174. 'org-block-todo-from-checkboxes))
  4175. ;; Comment characters
  4176. (org-set-local 'comment-start "#")
  4177. (org-set-local 'comment-padding " ")
  4178. ;; Align options lines
  4179. (org-set-local
  4180. 'align-mode-rules-list
  4181. '((org-in-buffer-settings
  4182. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4183. (modes . '(org-mode)))))
  4184. ;; Imenu
  4185. (org-set-local 'imenu-create-index-function
  4186. 'org-imenu-get-tree)
  4187. ;; Make isearch reveal context
  4188. (if (or (featurep 'xemacs)
  4189. (not (boundp 'outline-isearch-open-invisible-function)))
  4190. ;; Emacs 21 and XEmacs make use of the hook
  4191. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4192. ;; Emacs 22 deals with this through a special variable
  4193. (org-set-local 'outline-isearch-open-invisible-function
  4194. (lambda (&rest ignore) (org-show-context 'isearch))))
  4195. ;; Turn on org-beamer-mode?
  4196. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4197. ;; Setup the pcomplete hooks
  4198. (set (make-local-variable 'pcomplete-command-completion-function)
  4199. 'org-complete-initial)
  4200. (set (make-local-variable 'pcomplete-command-name-function)
  4201. 'org-command-at-point)
  4202. (set (make-local-variable 'pcomplete-default-completion-function)
  4203. 'ignore)
  4204. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4205. 'org-parse-arguments)
  4206. (set (make-local-variable 'pcomplete-termination-string) "")
  4207. ;; If empty file that did not turn on org-mode automatically, make it to.
  4208. (if (and org-insert-mode-line-in-empty-file
  4209. (interactive-p)
  4210. (= (point-min) (point-max)))
  4211. (insert "# -*- mode: org -*-\n\n"))
  4212. (unless org-inhibit-startup
  4213. (when org-startup-align-all-tables
  4214. (let ((bmp (buffer-modified-p)))
  4215. (org-table-map-tables 'org-table-align 'quietly)
  4216. (set-buffer-modified-p bmp)))
  4217. (when org-startup-with-inline-images
  4218. (org-display-inline-images))
  4219. (when org-startup-indented
  4220. (require 'org-indent)
  4221. (org-indent-mode 1))
  4222. (unless org-inhibit-startup-visibility-stuff
  4223. (org-set-startup-visibility))))
  4224. (when (fboundp 'abbrev-table-put)
  4225. (abbrev-table-put org-mode-abbrev-table
  4226. :parents (list text-mode-abbrev-table)))
  4227. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4228. (defun org-current-time ()
  4229. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4230. (if (> (car org-time-stamp-rounding-minutes) 1)
  4231. (let ((r (car org-time-stamp-rounding-minutes))
  4232. (time (decode-time)))
  4233. (apply 'encode-time
  4234. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4235. (nthcdr 2 time))))
  4236. (current-time)))
  4237. ;;;; Font-Lock stuff, including the activators
  4238. (defvar org-mouse-map (make-sparse-keymap))
  4239. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4240. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4241. (when org-mouse-1-follows-link
  4242. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4243. (when org-tab-follows-link
  4244. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4245. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4246. (require 'font-lock)
  4247. (defconst org-non-link-chars "]\t\n\r<>")
  4248. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4249. "shell" "elisp" "doi" "message"))
  4250. (defvar org-link-types-re nil
  4251. "Matches a link that has a url-like prefix like \"http:\"")
  4252. (defvar org-link-re-with-space nil
  4253. "Matches a link with spaces, optional angular brackets around it.")
  4254. (defvar org-link-re-with-space2 nil
  4255. "Matches a link with spaces, optional angular brackets around it.")
  4256. (defvar org-link-re-with-space3 nil
  4257. "Matches a link with spaces, only for internal part in bracket links.")
  4258. (defvar org-angle-link-re nil
  4259. "Matches link with angular brackets, spaces are allowed.")
  4260. (defvar org-plain-link-re nil
  4261. "Matches plain link, without spaces.")
  4262. (defvar org-bracket-link-regexp nil
  4263. "Matches a link in double brackets.")
  4264. (defvar org-bracket-link-analytic-regexp nil
  4265. "Regular expression used to analyze links.
  4266. Here is what the match groups contain after a match:
  4267. 1: http:
  4268. 2: http
  4269. 3: path
  4270. 4: [desc]
  4271. 5: desc")
  4272. (defvar org-bracket-link-analytic-regexp++ nil
  4273. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4274. (defvar org-any-link-re nil
  4275. "Regular expression matching any link.")
  4276. (defcustom org-match-sexp-depth 3
  4277. "Number of stacked braces for sub/superscript matching.
  4278. This has to be set before loading org.el to be effective."
  4279. :group 'org-export-translation ; ??????????????????????????/
  4280. :type 'integer)
  4281. (defun org-create-multibrace-regexp (left right n)
  4282. "Create a regular expression which will match a balanced sexp.
  4283. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4284. as single character strings.
  4285. The regexp returned will match the entire expression including the
  4286. delimiters. It will also define a single group which contains the
  4287. match except for the outermost delimiters. The maximum depth of
  4288. stacked delimiters is N. Escaping delimiters is not possible."
  4289. (let* ((nothing (concat "[^" left right "]*?"))
  4290. (or "\\|")
  4291. (re nothing)
  4292. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4293. (while (> n 1)
  4294. (setq n (1- n)
  4295. re (concat re or next)
  4296. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4297. (concat left "\\(" re "\\)" right)))
  4298. (defvar org-match-substring-regexp
  4299. (concat
  4300. "\\([^\\]\\)\\([_^]\\)\\("
  4301. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4302. "\\|"
  4303. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4304. "\\|"
  4305. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4306. "The regular expression matching a sub- or superscript.")
  4307. (defvar org-match-substring-with-braces-regexp
  4308. (concat
  4309. "\\([^\\]\\)\\([_^]\\)\\("
  4310. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4311. "\\)")
  4312. "The regular expression matching a sub- or superscript, forcing braces.")
  4313. (defun org-make-link-regexps ()
  4314. "Update the link regular expressions.
  4315. This should be called after the variable `org-link-types' has changed."
  4316. (setq org-link-types-re
  4317. (concat
  4318. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4319. org-link-re-with-space
  4320. (concat
  4321. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4322. "\\([^" org-non-link-chars " ]"
  4323. "[^" org-non-link-chars "]*"
  4324. "[^" org-non-link-chars " ]\\)>?")
  4325. org-link-re-with-space2
  4326. (concat
  4327. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4328. "\\([^" org-non-link-chars " ]"
  4329. "[^\t\n\r]*"
  4330. "[^" org-non-link-chars " ]\\)>?")
  4331. org-link-re-with-space3
  4332. (concat
  4333. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4334. "\\([^" org-non-link-chars " ]"
  4335. "[^\t\n\r]*\\)")
  4336. org-angle-link-re
  4337. (concat
  4338. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4339. "\\([^" org-non-link-chars " ]"
  4340. "[^" org-non-link-chars "]*"
  4341. "\\)>")
  4342. org-plain-link-re
  4343. (concat
  4344. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4345. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4346. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4347. org-bracket-link-regexp
  4348. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4349. org-bracket-link-analytic-regexp
  4350. (concat
  4351. "\\[\\["
  4352. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4353. "\\([^]]+\\)"
  4354. "\\]"
  4355. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4356. "\\]")
  4357. org-bracket-link-analytic-regexp++
  4358. (concat
  4359. "\\[\\["
  4360. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4361. "\\([^]]+\\)"
  4362. "\\]"
  4363. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4364. "\\]")
  4365. org-any-link-re
  4366. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4367. org-angle-link-re "\\)\\|\\("
  4368. org-plain-link-re "\\)")))
  4369. (org-make-link-regexps)
  4370. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4371. "Regular expression for fast time stamp matching.")
  4372. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4373. "Regular expression for fast time stamp matching.")
  4374. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4375. "Regular expression matching time strings for analysis.
  4376. This one does not require the space after the date, so it can be used
  4377. on a string that terminates immediately after the date.")
  4378. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4379. "Regular expression matching time strings for analysis.")
  4380. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4381. "Regular expression matching time stamps, with groups.")
  4382. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4383. "Regular expression matching time stamps (also [..]), with groups.")
  4384. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4385. "Regular expression matching a time stamp range.")
  4386. (defconst org-tr-regexp-both
  4387. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4388. "Regular expression matching a time stamp range.")
  4389. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4390. org-ts-regexp "\\)?")
  4391. "Regular expression matching a time stamp or time stamp range.")
  4392. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4393. org-ts-regexp-both "\\)?")
  4394. "Regular expression matching a time stamp or time stamp range.
  4395. The time stamps may be either active or inactive.")
  4396. (defvar org-emph-face nil)
  4397. (defun org-do-emphasis-faces (limit)
  4398. "Run through the buffer and add overlays to links."
  4399. (let (rtn a)
  4400. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4401. (if (not (= (char-after (match-beginning 3))
  4402. (char-after (match-beginning 4))))
  4403. (progn
  4404. (setq rtn t)
  4405. (setq a (assoc (match-string 3) org-emphasis-alist))
  4406. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4407. 'face
  4408. (nth 1 a))
  4409. (and (nth 4 a)
  4410. (org-remove-flyspell-overlays-in
  4411. (match-beginning 0) (match-end 0)))
  4412. (add-text-properties (match-beginning 2) (match-end 2)
  4413. '(font-lock-multiline t org-emphasis t))
  4414. (when org-hide-emphasis-markers
  4415. (add-text-properties (match-end 4) (match-beginning 5)
  4416. '(invisible org-link))
  4417. (add-text-properties (match-beginning 3) (match-end 3)
  4418. '(invisible org-link)))))
  4419. (backward-char 1))
  4420. rtn))
  4421. (defun org-emphasize (&optional char)
  4422. "Insert or change an emphasis, i.e. a font like bold or italic.
  4423. If there is an active region, change that region to a new emphasis.
  4424. If there is no region, just insert the marker characters and position
  4425. the cursor between them.
  4426. CHAR should be either the marker character, or the first character of the
  4427. HTML tag associated with that emphasis. If CHAR is a space, the means
  4428. to remove the emphasis of the selected region.
  4429. If char is not given (for example in an interactive call) it
  4430. will be prompted for."
  4431. (interactive)
  4432. (let ((eal org-emphasis-alist) e det
  4433. (erc org-emphasis-regexp-components)
  4434. (prompt "")
  4435. (string "") beg end move tag c s)
  4436. (if (org-region-active-p)
  4437. (setq beg (region-beginning) end (region-end)
  4438. string (buffer-substring beg end))
  4439. (setq move t))
  4440. (while (setq e (pop eal))
  4441. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4442. c (aref tag 0))
  4443. (push (cons c (string-to-char (car e))) det)
  4444. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4445. (substring tag 1)))))
  4446. (setq det (nreverse det))
  4447. (unless char
  4448. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4449. (setq char (read-char-exclusive)))
  4450. (setq char (or (cdr (assoc char det)) char))
  4451. (if (equal char ?\ )
  4452. (setq s "" move nil)
  4453. (unless (assoc (char-to-string char) org-emphasis-alist)
  4454. (error "No such emphasis marker: \"%c\"" char))
  4455. (setq s (char-to-string char)))
  4456. (while (and (> (length string) 1)
  4457. (equal (substring string 0 1) (substring string -1))
  4458. (assoc (substring string 0 1) org-emphasis-alist))
  4459. (setq string (substring string 1 -1)))
  4460. (setq string (concat s string s))
  4461. (if beg (delete-region beg end))
  4462. (unless (or (bolp)
  4463. (string-match (concat "[" (nth 0 erc) "\n]")
  4464. (char-to-string (char-before (point)))))
  4465. (insert " "))
  4466. (unless (or (eobp)
  4467. (string-match (concat "[" (nth 1 erc) "\n]")
  4468. (char-to-string (char-after (point)))))
  4469. (insert " ") (backward-char 1))
  4470. (insert string)
  4471. (and move (backward-char 1))))
  4472. (defconst org-nonsticky-props
  4473. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4474. (defsubst org-rear-nonsticky-at (pos)
  4475. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4476. (defun org-activate-plain-links (limit)
  4477. "Run through the buffer and add overlays to links."
  4478. (catch 'exit
  4479. (let (f)
  4480. (if (re-search-forward org-plain-link-re limit t)
  4481. (progn
  4482. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4483. (setq f (get-text-property (match-beginning 0) 'face))
  4484. (if (or (eq f 'org-tag)
  4485. (and (listp f) (memq 'org-tag f)))
  4486. nil
  4487. (add-text-properties (match-beginning 0) (match-end 0)
  4488. (list 'mouse-face 'highlight
  4489. 'face 'org-link
  4490. 'keymap org-mouse-map))
  4491. (org-rear-nonsticky-at (match-end 0)))
  4492. t)))))
  4493. (defun org-activate-code (limit)
  4494. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4495. (progn
  4496. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4497. (remove-text-properties (match-beginning 0) (match-end 0)
  4498. '(display t invisible t intangible t))
  4499. t)))
  4500. (defcustom org-src-fontify-natively nil
  4501. "When non-nil, fontify code in code blocks."
  4502. :type 'boolean
  4503. :group 'org-appearance
  4504. :group 'org-babel)
  4505. (defun org-fontify-meta-lines-and-blocks (limit)
  4506. "Fontify #+ lines and blocks, in the correct ways."
  4507. (let ((case-fold-search t))
  4508. (if (re-search-forward
  4509. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4510. limit t)
  4511. (let ((beg (match-beginning 0))
  4512. (block-start (match-end 0))
  4513. (block-end nil)
  4514. (lang (match-string 7))
  4515. (beg1 (line-beginning-position 2))
  4516. (dc1 (downcase (match-string 2)))
  4517. (dc3 (downcase (match-string 3)))
  4518. end end1 quoting block-type)
  4519. (cond
  4520. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4521. ;; a single line of backend-specific content
  4522. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4523. (remove-text-properties (match-beginning 0) (match-end 0)
  4524. '(display t invisible t intangible t))
  4525. (add-text-properties (match-beginning 1) (match-end 3)
  4526. '(font-lock-fontified t face org-meta-line))
  4527. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4528. '(font-lock-fontified t face org-block))
  4529. ; for backend-specific code
  4530. t)
  4531. ((and (match-end 4) (equal dc3 "begin"))
  4532. ;; Truly a block
  4533. (setq block-type (downcase (match-string 5))
  4534. quoting (member block-type org-protecting-blocks))
  4535. (when (re-search-forward
  4536. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4537. nil t) ;; on purpose, we look further than LIMIT
  4538. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4539. (setq block-end (match-beginning 0))
  4540. (when quoting
  4541. (remove-text-properties beg end
  4542. '(display t invisible t intangible t)))
  4543. (add-text-properties
  4544. beg end
  4545. '(font-lock-fontified t font-lock-multiline t))
  4546. (add-text-properties beg beg1 '(face org-meta-line))
  4547. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4548. ; for end_src
  4549. (cond
  4550. ((and lang org-src-fontify-natively)
  4551. (org-src-font-lock-fontify-block lang block-start block-end))
  4552. (quoting
  4553. (add-text-properties beg1 (+ end1 1) '(face
  4554. org-block)))
  4555. ; end of source block
  4556. ((not org-fontify-quote-and-verse-blocks))
  4557. ((string= block-type "quote")
  4558. (add-text-properties beg1 end1 '(face org-quote)))
  4559. ((string= block-type "verse")
  4560. (add-text-properties beg1 end1 '(face org-verse))))
  4561. t))
  4562. ((member dc1 '("title:" "author:" "email:" "date:"))
  4563. (add-text-properties
  4564. beg (match-end 3)
  4565. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4566. '(font-lock-fontified t invisible t)
  4567. '(font-lock-fontified t face org-document-info-keyword)))
  4568. (add-text-properties
  4569. (match-beginning 6) (match-end 6)
  4570. (if (string-equal dc1 "title:")
  4571. '(font-lock-fontified t face org-document-title)
  4572. '(font-lock-fontified t face org-document-info))))
  4573. ((not (member (char-after beg) '(?\ ?\t)))
  4574. ;; just any other in-buffer setting, but not indented
  4575. (add-text-properties
  4576. beg (match-end 0)
  4577. '(font-lock-fontified t face org-meta-line))
  4578. t)
  4579. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4580. "orgtbl:" "tblfm:" "tblname:" "result:"
  4581. "results:" "source:" "srcname:" "call:"))
  4582. (and (match-end 4) (equal dc3 "attr")))
  4583. (add-text-properties
  4584. beg (match-end 0)
  4585. '(font-lock-fontified t face org-meta-line))
  4586. t)
  4587. ((member dc3 '(" " ""))
  4588. (add-text-properties
  4589. beg (match-end 0)
  4590. '(font-lock-fontified t face font-lock-comment-face)))
  4591. (t nil))))))
  4592. (defun org-activate-angle-links (limit)
  4593. "Run through the buffer and add overlays to links."
  4594. (if (re-search-forward org-angle-link-re limit t)
  4595. (progn
  4596. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4597. (add-text-properties (match-beginning 0) (match-end 0)
  4598. (list 'mouse-face 'highlight
  4599. 'keymap org-mouse-map))
  4600. (org-rear-nonsticky-at (match-end 0))
  4601. t)))
  4602. (defun org-activate-footnote-links (limit)
  4603. "Run through the buffer and add overlays to links."
  4604. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4605. limit t)
  4606. (progn
  4607. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4608. (add-text-properties (match-beginning 2) (match-end 2)
  4609. (list 'mouse-face 'highlight
  4610. 'keymap org-mouse-map
  4611. 'help-echo
  4612. (if (= (point-at-bol) (match-beginning 2))
  4613. "Footnote definition"
  4614. "Footnote reference")
  4615. ))
  4616. (org-rear-nonsticky-at (match-end 2))
  4617. t)))
  4618. (defun org-activate-bracket-links (limit)
  4619. "Run through the buffer and add overlays to bracketed links."
  4620. (if (re-search-forward org-bracket-link-regexp limit t)
  4621. (let* ((help (concat "LINK: "
  4622. (org-match-string-no-properties 1)))
  4623. ;; FIXME: above we should remove the escapes.
  4624. ;; but that requires another match, protecting match data,
  4625. ;; a lot of overhead for font-lock.
  4626. (ip (org-maybe-intangible
  4627. (list 'invisible 'org-link
  4628. 'keymap org-mouse-map 'mouse-face 'highlight
  4629. 'font-lock-multiline t 'help-echo help)))
  4630. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4631. 'font-lock-multiline t 'help-echo help)))
  4632. ;; We need to remove the invisible property here. Table narrowing
  4633. ;; may have made some of this invisible.
  4634. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4635. (remove-text-properties (match-beginning 0) (match-end 0)
  4636. '(invisible nil))
  4637. (if (match-end 3)
  4638. (progn
  4639. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4640. (org-rear-nonsticky-at (match-beginning 3))
  4641. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4642. (org-rear-nonsticky-at (match-end 3))
  4643. (add-text-properties (match-end 3) (match-end 0) ip)
  4644. (org-rear-nonsticky-at (match-end 0)))
  4645. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4646. (org-rear-nonsticky-at (match-beginning 1))
  4647. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4648. (org-rear-nonsticky-at (match-end 1))
  4649. (add-text-properties (match-end 1) (match-end 0) ip)
  4650. (org-rear-nonsticky-at (match-end 0)))
  4651. t)))
  4652. (defun org-activate-dates (limit)
  4653. "Run through the buffer and add overlays to dates."
  4654. (if (re-search-forward org-tsr-regexp-both limit t)
  4655. (progn
  4656. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4657. (add-text-properties (match-beginning 0) (match-end 0)
  4658. (list 'mouse-face 'highlight
  4659. 'keymap org-mouse-map))
  4660. (org-rear-nonsticky-at (match-end 0))
  4661. (when org-display-custom-times
  4662. (if (match-end 3)
  4663. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4664. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4665. t)))
  4666. (defvar org-target-link-regexp nil
  4667. "Regular expression matching radio targets in plain text.")
  4668. (make-variable-buffer-local 'org-target-link-regexp)
  4669. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4670. "Regular expression matching a link target.")
  4671. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4672. "Regular expression matching a radio target.")
  4673. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4674. "Regular expression matching any target.")
  4675. (defun org-activate-target-links (limit)
  4676. "Run through the buffer and add overlays to target matches."
  4677. (when org-target-link-regexp
  4678. (let ((case-fold-search t))
  4679. (if (re-search-forward org-target-link-regexp limit t)
  4680. (progn
  4681. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4682. (add-text-properties (match-beginning 0) (match-end 0)
  4683. (list 'mouse-face 'highlight
  4684. 'keymap org-mouse-map
  4685. 'help-echo "Radio target link"
  4686. 'org-linked-text t))
  4687. (org-rear-nonsticky-at (match-end 0))
  4688. t)))))
  4689. (defun org-update-radio-target-regexp ()
  4690. "Find all radio targets in this file and update the regular expression."
  4691. (interactive)
  4692. (when (memq 'radio org-activate-links)
  4693. (setq org-target-link-regexp
  4694. (org-make-target-link-regexp (org-all-targets 'radio)))
  4695. (org-restart-font-lock)))
  4696. (defun org-hide-wide-columns (limit)
  4697. (let (s e)
  4698. (setq s (text-property-any (point) (or limit (point-max))
  4699. 'org-cwidth t))
  4700. (when s
  4701. (setq e (next-single-property-change s 'org-cwidth))
  4702. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4703. (goto-char e)
  4704. t)))
  4705. (defvar org-latex-and-specials-regexp nil
  4706. "Regular expression for highlighting export special stuff.")
  4707. (defvar org-match-substring-regexp)
  4708. (defvar org-match-substring-with-braces-regexp)
  4709. ;; This should be with the exporter code, but we also use if for font-locking
  4710. (defconst org-export-html-special-string-regexps
  4711. '(("\\\\-" . "&shy;")
  4712. ("---\\([^-]\\)" . "&mdash;\\1")
  4713. ("--\\([^-]\\)" . "&ndash;\\1")
  4714. ("\\.\\.\\." . "&hellip;"))
  4715. "Regular expressions for special string conversion.")
  4716. (defun org-compute-latex-and-specials-regexp ()
  4717. "Compute regular expression for stuff treated specially by exporters."
  4718. (if (not org-highlight-latex-fragments-and-specials)
  4719. (org-set-local 'org-latex-and-specials-regexp nil)
  4720. (require 'org-exp)
  4721. (let*
  4722. ((matchers (plist-get org-format-latex-options :matchers))
  4723. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4724. org-latex-regexps)))
  4725. (org-export-allow-BIND nil)
  4726. (options (org-combine-plists (org-default-export-plist)
  4727. (org-infile-export-plist)))
  4728. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4729. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4730. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4731. (org-export-html-expand (plist-get options :expand-quoted-html))
  4732. (org-export-with-special-strings (plist-get options :special-strings))
  4733. (re-sub
  4734. (cond
  4735. ((equal org-export-with-sub-superscripts '{})
  4736. (list org-match-substring-with-braces-regexp))
  4737. (org-export-with-sub-superscripts
  4738. (list org-match-substring-regexp))
  4739. (t nil)))
  4740. (re-latex
  4741. (if org-export-with-LaTeX-fragments
  4742. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4743. (re-macros
  4744. (if org-export-with-TeX-macros
  4745. (list (concat "\\\\"
  4746. (regexp-opt
  4747. (append
  4748. (delq nil
  4749. (mapcar 'car-safe
  4750. (append org-entities-user
  4751. org-entities)))
  4752. (if (boundp 'org-latex-entities)
  4753. (mapcar (lambda (x)
  4754. (or (car-safe x) x))
  4755. org-latex-entities)
  4756. nil))
  4757. 'words))) ; FIXME
  4758. ))
  4759. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4760. (re-special (if org-export-with-special-strings
  4761. (mapcar (lambda (x) (car x))
  4762. org-export-html-special-string-regexps)))
  4763. (re-rest
  4764. (delq nil
  4765. (list
  4766. (if org-export-html-expand "@<[^>\n]+>")
  4767. ))))
  4768. (org-set-local
  4769. 'org-latex-and-specials-regexp
  4770. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4771. re-rest) "\\|")))))
  4772. (defun org-do-latex-and-special-faces (limit)
  4773. "Run through the buffer and add overlays to links."
  4774. (when org-latex-and-specials-regexp
  4775. (let (rtn d)
  4776. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4777. limit t))
  4778. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4779. 'face))
  4780. '(org-code org-verbatim underline)))
  4781. (progn
  4782. (setq rtn t
  4783. d (cond ((member (char-after (1+ (match-beginning 0)))
  4784. '(?_ ?^)) 1)
  4785. (t 0)))
  4786. (font-lock-prepend-text-property
  4787. (+ d (match-beginning 0)) (match-end 0)
  4788. 'face 'org-latex-and-export-specials)
  4789. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4790. '(font-lock-multiline t)))))
  4791. rtn)))
  4792. (defun org-restart-font-lock ()
  4793. "Restart `font-lock-mode', to force refontification."
  4794. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4795. (font-lock-mode -1)
  4796. (font-lock-mode 1)))
  4797. (defun org-all-targets (&optional radio)
  4798. "Return a list of all targets in this file.
  4799. With optional argument RADIO, only find radio targets."
  4800. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4801. rtn)
  4802. (save-excursion
  4803. (goto-char (point-min))
  4804. (while (re-search-forward re nil t)
  4805. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4806. rtn)))
  4807. (defun org-make-target-link-regexp (targets)
  4808. "Make regular expression matching all strings in TARGETS.
  4809. The regular expression finds the targets also if there is a line break
  4810. between words."
  4811. (and targets
  4812. (concat
  4813. "\\<\\("
  4814. (mapconcat
  4815. (lambda (x)
  4816. (while (string-match " +" x)
  4817. (setq x (replace-match "\\s-+" t t x)))
  4818. x)
  4819. targets
  4820. "\\|")
  4821. "\\)\\>")))
  4822. (defun org-activate-tags (limit)
  4823. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4824. (progn
  4825. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4826. (add-text-properties (match-beginning 1) (match-end 1)
  4827. (list 'mouse-face 'highlight
  4828. 'keymap org-mouse-map))
  4829. (org-rear-nonsticky-at (match-end 1))
  4830. t)))
  4831. (defun org-outline-level ()
  4832. "Compute the outline level of the heading at point.
  4833. This function assumes that the cursor is at the beginning of a line matched
  4834. by `outline-regexp'. Otherwise it returns garbage.
  4835. If this is called at a normal headline, the level is the number of stars.
  4836. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4837. For plain list items, if they are matched by `outline-regexp', this returns
  4838. 1000 plus the line indentation."
  4839. (save-excursion
  4840. (looking-at outline-regexp)
  4841. (if (match-beginning 1)
  4842. (+ (org-get-string-indentation (match-string 1)) 1000)
  4843. (1- (- (match-end 0) (match-beginning 0))))))
  4844. (defvar org-font-lock-keywords nil)
  4845. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4846. "Regular expression matching a property line.")
  4847. (defvar org-font-lock-hook nil
  4848. "Functions to be called for special font lock stuff.")
  4849. (defvar org-font-lock-set-keywords-hook nil
  4850. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4851. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4852. it is installed to be used by font lock. This can be useful if something
  4853. needs to be inserted at a specific position in the font-lock sequence.")
  4854. (defun org-font-lock-hook (limit)
  4855. (run-hook-with-args 'org-font-lock-hook limit))
  4856. (defun org-set-font-lock-defaults ()
  4857. (let* ((em org-fontify-emphasized-text)
  4858. (lk org-activate-links)
  4859. (org-font-lock-extra-keywords
  4860. (list
  4861. ;; Call the hook
  4862. '(org-font-lock-hook)
  4863. ;; Headlines
  4864. `(,(if org-fontify-whole-heading-line
  4865. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4866. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4867. (1 (org-get-level-face 1))
  4868. (2 (org-get-level-face 2))
  4869. (3 (org-get-level-face 3)))
  4870. ;; Table lines
  4871. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4872. (1 'org-table t))
  4873. ;; Table internals
  4874. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4875. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4876. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4877. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  4878. ;; Drawers
  4879. (list org-drawer-regexp '(0 'org-special-keyword t))
  4880. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4881. ;; Properties
  4882. (list org-property-re
  4883. '(1 'org-special-keyword t)
  4884. '(3 'org-property-value t))
  4885. ;; Links
  4886. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4887. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4888. (if (memq 'plain lk) '(org-activate-plain-links))
  4889. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4890. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4891. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4892. (if (memq 'footnote lk) '(org-activate-footnote-links
  4893. (2 'org-footnote t)))
  4894. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4895. '(org-hide-wide-columns (0 nil append))
  4896. ;; TODO lines
  4897. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4898. '(1 (org-get-todo-face 1) t))
  4899. ;; DONE
  4900. (if org-fontify-done-headline
  4901. (list (concat "^[*]+ +\\<\\("
  4902. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4903. "\\)\\(.*\\)")
  4904. '(2 'org-headline-done t))
  4905. nil)
  4906. ;; Priorities
  4907. '(org-font-lock-add-priority-faces)
  4908. ;; Tags
  4909. '(org-font-lock-add-tag-faces)
  4910. ;; Special keywords
  4911. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4912. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4913. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4914. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4915. ;; Emphasis
  4916. (if em
  4917. (if (featurep 'xemacs)
  4918. '(org-do-emphasis-faces (0 nil append))
  4919. '(org-do-emphasis-faces)))
  4920. ;; Checkboxes
  4921. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4922. 1 'org-checkbox prepend)
  4923. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4924. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4925. (0 (org-get-checkbox-statistics-face) t)))
  4926. ;; Description list items
  4927. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(.*? ::\\)"
  4928. 2 'bold prepend)
  4929. ;; ARCHIVEd headings
  4930. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4931. '(1 'org-archived prepend))
  4932. ;; Specials
  4933. '(org-do-latex-and-special-faces)
  4934. '(org-fontify-entities)
  4935. '(org-raise-scripts)
  4936. ;; Code
  4937. '(org-activate-code (1 'org-code t))
  4938. ;; COMMENT
  4939. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4940. "\\|" org-quote-string "\\)\\>")
  4941. '(1 'org-special-keyword t))
  4942. '("^#.*" (0 'font-lock-comment-face t))
  4943. ;; Blocks and meta lines
  4944. '(org-fontify-meta-lines-and-blocks)
  4945. )))
  4946. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4947. (run-hooks 'org-font-lock-set-keywords-hook)
  4948. ;; Now set the full font-lock-keywords
  4949. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4950. (org-set-local 'font-lock-defaults
  4951. '(org-font-lock-keywords t nil nil backward-paragraph))
  4952. (kill-local-variable 'font-lock-keywords) nil))
  4953. (defun org-toggle-pretty-entities ()
  4954. "Toggle the composition display of entities as UTF8 characters."
  4955. (interactive)
  4956. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4957. (org-restart-font-lock)
  4958. (if org-pretty-entities
  4959. (message "Entities are displayed as UTF8 characers")
  4960. (save-restriction
  4961. (widen)
  4962. (org-decompose-region (point-min) (point-max))
  4963. (message "Entities are displayed plain"))))
  4964. (defun org-fontify-entities (limit)
  4965. "Find an entity to fontify."
  4966. (let (ee)
  4967. (when org-pretty-entities
  4968. (catch 'match
  4969. (while (re-search-forward
  4970. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  4971. limit t)
  4972. (if (and (not (org-in-indented-comment-line))
  4973. (setq ee (org-entity-get (match-string 1)))
  4974. (= (length (nth 6 ee)) 1))
  4975. (progn
  4976. (add-text-properties
  4977. (match-beginning 0) (match-end 1)
  4978. (list 'font-lock-fontified t))
  4979. (compose-region (match-beginning 0) (match-end 1)
  4980. (nth 6 ee) nil)
  4981. (backward-char 1)
  4982. (throw 'match t))))
  4983. nil))))
  4984. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4985. "Fontify string S like in Org-mode."
  4986. (with-temp-buffer
  4987. (insert s)
  4988. (let ((org-odd-levels-only odd-levels))
  4989. (org-mode)
  4990. (font-lock-fontify-buffer)
  4991. (buffer-string))))
  4992. (defvar org-m nil)
  4993. (defvar org-l nil)
  4994. (defvar org-f nil)
  4995. (defun org-get-level-face (n)
  4996. "Get the right face for match N in font-lock matching of headlines."
  4997. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4998. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4999. (if org-cycle-level-faces
  5000. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5001. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5002. (cond
  5003. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5004. ((eq n 2) org-f)
  5005. (t (if org-level-color-stars-only nil org-f))))
  5006. (defun org-get-todo-face (kwd)
  5007. "Get the right face for a TODO keyword KWD.
  5008. If KWD is a number, get the corresponding match group."
  5009. (if (numberp kwd) (setq kwd (match-string kwd)))
  5010. (or (org-face-from-face-or-color
  5011. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5012. (and (member kwd org-done-keywords) 'org-done)
  5013. 'org-todo))
  5014. (defun org-face-from-face-or-color (context inherit face-or-color)
  5015. "Create a face list that inherits INHERIT, but sets the foreground color.
  5016. When FACE-OR-COLOR is not a string, just return it."
  5017. (if (stringp face-or-color)
  5018. (list :inherit inherit
  5019. (cdr (assoc context org-faces-easy-properties))
  5020. face-or-color)
  5021. face-or-color))
  5022. (defun org-font-lock-add-tag-faces (limit)
  5023. "Add the special tag faces."
  5024. (when (and org-tag-faces org-tags-special-faces-re)
  5025. (while (re-search-forward org-tags-special-faces-re limit t)
  5026. (add-text-properties (match-beginning 1) (match-end 1)
  5027. (list 'face (org-get-tag-face 1)
  5028. 'font-lock-fontified t))
  5029. (backward-char 1))))
  5030. (defun org-font-lock-add-priority-faces (limit)
  5031. "Add the special priority faces."
  5032. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5033. (add-text-properties
  5034. (match-beginning 0) (match-end 0)
  5035. (list 'face (or (org-face-from-face-or-color
  5036. 'priority 'org-special-keyword
  5037. (cdr (assoc (char-after (match-beginning 1))
  5038. org-priority-faces)))
  5039. 'org-special-keyword)
  5040. 'font-lock-fontified t))))
  5041. (defun org-get-tag-face (kwd)
  5042. "Get the right face for a TODO keyword KWD.
  5043. If KWD is a number, get the corresponding match group."
  5044. (if (numberp kwd) (setq kwd (match-string kwd)))
  5045. (or (org-face-from-face-or-color
  5046. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5047. 'org-tag))
  5048. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5049. "Remove fontification and activation overlays from links."
  5050. (font-lock-default-unfontify-region beg end)
  5051. (let* ((buffer-undo-list t)
  5052. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5053. (inhibit-modification-hooks t)
  5054. deactivate-mark buffer-file-name buffer-file-truename)
  5055. (org-decompose-region beg end)
  5056. (remove-text-properties
  5057. beg end
  5058. (if org-indent-mode
  5059. ;; also remove line-prefix and wrap-prefix properties
  5060. '(mouse-face t keymap t org-linked-text t
  5061. invisible t intangible t
  5062. line-prefix t wrap-prefix t
  5063. org-no-flyspell t org-emphasis t)
  5064. '(mouse-face t keymap t org-linked-text t
  5065. invisible t intangible t
  5066. org-no-flyspell t org-emphasis t)))
  5067. (org-remove-font-lock-display-properties beg end)))
  5068. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5069. ((raise 0.3) (height 0.7))
  5070. ((raise -0.5))
  5071. ((raise 0.5)))
  5072. "Display properties for showing superscripts and subscripts.")
  5073. (defun org-remove-font-lock-display-properties (beg end)
  5074. "Remove specific display properties that have been added by font lock.
  5075. The will remove the raise properties that are used to show superscripts
  5076. and subscripts."
  5077. (let (next prop)
  5078. (while (< beg end)
  5079. (setq next (next-single-property-change beg 'display nil end)
  5080. prop (get-text-property beg 'display))
  5081. (if (member prop org-script-display)
  5082. (put-text-property beg next 'display nil))
  5083. (setq beg next))))
  5084. (defun org-raise-scripts (limit)
  5085. "Add raise properties to sub/superscripts."
  5086. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5087. (if (re-search-forward
  5088. (if (eq org-use-sub-superscripts t)
  5089. org-match-substring-regexp
  5090. org-match-substring-with-braces-regexp)
  5091. limit t)
  5092. (let* ((pos (point)) table-p comment-p
  5093. (mpos (match-beginning 3))
  5094. (emph-p (get-text-property mpos 'org-emphasis))
  5095. (link-p (get-text-property mpos 'mouse-face))
  5096. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5097. (goto-char (point-at-bol))
  5098. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5099. comment-p (org-looking-at-p "[ \t]*#"))
  5100. (goto-char pos)
  5101. ;; FIXME: Should we go back one character here, for a_b^c
  5102. ;; (goto-char (1- pos)) ;????????????????????
  5103. (if (or comment-p emph-p link-p keyw-p)
  5104. t
  5105. (put-text-property (match-beginning 3) (match-end 0)
  5106. 'display
  5107. (if (equal (char-after (match-beginning 2)) ?^)
  5108. (nth (if table-p 3 1) org-script-display)
  5109. (nth (if table-p 2 0) org-script-display)))
  5110. (add-text-properties (match-beginning 2) (match-end 2)
  5111. (list 'invisible t
  5112. 'org-dwidth t 'org-dwidth-n 1))
  5113. (if (and (eq (char-after (match-beginning 3)) ?{)
  5114. (eq (char-before (match-end 3)) ?}))
  5115. (progn
  5116. (add-text-properties
  5117. (match-beginning 3) (1+ (match-beginning 3))
  5118. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5119. (add-text-properties
  5120. (1- (match-end 3)) (match-end 3)
  5121. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5122. t)))))
  5123. ;;;; Visibility cycling, including org-goto and indirect buffer
  5124. ;;; Cycling
  5125. (defvar org-cycle-global-status nil)
  5126. (make-variable-buffer-local 'org-cycle-global-status)
  5127. (defvar org-cycle-subtree-status nil)
  5128. (make-variable-buffer-local 'org-cycle-subtree-status)
  5129. ;;;###autoload
  5130. (defvar org-inlinetask-min-level)
  5131. (defun org-cycle (&optional arg)
  5132. "TAB-action and visibility cycling for Org-mode.
  5133. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5134. is outline visibility cycling, but it also invokes other actions
  5135. in special contexts.
  5136. - When this function is called with a prefix argument, rotate the entire
  5137. buffer through 3 states (global cycling)
  5138. 1. OVERVIEW: Show only top-level headlines.
  5139. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5140. 3. SHOW ALL: Show everything.
  5141. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5142. determined by the variable `org-startup-folded', and by any VISIBILITY
  5143. properties in the buffer.
  5144. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5145. including any drawers.
  5146. - When inside a table, re-align the table and move to the next field.
  5147. - When point is at the beginning of a headline, rotate the subtree started
  5148. by this line through 3 different states (local cycling)
  5149. 1. FOLDED: Only the main headline is shown.
  5150. 2. CHILDREN: The main headline and the direct children are shown.
  5151. From this state, you can move to one of the children
  5152. and zoom in further.
  5153. 3. SUBTREE: Show the entire subtree, including body text.
  5154. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5155. - When point is at the beginning of an empty headline and the variable
  5156. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5157. of the headline by demoting and promoting it to likely levels. This
  5158. speeds up creation document structure by pressing TAB once or several
  5159. times right after creating a new headline.
  5160. - When there is a numeric prefix, go up to a heading with level ARG, do
  5161. a `show-subtree' and return to the previous cursor position. If ARG
  5162. is negative, go up that many levels.
  5163. - When point is not at the beginning of a headline, execute the global
  5164. binding for TAB, which is re-indenting the line. See the option
  5165. `org-cycle-emulate-tab' for details.
  5166. - Special case: if point is at the beginning of the buffer and there is
  5167. no headline in line 1, this function will act as if called with prefix arg
  5168. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5169. But only if also the variable `org-cycle-global-at-bob' is t."
  5170. (interactive "P")
  5171. (org-load-modules-maybe)
  5172. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5173. (and org-cycle-level-after-item/entry-creation
  5174. (or (org-cycle-level)
  5175. (org-cycle-item-indentation))))
  5176. (let* ((limit-level
  5177. (or org-cycle-max-level
  5178. (and (boundp 'org-inlinetask-min-level)
  5179. org-inlinetask-min-level
  5180. (1- org-inlinetask-min-level))))
  5181. (nstars (and limit-level
  5182. (if org-odd-levels-only
  5183. (and limit-level (1- (* limit-level 2)))
  5184. limit-level)))
  5185. (outline-regexp
  5186. (cond
  5187. ((not (org-mode-p)) outline-regexp)
  5188. ((or (eq org-cycle-include-plain-lists 'integrate)
  5189. (and org-cycle-include-plain-lists (org-at-item-p)))
  5190. (concat "\\(?:\\*"
  5191. (if nstars (format "\\{1,%d\\}" nstars) "+")
  5192. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  5193. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  5194. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5195. (not (looking-at outline-regexp))))
  5196. (org-cycle-hook
  5197. (if bob-special
  5198. (delq 'org-optimize-window-after-visibility-change
  5199. (copy-sequence org-cycle-hook))
  5200. org-cycle-hook))
  5201. (pos (point)))
  5202. (if (or bob-special (equal arg '(4)))
  5203. ;; special case: use global cycling
  5204. (setq arg t))
  5205. (cond
  5206. ((equal arg '(16))
  5207. (setq last-command 'dummy)
  5208. (org-set-startup-visibility)
  5209. (message "Startup visibility, plus VISIBILITY properties"))
  5210. ((equal arg '(64))
  5211. (show-all)
  5212. (message "Entire buffer visible, including drawers"))
  5213. ((org-at-table-p 'any)
  5214. ;; Enter the table or move to the next field in the table
  5215. (if (org-at-table.el-p)
  5216. (message "Use C-c ' to edit table.el tables")
  5217. (if arg (org-table-edit-field t)
  5218. (org-table-justify-field-maybe)
  5219. (call-interactively 'org-table-next-field))))
  5220. ((run-hook-with-args-until-success
  5221. 'org-tab-after-check-for-table-hook))
  5222. ((eq arg t) ;; Global cycling
  5223. (org-cycle-internal-global))
  5224. ((and org-drawers org-drawer-regexp
  5225. (save-excursion
  5226. (beginning-of-line 1)
  5227. (looking-at org-drawer-regexp)))
  5228. ;; Toggle block visibility
  5229. (org-flag-drawer
  5230. (not (get-char-property (match-end 0) 'invisible))))
  5231. ((integerp arg)
  5232. ;; Show-subtree, ARG levels up from here.
  5233. (save-excursion
  5234. (org-back-to-heading)
  5235. (outline-up-heading (if (< arg 0) (- arg)
  5236. (- (funcall outline-level) arg)))
  5237. (org-show-subtree)))
  5238. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5239. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5240. (org-cycle-internal-local))
  5241. ;; TAB emulation and template completion
  5242. (buffer-read-only (org-back-to-heading))
  5243. ((run-hook-with-args-until-success
  5244. 'org-tab-after-check-for-cycling-hook))
  5245. ((org-try-structure-completion))
  5246. ((org-try-cdlatex-tab))
  5247. ((run-hook-with-args-until-success
  5248. 'org-tab-before-tab-emulation-hook))
  5249. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5250. (or (not (bolp))
  5251. (not (looking-at outline-regexp))))
  5252. (call-interactively (global-key-binding "\t")))
  5253. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5254. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5255. (or (and (eq org-cycle-emulate-tab 'white)
  5256. (= (match-end 0) (point-at-eol)))
  5257. (and (eq org-cycle-emulate-tab 'whitestart)
  5258. (>= (match-end 0) pos))))
  5259. t
  5260. (eq org-cycle-emulate-tab t))
  5261. (call-interactively (global-key-binding "\t")))
  5262. (t (save-excursion
  5263. (org-back-to-heading)
  5264. (org-cycle)))))))
  5265. (defun org-cycle-internal-global ()
  5266. "Do the global cycling action."
  5267. (cond
  5268. ((and (eq last-command this-command)
  5269. (eq org-cycle-global-status 'overview))
  5270. ;; We just created the overview - now do table of contents
  5271. ;; This can be slow in very large buffers, so indicate action
  5272. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5273. (message "CONTENTS...")
  5274. (org-content)
  5275. (message "CONTENTS...done")
  5276. (setq org-cycle-global-status 'contents)
  5277. (run-hook-with-args 'org-cycle-hook 'contents))
  5278. ((and (eq last-command this-command)
  5279. (eq org-cycle-global-status 'contents))
  5280. ;; We just showed the table of contents - now show everything
  5281. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5282. (show-all)
  5283. (message "SHOW ALL")
  5284. (setq org-cycle-global-status 'all)
  5285. (run-hook-with-args 'org-cycle-hook 'all))
  5286. (t
  5287. ;; Default action: go to overview
  5288. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5289. (org-overview)
  5290. (message "OVERVIEW")
  5291. (setq org-cycle-global-status 'overview)
  5292. (run-hook-with-args 'org-cycle-hook 'overview))))
  5293. (defun org-cycle-internal-local ()
  5294. "Do the local cycling action."
  5295. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  5296. ;; First, some boundaries
  5297. (save-excursion
  5298. (org-back-to-heading)
  5299. (setq level (funcall outline-level))
  5300. (save-excursion
  5301. (beginning-of-line 2)
  5302. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  5303. ; XEmacs does not have `next-single-char-property-change'
  5304. ; I'm not sure about Emacs 21.
  5305. (while (and (not (eobp)) ;; this is like `next-line'
  5306. (get-char-property (1- (point)) 'invisible))
  5307. (beginning-of-line 2))
  5308. (while (and (not (eobp)) ;; this is like `next-line'
  5309. (get-char-property (1- (point)) 'invisible))
  5310. (goto-char (next-single-char-property-change (point) 'invisible))
  5311. (and (eolp) (beginning-of-line 2))))
  5312. (setq eol (point)))
  5313. (outline-end-of-heading) (setq eoh (point))
  5314. (save-excursion
  5315. (outline-next-heading)
  5316. (setq has-children (and (org-at-heading-p t)
  5317. (> (funcall outline-level) level))))
  5318. ;; if we're in a list, org-end-of-subtree is in fact org-end-of-item.
  5319. (if (org-at-item-p)
  5320. (setq eos (if (and (org-end-of-item) (bolp))
  5321. (1- (point))
  5322. (point)))
  5323. (org-end-of-subtree t)
  5324. (unless (eobp)
  5325. (skip-chars-forward " \t\n"))
  5326. (setq eos (if (eobp) (point) (1- (point))))))
  5327. ;; Find out what to do next and set `this-command'
  5328. (cond
  5329. ((= eos eoh)
  5330. ;; Nothing is hidden behind this heading
  5331. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5332. (message "EMPTY ENTRY")
  5333. (setq org-cycle-subtree-status nil)
  5334. (save-excursion
  5335. (goto-char eos)
  5336. (outline-next-heading)
  5337. (if (org-invisible-p) (org-flag-heading nil))))
  5338. ((and (or (>= eol eos)
  5339. (not (string-match "\\S-" (buffer-substring eol eos))))
  5340. (or has-children
  5341. (not (setq children-skipped
  5342. org-cycle-skip-children-state-if-no-children))))
  5343. ;; Entire subtree is hidden in one line: children view
  5344. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5345. (org-show-entry)
  5346. (show-children)
  5347. (message "CHILDREN")
  5348. (save-excursion
  5349. (goto-char eos)
  5350. (outline-next-heading)
  5351. (if (org-invisible-p) (org-flag-heading nil)))
  5352. (setq org-cycle-subtree-status 'children)
  5353. (run-hook-with-args 'org-cycle-hook 'children))
  5354. ((or children-skipped
  5355. (and (eq last-command this-command)
  5356. (eq org-cycle-subtree-status 'children)))
  5357. ;; We just showed the children, or no children are there,
  5358. ;; now show everything.
  5359. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5360. (outline-flag-region eoh eos nil)
  5361. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5362. (setq org-cycle-subtree-status 'subtree)
  5363. (run-hook-with-args 'org-cycle-hook 'subtree))
  5364. (t
  5365. ;; Default action: hide the subtree.
  5366. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5367. (outline-flag-region eoh eos t)
  5368. (message "FOLDED")
  5369. (setq org-cycle-subtree-status 'folded)
  5370. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5371. ;;;###autoload
  5372. (defun org-global-cycle (&optional arg)
  5373. "Cycle the global visibility. For details see `org-cycle'.
  5374. With \\[universal-argument] prefix arg, switch to startup visibility.
  5375. With a numeric prefix, show all headlines up to that level."
  5376. (interactive "P")
  5377. (let ((org-cycle-include-plain-lists
  5378. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5379. (cond
  5380. ((integerp arg)
  5381. (show-all)
  5382. (hide-sublevels arg)
  5383. (setq org-cycle-global-status 'contents))
  5384. ((equal arg '(4))
  5385. (org-set-startup-visibility)
  5386. (message "Startup visibility, plus VISIBILITY properties."))
  5387. (t
  5388. (org-cycle '(4))))))
  5389. (defun org-set-startup-visibility ()
  5390. "Set the visibility required by startup options and properties."
  5391. (cond
  5392. ((eq org-startup-folded t)
  5393. (org-cycle '(4)))
  5394. ((eq org-startup-folded 'content)
  5395. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5396. (org-cycle '(4)) (org-cycle '(4)))))
  5397. (unless (eq org-startup-folded 'showeverything)
  5398. (if org-hide-block-startup (org-hide-block-all))
  5399. (org-set-visibility-according-to-property 'no-cleanup)
  5400. (org-cycle-hide-archived-subtrees 'all)
  5401. (org-cycle-hide-drawers 'all)
  5402. (org-cycle-show-empty-lines t)))
  5403. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5404. "Switch subtree visibilities according to :VISIBILITY: property."
  5405. (interactive)
  5406. (let (org-show-entry-below state)
  5407. (save-excursion
  5408. (goto-char (point-max))
  5409. (while (re-search-backward
  5410. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5411. nil t)
  5412. (setq state (match-string 1))
  5413. (save-excursion
  5414. (org-back-to-heading t)
  5415. (hide-subtree)
  5416. (org-reveal)
  5417. (cond
  5418. ((equal state '("fold" "folded"))
  5419. (hide-subtree))
  5420. ((equal state "children")
  5421. (org-show-hidden-entry)
  5422. (show-children))
  5423. ((equal state "content")
  5424. (save-excursion
  5425. (save-restriction
  5426. (org-narrow-to-subtree)
  5427. (org-content))))
  5428. ((member state '("all" "showall"))
  5429. (show-subtree)))))
  5430. (unless no-cleanup
  5431. (org-cycle-hide-archived-subtrees 'all)
  5432. (org-cycle-hide-drawers 'all)
  5433. (org-cycle-show-empty-lines 'all)))))
  5434. (defun org-overview ()
  5435. "Switch to overview mode, showing only top-level headlines.
  5436. Really, this shows all headlines with level equal or greater than the level
  5437. of the first headline in the buffer. This is important, because if the
  5438. first headline is not level one, then (hide-sublevels 1) gives confusing
  5439. results."
  5440. (interactive)
  5441. (let ((level (save-excursion
  5442. (goto-char (point-min))
  5443. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5444. (progn
  5445. (goto-char (match-beginning 0))
  5446. (funcall outline-level))))))
  5447. (and level (hide-sublevels level))))
  5448. (defun org-content (&optional arg)
  5449. "Show all headlines in the buffer, like a table of contents.
  5450. With numerical argument N, show content up to level N."
  5451. (interactive "P")
  5452. (save-excursion
  5453. ;; Visit all headings and show their offspring
  5454. (and (integerp arg) (org-overview))
  5455. (goto-char (point-max))
  5456. (catch 'exit
  5457. (while (and (progn (condition-case nil
  5458. (outline-previous-visible-heading 1)
  5459. (error (goto-char (point-min))))
  5460. t)
  5461. (looking-at outline-regexp))
  5462. (if (integerp arg)
  5463. (show-children (1- arg))
  5464. (show-branches))
  5465. (if (bobp) (throw 'exit nil))))))
  5466. (defun org-optimize-window-after-visibility-change (state)
  5467. "Adjust the window after a change in outline visibility.
  5468. This function is the default value of the hook `org-cycle-hook'."
  5469. (when (get-buffer-window (current-buffer))
  5470. (cond
  5471. ((eq state 'content) nil)
  5472. ((eq state 'all) nil)
  5473. ((eq state 'folded) nil)
  5474. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5475. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5476. (defun org-remove-empty-overlays-at (pos)
  5477. "Remove outline overlays that do not contain non-white stuff."
  5478. (mapc
  5479. (lambda (o)
  5480. (and (eq 'outline (overlay-get o 'invisible))
  5481. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5482. (overlay-end o))))
  5483. (delete-overlay o)))
  5484. (overlays-at pos)))
  5485. (defun org-clean-visibility-after-subtree-move ()
  5486. "Fix visibility issues after moving a subtree."
  5487. ;; First, find a reasonable region to look at:
  5488. ;; Start two siblings above, end three below
  5489. (let* ((beg (save-excursion
  5490. (and (org-get-last-sibling)
  5491. (org-get-last-sibling))
  5492. (point)))
  5493. (end (save-excursion
  5494. (and (org-get-next-sibling)
  5495. (org-get-next-sibling)
  5496. (org-get-next-sibling))
  5497. (if (org-at-heading-p)
  5498. (point-at-eol)
  5499. (point))))
  5500. (level (looking-at "\\*+"))
  5501. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5502. (save-excursion
  5503. (save-restriction
  5504. (narrow-to-region beg end)
  5505. (when re
  5506. ;; Properly fold already folded siblings
  5507. (goto-char (point-min))
  5508. (while (re-search-forward re nil t)
  5509. (if (and (not (org-invisible-p))
  5510. (save-excursion
  5511. (goto-char (point-at-eol)) (org-invisible-p)))
  5512. (hide-entry))))
  5513. (org-cycle-show-empty-lines 'overview)
  5514. (org-cycle-hide-drawers 'overview)))))
  5515. (defun org-cycle-show-empty-lines (state)
  5516. "Show empty lines above all visible headlines.
  5517. The region to be covered depends on STATE when called through
  5518. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5519. entire buffer covered. Note that an empty line is only shown if there
  5520. are at least `org-cycle-separator-lines' empty lines before the headline."
  5521. (when (not (= org-cycle-separator-lines 0))
  5522. (save-excursion
  5523. (let* ((n (abs org-cycle-separator-lines))
  5524. (re (cond
  5525. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5526. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5527. (t (let ((ns (number-to-string (- n 2))))
  5528. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5529. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5530. beg end b e)
  5531. (cond
  5532. ((memq state '(overview contents t))
  5533. (setq beg (point-min) end (point-max)))
  5534. ((memq state '(children folded))
  5535. (setq beg (point) end (progn (org-end-of-subtree t t)
  5536. (beginning-of-line 2)
  5537. (point)))))
  5538. (when beg
  5539. (goto-char beg)
  5540. (while (re-search-forward re end t)
  5541. (unless (get-char-property (match-end 1) 'invisible)
  5542. (setq e (match-end 1))
  5543. (if (< org-cycle-separator-lines 0)
  5544. (setq b (save-excursion
  5545. (goto-char (match-beginning 0))
  5546. (org-back-over-empty-lines)
  5547. (if (save-excursion
  5548. (goto-char (max (point-min) (1- (point))))
  5549. (org-on-heading-p))
  5550. (1- (point))
  5551. (point))))
  5552. (setq b (match-beginning 1)))
  5553. (outline-flag-region b e nil)))))))
  5554. ;; Never hide empty lines at the end of the file.
  5555. (save-excursion
  5556. (goto-char (point-max))
  5557. (outline-previous-heading)
  5558. (outline-end-of-heading)
  5559. (if (and (looking-at "[ \t\n]+")
  5560. (= (match-end 0) (point-max)))
  5561. (outline-flag-region (point) (match-end 0) nil))))
  5562. (defun org-show-empty-lines-in-parent ()
  5563. "Move to the parent and re-show empty lines before visible headlines."
  5564. (save-excursion
  5565. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5566. (org-cycle-show-empty-lines context))))
  5567. (defun org-files-list ()
  5568. "Return `org-agenda-files' list, plus all open org-mode files.
  5569. This is useful for operations that need to scan all of a user's
  5570. open and agenda-wise Org files."
  5571. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5572. (dolist (buf (buffer-list))
  5573. (with-current-buffer buf
  5574. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5575. (let ((file (expand-file-name (buffer-file-name))))
  5576. (unless (member file files)
  5577. (push file files))))))
  5578. files))
  5579. (defsubst org-entry-beginning-position ()
  5580. "Return the beginning position of the current entry."
  5581. (save-excursion (outline-back-to-heading t) (point)))
  5582. (defsubst org-entry-end-position ()
  5583. "Return the end position of the current entry."
  5584. (save-excursion (outline-next-heading) (point)))
  5585. (defun org-cycle-hide-drawers (state)
  5586. "Re-hide all drawers after a visibility state change."
  5587. (when (and (org-mode-p)
  5588. (not (memq state '(overview folded contents))))
  5589. (save-excursion
  5590. (let* ((globalp (memq state '(contents all)))
  5591. (beg (if globalp (point-min) (point)))
  5592. (end (if globalp (point-max)
  5593. (if (eq state 'children)
  5594. (save-excursion (outline-next-heading) (point))
  5595. (org-end-of-subtree t)))))
  5596. (goto-char beg)
  5597. (while (re-search-forward org-drawer-regexp end t)
  5598. (org-flag-drawer t))))))
  5599. (defun org-flag-drawer (flag)
  5600. (save-excursion
  5601. (beginning-of-line 1)
  5602. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5603. (let ((b (match-end 0))
  5604. (outline-regexp org-outline-regexp))
  5605. (if (re-search-forward
  5606. "^[ \t]*:END:"
  5607. (save-excursion (outline-next-heading) (point)) t)
  5608. (outline-flag-region b (point-at-eol) flag)
  5609. (error ":END: line missing at position %s" b))))))
  5610. (defun org-subtree-end-visible-p ()
  5611. "Is the end of the current subtree visible?"
  5612. (pos-visible-in-window-p
  5613. (save-excursion (org-end-of-subtree t) (point))))
  5614. (defun org-first-headline-recenter (&optional N)
  5615. "Move cursor to the first headline and recenter the headline.
  5616. Optional argument N means put the headline into the Nth line of the window."
  5617. (goto-char (point-min))
  5618. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5619. (beginning-of-line)
  5620. (recenter (prefix-numeric-value N))))
  5621. ;;; Saving and restoring visibility
  5622. (defun org-outline-overlay-data (&optional use-markers)
  5623. "Return a list of the locations of all outline overlays.
  5624. These are overlays with the `invisible' property value `outline'.
  5625. The return value is a list of cons cells, with start and stop
  5626. positions for each overlay.
  5627. If USE-MARKERS is set, return the positions as markers."
  5628. (let (beg end)
  5629. (save-excursion
  5630. (save-restriction
  5631. (widen)
  5632. (delq nil
  5633. (mapcar (lambda (o)
  5634. (when (eq (overlay-get o 'invisible) 'outline)
  5635. (setq beg (overlay-start o)
  5636. end (overlay-end o))
  5637. (and beg end (> end beg)
  5638. (if use-markers
  5639. (cons (move-marker (make-marker) beg)
  5640. (move-marker (make-marker) end))
  5641. (cons beg end)))))
  5642. (overlays-in (point-min) (point-max))))))))
  5643. (defun org-set-outline-overlay-data (data)
  5644. "Create visibility overlays for all positions in DATA.
  5645. DATA should have been made by `org-outline-overlay-data'."
  5646. (let (o)
  5647. (save-excursion
  5648. (save-restriction
  5649. (widen)
  5650. (show-all)
  5651. (mapc (lambda (c)
  5652. (setq o (make-overlay (car c) (cdr c)))
  5653. (overlay-put o 'invisible 'outline))
  5654. data)))))
  5655. ;;; Folding of blocks
  5656. (defconst org-block-regexp
  5657. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5658. "Regular expression for hiding blocks.")
  5659. (defvar org-hide-block-overlays nil
  5660. "Overlays hiding blocks.")
  5661. (make-variable-buffer-local 'org-hide-block-overlays)
  5662. (defun org-block-map (function &optional start end)
  5663. "Call FUNCTION at the head of all source blocks in the current buffer.
  5664. Optional arguments START and END can be used to limit the range."
  5665. (let ((start (or start (point-min)))
  5666. (end (or end (point-max))))
  5667. (save-excursion
  5668. (goto-char start)
  5669. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5670. (save-excursion
  5671. (save-match-data
  5672. (goto-char (match-beginning 0))
  5673. (funcall function)))))))
  5674. (defun org-hide-block-toggle-all ()
  5675. "Toggle the visibility of all blocks in the current buffer."
  5676. (org-block-map #'org-hide-block-toggle))
  5677. (defun org-hide-block-all ()
  5678. "Fold all blocks in the current buffer."
  5679. (interactive)
  5680. (org-show-block-all)
  5681. (org-block-map #'org-hide-block-toggle-maybe))
  5682. (defun org-show-block-all ()
  5683. "Unfold all blocks in the current buffer."
  5684. (interactive)
  5685. (mapc 'delete-overlay org-hide-block-overlays)
  5686. (setq org-hide-block-overlays nil))
  5687. (defun org-hide-block-toggle-maybe ()
  5688. "Toggle visibility of block at point."
  5689. (interactive)
  5690. (let ((case-fold-search t))
  5691. (if (save-excursion
  5692. (beginning-of-line 1)
  5693. (looking-at org-block-regexp))
  5694. (progn (org-hide-block-toggle)
  5695. t) ;; to signal that we took action
  5696. nil))) ;; to signal that we did not
  5697. (defun org-hide-block-toggle (&optional force)
  5698. "Toggle the visibility of the current block."
  5699. (interactive)
  5700. (save-excursion
  5701. (beginning-of-line)
  5702. (if (re-search-forward org-block-regexp nil t)
  5703. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5704. (end (match-end 0)) ;; end of entire body
  5705. ov)
  5706. (if (memq t (mapcar (lambda (overlay)
  5707. (eq (overlay-get overlay 'invisible)
  5708. 'org-hide-block))
  5709. (overlays-at start)))
  5710. (if (or (not force) (eq force 'off))
  5711. (mapc (lambda (ov)
  5712. (when (member ov org-hide-block-overlays)
  5713. (setq org-hide-block-overlays
  5714. (delq ov org-hide-block-overlays)))
  5715. (when (eq (overlay-get ov 'invisible)
  5716. 'org-hide-block)
  5717. (delete-overlay ov)))
  5718. (overlays-at start)))
  5719. (setq ov (make-overlay start end))
  5720. (overlay-put ov 'invisible 'org-hide-block)
  5721. ;; make the block accessible to isearch
  5722. (overlay-put
  5723. ov 'isearch-open-invisible
  5724. (lambda (ov)
  5725. (when (member ov org-hide-block-overlays)
  5726. (setq org-hide-block-overlays
  5727. (delq ov org-hide-block-overlays)))
  5728. (when (eq (overlay-get ov 'invisible)
  5729. 'org-hide-block)
  5730. (delete-overlay ov))))
  5731. (push ov org-hide-block-overlays)))
  5732. (error "Not looking at a source block"))))
  5733. ;; org-tab-after-check-for-cycling-hook
  5734. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5735. ;; Remove overlays when changing major mode
  5736. (add-hook 'org-mode-hook
  5737. (lambda () (org-add-hook 'change-major-mode-hook
  5738. 'org-show-block-all 'append 'local)))
  5739. ;;; Org-goto
  5740. (defvar org-goto-window-configuration nil)
  5741. (defvar org-goto-marker nil)
  5742. (defvar org-goto-map
  5743. (let ((map (make-sparse-keymap)))
  5744. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5745. (while (setq cmd (pop cmds))
  5746. (substitute-key-definition cmd cmd map global-map)))
  5747. (suppress-keymap map)
  5748. (org-defkey map "\C-m" 'org-goto-ret)
  5749. (org-defkey map [(return)] 'org-goto-ret)
  5750. (org-defkey map [(left)] 'org-goto-left)
  5751. (org-defkey map [(right)] 'org-goto-right)
  5752. (org-defkey map [(control ?g)] 'org-goto-quit)
  5753. (org-defkey map "\C-i" 'org-cycle)
  5754. (org-defkey map [(tab)] 'org-cycle)
  5755. (org-defkey map [(down)] 'outline-next-visible-heading)
  5756. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5757. (if org-goto-auto-isearch
  5758. (if (fboundp 'define-key-after)
  5759. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5760. nil)
  5761. (org-defkey map "q" 'org-goto-quit)
  5762. (org-defkey map "n" 'outline-next-visible-heading)
  5763. (org-defkey map "p" 'outline-previous-visible-heading)
  5764. (org-defkey map "f" 'outline-forward-same-level)
  5765. (org-defkey map "b" 'outline-backward-same-level)
  5766. (org-defkey map "u" 'outline-up-heading))
  5767. (org-defkey map "/" 'org-occur)
  5768. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5769. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5770. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5771. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5772. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5773. map))
  5774. (defconst org-goto-help
  5775. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5776. RET=jump to location [Q]uit and return to previous location
  5777. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5778. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5779. ;; FIXME: Docstring does not mention both interfaces
  5780. (defun org-goto (&optional alternative-interface)
  5781. "Look up a different location in the current file, keeping current visibility.
  5782. When you want look-up or go to a different location in a document, the
  5783. fastest way is often to fold the entire buffer and then dive into the tree.
  5784. This method has the disadvantage, that the previous location will be folded,
  5785. which may not be what you want.
  5786. This command works around this by showing a copy of the current buffer
  5787. in an indirect buffer, in overview mode. You can dive into the tree in
  5788. that copy, use org-occur and incremental search to find a location.
  5789. When pressing RET or `Q', the command returns to the original buffer in
  5790. which the visibility is still unchanged. After RET is will also jump to
  5791. the location selected in the indirect buffer and expose the
  5792. the headline hierarchy above."
  5793. (interactive "P")
  5794. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5795. (org-refile-use-outline-path t)
  5796. (org-refile-target-verify-function nil)
  5797. (interface
  5798. (if (not alternative-interface)
  5799. org-goto-interface
  5800. (if (eq org-goto-interface 'outline)
  5801. 'outline-path-completion
  5802. 'outline)))
  5803. (org-goto-start-pos (point))
  5804. (selected-point
  5805. (if (eq interface 'outline)
  5806. (car (org-get-location (current-buffer) org-goto-help))
  5807. (let ((pa (org-refile-get-location "Goto: ")))
  5808. (org-refile-check-position pa)
  5809. (nth 3 pa)))))
  5810. (if selected-point
  5811. (progn
  5812. (org-mark-ring-push org-goto-start-pos)
  5813. (goto-char selected-point)
  5814. (if (or (org-invisible-p) (org-invisible-p2))
  5815. (org-show-context 'org-goto)))
  5816. (message "Quit"))))
  5817. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5818. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5819. (defvar org-goto-local-auto-isearch-map) ; defined below
  5820. (defun org-get-location (buf help)
  5821. "Let the user select a location in the Org-mode buffer BUF.
  5822. This function uses a recursive edit. It returns the selected position
  5823. or nil."
  5824. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5825. (isearch-hide-immediately nil)
  5826. (isearch-search-fun-function
  5827. (lambda () 'org-goto-local-search-headings))
  5828. (org-goto-selected-point org-goto-exit-command)
  5829. (pop-up-frames nil)
  5830. (special-display-buffer-names nil)
  5831. (special-display-regexps nil)
  5832. (special-display-function nil))
  5833. (save-excursion
  5834. (save-window-excursion
  5835. (delete-other-windows)
  5836. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5837. (switch-to-buffer
  5838. (condition-case nil
  5839. (make-indirect-buffer (current-buffer) "*org-goto*")
  5840. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5841. (with-output-to-temp-buffer "*Help*"
  5842. (princ help))
  5843. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5844. (setq buffer-read-only nil)
  5845. (let ((org-startup-truncated t)
  5846. (org-startup-folded nil)
  5847. (org-startup-align-all-tables nil))
  5848. (org-mode)
  5849. (org-overview))
  5850. (setq buffer-read-only t)
  5851. (if (and (boundp 'org-goto-start-pos)
  5852. (integer-or-marker-p org-goto-start-pos))
  5853. (let ((org-show-hierarchy-above t)
  5854. (org-show-siblings t)
  5855. (org-show-following-heading t))
  5856. (goto-char org-goto-start-pos)
  5857. (and (org-invisible-p) (org-show-context)))
  5858. (goto-char (point-min)))
  5859. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5860. (message "Select location and press RET")
  5861. (use-local-map org-goto-map)
  5862. (recursive-edit)
  5863. ))
  5864. (kill-buffer "*org-goto*")
  5865. (cons org-goto-selected-point org-goto-exit-command)))
  5866. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5867. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5868. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5869. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5870. (defun org-goto-local-search-headings (string bound noerror)
  5871. "Search and make sure that any matches are in headlines."
  5872. (catch 'return
  5873. (while (if isearch-forward
  5874. (search-forward string bound noerror)
  5875. (search-backward string bound noerror))
  5876. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5877. (and (member :headline context)
  5878. (not (member :tags context))))
  5879. (throw 'return (point))))))
  5880. (defun org-goto-local-auto-isearch ()
  5881. "Start isearch."
  5882. (interactive)
  5883. (goto-char (point-min))
  5884. (let ((keys (this-command-keys)))
  5885. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5886. (isearch-mode t)
  5887. (isearch-process-search-char (string-to-char keys)))))
  5888. (defun org-goto-ret (&optional arg)
  5889. "Finish `org-goto' by going to the new location."
  5890. (interactive "P")
  5891. (setq org-goto-selected-point (point)
  5892. org-goto-exit-command 'return)
  5893. (throw 'exit nil))
  5894. (defun org-goto-left ()
  5895. "Finish `org-goto' by going to the new location."
  5896. (interactive)
  5897. (if (org-on-heading-p)
  5898. (progn
  5899. (beginning-of-line 1)
  5900. (setq org-goto-selected-point (point)
  5901. org-goto-exit-command 'left)
  5902. (throw 'exit nil))
  5903. (error "Not on a heading")))
  5904. (defun org-goto-right ()
  5905. "Finish `org-goto' by going to the new location."
  5906. (interactive)
  5907. (if (org-on-heading-p)
  5908. (progn
  5909. (setq org-goto-selected-point (point)
  5910. org-goto-exit-command 'right)
  5911. (throw 'exit nil))
  5912. (error "Not on a heading")))
  5913. (defun org-goto-quit ()
  5914. "Finish `org-goto' without cursor motion."
  5915. (interactive)
  5916. (setq org-goto-selected-point nil)
  5917. (setq org-goto-exit-command 'quit)
  5918. (throw 'exit nil))
  5919. ;;; Indirect buffer display of subtrees
  5920. (defvar org-indirect-dedicated-frame nil
  5921. "This is the frame being used for indirect tree display.")
  5922. (defvar org-last-indirect-buffer nil)
  5923. (defun org-tree-to-indirect-buffer (&optional arg)
  5924. "Create indirect buffer and narrow it to current subtree.
  5925. With numerical prefix ARG, go up to this level and then take that tree.
  5926. If ARG is negative, go up that many levels.
  5927. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5928. indirect buffer previously made with this command, to avoid proliferation of
  5929. indirect buffers. However, when you call the command with a \
  5930. \\[universal-argument] prefix, or
  5931. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5932. is kept so that you can work with several indirect buffers at the same time.
  5933. If `org-indirect-buffer-display' is `dedicated-frame', the \
  5934. \\[universal-argument] prefix also
  5935. requests that a new frame be made for the new buffer, so that the dedicated
  5936. frame is not changed."
  5937. (interactive "P")
  5938. (let ((cbuf (current-buffer))
  5939. (cwin (selected-window))
  5940. (pos (point))
  5941. beg end level heading ibuf)
  5942. (save-excursion
  5943. (org-back-to-heading t)
  5944. (when (numberp arg)
  5945. (setq level (org-outline-level))
  5946. (if (< arg 0) (setq arg (+ level arg)))
  5947. (while (> (setq level (org-outline-level)) arg)
  5948. (outline-up-heading 1 t)))
  5949. (setq beg (point)
  5950. heading (org-get-heading))
  5951. (org-end-of-subtree t t)
  5952. (if (org-on-heading-p) (backward-char 1))
  5953. (setq end (point)))
  5954. (if (and (buffer-live-p org-last-indirect-buffer)
  5955. (not (eq org-indirect-buffer-display 'new-frame))
  5956. (not arg))
  5957. (kill-buffer org-last-indirect-buffer))
  5958. (setq ibuf (org-get-indirect-buffer cbuf)
  5959. org-last-indirect-buffer ibuf)
  5960. (cond
  5961. ((or (eq org-indirect-buffer-display 'new-frame)
  5962. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5963. (select-frame (make-frame))
  5964. (delete-other-windows)
  5965. (switch-to-buffer ibuf)
  5966. (org-set-frame-title heading))
  5967. ((eq org-indirect-buffer-display 'dedicated-frame)
  5968. (raise-frame
  5969. (select-frame (or (and org-indirect-dedicated-frame
  5970. (frame-live-p org-indirect-dedicated-frame)
  5971. org-indirect-dedicated-frame)
  5972. (setq org-indirect-dedicated-frame (make-frame)))))
  5973. (delete-other-windows)
  5974. (switch-to-buffer ibuf)
  5975. (org-set-frame-title (concat "Indirect: " heading)))
  5976. ((eq org-indirect-buffer-display 'current-window)
  5977. (switch-to-buffer ibuf))
  5978. ((eq org-indirect-buffer-display 'other-window)
  5979. (pop-to-buffer ibuf))
  5980. (t (error "Invalid value")))
  5981. (if (featurep 'xemacs)
  5982. (save-excursion (org-mode) (turn-on-font-lock)))
  5983. (narrow-to-region beg end)
  5984. (show-all)
  5985. (goto-char pos)
  5986. (and (window-live-p cwin) (select-window cwin))))
  5987. (defun org-get-indirect-buffer (&optional buffer)
  5988. (setq buffer (or buffer (current-buffer)))
  5989. (let ((n 1) (base (buffer-name buffer)) bname)
  5990. (while (buffer-live-p
  5991. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5992. (setq n (1+ n)))
  5993. (condition-case nil
  5994. (make-indirect-buffer buffer bname 'clone)
  5995. (error (make-indirect-buffer buffer bname)))))
  5996. (defun org-set-frame-title (title)
  5997. "Set the title of the current frame to the string TITLE."
  5998. ;; FIXME: how to name a single frame in XEmacs???
  5999. (unless (featurep 'xemacs)
  6000. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6001. ;;;; Structure editing
  6002. ;;; Inserting headlines
  6003. (defun org-previous-line-empty-p ()
  6004. (save-excursion
  6005. (and (not (bobp))
  6006. (or (beginning-of-line 0) t)
  6007. (save-match-data
  6008. (looking-at "[ \t]*$")))))
  6009. (defun org-insert-heading (&optional force-heading invisible-ok)
  6010. "Insert a new heading or item with same depth at point.
  6011. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6012. If point is at the beginning of a headline, insert a sibling before the
  6013. current headline. If point is not at the beginning, split the line,
  6014. create the new headline with the text in the current line after point
  6015. \(but see also the variable `org-M-RET-may-split-line').
  6016. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6017. This is important for non-interactive uses of the command."
  6018. (interactive "P")
  6019. (if (or (= (buffer-size) 0)
  6020. (and (not (save-excursion
  6021. (and (ignore-errors (org-back-to-heading invisible-ok))
  6022. (org-on-heading-p))))
  6023. (not (org-in-item-p))))
  6024. (progn
  6025. (insert "\n* ")
  6026. (run-hooks 'org-insert-heading-hook))
  6027. (when (or force-heading (not (org-insert-item)))
  6028. (let* ((empty-line-p nil)
  6029. (level nil)
  6030. (on-heading (org-on-heading-p))
  6031. (head (save-excursion
  6032. (condition-case nil
  6033. (progn
  6034. (org-back-to-heading invisible-ok)
  6035. (when (and (not on-heading)
  6036. (featurep 'org-inlinetask)
  6037. (integerp org-inlinetask-min-level)
  6038. (>= (length (match-string 0))
  6039. org-inlinetask-min-level))
  6040. ;; Find a heading level before the inline task
  6041. (while (and (setq level (org-up-heading-safe))
  6042. (>= level org-inlinetask-min-level)))
  6043. (if (org-on-heading-p)
  6044. (org-back-to-heading invisible-ok)
  6045. (error "This should not happen")))
  6046. (setq empty-line-p (org-previous-line-empty-p))
  6047. (match-string 0))
  6048. (error "*"))))
  6049. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6050. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6051. pos hide-previous previous-pos)
  6052. (cond
  6053. ((and (org-on-heading-p) (bolp)
  6054. (or (bobp)
  6055. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  6056. ;; insert before the current line
  6057. (open-line (if blank 2 1)))
  6058. ((and (bolp)
  6059. (not org-insert-heading-respect-content)
  6060. (or (bobp)
  6061. (save-excursion
  6062. (backward-char 1) (not (org-invisible-p)))))
  6063. ;; insert right here
  6064. nil)
  6065. (t
  6066. ;; somewhere in the line
  6067. (save-excursion
  6068. (setq previous-pos (point-at-bol))
  6069. (end-of-line)
  6070. (setq hide-previous (org-invisible-p)))
  6071. (and org-insert-heading-respect-content (org-show-subtree))
  6072. (let ((split
  6073. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6074. (save-excursion
  6075. (let ((p (point)))
  6076. (goto-char (point-at-bol))
  6077. (and (looking-at org-complex-heading-regexp)
  6078. (> p (match-beginning 4)))))))
  6079. tags pos)
  6080. (cond
  6081. (org-insert-heading-respect-content
  6082. (org-end-of-subtree nil t)
  6083. (when (featurep 'org-inlinetask)
  6084. (while (and (not (eobp))
  6085. (looking-at "\\(\\*+\\)[ \t]+")
  6086. (>= (length (match-string 1))
  6087. org-inlinetask-min-level))
  6088. (org-end-of-subtree nil t)))
  6089. (or (bolp) (newline))
  6090. (or (org-previous-line-empty-p)
  6091. (and blank (newline)))
  6092. (open-line 1))
  6093. ((org-on-heading-p)
  6094. (when hide-previous
  6095. (show-children)
  6096. (org-show-entry))
  6097. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6098. (setq tags (and (match-end 2) (match-string 2)))
  6099. (and (match-end 1)
  6100. (delete-region (match-beginning 1) (match-end 1)))
  6101. (setq pos (point-at-bol))
  6102. (or split (end-of-line 1))
  6103. (delete-horizontal-space)
  6104. (if (string-match "\\`\\*+\\'"
  6105. (buffer-substring (point-at-bol) (point)))
  6106. (insert " "))
  6107. (newline (if blank 2 1))
  6108. (when tags
  6109. (save-excursion
  6110. (goto-char pos)
  6111. (end-of-line 1)
  6112. (insert " " tags)
  6113. (org-set-tags nil 'align))))
  6114. (t
  6115. (or split (end-of-line 1))
  6116. (newline (if blank 2 1)))))))
  6117. (insert head) (just-one-space)
  6118. (setq pos (point))
  6119. (end-of-line 1)
  6120. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6121. (when (and org-insert-heading-respect-content hide-previous)
  6122. (save-excursion
  6123. (goto-char previous-pos)
  6124. (hide-subtree)))
  6125. (run-hooks 'org-insert-heading-hook)))))
  6126. (defun org-get-heading (&optional no-tags)
  6127. "Return the heading of the current entry, without the stars."
  6128. (save-excursion
  6129. (org-back-to-heading t)
  6130. (if (looking-at
  6131. (if no-tags
  6132. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6133. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6134. (match-string 1) "")))
  6135. (defun org-heading-components ()
  6136. "Return the components of the current heading.
  6137. This is a list with the following elements:
  6138. - the level as an integer
  6139. - the reduced level, different if `org-odd-levels-only' is set.
  6140. - the TODO keyword, or nil
  6141. - the priority character, like ?A, or nil if no priority is given
  6142. - the headline text itself, or the tags string if no headline text
  6143. - the tags string, or nil."
  6144. (save-excursion
  6145. (org-back-to-heading t)
  6146. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6147. (list (length (match-string 1))
  6148. (org-reduced-level (length (match-string 1)))
  6149. (org-match-string-no-properties 2)
  6150. (and (match-end 3) (aref (match-string 3) 2))
  6151. (org-match-string-no-properties 4)
  6152. (org-match-string-no-properties 5)))))
  6153. (defun org-get-entry ()
  6154. "Get the entry text, after heading, entire subtree."
  6155. (save-excursion
  6156. (org-back-to-heading t)
  6157. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6158. (defun org-insert-heading-after-current ()
  6159. "Insert a new heading with same level as current, after current subtree."
  6160. (interactive)
  6161. (org-back-to-heading)
  6162. (org-insert-heading)
  6163. (org-move-subtree-down)
  6164. (end-of-line 1))
  6165. (defun org-insert-heading-respect-content ()
  6166. (interactive)
  6167. (let ((org-insert-heading-respect-content t))
  6168. (org-insert-heading t)))
  6169. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6170. (interactive "P")
  6171. (let ((org-insert-heading-respect-content t))
  6172. (org-insert-todo-heading force-state t)))
  6173. (defun org-insert-todo-heading (arg &optional force-heading)
  6174. "Insert a new heading with the same level and TODO state as current heading.
  6175. If the heading has no TODO state, or if the state is DONE, use the first
  6176. state (TODO by default). Also with prefix arg, force first state."
  6177. (interactive "P")
  6178. (when (or force-heading (not (org-insert-item 'checkbox)))
  6179. (org-insert-heading force-heading)
  6180. (save-excursion
  6181. (org-back-to-heading)
  6182. (outline-previous-heading)
  6183. (looking-at org-todo-line-regexp))
  6184. (let*
  6185. ((new-mark-x
  6186. (if (or arg
  6187. (not (match-beginning 2))
  6188. (member (match-string 2) org-done-keywords))
  6189. (car org-todo-keywords-1)
  6190. (match-string 2)))
  6191. (new-mark
  6192. (or
  6193. (run-hook-with-args-until-success
  6194. 'org-todo-get-default-hook new-mark-x nil)
  6195. new-mark-x)))
  6196. (beginning-of-line 1)
  6197. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6198. (if org-treat-insert-todo-heading-as-state-change
  6199. (org-todo new-mark)
  6200. (insert new-mark " "))))
  6201. (when org-provide-todo-statistics
  6202. (org-update-parent-todo-statistics))))
  6203. (defun org-insert-subheading (arg)
  6204. "Insert a new subheading and demote it.
  6205. Works for outline headings and for plain lists alike."
  6206. (interactive "P")
  6207. (org-insert-heading arg)
  6208. (cond
  6209. ((org-on-heading-p) (org-do-demote))
  6210. ((org-at-item-p) (org-indent-item))))
  6211. (defun org-insert-todo-subheading (arg)
  6212. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6213. Works for outline headings and for plain lists alike."
  6214. (interactive "P")
  6215. (org-insert-todo-heading arg)
  6216. (cond
  6217. ((org-on-heading-p) (org-do-demote))
  6218. ((org-at-item-p) (org-indent-item))))
  6219. ;;; Promotion and Demotion
  6220. (defvar org-after-demote-entry-hook nil
  6221. "Hook run after an entry has been demoted.
  6222. The cursor will be at the beginning of the entry.
  6223. When a subtree is being demoted, the hook will be called for each node.")
  6224. (defvar org-after-promote-entry-hook nil
  6225. "Hook run after an entry has been promoted.
  6226. The cursor will be at the beginning of the entry.
  6227. When a subtree is being promoted, the hook will be called for each node.")
  6228. (defun org-promote-subtree ()
  6229. "Promote the entire subtree.
  6230. See also `org-promote'."
  6231. (interactive)
  6232. (save-excursion
  6233. (org-map-tree 'org-promote))
  6234. (org-fix-position-after-promote))
  6235. (defun org-demote-subtree ()
  6236. "Demote the entire subtree. See `org-demote'.
  6237. See also `org-promote'."
  6238. (interactive)
  6239. (save-excursion
  6240. (org-map-tree 'org-demote))
  6241. (org-fix-position-after-promote))
  6242. (defun org-do-promote ()
  6243. "Promote the current heading higher up the tree.
  6244. If the region is active in `transient-mark-mode', promote all headings
  6245. in the region."
  6246. (interactive)
  6247. (save-excursion
  6248. (if (org-region-active-p)
  6249. (org-map-region 'org-promote (region-beginning) (region-end))
  6250. (org-promote)))
  6251. (org-fix-position-after-promote))
  6252. (defun org-do-demote ()
  6253. "Demote the current heading lower down the tree.
  6254. If the region is active in `transient-mark-mode', demote all headings
  6255. in the region."
  6256. (interactive)
  6257. (save-excursion
  6258. (if (org-region-active-p)
  6259. (org-map-region 'org-demote (region-beginning) (region-end))
  6260. (org-demote)))
  6261. (org-fix-position-after-promote))
  6262. (defun org-fix-position-after-promote ()
  6263. "Make sure that after pro/demotion cursor position is right."
  6264. (let ((pos (point)))
  6265. (when (save-excursion
  6266. (beginning-of-line 1)
  6267. (looking-at org-todo-line-regexp)
  6268. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6269. (cond ((eobp) (insert " "))
  6270. ((eolp) (insert " "))
  6271. ((equal (char-after) ?\ ) (forward-char 1))))))
  6272. (defun org-current-level ()
  6273. "Return the level of the current entry, or nil if before the first headline.
  6274. The level is the number of stars at the beginning of the headline."
  6275. (save-excursion
  6276. (condition-case nil
  6277. (progn
  6278. (org-back-to-heading t)
  6279. (funcall outline-level))
  6280. (error nil))))
  6281. (defun org-get-previous-line-level ()
  6282. "Return the outline depth of the last headline before the current line.
  6283. Returns 0 for the first headline in the buffer, and nil if before the
  6284. first headline."
  6285. (let ((current-level (org-current-level))
  6286. (prev-level (when (> (line-number-at-pos) 1)
  6287. (save-excursion
  6288. (beginning-of-line 0)
  6289. (org-current-level)))))
  6290. (cond ((null current-level) nil) ; Before first headline
  6291. ((null prev-level) 0) ; At first headline
  6292. (prev-level))))
  6293. (defun org-reduced-level (l)
  6294. "Compute the effective level of a heading.
  6295. This takes into account the setting of `org-odd-levels-only'."
  6296. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6297. (defun org-level-increment ()
  6298. "Return the number of stars that will be added or removed at a
  6299. time to headlines when structure editing, based on the value of
  6300. `org-odd-levels-only'."
  6301. (if org-odd-levels-only 2 1))
  6302. (defun org-get-valid-level (level &optional change)
  6303. "Rectify a level change under the influence of `org-odd-levels-only'
  6304. LEVEL is a current level, CHANGE is by how much the level should be
  6305. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6306. even level numbers will become the next higher odd number."
  6307. (if org-odd-levels-only
  6308. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6309. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6310. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6311. (max 1 (+ level (or change 0)))))
  6312. (if (boundp 'define-obsolete-function-alias)
  6313. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6314. (define-obsolete-function-alias 'org-get-legal-level
  6315. 'org-get-valid-level)
  6316. (define-obsolete-function-alias 'org-get-legal-level
  6317. 'org-get-valid-level "23.1")))
  6318. (defun org-promote ()
  6319. "Promote the current heading higher up the tree.
  6320. If the region is active in `transient-mark-mode', promote all headings
  6321. in the region."
  6322. (org-back-to-heading t)
  6323. (let* ((level (save-match-data (funcall outline-level)))
  6324. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6325. (diff (abs (- level (length up-head) -1))))
  6326. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6327. (replace-match up-head nil t)
  6328. ;; Fixup tag positioning
  6329. (and org-auto-align-tags (org-set-tags nil t))
  6330. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6331. (run-hooks 'org-after-promote-entry-hook)))
  6332. (defun org-demote ()
  6333. "Demote the current heading lower down the tree.
  6334. If the region is active in `transient-mark-mode', demote all headings
  6335. in the region."
  6336. (org-back-to-heading t)
  6337. (let* ((level (save-match-data (funcall outline-level)))
  6338. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6339. (diff (abs (- level (length down-head) -1))))
  6340. (replace-match down-head nil t)
  6341. ;; Fixup tag positioning
  6342. (and org-auto-align-tags (org-set-tags nil t))
  6343. (if org-adapt-indentation (org-fixup-indentation diff))
  6344. (run-hooks 'org-after-demote-entry-hook)))
  6345. (defun org-cycle-level ()
  6346. "Cycle the level of an empty headline through possible states.
  6347. This goes first to child, then to parent, level, then up the hierarchy.
  6348. After top level, it switches back to sibling level."
  6349. (interactive)
  6350. (let ((org-adapt-indentation nil))
  6351. (when (org-point-at-end-of-empty-headline)
  6352. (setq this-command 'org-cycle-level) ; Only needed for caching
  6353. (let ((cur-level (org-current-level))
  6354. (prev-level (org-get-previous-line-level)))
  6355. (cond
  6356. ;; If first headline in file, promote to top-level.
  6357. ((= prev-level 0)
  6358. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6359. do (org-do-promote)))
  6360. ;; If same level as prev, demote one.
  6361. ((= prev-level cur-level)
  6362. (org-do-demote))
  6363. ;; If parent is top-level, promote to top level if not already.
  6364. ((= prev-level 1)
  6365. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6366. do (org-do-promote)))
  6367. ;; If top-level, return to prev-level.
  6368. ((= cur-level 1)
  6369. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6370. do (org-do-demote)))
  6371. ;; If less than prev-level, promote one.
  6372. ((< cur-level prev-level)
  6373. (org-do-promote))
  6374. ;; If deeper than prev-level, promote until higher than
  6375. ;; prev-level.
  6376. ((> cur-level prev-level)
  6377. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6378. do (org-do-promote))))
  6379. t))))
  6380. (defun org-map-tree (fun)
  6381. "Call FUN for every heading underneath the current one."
  6382. (org-back-to-heading)
  6383. (let ((level (funcall outline-level)))
  6384. (save-excursion
  6385. (funcall fun)
  6386. (while (and (progn
  6387. (outline-next-heading)
  6388. (> (funcall outline-level) level))
  6389. (not (eobp)))
  6390. (funcall fun)))))
  6391. (defun org-map-region (fun beg end)
  6392. "Call FUN for every heading between BEG and END."
  6393. (let ((org-ignore-region t))
  6394. (save-excursion
  6395. (setq end (copy-marker end))
  6396. (goto-char beg)
  6397. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6398. (< (point) end))
  6399. (funcall fun))
  6400. (while (and (progn
  6401. (outline-next-heading)
  6402. (< (point) end))
  6403. (not (eobp)))
  6404. (funcall fun)))))
  6405. (defun org-fixup-indentation (diff)
  6406. "Change the indentation in the current entry by DIFF.
  6407. However, if any line in the current entry has no indentation, or if it
  6408. would end up with no indentation after the change, nothing at all is done."
  6409. (save-excursion
  6410. (let ((end (save-excursion (outline-next-heading)
  6411. (point-marker)))
  6412. (prohibit (if (> diff 0)
  6413. "^\\S-"
  6414. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6415. col)
  6416. (unless (save-excursion (end-of-line 1)
  6417. (re-search-forward prohibit end t))
  6418. (while (and (< (point) end)
  6419. (re-search-forward "^[ \t]+" end t))
  6420. (goto-char (match-end 0))
  6421. (setq col (current-column))
  6422. (if (< diff 0) (replace-match ""))
  6423. (org-indent-to-column (+ diff col))))
  6424. (move-marker end nil))))
  6425. (defun org-convert-to-odd-levels ()
  6426. "Convert an org-mode file with all levels allowed to one with odd levels.
  6427. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6428. level 5 etc."
  6429. (interactive)
  6430. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6431. (let ((outline-regexp org-outline-regexp)
  6432. (outline-level 'org-outline-level)
  6433. (org-odd-levels-only nil) n)
  6434. (save-excursion
  6435. (goto-char (point-min))
  6436. (while (re-search-forward "^\\*\\*+ " nil t)
  6437. (setq n (- (length (match-string 0)) 2))
  6438. (while (>= (setq n (1- n)) 0)
  6439. (org-demote))
  6440. (end-of-line 1))))))
  6441. (defun org-convert-to-oddeven-levels ()
  6442. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6443. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6444. file contains a section with an even level, conversion would
  6445. destroy the structure of the file. An error is signaled in this
  6446. case."
  6447. (interactive)
  6448. (goto-char (point-min))
  6449. ;; First check if there are no even levels
  6450. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6451. (org-show-context t)
  6452. (error "Not all levels are odd in this file. Conversion not possible"))
  6453. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6454. (let ((outline-regexp org-outline-regexp)
  6455. (outline-level 'org-outline-level)
  6456. (org-odd-levels-only nil) n)
  6457. (save-excursion
  6458. (goto-char (point-min))
  6459. (while (re-search-forward "^\\*\\*+ " nil t)
  6460. (setq n (/ (1- (length (match-string 0))) 2))
  6461. (while (>= (setq n (1- n)) 0)
  6462. (org-promote))
  6463. (end-of-line 1))))))
  6464. (defun org-tr-level (n)
  6465. "Make N odd if required."
  6466. (if org-odd-levels-only (1+ (/ n 2)) n))
  6467. ;;; Vertical tree motion, cutting and pasting of subtrees
  6468. (defun org-move-subtree-up (&optional arg)
  6469. "Move the current subtree up past ARG headlines of the same level."
  6470. (interactive "p")
  6471. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6472. (defun org-move-subtree-down (&optional arg)
  6473. "Move the current subtree down past ARG headlines of the same level."
  6474. (interactive "p")
  6475. (setq arg (prefix-numeric-value arg))
  6476. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6477. 'org-get-last-sibling))
  6478. (ins-point (make-marker))
  6479. (cnt (abs arg))
  6480. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6481. ;; Select the tree
  6482. (org-back-to-heading)
  6483. (setq beg0 (point))
  6484. (save-excursion
  6485. (setq ne-beg (org-back-over-empty-lines))
  6486. (setq beg (point)))
  6487. (save-match-data
  6488. (save-excursion (outline-end-of-heading)
  6489. (setq folded (org-invisible-p)))
  6490. (outline-end-of-subtree))
  6491. (outline-next-heading)
  6492. (setq ne-end (org-back-over-empty-lines))
  6493. (setq end (point))
  6494. (goto-char beg0)
  6495. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6496. ;; include less whitespace
  6497. (save-excursion
  6498. (goto-char beg)
  6499. (forward-line (- ne-beg ne-end))
  6500. (setq beg (point))))
  6501. ;; Find insertion point, with error handling
  6502. (while (> cnt 0)
  6503. (or (and (funcall movfunc) (looking-at outline-regexp))
  6504. (progn (goto-char beg0)
  6505. (error "Cannot move past superior level or buffer limit")))
  6506. (setq cnt (1- cnt)))
  6507. (if (> arg 0)
  6508. ;; Moving forward - still need to move over subtree
  6509. (progn (org-end-of-subtree t t)
  6510. (save-excursion
  6511. (org-back-over-empty-lines)
  6512. (or (bolp) (newline)))))
  6513. (setq ne-ins (org-back-over-empty-lines))
  6514. (move-marker ins-point (point))
  6515. (setq txt (buffer-substring beg end))
  6516. (org-save-markers-in-region beg end)
  6517. (delete-region beg end)
  6518. (org-remove-empty-overlays-at beg)
  6519. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6520. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6521. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6522. (let ((bbb (point)))
  6523. (insert-before-markers txt)
  6524. (org-reinstall-markers-in-region bbb)
  6525. (move-marker ins-point bbb))
  6526. (or (bolp) (insert "\n"))
  6527. (setq ins-end (point))
  6528. (goto-char ins-point)
  6529. (org-skip-whitespace)
  6530. (when (and (< arg 0)
  6531. (org-first-sibling-p)
  6532. (> ne-ins ne-beg))
  6533. ;; Move whitespace back to beginning
  6534. (save-excursion
  6535. (goto-char ins-end)
  6536. (let ((kill-whole-line t))
  6537. (kill-line (- ne-ins ne-beg)) (point)))
  6538. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6539. (move-marker ins-point nil)
  6540. (if folded
  6541. (hide-subtree)
  6542. (org-show-entry)
  6543. (show-children)
  6544. (org-cycle-hide-drawers 'children))
  6545. (org-clean-visibility-after-subtree-move)))
  6546. (defvar org-subtree-clip ""
  6547. "Clipboard for cut and paste of subtrees.
  6548. This is actually only a copy of the kill, because we use the normal kill
  6549. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6550. (defvar org-subtree-clip-folded nil
  6551. "Was the last copied subtree folded?
  6552. This is used to fold the tree back after pasting.")
  6553. (defun org-cut-subtree (&optional n)
  6554. "Cut the current subtree into the clipboard.
  6555. With prefix arg N, cut this many sequential subtrees.
  6556. This is a short-hand for marking the subtree and then cutting it."
  6557. (interactive "p")
  6558. (org-copy-subtree n 'cut))
  6559. (defun org-copy-subtree (&optional n cut force-store-markers)
  6560. "Cut the current subtree into the clipboard.
  6561. With prefix arg N, cut this many sequential subtrees.
  6562. This is a short-hand for marking the subtree and then copying it.
  6563. If CUT is non-nil, actually cut the subtree.
  6564. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6565. of some markers in the region, even if CUT is non-nil. This is
  6566. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6567. (interactive "p")
  6568. (let (beg end folded (beg0 (point)))
  6569. (if (interactive-p)
  6570. (org-back-to-heading nil) ; take what looks like a subtree
  6571. (org-back-to-heading t)) ; take what is really there
  6572. (org-back-over-empty-lines)
  6573. (setq beg (point))
  6574. (skip-chars-forward " \t\r\n")
  6575. (save-match-data
  6576. (save-excursion (outline-end-of-heading)
  6577. (setq folded (org-invisible-p)))
  6578. (condition-case nil
  6579. (org-forward-same-level (1- n) t)
  6580. (error nil))
  6581. (org-end-of-subtree t t))
  6582. (org-back-over-empty-lines)
  6583. (setq end (point))
  6584. (goto-char beg0)
  6585. (when (> end beg)
  6586. (setq org-subtree-clip-folded folded)
  6587. (when (or cut force-store-markers)
  6588. (org-save-markers-in-region beg end))
  6589. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6590. (setq org-subtree-clip (current-kill 0))
  6591. (message "%s: Subtree(s) with %d characters"
  6592. (if cut "Cut" "Copied")
  6593. (length org-subtree-clip)))))
  6594. (defun org-paste-subtree (&optional level tree for-yank)
  6595. "Paste the clipboard as a subtree, with modification of headline level.
  6596. The entire subtree is promoted or demoted in order to match a new headline
  6597. level.
  6598. If the cursor is at the beginning of a headline, the same level as
  6599. that headline is used to paste the tree
  6600. If not, the new level is derived from the *visible* headings
  6601. before and after the insertion point, and taken to be the inferior headline
  6602. level of the two. So if the previous visible heading is level 3 and the
  6603. next is level 4 (or vice versa), level 4 will be used for insertion.
  6604. This makes sure that the subtree remains an independent subtree and does
  6605. not swallow low level entries.
  6606. You can also force a different level, either by using a numeric prefix
  6607. argument, or by inserting the heading marker by hand. For example, if the
  6608. cursor is after \"*****\", then the tree will be shifted to level 5.
  6609. If optional TREE is given, use this text instead of the kill ring.
  6610. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6611. move back over whitespace before inserting, and move point to the end of
  6612. the inserted text when done."
  6613. (interactive "P")
  6614. (setq tree (or tree (and kill-ring (current-kill 0))))
  6615. (unless (org-kill-is-subtree-p tree)
  6616. (error "%s"
  6617. (substitute-command-keys
  6618. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6619. (let* ((visp (not (org-invisible-p)))
  6620. (txt tree)
  6621. (^re (concat "^\\(" outline-regexp "\\)"))
  6622. (re (concat "\\(" outline-regexp "\\)"))
  6623. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6624. (old-level (if (string-match ^re txt)
  6625. (- (match-end 0) (match-beginning 0) 1)
  6626. -1))
  6627. (force-level (cond (level (prefix-numeric-value level))
  6628. ((and (looking-at "[ \t]*$")
  6629. (string-match
  6630. ^re_ (buffer-substring
  6631. (point-at-bol) (point))))
  6632. (- (match-end 1) (match-beginning 1)))
  6633. ((and (bolp)
  6634. (looking-at org-outline-regexp))
  6635. (- (match-end 0) (point) 1))
  6636. (t nil)))
  6637. (previous-level (save-excursion
  6638. (condition-case nil
  6639. (progn
  6640. (outline-previous-visible-heading 1)
  6641. (if (looking-at re)
  6642. (- (match-end 0) (match-beginning 0) 1)
  6643. 1))
  6644. (error 1))))
  6645. (next-level (save-excursion
  6646. (condition-case nil
  6647. (progn
  6648. (or (looking-at outline-regexp)
  6649. (outline-next-visible-heading 1))
  6650. (if (looking-at re)
  6651. (- (match-end 0) (match-beginning 0) 1)
  6652. 1))
  6653. (error 1))))
  6654. (new-level (or force-level (max previous-level next-level)))
  6655. (shift (if (or (= old-level -1)
  6656. (= new-level -1)
  6657. (= old-level new-level))
  6658. 0
  6659. (- new-level old-level)))
  6660. (delta (if (> shift 0) -1 1))
  6661. (func (if (> shift 0) 'org-demote 'org-promote))
  6662. (org-odd-levels-only nil)
  6663. beg end newend)
  6664. ;; Remove the forced level indicator
  6665. (if force-level
  6666. (delete-region (point-at-bol) (point)))
  6667. ;; Paste
  6668. (beginning-of-line 1)
  6669. (unless for-yank (org-back-over-empty-lines))
  6670. (setq beg (point))
  6671. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6672. (insert-before-markers txt)
  6673. (unless (string-match "\n\\'" txt) (insert "\n"))
  6674. (setq newend (point))
  6675. (org-reinstall-markers-in-region beg)
  6676. (setq end (point))
  6677. (goto-char beg)
  6678. (skip-chars-forward " \t\n\r")
  6679. (setq beg (point))
  6680. (if (and (org-invisible-p) visp)
  6681. (save-excursion (outline-show-heading)))
  6682. ;; Shift if necessary
  6683. (unless (= shift 0)
  6684. (save-restriction
  6685. (narrow-to-region beg end)
  6686. (while (not (= shift 0))
  6687. (org-map-region func (point-min) (point-max))
  6688. (setq shift (+ delta shift)))
  6689. (goto-char (point-min))
  6690. (setq newend (point-max))))
  6691. (when (or (interactive-p) for-yank)
  6692. (message "Clipboard pasted as level %d subtree" new-level))
  6693. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6694. kill-ring
  6695. (eq org-subtree-clip (current-kill 0))
  6696. org-subtree-clip-folded)
  6697. ;; The tree was folded before it was killed/copied
  6698. (hide-subtree))
  6699. (and for-yank (goto-char newend))))
  6700. (defun org-kill-is-subtree-p (&optional txt)
  6701. "Check if the current kill is an outline subtree, or a set of trees.
  6702. Returns nil if kill does not start with a headline, or if the first
  6703. headline level is not the largest headline level in the tree.
  6704. So this will actually accept several entries of equal levels as well,
  6705. which is OK for `org-paste-subtree'.
  6706. If optional TXT is given, check this string instead of the current kill."
  6707. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6708. (start-level (and kill
  6709. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6710. org-outline-regexp "\\)")
  6711. kill)
  6712. (- (match-end 2) (match-beginning 2) 1)))
  6713. (re (concat "^" org-outline-regexp))
  6714. (start (1+ (or (match-beginning 2) -1))))
  6715. (if (not start-level)
  6716. (progn
  6717. nil) ;; does not even start with a heading
  6718. (catch 'exit
  6719. (while (setq start (string-match re kill (1+ start)))
  6720. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6721. (throw 'exit nil)))
  6722. t))))
  6723. (defvar org-markers-to-move nil
  6724. "Markers that should be moved with a cut-and-paste operation.
  6725. Those markers are stored together with their positions relative to
  6726. the start of the region.")
  6727. (defun org-save-markers-in-region (beg end)
  6728. "Check markers in region.
  6729. If these markers are between BEG and END, record their position relative
  6730. to BEG, so that after moving the block of text, we can put the markers back
  6731. into place.
  6732. This function gets called just before an entry or tree gets cut from the
  6733. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6734. called immediately, to move the markers with the entries."
  6735. (setq org-markers-to-move nil)
  6736. (when (featurep 'org-clock)
  6737. (org-clock-save-markers-for-cut-and-paste beg end))
  6738. (when (featurep 'org-agenda)
  6739. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6740. (defun org-check-and-save-marker (marker beg end)
  6741. "Check if MARKER is between BEG and END.
  6742. If yes, remember the marker and the distance to BEG."
  6743. (when (and (marker-buffer marker)
  6744. (equal (marker-buffer marker) (current-buffer)))
  6745. (if (and (>= marker beg) (< marker end))
  6746. (push (cons marker (- marker beg)) org-markers-to-move))))
  6747. (defun org-reinstall-markers-in-region (beg)
  6748. "Move all remembered markers to their position relative to BEG."
  6749. (mapc (lambda (x)
  6750. (move-marker (car x) (+ beg (cdr x))))
  6751. org-markers-to-move)
  6752. (setq org-markers-to-move nil))
  6753. (defun org-narrow-to-subtree ()
  6754. "Narrow buffer to the current subtree."
  6755. (interactive)
  6756. (save-excursion
  6757. (save-match-data
  6758. (narrow-to-region
  6759. (progn (org-back-to-heading t) (point))
  6760. (progn (org-end-of-subtree t t)
  6761. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6762. (point))))))
  6763. (eval-when-compile
  6764. (defvar org-property-drawer-re))
  6765. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6766. "Clone the task (subtree) at point N times.
  6767. The clones will be inserted as siblings.
  6768. In interactive use, the user will be prompted for the number of
  6769. clones to be produced, and for a time SHIFT, which may be a
  6770. repeater as used in time stamps, for example `+3d'.
  6771. When a valid repeater is given and the entry contains any time
  6772. stamps, the clones will become a sequence in time, with time
  6773. stamps in the subtree shifted for each clone produced. If SHIFT
  6774. is nil or the empty string, time stamps will be left alone. The
  6775. ID property of the original subtree is removed.
  6776. If the original subtree did contain time stamps with a repeater,
  6777. the following will happen:
  6778. - the repeater will be removed in each clone
  6779. - an additional clone will be produced, with the current, unshifted
  6780. date(s) in the entry.
  6781. - the original entry will be placed *after* all the clones, with
  6782. repeater intact.
  6783. - the start days in the repeater in the original entry will be shifted
  6784. to past the last clone.
  6785. I this way you can spell out a number of instances of a repeating task,
  6786. and still retain the repeater to cover future instances of the task."
  6787. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6788. (let (beg end template task idprop
  6789. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6790. (if (not (and (integerp n) (> n 0)))
  6791. (error "Invalid number of replications %s" n))
  6792. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6793. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6794. shift)))
  6795. (error "Invalid shift specification %s" shift))
  6796. (when doshift
  6797. (setq shift-n (string-to-number (match-string 1 shift))
  6798. shift-what (cdr (assoc (match-string 2 shift)
  6799. '(("d" . day) ("w" . week)
  6800. ("m" . month) ("y" . year))))))
  6801. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6802. (setq nmin 1 nmax n)
  6803. (org-back-to-heading t)
  6804. (setq beg (point))
  6805. (setq idprop (org-entry-get nil "ID"))
  6806. (org-end-of-subtree t t)
  6807. (or (bolp) (insert "\n"))
  6808. (setq end (point))
  6809. (setq template (buffer-substring beg end))
  6810. (when (and doshift
  6811. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6812. (delete-region beg end)
  6813. (setq end beg)
  6814. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6815. (goto-char end)
  6816. (loop for n from nmin to nmax do
  6817. ;; prepare clone
  6818. (with-temp-buffer
  6819. (insert template)
  6820. (org-mode)
  6821. (goto-char (point-min))
  6822. (and idprop (if org-clone-delete-id
  6823. (org-entry-delete nil "ID")
  6824. (org-id-get-create t)))
  6825. (while (re-search-forward org-property-drawer-re nil t)
  6826. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6827. (goto-char (point-min))
  6828. (when doshift
  6829. (while (re-search-forward org-ts-regexp-both nil t)
  6830. (org-timestamp-change (* n shift-n) shift-what))
  6831. (unless (= n n-no-remove)
  6832. (goto-char (point-min))
  6833. (while (re-search-forward org-ts-regexp nil t)
  6834. (save-excursion
  6835. (goto-char (match-beginning 0))
  6836. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6837. (delete-region (match-beginning 1) (match-end 1)))))))
  6838. (setq task (buffer-string)))
  6839. (insert task))
  6840. (goto-char beg)))
  6841. ;;; Outline Sorting
  6842. (defun org-sort (with-case)
  6843. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6844. Optional argument WITH-CASE means sort case-sensitively.
  6845. With a double prefix argument, also remove duplicate entries."
  6846. (interactive "P")
  6847. (cond
  6848. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6849. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6850. (t
  6851. (org-call-with-arg 'org-sort-entries with-case))))
  6852. (defun org-sort-remove-invisible (s)
  6853. (remove-text-properties 0 (length s) org-rm-props s)
  6854. (while (string-match org-bracket-link-regexp s)
  6855. (setq s (replace-match (if (match-end 2)
  6856. (match-string 3 s)
  6857. (match-string 1 s)) t t s)))
  6858. s)
  6859. (defvar org-priority-regexp) ; defined later in the file
  6860. (defvar org-after-sorting-entries-or-items-hook nil
  6861. "Hook that is run after a bunch of entries or items have been sorted.
  6862. When children are sorted, the cursor is in the parent line when this
  6863. hook gets called. When a region or a plain list is sorted, the cursor
  6864. will be in the first entry of the sorted region/list.")
  6865. (defun org-sort-entries
  6866. (&optional with-case sorting-type getkey-func compare-func property)
  6867. "Sort entries on a certain level of an outline tree.
  6868. If there is an active region, the entries in the region are sorted.
  6869. Else, if the cursor is before the first entry, sort the top-level items.
  6870. Else, the children of the entry at point are sorted.
  6871. Sorting can be alphabetically, numerically, by date/time as given by
  6872. a time stamp, by a property or by priority.
  6873. The command prompts for the sorting type unless it has been given to the
  6874. function through the SORTING-TYPE argument, which needs to be a character,
  6875. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6876. precise meaning of each character:
  6877. n Numerically, by converting the beginning of the entry/item to a number.
  6878. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6879. t By date/time, either the first active time stamp in the entry, or, if
  6880. none exist, by the first inactive one.
  6881. s By the scheduled date/time.
  6882. d By deadline date/time.
  6883. c By creation time, which is assumed to be the first inactive time stamp
  6884. at the beginning of a line.
  6885. p By priority according to the cookie.
  6886. r By the value of a property.
  6887. Capital letters will reverse the sort order.
  6888. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6889. called with point at the beginning of the record. It must return either
  6890. a string or a number that should serve as the sorting key for that record.
  6891. Comparing entries ignores case by default. However, with an optional argument
  6892. WITH-CASE, the sorting considers case as well."
  6893. (interactive "P")
  6894. (let ((case-func (if with-case 'identity 'downcase))
  6895. start beg end stars re re2
  6896. txt what tmp)
  6897. ;; Find beginning and end of region to sort
  6898. (cond
  6899. ((org-region-active-p)
  6900. ;; we will sort the region
  6901. (setq end (region-end)
  6902. what "region")
  6903. (goto-char (region-beginning))
  6904. (if (not (org-on-heading-p)) (outline-next-heading))
  6905. (setq start (point)))
  6906. ((or (org-on-heading-p)
  6907. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6908. ;; we will sort the children of the current headline
  6909. (org-back-to-heading)
  6910. (setq start (point)
  6911. end (progn (org-end-of-subtree t t)
  6912. (or (bolp) (insert "\n"))
  6913. (org-back-over-empty-lines)
  6914. (point))
  6915. what "children")
  6916. (goto-char start)
  6917. (show-subtree)
  6918. (outline-next-heading))
  6919. (t
  6920. ;; we will sort the top-level entries in this file
  6921. (goto-char (point-min))
  6922. (or (org-on-heading-p) (outline-next-heading))
  6923. (setq start (point))
  6924. (goto-char (point-max))
  6925. (beginning-of-line 1)
  6926. (when (looking-at ".*?\\S-")
  6927. ;; File ends in a non-white line
  6928. (end-of-line 1)
  6929. (insert "\n"))
  6930. (setq end (point-max))
  6931. (setq what "top-level")
  6932. (goto-char start)
  6933. (show-all)))
  6934. (setq beg (point))
  6935. (if (>= beg end) (error "Nothing to sort"))
  6936. (looking-at "\\(\\*+\\)")
  6937. (setq stars (match-string 1)
  6938. re (concat "^" (regexp-quote stars) " +")
  6939. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6940. txt (buffer-substring beg end))
  6941. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6942. (if (and (not (equal stars "*")) (string-match re2 txt))
  6943. (error "Region to sort contains a level above the first entry"))
  6944. (unless sorting-type
  6945. (message
  6946. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6947. [t]ime [s]cheduled [d]eadline [c]reated
  6948. A/N/T/S/D/C/P/O/F means reversed:"
  6949. what)
  6950. (setq sorting-type (read-char-exclusive))
  6951. (and (= (downcase sorting-type) ?f)
  6952. (setq getkey-func
  6953. (org-icompleting-read "Sort using function: "
  6954. obarray 'fboundp t nil nil))
  6955. (setq getkey-func (intern getkey-func)))
  6956. (and (= (downcase sorting-type) ?r)
  6957. (setq property
  6958. (org-icompleting-read "Property: "
  6959. (mapcar 'list (org-buffer-property-keys t))
  6960. nil t))))
  6961. (message "Sorting entries...")
  6962. (save-restriction
  6963. (narrow-to-region start end)
  6964. (let ((dcst (downcase sorting-type))
  6965. (case-fold-search nil)
  6966. (now (current-time)))
  6967. (sort-subr
  6968. (/= dcst sorting-type)
  6969. ;; This function moves to the beginning character of the "record" to
  6970. ;; be sorted.
  6971. (lambda nil
  6972. (if (re-search-forward re nil t)
  6973. (goto-char (match-beginning 0))
  6974. (goto-char (point-max))))
  6975. ;; This function moves to the last character of the "record" being
  6976. ;; sorted.
  6977. (lambda nil
  6978. (save-match-data
  6979. (condition-case nil
  6980. (outline-forward-same-level 1)
  6981. (error
  6982. (goto-char (point-max))))))
  6983. ;; This function returns the value that gets sorted against.
  6984. (lambda nil
  6985. (cond
  6986. ((= dcst ?n)
  6987. (if (looking-at org-complex-heading-regexp)
  6988. (string-to-number (match-string 4))
  6989. nil))
  6990. ((= dcst ?a)
  6991. (if (looking-at org-complex-heading-regexp)
  6992. (funcall case-func (match-string 4))
  6993. nil))
  6994. ((= dcst ?t)
  6995. (let ((end (save-excursion (outline-next-heading) (point))))
  6996. (if (or (re-search-forward org-ts-regexp end t)
  6997. (re-search-forward org-ts-regexp-both end t))
  6998. (org-time-string-to-seconds (match-string 0))
  6999. (org-float-time now))))
  7000. ((= dcst ?c)
  7001. (let ((end (save-excursion (outline-next-heading) (point))))
  7002. (if (re-search-forward
  7003. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7004. end t)
  7005. (org-time-string-to-seconds (match-string 0))
  7006. (org-float-time now))))
  7007. ((= dcst ?s)
  7008. (let ((end (save-excursion (outline-next-heading) (point))))
  7009. (if (re-search-forward org-scheduled-time-regexp end t)
  7010. (org-time-string-to-seconds (match-string 1))
  7011. (org-float-time now))))
  7012. ((= dcst ?d)
  7013. (let ((end (save-excursion (outline-next-heading) (point))))
  7014. (if (re-search-forward org-deadline-time-regexp end t)
  7015. (org-time-string-to-seconds (match-string 1))
  7016. (org-float-time now))))
  7017. ((= dcst ?p)
  7018. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7019. (string-to-char (match-string 2))
  7020. org-default-priority))
  7021. ((= dcst ?r)
  7022. (or (org-entry-get nil property) ""))
  7023. ((= dcst ?o)
  7024. (if (looking-at org-complex-heading-regexp)
  7025. (- 9999 (length (member (match-string 2)
  7026. org-todo-keywords-1)))))
  7027. ((= dcst ?f)
  7028. (if getkey-func
  7029. (progn
  7030. (setq tmp (funcall getkey-func))
  7031. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7032. tmp)
  7033. (error "Invalid key function `%s'" getkey-func)))
  7034. (t (error "Invalid sorting type `%c'" sorting-type))))
  7035. nil
  7036. (cond
  7037. ((= dcst ?a) 'string<)
  7038. ((= dcst ?f) compare-func)
  7039. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7040. (t nil)))))
  7041. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7042. (message "Sorting entries...done")))
  7043. (defun org-do-sort (table what &optional with-case sorting-type)
  7044. "Sort TABLE of WHAT according to SORTING-TYPE.
  7045. The user will be prompted for the SORTING-TYPE if the call to this
  7046. function does not specify it. WHAT is only for the prompt, to indicate
  7047. what is being sorted. The sorting key will be extracted from
  7048. the car of the elements of the table.
  7049. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7050. (unless sorting-type
  7051. (message
  7052. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7053. what)
  7054. (setq sorting-type (read-char-exclusive)))
  7055. (let ((dcst (downcase sorting-type))
  7056. extractfun comparefun)
  7057. ;; Define the appropriate functions
  7058. (cond
  7059. ((= dcst ?n)
  7060. (setq extractfun 'string-to-number
  7061. comparefun (if (= dcst sorting-type) '< '>)))
  7062. ((= dcst ?a)
  7063. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7064. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7065. comparefun (if (= dcst sorting-type)
  7066. 'string<
  7067. (lambda (a b) (and (not (string< a b))
  7068. (not (string= a b)))))))
  7069. ((= dcst ?t)
  7070. (setq extractfun
  7071. (lambda (x)
  7072. (if (or (string-match org-ts-regexp x)
  7073. (string-match org-ts-regexp-both x))
  7074. (org-float-time
  7075. (org-time-string-to-time (match-string 0 x)))
  7076. 0))
  7077. comparefun (if (= dcst sorting-type) '< '>)))
  7078. (t (error "Invalid sorting type `%c'" sorting-type)))
  7079. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7080. table)
  7081. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7082. ;;; The orgstruct minor mode
  7083. ;; Define a minor mode which can be used in other modes in order to
  7084. ;; integrate the org-mode structure editing commands.
  7085. ;; This is really a hack, because the org-mode structure commands use
  7086. ;; keys which normally belong to the major mode. Here is how it
  7087. ;; works: The minor mode defines all the keys necessary to operate the
  7088. ;; structure commands, but wraps the commands into a function which
  7089. ;; tests if the cursor is currently at a headline or a plain list
  7090. ;; item. If that is the case, the structure command is used,
  7091. ;; temporarily setting many Org-mode variables like regular
  7092. ;; expressions for filling etc. However, when any of those keys is
  7093. ;; used at a different location, function uses `key-binding' to look
  7094. ;; up if the key has an associated command in another currently active
  7095. ;; keymap (minor modes, major mode, global), and executes that
  7096. ;; command. There might be problems if any of the keys is otherwise
  7097. ;; used as a prefix key.
  7098. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7099. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7100. ;; addresses this by checking explicitly for both bindings.
  7101. (defvar orgstruct-mode-map (make-sparse-keymap)
  7102. "Keymap for the minor `orgstruct-mode'.")
  7103. (defvar org-local-vars nil
  7104. "List of local variables, for use by `orgstruct-mode'.")
  7105. ;;;###autoload
  7106. (define-minor-mode orgstruct-mode
  7107. "Toggle the minor mode `orgstruct-mode'.
  7108. This mode is for using Org-mode structure commands in other
  7109. modes. The following keys behave as if Org-mode were active, if
  7110. the cursor is on a headline, or on a plain list item (both as
  7111. defined by Org-mode).
  7112. M-up Move entry/item up
  7113. M-down Move entry/item down
  7114. M-left Promote
  7115. M-right Demote
  7116. M-S-up Move entry/item up
  7117. M-S-down Move entry/item down
  7118. M-S-left Promote subtree
  7119. M-S-right Demote subtree
  7120. M-q Fill paragraph and items like in Org-mode
  7121. C-c ^ Sort entries
  7122. C-c - Cycle list bullet
  7123. TAB Cycle item visibility
  7124. M-RET Insert new heading/item
  7125. S-M-RET Insert new TODO heading / Checkbox item
  7126. C-c C-c Set tags / toggle checkbox"
  7127. nil " OrgStruct" nil
  7128. (org-load-modules-maybe)
  7129. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7130. ;;;###autoload
  7131. (defun turn-on-orgstruct ()
  7132. "Unconditionally turn on `orgstruct-mode'."
  7133. (orgstruct-mode 1))
  7134. (defun orgstruct++-mode (&optional arg)
  7135. "Toggle `orgstruct-mode', the enhanced version of it.
  7136. In addition to setting orgstruct-mode, this also exports all indentation
  7137. and autofilling variables from org-mode into the buffer. It will also
  7138. recognize item context in multiline items.
  7139. Note that turning off orgstruct-mode will *not* remove the
  7140. indentation/paragraph settings. This can only be done by refreshing the
  7141. major mode, for example with \\[normal-mode]."
  7142. (interactive "P")
  7143. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7144. (if (< arg 1)
  7145. (orgstruct-mode -1)
  7146. (orgstruct-mode 1)
  7147. (let (var val)
  7148. (mapc
  7149. (lambda (x)
  7150. (when (string-match
  7151. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7152. (symbol-name (car x)))
  7153. (setq var (car x) val (nth 1 x))
  7154. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7155. org-local-vars)
  7156. (org-set-local 'orgstruct-is-++ t))))
  7157. (defvar orgstruct-is-++ nil
  7158. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7159. (make-variable-buffer-local 'orgstruct-is-++)
  7160. ;;;###autoload
  7161. (defun turn-on-orgstruct++ ()
  7162. "Unconditionally turn on `orgstruct++-mode'."
  7163. (orgstruct++-mode 1))
  7164. (defun orgstruct-error ()
  7165. "Error when there is no default binding for a structure key."
  7166. (interactive)
  7167. (error "This key has no function outside structure elements"))
  7168. (defun orgstruct-setup ()
  7169. "Setup orgstruct keymaps."
  7170. (let ((nfunc 0)
  7171. (bindings
  7172. (list
  7173. '([(meta up)] org-metaup)
  7174. '([(meta down)] org-metadown)
  7175. '([(meta left)] org-metaleft)
  7176. '([(meta right)] org-metaright)
  7177. '([(meta shift up)] org-shiftmetaup)
  7178. '([(meta shift down)] org-shiftmetadown)
  7179. '([(meta shift left)] org-shiftmetaleft)
  7180. '([(meta shift right)] org-shiftmetaright)
  7181. '([?\e (up)] org-metaup)
  7182. '([?\e (down)] org-metadown)
  7183. '([?\e (left)] org-metaleft)
  7184. '([?\e (right)] org-metaright)
  7185. '([?\e (shift up)] org-shiftmetaup)
  7186. '([?\e (shift down)] org-shiftmetadown)
  7187. '([?\e (shift left)] org-shiftmetaleft)
  7188. '([?\e (shift right)] org-shiftmetaright)
  7189. '([(shift up)] org-shiftup)
  7190. '([(shift down)] org-shiftdown)
  7191. '([(shift left)] org-shiftleft)
  7192. '([(shift right)] org-shiftright)
  7193. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7194. '("\M-q" fill-paragraph)
  7195. '("\C-c^" org-sort)
  7196. '("\C-c-" org-cycle-list-bullet)))
  7197. elt key fun cmd)
  7198. (while (setq elt (pop bindings))
  7199. (setq nfunc (1+ nfunc))
  7200. (setq key (org-key (car elt))
  7201. fun (nth 1 elt)
  7202. cmd (orgstruct-make-binding fun nfunc key))
  7203. (org-defkey orgstruct-mode-map key cmd))
  7204. ;; Special treatment needed for TAB and RET
  7205. (org-defkey orgstruct-mode-map [(tab)]
  7206. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7207. (org-defkey orgstruct-mode-map "\C-i"
  7208. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7209. (org-defkey orgstruct-mode-map "\M-\C-m"
  7210. (orgstruct-make-binding 'org-insert-heading 105
  7211. "\M-\C-m" [(meta return)]))
  7212. (org-defkey orgstruct-mode-map [(meta return)]
  7213. (orgstruct-make-binding 'org-insert-heading 106
  7214. [(meta return)] "\M-\C-m"))
  7215. (org-defkey orgstruct-mode-map [(shift meta return)]
  7216. (orgstruct-make-binding 'org-insert-todo-heading 107
  7217. [(meta return)] "\M-\C-m"))
  7218. (org-defkey orgstruct-mode-map "\e\C-m"
  7219. (orgstruct-make-binding 'org-insert-heading 108
  7220. "\e\C-m" [?\e (return)]))
  7221. (org-defkey orgstruct-mode-map [?\e (return)]
  7222. (orgstruct-make-binding 'org-insert-heading 109
  7223. [?\e (return)] "\e\C-m"))
  7224. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7225. (orgstruct-make-binding 'org-insert-todo-heading 110
  7226. [?\e (return)] "\e\C-m"))
  7227. (unless org-local-vars
  7228. (setq org-local-vars (org-get-local-variables)))
  7229. t))
  7230. (defun orgstruct-make-binding (fun n &rest keys)
  7231. "Create a function for binding in the structure minor mode.
  7232. FUN is the command to call inside a table. N is used to create a unique
  7233. command name. KEYS are keys that should be checked in for a command
  7234. to execute outside of tables."
  7235. (eval
  7236. (list 'defun
  7237. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7238. '(arg)
  7239. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7240. "Outside of structure, run the binding of `"
  7241. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7242. "'.")
  7243. '(interactive "p")
  7244. (list 'if
  7245. `(org-context-p 'headline 'item
  7246. (and orgstruct-is-++
  7247. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7248. 'item-body))
  7249. (list 'org-run-like-in-org-mode (list 'quote fun))
  7250. (list 'let '(orgstruct-mode)
  7251. (list 'call-interactively
  7252. (append '(or)
  7253. (mapcar (lambda (k)
  7254. (list 'key-binding k))
  7255. keys)
  7256. '('orgstruct-error))))))))
  7257. (defun org-context-p (&rest contexts)
  7258. "Check if local context is any of CONTEXTS.
  7259. Possible values in the list of contexts are `table', `headline', and `item'."
  7260. (let ((pos (point)))
  7261. (goto-char (point-at-bol))
  7262. (prog1 (or (and (memq 'table contexts)
  7263. (looking-at "[ \t]*|"))
  7264. (and (memq 'headline contexts)
  7265. ;;????????? (looking-at "\\*+"))
  7266. (looking-at outline-regexp))
  7267. (and (memq 'item contexts)
  7268. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7269. (and (memq 'item-body contexts)
  7270. (org-in-item-p)))
  7271. (goto-char pos))))
  7272. (defun org-get-local-variables ()
  7273. "Return a list of all local variables in an org-mode buffer."
  7274. (let (varlist)
  7275. (with-current-buffer (get-buffer-create "*Org tmp*")
  7276. (erase-buffer)
  7277. (org-mode)
  7278. (setq varlist (buffer-local-variables)))
  7279. (kill-buffer "*Org tmp*")
  7280. (delq nil
  7281. (mapcar
  7282. (lambda (x)
  7283. (setq x
  7284. (if (symbolp x)
  7285. (list x)
  7286. (list (car x) (list 'quote (cdr x)))))
  7287. (if (string-match
  7288. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7289. (symbol-name (car x)))
  7290. x nil))
  7291. varlist))))
  7292. ;;;###autoload
  7293. (defun org-run-like-in-org-mode (cmd)
  7294. "Run a command, pretending that the current buffer is in Org-mode.
  7295. This will temporarily bind local variables that are typically bound in
  7296. Org-mode to the values they have in Org-mode, and then interactively
  7297. call CMD."
  7298. (org-load-modules-maybe)
  7299. (unless org-local-vars
  7300. (setq org-local-vars (org-get-local-variables)))
  7301. (eval (list 'let org-local-vars
  7302. (list 'call-interactively (list 'quote cmd)))))
  7303. ;;;; Archiving
  7304. (defun org-get-category (&optional pos)
  7305. "Get the category applying to position POS."
  7306. (get-text-property (or pos (point)) 'org-category))
  7307. (defun org-refresh-category-properties ()
  7308. "Refresh category text properties in the buffer."
  7309. (let ((def-cat (cond
  7310. ((null org-category)
  7311. (if buffer-file-name
  7312. (file-name-sans-extension
  7313. (file-name-nondirectory buffer-file-name))
  7314. "???"))
  7315. ((symbolp org-category) (symbol-name org-category))
  7316. (t org-category)))
  7317. beg end cat pos optionp)
  7318. (org-unmodified
  7319. (save-excursion
  7320. (save-restriction
  7321. (widen)
  7322. (goto-char (point-min))
  7323. (put-text-property (point) (point-max) 'org-category def-cat)
  7324. (while (re-search-forward
  7325. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7326. (setq pos (match-end 0)
  7327. optionp (equal (char-after (match-beginning 0)) ?#)
  7328. cat (org-trim (match-string 2)))
  7329. (if optionp
  7330. (setq beg (point-at-bol) end (point-max))
  7331. (org-back-to-heading t)
  7332. (setq beg (point) end (org-end-of-subtree t t)))
  7333. (put-text-property beg end 'org-category cat)
  7334. (goto-char pos)))))))
  7335. ;;;; Link Stuff
  7336. ;;; Link abbreviations
  7337. (defun org-link-expand-abbrev (link)
  7338. "Apply replacements as defined in `org-link-abbrev-alist."
  7339. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7340. (let* ((key (match-string 1 link))
  7341. (as (or (assoc key org-link-abbrev-alist-local)
  7342. (assoc key org-link-abbrev-alist)))
  7343. (tag (and (match-end 2) (match-string 3 link)))
  7344. rpl)
  7345. (if (not as)
  7346. link
  7347. (setq rpl (cdr as))
  7348. (cond
  7349. ((symbolp rpl) (funcall rpl tag))
  7350. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7351. ((string-match "%h" rpl)
  7352. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7353. (t (concat rpl tag)))))
  7354. link))
  7355. ;;; Storing and inserting links
  7356. (defvar org-insert-link-history nil
  7357. "Minibuffer history for links inserted with `org-insert-link'.")
  7358. (defvar org-stored-links nil
  7359. "Contains the links stored with `org-store-link'.")
  7360. (defvar org-store-link-plist nil
  7361. "Plist with info about the most recently link created with `org-store-link'.")
  7362. (defvar org-link-protocols nil
  7363. "Link protocols added to Org-mode using `org-add-link-type'.")
  7364. (defvar org-store-link-functions nil
  7365. "List of functions that are called to create and store a link.
  7366. Each function will be called in turn until one returns a non-nil
  7367. value. Each function should check if it is responsible for creating
  7368. this link (for example by looking at the major mode).
  7369. If not, it must exit and return nil.
  7370. If yes, it should return a non-nil value after a calling
  7371. `org-store-link-props' with a list of properties and values.
  7372. Special properties are:
  7373. :type The link prefix, like \"http\". This must be given.
  7374. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7375. This is obligatory as well.
  7376. :description Optional default description for the second pair
  7377. of brackets in an Org-mode link. The user can still change
  7378. this when inserting this link into an Org-mode buffer.
  7379. In addition to these, any additional properties can be specified
  7380. and then used in remember templates.")
  7381. (defun org-add-link-type (type &optional follow export)
  7382. "Add TYPE to the list of `org-link-types'.
  7383. Re-compute all regular expressions depending on `org-link-types'
  7384. FOLLOW and EXPORT are two functions.
  7385. FOLLOW should take the link path as the single argument and do whatever
  7386. is necessary to follow the link, for example find a file or display
  7387. a mail message.
  7388. EXPORT should format the link path for export to one of the export formats.
  7389. It should be a function accepting three arguments:
  7390. path the path of the link, the text after the prefix (like \"http:\")
  7391. desc the description of the link, if any, nil if there was no description
  7392. format the export format, a symbol like `html' or `latex' or `ascii'..
  7393. The function may use the FORMAT information to return different values
  7394. depending on the format. The return value will be put literally into
  7395. the exported file. If the return value is nil, this means Org should
  7396. do what it normally does with links which do not have EXPORT defined.
  7397. Org-mode has a built-in default for exporting links. If you are happy with
  7398. this default, there is no need to define an export function for the link
  7399. type. For a simple example of an export function, see `org-bbdb.el'."
  7400. (add-to-list 'org-link-types type t)
  7401. (org-make-link-regexps)
  7402. (if (assoc type org-link-protocols)
  7403. (setcdr (assoc type org-link-protocols) (list follow export))
  7404. (push (list type follow export) org-link-protocols)))
  7405. (defvar org-agenda-buffer-name)
  7406. ;;;###autoload
  7407. (defun org-store-link (arg)
  7408. "\\<org-mode-map>Store an org-link to the current location.
  7409. This link is added to `org-stored-links' and can later be inserted
  7410. into an org-buffer with \\[org-insert-link].
  7411. For some link types, a prefix arg is interpreted:
  7412. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7413. For file links, arg negates `org-context-in-file-links'."
  7414. (interactive "P")
  7415. (org-load-modules-maybe)
  7416. (setq org-store-link-plist nil) ; reset
  7417. (let ((outline-regexp (org-get-limited-outline-regexp))
  7418. link cpltxt desc description search txt custom-id agenda-link)
  7419. (cond
  7420. ((run-hook-with-args-until-success 'org-store-link-functions)
  7421. (setq link (plist-get org-store-link-plist :link)
  7422. desc (or (plist-get org-store-link-plist :description) link)))
  7423. ((equal (buffer-name) "*Org Edit Src Example*")
  7424. (let (label gc)
  7425. (while (or (not label)
  7426. (save-excursion
  7427. (save-restriction
  7428. (widen)
  7429. (goto-char (point-min))
  7430. (re-search-forward
  7431. (regexp-quote (format org-coderef-label-format label))
  7432. nil t))))
  7433. (when label (message "Label exists already") (sit-for 2))
  7434. (setq label (read-string "Code line label: " label)))
  7435. (end-of-line 1)
  7436. (setq link (format org-coderef-label-format label))
  7437. (setq gc (- 79 (length link)))
  7438. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7439. (insert link)
  7440. (setq link (concat "(" label ")") desc nil)))
  7441. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7442. ;; We are in the agenda, link to referenced location
  7443. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7444. (get-text-property (point) 'org-marker))))
  7445. (when m
  7446. (org-with-point-at m
  7447. (setq agenda-link
  7448. (if (interactive-p)
  7449. (call-interactively 'org-store-link)
  7450. (org-store-link nil)))))))
  7451. ((eq major-mode 'calendar-mode)
  7452. (let ((cd (calendar-cursor-to-date)))
  7453. (setq link
  7454. (format-time-string
  7455. (car org-time-stamp-formats)
  7456. (apply 'encode-time
  7457. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7458. nil nil nil))))
  7459. (org-store-link-props :type "calendar" :date cd)))
  7460. ((eq major-mode 'w3-mode)
  7461. (setq cpltxt (if (and (buffer-name)
  7462. (not (string-match "Untitled" (buffer-name))))
  7463. (buffer-name)
  7464. (url-view-url t))
  7465. link (org-make-link (url-view-url t)))
  7466. (org-store-link-props :type "w3" :url (url-view-url t)))
  7467. ((eq major-mode 'w3m-mode)
  7468. (setq cpltxt (or w3m-current-title w3m-current-url)
  7469. link (org-make-link w3m-current-url))
  7470. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7471. ((setq search (run-hook-with-args-until-success
  7472. 'org-create-file-search-functions))
  7473. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7474. "::" search))
  7475. (setq cpltxt (or description link)))
  7476. ((eq major-mode 'image-mode)
  7477. (setq cpltxt (concat "file:"
  7478. (abbreviate-file-name buffer-file-name))
  7479. link (org-make-link cpltxt))
  7480. (org-store-link-props :type "image" :file buffer-file-name))
  7481. ((eq major-mode 'dired-mode)
  7482. ;; link to the file in the current line
  7483. (let ((file (dired-get-filename nil t)))
  7484. (setq file (if file
  7485. (abbreviate-file-name
  7486. (expand-file-name (dired-get-filename nil t)))
  7487. ;; otherwise, no file so use current directory.
  7488. default-directory))
  7489. (setq cpltxt (concat "file:" file)
  7490. link (org-make-link cpltxt))))
  7491. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7492. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7493. (cond
  7494. ((org-in-regexp "<<\\(.*?\\)>>")
  7495. (setq cpltxt
  7496. (concat "file:"
  7497. (abbreviate-file-name
  7498. (buffer-file-name (buffer-base-buffer)))
  7499. "::" (match-string 1))
  7500. link (org-make-link cpltxt)))
  7501. ((and (featurep 'org-id)
  7502. (or (eq org-link-to-org-use-id t)
  7503. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7504. (interactive-p))
  7505. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7506. (interactive-p)
  7507. (not custom-id))
  7508. (and org-link-to-org-use-id
  7509. (condition-case nil
  7510. (org-entry-get nil "ID")
  7511. (error nil)))))
  7512. ;; We can make a link using the ID.
  7513. (setq link (condition-case nil
  7514. (prog1 (org-id-store-link)
  7515. (setq desc (plist-get org-store-link-plist
  7516. :description)))
  7517. (error
  7518. ;; probably before first headline, link to file only
  7519. (concat "file:"
  7520. (abbreviate-file-name
  7521. (buffer-file-name (buffer-base-buffer))))))))
  7522. (t
  7523. ;; Just link to current headline
  7524. (setq cpltxt (concat "file:"
  7525. (abbreviate-file-name
  7526. (buffer-file-name (buffer-base-buffer)))))
  7527. ;; Add a context search string
  7528. (when (org-xor org-context-in-file-links arg)
  7529. (setq txt (cond
  7530. ((org-on-heading-p) nil)
  7531. ((org-region-active-p)
  7532. (buffer-substring (region-beginning) (region-end)))
  7533. (t nil)))
  7534. (when (or (null txt) (string-match "\\S-" txt))
  7535. (setq cpltxt
  7536. (concat cpltxt "::"
  7537. (condition-case nil
  7538. (org-make-org-heading-search-string txt)
  7539. (error "")))
  7540. desc (or (nth 4 (ignore-errors
  7541. (org-heading-components))) "NONE"))))
  7542. (if (string-match "::\\'" cpltxt)
  7543. (setq cpltxt (substring cpltxt 0 -2)))
  7544. (setq link (org-make-link cpltxt)))))
  7545. ((buffer-file-name (buffer-base-buffer))
  7546. ;; Just link to this file here.
  7547. (setq cpltxt (concat "file:"
  7548. (abbreviate-file-name
  7549. (buffer-file-name (buffer-base-buffer)))))
  7550. ;; Add a context string
  7551. (when (org-xor org-context-in-file-links arg)
  7552. (setq txt (if (org-region-active-p)
  7553. (buffer-substring (region-beginning) (region-end))
  7554. (buffer-substring (point-at-bol) (point-at-eol))))
  7555. ;; Only use search option if there is some text.
  7556. (when (string-match "\\S-" txt)
  7557. (setq cpltxt
  7558. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7559. desc "NONE")))
  7560. (setq link (org-make-link cpltxt)))
  7561. ((interactive-p)
  7562. (error "Cannot link to a buffer which is not visiting a file"))
  7563. (t (setq link nil)))
  7564. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7565. (setq link (or link cpltxt)
  7566. desc (or desc cpltxt))
  7567. (if (equal desc "NONE") (setq desc nil))
  7568. (if (and (or (interactive-p) executing-kbd-macro) link)
  7569. (progn
  7570. (setq org-stored-links
  7571. (cons (list link desc) org-stored-links))
  7572. (message "Stored: %s" (or desc link))
  7573. (when custom-id
  7574. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7575. "::#" custom-id))
  7576. (setq org-stored-links
  7577. (cons (list link desc) org-stored-links))))
  7578. (or agenda-link (and link (org-make-link-string link desc))))))
  7579. (defun org-store-link-props (&rest plist)
  7580. "Store link properties, extract names and addresses."
  7581. (let (x adr)
  7582. (when (setq x (plist-get plist :from))
  7583. (setq adr (mail-extract-address-components x))
  7584. (setq plist (plist-put plist :fromname (car adr)))
  7585. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7586. (when (setq x (plist-get plist :to))
  7587. (setq adr (mail-extract-address-components x))
  7588. (setq plist (plist-put plist :toname (car adr)))
  7589. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7590. (let ((from (plist-get plist :from))
  7591. (to (plist-get plist :to)))
  7592. (when (and from to org-from-is-user-regexp)
  7593. (setq plist
  7594. (plist-put plist :fromto
  7595. (if (string-match org-from-is-user-regexp from)
  7596. (concat "to %t")
  7597. (concat "from %f"))))))
  7598. (setq org-store-link-plist plist))
  7599. (defun org-add-link-props (&rest plist)
  7600. "Add these properties to the link property list."
  7601. (let (key value)
  7602. (while plist
  7603. (setq key (pop plist) value (pop plist))
  7604. (setq org-store-link-plist
  7605. (plist-put org-store-link-plist key value)))))
  7606. (defun org-email-link-description (&optional fmt)
  7607. "Return the description part of an email link.
  7608. This takes information from `org-store-link-plist' and formats it
  7609. according to FMT (default from `org-email-link-description-format')."
  7610. (setq fmt (or fmt org-email-link-description-format))
  7611. (let* ((p org-store-link-plist)
  7612. (to (plist-get p :toaddress))
  7613. (from (plist-get p :fromaddress))
  7614. (table
  7615. (list
  7616. (cons "%c" (plist-get p :fromto))
  7617. (cons "%F" (plist-get p :from))
  7618. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7619. (cons "%T" (plist-get p :to))
  7620. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7621. (cons "%s" (plist-get p :subject))
  7622. (cons "%m" (plist-get p :message-id)))))
  7623. (when (string-match "%c" fmt)
  7624. ;; Check if the user wrote this message
  7625. (if (and org-from-is-user-regexp from to
  7626. (save-match-data (string-match org-from-is-user-regexp from)))
  7627. (setq fmt (replace-match "to %t" t t fmt))
  7628. (setq fmt (replace-match "from %f" t t fmt))))
  7629. (org-replace-escapes fmt table)))
  7630. (defun org-make-org-heading-search-string (&optional string heading)
  7631. "Make search string for STRING or current headline."
  7632. (interactive)
  7633. (let ((s (or string (org-get-heading))))
  7634. (unless (and string (not heading))
  7635. ;; We are using a headline, clean up garbage in there.
  7636. (if (string-match org-todo-regexp s)
  7637. (setq s (replace-match "" t t s)))
  7638. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7639. (setq s (replace-match "" t t s)))
  7640. (setq s (org-trim s))
  7641. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7642. org-comment-string "\\)") s)
  7643. (setq s (replace-match "" t t s)))
  7644. (while (string-match org-ts-regexp s)
  7645. (setq s (replace-match "" t t s))))
  7646. (or string (setq s (concat "*" s))) ; Add * for headlines
  7647. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7648. (defun org-make-link (&rest strings)
  7649. "Concatenate STRINGS."
  7650. (apply 'concat strings))
  7651. (defun org-make-link-string (link &optional description)
  7652. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7653. (unless (string-match "\\S-" link)
  7654. (error "Empty link"))
  7655. (when (and description
  7656. (stringp description)
  7657. (not (string-match "\\S-" description)))
  7658. (setq description nil))
  7659. (when (stringp description)
  7660. ;; Remove brackets from the description, they are fatal.
  7661. (while (string-match "\\[" description)
  7662. (setq description (replace-match "{" t t description)))
  7663. (while (string-match "\\]" description)
  7664. (setq description (replace-match "}" t t description))))
  7665. (when (equal (org-link-escape link) description)
  7666. ;; No description needed, it is identical
  7667. (setq description nil))
  7668. (when (and (not description)
  7669. (not (equal link (org-link-escape link))))
  7670. (setq description (org-extract-attributes link)))
  7671. (setq link (if (string-match org-link-types-re link)
  7672. (concat (match-string 1 link)
  7673. (org-link-escape (substring link (match-end 1))))
  7674. (org-link-escape link)))
  7675. (concat "[[" link "]"
  7676. (if description (concat "[" description "]") "")
  7677. "]"))
  7678. (defconst org-link-escape-chars
  7679. '((?\ . "%20")
  7680. (?\[ . "%5B")
  7681. (?\] . "%5D")
  7682. (?\340 . "%E0") ; `a
  7683. (?\342 . "%E2") ; ^a
  7684. (?\347 . "%E7") ; ,c
  7685. (?\350 . "%E8") ; `e
  7686. (?\351 . "%E9") ; 'e
  7687. (?\352 . "%EA") ; ^e
  7688. (?\356 . "%EE") ; ^i
  7689. (?\364 . "%F4") ; ^o
  7690. (?\371 . "%F9") ; `u
  7691. (?\373 . "%FB") ; ^u
  7692. (?\; . "%3B")
  7693. ;; (?? . "%3F")
  7694. (?= . "%3D")
  7695. (?+ . "%2B")
  7696. )
  7697. "Association list of escapes for some characters problematic in links.
  7698. This is the list that is used for internal purposes.")
  7699. (defvar org-url-encoding-use-url-hexify nil)
  7700. (defconst org-link-escape-chars-browser
  7701. '((?\ . "%20")) ; 32 for the SPC char
  7702. "Association list of escapes for some characters problematic in links.
  7703. This is the list that is used before handing over to the browser.")
  7704. (defun org-link-escape (text &optional table)
  7705. "Escape characters in TEXT that are problematic for links."
  7706. (if (and org-url-encoding-use-url-hexify (not table))
  7707. (url-hexify-string text)
  7708. (setq table (or table org-link-escape-chars))
  7709. (when text
  7710. (let ((re (mapconcat (lambda (x) (regexp-quote
  7711. (char-to-string (car x))))
  7712. table "\\|")))
  7713. (while (string-match re text)
  7714. (setq text
  7715. (replace-match
  7716. (cdr (assoc (string-to-char (match-string 0 text))
  7717. table))
  7718. t t text)))
  7719. text))))
  7720. (defun org-link-unescape (text &optional table)
  7721. "Reverse the action of `org-link-escape'."
  7722. (if (and org-url-encoding-use-url-hexify (not table))
  7723. (url-unhex-string text)
  7724. (setq table (or table org-link-escape-chars))
  7725. (when text
  7726. (let ((case-fold-search t)
  7727. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7728. table "\\|")))
  7729. (while (string-match re text)
  7730. (setq text
  7731. (replace-match
  7732. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7733. table)))
  7734. t t text)))
  7735. text))))
  7736. (defun org-xor (a b)
  7737. "Exclusive or."
  7738. (if a (not b) b))
  7739. (defun org-fixup-message-id-for-http (s)
  7740. "Replace special characters in a message id, so it can be used in an http query."
  7741. (when (string-match "%" s)
  7742. (setq s (mapconcat (lambda (c)
  7743. (if (eq c ?%)
  7744. "%25"
  7745. (char-to-string c)))
  7746. s "")))
  7747. (while (string-match "<" s)
  7748. (setq s (replace-match "%3C" t t s)))
  7749. (while (string-match ">" s)
  7750. (setq s (replace-match "%3E" t t s)))
  7751. (while (string-match "@" s)
  7752. (setq s (replace-match "%40" t t s)))
  7753. s)
  7754. ;;;###autoload
  7755. (defun org-insert-link-global ()
  7756. "Insert a link like Org-mode does.
  7757. This command can be called in any mode to insert a link in Org-mode syntax."
  7758. (interactive)
  7759. (org-load-modules-maybe)
  7760. (org-run-like-in-org-mode 'org-insert-link))
  7761. (defun org-insert-link (&optional complete-file link-location)
  7762. "Insert a link. At the prompt, enter the link.
  7763. Completion can be used to insert any of the link protocol prefixes like
  7764. http or ftp in use.
  7765. The history can be used to select a link previously stored with
  7766. `org-store-link'. When the empty string is entered (i.e. if you just
  7767. press RET at the prompt), the link defaults to the most recently
  7768. stored link. As SPC triggers completion in the minibuffer, you need to
  7769. use M-SPC or C-q SPC to force the insertion of a space character.
  7770. You will also be prompted for a description, and if one is given, it will
  7771. be displayed in the buffer instead of the link.
  7772. If there is already a link at point, this command will allow you to edit link
  7773. and description parts.
  7774. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7775. be selected using completion. The path to the file will be relative to the
  7776. current directory if the file is in the current directory or a subdirectory.
  7777. Otherwise, the link will be the absolute path as completed in the minibuffer
  7778. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7779. option `org-link-file-path-type'.
  7780. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7781. the current directory or below.
  7782. With three \\[universal-argument] prefixes, negate the meaning of
  7783. `org-keep-stored-link-after-insertion'.
  7784. If `org-make-link-description-function' is non-nil, this function will be
  7785. called with the link target, and the result will be the default
  7786. link description.
  7787. If the LINK-LOCATION parameter is non-nil, this value will be
  7788. used as the link location instead of reading one interactively."
  7789. (interactive "P")
  7790. (let* ((wcf (current-window-configuration))
  7791. (region (if (org-region-active-p)
  7792. (buffer-substring (region-beginning) (region-end))))
  7793. (remove (and region (list (region-beginning) (region-end))))
  7794. (desc region)
  7795. tmphist ; byte-compile incorrectly complains about this
  7796. (link link-location)
  7797. entry file all-prefixes)
  7798. (cond
  7799. (link-location) ; specified by arg, just use it.
  7800. ((org-in-regexp org-bracket-link-regexp 1)
  7801. ;; We do have a link at point, and we are going to edit it.
  7802. (setq remove (list (match-beginning 0) (match-end 0)))
  7803. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7804. (setq link (read-string "Link: "
  7805. (org-link-unescape
  7806. (org-match-string-no-properties 1)))))
  7807. ((or (org-in-regexp org-angle-link-re)
  7808. (org-in-regexp org-plain-link-re))
  7809. ;; Convert to bracket link
  7810. (setq remove (list (match-beginning 0) (match-end 0))
  7811. link (read-string "Link: "
  7812. (org-remove-angle-brackets (match-string 0)))))
  7813. ((member complete-file '((4) (16)))
  7814. ;; Completing read for file names.
  7815. (setq link (org-file-complete-link complete-file)))
  7816. (t
  7817. ;; Read link, with completion for stored links.
  7818. (with-output-to-temp-buffer "*Org Links*"
  7819. (princ "Insert a link.
  7820. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7821. (when org-stored-links
  7822. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7823. (princ (mapconcat
  7824. (lambda (x)
  7825. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7826. (reverse org-stored-links) "\n"))))
  7827. (let ((cw (selected-window)))
  7828. (select-window (get-buffer-window "*Org Links*" 'visible))
  7829. (setq truncate-lines t)
  7830. (unless (pos-visible-in-window-p (point-max))
  7831. (org-fit-window-to-buffer))
  7832. (and (window-live-p cw) (select-window cw)))
  7833. ;; Fake a link history, containing the stored links.
  7834. (setq tmphist (append (mapcar 'car org-stored-links)
  7835. org-insert-link-history))
  7836. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7837. (mapcar 'car org-link-abbrev-alist)
  7838. org-link-types))
  7839. (unwind-protect
  7840. (progn
  7841. (setq link
  7842. (let ((org-completion-use-ido nil)
  7843. (org-completion-use-iswitchb nil))
  7844. (org-completing-read
  7845. "Link: "
  7846. (append
  7847. (mapcar (lambda (x) (list (concat x ":")))
  7848. all-prefixes)
  7849. (mapcar 'car org-stored-links))
  7850. nil nil nil
  7851. 'tmphist
  7852. (car (car org-stored-links)))))
  7853. (if (not (string-match "\\S-" link))
  7854. (error "No link selected"))
  7855. (if (or (member link all-prefixes)
  7856. (and (equal ":" (substring link -1))
  7857. (member (substring link 0 -1) all-prefixes)
  7858. (setq link (substring link 0 -1))))
  7859. (setq link (org-link-try-special-completion link))))
  7860. (set-window-configuration wcf)
  7861. (kill-buffer "*Org Links*"))
  7862. (setq entry (assoc link org-stored-links))
  7863. (or entry (push link org-insert-link-history))
  7864. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7865. (not org-keep-stored-link-after-insertion))
  7866. (setq org-stored-links (delq (assoc link org-stored-links)
  7867. org-stored-links)))
  7868. (setq desc (or desc (nth 1 entry)))))
  7869. (if (string-match org-plain-link-re link)
  7870. ;; URL-like link, normalize the use of angular brackets.
  7871. (setq link (org-make-link (org-remove-angle-brackets link))))
  7872. ;; Check if we are linking to the current file with a search option
  7873. ;; If yes, simplify the link by using only the search option.
  7874. (when (and buffer-file-name
  7875. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7876. (let* ((path (match-string 1 link))
  7877. (case-fold-search nil)
  7878. (search (match-string 2 link)))
  7879. (save-match-data
  7880. (if (equal (file-truename buffer-file-name) (file-truename path))
  7881. ;; We are linking to this same file, with a search option
  7882. (setq link search)))))
  7883. ;; Check if we can/should use a relative path. If yes, simplify the link
  7884. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7885. (let* ((type (match-string 1 link))
  7886. (path (match-string 2 link))
  7887. (origpath path)
  7888. (case-fold-search nil))
  7889. (cond
  7890. ((or (eq org-link-file-path-type 'absolute)
  7891. (equal complete-file '(16)))
  7892. (setq path (abbreviate-file-name (expand-file-name path))))
  7893. ((eq org-link-file-path-type 'noabbrev)
  7894. (setq path (expand-file-name path)))
  7895. ((eq org-link-file-path-type 'relative)
  7896. (setq path (file-relative-name path)))
  7897. (t
  7898. (save-match-data
  7899. (if (string-match (concat "^" (regexp-quote
  7900. (expand-file-name
  7901. (file-name-as-directory
  7902. default-directory))))
  7903. (expand-file-name path))
  7904. ;; We are linking a file with relative path name.
  7905. (setq path (substring (expand-file-name path)
  7906. (match-end 0)))
  7907. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7908. (setq link (concat type path))
  7909. (if (equal desc origpath)
  7910. (setq desc path))))
  7911. (if org-make-link-description-function
  7912. (setq desc (funcall org-make-link-description-function link desc)))
  7913. (setq desc (read-string "Description: " desc))
  7914. (unless (string-match "\\S-" desc) (setq desc nil))
  7915. (if remove (apply 'delete-region remove))
  7916. (insert (org-make-link-string link desc))))
  7917. (defun org-link-try-special-completion (type)
  7918. "If there is completion support for link type TYPE, offer it."
  7919. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7920. (if (functionp fun)
  7921. (funcall fun)
  7922. (read-string "Link (no completion support): " (concat type ":")))))
  7923. (defun org-file-complete-link (&optional arg)
  7924. "Create a file link using completion."
  7925. (let (file link)
  7926. (setq file (read-file-name "File: "))
  7927. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7928. (pwd1 (file-name-as-directory (abbreviate-file-name
  7929. (expand-file-name ".")))))
  7930. (cond
  7931. ((equal arg '(16))
  7932. (setq link (org-make-link
  7933. "file:"
  7934. (abbreviate-file-name (expand-file-name file)))))
  7935. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7936. (setq link (org-make-link "file:" (match-string 1 file))))
  7937. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7938. (expand-file-name file))
  7939. (setq link (org-make-link
  7940. "file:" (match-string 1 (expand-file-name file)))))
  7941. (t (setq link (org-make-link "file:" file)))))
  7942. link))
  7943. (defun org-completing-read (&rest args)
  7944. "Completing-read with SPACE being a normal character."
  7945. (let ((minibuffer-local-completion-map
  7946. (copy-keymap minibuffer-local-completion-map)))
  7947. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7948. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7949. (apply 'org-icompleting-read args)))
  7950. (defun org-completing-read-no-i (&rest args)
  7951. (let (org-completion-use-ido org-completion-use-iswitchb)
  7952. (apply 'org-completing-read args)))
  7953. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7954. "Use iswitch as a completing-read replacement to choose from choices.
  7955. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7956. from."
  7957. (let* ((iswitchb-use-virtual-buffers nil)
  7958. (iswitchb-make-buflist-hook
  7959. (lambda ()
  7960. (setq iswitchb-temp-buflist choices))))
  7961. (iswitchb-read-buffer prompt)))
  7962. (defun org-icompleting-read (&rest args)
  7963. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7964. (org-without-partial-completion
  7965. (if (and org-completion-use-ido
  7966. (fboundp 'ido-completing-read)
  7967. (boundp 'ido-mode) ido-mode
  7968. (listp (second args)))
  7969. (let ((ido-enter-matching-directory nil))
  7970. (apply 'ido-completing-read (concat (car args))
  7971. (if (consp (car (nth 1 args)))
  7972. (mapcar (lambda (x) (car x)) (nth 1 args))
  7973. (nth 1 args))
  7974. (cddr args)))
  7975. (if (and org-completion-use-iswitchb
  7976. (boundp 'iswitchb-mode) iswitchb-mode
  7977. (listp (second args)))
  7978. (apply 'org-iswitchb-completing-read (concat (car args))
  7979. (if (consp (car (nth 1 args)))
  7980. (mapcar (lambda (x) (car x)) (nth 1 args))
  7981. (nth 1 args))
  7982. (cddr args))
  7983. (apply 'completing-read args)))))
  7984. (defun org-extract-attributes (s)
  7985. "Extract the attributes cookie from a string and set as text property."
  7986. (let (a attr (start 0) key value)
  7987. (save-match-data
  7988. (when (string-match "{{\\([^}]+\\)}}$" s)
  7989. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7990. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7991. (setq key (match-string 1 a) value (match-string 2 a)
  7992. start (match-end 0)
  7993. attr (plist-put attr (intern key) value))))
  7994. (org-add-props s nil 'org-attr attr))
  7995. s))
  7996. (defun org-extract-attributes-from-string (tag)
  7997. (let (key value attr)
  7998. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7999. (setq key (match-string 1 tag) value (match-string 2 tag)
  8000. tag (replace-match "" t t tag)
  8001. attr (plist-put attr (intern key) value)))
  8002. (cons tag attr)))
  8003. (defun org-attributes-to-string (plist)
  8004. "Format a property list into an HTML attribute list."
  8005. (let ((s "") key value)
  8006. (while plist
  8007. (setq key (pop plist) value (pop plist))
  8008. (and value
  8009. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8010. s))
  8011. ;;; Opening/following a link
  8012. (defvar org-link-search-failed nil)
  8013. (defvar org-open-link-functions nil
  8014. "Hook for functions finding a plain text link.
  8015. These functions must take a single argument, the link content.
  8016. They will be called for links that look like [[link text][description]]
  8017. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8018. like a filename (e.g. \"./blue.png\").
  8019. These functions will be called *before* Org attempts to resolve the
  8020. link by doing text searches in the current buffer - so if you want a
  8021. link \"[[target]]\" to still find \"<<target>>\", your function should
  8022. handle this as a special case.
  8023. When the function does handle the link, it must return a non-nil value.
  8024. If it decides that it is not responsible for this link, it must return
  8025. nil to indicate that that Org-mode can continue with other options
  8026. like exact and fuzzy text search.")
  8027. (defun org-next-link ()
  8028. "Move forward to the next link.
  8029. If the link is in hidden text, expose it."
  8030. (interactive)
  8031. (when (and org-link-search-failed (eq this-command last-command))
  8032. (goto-char (point-min))
  8033. (message "Link search wrapped back to beginning of buffer"))
  8034. (setq org-link-search-failed nil)
  8035. (let* ((pos (point))
  8036. (ct (org-context))
  8037. (a (assoc :link ct)))
  8038. (if a (goto-char (nth 2 a)))
  8039. (if (re-search-forward org-any-link-re nil t)
  8040. (progn
  8041. (goto-char (match-beginning 0))
  8042. (if (org-invisible-p) (org-show-context)))
  8043. (goto-char pos)
  8044. (setq org-link-search-failed t)
  8045. (error "No further link found"))))
  8046. (defun org-previous-link ()
  8047. "Move backward to the previous link.
  8048. If the link is in hidden text, expose it."
  8049. (interactive)
  8050. (when (and org-link-search-failed (eq this-command last-command))
  8051. (goto-char (point-max))
  8052. (message "Link search wrapped back to end of buffer"))
  8053. (setq org-link-search-failed nil)
  8054. (let* ((pos (point))
  8055. (ct (org-context))
  8056. (a (assoc :link ct)))
  8057. (if a (goto-char (nth 1 a)))
  8058. (if (re-search-backward org-any-link-re nil t)
  8059. (progn
  8060. (goto-char (match-beginning 0))
  8061. (if (org-invisible-p) (org-show-context)))
  8062. (goto-char pos)
  8063. (setq org-link-search-failed t)
  8064. (error "No further link found"))))
  8065. (defun org-translate-link (s)
  8066. "Translate a link string if a translation function has been defined."
  8067. (if (and org-link-translation-function
  8068. (fboundp org-link-translation-function)
  8069. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8070. (progn
  8071. (setq s (funcall org-link-translation-function
  8072. (match-string 1) (match-string 2)))
  8073. (concat (car s) ":" (cdr s)))
  8074. s))
  8075. (defun org-translate-link-from-planner (type path)
  8076. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8077. This is still an experimental function, your mileage may vary."
  8078. (cond
  8079. ((member type '("http" "https" "news" "ftp"))
  8080. ;; standard Internet links are the same.
  8081. nil)
  8082. ((and (equal type "irc") (string-match "^//" path))
  8083. ;; Planner has two / at the beginning of an irc link, we have 1.
  8084. ;; We should have zero, actually....
  8085. (setq path (substring path 1)))
  8086. ((and (equal type "lisp") (string-match "^/" path))
  8087. ;; Planner has a slash, we do not.
  8088. (setq type "elisp" path (substring path 1)))
  8089. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8090. ;; A typical message link. Planner has the id after the final slash,
  8091. ;; we separate it with a hash mark
  8092. (setq path (concat (match-string 1 path) "#"
  8093. (org-remove-angle-brackets (match-string 2 path)))))
  8094. )
  8095. (cons type path))
  8096. (defun org-find-file-at-mouse (ev)
  8097. "Open file link or URL at mouse."
  8098. (interactive "e")
  8099. (mouse-set-point ev)
  8100. (org-open-at-point 'in-emacs))
  8101. (defun org-open-at-mouse (ev)
  8102. "Open file link or URL at mouse."
  8103. (interactive "e")
  8104. (mouse-set-point ev)
  8105. (if (eq major-mode 'org-agenda-mode)
  8106. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8107. (org-open-at-point))
  8108. (defvar org-window-config-before-follow-link nil
  8109. "The window configuration before following a link.
  8110. This is saved in case the need arises to restore it.")
  8111. (defvar org-open-link-marker (make-marker)
  8112. "Marker pointing to the location where `org-open-at-point; was called.")
  8113. ;;;###autoload
  8114. (defun org-open-at-point-global ()
  8115. "Follow a link like Org-mode does.
  8116. This command can be called in any mode to follow a link that has
  8117. Org-mode syntax."
  8118. (interactive)
  8119. (org-run-like-in-org-mode 'org-open-at-point))
  8120. ;;;###autoload
  8121. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8122. "Open a link in the string S, as if it was in Org-mode."
  8123. (interactive "sLink: \nP")
  8124. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8125. (with-temp-buffer
  8126. (let ((org-inhibit-startup t))
  8127. (org-mode)
  8128. (insert s)
  8129. (goto-char (point-min))
  8130. (when reference-buffer
  8131. (setq org-link-abbrev-alist-local
  8132. (with-current-buffer reference-buffer
  8133. org-link-abbrev-alist-local)))
  8134. (org-open-at-point arg reference-buffer)))))
  8135. (defvar org-open-at-point-functions nil
  8136. "Hook that is run when following a link at point.
  8137. Functions in this hook must return t if they identify and follow
  8138. a link at point. If they don't find anything interesting at point,
  8139. they must return nil.")
  8140. (defun org-open-at-point (&optional in-emacs reference-buffer)
  8141. "Open link at or after point.
  8142. If there is no link at point, this function will search forward up to
  8143. the end of the current line.
  8144. Normally, files will be opened by an appropriate application. If the
  8145. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  8146. With a double prefix argument, try to open outside of Emacs, in the
  8147. application the system uses for this file type."
  8148. (interactive "P")
  8149. ;; if in a code block, then open the block's results
  8150. (unless (call-interactively #'org-babel-open-src-block-result)
  8151. (org-load-modules-maybe)
  8152. (move-marker org-open-link-marker (point))
  8153. (setq org-window-config-before-follow-link (current-window-configuration))
  8154. (org-remove-occur-highlights nil nil t)
  8155. (cond
  8156. ((and (org-on-heading-p)
  8157. (not (org-in-regexp
  8158. (concat org-plain-link-re "\\|"
  8159. org-bracket-link-regexp "\\|"
  8160. org-angle-link-re "\\|"
  8161. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8162. (not (get-text-property (point) 'org-linked-text)))
  8163. (or (org-offer-links-in-entry in-emacs)
  8164. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8165. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8166. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8167. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8168. (org-footnote-action))
  8169. (t
  8170. (let (type path link line search (pos (point)))
  8171. (catch 'match
  8172. (save-excursion
  8173. (skip-chars-forward "^]\n\r")
  8174. (when (org-in-regexp org-bracket-link-regexp 1)
  8175. (setq link (org-extract-attributes
  8176. (org-link-unescape (org-match-string-no-properties 1))))
  8177. (while (string-match " *\n *" link)
  8178. (setq link (replace-match " " t t link)))
  8179. (setq link (org-link-expand-abbrev link))
  8180. (cond
  8181. ((or (file-name-absolute-p link)
  8182. (string-match "^\\.\\.?/" link))
  8183. (setq type "file" path link))
  8184. ((string-match org-link-re-with-space3 link)
  8185. (setq type (match-string 1 link) path (match-string 2 link)))
  8186. (t (setq type "thisfile" path link)))
  8187. (throw 'match t)))
  8188. (when (get-text-property (point) 'org-linked-text)
  8189. (setq type "thisfile"
  8190. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8191. (1+ (point)) (point))
  8192. path (buffer-substring
  8193. (previous-single-property-change pos 'org-linked-text)
  8194. (next-single-property-change pos 'org-linked-text)))
  8195. (throw 'match t))
  8196. (save-excursion
  8197. (when (or (org-in-regexp org-angle-link-re)
  8198. (org-in-regexp org-plain-link-re))
  8199. (setq type (match-string 1) path (match-string 2))
  8200. (throw 'match t)))
  8201. (save-excursion
  8202. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8203. (setq type "tags"
  8204. path (match-string 1))
  8205. (while (string-match ":" path)
  8206. (setq path (replace-match "+" t t path)))
  8207. (throw 'match t)))
  8208. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8209. (setq type "tree-match"
  8210. path (match-string 1))
  8211. (throw 'match t)))
  8212. (unless path
  8213. (error "No link found"))
  8214. ;; switch back to reference buffer
  8215. ;; needed when if called in a temporary buffer through
  8216. ;; org-open-link-from-string
  8217. (with-current-buffer (or reference-buffer (current-buffer))
  8218. ;; Remove any trailing spaces in path
  8219. (if (string-match " +\\'" path)
  8220. (setq path (replace-match "" t t path)))
  8221. (if (and org-link-translation-function
  8222. (fboundp org-link-translation-function))
  8223. ;; Check if we need to translate the link
  8224. (let ((tmp (funcall org-link-translation-function type path)))
  8225. (setq type (car tmp) path (cdr tmp))))
  8226. (cond
  8227. ((assoc type org-link-protocols)
  8228. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8229. ((equal type "mailto")
  8230. (let ((cmd (car org-link-mailto-program))
  8231. (args (cdr org-link-mailto-program)) args1
  8232. (address path) (subject "") a)
  8233. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8234. (setq address (match-string 1 path)
  8235. subject (org-link-escape (match-string 2 path))))
  8236. (while args
  8237. (cond
  8238. ((not (stringp (car args))) (push (pop args) args1))
  8239. (t (setq a (pop args))
  8240. (if (string-match "%a" a)
  8241. (setq a (replace-match address t t a)))
  8242. (if (string-match "%s" a)
  8243. (setq a (replace-match subject t t a)))
  8244. (push a args1))))
  8245. (apply cmd (nreverse args1))))
  8246. ((member type '("http" "https" "ftp" "news"))
  8247. (browse-url (concat type ":" (org-link-escape
  8248. path org-link-escape-chars-browser))))
  8249. ((string= type "doi")
  8250. (browse-url (concat "http://dx.doi.org/"
  8251. (org-link-escape
  8252. path org-link-escape-chars-browser))))
  8253. ((member type '("message"))
  8254. (browse-url (concat type ":" path)))
  8255. ((string= type "tags")
  8256. (org-tags-view in-emacs path))
  8257. ((string= type "tree-match")
  8258. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8259. ((string= type "file")
  8260. (if (string-match "::\\([0-9]+\\)\\'" path)
  8261. (setq line (string-to-number (match-string 1 path))
  8262. path (substring path 0 (match-beginning 0)))
  8263. (if (string-match "::\\(.+\\)\\'" path)
  8264. (setq search (match-string 1 path)
  8265. path (substring path 0 (match-beginning 0)))))
  8266. (if (string-match "[*?{]" (file-name-nondirectory path))
  8267. (dired path)
  8268. (org-open-file path in-emacs line search)))
  8269. ((string= type "shell")
  8270. (let ((cmd path))
  8271. (if (or (not org-confirm-shell-link-function)
  8272. (funcall org-confirm-shell-link-function
  8273. (format "Execute \"%s\" in shell? "
  8274. (org-add-props cmd nil
  8275. 'face 'org-warning))))
  8276. (progn
  8277. (message "Executing %s" cmd)
  8278. (shell-command cmd))
  8279. (error "Abort"))))
  8280. ((string= type "elisp")
  8281. (let ((cmd path))
  8282. (if (or (not org-confirm-elisp-link-function)
  8283. (funcall org-confirm-elisp-link-function
  8284. (format "Execute \"%s\" as elisp? "
  8285. (org-add-props cmd nil
  8286. 'face 'org-warning))))
  8287. (message "%s => %s" cmd
  8288. (if (equal (string-to-char cmd) ?\()
  8289. (eval (read cmd))
  8290. (call-interactively (read cmd))))
  8291. (error "Abort"))))
  8292. ((and (string= type "thisfile")
  8293. (run-hook-with-args-until-success
  8294. 'org-open-link-functions path)))
  8295. ((string= type "thisfile")
  8296. (if in-emacs
  8297. (switch-to-buffer-other-window
  8298. (org-get-buffer-for-internal-link (current-buffer)))
  8299. (org-mark-ring-push))
  8300. (let ((cmd `(org-link-search
  8301. ,path
  8302. ,(cond ((equal in-emacs '(4)) 'occur)
  8303. ((equal in-emacs '(16)) 'org-occur)
  8304. (t nil))
  8305. ,pos)))
  8306. (condition-case nil (eval cmd)
  8307. (error (progn (widen) (eval cmd))))))
  8308. (t
  8309. (browse-url-at-point)))))))
  8310. (move-marker org-open-link-marker nil)
  8311. (run-hook-with-args 'org-follow-link-hook)))
  8312. (defun org-offer-links-in-entry (&optional nth zero)
  8313. "Offer links in the current entry and follow the selected link.
  8314. If there is only one link, follow it immediately as well.
  8315. If NTH is an integer, immediately pick the NTH link found.
  8316. If ZERO is a string, check also this string for a link, and if
  8317. there is one, offer it as link number zero."
  8318. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8319. "\\(" org-angle-link-re "\\)\\|"
  8320. "\\(" org-plain-link-re "\\)"))
  8321. (cnt ?0)
  8322. (in-emacs (if (integerp nth) nil nth))
  8323. have-zero end links link c)
  8324. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8325. (push (match-string 0 zero) links)
  8326. (setq cnt (1- cnt) have-zero t))
  8327. (save-excursion
  8328. (org-back-to-heading t)
  8329. (setq end (save-excursion (outline-next-heading) (point)))
  8330. (while (re-search-forward re end t)
  8331. (push (match-string 0) links))
  8332. (setq links (org-uniquify (reverse links))))
  8333. (cond
  8334. ((null links)
  8335. (message "No links"))
  8336. ((equal (length links) 1)
  8337. (setq link (list (car links))))
  8338. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8339. (setq link (nth (if have-zero nth (1- nth)) links)))
  8340. (t ; we have to select a link
  8341. (save-excursion
  8342. (save-window-excursion
  8343. (delete-other-windows)
  8344. (with-output-to-temp-buffer "*Select Link*"
  8345. (mapc (lambda (l)
  8346. (if (not (string-match org-bracket-link-regexp l))
  8347. (princ (format "[%c] %s\n" (incf cnt)
  8348. (org-remove-angle-brackets l)))
  8349. (if (match-end 3)
  8350. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8351. (match-string 3 l) (match-string 1 l)))
  8352. (princ (format "[%c] %s\n" (incf cnt)
  8353. (match-string 1 l))))))
  8354. links))
  8355. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8356. (message "Select link to open, RET to open all:")
  8357. (setq c (read-char-exclusive))
  8358. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8359. (when (equal c ?q) (error "Abort"))
  8360. (if (equal c ?\C-m)
  8361. (setq link links)
  8362. (setq nth (- c ?0))
  8363. (if have-zero (setq nth (1+ nth)))
  8364. (unless (and (integerp nth) (>= (length links) nth))
  8365. (error "Invalid link selection"))
  8366. (setq link (list (nth (1- nth) links))))))
  8367. (if link
  8368. (let ((buf (current-buffer)))
  8369. (dolist (l link)
  8370. (org-open-link-from-string l in-emacs buf))
  8371. t)
  8372. nil)))
  8373. ;; Add special file links that specify the way of opening
  8374. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8375. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8376. (defun org-open-file-with-system (path)
  8377. "Open file at PATH using the system way of opening it."
  8378. (org-open-file path 'system))
  8379. (defun org-open-file-with-emacs (path)
  8380. "Open file at PATH in Emacs."
  8381. (org-open-file path 'emacs))
  8382. (defun org-remove-file-link-modifiers ()
  8383. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8384. (goto-char (point-min))
  8385. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8386. (org-if-unprotected
  8387. (replace-match "file:" t t))))
  8388. (eval-after-load "org-exp"
  8389. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8390. 'org-remove-file-link-modifiers))
  8391. ;;;; Time estimates
  8392. (defun org-get-effort (&optional pom)
  8393. "Get the effort estimate for the current entry."
  8394. (org-entry-get pom org-effort-property))
  8395. ;;; File search
  8396. (defvar org-create-file-search-functions nil
  8397. "List of functions to construct the right search string for a file link.
  8398. These functions are called in turn with point at the location to
  8399. which the link should point.
  8400. A function in the hook should first test if it would like to
  8401. handle this file type, for example by checking the `major-mode'
  8402. or the file extension. If it decides not to handle this file, it
  8403. should just return nil to give other functions a chance. If it
  8404. does handle the file, it must return the search string to be used
  8405. when following the link. The search string will be part of the
  8406. file link, given after a double colon, and `org-open-at-point'
  8407. will automatically search for it. If special measures must be
  8408. taken to make the search successful, another function should be
  8409. added to the companion hook `org-execute-file-search-functions',
  8410. which see.
  8411. A function in this hook may also use `setq' to set the variable
  8412. `description' to provide a suggestion for the descriptive text to
  8413. be used for this link when it gets inserted into an Org-mode
  8414. buffer with \\[org-insert-link].")
  8415. (defvar org-execute-file-search-functions nil
  8416. "List of functions to execute a file search triggered by a link.
  8417. Functions added to this hook must accept a single argument, the
  8418. search string that was part of the file link, the part after the
  8419. double colon. The function must first check if it would like to
  8420. handle this search, for example by checking the `major-mode' or
  8421. the file extension. If it decides not to handle this search, it
  8422. should just return nil to give other functions a chance. If it
  8423. does handle the search, it must return a non-nil value to keep
  8424. other functions from trying.
  8425. Each function can access the current prefix argument through the
  8426. variable `current-prefix-argument'. Note that a single prefix is
  8427. used to force opening a link in Emacs, so it may be good to only
  8428. use a numeric or double prefix to guide the search function.
  8429. In case this is needed, a function in this hook can also restore
  8430. the window configuration before `org-open-at-point' was called using:
  8431. (set-window-configuration org-window-config-before-follow-link)")
  8432. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8433. (defun org-link-search (s &optional type avoid-pos)
  8434. "Search for a link search option.
  8435. If S is surrounded by forward slashes, it is interpreted as a
  8436. regular expression. In org-mode files, this will create an `org-occur'
  8437. sparse tree. In ordinary files, `occur' will be used to list matches.
  8438. If the current buffer is in `dired-mode', grep will be used to search
  8439. in all files. If AVOID-POS is given, ignore matches near that position."
  8440. (let ((case-fold-search t)
  8441. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8442. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8443. (append '(("") (" ") ("\t") ("\n"))
  8444. org-emphasis-alist)
  8445. "\\|") "\\)"))
  8446. (pos (point))
  8447. (pre nil) (post nil)
  8448. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8449. (cond
  8450. ;; First check if there are any special search functions
  8451. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8452. ;; Now try the builtin stuff
  8453. ((and (equal (string-to-char s0) ?#)
  8454. (> (length s0) 1)
  8455. (save-excursion
  8456. (goto-char (point-min))
  8457. (and
  8458. (re-search-forward
  8459. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8460. (setq type 'dedicated
  8461. pos (match-beginning 0))))
  8462. ;; There is an exact target for this
  8463. (goto-char pos)
  8464. (org-back-to-heading t)))
  8465. ((save-excursion
  8466. (goto-char (point-min))
  8467. (and
  8468. (re-search-forward
  8469. (concat "<<" (regexp-quote s0) ">>") nil t)
  8470. (setq type 'dedicated
  8471. pos (match-beginning 0))))
  8472. ;; There is an exact target for this
  8473. (goto-char pos))
  8474. ((and (string-match "^(\\(.*\\))$" s0)
  8475. (save-excursion
  8476. (goto-char (point-min))
  8477. (and
  8478. (re-search-forward
  8479. (concat "[^[]" (regexp-quote
  8480. (format org-coderef-label-format
  8481. (match-string 1 s0))))
  8482. nil t)
  8483. (setq type 'dedicated
  8484. pos (1+ (match-beginning 0))))))
  8485. ;; There is a coderef target for this
  8486. (goto-char pos))
  8487. ((string-match "^/\\(.*\\)/$" s)
  8488. ;; A regular expression
  8489. (cond
  8490. ((org-mode-p)
  8491. (org-occur (match-string 1 s)))
  8492. ;;((eq major-mode 'dired-mode)
  8493. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8494. (t (org-do-occur (match-string 1 s)))))
  8495. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8496. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8497. (goto-char (point-min))
  8498. (cond
  8499. ((let (case-fold-search)
  8500. (re-search-forward (format org-complex-heading-regexp-format
  8501. (regexp-quote s))
  8502. nil t))
  8503. ;; OK, found a match
  8504. (setq type 'dedicated)
  8505. (goto-char (match-beginning 0)))
  8506. ((and (not org-link-search-inhibit-query)
  8507. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8508. (y-or-n-p "No match - create this as a new heading? "))
  8509. (goto-char (point-max))
  8510. (or (bolp) (newline))
  8511. (insert "* " s "\n")
  8512. (beginning-of-line 0))
  8513. (t
  8514. (goto-char pos)
  8515. (error "No match"))))
  8516. (t
  8517. ;; A normal search string
  8518. (when (equal (string-to-char s) ?*)
  8519. ;; Anchor on headlines, post may include tags.
  8520. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8521. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8522. s (substring s 1)))
  8523. (remove-text-properties
  8524. 0 (length s)
  8525. '(face nil mouse-face nil keymap nil fontified nil) s)
  8526. ;; Make a series of regular expressions to find a match
  8527. (setq words (org-split-string s "[ \n\r\t]+")
  8528. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8529. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8530. "\\)" markers)
  8531. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8532. re2a (concat "[ \t\r\n]" re2a_)
  8533. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8534. re4 (concat "[^a-zA-Z_]" re4_)
  8535. re1 (concat pre re2 post)
  8536. re3 (concat pre (if pre re4_ re4) post)
  8537. re5 (concat pre ".*" re4)
  8538. re2 (concat pre re2)
  8539. re2a (concat pre (if pre re2a_ re2a))
  8540. re4 (concat pre (if pre re4_ re4))
  8541. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8542. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8543. re5 "\\)"
  8544. ))
  8545. (cond
  8546. ((eq type 'org-occur) (org-occur reall))
  8547. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8548. (t (goto-char (point-min))
  8549. (setq type 'fuzzy)
  8550. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8551. (org-search-not-self 1 re1 nil t)
  8552. (org-search-not-self 1 re2 nil t)
  8553. (org-search-not-self 1 re2a nil t)
  8554. (org-search-not-self 1 re3 nil t)
  8555. (org-search-not-self 1 re4 nil t)
  8556. (org-search-not-self 1 re5 nil t)
  8557. )
  8558. (goto-char (match-beginning 1))
  8559. (goto-char pos)
  8560. (error "No match"))))))
  8561. (and (org-mode-p) (org-show-context 'link-search))
  8562. type))
  8563. (defun org-search-not-self (group &rest args)
  8564. "Execute `re-search-forward', but only accept matches that do not
  8565. enclose the position of `org-open-link-marker'."
  8566. (let ((m org-open-link-marker))
  8567. (catch 'exit
  8568. (while (apply 're-search-forward args)
  8569. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8570. (goto-char (match-end group))
  8571. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8572. (> (match-beginning 0) (marker-position m))
  8573. (< (match-end 0) (marker-position m)))
  8574. (save-match-data
  8575. (or (not (org-in-regexp
  8576. org-bracket-link-analytic-regexp 1))
  8577. (not (match-end 4)) ; no description
  8578. (and (<= (match-beginning 4) (point))
  8579. (>= (match-end 4) (point))))))
  8580. (throw 'exit (point))))))))
  8581. (defun org-get-buffer-for-internal-link (buffer)
  8582. "Return a buffer to be used for displaying the link target of internal links."
  8583. (cond
  8584. ((not org-display-internal-link-with-indirect-buffer)
  8585. buffer)
  8586. ((string-match "(Clone)$" (buffer-name buffer))
  8587. (message "Buffer is already a clone, not making another one")
  8588. ;; we also do not modify visibility in this case
  8589. buffer)
  8590. (t ; make a new indirect buffer for displaying the link
  8591. (let* ((bn (buffer-name buffer))
  8592. (ibn (concat bn "(Clone)"))
  8593. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8594. (with-current-buffer ib (org-overview))
  8595. ib))))
  8596. (defun org-do-occur (regexp &optional cleanup)
  8597. "Call the Emacs command `occur'.
  8598. If CLEANUP is non-nil, remove the printout of the regular expression
  8599. in the *Occur* buffer. This is useful if the regex is long and not useful
  8600. to read."
  8601. (occur regexp)
  8602. (when cleanup
  8603. (let ((cwin (selected-window)) win beg end)
  8604. (when (setq win (get-buffer-window "*Occur*"))
  8605. (select-window win))
  8606. (goto-char (point-min))
  8607. (when (re-search-forward "match[a-z]+" nil t)
  8608. (setq beg (match-end 0))
  8609. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8610. (setq end (1- (match-beginning 0)))))
  8611. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8612. (goto-char (point-min))
  8613. (select-window cwin))))
  8614. ;;; The mark ring for links jumps
  8615. (defvar org-mark-ring nil
  8616. "Mark ring for positions before jumps in Org-mode.")
  8617. (defvar org-mark-ring-last-goto nil
  8618. "Last position in the mark ring used to go back.")
  8619. ;; Fill and close the ring
  8620. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8621. (loop for i from 1 to org-mark-ring-length do
  8622. (push (make-marker) org-mark-ring))
  8623. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8624. org-mark-ring)
  8625. (defun org-mark-ring-push (&optional pos buffer)
  8626. "Put the current position or POS into the mark ring and rotate it."
  8627. (interactive)
  8628. (setq pos (or pos (point)))
  8629. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8630. (move-marker (car org-mark-ring)
  8631. (or pos (point))
  8632. (or buffer (current-buffer)))
  8633. (message "%s"
  8634. (substitute-command-keys
  8635. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8636. (defun org-mark-ring-goto (&optional n)
  8637. "Jump to the previous position in the mark ring.
  8638. With prefix arg N, jump back that many stored positions. When
  8639. called several times in succession, walk through the entire ring.
  8640. Org-mode commands jumping to a different position in the current file,
  8641. or to another Org-mode file, automatically push the old position
  8642. onto the ring."
  8643. (interactive "p")
  8644. (let (p m)
  8645. (if (eq last-command this-command)
  8646. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8647. (setq p org-mark-ring))
  8648. (setq org-mark-ring-last-goto p)
  8649. (setq m (car p))
  8650. (switch-to-buffer (marker-buffer m))
  8651. (goto-char m)
  8652. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8653. (defun org-remove-angle-brackets (s)
  8654. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8655. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8656. s)
  8657. (defun org-add-angle-brackets (s)
  8658. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8659. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8660. s)
  8661. (defun org-remove-double-quotes (s)
  8662. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8663. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8664. s)
  8665. ;;; Following specific links
  8666. (defun org-follow-timestamp-link ()
  8667. (cond
  8668. ((org-at-date-range-p t)
  8669. (let ((org-agenda-start-on-weekday)
  8670. (t1 (match-string 1))
  8671. (t2 (match-string 2)))
  8672. (setq t1 (time-to-days (org-time-string-to-time t1))
  8673. t2 (time-to-days (org-time-string-to-time t2)))
  8674. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8675. ((org-at-timestamp-p t)
  8676. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8677. (substring (match-string 1) 0 10)))
  8678. 1))
  8679. (t (error "This should not happen"))))
  8680. ;;; Following file links
  8681. (defvar org-wait nil)
  8682. (defun org-open-file (path &optional in-emacs line search)
  8683. "Open the file at PATH.
  8684. First, this expands any special file name abbreviations. Then the
  8685. configuration variable `org-file-apps' is checked if it contains an
  8686. entry for this file type, and if yes, the corresponding command is launched.
  8687. If no application is found, Emacs simply visits the file.
  8688. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8689. With a double \\[universal-argument] \\[universal-argument] \
  8690. prefix arg, Org tries to avoid opening in Emacs
  8691. and to use an external application to visit the file.
  8692. Optional LINE specifies a line to go to, optional SEARCH a string
  8693. to search for. If LINE or SEARCH is given, the file will be
  8694. opened in Emacs, unless an entry from org-file-apps that makes
  8695. use of groups in a regexp matches.
  8696. If the file does not exist, an error is thrown."
  8697. (let* ((file (if (equal path "")
  8698. buffer-file-name
  8699. (substitute-in-file-name (expand-file-name path))))
  8700. (file-apps (append org-file-apps (org-default-apps)))
  8701. (apps (org-remove-if
  8702. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8703. (apps-dlink (org-remove-if-not
  8704. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8705. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8706. (dirp (if remp nil (file-directory-p file)))
  8707. (file (if (and dirp org-open-directory-means-index-dot-org)
  8708. (concat (file-name-as-directory file) "index.org")
  8709. file))
  8710. (a-m-a-p (assq 'auto-mode apps))
  8711. (dfile (downcase file))
  8712. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8713. (link (cond ((and (eq line nil)
  8714. (eq search nil))
  8715. file)
  8716. (line
  8717. (concat file "::" (number-to-string line)))
  8718. (search
  8719. (concat file "::" search))))
  8720. (dlink (downcase link))
  8721. (old-buffer (current-buffer))
  8722. (old-pos (point))
  8723. (old-mode major-mode)
  8724. ext cmd link-match-data)
  8725. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8726. (setq ext (match-string 1 dfile))
  8727. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8728. (setq ext (match-string 1 dfile))))
  8729. (cond
  8730. ((member in-emacs '((16) system))
  8731. (setq cmd (cdr (assoc 'system apps))))
  8732. (in-emacs (setq cmd 'emacs))
  8733. (t
  8734. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8735. (and dirp (cdr (assoc 'directory apps)))
  8736. ; first, try matching against apps-dlink
  8737. ; if we get a match here, store the match data for later
  8738. (let ((match (assoc-default dlink apps-dlink
  8739. 'string-match)))
  8740. (if match
  8741. (progn (setq link-match-data (match-data))
  8742. match)
  8743. (progn (setq in-emacs (or in-emacs line search))
  8744. nil))) ; if we have no match in apps-dlink,
  8745. ; always open the file in emacs if line or search
  8746. ; is given (for backwards compatibility)
  8747. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8748. 'string-match)
  8749. (cdr (assoc ext apps))
  8750. (cdr (assoc t apps))))))
  8751. (when (eq cmd 'system)
  8752. (setq cmd (cdr (assoc 'system apps))))
  8753. (when (eq cmd 'default)
  8754. (setq cmd (cdr (assoc t apps))))
  8755. (when (eq cmd 'mailcap)
  8756. (require 'mailcap)
  8757. (mailcap-parse-mailcaps)
  8758. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8759. (command (mailcap-mime-info mime-type)))
  8760. (if (stringp command)
  8761. (setq cmd command)
  8762. (setq cmd 'emacs))))
  8763. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8764. (not (file-exists-p file))
  8765. (not org-open-non-existing-files))
  8766. (error "No such file: %s" file))
  8767. (cond
  8768. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8769. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8770. (while (string-match "['\"]%s['\"]" cmd)
  8771. (setq cmd (replace-match "%s" t t cmd)))
  8772. (while (string-match "%s" cmd)
  8773. (setq cmd (replace-match
  8774. (save-match-data
  8775. (shell-quote-argument
  8776. (convert-standard-filename file)))
  8777. t t cmd)))
  8778. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8779. (save-match-data
  8780. (let ((match-index 1)
  8781. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8782. (set-match-data link-match-data)
  8783. (while (<= match-index number-of-groups)
  8784. (let ((regex (concat "%" (number-to-string match-index)))
  8785. (replace-with (match-string match-index dlink)))
  8786. (while (string-match regex cmd)
  8787. (setq cmd (replace-match replace-with t t cmd))))
  8788. (setq match-index (+ match-index 1)))))
  8789. (save-window-excursion
  8790. (start-process-shell-command cmd nil cmd)
  8791. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8792. ))
  8793. ((or (stringp cmd)
  8794. (eq cmd 'emacs))
  8795. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8796. (widen)
  8797. (if line (org-goto-line line)
  8798. (if search (org-link-search search))))
  8799. ((consp cmd)
  8800. (let ((file (convert-standard-filename file)))
  8801. (save-match-data
  8802. (set-match-data link-match-data)
  8803. (eval cmd))))
  8804. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8805. (and (org-mode-p) (eq old-mode 'org-mode)
  8806. (or (not (equal old-buffer (current-buffer)))
  8807. (not (equal old-pos (point))))
  8808. (org-mark-ring-push old-pos old-buffer))))
  8809. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8810. "This function returns non-nil if `entry' uses a regular
  8811. expression which should be matched against the whole link by
  8812. org-open-file.
  8813. It assumes that is the case when the entry uses a regular
  8814. expression which has at least one grouping construct and the
  8815. action is either a lisp form or a command string containing
  8816. '%1', i.e. using at least one subexpression match as a
  8817. parameter."
  8818. (let ((selector (car entry))
  8819. (action (cdr entry)))
  8820. (if (stringp selector)
  8821. (and (> (regexp-opt-depth selector) 0)
  8822. (or (and (stringp action)
  8823. (string-match "%[0-9]" action))
  8824. (consp action)))
  8825. nil)))
  8826. (defun org-default-apps ()
  8827. "Return the default applications for this operating system."
  8828. (cond
  8829. ((eq system-type 'darwin)
  8830. org-file-apps-defaults-macosx)
  8831. ((eq system-type 'windows-nt)
  8832. org-file-apps-defaults-windowsnt)
  8833. (t org-file-apps-defaults-gnu)))
  8834. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8835. "Convert extensions to regular expressions in the cars of LIST.
  8836. Also, weed out any non-string entries, because the return value is used
  8837. only for regexp matching.
  8838. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8839. point to the symbol `emacs', indicating that the file should
  8840. be opened in Emacs."
  8841. (append
  8842. (delq nil
  8843. (mapcar (lambda (x)
  8844. (if (not (stringp (car x)))
  8845. nil
  8846. (if (string-match "\\W" (car x))
  8847. x
  8848. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8849. list))
  8850. (if add-auto-mode
  8851. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8852. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8853. (defun org-file-remote-p (file)
  8854. "Test whether FILE specifies a location on a remote system.
  8855. Return non-nil if the location is indeed remote.
  8856. For example, the filename \"/user@host:/foo\" specifies a location
  8857. on the system \"/user@host:\"."
  8858. (cond ((fboundp 'file-remote-p)
  8859. (file-remote-p file))
  8860. ((fboundp 'tramp-handle-file-remote-p)
  8861. (tramp-handle-file-remote-p file))
  8862. ((and (boundp 'ange-ftp-name-format)
  8863. (string-match (car ange-ftp-name-format) file))
  8864. t)
  8865. (t nil)))
  8866. ;;;; Refiling
  8867. (defun org-get-org-file ()
  8868. "Read a filename, with default directory `org-directory'."
  8869. (let ((default (or org-default-notes-file remember-data-file)))
  8870. (read-file-name (format "File name [%s]: " default)
  8871. (file-name-as-directory org-directory)
  8872. default)))
  8873. (defun org-notes-order-reversed-p ()
  8874. "Check if the current file should receive notes in reversed order."
  8875. (cond
  8876. ((not org-reverse-note-order) nil)
  8877. ((eq t org-reverse-note-order) t)
  8878. ((not (listp org-reverse-note-order)) nil)
  8879. (t (catch 'exit
  8880. (let ((all org-reverse-note-order)
  8881. entry)
  8882. (while (setq entry (pop all))
  8883. (if (string-match (car entry) buffer-file-name)
  8884. (throw 'exit (cdr entry))))
  8885. nil)))))
  8886. (defvar org-refile-target-table nil
  8887. "The list of refile targets, created by `org-refile'.")
  8888. (defvar org-agenda-new-buffers nil
  8889. "Buffers created to visit agenda files.")
  8890. (defvar org-refile-cache nil
  8891. "Cache for refile targets.")
  8892. (defvar org-refile-markers nil
  8893. "All the markers used for caching refile locations.")
  8894. (defun org-refile-marker (pos)
  8895. "Get a new refile marker, but only if caching is in use."
  8896. (if (not org-refile-use-cache)
  8897. pos
  8898. (let ((m (make-marker)))
  8899. (move-marker m pos)
  8900. (push m org-refile-markers)
  8901. m)))
  8902. (defun org-refile-cache-clear ()
  8903. "Clear the refile cache and disable all the markers."
  8904. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8905. (setq org-refile-markers nil)
  8906. (setq org-refile-cache nil)
  8907. (message "Refile cache has been cleared"))
  8908. (defun org-refile-cache-check-set (set)
  8909. "Check if all the markers in the cache still have live buffers."
  8910. (let (marker)
  8911. (catch 'exit
  8912. (while (and set (setq marker (nth 3 (pop set))))
  8913. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8914. (when (and marker (null (marker-buffer marker)))
  8915. (message "not found") (sit-for 3)
  8916. (throw 'exit nil)))
  8917. t)))
  8918. (defun org-refile-cache-put (set &rest identifiers)
  8919. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8920. (let* ((key (sha1 (prin1-to-string identifiers)))
  8921. (entry (assoc key org-refile-cache)))
  8922. (if entry
  8923. (setcdr entry set)
  8924. (push (cons key set) org-refile-cache))))
  8925. (defun org-refile-cache-get (&rest identifiers)
  8926. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8927. (cond
  8928. ((not org-refile-cache) nil)
  8929. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8930. (t
  8931. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8932. org-refile-cache))))
  8933. (and set (org-refile-cache-check-set set) set)))))
  8934. (defun org-get-refile-targets (&optional default-buffer)
  8935. "Produce a table with refile targets."
  8936. (let ((case-fold-search nil)
  8937. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8938. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8939. targets tgs txt re files f desc descre fast-path-p level pos0)
  8940. (message "Getting targets...")
  8941. (with-current-buffer (or default-buffer (current-buffer))
  8942. (while (setq entry (pop entries))
  8943. (setq files (car entry) desc (cdr entry))
  8944. (setq fast-path-p nil)
  8945. (cond
  8946. ((null files) (setq files (list (current-buffer))))
  8947. ((eq files 'org-agenda-files)
  8948. (setq files (org-agenda-files 'unrestricted)))
  8949. ((and (symbolp files) (fboundp files))
  8950. (setq files (funcall files)))
  8951. ((and (symbolp files) (boundp files))
  8952. (setq files (symbol-value files))))
  8953. (if (stringp files) (setq files (list files)))
  8954. (cond
  8955. ((eq (car desc) :tag)
  8956. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8957. ((eq (car desc) :todo)
  8958. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8959. ((eq (car desc) :regexp)
  8960. (setq descre (cdr desc)))
  8961. ((eq (car desc) :level)
  8962. (setq descre (concat "^\\*\\{" (number-to-string
  8963. (if org-odd-levels-only
  8964. (1- (* 2 (cdr desc)))
  8965. (cdr desc)))
  8966. "\\}[ \t]")))
  8967. ((eq (car desc) :maxlevel)
  8968. (setq fast-path-p t)
  8969. (setq descre (concat "^\\*\\{1," (number-to-string
  8970. (if org-odd-levels-only
  8971. (1- (* 2 (cdr desc)))
  8972. (cdr desc)))
  8973. "\\}[ \t]")))
  8974. (t (error "Bad refiling target description %s" desc)))
  8975. (while (setq f (pop files))
  8976. (with-current-buffer
  8977. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8978. (or
  8979. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8980. (progn
  8981. (if (bufferp f) (setq f (buffer-file-name
  8982. (buffer-base-buffer f))))
  8983. (setq f (and f (expand-file-name f)))
  8984. (if (eq org-refile-use-outline-path 'file)
  8985. (push (list (file-name-nondirectory f) f nil nil) tgs))
  8986. (save-excursion
  8987. (save-restriction
  8988. (widen)
  8989. (goto-char (point-min))
  8990. (while (re-search-forward descre nil t)
  8991. (goto-char (setq pos0 (point-at-bol)))
  8992. (catch 'next
  8993. (when org-refile-target-verify-function
  8994. (save-match-data
  8995. (or (funcall org-refile-target-verify-function)
  8996. (throw 'next t))))
  8997. (when (looking-at org-complex-heading-regexp)
  8998. (setq level (org-reduced-level
  8999. (- (match-end 1) (match-beginning 1)))
  9000. txt (org-link-display-format (match-string 4))
  9001. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9002. re (format org-complex-heading-regexp-format
  9003. (regexp-quote (match-string 4))))
  9004. (when org-refile-use-outline-path
  9005. (setq txt (mapconcat
  9006. 'org-protect-slash
  9007. (append
  9008. (if (eq org-refile-use-outline-path
  9009. 'file)
  9010. (list (file-name-nondirectory
  9011. (buffer-file-name
  9012. (buffer-base-buffer))))
  9013. (if (eq org-refile-use-outline-path
  9014. 'full-file-path)
  9015. (list (buffer-file-name
  9016. (buffer-base-buffer)))))
  9017. (org-get-outline-path fast-path-p
  9018. level txt)
  9019. (list txt))
  9020. "/")))
  9021. (push (list txt f re (org-refile-marker (point)))
  9022. tgs)))
  9023. (when (= (point) pos0)
  9024. ;; verification function has not moved point
  9025. (goto-char (point-at-eol))))))))
  9026. (when org-refile-use-cache
  9027. (org-refile-cache-put tgs (buffer-file-name) descre))
  9028. (setq targets (append tgs targets))
  9029. ))))
  9030. (message "Getting targets...done")
  9031. (nreverse targets)))
  9032. (defun org-protect-slash (s)
  9033. (while (string-match "/" s)
  9034. (setq s (replace-match "\\" t t s)))
  9035. s)
  9036. (defvar org-olpa (make-vector 20 nil))
  9037. (defun org-get-outline-path (&optional fastp level heading)
  9038. "Return the outline path to the current entry, as a list.
  9039. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9040. routine which makes outline path derivations for an entire file,
  9041. avoiding backtracing. Refile target collection makes use of that."
  9042. (if fastp
  9043. (progn
  9044. (if (> level 19)
  9045. (error "Outline path failure, more than 19 levels"))
  9046. (loop for i from level upto 19 do
  9047. (aset org-olpa i nil))
  9048. (prog1
  9049. (delq nil (append org-olpa nil))
  9050. (aset org-olpa level heading)))
  9051. (let (rtn case-fold-search)
  9052. (save-excursion
  9053. (save-restriction
  9054. (widen)
  9055. (while (org-up-heading-safe)
  9056. (when (looking-at org-complex-heading-regexp)
  9057. (push (org-match-string-no-properties 4) rtn)))
  9058. rtn)))))
  9059. (defun org-format-outline-path (path &optional width prefix)
  9060. "Format the outline path PATH for display.
  9061. Width is the maximum number of characters that is available.
  9062. Prefix is a prefix to be included in the returned string,
  9063. such as the file name."
  9064. (setq width (or width 79))
  9065. (if prefix (setq width (- width (length prefix))))
  9066. (if (not path)
  9067. (or prefix "")
  9068. (let* ((nsteps (length path))
  9069. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9070. (maxwidth (if (<= total-width width)
  9071. 10000 ;; everything fits
  9072. ;; we need to shorten the level headings
  9073. (/ (- width nsteps) nsteps)))
  9074. (org-odd-levels-only nil)
  9075. (n 0)
  9076. (total (1+ (length prefix))))
  9077. (setq maxwidth (max maxwidth 10))
  9078. (concat prefix
  9079. (mapconcat
  9080. (lambda (h)
  9081. (setq n (1+ n))
  9082. (if (and (= n nsteps) (< maxwidth 10000))
  9083. (setq maxwidth (- total-width total)))
  9084. (if (< (length h) maxwidth)
  9085. (progn (setq total (+ total (length h) 1)) h)
  9086. (setq h (substring h 0 (- maxwidth 2))
  9087. total (+ total maxwidth 1))
  9088. (if (string-match "[ \t]+\\'" h)
  9089. (setq h (substring h 0 (match-beginning 0))))
  9090. (setq h (concat h "..")))
  9091. (org-add-props h nil 'face
  9092. (nth (% (1- n) org-n-level-faces)
  9093. org-level-faces))
  9094. h)
  9095. path "/")))))
  9096. (defun org-display-outline-path (&optional file current)
  9097. "Display the current outline path in the echo area."
  9098. (interactive "P")
  9099. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9100. (case-fold-search nil)
  9101. (path (and (org-mode-p) (org-get-outline-path))))
  9102. (if current (setq path (append path
  9103. (save-excursion
  9104. (org-back-to-heading t)
  9105. (if (looking-at org-complex-heading-regexp)
  9106. (list (match-string 4)))))))
  9107. (message "%s"
  9108. (org-format-outline-path
  9109. path
  9110. (1- (frame-width))
  9111. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9112. (defvar org-refile-history nil
  9113. "History for refiling operations.")
  9114. (defvar org-after-refile-insert-hook nil
  9115. "Hook run after `org-refile' has inserted its stuff at the new location.
  9116. Note that this is still *before* the stuff will be removed from
  9117. the *old* location.")
  9118. (defvar org-capture-last-stored-marker)
  9119. (defun org-refile (&optional goto default-buffer rfloc)
  9120. "Move the entry at point to another heading.
  9121. The list of target headings is compiled using the information in
  9122. `org-refile-targets', which see. This list is created before each use
  9123. and will therefore always be up-to-date.
  9124. At the target location, the entry is filed as a subitem of the target heading.
  9125. Depending on `org-reverse-note-order', the new subitem will either be the
  9126. first or the last subitem.
  9127. If there is an active region, all entries in that region will be moved.
  9128. However, the region must fulfill the requirement that the first heading
  9129. is the first one sets the top-level of the moved text - at most siblings
  9130. below it are allowed.
  9131. With prefix arg GOTO, the command will only visit the target location,
  9132. not actually move anything.
  9133. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9134. go to the location where the last refiling
  9135. operation has put the subtree.
  9136. With a prefix argument of `2', refile to the running clock.
  9137. RFLOC can be a refile location obtained in a different way.
  9138. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9139. If you are using target caching (see `org-refile-use-cache'),
  9140. You have to clear the target cache in order to find new targets.
  9141. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9142. (interactive "P")
  9143. (if (member goto '(0 (64)))
  9144. (org-refile-cache-clear)
  9145. (let* ((cbuf (current-buffer))
  9146. (regionp (org-region-active-p))
  9147. (region-start (and regionp (region-beginning)))
  9148. (region-end (and regionp (region-end)))
  9149. (region-length (and regionp (- region-end region-start)))
  9150. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9151. pos it nbuf file re level reversed)
  9152. (setq last-command nil)
  9153. (when regionp
  9154. (goto-char region-start)
  9155. (or (bolp) (goto-char (point-at-bol)))
  9156. (setq region-start (point))
  9157. (unless (org-kill-is-subtree-p
  9158. (buffer-substring region-start region-end))
  9159. (error "The region is not a (sequence of) subtree(s)")))
  9160. (if (equal goto '(16))
  9161. (org-refile-goto-last-stored)
  9162. (when (or
  9163. (and (equal goto 2)
  9164. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9165. (prog1
  9166. (setq it (list (or org-clock-heading "running clock")
  9167. (buffer-file-name
  9168. (marker-buffer org-clock-hd-marker))
  9169. ""
  9170. (marker-position org-clock-hd-marker)))
  9171. (setq goto nil)))
  9172. (setq it (or rfloc
  9173. (save-excursion
  9174. (org-refile-get-location
  9175. (if goto "Goto: " "Refile to: ") default-buffer
  9176. org-refile-allow-creating-parent-nodes)))))
  9177. (setq file (nth 1 it)
  9178. re (nth 2 it)
  9179. pos (nth 3 it))
  9180. (if (and (not goto)
  9181. pos
  9182. (equal (buffer-file-name) file)
  9183. (if regionp
  9184. (and (>= pos region-start)
  9185. (<= pos region-end))
  9186. (and (>= pos (point))
  9187. (< pos (save-excursion
  9188. (org-end-of-subtree t t))))))
  9189. (error "Cannot refile to position inside the tree or region"))
  9190. (setq nbuf (or (find-buffer-visiting file)
  9191. (find-file-noselect file)))
  9192. (if goto
  9193. (progn
  9194. (switch-to-buffer nbuf)
  9195. (goto-char pos)
  9196. (org-show-context 'org-goto))
  9197. (if regionp
  9198. (progn
  9199. (org-kill-new (buffer-substring region-start region-end))
  9200. (org-save-markers-in-region region-start region-end))
  9201. (org-copy-subtree 1 nil t))
  9202. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9203. (find-file-noselect file)))
  9204. (setq reversed (org-notes-order-reversed-p))
  9205. (save-excursion
  9206. (save-restriction
  9207. (widen)
  9208. (if pos
  9209. (progn
  9210. (goto-char pos)
  9211. (looking-at outline-regexp)
  9212. (setq level (org-get-valid-level (funcall outline-level) 1))
  9213. (goto-char
  9214. (if reversed
  9215. (or (outline-next-heading) (point-max))
  9216. (or (save-excursion (org-get-next-sibling))
  9217. (org-end-of-subtree t t)
  9218. (point-max)))))
  9219. (setq level 1)
  9220. (if (not reversed)
  9221. (goto-char (point-max))
  9222. (goto-char (point-min))
  9223. (or (outline-next-heading) (goto-char (point-max)))))
  9224. (if (not (bolp)) (newline))
  9225. (org-paste-subtree level)
  9226. (when org-log-refile
  9227. (org-add-log-setup 'refile nil nil 'findpos
  9228. org-log-refile)
  9229. (unless (eq org-log-refile 'note)
  9230. (save-excursion (org-add-log-note))))
  9231. (and org-auto-align-tags (org-set-tags nil t))
  9232. (bookmark-set "org-refile-last-stored")
  9233. ;; If we are refiling for capture, make sure that the
  9234. ;; last-capture pointers point here
  9235. (when (org-bound-and-true-p org-refile-for-capture)
  9236. (bookmark-set "org-capture-last-stored-marker")
  9237. (move-marker org-capture-last-stored-marker (point)))
  9238. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9239. (run-hooks 'org-after-refile-insert-hook))))
  9240. (if regionp
  9241. (delete-region (point) (+ (point) region-length))
  9242. (org-cut-subtree))
  9243. (when (featurep 'org-inlinetask)
  9244. (org-inlinetask-remove-END-maybe))
  9245. (setq org-markers-to-move nil)
  9246. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9247. (defun org-refile-goto-last-stored ()
  9248. "Go to the location where the last refile was stored."
  9249. (interactive)
  9250. (bookmark-jump "org-refile-last-stored")
  9251. (message "This is the location of the last refile"))
  9252. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9253. "Prompt the user for a refile location, using PROMPT."
  9254. (let ((org-refile-targets org-refile-targets)
  9255. (org-refile-use-outline-path org-refile-use-outline-path))
  9256. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  9257. (unless org-refile-target-table
  9258. (error "No refile targets"))
  9259. (let* ((cbuf (current-buffer))
  9260. (partial-completion-mode nil)
  9261. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9262. (cfunc (if (and org-refile-use-outline-path
  9263. org-outline-path-complete-in-steps)
  9264. 'org-olpath-completing-read
  9265. 'org-icompleting-read))
  9266. (extra (if org-refile-use-outline-path "/" ""))
  9267. (filename (and cfn (expand-file-name cfn)))
  9268. (tbl (mapcar
  9269. (lambda (x)
  9270. (if (and (not (member org-refile-use-outline-path
  9271. '(file full-file-path)))
  9272. (not (equal filename (nth 1 x))))
  9273. (cons (concat (car x) extra " ("
  9274. (file-name-nondirectory (nth 1 x)) ")")
  9275. (cdr x))
  9276. (cons (concat (car x) extra) (cdr x))))
  9277. org-refile-target-table))
  9278. (completion-ignore-case t)
  9279. pa answ parent-target child parent old-hist)
  9280. (setq old-hist org-refile-history)
  9281. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9282. nil 'org-refile-history))
  9283. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9284. (org-refile-check-position pa)
  9285. (if pa
  9286. (progn
  9287. (when (or (not org-refile-history)
  9288. (not (eq old-hist org-refile-history))
  9289. (not (equal (car pa) (car org-refile-history))))
  9290. (setq org-refile-history
  9291. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9292. org-refile-history
  9293. (cdr org-refile-history))))
  9294. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9295. (pop org-refile-history)))
  9296. pa)
  9297. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9298. (progn
  9299. (setq parent (match-string 1 answ)
  9300. child (match-string 2 answ))
  9301. (setq parent-target (or (assoc parent tbl)
  9302. (assoc (concat parent "/") tbl)))
  9303. (when (and parent-target
  9304. (or (eq new-nodes t)
  9305. (and (eq new-nodes 'confirm)
  9306. (y-or-n-p (format "Create new node \"%s\"? "
  9307. child)))))
  9308. (org-refile-new-child parent-target child)))
  9309. (error "Invalid target location")))))
  9310. (defun org-refile-check-position (refile-pointer)
  9311. "Check if the refile pointer matches the readline to which it points."
  9312. (let* ((file (nth 1 refile-pointer))
  9313. (re (nth 2 refile-pointer))
  9314. (pos (nth 3 refile-pointer))
  9315. buffer)
  9316. (when (org-string-nw-p re)
  9317. (setq buffer (if (markerp pos)
  9318. (marker-buffer pos)
  9319. (or (find-buffer-visiting file)
  9320. (find-file-noselect file))))
  9321. (with-current-buffer buffer
  9322. (save-excursion
  9323. (save-restriction
  9324. (widen)
  9325. (goto-char pos)
  9326. (beginning-of-line 1)
  9327. (unless (org-looking-at-p re)
  9328. (error "Invalid refile position, please rebuild the cache"))))))))
  9329. (defun org-refile-new-child (parent-target child)
  9330. "Use refile target PARENT-TARGET to add new CHILD below it."
  9331. (unless parent-target
  9332. (error "Cannot find parent for new node"))
  9333. (let ((file (nth 1 parent-target))
  9334. (pos (nth 3 parent-target))
  9335. level)
  9336. (with-current-buffer (or (find-buffer-visiting file)
  9337. (find-file-noselect file))
  9338. (save-excursion
  9339. (save-restriction
  9340. (widen)
  9341. (if pos
  9342. (goto-char pos)
  9343. (goto-char (point-max))
  9344. (if (not (bolp)) (newline)))
  9345. (when (looking-at outline-regexp)
  9346. (setq level (funcall outline-level))
  9347. (org-end-of-subtree t t))
  9348. (org-back-over-empty-lines)
  9349. (insert "\n" (make-string
  9350. (if pos (org-get-valid-level level 1) 1) ?*)
  9351. " " child "\n")
  9352. (beginning-of-line 0)
  9353. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9354. (defun org-olpath-completing-read (prompt collection &rest args)
  9355. "Read an outline path like a file name."
  9356. (let ((thetable collection)
  9357. (org-completion-use-ido nil) ; does not work with ido.
  9358. (org-completion-use-iswitchb nil)) ; or iswitchb
  9359. (apply
  9360. 'org-icompleting-read prompt
  9361. (lambda (string predicate &optional flag)
  9362. (let (rtn r f (l (length string)))
  9363. (cond
  9364. ((eq flag nil)
  9365. ;; try completion
  9366. (try-completion string thetable))
  9367. ((eq flag t)
  9368. ;; all-completions
  9369. (setq rtn (all-completions string thetable predicate))
  9370. (mapcar
  9371. (lambda (x)
  9372. (setq r (substring x l))
  9373. (if (string-match " ([^)]*)$" x)
  9374. (setq f (match-string 0 x))
  9375. (setq f ""))
  9376. (if (string-match "/" r)
  9377. (concat string (substring r 0 (match-end 0)) f)
  9378. x))
  9379. rtn))
  9380. ((eq flag 'lambda)
  9381. ;; exact match?
  9382. (assoc string thetable)))
  9383. ))
  9384. args)))
  9385. ;;;; Dynamic blocks
  9386. (defun org-find-dblock (name)
  9387. "Find the first dynamic block with name NAME in the buffer.
  9388. If not found, stay at current position and return nil."
  9389. (let (pos)
  9390. (save-excursion
  9391. (goto-char (point-min))
  9392. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9393. nil t)
  9394. (match-beginning 0))))
  9395. (if pos (goto-char pos))
  9396. pos))
  9397. (defconst org-dblock-start-re
  9398. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9399. "Matches the start line of a dynamic block, with parameters.")
  9400. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9401. "Matches the end of a dynamic block.")
  9402. (defun org-create-dblock (plist)
  9403. "Create a dynamic block section, with parameters taken from PLIST.
  9404. PLIST must contain a :name entry which is used as name of the block."
  9405. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9406. (end-of-line 1)
  9407. (newline))
  9408. (let ((col (current-column))
  9409. (name (plist-get plist :name)))
  9410. (insert "#+BEGIN: " name)
  9411. (while plist
  9412. (if (eq (car plist) :name)
  9413. (setq plist (cddr plist))
  9414. (insert " " (prin1-to-string (pop plist)))))
  9415. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9416. (beginning-of-line -2)))
  9417. (defun org-prepare-dblock ()
  9418. "Prepare dynamic block for refresh.
  9419. This empties the block, puts the cursor at the insert position and returns
  9420. the property list including an extra property :name with the block name."
  9421. (unless (looking-at org-dblock-start-re)
  9422. (error "Not at a dynamic block"))
  9423. (let* ((begdel (1+ (match-end 0)))
  9424. (name (org-no-properties (match-string 1)))
  9425. (params (append (list :name name)
  9426. (read (concat "(" (match-string 3) ")")))))
  9427. (save-excursion
  9428. (beginning-of-line 1)
  9429. (skip-chars-forward " \t")
  9430. (setq params (plist-put params :indentation-column (current-column))))
  9431. (unless (re-search-forward org-dblock-end-re nil t)
  9432. (error "Dynamic block not terminated"))
  9433. (setq params
  9434. (append params
  9435. (list :content (buffer-substring
  9436. begdel (match-beginning 0)))))
  9437. (delete-region begdel (match-beginning 0))
  9438. (goto-char begdel)
  9439. (open-line 1)
  9440. params))
  9441. (defun org-map-dblocks (&optional command)
  9442. "Apply COMMAND to all dynamic blocks in the current buffer.
  9443. If COMMAND is not given, use `org-update-dblock'."
  9444. (let ((cmd (or command 'org-update-dblock)))
  9445. (save-excursion
  9446. (goto-char (point-min))
  9447. (while (re-search-forward org-dblock-start-re nil t)
  9448. (goto-char (match-beginning 0))
  9449. (save-excursion
  9450. (condition-case nil
  9451. (funcall cmd)
  9452. (error (message "Error during update of dynamic block"))))
  9453. (unless (re-search-forward org-dblock-end-re nil t)
  9454. (error "Dynamic block not terminated"))))))
  9455. (defun org-dblock-update (&optional arg)
  9456. "User command for updating dynamic blocks.
  9457. Update the dynamic block at point. With prefix ARG, update all dynamic
  9458. blocks in the buffer."
  9459. (interactive "P")
  9460. (if arg
  9461. (org-update-all-dblocks)
  9462. (or (looking-at org-dblock-start-re)
  9463. (org-beginning-of-dblock))
  9464. (org-update-dblock)))
  9465. (defun org-update-dblock ()
  9466. "Update the dynamic block at point.
  9467. This means to empty the block, parse for parameters and then call
  9468. the correct writing function."
  9469. (interactive)
  9470. (save-window-excursion
  9471. (let* ((pos (point))
  9472. (line (org-current-line))
  9473. (params (org-prepare-dblock))
  9474. (name (plist-get params :name))
  9475. (indent (plist-get params :indentation-column))
  9476. (cmd (intern (concat "org-dblock-write:" name))))
  9477. (message "Updating dynamic block `%s' at line %d..." name line)
  9478. (funcall cmd params)
  9479. (message "Updating dynamic block `%s' at line %d...done" name line)
  9480. (goto-char pos)
  9481. (when (and indent (> indent 0))
  9482. (setq indent (make-string indent ?\ ))
  9483. (save-excursion
  9484. (org-beginning-of-dblock)
  9485. (forward-line 1)
  9486. (while (not (looking-at org-dblock-end-re))
  9487. (insert indent)
  9488. (beginning-of-line 2))
  9489. (when (looking-at org-dblock-end-re)
  9490. (and (looking-at "[ \t]+")
  9491. (replace-match ""))
  9492. (insert indent)))))))
  9493. (defun org-beginning-of-dblock ()
  9494. "Find the beginning of the dynamic block at point.
  9495. Error if there is no such block at point."
  9496. (let ((pos (point))
  9497. beg)
  9498. (end-of-line 1)
  9499. (if (and (re-search-backward org-dblock-start-re nil t)
  9500. (setq beg (match-beginning 0))
  9501. (re-search-forward org-dblock-end-re nil t)
  9502. (> (match-end 0) pos))
  9503. (goto-char beg)
  9504. (goto-char pos)
  9505. (error "Not in a dynamic block"))))
  9506. (defun org-update-all-dblocks ()
  9507. "Update all dynamic blocks in the buffer.
  9508. This function can be used in a hook."
  9509. (interactive)
  9510. (when (org-mode-p)
  9511. (org-map-dblocks 'org-update-dblock)))
  9512. ;;;; Completion
  9513. (defconst org-additional-option-like-keywords
  9514. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  9515. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  9516. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9517. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  9518. "BEGIN:" "END:"
  9519. "ORGTBL" "TBLFM:" "TBLNAME:"
  9520. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9521. "BEGIN_QUOTE" "END_QUOTE"
  9522. "BEGIN_VERSE" "END_VERSE"
  9523. "BEGIN_CENTER" "END_CENTER"
  9524. "BEGIN_SRC" "END_SRC"
  9525. "CATEGORY" "COLUMNS" "PROPERTY"
  9526. "CAPTION" "LABEL"
  9527. "SETUPFILE"
  9528. "BIND"
  9529. "MACRO"))
  9530. (defcustom org-structure-template-alist
  9531. '(
  9532. ("s" "#+begin_src ?\n\n#+end_src"
  9533. "<src lang=\"?\">\n\n</src>")
  9534. ("e" "#+begin_example\n?\n#+end_example"
  9535. "<example>\n?\n</example>")
  9536. ("q" "#+begin_quote\n?\n#+end_quote"
  9537. "<quote>\n?\n</quote>")
  9538. ("v" "#+begin_verse\n?\n#+end_verse"
  9539. "<verse>\n?\n/verse>")
  9540. ("c" "#+begin_center\n?\n#+end_center"
  9541. "<center>\n?\n/center>")
  9542. ("l" "#+begin_latex\n?\n#+end_latex"
  9543. "<literal style=\"latex\">\n?\n</literal>")
  9544. ("L" "#+latex: "
  9545. "<literal style=\"latex\">?</literal>")
  9546. ("h" "#+begin_html\n?\n#+end_html"
  9547. "<literal style=\"html\">\n?\n</literal>")
  9548. ("H" "#+html: "
  9549. "<literal style=\"html\">?</literal>")
  9550. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9551. ("A" "#+ascii: ")
  9552. ("i" "#+include %file ?"
  9553. "<include file=%file markup=\"?\">")
  9554. )
  9555. "Structure completion elements.
  9556. This is a list of abbreviation keys and values. The value gets inserted
  9557. if you type `<' followed by the key and then press the completion key,
  9558. usually `M-TAB'. %file will be replaced by a file name after prompting
  9559. for the file using completion.
  9560. There are two templates for each key, the first uses the original Org syntax,
  9561. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9562. the default when the /org-mtags.el/ module has been loaded. See also the
  9563. variable `org-mtags-prefer-muse-templates'.
  9564. This is an experimental feature, it is undecided if it is going to stay in."
  9565. :group 'org-completion
  9566. :type '(repeat
  9567. (string :tag "Key")
  9568. (string :tag "Template")
  9569. (string :tag "Muse Template")))
  9570. (defun org-try-structure-completion ()
  9571. "Try to complete a structure template before point.
  9572. This looks for strings like \"<e\" on an otherwise empty line and
  9573. expands them."
  9574. (let ((l (buffer-substring (point-at-bol) (point)))
  9575. a)
  9576. (when (and (looking-at "[ \t]*$")
  9577. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9578. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9579. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9580. (match-beginning 1)) a)
  9581. t)))
  9582. (defun org-complete-expand-structure-template (start cell)
  9583. "Expand a structure template."
  9584. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9585. (rpl (nth (if musep 2 1) cell))
  9586. (ind ""))
  9587. (delete-region start (point))
  9588. (when (string-match "\\`#\\+" rpl)
  9589. (cond
  9590. ((bolp))
  9591. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9592. (setq ind (buffer-substring (point-at-bol) (point))))
  9593. (t (newline))))
  9594. (setq start (point))
  9595. (if (string-match "%file" rpl)
  9596. (setq rpl (replace-match
  9597. (concat
  9598. "\""
  9599. (save-match-data
  9600. (abbreviate-file-name (read-file-name "Include file: ")))
  9601. "\"")
  9602. t t rpl)))
  9603. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9604. (concat "\n" ind)))
  9605. (insert rpl)
  9606. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9607. ;;;; TODO, DEADLINE, Comments
  9608. (defun org-toggle-comment ()
  9609. "Change the COMMENT state of an entry."
  9610. (interactive)
  9611. (save-excursion
  9612. (org-back-to-heading)
  9613. (let (case-fold-search)
  9614. (if (looking-at (concat outline-regexp
  9615. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9616. (replace-match "" t t nil 1)
  9617. (if (looking-at outline-regexp)
  9618. (progn
  9619. (goto-char (match-end 0))
  9620. (insert org-comment-string " ")))))))
  9621. (defvar org-last-todo-state-is-todo nil
  9622. "This is non-nil when the last TODO state change led to a TODO state.
  9623. If the last change removed the TODO tag or switched to DONE, then
  9624. this is nil.")
  9625. (defvar org-setting-tags nil) ; dynamically skipped
  9626. (defvar org-todo-setup-filter-hook nil
  9627. "Hook for functions that pre-filter todo specs.
  9628. Each function takes a todo spec and returns either nil or the spec
  9629. transformed into canonical form." )
  9630. (defvar org-todo-get-default-hook nil
  9631. "Hook for functions that get a default item for todo.
  9632. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9633. nil or a string to be used for the todo mark." )
  9634. (defvar org-agenda-headline-snapshot-before-repeat)
  9635. (defun org-todo (&optional arg)
  9636. "Change the TODO state of an item.
  9637. The state of an item is given by a keyword at the start of the heading,
  9638. like
  9639. *** TODO Write paper
  9640. *** DONE Call mom
  9641. The different keywords are specified in the variable `org-todo-keywords'.
  9642. By default the available states are \"TODO\" and \"DONE\".
  9643. So for this example: when the item starts with TODO, it is changed to DONE.
  9644. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9645. DONE are present, add TODO at the beginning of the heading.
  9646. With \\[universal-argument] prefix arg, use completion to determine the new \
  9647. state.
  9648. With numeric prefix arg, switch to that state.
  9649. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9650. keywords (nextset).
  9651. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9652. For calling through lisp, arg is also interpreted in the following way:
  9653. 'none -> empty state
  9654. \"\"(empty string) -> switch to empty state
  9655. 'done -> switch to DONE
  9656. 'nextset -> switch to the next set of keywords
  9657. 'previousset -> switch to the previous set of keywords
  9658. \"WAITING\" -> switch to the specified keyword, but only if it
  9659. really is a member of `org-todo-keywords'."
  9660. (interactive "P")
  9661. (if (equal arg '(16)) (setq arg 'nextset))
  9662. (let ((org-blocker-hook org-blocker-hook)
  9663. (case-fold-search nil))
  9664. (when (equal arg '(64))
  9665. (setq arg nil org-blocker-hook nil))
  9666. (when (and org-blocker-hook
  9667. (or org-inhibit-blocking
  9668. (org-entry-get nil "NOBLOCKING")))
  9669. (setq org-blocker-hook nil))
  9670. (save-excursion
  9671. (catch 'exit
  9672. (org-back-to-heading t)
  9673. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9674. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9675. (looking-at " *"))
  9676. (let* ((match-data (match-data))
  9677. (startpos (point-at-bol))
  9678. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9679. (org-log-done org-log-done)
  9680. (org-log-repeat org-log-repeat)
  9681. (org-todo-log-states org-todo-log-states)
  9682. (this (match-string 1))
  9683. (hl-pos (match-beginning 0))
  9684. (head (org-get-todo-sequence-head this))
  9685. (ass (assoc head org-todo-kwd-alist))
  9686. (interpret (nth 1 ass))
  9687. (done-word (nth 3 ass))
  9688. (final-done-word (nth 4 ass))
  9689. (last-state (or this ""))
  9690. (completion-ignore-case t)
  9691. (member (member this org-todo-keywords-1))
  9692. (tail (cdr member))
  9693. (state (cond
  9694. ((and org-todo-key-trigger
  9695. (or (and (equal arg '(4))
  9696. (eq org-use-fast-todo-selection 'prefix))
  9697. (and (not arg) org-use-fast-todo-selection
  9698. (not (eq org-use-fast-todo-selection
  9699. 'prefix)))))
  9700. ;; Use fast selection
  9701. (org-fast-todo-selection))
  9702. ((and (equal arg '(4))
  9703. (or (not org-use-fast-todo-selection)
  9704. (not org-todo-key-trigger)))
  9705. ;; Read a state with completion
  9706. (org-icompleting-read
  9707. "State: " (mapcar (lambda(x) (list x))
  9708. org-todo-keywords-1)
  9709. nil t))
  9710. ((eq arg 'right)
  9711. (if this
  9712. (if tail (car tail) nil)
  9713. (car org-todo-keywords-1)))
  9714. ((eq arg 'left)
  9715. (if (equal member org-todo-keywords-1)
  9716. nil
  9717. (if this
  9718. (nth (- (length org-todo-keywords-1)
  9719. (length tail) 2)
  9720. org-todo-keywords-1)
  9721. (org-last org-todo-keywords-1))))
  9722. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9723. (setq arg nil))) ; hack to fall back to cycling
  9724. (arg
  9725. ;; user or caller requests a specific state
  9726. (cond
  9727. ((equal arg "") nil)
  9728. ((eq arg 'none) nil)
  9729. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9730. ((eq arg 'nextset)
  9731. (or (car (cdr (member head org-todo-heads)))
  9732. (car org-todo-heads)))
  9733. ((eq arg 'previousset)
  9734. (let ((org-todo-heads (reverse org-todo-heads)))
  9735. (or (car (cdr (member head org-todo-heads)))
  9736. (car org-todo-heads))))
  9737. ((car (member arg org-todo-keywords-1)))
  9738. ((stringp arg)
  9739. (error "State `%s' not valid in this file" arg))
  9740. ((nth (1- (prefix-numeric-value arg))
  9741. org-todo-keywords-1))))
  9742. ((null member) (or head (car org-todo-keywords-1)))
  9743. ((equal this final-done-word) nil) ;; -> make empty
  9744. ((null tail) nil) ;; -> first entry
  9745. ((memq interpret '(type priority))
  9746. (if (eq this-command last-command)
  9747. (car tail)
  9748. (if (> (length tail) 0)
  9749. (or done-word (car org-done-keywords))
  9750. nil)))
  9751. (t
  9752. (car tail))))
  9753. (state (or
  9754. (run-hook-with-args-until-success
  9755. 'org-todo-get-default-hook state last-state)
  9756. state))
  9757. (next (if state (concat " " state " ") " "))
  9758. (change-plist (list :type 'todo-state-change :from this :to state
  9759. :position startpos))
  9760. dolog now-done-p)
  9761. (when org-blocker-hook
  9762. (setq org-last-todo-state-is-todo
  9763. (not (member this org-done-keywords)))
  9764. (unless (save-excursion
  9765. (save-match-data
  9766. (run-hook-with-args-until-failure
  9767. 'org-blocker-hook change-plist)))
  9768. (if (interactive-p)
  9769. (error "TODO state change from %s to %s blocked" this state)
  9770. ;; fail silently
  9771. (message "TODO state change from %s to %s blocked" this state)
  9772. (throw 'exit nil))))
  9773. (store-match-data match-data)
  9774. (replace-match next t t)
  9775. (unless (pos-visible-in-window-p hl-pos)
  9776. (message "TODO state changed to %s" (org-trim next)))
  9777. (unless head
  9778. (setq head (org-get-todo-sequence-head state)
  9779. ass (assoc head org-todo-kwd-alist)
  9780. interpret (nth 1 ass)
  9781. done-word (nth 3 ass)
  9782. final-done-word (nth 4 ass)))
  9783. (when (memq arg '(nextset previousset))
  9784. (message "Keyword-Set %d/%d: %s"
  9785. (- (length org-todo-sets) -1
  9786. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9787. (length org-todo-sets)
  9788. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9789. (setq org-last-todo-state-is-todo
  9790. (not (member state org-done-keywords)))
  9791. (setq now-done-p (and (member state org-done-keywords)
  9792. (not (member this org-done-keywords))))
  9793. (and logging (org-local-logging logging))
  9794. (when (and (or org-todo-log-states org-log-done)
  9795. (not (eq org-inhibit-logging t))
  9796. (not (memq arg '(nextset previousset))))
  9797. ;; we need to look at recording a time and note
  9798. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9799. (nth 2 (assoc this org-todo-log-states))))
  9800. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9801. (setq dolog 'time))
  9802. (when (and state
  9803. (member state org-not-done-keywords)
  9804. (not (member this org-not-done-keywords)))
  9805. ;; This is now a todo state and was not one before
  9806. ;; If there was a CLOSED time stamp, get rid of it.
  9807. (org-add-planning-info nil nil 'closed))
  9808. (when (and now-done-p org-log-done)
  9809. ;; It is now done, and it was not done before
  9810. (org-add-planning-info 'closed (org-current-time))
  9811. (if (and (not dolog) (eq 'note org-log-done))
  9812. (org-add-log-setup 'done state this 'findpos 'note)))
  9813. (when (and state dolog)
  9814. ;; This is a non-nil state, and we need to log it
  9815. (org-add-log-setup 'state state this 'findpos dolog)))
  9816. ;; Fixup tag positioning
  9817. (org-todo-trigger-tag-changes state)
  9818. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9819. (when org-provide-todo-statistics
  9820. (org-update-parent-todo-statistics))
  9821. (run-hooks 'org-after-todo-state-change-hook)
  9822. (if (and arg (not (member state org-done-keywords)))
  9823. (setq head (org-get-todo-sequence-head state)))
  9824. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9825. ;; Do we need to trigger a repeat?
  9826. (when now-done-p
  9827. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9828. ;; This is for the agenda, take a snapshot of the headline.
  9829. (save-match-data
  9830. (setq org-agenda-headline-snapshot-before-repeat
  9831. (org-get-heading))))
  9832. (org-auto-repeat-maybe state))
  9833. ;; Fixup cursor location if close to the keyword
  9834. (if (and (outline-on-heading-p)
  9835. (not (bolp))
  9836. (save-excursion (beginning-of-line 1)
  9837. (looking-at org-todo-line-regexp))
  9838. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9839. (progn
  9840. (goto-char (or (match-end 2) (match-end 1)))
  9841. (and (looking-at " ") (just-one-space))))
  9842. (when org-trigger-hook
  9843. (save-excursion
  9844. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9845. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9846. "Block turning an entry into a TODO, using the hierarchy.
  9847. This checks whether the current task should be blocked from state
  9848. changes. Such blocking occurs when:
  9849. 1. The task has children which are not all in a completed state.
  9850. 2. A task has a parent with the property :ORDERED:, and there
  9851. are siblings prior to the current task with incomplete
  9852. status.
  9853. 3. The parent of the task is blocked because it has siblings that should
  9854. be done first, or is child of a block grandparent TODO entry."
  9855. (if (not org-enforce-todo-dependencies)
  9856. t ; if locally turned off don't block
  9857. (catch 'dont-block
  9858. ;; If this is not a todo state change, or if this entry is already DONE,
  9859. ;; do not block
  9860. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9861. (member (plist-get change-plist :from)
  9862. (cons 'done org-done-keywords))
  9863. (member (plist-get change-plist :to)
  9864. (cons 'todo org-not-done-keywords))
  9865. (not (plist-get change-plist :to)))
  9866. (throw 'dont-block t))
  9867. ;; If this task has children, and any are undone, it's blocked
  9868. (save-excursion
  9869. (org-back-to-heading t)
  9870. (let ((this-level (funcall outline-level)))
  9871. (outline-next-heading)
  9872. (let ((child-level (funcall outline-level)))
  9873. (while (and (not (eobp))
  9874. (> child-level this-level))
  9875. ;; this todo has children, check whether they are all
  9876. ;; completed
  9877. (if (and (not (org-entry-is-done-p))
  9878. (org-entry-is-todo-p))
  9879. (throw 'dont-block nil))
  9880. (outline-next-heading)
  9881. (setq child-level (funcall outline-level))))))
  9882. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9883. ;; any previous siblings are undone, it's blocked
  9884. (save-excursion
  9885. (org-back-to-heading t)
  9886. (let* ((pos (point))
  9887. (parent-pos (and (org-up-heading-safe) (point))))
  9888. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9889. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9890. (forward-line 1)
  9891. (re-search-forward org-not-done-heading-regexp pos t))
  9892. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9893. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9894. (while t
  9895. (goto-char parent-pos)
  9896. (if (not (looking-at org-not-done-heading-regexp))
  9897. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9898. (setq pos (point))
  9899. (setq parent-pos (and (org-up-heading-safe) (point)))
  9900. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9901. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9902. (forward-line 1)
  9903. (re-search-forward org-not-done-heading-regexp pos t))
  9904. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9905. (defcustom org-track-ordered-property-with-tag nil
  9906. "Should the ORDERED property also be shown as a tag?
  9907. The ORDERED property decides if an entry should require subtasks to be
  9908. completed in sequence. Since a property is not very visible, setting
  9909. this option means that toggling the ORDERED property with the command
  9910. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9911. not relevant for the behavior, but it makes things more visible.
  9912. Note that toggling the tag with tags commands will not change the property
  9913. and therefore not influence behavior!
  9914. This can be t, meaning the tag ORDERED should be used, It can also be a
  9915. string to select a different tag for this task."
  9916. :group 'org-todo
  9917. :type '(choice
  9918. (const :tag "No tracking" nil)
  9919. (const :tag "Track with ORDERED tag" t)
  9920. (string :tag "Use other tag")))
  9921. (defun org-toggle-ordered-property ()
  9922. "Toggle the ORDERED property of the current entry.
  9923. For better visibility, you can track the value of this property with a tag.
  9924. See variable `org-track-ordered-property-with-tag'."
  9925. (interactive)
  9926. (let* ((t1 org-track-ordered-property-with-tag)
  9927. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9928. (save-excursion
  9929. (org-back-to-heading)
  9930. (if (org-entry-get nil "ORDERED")
  9931. (progn
  9932. (org-delete-property "ORDERED")
  9933. (and tag (org-toggle-tag tag 'off))
  9934. (message "Subtasks can be completed in arbitrary order"))
  9935. (org-entry-put nil "ORDERED" "t")
  9936. (and tag (org-toggle-tag tag 'on))
  9937. (message "Subtasks must be completed in sequence")))))
  9938. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9939. (defun org-block-todo-from-checkboxes (change-plist)
  9940. "Block turning an entry into a TODO, using checkboxes.
  9941. This checks whether the current task should be blocked from state
  9942. changes because there are unchecked boxes in this entry."
  9943. (if (not org-enforce-todo-checkbox-dependencies)
  9944. t ; if locally turned off don't block
  9945. (catch 'dont-block
  9946. ;; If this is not a todo state change, or if this entry is already DONE,
  9947. ;; do not block
  9948. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9949. (member (plist-get change-plist :from)
  9950. (cons 'done org-done-keywords))
  9951. (member (plist-get change-plist :to)
  9952. (cons 'todo org-not-done-keywords))
  9953. (not (plist-get change-plist :to)))
  9954. (throw 'dont-block t))
  9955. ;; If this task has checkboxes that are not checked, it's blocked
  9956. (save-excursion
  9957. (org-back-to-heading t)
  9958. (let ((beg (point)) end)
  9959. (outline-next-heading)
  9960. (setq end (point))
  9961. (goto-char beg)
  9962. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9963. end t)
  9964. (progn
  9965. (if (boundp 'org-blocked-by-checkboxes)
  9966. (setq org-blocked-by-checkboxes t))
  9967. (throw 'dont-block nil)))))
  9968. t))) ; do not block
  9969. (defun org-entry-blocked-p ()
  9970. "Is the current entry blocked?"
  9971. (if (org-entry-get nil "NOBLOCKING")
  9972. nil ;; Never block this entry
  9973. (not
  9974. (run-hook-with-args-until-failure
  9975. 'org-blocker-hook
  9976. (list :type 'todo-state-change
  9977. :position (point)
  9978. :from 'todo
  9979. :to 'done)))))
  9980. (defun org-update-statistics-cookies (all)
  9981. "Update the statistics cookie, either from TODO or from checkboxes.
  9982. This should be called with the cursor in a line with a statistics cookie."
  9983. (interactive "P")
  9984. (if all
  9985. (progn
  9986. (org-update-checkbox-count 'all)
  9987. (org-map-entries 'org-update-parent-todo-statistics))
  9988. (if (not (org-on-heading-p))
  9989. (org-update-checkbox-count)
  9990. (let ((pos (move-marker (make-marker) (point)))
  9991. end l1 l2)
  9992. (ignore-errors (org-back-to-heading t))
  9993. (if (not (org-on-heading-p))
  9994. (org-update-checkbox-count)
  9995. (setq l1 (org-outline-level))
  9996. (setq end (save-excursion
  9997. (outline-next-heading)
  9998. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9999. (point)))
  10000. (if (and (save-excursion
  10001. (re-search-forward
  10002. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10003. (not (save-excursion (re-search-forward
  10004. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10005. (org-update-checkbox-count)
  10006. (if (and l2 (> l2 l1))
  10007. (progn
  10008. (goto-char end)
  10009. (org-update-parent-todo-statistics))
  10010. (goto-char pos)
  10011. (beginning-of-line 1)
  10012. (while (re-search-forward
  10013. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10014. (point-at-eol) t)
  10015. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10016. (goto-char pos)
  10017. (move-marker pos nil)))))
  10018. (defvar org-entry-property-inherited-from) ;; defined below
  10019. (defun org-update-parent-todo-statistics ()
  10020. "Update any statistics cookie in the parent of the current headline.
  10021. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10022. the entire subtree and this will travel up the hierarchy and update
  10023. statistics everywhere."
  10024. (interactive)
  10025. (let* ((lim 0) prop
  10026. (recursive (or (not org-hierarchical-todo-statistics)
  10027. (string-match
  10028. "\\<recursive\\>"
  10029. (or (setq prop (org-entry-get
  10030. nil "COOKIE_DATA" 'inherit)) ""))))
  10031. (lim (or (and prop (marker-position
  10032. org-entry-property-inherited-from))
  10033. lim))
  10034. (first t)
  10035. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10036. level ltoggle l1 new ndel
  10037. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  10038. (catch 'exit
  10039. (save-excursion
  10040. (beginning-of-line 1)
  10041. (if (org-at-heading-p)
  10042. (setq ltoggle (funcall outline-level))
  10043. (error "This should not happen"))
  10044. (while (and (setq level (org-up-heading-safe))
  10045. (or recursive first)
  10046. (>= (point) lim))
  10047. (setq first nil cookie-present nil)
  10048. (unless (and level
  10049. (not (string-match
  10050. "\\<checkbox\\>"
  10051. (downcase
  10052. (or (org-entry-get
  10053. nil "COOKIE_DATA")
  10054. "")))))
  10055. (throw 'exit nil))
  10056. (while (re-search-forward box-re (point-at-eol) t)
  10057. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10058. (setq is-percent (match-end 2))
  10059. (save-match-data
  10060. (unless (outline-next-heading) (throw 'exit nil))
  10061. (while (and (looking-at org-complex-heading-regexp)
  10062. (> (setq l1 (length (match-string 1))) level))
  10063. (setq kwd (and (or recursive (= l1 ltoggle))
  10064. (match-string 2)))
  10065. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10066. (and (listp org-provide-todo-statistics)
  10067. (or (member kwd org-provide-todo-statistics)
  10068. (member kwd org-done-keywords))))
  10069. (setq cnt-all (1+ cnt-all))
  10070. (if (eq org-provide-todo-statistics t)
  10071. (and kwd (setq cnt-all (1+ cnt-all)))))
  10072. (and (member kwd org-done-keywords)
  10073. (setq cnt-done (1+ cnt-done)))
  10074. (outline-next-heading)))
  10075. (setq new
  10076. (if is-percent
  10077. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10078. (format "[%d/%d]" cnt-done cnt-all))
  10079. ndel (- (match-end 0) (match-beginning 0)))
  10080. (goto-char (match-beginning 0))
  10081. (insert new)
  10082. (delete-region (point) (+ (point) ndel)))
  10083. (when cookie-present
  10084. (run-hook-with-args 'org-after-todo-statistics-hook
  10085. cnt-done (- cnt-all cnt-done))))))
  10086. (run-hooks 'org-todo-statistics-hook)))
  10087. (defvar org-after-todo-statistics-hook nil
  10088. "Hook that is called after a TODO statistics cookie has been updated.
  10089. Each function is called with two arguments: the number of not-done entries
  10090. and the number of done entries.
  10091. For example, the following function, when added to this hook, will switch
  10092. an entry to DONE when all children are done, and back to TODO when new
  10093. entries are set to a TODO status. Note that this hook is only called
  10094. when there is a statistics cookie in the headline!
  10095. (defun org-summary-todo (n-done n-not-done)
  10096. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10097. (let (org-log-done org-log-states) ; turn off logging
  10098. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10099. ")
  10100. (defvar org-todo-statistics-hook nil
  10101. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10102. This hook runs even if there is no statistics cookie present, in which case
  10103. `org-after-todo-statistics-hook' would not run.")
  10104. (defun org-todo-trigger-tag-changes (state)
  10105. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10106. (let ((l org-todo-state-tags-triggers)
  10107. changes)
  10108. (when (or (not state) (equal state ""))
  10109. (setq changes (append changes (cdr (assoc "" l)))))
  10110. (when (and (stringp state) (> (length state) 0))
  10111. (setq changes (append changes (cdr (assoc state l)))))
  10112. (when (member state org-not-done-keywords)
  10113. (setq changes (append changes (cdr (assoc 'todo l)))))
  10114. (when (member state org-done-keywords)
  10115. (setq changes (append changes (cdr (assoc 'done l)))))
  10116. (dolist (c changes)
  10117. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10118. (defun org-local-logging (value)
  10119. "Get logging settings from a property VALUE."
  10120. (let* (words w a)
  10121. ;; directly set the variables, they are already local.
  10122. (setq org-log-done nil
  10123. org-log-repeat nil
  10124. org-todo-log-states nil)
  10125. (setq words (org-split-string value))
  10126. (while (setq w (pop words))
  10127. (cond
  10128. ((setq a (assoc w org-startup-options))
  10129. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10130. (set (nth 1 a) (nth 2 a))))
  10131. ((setq a (org-extract-log-state-settings w))
  10132. (and (member (car a) org-todo-keywords-1)
  10133. (push a org-todo-log-states)))))))
  10134. (defun org-get-todo-sequence-head (kwd)
  10135. "Return the head of the TODO sequence to which KWD belongs.
  10136. If KWD is not set, check if there is a text property remembering the
  10137. right sequence."
  10138. (let (p)
  10139. (cond
  10140. ((not kwd)
  10141. (or (get-text-property (point-at-bol) 'org-todo-head)
  10142. (progn
  10143. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10144. nil (point-at-eol)))
  10145. (get-text-property p 'org-todo-head))))
  10146. ((not (member kwd org-todo-keywords-1))
  10147. (car org-todo-keywords-1))
  10148. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10149. (defun org-fast-todo-selection ()
  10150. "Fast TODO keyword selection with single keys.
  10151. Returns the new TODO keyword, or nil if no state change should occur."
  10152. (let* ((fulltable org-todo-key-alist)
  10153. (done-keywords org-done-keywords) ;; needed for the faces.
  10154. (maxlen (apply 'max (mapcar
  10155. (lambda (x)
  10156. (if (stringp (car x)) (string-width (car x)) 0))
  10157. fulltable)))
  10158. (expert nil)
  10159. (fwidth (+ maxlen 3 1 3))
  10160. (ncol (/ (- (window-width) 4) fwidth))
  10161. tg cnt e c tbl
  10162. groups ingroup)
  10163. (save-excursion
  10164. (save-window-excursion
  10165. (if expert
  10166. (set-buffer (get-buffer-create " *Org todo*"))
  10167. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10168. (erase-buffer)
  10169. (org-set-local 'org-done-keywords done-keywords)
  10170. (setq tbl fulltable cnt 0)
  10171. (while (setq e (pop tbl))
  10172. (cond
  10173. ((equal e '(:startgroup))
  10174. (push '() groups) (setq ingroup t)
  10175. (when (not (= cnt 0))
  10176. (setq cnt 0)
  10177. (insert "\n"))
  10178. (insert "{ "))
  10179. ((equal e '(:endgroup))
  10180. (setq ingroup nil cnt 0)
  10181. (insert "}\n"))
  10182. ((equal e '(:newline))
  10183. (when (not (= cnt 0))
  10184. (setq cnt 0)
  10185. (insert "\n")
  10186. (setq e (car tbl))
  10187. (while (equal (car tbl) '(:newline))
  10188. (insert "\n")
  10189. (setq tbl (cdr tbl)))))
  10190. (t
  10191. (setq tg (car e) c (cdr e))
  10192. (if ingroup (push tg (car groups)))
  10193. (setq tg (org-add-props tg nil 'face
  10194. (org-get-todo-face tg)))
  10195. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10196. (insert "[" c "] " tg (make-string
  10197. (- fwidth 4 (length tg)) ?\ ))
  10198. (when (= (setq cnt (1+ cnt)) ncol)
  10199. (insert "\n")
  10200. (if ingroup (insert " "))
  10201. (setq cnt 0)))))
  10202. (insert "\n")
  10203. (goto-char (point-min))
  10204. (if (not expert) (org-fit-window-to-buffer))
  10205. (message "[a-z..]:Set [SPC]:clear")
  10206. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10207. (cond
  10208. ((or (= c ?\C-g)
  10209. (and (= c ?q) (not (rassoc c fulltable))))
  10210. (setq quit-flag t))
  10211. ((= c ?\ ) nil)
  10212. ((setq e (rassoc c fulltable) tg (car e))
  10213. tg)
  10214. (t (setq quit-flag t)))))))
  10215. (defun org-entry-is-todo-p ()
  10216. (member (org-get-todo-state) org-not-done-keywords))
  10217. (defun org-entry-is-done-p ()
  10218. (member (org-get-todo-state) org-done-keywords))
  10219. (defun org-get-todo-state ()
  10220. (save-excursion
  10221. (org-back-to-heading t)
  10222. (and (looking-at org-todo-line-regexp)
  10223. (match-end 2)
  10224. (match-string 2))))
  10225. (defun org-at-date-range-p (&optional inactive-ok)
  10226. "Is the cursor inside a date range?"
  10227. (interactive)
  10228. (save-excursion
  10229. (catch 'exit
  10230. (let ((pos (point)))
  10231. (skip-chars-backward "^[<\r\n")
  10232. (skip-chars-backward "<[")
  10233. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10234. (>= (match-end 0) pos)
  10235. (throw 'exit t))
  10236. (skip-chars-backward "^<[\r\n")
  10237. (skip-chars-backward "<[")
  10238. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10239. (>= (match-end 0) pos)
  10240. (throw 'exit t)))
  10241. nil)))
  10242. (defun org-get-repeat (&optional tagline)
  10243. "Check if there is a deadline/schedule with repeater in this entry."
  10244. (save-match-data
  10245. (save-excursion
  10246. (org-back-to-heading t)
  10247. (and (re-search-forward (if tagline
  10248. (concat tagline "\\s-*" org-repeat-re)
  10249. org-repeat-re)
  10250. (org-entry-end-position) t)
  10251. (match-string-no-properties 1)))))
  10252. (defvar org-last-changed-timestamp)
  10253. (defvar org-last-inserted-timestamp)
  10254. (defvar org-log-post-message)
  10255. (defvar org-log-note-purpose)
  10256. (defvar org-log-note-how)
  10257. (defvar org-log-note-extra)
  10258. (defun org-auto-repeat-maybe (done-word)
  10259. "Check if the current headline contains a repeated deadline/schedule.
  10260. If yes, set TODO state back to what it was and change the base date
  10261. of repeating deadline/scheduled time stamps to new date.
  10262. This function is run automatically after each state change to a DONE state."
  10263. ;; last-state is dynamically scoped into this function
  10264. (let* ((repeat (org-get-repeat))
  10265. (aa (assoc last-state org-todo-kwd-alist))
  10266. (interpret (nth 1 aa))
  10267. (head (nth 2 aa))
  10268. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10269. (msg "Entry repeats: ")
  10270. (org-log-done nil)
  10271. (org-todo-log-states nil)
  10272. re type n what ts time to-state)
  10273. (when repeat
  10274. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10275. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10276. org-todo-repeat-to-state))
  10277. (unless (and to-state (member to-state org-todo-keywords-1))
  10278. (setq to-state (if (eq interpret 'type) last-state head)))
  10279. (org-todo to-state)
  10280. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10281. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10282. (org-time-stamp-format t t))))
  10283. (when org-log-repeat
  10284. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10285. (memq 'org-add-log-note post-command-hook))
  10286. ;; OK, we are already setup for some record
  10287. (if (eq org-log-repeat 'note)
  10288. ;; make sure we take a note, not only a time stamp
  10289. (setq org-log-note-how 'note))
  10290. ;; Set up for taking a record
  10291. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10292. last-state
  10293. 'findpos org-log-repeat)))
  10294. (org-back-to-heading t)
  10295. (org-add-planning-info nil nil 'closed)
  10296. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10297. org-deadline-time-regexp "\\)\\|\\("
  10298. org-ts-regexp "\\)"))
  10299. (while (re-search-forward
  10300. re (save-excursion (outline-next-heading) (point)) t)
  10301. (setq type (if (match-end 1) org-scheduled-string
  10302. (if (match-end 3) org-deadline-string "Plain:"))
  10303. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10304. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10305. (setq n (string-to-number (match-string 2 ts))
  10306. what (match-string 3 ts))
  10307. (if (equal what "w") (setq n (* n 7) what "d"))
  10308. ;; Preparation, see if we need to modify the start date for the change
  10309. (when (match-end 1)
  10310. (setq time (save-match-data (org-time-string-to-time ts)))
  10311. (cond
  10312. ((equal (match-string 1 ts) ".")
  10313. ;; Shift starting date to today
  10314. (org-timestamp-change
  10315. (- (time-to-days (current-time)) (time-to-days time))
  10316. 'day))
  10317. ((equal (match-string 1 ts) "+")
  10318. (let ((nshiftmax 10) (nshift 0))
  10319. (while (or (= nshift 0)
  10320. (<= (time-to-days time)
  10321. (time-to-days (current-time))))
  10322. (when (= (incf nshift) nshiftmax)
  10323. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10324. (error "Abort")))
  10325. (org-timestamp-change n (cdr (assoc what whata)))
  10326. (org-at-timestamp-p t)
  10327. (setq ts (match-string 1))
  10328. (setq time (save-match-data (org-time-string-to-time ts)))))
  10329. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10330. ;; rematch, so that we have everything in place for the real shift
  10331. (org-at-timestamp-p t)
  10332. (setq ts (match-string 1))
  10333. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10334. (org-timestamp-change n (cdr (assoc what whata)))
  10335. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10336. (setq org-log-post-message msg)
  10337. (message "%s" msg))))
  10338. (defun org-show-todo-tree (arg)
  10339. "Make a compact tree which shows all headlines marked with TODO.
  10340. The tree will show the lines where the regexp matches, and all higher
  10341. headlines above the match.
  10342. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10343. With a numeric prefix N, construct a sparse tree for the Nth element
  10344. of `org-todo-keywords-1'."
  10345. (interactive "P")
  10346. (let ((case-fold-search nil)
  10347. (kwd-re
  10348. (cond ((null arg) org-not-done-regexp)
  10349. ((equal arg '(4))
  10350. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10351. (mapcar 'list org-todo-keywords-1))))
  10352. (concat "\\("
  10353. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10354. "\\)\\>")))
  10355. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10356. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10357. org-todo-keywords-1)))
  10358. (t (error "Invalid prefix argument: %s" arg)))))
  10359. (message "%d TODO entries found"
  10360. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10361. (defun org-deadline (&optional remove time)
  10362. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10363. With argument REMOVE, remove any deadline from the item.
  10364. When TIME is set, it should be an internal time specification, and the
  10365. scheduling will use the corresponding date."
  10366. (interactive "P")
  10367. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10368. (repeater (and old-date
  10369. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10370. (match-string 1 old-date))))
  10371. (if remove
  10372. (progn
  10373. (when (and old-date org-log-redeadline)
  10374. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10375. org-log-redeadline))
  10376. (org-remove-timestamp-with-keyword org-deadline-string)
  10377. (message "Item no longer has a deadline."))
  10378. (org-add-planning-info 'deadline time 'closed)
  10379. (when (and old-date org-log-redeadline
  10380. (not (equal old-date
  10381. (substring org-last-inserted-timestamp 1 -1))))
  10382. (org-add-log-setup 'redeadline nil old-date 'findpos
  10383. org-log-redeadline))
  10384. (when repeater
  10385. (save-excursion
  10386. (org-back-to-heading t)
  10387. (when (re-search-forward (concat org-deadline-string " "
  10388. org-last-inserted-timestamp)
  10389. (save-excursion
  10390. (outline-next-heading) (point)) t)
  10391. (goto-char (1- (match-end 0)))
  10392. (insert " " repeater)
  10393. (setq org-last-inserted-timestamp
  10394. (concat (substring org-last-inserted-timestamp 0 -1)
  10395. " " repeater
  10396. (substring org-last-inserted-timestamp -1))))))
  10397. (message "Deadline on %s" org-last-inserted-timestamp))))
  10398. (defun org-schedule (&optional remove time)
  10399. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10400. With argument REMOVE, remove any scheduling date from the item.
  10401. When TIME is set, it should be an internal time specification, and the
  10402. scheduling will use the corresponding date."
  10403. (interactive "P")
  10404. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10405. (repeater (and old-date
  10406. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10407. (match-string 1 old-date))))
  10408. (if remove
  10409. (progn
  10410. (when (and old-date org-log-reschedule)
  10411. (org-add-log-setup 'delschedule nil old-date 'findpos
  10412. org-log-reschedule))
  10413. (org-remove-timestamp-with-keyword org-scheduled-string)
  10414. (message "Item is no longer scheduled."))
  10415. (org-add-planning-info 'scheduled time 'closed)
  10416. (when (and old-date org-log-reschedule
  10417. (not (equal old-date
  10418. (substring org-last-inserted-timestamp 1 -1))))
  10419. (org-add-log-setup 'reschedule nil old-date 'findpos
  10420. org-log-reschedule))
  10421. (when repeater
  10422. (save-excursion
  10423. (org-back-to-heading t)
  10424. (when (re-search-forward (concat org-scheduled-string " "
  10425. org-last-inserted-timestamp)
  10426. (save-excursion
  10427. (outline-next-heading) (point)) t)
  10428. (goto-char (1- (match-end 0)))
  10429. (insert " " repeater)
  10430. (setq org-last-inserted-timestamp
  10431. (concat (substring org-last-inserted-timestamp 0 -1)
  10432. " " repeater
  10433. (substring org-last-inserted-timestamp -1))))))
  10434. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10435. (defun org-get-scheduled-time (pom &optional inherit)
  10436. "Get the scheduled time as a time tuple, of a format suitable
  10437. for calling org-schedule with, or if there is no scheduling,
  10438. returns nil."
  10439. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10440. (when time
  10441. (apply 'encode-time (org-parse-time-string time)))))
  10442. (defun org-get-deadline-time (pom &optional inherit)
  10443. "Get the deadline as a time tuple, of a format suitable for
  10444. calling org-deadline with, or if there is no scheduling, returns
  10445. nil."
  10446. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10447. (when time
  10448. (apply 'encode-time (org-parse-time-string time)))))
  10449. (defun org-remove-timestamp-with-keyword (keyword)
  10450. "Remove all time stamps with KEYWORD in the current entry."
  10451. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10452. beg)
  10453. (save-excursion
  10454. (org-back-to-heading t)
  10455. (setq beg (point))
  10456. (outline-next-heading)
  10457. (while (re-search-backward re beg t)
  10458. (replace-match "")
  10459. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10460. (equal (char-before) ?\ ))
  10461. (backward-delete-char 1)
  10462. (if (string-match "^[ \t]*$" (buffer-substring
  10463. (point-at-bol) (point-at-eol)))
  10464. (delete-region (point-at-bol)
  10465. (min (point-max) (1+ (point-at-eol))))))))))
  10466. (defun org-add-planning-info (what &optional time &rest remove)
  10467. "Insert new timestamp with keyword in the line directly after the headline.
  10468. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10469. If non is given, the user is prompted for a date.
  10470. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10471. be removed."
  10472. (interactive)
  10473. (let (org-time-was-given org-end-time-was-given ts
  10474. end default-time default-input)
  10475. (catch 'exit
  10476. (when (and (not time) (memq what '(scheduled deadline)))
  10477. ;; Try to get a default date/time from existing timestamp
  10478. (save-excursion
  10479. (org-back-to-heading t)
  10480. (setq end (save-excursion (outline-next-heading) (point)))
  10481. (when (re-search-forward (if (eq what 'scheduled)
  10482. org-scheduled-time-regexp
  10483. org-deadline-time-regexp)
  10484. end t)
  10485. (setq ts (match-string 1)
  10486. default-time
  10487. (apply 'encode-time (org-parse-time-string ts))
  10488. default-input (and ts (org-get-compact-tod ts))))))
  10489. (when what
  10490. ;; If necessary, get the time from the user
  10491. (setq time (or time (org-read-date nil 'to-time nil nil
  10492. default-time default-input))))
  10493. (when (and org-insert-labeled-timestamps-at-point
  10494. (member what '(scheduled deadline)))
  10495. (insert
  10496. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10497. (org-insert-time-stamp time org-time-was-given
  10498. nil nil nil (list org-end-time-was-given))
  10499. (setq what nil))
  10500. (save-excursion
  10501. (save-restriction
  10502. (let (col list elt ts buffer-invisibility-spec)
  10503. (org-back-to-heading t)
  10504. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10505. (goto-char (match-end 1))
  10506. (setq col (current-column))
  10507. (goto-char (match-end 0))
  10508. (if (eobp) (insert "\n") (forward-char 1))
  10509. (when (and (not what)
  10510. (not (looking-at
  10511. (concat "[ \t]*"
  10512. org-keyword-time-not-clock-regexp))))
  10513. ;; Nothing to add, nothing to remove...... :-)
  10514. (throw 'exit nil))
  10515. (if (and (not (looking-at outline-regexp))
  10516. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10517. "[^\r\n]*"))
  10518. (not (equal (match-string 1) org-clock-string)))
  10519. (narrow-to-region (match-beginning 0) (match-end 0))
  10520. (insert-before-markers "\n")
  10521. (backward-char 1)
  10522. (narrow-to-region (point) (point))
  10523. (and org-adapt-indentation (org-indent-to-column col)))
  10524. ;; Check if we have to remove something.
  10525. (setq list (cons what remove))
  10526. (while list
  10527. (setq elt (pop list))
  10528. (goto-char (point-min))
  10529. (when (or (and (eq elt 'scheduled)
  10530. (re-search-forward org-scheduled-time-regexp nil t))
  10531. (and (eq elt 'deadline)
  10532. (re-search-forward org-deadline-time-regexp nil t))
  10533. (and (eq elt 'closed)
  10534. (re-search-forward org-closed-time-regexp nil t)))
  10535. (replace-match "")
  10536. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10537. (skip-chars-backward " ")
  10538. (if (looking-at " +") (replace-match ""))))
  10539. (goto-char (point-max))
  10540. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10541. (when what
  10542. (insert
  10543. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10544. (cond ((eq what 'scheduled) org-scheduled-string)
  10545. ((eq what 'deadline) org-deadline-string)
  10546. ((eq what 'closed) org-closed-string))
  10547. " ")
  10548. (setq ts (org-insert-time-stamp
  10549. time
  10550. (or org-time-was-given
  10551. (and (eq what 'closed) org-log-done-with-time))
  10552. (eq what 'closed)
  10553. nil nil (list org-end-time-was-given)))
  10554. (end-of-line 1))
  10555. (goto-char (point-min))
  10556. (widen)
  10557. (if (and (looking-at "[ \t]*\n")
  10558. (equal (char-before) ?\n))
  10559. (delete-region (1- (point)) (point-at-eol)))
  10560. ts))))))
  10561. (defvar org-log-note-marker (make-marker))
  10562. (defvar org-log-note-purpose nil)
  10563. (defvar org-log-note-state nil)
  10564. (defvar org-log-note-previous-state nil)
  10565. (defvar org-log-note-how nil)
  10566. (defvar org-log-note-extra nil)
  10567. (defvar org-log-note-window-configuration nil)
  10568. (defvar org-log-note-return-to (make-marker))
  10569. (defvar org-log-post-message nil
  10570. "Message to be displayed after a log note has been stored.
  10571. The auto-repeater uses this.")
  10572. (defun org-add-note ()
  10573. "Add a note to the current entry.
  10574. This is done in the same way as adding a state change note."
  10575. (interactive)
  10576. (org-add-log-setup 'note nil nil 'findpos nil))
  10577. (defvar org-property-end-re)
  10578. (defun org-add-log-setup (&optional purpose state prev-state
  10579. findpos how extra)
  10580. "Set up the post command hook to take a note.
  10581. If this is about to TODO state change, the new state is expected in STATE.
  10582. When FINDPOS is non-nil, find the correct position for the note in
  10583. the current entry. If not, assume that it can be inserted at point.
  10584. HOW is an indicator what kind of note should be created.
  10585. EXTRA is additional text that will be inserted into the notes buffer."
  10586. (let* ((org-log-into-drawer (org-log-into-drawer))
  10587. (drawer (cond ((stringp org-log-into-drawer)
  10588. org-log-into-drawer)
  10589. (org-log-into-drawer "LOGBOOK")
  10590. (t nil))))
  10591. (save-restriction
  10592. (save-excursion
  10593. (when findpos
  10594. (org-back-to-heading t)
  10595. (narrow-to-region (point) (save-excursion
  10596. (outline-next-heading) (point)))
  10597. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10598. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10599. "[^\r\n]*\\)?"))
  10600. (goto-char (match-end 0))
  10601. (cond
  10602. (drawer
  10603. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10604. nil t)
  10605. (progn
  10606. (goto-char (match-end 0))
  10607. (or org-log-states-order-reversed
  10608. (and (re-search-forward org-property-end-re nil t)
  10609. (goto-char (1- (match-beginning 0))))))
  10610. (insert "\n:" drawer ":\n:END:")
  10611. (beginning-of-line 0)
  10612. (org-indent-line-function)
  10613. (beginning-of-line 2)
  10614. (org-indent-line-function)
  10615. (end-of-line 0)))
  10616. ((and org-log-state-notes-insert-after-drawers
  10617. (save-excursion
  10618. (forward-line) (looking-at org-drawer-regexp)))
  10619. (forward-line)
  10620. (while (looking-at org-drawer-regexp)
  10621. (goto-char (match-end 0))
  10622. (re-search-forward org-property-end-re (point-max) t)
  10623. (forward-line))
  10624. (forward-line -1)))
  10625. (unless org-log-states-order-reversed
  10626. (and (= (char-after) ?\n) (forward-char 1))
  10627. (org-skip-over-state-notes)
  10628. (skip-chars-backward " \t\n\r")))
  10629. (move-marker org-log-note-marker (point))
  10630. (setq org-log-note-purpose purpose
  10631. org-log-note-state state
  10632. org-log-note-previous-state prev-state
  10633. org-log-note-how how
  10634. org-log-note-extra extra)
  10635. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10636. (defun org-skip-over-state-notes ()
  10637. "Skip past the list of State notes in an entry."
  10638. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10639. (when (org-in-item-p)
  10640. (let ((limit (org-list-bottom-point)))
  10641. (while (looking-at "[ \t]*- State")
  10642. (goto-char (or (org-get-next-item (point) limit)
  10643. (org-get-end-of-item limit)))))))
  10644. (defun org-add-log-note (&optional purpose)
  10645. "Pop up a window for taking a note, and add this note later at point."
  10646. (remove-hook 'post-command-hook 'org-add-log-note)
  10647. (setq org-log-note-window-configuration (current-window-configuration))
  10648. (delete-other-windows)
  10649. (move-marker org-log-note-return-to (point))
  10650. (switch-to-buffer (marker-buffer org-log-note-marker))
  10651. (goto-char org-log-note-marker)
  10652. (org-switch-to-buffer-other-window "*Org Note*")
  10653. (erase-buffer)
  10654. (if (memq org-log-note-how '(time state))
  10655. (let (current-prefix-arg) (org-store-log-note))
  10656. (let ((org-inhibit-startup t)) (org-mode))
  10657. (insert (format "# Insert note for %s.
  10658. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10659. (cond
  10660. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10661. ((eq org-log-note-purpose 'done) "closed todo item")
  10662. ((eq org-log-note-purpose 'state)
  10663. (format "state change from \"%s\" to \"%s\""
  10664. (or org-log-note-previous-state "")
  10665. (or org-log-note-state "")))
  10666. ((eq org-log-note-purpose 'reschedule)
  10667. "rescheduling")
  10668. ((eq org-log-note-purpose 'delschedule)
  10669. "no longer scheduled")
  10670. ((eq org-log-note-purpose 'redeadline)
  10671. "changing deadline")
  10672. ((eq org-log-note-purpose 'deldeadline)
  10673. "removing deadline")
  10674. ((eq org-log-note-purpose 'refile)
  10675. "refiling")
  10676. ((eq org-log-note-purpose 'note)
  10677. "this entry")
  10678. (t (error "This should not happen")))))
  10679. (if org-log-note-extra (insert org-log-note-extra))
  10680. (org-set-local 'org-finish-function 'org-store-log-note)))
  10681. (defvar org-note-abort nil) ; dynamically scoped
  10682. (defun org-store-log-note ()
  10683. "Finish taking a log note, and insert it to where it belongs."
  10684. (let ((txt (buffer-string))
  10685. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10686. lines ind bul)
  10687. (kill-buffer (current-buffer))
  10688. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10689. (setq txt (replace-match "" t t txt)))
  10690. (if (string-match "\\s-+\\'" txt)
  10691. (setq txt (replace-match "" t t txt)))
  10692. (setq lines (org-split-string txt "\n"))
  10693. (when (and note (string-match "\\S-" note))
  10694. (setq note
  10695. (org-replace-escapes
  10696. note
  10697. (list (cons "%u" (user-login-name))
  10698. (cons "%U" user-full-name)
  10699. (cons "%t" (format-time-string
  10700. (org-time-stamp-format 'long 'inactive)
  10701. (current-time)))
  10702. (cons "%T" (format-time-string
  10703. (org-time-stamp-format 'long nil)
  10704. (current-time)))
  10705. (cons "%s" (if org-log-note-state
  10706. (concat "\"" org-log-note-state "\"")
  10707. ""))
  10708. (cons "%S" (if org-log-note-previous-state
  10709. (concat "\"" org-log-note-previous-state "\"")
  10710. "\"\"")))))
  10711. (if lines (setq note (concat note " \\\\")))
  10712. (push note lines))
  10713. (when (or current-prefix-arg org-note-abort)
  10714. (when org-log-into-drawer
  10715. (org-remove-empty-drawer-at
  10716. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10717. org-log-note-marker))
  10718. (setq lines nil))
  10719. (when lines
  10720. (with-current-buffer (marker-buffer org-log-note-marker)
  10721. (save-excursion
  10722. (goto-char org-log-note-marker)
  10723. (move-marker org-log-note-marker nil)
  10724. (end-of-line 1)
  10725. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10726. (setq ind (save-excursion
  10727. (if (org-in-item-p)
  10728. (progn
  10729. (goto-char (org-list-top-point))
  10730. (org-get-indentation))
  10731. (skip-chars-backward " \r\t\n")
  10732. (cond
  10733. ((and (org-at-heading-p)
  10734. org-adapt-indentation)
  10735. (1+ (org-current-level)))
  10736. ((org-at-heading-p) 0)
  10737. (t (org-get-indentation))))))
  10738. (setq bul (org-list-bullet-string "-"))
  10739. (org-indent-line-to ind)
  10740. (insert bul (pop lines))
  10741. (let ((ind-body (+ (length bul) ind)))
  10742. (while lines
  10743. (insert "\n")
  10744. (org-indent-line-to ind-body)
  10745. (insert (pop lines))))
  10746. (message "Note stored")
  10747. (org-back-to-heading t)
  10748. (org-cycle-hide-drawers 'children)))))
  10749. (set-window-configuration org-log-note-window-configuration)
  10750. (with-current-buffer (marker-buffer org-log-note-return-to)
  10751. (goto-char org-log-note-return-to))
  10752. (move-marker org-log-note-return-to nil)
  10753. (and org-log-post-message (message "%s" org-log-post-message)))
  10754. (defun org-remove-empty-drawer-at (drawer pos)
  10755. "Remove an empty drawer DRAWER at position POS.
  10756. POS may also be a marker."
  10757. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10758. (save-excursion
  10759. (save-restriction
  10760. (widen)
  10761. (goto-char pos)
  10762. (if (org-in-regexp
  10763. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10764. (replace-match ""))))))
  10765. (defun org-sparse-tree (&optional arg)
  10766. "Create a sparse tree, prompt for the details.
  10767. This command can create sparse trees. You first need to select the type
  10768. of match used to create the tree:
  10769. t Show all TODO entries.
  10770. T Show entries with a specific TODO keyword.
  10771. m Show entries selected by a tags/property match.
  10772. p Enter a property name and its value (both with completion on existing
  10773. names/values) and show entries with that property.
  10774. r Show entries matching a regular expression (`/' can be used as well)
  10775. d Show deadlines due within `org-deadline-warning-days'.
  10776. b Show deadlines and scheduled items before a date.
  10777. a Show deadlines and scheduled items after a date."
  10778. (interactive "P")
  10779. (let (ans kwd value)
  10780. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  10781. (setq ans (read-char-exclusive))
  10782. (cond
  10783. ((equal ans ?d)
  10784. (call-interactively 'org-check-deadlines))
  10785. ((equal ans ?b)
  10786. (call-interactively 'org-check-before-date))
  10787. ((equal ans ?a)
  10788. (call-interactively 'org-check-after-date))
  10789. ((equal ans ?t)
  10790. (org-show-todo-tree nil))
  10791. ((equal ans ?T)
  10792. (org-show-todo-tree '(4)))
  10793. ((member ans '(?T ?m))
  10794. (call-interactively 'org-match-sparse-tree))
  10795. ((member ans '(?p ?P))
  10796. (setq kwd (org-icompleting-read "Property: "
  10797. (mapcar 'list (org-buffer-property-keys))))
  10798. (setq value (org-icompleting-read "Value: "
  10799. (mapcar 'list (org-property-values kwd))))
  10800. (unless (string-match "\\`{.*}\\'" value)
  10801. (setq value (concat "\"" value "\"")))
  10802. (org-match-sparse-tree arg (concat kwd "=" value)))
  10803. ((member ans '(?r ?R ?/))
  10804. (call-interactively 'org-occur))
  10805. (t (error "No such sparse tree command \"%c\"" ans)))))
  10806. (defvar org-occur-highlights nil
  10807. "List of overlays used for occur matches.")
  10808. (make-variable-buffer-local 'org-occur-highlights)
  10809. (defvar org-occur-parameters nil
  10810. "Parameters of the active org-occur calls.
  10811. This is a list, each call to org-occur pushes as cons cell,
  10812. containing the regular expression and the callback, onto the list.
  10813. The list can contain several entries if `org-occur' has been called
  10814. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10815. will only contain one set of parameters. When the highlights are
  10816. removed (for example with `C-c C-c', or with the next edit (depending
  10817. on `org-remove-highlights-with-change'), this variable is emptied
  10818. as well.")
  10819. (make-variable-buffer-local 'org-occur-parameters)
  10820. (defun org-occur (regexp &optional keep-previous callback)
  10821. "Make a compact tree which shows all matches of REGEXP.
  10822. The tree will show the lines where the regexp matches, and all higher
  10823. headlines above the match. It will also show the heading after the match,
  10824. to make sure editing the matching entry is easy.
  10825. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10826. call to `org-occur' will be kept, to allow stacking of calls to this
  10827. command.
  10828. If CALLBACK is non-nil, it is a function which is called to confirm
  10829. that the match should indeed be shown."
  10830. (interactive "sRegexp: \nP")
  10831. (when (equal regexp "")
  10832. (error "Regexp cannot be empty"))
  10833. (unless keep-previous
  10834. (org-remove-occur-highlights nil nil t))
  10835. (push (cons regexp callback) org-occur-parameters)
  10836. (let ((cnt 0))
  10837. (save-excursion
  10838. (goto-char (point-min))
  10839. (if (or (not keep-previous) ; do not want to keep
  10840. (not org-occur-highlights)) ; no previous matches
  10841. ;; hide everything
  10842. (org-overview))
  10843. (while (re-search-forward regexp nil t)
  10844. (when (or (not callback)
  10845. (save-match-data (funcall callback)))
  10846. (setq cnt (1+ cnt))
  10847. (when org-highlight-sparse-tree-matches
  10848. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10849. (org-show-context 'occur-tree))))
  10850. (when org-remove-highlights-with-change
  10851. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10852. nil 'local))
  10853. (unless org-sparse-tree-open-archived-trees
  10854. (org-hide-archived-subtrees (point-min) (point-max)))
  10855. (run-hooks 'org-occur-hook)
  10856. (if (interactive-p)
  10857. (message "%d match(es) for regexp %s" cnt regexp))
  10858. cnt))
  10859. (defun org-show-context (&optional key)
  10860. "Make sure point and context are visible.
  10861. How much context is shown depends upon the variables
  10862. `org-show-hierarchy-above', `org-show-following-heading'. and
  10863. `org-show-siblings'."
  10864. (let ((heading-p (org-on-heading-p t))
  10865. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10866. (following-p (org-get-alist-option org-show-following-heading key))
  10867. (entry-p (org-get-alist-option org-show-entry-below key))
  10868. (siblings-p (org-get-alist-option org-show-siblings key)))
  10869. (catch 'exit
  10870. ;; Show heading or entry text
  10871. (if (and heading-p (not entry-p))
  10872. (org-flag-heading nil) ; only show the heading
  10873. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10874. (org-show-hidden-entry))) ; show entire entry
  10875. (when following-p
  10876. ;; Show next sibling, or heading below text
  10877. (save-excursion
  10878. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10879. (org-flag-heading nil))))
  10880. (when siblings-p (org-show-siblings))
  10881. (when hierarchy-p
  10882. ;; show all higher headings, possibly with siblings
  10883. (save-excursion
  10884. (while (and (condition-case nil
  10885. (progn (org-up-heading-all 1) t)
  10886. (error nil))
  10887. (not (bobp)))
  10888. (org-flag-heading nil)
  10889. (when siblings-p (org-show-siblings))))))))
  10890. (defvar org-reveal-start-hook nil
  10891. "Hook run before revealing a location.")
  10892. (defun org-reveal (&optional siblings)
  10893. "Show current entry, hierarchy above it, and the following headline.
  10894. This can be used to show a consistent set of context around locations
  10895. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10896. not t for the search context.
  10897. With optional argument SIBLINGS, on each level of the hierarchy all
  10898. siblings are shown. This repairs the tree structure to what it would
  10899. look like when opened with hierarchical calls to `org-cycle'.
  10900. With double optional argument \\[universal-argument] \\[universal-argument], \
  10901. go to the parent and show the
  10902. entire tree."
  10903. (interactive "P")
  10904. (run-hooks 'org-reveal-start-hook)
  10905. (let ((org-show-hierarchy-above t)
  10906. (org-show-following-heading t)
  10907. (org-show-siblings (if siblings t org-show-siblings)))
  10908. (org-show-context nil))
  10909. (when (equal siblings '(16))
  10910. (save-excursion
  10911. (when (org-up-heading-safe)
  10912. (org-show-subtree)
  10913. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10914. (defun org-highlight-new-match (beg end)
  10915. "Highlight from BEG to END and mark the highlight is an occur headline."
  10916. (let ((ov (make-overlay beg end)))
  10917. (overlay-put ov 'face 'secondary-selection)
  10918. (push ov org-occur-highlights)))
  10919. (defun org-remove-occur-highlights (&optional beg end noremove)
  10920. "Remove the occur highlights from the buffer.
  10921. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10922. from the `before-change-functions' in the current buffer."
  10923. (interactive)
  10924. (unless org-inhibit-highlight-removal
  10925. (mapc 'delete-overlay org-occur-highlights)
  10926. (setq org-occur-highlights nil)
  10927. (setq org-occur-parameters nil)
  10928. (unless noremove
  10929. (remove-hook 'before-change-functions
  10930. 'org-remove-occur-highlights 'local))))
  10931. ;;;; Priorities
  10932. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10933. "Regular expression matching the priority indicator.")
  10934. (defvar org-remove-priority-next-time nil)
  10935. (defun org-priority-up ()
  10936. "Increase the priority of the current item."
  10937. (interactive)
  10938. (org-priority 'up))
  10939. (defun org-priority-down ()
  10940. "Decrease the priority of the current item."
  10941. (interactive)
  10942. (org-priority 'down))
  10943. (defun org-priority (&optional action)
  10944. "Change the priority of an item by ARG.
  10945. ACTION can be `set', `up', `down', or a character."
  10946. (interactive)
  10947. (unless org-enable-priority-commands
  10948. (error "Priority commands are disabled"))
  10949. (setq action (or action 'set))
  10950. (let (current new news have remove)
  10951. (save-excursion
  10952. (org-back-to-heading t)
  10953. (if (looking-at org-priority-regexp)
  10954. (setq current (string-to-char (match-string 2))
  10955. have t)
  10956. (setq current org-default-priority))
  10957. (cond
  10958. ((eq action 'remove)
  10959. (setq remove t new ?\ ))
  10960. ((or (eq action 'set)
  10961. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10962. (if (not (eq action 'set))
  10963. (setq new action)
  10964. (message "Priority %c-%c, SPC to remove: "
  10965. org-highest-priority org-lowest-priority)
  10966. (save-match-data
  10967. (setq new (read-char-exclusive))))
  10968. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10969. (= (upcase org-lowest-priority) org-lowest-priority))
  10970. (setq new (upcase new)))
  10971. (cond ((equal new ?\ ) (setq remove t))
  10972. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10973. (error "Priority must be between `%c' and `%c'"
  10974. org-highest-priority org-lowest-priority))))
  10975. ((eq action 'up)
  10976. (if (and (not have) (eq last-command this-command))
  10977. (setq new org-lowest-priority)
  10978. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10979. org-default-priority (1- current)))))
  10980. ((eq action 'down)
  10981. (if (and (not have) (eq last-command this-command))
  10982. (setq new org-highest-priority)
  10983. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10984. org-default-priority (1+ current)))))
  10985. (t (error "Invalid action")))
  10986. (if (or (< (upcase new) org-highest-priority)
  10987. (> (upcase new) org-lowest-priority))
  10988. (setq remove t))
  10989. (setq news (format "%c" new))
  10990. (if have
  10991. (if remove
  10992. (replace-match "" t t nil 1)
  10993. (replace-match news t t nil 2))
  10994. (if remove
  10995. (error "No priority cookie found in line")
  10996. (let ((case-fold-search nil))
  10997. (looking-at org-todo-line-regexp))
  10998. (if (match-end 2)
  10999. (progn
  11000. (goto-char (match-end 2))
  11001. (insert " [#" news "]"))
  11002. (goto-char (match-beginning 3))
  11003. (insert "[#" news "] "))))
  11004. (org-preserve-lc (org-set-tags nil 'align)))
  11005. (if remove
  11006. (message "Priority removed")
  11007. (message "Priority of current item set to %s" news))))
  11008. (defun org-get-priority (s)
  11009. "Find priority cookie and return priority."
  11010. (if (functionp org-get-priority-function)
  11011. (funcall org-get-priority-function)
  11012. (save-match-data
  11013. (if (not (string-match org-priority-regexp s))
  11014. (* 1000 (- org-lowest-priority org-default-priority))
  11015. (* 1000 (- org-lowest-priority
  11016. (string-to-char (match-string 2 s))))))))
  11017. ;;;; Tags
  11018. (defvar org-agenda-archives-mode)
  11019. (defvar org-map-continue-from nil
  11020. "Position from where mapping should continue.
  11021. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11022. (defvar org-scanner-tags nil
  11023. "The current tag list while the tags scanner is running.")
  11024. (defvar org-trust-scanner-tags nil
  11025. "Should `org-get-tags-at' use the tags fro the scanner.
  11026. This is for internal dynamical scoping only.
  11027. When this is non-nil, the function `org-get-tags-at' will return the value
  11028. of `org-scanner-tags' instead of building the list by itself. This
  11029. can lead to large speed-ups when the tags scanner is used in a file with
  11030. many entries, and when the list of tags is retrieved, for example to
  11031. obtain a list of properties. Building the tags list for each entry in such
  11032. a file becomes an N^2 operation - but with this variable set, it scales
  11033. as N.")
  11034. (defun org-scan-tags (action matcher &optional todo-only)
  11035. "Scan headline tags with inheritance and produce output ACTION.
  11036. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11037. or `agenda' to produce an entry list for an agenda view. It can also be
  11038. a Lisp form or a function that should be called at each matched headline, in
  11039. this case the return value is a list of all return values from these calls.
  11040. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11041. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11042. only lines with a TODO keyword are included in the output."
  11043. (require 'org-agenda)
  11044. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11045. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11046. (org-re
  11047. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11048. (props (list 'face 'default
  11049. 'done-face 'org-agenda-done
  11050. 'undone-face 'default
  11051. 'mouse-face 'highlight
  11052. 'org-not-done-regexp org-not-done-regexp
  11053. 'org-todo-regexp org-todo-regexp
  11054. 'help-echo
  11055. (format "mouse-2 or RET jump to org file %s"
  11056. (abbreviate-file-name
  11057. (or (buffer-file-name (buffer-base-buffer))
  11058. (buffer-name (buffer-base-buffer)))))))
  11059. (case-fold-search nil)
  11060. (org-map-continue-from nil)
  11061. lspos tags tags-list
  11062. (tags-alist (list (cons 0 org-file-tags)))
  11063. (llast 0) rtn rtn1 level category i txt
  11064. todo marker entry priority)
  11065. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11066. (setq action (list 'lambda nil action)))
  11067. (save-excursion
  11068. (goto-char (point-min))
  11069. (when (eq action 'sparse-tree)
  11070. (org-overview)
  11071. (org-remove-occur-highlights))
  11072. (while (re-search-forward re nil t)
  11073. (catch :skip
  11074. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11075. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11076. (goto-char (setq lspos (match-beginning 0)))
  11077. (setq level (org-reduced-level (funcall outline-level))
  11078. category (org-get-category))
  11079. (setq i llast llast level)
  11080. ;; remove tag lists from same and sublevels
  11081. (while (>= i level)
  11082. (when (setq entry (assoc i tags-alist))
  11083. (setq tags-alist (delete entry tags-alist)))
  11084. (setq i (1- i)))
  11085. ;; add the next tags
  11086. (when tags
  11087. (setq tags (org-split-string tags ":")
  11088. tags-alist
  11089. (cons (cons level tags) tags-alist)))
  11090. ;; compile tags for current headline
  11091. (setq tags-list
  11092. (if org-use-tag-inheritance
  11093. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11094. tags)
  11095. org-scanner-tags tags-list)
  11096. (when org-use-tag-inheritance
  11097. (setcdr (car tags-alist)
  11098. (mapcar (lambda (x)
  11099. (setq x (copy-sequence x))
  11100. (org-add-prop-inherited x))
  11101. (cdar tags-alist))))
  11102. (when (and tags org-use-tag-inheritance
  11103. (or (not (eq t org-use-tag-inheritance))
  11104. org-tags-exclude-from-inheritance))
  11105. ;; selective inheritance, remove uninherited ones
  11106. (setcdr (car tags-alist)
  11107. (org-remove-uniherited-tags (cdar tags-alist))))
  11108. (when (and (or (not todo-only)
  11109. (and (member todo org-not-done-keywords)
  11110. (or (not org-agenda-tags-todo-honor-ignore-options)
  11111. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11112. (let ((case-fold-search t)) (eval matcher))
  11113. (or
  11114. (not (member org-archive-tag tags-list))
  11115. ;; we have an archive tag, should we use this anyway?
  11116. (or (not org-agenda-skip-archived-trees)
  11117. (and (eq action 'agenda) org-agenda-archives-mode))))
  11118. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11119. ;; select this headline
  11120. (cond
  11121. ((eq action 'sparse-tree)
  11122. (and org-highlight-sparse-tree-matches
  11123. (org-get-heading) (match-end 0)
  11124. (org-highlight-new-match
  11125. (match-beginning 0) (match-beginning 1)))
  11126. (org-show-context 'tags-tree))
  11127. ((eq action 'agenda)
  11128. (setq txt (org-format-agenda-item
  11129. ""
  11130. (concat
  11131. (if (eq org-tags-match-list-sublevels 'indented)
  11132. (make-string (1- level) ?.) "")
  11133. (org-get-heading))
  11134. category
  11135. tags-list
  11136. )
  11137. priority (org-get-priority txt))
  11138. (goto-char lspos)
  11139. (setq marker (org-agenda-new-marker))
  11140. (org-add-props txt props
  11141. 'org-marker marker 'org-hd-marker marker 'org-category category
  11142. 'todo-state todo
  11143. 'priority priority 'type "tagsmatch")
  11144. (push txt rtn))
  11145. ((functionp action)
  11146. (setq org-map-continue-from nil)
  11147. (save-excursion
  11148. (setq rtn1 (funcall action))
  11149. (push rtn1 rtn)))
  11150. (t (error "Invalid action")))
  11151. ;; if we are to skip sublevels, jump to end of subtree
  11152. (unless org-tags-match-list-sublevels
  11153. (org-end-of-subtree t)
  11154. (backward-char 1))))
  11155. ;; Get the correct position from where to continue
  11156. (if org-map-continue-from
  11157. (goto-char org-map-continue-from)
  11158. (and (= (point) lspos) (end-of-line 1)))))
  11159. (when (and (eq action 'sparse-tree)
  11160. (not org-sparse-tree-open-archived-trees))
  11161. (org-hide-archived-subtrees (point-min) (point-max)))
  11162. (nreverse rtn)))
  11163. (defun org-remove-uniherited-tags (tags)
  11164. "Remove all tags that are not inherited from the list TAGS."
  11165. (cond
  11166. ((eq org-use-tag-inheritance t)
  11167. (if org-tags-exclude-from-inheritance
  11168. (org-delete-all org-tags-exclude-from-inheritance tags)
  11169. tags))
  11170. ((not org-use-tag-inheritance) nil)
  11171. ((stringp org-use-tag-inheritance)
  11172. (delq nil (mapcar
  11173. (lambda (x)
  11174. (if (and (string-match org-use-tag-inheritance x)
  11175. (not (member x org-tags-exclude-from-inheritance)))
  11176. x nil))
  11177. tags)))
  11178. ((listp org-use-tag-inheritance)
  11179. (delq nil (mapcar
  11180. (lambda (x)
  11181. (if (member x org-use-tag-inheritance) x nil))
  11182. tags)))))
  11183. (defvar todo-only) ;; dynamically scoped
  11184. (defun org-match-sparse-tree (&optional todo-only match)
  11185. "Create a sparse tree according to tags string MATCH.
  11186. MATCH can contain positive and negative selection of tags, like
  11187. \"+WORK+URGENT-WITHBOSS\".
  11188. If optional argument TODO-ONLY is non-nil, only select lines that are
  11189. also TODO lines."
  11190. (interactive "P")
  11191. (org-prepare-agenda-buffers (list (current-buffer)))
  11192. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11193. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11194. (defvar org-cached-props nil)
  11195. (defun org-cached-entry-get (pom property)
  11196. (if (or (eq t org-use-property-inheritance)
  11197. (and (stringp org-use-property-inheritance)
  11198. (string-match org-use-property-inheritance property))
  11199. (and (listp org-use-property-inheritance)
  11200. (member property org-use-property-inheritance)))
  11201. ;; Caching is not possible, check it directly
  11202. (org-entry-get pom property 'inherit)
  11203. ;; Get all properties, so that we can do complicated checks easily
  11204. (cdr (assoc property (or org-cached-props
  11205. (setq org-cached-props
  11206. (org-entry-properties pom)))))))
  11207. (defun org-global-tags-completion-table (&optional files)
  11208. "Return the list of all tags in all agenda buffer/files."
  11209. (save-excursion
  11210. (org-uniquify
  11211. (delq nil
  11212. (apply 'append
  11213. (mapcar
  11214. (lambda (file)
  11215. (set-buffer (find-file-noselect file))
  11216. (append (org-get-buffer-tags)
  11217. (mapcar (lambda (x) (if (stringp (car-safe x))
  11218. (list (car-safe x)) nil))
  11219. org-tag-alist)))
  11220. (if (and files (car files))
  11221. files
  11222. (org-agenda-files))))))))
  11223. (defun org-make-tags-matcher (match)
  11224. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11225. ;; todo-only is scoped dynamically into this function, and the function
  11226. ;; may change it if the matcher asks for it.
  11227. (unless match
  11228. ;; Get a new match request, with completion
  11229. (let ((org-last-tags-completion-table
  11230. (org-global-tags-completion-table)))
  11231. (setq match (org-completing-read-no-i
  11232. "Match: " 'org-tags-completion-function nil nil nil
  11233. 'org-tags-history))))
  11234. ;; Parse the string and create a lisp form
  11235. (let ((match0 match)
  11236. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11237. minus tag mm
  11238. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11239. orterms term orlist re-p str-p level-p level-op time-p
  11240. prop-p pn pv po cat-p gv rest)
  11241. (if (string-match "/+" match)
  11242. ;; match contains also a todo-matching request
  11243. (progn
  11244. (setq tagsmatch (substring match 0 (match-beginning 0))
  11245. todomatch (substring match (match-end 0)))
  11246. (if (string-match "^!" todomatch)
  11247. (setq todo-only t todomatch (substring todomatch 1)))
  11248. (if (string-match "^\\s-*$" todomatch)
  11249. (setq todomatch nil)))
  11250. ;; only matching tags
  11251. (setq tagsmatch match todomatch nil))
  11252. ;; Make the tags matcher
  11253. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11254. (setq tagsmatcher t)
  11255. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11256. (while (setq term (pop orterms))
  11257. (while (and (equal (substring term -1) "\\") orterms)
  11258. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11259. (while (string-match re term)
  11260. (setq rest (substring term (match-end 0))
  11261. minus (and (match-end 1)
  11262. (equal (match-string 1 term) "-"))
  11263. tag (save-match-data (replace-regexp-in-string
  11264. "\\\\-" "-"
  11265. (match-string 2 term)))
  11266. re-p (equal (string-to-char tag) ?{)
  11267. level-p (match-end 4)
  11268. prop-p (match-end 5)
  11269. mm (cond
  11270. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11271. (level-p
  11272. (setq level-op (org-op-to-function (match-string 3 term)))
  11273. `(,level-op level ,(string-to-number
  11274. (match-string 4 term))))
  11275. (prop-p
  11276. (setq pn (match-string 5 term)
  11277. po (match-string 6 term)
  11278. pv (match-string 7 term)
  11279. cat-p (equal pn "CATEGORY")
  11280. re-p (equal (string-to-char pv) ?{)
  11281. str-p (equal (string-to-char pv) ?\")
  11282. time-p (save-match-data
  11283. (string-match "^\"[[<].*[]>]\"$" pv))
  11284. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11285. (if time-p (setq pv (org-matcher-time pv)))
  11286. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11287. (cond
  11288. ((equal pn "CATEGORY")
  11289. (setq gv '(get-text-property (point) 'org-category)))
  11290. ((equal pn "TODO")
  11291. (setq gv 'todo))
  11292. (t
  11293. (setq gv `(org-cached-entry-get nil ,pn))))
  11294. (if re-p
  11295. (if (eq po 'org<>)
  11296. `(not (string-match ,pv (or ,gv "")))
  11297. `(string-match ,pv (or ,gv "")))
  11298. (if str-p
  11299. `(,po (or ,gv "") ,pv)
  11300. `(,po (string-to-number (or ,gv ""))
  11301. ,(string-to-number pv) ))))
  11302. (t `(member ,tag tags-list)))
  11303. mm (if minus (list 'not mm) mm)
  11304. term rest)
  11305. (push mm tagsmatcher))
  11306. (push (if (> (length tagsmatcher) 1)
  11307. (cons 'and tagsmatcher)
  11308. (car tagsmatcher))
  11309. orlist)
  11310. (setq tagsmatcher nil))
  11311. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11312. (setq tagsmatcher
  11313. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11314. ;; Make the todo matcher
  11315. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11316. (setq todomatcher t)
  11317. (setq orterms (org-split-string todomatch "|") orlist nil)
  11318. (while (setq term (pop orterms))
  11319. (while (string-match re term)
  11320. (setq minus (and (match-end 1)
  11321. (equal (match-string 1 term) "-"))
  11322. kwd (match-string 2 term)
  11323. re-p (equal (string-to-char kwd) ?{)
  11324. term (substring term (match-end 0))
  11325. mm (if re-p
  11326. `(string-match ,(substring kwd 1 -1) todo)
  11327. (list 'equal 'todo kwd))
  11328. mm (if minus (list 'not mm) mm))
  11329. (push mm todomatcher))
  11330. (push (if (> (length todomatcher) 1)
  11331. (cons 'and todomatcher)
  11332. (car todomatcher))
  11333. orlist)
  11334. (setq todomatcher nil))
  11335. (setq todomatcher (if (> (length orlist) 1)
  11336. (cons 'or orlist) (car orlist))))
  11337. ;; Return the string and lisp forms of the matcher
  11338. (setq matcher (if todomatcher
  11339. (list 'and tagsmatcher todomatcher)
  11340. tagsmatcher))
  11341. (cons match0 matcher)))
  11342. (defun org-op-to-function (op &optional stringp)
  11343. "Turn an operator into the appropriate function."
  11344. (setq op
  11345. (cond
  11346. ((equal op "<" ) '(< string< org-time<))
  11347. ((equal op ">" ) '(> org-string> org-time>))
  11348. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11349. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11350. ((member op '("=" "==")) '(= string= org-time=))
  11351. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11352. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11353. (defun org<> (a b) (not (= a b)))
  11354. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11355. (defun org-string>= (a b) (not (string< a b)))
  11356. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11357. (defun org-string<> (a b) (not (string= a b)))
  11358. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11359. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11360. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11361. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11362. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11363. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11364. (defun org-2ft (s)
  11365. "Convert S to a floating point time.
  11366. If S is already a number, just return it. If it is a string, parse
  11367. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11368. (cond
  11369. ((numberp s) s)
  11370. ((stringp s)
  11371. (condition-case nil
  11372. (float-time (apply 'encode-time (org-parse-time-string s)))
  11373. (error 0.)))
  11374. (t 0.)))
  11375. (defun org-time-today ()
  11376. "Time in seconds today at 0:00.
  11377. Returns the float number of seconds since the beginning of the
  11378. epoch to the beginning of today (00:00)."
  11379. (float-time (apply 'encode-time
  11380. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11381. (defun org-matcher-time (s)
  11382. "Interpret a time comparison value."
  11383. (save-match-data
  11384. (cond
  11385. ((string= s "<now>") (float-time))
  11386. ((string= s "<today>") (org-time-today))
  11387. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11388. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11389. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11390. (+ (org-time-today)
  11391. (* (string-to-number (match-string 1 s))
  11392. (cdr (assoc (match-string 2 s)
  11393. '(("d" . 86400.0) ("w" . 604800.0)
  11394. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11395. (t (org-2ft s)))))
  11396. (defun org-match-any-p (re list)
  11397. "Does re match any element of list?"
  11398. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11399. (delq nil list))
  11400. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11401. (defvar org-tags-overlay (make-overlay 1 1))
  11402. (org-detach-overlay org-tags-overlay)
  11403. (defun org-get-local-tags-at (&optional pos)
  11404. "Get a list of tags defined in the current headline."
  11405. (org-get-tags-at pos 'local))
  11406. (defun org-get-local-tags ()
  11407. "Get a list of tags defined in the current headline."
  11408. (org-get-tags-at nil 'local))
  11409. (defun org-get-tags-at (&optional pos local)
  11410. "Get a list of all headline tags applicable at POS.
  11411. POS defaults to point. If tags are inherited, the list contains
  11412. the targets in the same sequence as the headlines appear, i.e.
  11413. the tags of the current headline come last.
  11414. When LOCAL is non-nil, only return tags from the current headline,
  11415. ignore inherited ones."
  11416. (interactive)
  11417. (if (and org-trust-scanner-tags
  11418. (or (not pos) (equal pos (point)))
  11419. (not local))
  11420. org-scanner-tags
  11421. (let (tags ltags lastpos parent)
  11422. (save-excursion
  11423. (save-restriction
  11424. (widen)
  11425. (goto-char (or pos (point)))
  11426. (save-match-data
  11427. (catch 'done
  11428. (condition-case nil
  11429. (progn
  11430. (org-back-to-heading t)
  11431. (while (not (equal lastpos (point)))
  11432. (setq lastpos (point))
  11433. (when (looking-at
  11434. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11435. (setq ltags (org-split-string
  11436. (org-match-string-no-properties 1) ":"))
  11437. (when parent
  11438. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11439. (setq tags (append
  11440. (if parent
  11441. (org-remove-uniherited-tags ltags)
  11442. ltags)
  11443. tags)))
  11444. (or org-use-tag-inheritance (throw 'done t))
  11445. (if local (throw 'done t))
  11446. (or (org-up-heading-safe) (error nil))
  11447. (setq parent t)))
  11448. (error nil)))))
  11449. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11450. (defun org-add-prop-inherited (s)
  11451. (add-text-properties 0 (length s) '(inherited t) s)
  11452. s)
  11453. (defun org-toggle-tag (tag &optional onoff)
  11454. "Toggle the tag TAG for the current line.
  11455. If ONOFF is `on' or `off', don't toggle but set to this state."
  11456. (let (res current)
  11457. (save-excursion
  11458. (org-back-to-heading t)
  11459. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11460. (point-at-eol) t)
  11461. (progn
  11462. (setq current (match-string 1))
  11463. (replace-match ""))
  11464. (setq current ""))
  11465. (setq current (nreverse (org-split-string current ":")))
  11466. (cond
  11467. ((eq onoff 'on)
  11468. (setq res t)
  11469. (or (member tag current) (push tag current)))
  11470. ((eq onoff 'off)
  11471. (or (not (member tag current)) (setq current (delete tag current))))
  11472. (t (if (member tag current)
  11473. (setq current (delete tag current))
  11474. (setq res t)
  11475. (push tag current))))
  11476. (end-of-line 1)
  11477. (if current
  11478. (progn
  11479. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11480. (org-set-tags nil t))
  11481. (delete-horizontal-space))
  11482. (run-hooks 'org-after-tags-change-hook))
  11483. res))
  11484. (defun org-align-tags-here (to-col)
  11485. ;; Assumes that this is a headline
  11486. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11487. (beginning-of-line 1)
  11488. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11489. (< pos (match-beginning 2)))
  11490. (progn
  11491. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11492. (goto-char (match-beginning 1))
  11493. (insert " ")
  11494. (delete-region (point) (1+ (match-beginning 2)))
  11495. (setq ncol (max (1+ (current-column))
  11496. (1+ col)
  11497. (if (> to-col 0)
  11498. to-col
  11499. (- (abs to-col) tags-l))))
  11500. (setq p (point))
  11501. (insert (make-string (- ncol (current-column)) ?\ ))
  11502. (setq ncol (current-column))
  11503. (when indent-tabs-mode (tabify p (point-at-eol)))
  11504. (org-move-to-column (min ncol col) t))
  11505. (goto-char pos))))
  11506. (defun org-set-tags-command (&optional arg just-align)
  11507. "Call the set-tags command for the current entry."
  11508. (interactive "P")
  11509. (if (org-on-heading-p)
  11510. (org-set-tags arg just-align)
  11511. (save-excursion
  11512. (org-back-to-heading t)
  11513. (org-set-tags arg just-align))))
  11514. (defun org-set-tags-to (data)
  11515. "Set the tags of the current entry to DATA, replacing the current tags.
  11516. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11517. If DATA is nil or the empty string, any tags will be removed."
  11518. (interactive "sTags: ")
  11519. (setq data
  11520. (cond
  11521. ((eq data nil) "")
  11522. ((equal data "") "")
  11523. ((stringp data)
  11524. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11525. ":"))
  11526. ((listp data)
  11527. (concat ":" (mapconcat 'identity data ":") ":"))
  11528. (t nil)))
  11529. (when data
  11530. (save-excursion
  11531. (org-back-to-heading t)
  11532. (when (looking-at org-complex-heading-regexp)
  11533. (if (match-end 5)
  11534. (progn
  11535. (goto-char (match-beginning 5))
  11536. (insert data)
  11537. (delete-region (point) (point-at-eol))
  11538. (org-set-tags nil 'align))
  11539. (goto-char (point-at-eol))
  11540. (insert " " data)
  11541. (org-set-tags nil 'align)))
  11542. (beginning-of-line 1)
  11543. (if (looking-at ".*?\\([ \t]+\\)$")
  11544. (delete-region (match-beginning 1) (match-end 1))))))
  11545. (defun org-align-all-tags ()
  11546. "Align the tags i all headings."
  11547. (interactive)
  11548. (save-excursion
  11549. (or (ignore-errors (org-back-to-heading t))
  11550. (outline-next-heading))
  11551. (if (org-on-heading-p)
  11552. (org-set-tags t)
  11553. (message "No headings"))))
  11554. (defvar org-indent-indentation-per-level)
  11555. (defun org-set-tags (&optional arg just-align)
  11556. "Set the tags for the current headline.
  11557. With prefix ARG, realign all tags in headings in the current buffer."
  11558. (interactive "P")
  11559. (let* ((re (concat "^" outline-regexp))
  11560. (current (org-get-tags-string))
  11561. (col (current-column))
  11562. (org-setting-tags t)
  11563. table current-tags inherited-tags ; computed below when needed
  11564. tags p0 c0 c1 rpl di tc level)
  11565. (if arg
  11566. (save-excursion
  11567. (goto-char (point-min))
  11568. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11569. (while (re-search-forward re nil t)
  11570. (org-set-tags nil t)
  11571. (end-of-line 1)))
  11572. (message "All tags realigned to column %d" org-tags-column))
  11573. (if just-align
  11574. (setq tags current)
  11575. ;; Get a new set of tags from the user
  11576. (save-excursion
  11577. (setq table (append org-tag-persistent-alist
  11578. (or org-tag-alist (org-get-buffer-tags))
  11579. (and
  11580. org-complete-tags-always-offer-all-agenda-tags
  11581. (org-global-tags-completion-table
  11582. (org-agenda-files))))
  11583. org-last-tags-completion-table table
  11584. current-tags (org-split-string current ":")
  11585. inherited-tags (nreverse
  11586. (nthcdr (length current-tags)
  11587. (nreverse (org-get-tags-at))))
  11588. tags
  11589. (if (or (eq t org-use-fast-tag-selection)
  11590. (and org-use-fast-tag-selection
  11591. (delq nil (mapcar 'cdr table))))
  11592. (org-fast-tag-selection
  11593. current-tags inherited-tags table
  11594. (if org-fast-tag-selection-include-todo
  11595. org-todo-key-alist))
  11596. (let ((org-add-colon-after-tag-completion t))
  11597. (org-trim
  11598. (org-without-partial-completion
  11599. (org-icompleting-read "Tags: "
  11600. 'org-tags-completion-function
  11601. nil nil current 'org-tags-history)))))))
  11602. (while (string-match "[-+&]+" tags)
  11603. ;; No boolean logic, just a list
  11604. (setq tags (replace-match ":" t t tags))))
  11605. (setq tags (replace-regexp-in-string "[ ,]" ":" tags))
  11606. (if org-tags-sort-function
  11607. (setq tags (mapconcat 'identity
  11608. (sort (org-split-string
  11609. tags (org-re "[^[:alnum:]_@#%]+"))
  11610. org-tags-sort-function) ":")))
  11611. (if (string-match "\\`[\t ]*\\'" tags)
  11612. (setq tags "")
  11613. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11614. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11615. ;; Insert new tags at the correct column
  11616. (beginning-of-line 1)
  11617. (setq level (or (and (looking-at org-outline-regexp)
  11618. (- (match-end 0) (point) 1))
  11619. 1))
  11620. (cond
  11621. ((and (equal current "") (equal tags "")))
  11622. ((re-search-forward
  11623. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11624. (point-at-eol) t)
  11625. (if (equal tags "")
  11626. (setq rpl "")
  11627. (goto-char (match-beginning 0))
  11628. (setq c0 (current-column)
  11629. ;; compute offset for the case of org-indent-mode active
  11630. di (if org-indent-mode
  11631. (* (1- org-indent-indentation-per-level) (1- level))
  11632. 0)
  11633. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11634. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11635. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11636. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11637. (replace-match rpl t t)
  11638. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11639. tags)
  11640. (t (error "Tags alignment failed")))
  11641. (org-move-to-column col)
  11642. (unless just-align
  11643. (run-hooks 'org-after-tags-change-hook)))))
  11644. (defun org-change-tag-in-region (beg end tag off)
  11645. "Add or remove TAG for each entry in the region.
  11646. This works in the agenda, and also in an org-mode buffer."
  11647. (interactive
  11648. (list (region-beginning) (region-end)
  11649. (let ((org-last-tags-completion-table
  11650. (if (org-mode-p)
  11651. (org-get-buffer-tags)
  11652. (org-global-tags-completion-table))))
  11653. (org-icompleting-read
  11654. "Tag: " 'org-tags-completion-function nil nil nil
  11655. 'org-tags-history))
  11656. (progn
  11657. (message "[s]et or [r]emove? ")
  11658. (equal (read-char-exclusive) ?r))))
  11659. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11660. (let ((agendap (equal major-mode 'org-agenda-mode))
  11661. l1 l2 m buf pos newhead (cnt 0))
  11662. (goto-char end)
  11663. (setq l2 (1- (org-current-line)))
  11664. (goto-char beg)
  11665. (setq l1 (org-current-line))
  11666. (loop for l from l1 to l2 do
  11667. (org-goto-line l)
  11668. (setq m (get-text-property (point) 'org-hd-marker))
  11669. (when (or (and (org-mode-p) (org-on-heading-p))
  11670. (and agendap m))
  11671. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11672. pos (if agendap m (point)))
  11673. (with-current-buffer buf
  11674. (save-excursion
  11675. (save-restriction
  11676. (goto-char pos)
  11677. (setq cnt (1+ cnt))
  11678. (org-toggle-tag tag (if off 'off 'on))
  11679. (setq newhead (org-get-heading)))))
  11680. (and agendap (org-agenda-change-all-lines newhead m))))
  11681. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11682. (defun org-tags-completion-function (string predicate &optional flag)
  11683. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11684. (confirm (lambda (x) (stringp (car x)))))
  11685. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11686. (setq s1 (match-string 1 string)
  11687. s2 (match-string 2 string))
  11688. (setq s1 "" s2 string))
  11689. (cond
  11690. ((eq flag nil)
  11691. ;; try completion
  11692. (setq rtn (try-completion s2 ctable confirm))
  11693. (if (stringp rtn)
  11694. (setq rtn
  11695. (concat s1 s2 (substring rtn (length s2))
  11696. (if (and org-add-colon-after-tag-completion
  11697. (assoc rtn ctable))
  11698. ":" ""))))
  11699. rtn)
  11700. ((eq flag t)
  11701. ;; all-completions
  11702. (all-completions s2 ctable confirm)
  11703. )
  11704. ((eq flag 'lambda)
  11705. ;; exact match?
  11706. (assoc s2 ctable)))
  11707. ))
  11708. (defun org-fast-tag-insert (kwd tags face &optional end)
  11709. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11710. (insert (format "%-12s" (concat kwd ":"))
  11711. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11712. (or end "")))
  11713. (defun org-fast-tag-show-exit (flag)
  11714. (save-excursion
  11715. (org-goto-line 3)
  11716. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11717. (replace-match ""))
  11718. (when flag
  11719. (end-of-line 1)
  11720. (org-move-to-column (- (window-width) 19) t)
  11721. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11722. (defun org-set-current-tags-overlay (current prefix)
  11723. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11724. (if (featurep 'xemacs)
  11725. (org-overlay-display org-tags-overlay (concat prefix s)
  11726. 'secondary-selection)
  11727. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11728. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11729. (defvar org-last-tag-selection-key nil)
  11730. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11731. "Fast tag selection with single keys.
  11732. CURRENT is the current list of tags in the headline, INHERITED is the
  11733. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11734. possibly with grouping information. TODO-TABLE is a similar table with
  11735. TODO keywords, should these have keys assigned to them.
  11736. If the keys are nil, a-z are automatically assigned.
  11737. Returns the new tags string, or nil to not change the current settings."
  11738. (let* ((fulltable (append table todo-table))
  11739. (maxlen (apply 'max (mapcar
  11740. (lambda (x)
  11741. (if (stringp (car x)) (string-width (car x)) 0))
  11742. fulltable)))
  11743. (buf (current-buffer))
  11744. (expert (eq org-fast-tag-selection-single-key 'expert))
  11745. (buffer-tags nil)
  11746. (fwidth (+ maxlen 3 1 3))
  11747. (ncol (/ (- (window-width) 4) fwidth))
  11748. (i-face 'org-done)
  11749. (c-face 'org-todo)
  11750. tg cnt e c char c1 c2 ntable tbl rtn
  11751. ov-start ov-end ov-prefix
  11752. (exit-after-next org-fast-tag-selection-single-key)
  11753. (done-keywords org-done-keywords)
  11754. groups ingroup)
  11755. (save-excursion
  11756. (beginning-of-line 1)
  11757. (if (looking-at
  11758. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11759. (setq ov-start (match-beginning 1)
  11760. ov-end (match-end 1)
  11761. ov-prefix "")
  11762. (setq ov-start (1- (point-at-eol))
  11763. ov-end (1+ ov-start))
  11764. (skip-chars-forward "^\n\r")
  11765. (setq ov-prefix
  11766. (concat
  11767. (buffer-substring (1- (point)) (point))
  11768. (if (> (current-column) org-tags-column)
  11769. " "
  11770. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11771. (move-overlay org-tags-overlay ov-start ov-end)
  11772. (save-window-excursion
  11773. (if expert
  11774. (set-buffer (get-buffer-create " *Org tags*"))
  11775. (delete-other-windows)
  11776. (split-window-vertically)
  11777. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11778. (erase-buffer)
  11779. (org-set-local 'org-done-keywords done-keywords)
  11780. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11781. (org-fast-tag-insert "Current" current c-face "\n\n")
  11782. (org-fast-tag-show-exit exit-after-next)
  11783. (org-set-current-tags-overlay current ov-prefix)
  11784. (setq tbl fulltable char ?a cnt 0)
  11785. (while (setq e (pop tbl))
  11786. (cond
  11787. ((equal (car e) :startgroup)
  11788. (push '() groups) (setq ingroup t)
  11789. (when (not (= cnt 0))
  11790. (setq cnt 0)
  11791. (insert "\n"))
  11792. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11793. ((equal (car e) :endgroup)
  11794. (setq ingroup nil cnt 0)
  11795. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11796. ((equal e '(:newline))
  11797. (when (not (= cnt 0))
  11798. (setq cnt 0)
  11799. (insert "\n")
  11800. (setq e (car tbl))
  11801. (while (equal (car tbl) '(:newline))
  11802. (insert "\n")
  11803. (setq tbl (cdr tbl)))))
  11804. (t
  11805. (setq tg (copy-sequence (car e)) c2 nil)
  11806. (if (cdr e)
  11807. (setq c (cdr e))
  11808. ;; automatically assign a character.
  11809. (setq c1 (string-to-char
  11810. (downcase (substring
  11811. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11812. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11813. (while (or (rassoc char ntable) (rassoc char table))
  11814. (setq char (1+ char)))
  11815. (setq c2 c1))
  11816. (setq c (or c2 char)))
  11817. (if ingroup (push tg (car groups)))
  11818. (setq tg (org-add-props tg nil 'face
  11819. (cond
  11820. ((not (assoc tg table))
  11821. (org-get-todo-face tg))
  11822. ((member tg current) c-face)
  11823. ((member tg inherited) i-face)
  11824. (t nil))))
  11825. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11826. (insert "[" c "] " tg (make-string
  11827. (- fwidth 4 (length tg)) ?\ ))
  11828. (push (cons tg c) ntable)
  11829. (when (= (setq cnt (1+ cnt)) ncol)
  11830. (insert "\n")
  11831. (if ingroup (insert " "))
  11832. (setq cnt 0)))))
  11833. (setq ntable (nreverse ntable))
  11834. (insert "\n")
  11835. (goto-char (point-min))
  11836. (if (not expert) (org-fit-window-to-buffer))
  11837. (setq rtn
  11838. (catch 'exit
  11839. (while t
  11840. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11841. (if (not groups) "no " "")
  11842. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11843. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11844. (setq org-last-tag-selection-key c)
  11845. (cond
  11846. ((= c ?\r) (throw 'exit t))
  11847. ((= c ?!)
  11848. (setq groups (not groups))
  11849. (goto-char (point-min))
  11850. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11851. ((= c ?\C-c)
  11852. (if (not expert)
  11853. (org-fast-tag-show-exit
  11854. (setq exit-after-next (not exit-after-next)))
  11855. (setq expert nil)
  11856. (delete-other-windows)
  11857. (split-window-vertically)
  11858. (org-switch-to-buffer-other-window " *Org tags*")
  11859. (org-fit-window-to-buffer)))
  11860. ((or (= c ?\C-g)
  11861. (and (= c ?q) (not (rassoc c ntable))))
  11862. (org-detach-overlay org-tags-overlay)
  11863. (setq quit-flag t))
  11864. ((= c ?\ )
  11865. (setq current nil)
  11866. (if exit-after-next (setq exit-after-next 'now)))
  11867. ((= c ?\t)
  11868. (condition-case nil
  11869. (setq tg (org-icompleting-read
  11870. "Tag: "
  11871. (or buffer-tags
  11872. (with-current-buffer buf
  11873. (org-get-buffer-tags)))))
  11874. (quit (setq tg "")))
  11875. (when (string-match "\\S-" tg)
  11876. (add-to-list 'buffer-tags (list tg))
  11877. (if (member tg current)
  11878. (setq current (delete tg current))
  11879. (push tg current)))
  11880. (if exit-after-next (setq exit-after-next 'now)))
  11881. ((setq e (rassoc c todo-table) tg (car e))
  11882. (with-current-buffer buf
  11883. (save-excursion (org-todo tg)))
  11884. (if exit-after-next (setq exit-after-next 'now)))
  11885. ((setq e (rassoc c ntable) tg (car e))
  11886. (if (member tg current)
  11887. (setq current (delete tg current))
  11888. (loop for g in groups do
  11889. (if (member tg g)
  11890. (mapc (lambda (x)
  11891. (setq current (delete x current)))
  11892. g)))
  11893. (push tg current))
  11894. (if exit-after-next (setq exit-after-next 'now))))
  11895. ;; Create a sorted list
  11896. (setq current
  11897. (sort current
  11898. (lambda (a b)
  11899. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11900. (if (eq exit-after-next 'now) (throw 'exit t))
  11901. (goto-char (point-min))
  11902. (beginning-of-line 2)
  11903. (delete-region (point) (point-at-eol))
  11904. (org-fast-tag-insert "Current" current c-face)
  11905. (org-set-current-tags-overlay current ov-prefix)
  11906. (while (re-search-forward
  11907. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  11908. (setq tg (match-string 1))
  11909. (add-text-properties
  11910. (match-beginning 1) (match-end 1)
  11911. (list 'face
  11912. (cond
  11913. ((member tg current) c-face)
  11914. ((member tg inherited) i-face)
  11915. (t (get-text-property (match-beginning 1) 'face))))))
  11916. (goto-char (point-min)))))
  11917. (org-detach-overlay org-tags-overlay)
  11918. (if rtn
  11919. (mapconcat 'identity current ":")
  11920. nil))))
  11921. (defun org-get-tags-string ()
  11922. "Get the TAGS string in the current headline."
  11923. (unless (org-on-heading-p t)
  11924. (error "Not on a heading"))
  11925. (save-excursion
  11926. (beginning-of-line 1)
  11927. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11928. (org-match-string-no-properties 1)
  11929. "")))
  11930. (defun org-get-tags ()
  11931. "Get the list of tags specified in the current headline."
  11932. (org-split-string (org-get-tags-string) ":"))
  11933. (defun org-get-buffer-tags ()
  11934. "Get a table of all tags used in the buffer, for completion."
  11935. (let (tags)
  11936. (save-excursion
  11937. (goto-char (point-min))
  11938. (while (re-search-forward
  11939. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  11940. (when (equal (char-after (point-at-bol 0)) ?*)
  11941. (mapc (lambda (x) (add-to-list 'tags x))
  11942. (org-split-string (org-match-string-no-properties 1) ":")))))
  11943. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11944. (mapcar 'list tags)))
  11945. ;;;; The mapping API
  11946. ;;;###autoload
  11947. (defun org-map-entries (func &optional match scope &rest skip)
  11948. "Call FUNC at each headline selected by MATCH in SCOPE.
  11949. FUNC is a function or a lisp form. The function will be called without
  11950. arguments, with the cursor positioned at the beginning of the headline.
  11951. The return values of all calls to the function will be collected and
  11952. returned as a list.
  11953. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11954. does not need to preserve point. After evaluation, the cursor will be
  11955. moved to the end of the line (presumably of the headline of the
  11956. processed entry) and search continues from there. Under some
  11957. circumstances, this may not produce the wanted results. For example,
  11958. if you have removed (e.g. archived) the current (sub)tree it could
  11959. mean that the next entry will be skipped entirely. In such cases, you
  11960. can specify the position from where search should continue by making
  11961. FUNC set the variable `org-map-continue-from' to the desired buffer
  11962. position.
  11963. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11964. Only headlines that are matched by this query will be considered during
  11965. the iteration. When MATCH is nil or t, all headlines will be
  11966. visited by the iteration.
  11967. SCOPE determines the scope of this command. It can be any of:
  11968. nil The current buffer, respecting the restriction if any
  11969. tree The subtree started with the entry at point
  11970. file The current buffer, without restriction
  11971. file-with-archives
  11972. The current buffer, and any archives associated with it
  11973. agenda All agenda files
  11974. agenda-with-archives
  11975. All agenda files with any archive files associated with them
  11976. \(file1 file2 ...)
  11977. If this is a list, all files in the list will be scanned
  11978. The remaining args are treated as settings for the skipping facilities of
  11979. the scanner. The following items can be given here:
  11980. archive skip trees with the archive tag.
  11981. comment skip trees with the COMMENT keyword
  11982. function or Emacs Lisp form:
  11983. will be used as value for `org-agenda-skip-function', so whenever
  11984. the function returns t, FUNC will not be called for that
  11985. entry and search will continue from the point where the
  11986. function leaves it.
  11987. If your function needs to retrieve the tags including inherited tags
  11988. at the *current* entry, you can use the value of the variable
  11989. `org-scanner-tags' which will be much faster than getting the value
  11990. with `org-get-tags-at'. If your function gets properties with
  11991. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11992. to t around the call to `org-entry-properties' to get the same speedup.
  11993. Note that if your function moves around to retrieve tags and properties at
  11994. a *different* entry, you cannot use these techniques."
  11995. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11996. (org-agenda-skip-archived-trees (memq 'archive skip))
  11997. (org-agenda-skip-comment-trees (memq 'comment skip))
  11998. (org-agenda-skip-function
  11999. (car (org-delete-all '(comment archive) skip)))
  12000. (org-tags-match-list-sublevels t)
  12001. matcher file res
  12002. org-todo-keywords-for-agenda
  12003. org-done-keywords-for-agenda
  12004. org-todo-keyword-alist-for-agenda
  12005. org-drawers-for-agenda
  12006. org-tag-alist-for-agenda)
  12007. (cond
  12008. ((eq match t) (setq matcher t))
  12009. ((eq match nil) (setq matcher t))
  12010. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12011. (save-excursion
  12012. (save-restriction
  12013. (when (eq scope 'tree)
  12014. (org-back-to-heading t)
  12015. (org-narrow-to-subtree)
  12016. (setq scope nil))
  12017. (if (not scope)
  12018. (progn
  12019. (org-prepare-agenda-buffers
  12020. (list (buffer-file-name (current-buffer))))
  12021. (setq res (org-scan-tags func matcher)))
  12022. ;; Get the right scope
  12023. (cond
  12024. ((and scope (listp scope) (symbolp (car scope)))
  12025. (setq scope (eval scope)))
  12026. ((eq scope 'agenda)
  12027. (setq scope (org-agenda-files t)))
  12028. ((eq scope 'agenda-with-archives)
  12029. (setq scope (org-agenda-files t))
  12030. (setq scope (org-add-archive-files scope)))
  12031. ((eq scope 'file)
  12032. (setq scope (list (buffer-file-name))))
  12033. ((eq scope 'file-with-archives)
  12034. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12035. (org-prepare-agenda-buffers scope)
  12036. (while (setq file (pop scope))
  12037. (with-current-buffer (org-find-base-buffer-visiting file)
  12038. (save-excursion
  12039. (save-restriction
  12040. (widen)
  12041. (goto-char (point-min))
  12042. (setq res (append res (org-scan-tags func matcher))))))))))
  12043. res))
  12044. ;;;; Properties
  12045. ;;; Setting and retrieving properties
  12046. (defconst org-special-properties
  12047. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12048. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  12049. "The special properties valid in Org-mode.
  12050. These are properties that are not defined in the property drawer,
  12051. but in some other way.")
  12052. (defconst org-default-properties
  12053. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12054. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12055. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12056. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12057. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12058. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12059. "Some properties that are used by Org-mode for various purposes.
  12060. Being in this list makes sure that they are offered for completion.")
  12061. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12062. "Regular expression matching the first line of a property drawer.")
  12063. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12064. "Regular expression matching the last line of a property drawer.")
  12065. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12066. "Regular expression matching the first line of a property drawer.")
  12067. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12068. "Regular expression matching the first line of a property drawer.")
  12069. (defconst org-property-drawer-re
  12070. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12071. org-property-end-re "\\)\n?")
  12072. "Matches an entire property drawer.")
  12073. (defconst org-clock-drawer-re
  12074. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12075. org-property-end-re "\\)\n?")
  12076. "Matches an entire clock drawer.")
  12077. (defun org-property-action ()
  12078. "Do an action on properties."
  12079. (interactive)
  12080. (let (c)
  12081. (org-at-property-p)
  12082. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12083. (setq c (read-char-exclusive))
  12084. (cond
  12085. ((equal c ?s)
  12086. (call-interactively 'org-set-property))
  12087. ((equal c ?d)
  12088. (call-interactively 'org-delete-property))
  12089. ((equal c ?D)
  12090. (call-interactively 'org-delete-property-globally))
  12091. ((equal c ?c)
  12092. (call-interactively 'org-compute-property-at-point))
  12093. (t (error "No such property action %c" c)))))
  12094. (defun org-set-effort (&optional value)
  12095. "Set the effort property of the current entry.
  12096. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12097. allowed value."
  12098. (interactive "P")
  12099. (if (equal value 0) (setq value 10))
  12100. (let* ((completion-ignore-case t)
  12101. (prop org-effort-property)
  12102. (cur (org-entry-get nil prop))
  12103. (allowed (org-property-get-allowed-values nil prop 'table))
  12104. (existing (mapcar 'list (org-property-values prop)))
  12105. rpl
  12106. (val (cond
  12107. ((stringp value) value)
  12108. ((and allowed (integerp value))
  12109. (or (car (nth (1- value) allowed))
  12110. (car (org-last allowed))))
  12111. (allowed
  12112. (message "Select 1-9,0, [RET%s]: %s"
  12113. (if cur (concat "=" cur) "")
  12114. (mapconcat 'car allowed " "))
  12115. (setq rpl (read-char-exclusive))
  12116. (if (equal rpl ?\r)
  12117. cur
  12118. (setq rpl (- rpl ?0))
  12119. (if (equal rpl 0) (setq rpl 10))
  12120. (if (and (> rpl 0) (<= rpl (length allowed)))
  12121. (car (nth (1- rpl) allowed))
  12122. (org-completing-read "Effort: " allowed nil))))
  12123. (t
  12124. (let (org-completion-use-ido org-completion-use-iswitchb)
  12125. (org-completing-read
  12126. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12127. (concat "[" cur "]") "")
  12128. ": ")
  12129. existing nil nil "" nil cur))))))
  12130. (unless (equal (org-entry-get nil prop) val)
  12131. (org-entry-put nil prop val))
  12132. (message "%s is now %s" prop val)))
  12133. (defun org-at-property-p ()
  12134. "Is cursor inside a property drawer?"
  12135. (save-excursion
  12136. (beginning-of-line 1)
  12137. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12138. (save-match-data ;; Used by calling procedures
  12139. (let ((p (point))
  12140. (range (unless (org-before-first-heading-p)
  12141. (org-get-property-block))))
  12142. (and range (<= (car range) p) (< p (cdr range))))))))
  12143. (defun org-get-property-block (&optional beg end force)
  12144. "Return the (beg . end) range of the body of the property drawer.
  12145. BEG and END can be beginning and end of subtree, if not given
  12146. they will be found.
  12147. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12148. (catch 'exit
  12149. (save-excursion
  12150. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12151. (end (or end (progn (outline-next-heading) (point)))))
  12152. (goto-char beg)
  12153. (if (re-search-forward org-property-start-re end t)
  12154. (setq beg (1+ (match-end 0)))
  12155. (if force
  12156. (save-excursion
  12157. (org-insert-property-drawer)
  12158. (setq end (progn (outline-next-heading) (point))))
  12159. (throw 'exit nil))
  12160. (goto-char beg)
  12161. (if (re-search-forward org-property-start-re end t)
  12162. (setq beg (1+ (match-end 0)))))
  12163. (if (re-search-forward org-property-end-re end t)
  12164. (setq end (match-beginning 0))
  12165. (or force (throw 'exit nil))
  12166. (goto-char beg)
  12167. (setq end beg)
  12168. (org-indent-line-function)
  12169. (insert ":END:\n"))
  12170. (cons beg end)))))
  12171. (defun org-entry-properties (&optional pom which specific)
  12172. "Get all properties of the entry at point-or-marker POM.
  12173. This includes the TODO keyword, the tags, time strings for deadline,
  12174. scheduled, and clocking, and any additional properties defined in the
  12175. entry. The return value is an alist, keys may occur multiple times
  12176. if the property key was used several times.
  12177. POM may also be nil, in which case the current entry is used.
  12178. If WHICH is nil or `all', get all properties. If WHICH is
  12179. `special' or `standard', only get that subclass. If WHICH
  12180. is a string only get exactly this property. Specific can be a string, the
  12181. specific property we are interested in. Specifying it can speed
  12182. things up because then unnecessary parsing is avoided."
  12183. (setq which (or which 'all))
  12184. (org-with-point-at pom
  12185. (let ((clockstr (substring org-clock-string 0 -1))
  12186. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12187. (case-fold-search nil)
  12188. beg end range props sum-props key key1 value string clocksum)
  12189. (save-excursion
  12190. (when (condition-case nil
  12191. (and (org-mode-p) (org-back-to-heading t))
  12192. (error nil))
  12193. (setq beg (point))
  12194. (setq sum-props (get-text-property (point) 'org-summaries))
  12195. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12196. (outline-next-heading)
  12197. (setq end (point))
  12198. (when (memq which '(all special))
  12199. ;; Get the special properties, like TODO and tags
  12200. (goto-char beg)
  12201. (when (and (or (not specific) (string= specific "TODO"))
  12202. (looking-at org-todo-line-regexp) (match-end 2))
  12203. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12204. (when (and (or (not specific) (string= specific "PRIORITY"))
  12205. (looking-at org-priority-regexp))
  12206. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12207. (when (and (or (not specific) (string= specific "TAGS"))
  12208. (setq value (org-get-tags-string))
  12209. (string-match "\\S-" value))
  12210. (push (cons "TAGS" value) props))
  12211. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12212. (setq value (org-get-tags-at)))
  12213. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12214. ":"))
  12215. props))
  12216. (when (or (not specific) (string= specific "BLOCKED"))
  12217. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12218. (when (or (not specific)
  12219. (member specific
  12220. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12221. "TIMESTAMP" "TIMESTAMP_IA")))
  12222. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12223. (setq key (if (match-end 1)
  12224. (substring (org-match-string-no-properties 1)
  12225. 0 -1))
  12226. string (if (equal key clockstr)
  12227. (org-no-properties
  12228. (org-trim
  12229. (buffer-substring
  12230. (match-beginning 3) (goto-char
  12231. (point-at-eol)))))
  12232. (substring (org-match-string-no-properties 3)
  12233. 1 -1)))
  12234. ;; Get the correct property name from the key. This is
  12235. ;; necessary if the user has configured time keywords.
  12236. (setq key1 (concat key ":"))
  12237. (cond
  12238. ((not key)
  12239. (setq key
  12240. (if (= (char-after (match-beginning 3)) ?\[)
  12241. "TIMESTAMP_IA" "TIMESTAMP")))
  12242. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12243. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12244. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12245. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12246. (when (or (equal key "CLOCK") (not (assoc key props)))
  12247. (push (cons key string) props))))
  12248. )
  12249. (when (memq which '(all standard))
  12250. ;; Get the standard properties, like :PROP: ...
  12251. (setq range (org-get-property-block beg end))
  12252. (when range
  12253. (goto-char (car range))
  12254. (while (re-search-forward
  12255. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12256. (cdr range) t)
  12257. (setq key (org-match-string-no-properties 1)
  12258. value (org-trim (or (org-match-string-no-properties 2) "")))
  12259. (unless (member key excluded)
  12260. (push (cons key (or value "")) props)))))
  12261. (if clocksum
  12262. (push (cons "CLOCKSUM"
  12263. (org-columns-number-to-string (/ (float clocksum) 60.)
  12264. 'add_times))
  12265. props))
  12266. (unless (assoc "CATEGORY" props)
  12267. (setq value (or (org-get-category)
  12268. (progn (org-refresh-category-properties)
  12269. (org-get-category))))
  12270. (push (cons "CATEGORY" value) props))
  12271. (append sum-props (nreverse props)))))))
  12272. (defun org-entry-get (pom property &optional inherit literal-nil)
  12273. "Get value of PROPERTY for entry at point-or-marker POM.
  12274. If INHERIT is non-nil and the entry does not have the property,
  12275. then also check higher levels of the hierarchy.
  12276. If INHERIT is the symbol `selective', use inheritance only if the setting
  12277. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12278. If the property is present but empty, the return value is the empty string.
  12279. If the property is not present at all, nil is returned.
  12280. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12281. do not interpret it as the list atom nil. This is used for inheritance
  12282. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12283. (org-with-point-at pom
  12284. (if (and inherit (if (eq inherit 'selective)
  12285. (org-property-inherit-p property)
  12286. t))
  12287. (org-entry-get-with-inheritance property literal-nil)
  12288. (if (member property org-special-properties)
  12289. ;; We need a special property. Use `org-entry-properties' to
  12290. ;; retrieve it, but specify the wanted property
  12291. (cdr (assoc property (org-entry-properties nil 'special property)))
  12292. (let ((range (org-get-property-block)))
  12293. (if (and range
  12294. (goto-char (car range))
  12295. (re-search-forward
  12296. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12297. (cdr range) t))
  12298. ;; Found the property, return it.
  12299. (if (match-end 1)
  12300. (if literal-nil
  12301. (org-match-string-no-properties 1)
  12302. (org-not-nil (org-match-string-no-properties 1)))
  12303. "")))))))
  12304. (defun org-property-or-variable-value (var &optional inherit)
  12305. "Check if there is a property fixing the value of VAR.
  12306. If yes, return this value. If not, return the current value of the variable."
  12307. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12308. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12309. (read prop)
  12310. (symbol-value var))))
  12311. (defun org-entry-delete (pom property)
  12312. "Delete the property PROPERTY from entry at point-or-marker POM."
  12313. (org-with-point-at pom
  12314. (if (member property org-special-properties)
  12315. nil ; cannot delete these properties.
  12316. (let ((range (org-get-property-block)))
  12317. (if (and range
  12318. (goto-char (car range))
  12319. (re-search-forward
  12320. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12321. (cdr range) t))
  12322. (progn
  12323. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12324. t)
  12325. nil)))))
  12326. ;; Multi-values properties are properties that contain multiple values
  12327. ;; These values are assumed to be single words, separated by whitespace.
  12328. (defun org-entry-add-to-multivalued-property (pom property value)
  12329. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12330. (let* ((old (org-entry-get pom property))
  12331. (values (and old (org-split-string old "[ \t]"))))
  12332. (setq value (org-entry-protect-space value))
  12333. (unless (member value values)
  12334. (setq values (cons value values))
  12335. (org-entry-put pom property
  12336. (mapconcat 'identity values " ")))))
  12337. (defun org-entry-remove-from-multivalued-property (pom property value)
  12338. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12339. (let* ((old (org-entry-get pom property))
  12340. (values (and old (org-split-string old "[ \t]"))))
  12341. (setq value (org-entry-protect-space value))
  12342. (when (member value values)
  12343. (setq values (delete value values))
  12344. (org-entry-put pom property
  12345. (mapconcat 'identity values " ")))))
  12346. (defun org-entry-member-in-multivalued-property (pom property value)
  12347. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12348. (let* ((old (org-entry-get pom property))
  12349. (values (and old (org-split-string old "[ \t]"))))
  12350. (setq value (org-entry-protect-space value))
  12351. (member value values)))
  12352. (defun org-entry-get-multivalued-property (pom property)
  12353. "Return a list of values in a multivalued property."
  12354. (let* ((value (org-entry-get pom property))
  12355. (values (and value (org-split-string value "[ \t]"))))
  12356. (mapcar 'org-entry-restore-space values)))
  12357. (defun org-entry-put-multivalued-property (pom property &rest values)
  12358. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12359. VALUES should be a list of strings. Spaces will be protected."
  12360. (org-entry-put pom property
  12361. (mapconcat 'org-entry-protect-space values " "))
  12362. (let* ((value (org-entry-get pom property))
  12363. (values (and value (org-split-string value "[ \t]"))))
  12364. (mapcar 'org-entry-restore-space values)))
  12365. (defun org-entry-protect-space (s)
  12366. "Protect spaces and newline in string S."
  12367. (while (string-match " " s)
  12368. (setq s (replace-match "%20" t t s)))
  12369. (while (string-match "\n" s)
  12370. (setq s (replace-match "%0A" t t s)))
  12371. s)
  12372. (defun org-entry-restore-space (s)
  12373. "Restore spaces and newline in string S."
  12374. (while (string-match "%20" s)
  12375. (setq s (replace-match " " t t s)))
  12376. (while (string-match "%0A" s)
  12377. (setq s (replace-match "\n" t t s)))
  12378. s)
  12379. (defvar org-entry-property-inherited-from (make-marker)
  12380. "Marker pointing to the entry from where a property was inherited.
  12381. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12382. location of the entry where the inheritance search matched. If there was
  12383. no match, the marker will point nowhere.
  12384. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12385. is set.")
  12386. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12387. "Get entry property, and search higher levels if not present.
  12388. The search will stop at the first ancestor which has the property defined.
  12389. If the value found is \"nil\", return nil to show that the property
  12390. should be considered as undefined (this is the meaning of nil here).
  12391. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12392. (move-marker org-entry-property-inherited-from nil)
  12393. (let (tmp)
  12394. (save-excursion
  12395. (save-restriction
  12396. (widen)
  12397. (catch 'ex
  12398. (while t
  12399. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12400. (org-back-to-heading t)
  12401. (move-marker org-entry-property-inherited-from (point))
  12402. (throw 'ex tmp))
  12403. (or (org-up-heading-safe) (throw 'ex nil)))))
  12404. (setq tmp (or tmp
  12405. (cdr (assoc property org-file-properties))
  12406. (cdr (assoc property org-global-properties))
  12407. (cdr (assoc property org-global-properties-fixed))))
  12408. (if literal-nil tmp (org-not-nil tmp)))))
  12409. (defvar org-property-changed-functions nil
  12410. "Hook called when the value of a property has changed.
  12411. Each hook function should accept two arguments, the name of the property
  12412. and the new value.")
  12413. (defun org-entry-put (pom property value)
  12414. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12415. (org-with-point-at pom
  12416. (org-back-to-heading t)
  12417. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12418. range)
  12419. (cond
  12420. ((equal property "TODO")
  12421. (when (and (stringp value) (string-match "\\S-" value)
  12422. (not (member value org-todo-keywords-1)))
  12423. (error "\"%s\" is not a valid TODO state" value))
  12424. (if (or (not value)
  12425. (not (string-match "\\S-" value)))
  12426. (setq value 'none))
  12427. (org-todo value)
  12428. (org-set-tags nil 'align))
  12429. ((equal property "PRIORITY")
  12430. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12431. (string-to-char value) ?\ ))
  12432. (org-set-tags nil 'align))
  12433. ((equal property "SCHEDULED")
  12434. (if (re-search-forward org-scheduled-time-regexp end t)
  12435. (cond
  12436. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12437. ((eq value 'later) (org-timestamp-change 1 'day))
  12438. (t (call-interactively 'org-schedule)))
  12439. (call-interactively 'org-schedule)))
  12440. ((equal property "DEADLINE")
  12441. (if (re-search-forward org-deadline-time-regexp end t)
  12442. (cond
  12443. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12444. ((eq value 'later) (org-timestamp-change 1 'day))
  12445. (t (call-interactively 'org-deadline)))
  12446. (call-interactively 'org-deadline)))
  12447. ((member property org-special-properties)
  12448. (error "The %s property can not yet be set with `org-entry-put'"
  12449. property))
  12450. (t ; a non-special property
  12451. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12452. (setq range (org-get-property-block beg end 'force))
  12453. (goto-char (car range))
  12454. (if (re-search-forward
  12455. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12456. (progn
  12457. (delete-region (match-beginning 1) (match-end 1))
  12458. (goto-char (match-beginning 1)))
  12459. (goto-char (cdr range))
  12460. (insert "\n")
  12461. (backward-char 1)
  12462. (org-indent-line-function)
  12463. (insert ":" property ":"))
  12464. (and value (insert " " value))
  12465. (org-indent-line-function)))))
  12466. (run-hook-with-args 'org-property-changed-functions property value)))
  12467. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12468. "Get all property keys in the current buffer.
  12469. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12470. like tags and TODO state.
  12471. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12472. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12473. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12474. formats in the current buffer."
  12475. (let (rtn range cfmt s p)
  12476. (save-excursion
  12477. (save-restriction
  12478. (widen)
  12479. (goto-char (point-min))
  12480. (while (re-search-forward org-property-start-re nil t)
  12481. (setq range (org-get-property-block))
  12482. (goto-char (car range))
  12483. (while (re-search-forward
  12484. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12485. (cdr range) t)
  12486. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12487. (outline-next-heading))))
  12488. (when include-specials
  12489. (setq rtn (append org-special-properties rtn)))
  12490. (when include-defaults
  12491. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12492. (add-to-list 'rtn org-effort-property))
  12493. (when include-columns
  12494. (save-excursion
  12495. (save-restriction
  12496. (widen)
  12497. (goto-char (point-min))
  12498. (while (re-search-forward
  12499. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12500. nil t)
  12501. (setq cfmt (match-string 2) s 0)
  12502. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12503. cfmt s)
  12504. (setq s (match-end 0)
  12505. p (match-string 1 cfmt))
  12506. (unless (or (equal p "ITEM")
  12507. (member p org-special-properties))
  12508. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12509. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12510. (defun org-property-values (key)
  12511. "Return a list of all values of property KEY."
  12512. (save-excursion
  12513. (save-restriction
  12514. (widen)
  12515. (goto-char (point-min))
  12516. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12517. values)
  12518. (while (re-search-forward re nil t)
  12519. (add-to-list 'values (org-trim (match-string 1))))
  12520. (delete "" values)))))
  12521. (defun org-insert-property-drawer ()
  12522. "Insert a property drawer into the current entry."
  12523. (interactive)
  12524. (org-back-to-heading t)
  12525. (looking-at outline-regexp)
  12526. (let ((indent (if org-adapt-indentation
  12527. (- (match-end 0)(match-beginning 0))
  12528. 0))
  12529. (beg (point))
  12530. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12531. end hiddenp)
  12532. (outline-next-heading)
  12533. (setq end (point))
  12534. (goto-char beg)
  12535. (while (re-search-forward re end t))
  12536. (setq hiddenp (org-invisible-p))
  12537. (end-of-line 1)
  12538. (and (equal (char-after) ?\n) (forward-char 1))
  12539. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12540. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12541. ;; just skip this line
  12542. (beginning-of-line 2)
  12543. ;; Drawer start, find the end
  12544. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12545. (beginning-of-line 1)))
  12546. (org-skip-over-state-notes)
  12547. (skip-chars-backward " \t\n\r")
  12548. (if (eq (char-before) ?*) (forward-char 1))
  12549. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12550. (beginning-of-line 0)
  12551. (org-indent-to-column indent)
  12552. (beginning-of-line 2)
  12553. (org-indent-to-column indent)
  12554. (beginning-of-line 0)
  12555. (if hiddenp
  12556. (save-excursion
  12557. (org-back-to-heading t)
  12558. (hide-entry))
  12559. (org-flag-drawer t))))
  12560. (defun org-set-property (property value)
  12561. "In the current entry, set PROPERTY to VALUE.
  12562. When called interactively, this will prompt for a property name, offering
  12563. completion on existing and default properties. And then it will prompt
  12564. for a value, offering completion either on allowed values (via an inherited
  12565. xxx_ALL property) or on existing values in other instances of this property
  12566. in the current file."
  12567. (interactive
  12568. (let* ((completion-ignore-case t)
  12569. (keys (org-buffer-property-keys nil t t))
  12570. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12571. (prop (if (member prop0 keys)
  12572. prop0
  12573. (or (cdr (assoc (downcase prop0)
  12574. (mapcar (lambda (x) (cons (downcase x) x))
  12575. keys)))
  12576. prop0)))
  12577. (cur (org-entry-get nil prop))
  12578. (prompt (concat prop " value"
  12579. (if (and cur (string-match "\\S-" cur))
  12580. (concat " [" cur "]") "") ": "))
  12581. (allowed (org-property-get-allowed-values nil prop 'table))
  12582. (existing (mapcar 'list (org-property-values prop)))
  12583. (val (if allowed
  12584. (org-completing-read prompt allowed nil
  12585. (not (get-text-property 0 'org-unrestricted
  12586. (caar allowed))))
  12587. (let (org-completion-use-ido org-completion-use-iswitchb)
  12588. (org-completing-read prompt existing nil nil "" nil cur)))))
  12589. (list prop (if (equal val "") cur val))))
  12590. (unless (equal (org-entry-get nil property) value)
  12591. (org-entry-put nil property value)))
  12592. (defun org-delete-property (property)
  12593. "In the current entry, delete PROPERTY."
  12594. (interactive
  12595. (let* ((completion-ignore-case t)
  12596. (prop (org-icompleting-read "Property: "
  12597. (org-entry-properties nil 'standard))))
  12598. (list prop)))
  12599. (message "Property %s %s" property
  12600. (if (org-entry-delete nil property)
  12601. "deleted"
  12602. "was not present in the entry")))
  12603. (defun org-delete-property-globally (property)
  12604. "Remove PROPERTY globally, from all entries."
  12605. (interactive
  12606. (let* ((completion-ignore-case t)
  12607. (prop (org-icompleting-read
  12608. "Globally remove property: "
  12609. (mapcar 'list (org-buffer-property-keys)))))
  12610. (list prop)))
  12611. (save-excursion
  12612. (save-restriction
  12613. (widen)
  12614. (goto-char (point-min))
  12615. (let ((cnt 0))
  12616. (while (re-search-forward
  12617. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12618. nil t)
  12619. (setq cnt (1+ cnt))
  12620. (replace-match ""))
  12621. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12622. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12623. (defun org-compute-property-at-point ()
  12624. "Compute the property at point.
  12625. This looks for an enclosing column format, extracts the operator and
  12626. then applies it to the property in the column format's scope."
  12627. (interactive)
  12628. (unless (org-at-property-p)
  12629. (error "Not at a property"))
  12630. (let ((prop (org-match-string-no-properties 2)))
  12631. (org-columns-get-format-and-top-level)
  12632. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12633. (error "No operator defined for property %s" prop))
  12634. (org-columns-compute prop)))
  12635. (defvar org-property-allowed-value-functions nil
  12636. "Hook for functions supplying allowed values for a specific property.
  12637. The functions must take a single argument, the name of the property, and
  12638. return a flat list of allowed values. If \":ETC\" is one of
  12639. the values, this means that these values are intended as defaults for
  12640. completion, but that other values should be allowed too.
  12641. The functions must return nil if they are not responsible for this
  12642. property.")
  12643. (defun org-property-get-allowed-values (pom property &optional table)
  12644. "Get allowed values for the property PROPERTY.
  12645. When TABLE is non-nil, return an alist that can directly be used for
  12646. completion."
  12647. (let (vals)
  12648. (cond
  12649. ((equal property "TODO")
  12650. (setq vals (org-with-point-at pom
  12651. (append org-todo-keywords-1 '("")))))
  12652. ((equal property "PRIORITY")
  12653. (let ((n org-lowest-priority))
  12654. (while (>= n org-highest-priority)
  12655. (push (char-to-string n) vals)
  12656. (setq n (1- n)))))
  12657. ((member property org-special-properties))
  12658. ((setq vals (run-hook-with-args-until-success
  12659. 'org-property-allowed-value-functions property)))
  12660. (t
  12661. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12662. (when (and vals (string-match "\\S-" vals))
  12663. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12664. (setq vals (mapcar (lambda (x)
  12665. (cond ((stringp x) x)
  12666. ((numberp x) (number-to-string x))
  12667. ((symbolp x) (symbol-name x))
  12668. (t "???")))
  12669. vals)))))
  12670. (when (member ":ETC" vals)
  12671. (setq vals (remove ":ETC" vals))
  12672. (org-add-props (car vals) '(org-unrestricted t)))
  12673. (if table (mapcar 'list vals) vals)))
  12674. (defun org-property-previous-allowed-value (&optional previous)
  12675. "Switch to the next allowed value for this property."
  12676. (interactive)
  12677. (org-property-next-allowed-value t))
  12678. (defun org-property-next-allowed-value (&optional previous)
  12679. "Switch to the next allowed value for this property."
  12680. (interactive)
  12681. (unless (org-at-property-p)
  12682. (error "Not at a property"))
  12683. (let* ((key (match-string 2))
  12684. (value (match-string 3))
  12685. (allowed (or (org-property-get-allowed-values (point) key)
  12686. (and (member value '("[ ]" "[-]" "[X]"))
  12687. '("[ ]" "[X]"))))
  12688. nval)
  12689. (unless allowed
  12690. (error "Allowed values for this property have not been defined"))
  12691. (if previous (setq allowed (reverse allowed)))
  12692. (if (member value allowed)
  12693. (setq nval (car (cdr (member value allowed)))))
  12694. (setq nval (or nval (car allowed)))
  12695. (if (equal nval value)
  12696. (error "Only one allowed value for this property"))
  12697. (org-at-property-p)
  12698. (replace-match (concat " :" key ": " nval) t t)
  12699. (org-indent-line-function)
  12700. (beginning-of-line 1)
  12701. (skip-chars-forward " \t")
  12702. (run-hook-with-args 'org-property-changed-functions key nval)))
  12703. (defun org-find-olp (path &optional this-buffer)
  12704. "Return a marker pointing to the entry at outline path OLP.
  12705. If anything goes wrong, throw an error.
  12706. You can wrap this call to catch the error like this:
  12707. (condition-case msg
  12708. (org-mobile-locate-entry (match-string 4))
  12709. (error (nth 1 msg)))
  12710. The return value will then be either a string with the error message,
  12711. or a marker if everything is OK.
  12712. If THIS-BUFFER is set, the outline path does not contain a file,
  12713. only headings."
  12714. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12715. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12716. (level 1)
  12717. (lmin 1)
  12718. (lmax 1)
  12719. limit re end found pos heading cnt)
  12720. (unless buffer (error "File not found :%s" file))
  12721. (with-current-buffer buffer
  12722. (save-excursion
  12723. (save-restriction
  12724. (widen)
  12725. (setq limit (point-max))
  12726. (goto-char (point-min))
  12727. (while (setq heading (pop path))
  12728. (setq re (format org-complex-heading-regexp-format
  12729. (regexp-quote heading)))
  12730. (setq cnt 0 pos (point))
  12731. (while (re-search-forward re end t)
  12732. (setq level (- (match-end 1) (match-beginning 1)))
  12733. (if (and (>= level lmin) (<= level lmax))
  12734. (setq found (match-beginning 0) cnt (1+ cnt))))
  12735. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12736. lmax heading))
  12737. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12738. lmax heading))
  12739. (goto-char found)
  12740. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12741. (setq end (save-excursion (org-end-of-subtree t t))))
  12742. (when (org-on-heading-p)
  12743. (move-marker (make-marker) (point))))))))
  12744. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  12745. "Find node HEADING in BUFFER.
  12746. Return a marker to the heading if it was found, or nil if not.
  12747. If POS-ONLY is set, return just the position instead of a marker.
  12748. The heading text must match exact, but it may have a TODO keyword,
  12749. a priority cookie and tags in the standard locations."
  12750. (with-current-buffer (or buffer (current-buffer))
  12751. (save-excursion
  12752. (save-restriction
  12753. (widen)
  12754. (goto-char (point-min))
  12755. (let (case-fold-search)
  12756. (if (re-search-forward
  12757. (format org-complex-heading-regexp-format
  12758. (regexp-quote heading)) nil t)
  12759. (if pos-only
  12760. (match-beginning 0)
  12761. (move-marker (make-marker) (match-beginning 0)))))))))
  12762. (defun org-find-exact-heading-in-directory (heading &optional dir)
  12763. "Find Org node headline HEADING in all .org files in directory DIR.
  12764. When the target headline is found, return a marker to this location."
  12765. (let ((files (directory-files (or dir default-directory)
  12766. nil "\\`[^.#].*\\.org\\'"))
  12767. file visiting m buffer)
  12768. (catch 'found
  12769. (while (setq file (pop files))
  12770. (message "trying %s" file)
  12771. (setq visiting (org-find-base-buffer-visiting file))
  12772. (setq buffer (or visiting (find-file-noselect file)))
  12773. (setq m (org-find-exact-headline-in-buffer
  12774. heading buffer))
  12775. (when (and (not m) (not visiting)) (kill-buffer buffer))
  12776. (and m (throw 'found m))))))
  12777. (defun org-find-entry-with-id (ident)
  12778. "Locate the entry that contains the ID property with exact value IDENT.
  12779. IDENT can be a string, a symbol or a number, this function will search for
  12780. the string representation of it.
  12781. Return the position where this entry starts, or nil if there is no such entry."
  12782. (interactive "sID: ")
  12783. (let ((id (cond
  12784. ((stringp ident) ident)
  12785. ((symbol-name ident) (symbol-name ident))
  12786. ((numberp ident) (number-to-string ident))
  12787. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12788. (case-fold-search nil))
  12789. (save-excursion
  12790. (save-restriction
  12791. (widen)
  12792. (goto-char (point-min))
  12793. (when (re-search-forward
  12794. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12795. nil t)
  12796. (org-back-to-heading t)
  12797. (point))))))
  12798. ;;;; Timestamps
  12799. (defvar org-last-changed-timestamp nil)
  12800. (defvar org-last-inserted-timestamp nil
  12801. "The last time stamp inserted with `org-insert-time-stamp'.")
  12802. (defvar org-time-was-given) ; dynamically scoped parameter
  12803. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12804. (defvar org-ts-what) ; dynamically scoped parameter
  12805. (defun org-time-stamp (arg &optional inactive)
  12806. "Prompt for a date/time and insert a time stamp.
  12807. If the user specifies a time like HH:MM, or if this command is called
  12808. with a prefix argument, the time stamp will contain date and time.
  12809. Otherwise, only the date will be included. All parts of a date not
  12810. specified by the user will be filled in from the current date/time.
  12811. So if you press just return without typing anything, the time stamp
  12812. will represent the current date/time. If there is already a timestamp
  12813. at the cursor, it will be modified."
  12814. (interactive "P")
  12815. (let* ((ts nil)
  12816. (default-time
  12817. ;; Default time is either today, or, when entering a range,
  12818. ;; the range start.
  12819. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12820. (save-excursion
  12821. (re-search-backward
  12822. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12823. (- (point) 20) t)))
  12824. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12825. (current-time)))
  12826. (default-input (and ts (org-get-compact-tod ts)))
  12827. org-time-was-given org-end-time-was-given time)
  12828. (cond
  12829. ((and (org-at-timestamp-p t)
  12830. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12831. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12832. (insert "--")
  12833. (setq time (let ((this-command this-command))
  12834. (org-read-date arg 'totime nil nil
  12835. default-time default-input)))
  12836. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12837. ((org-at-timestamp-p t)
  12838. (setq time (let ((this-command this-command))
  12839. (org-read-date arg 'totime nil nil default-time default-input)))
  12840. (when (org-at-timestamp-p t) ; just to get the match data
  12841. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12842. (replace-match "")
  12843. (setq org-last-changed-timestamp
  12844. (org-insert-time-stamp
  12845. time (or org-time-was-given arg)
  12846. inactive nil nil (list org-end-time-was-given))))
  12847. (message "Timestamp updated"))
  12848. (t
  12849. (setq time (let ((this-command this-command))
  12850. (org-read-date arg 'totime nil nil default-time default-input)))
  12851. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12852. nil nil (list org-end-time-was-given))))))
  12853. ;; FIXME: can we use this for something else, like computing time differences?
  12854. (defun org-get-compact-tod (s)
  12855. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12856. (let* ((t1 (match-string 1 s))
  12857. (h1 (string-to-number (match-string 2 s)))
  12858. (m1 (string-to-number (match-string 3 s)))
  12859. (t2 (and (match-end 4) (match-string 5 s)))
  12860. (h2 (and t2 (string-to-number (match-string 6 s))))
  12861. (m2 (and t2 (string-to-number (match-string 7 s))))
  12862. dh dm)
  12863. (if (not t2)
  12864. t1
  12865. (setq dh (- h2 h1) dm (- m2 m1))
  12866. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12867. (concat t1 "+" (number-to-string dh)
  12868. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12869. (defun org-time-stamp-inactive (&optional arg)
  12870. "Insert an inactive time stamp.
  12871. An inactive time stamp is enclosed in square brackets instead of angle
  12872. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12873. does not link to the calendar and cannot be changed with the S-cursor keys.
  12874. So these are more for recording a certain time/date."
  12875. (interactive "P")
  12876. (org-time-stamp arg 'inactive))
  12877. (defvar org-date-ovl (make-overlay 1 1))
  12878. (overlay-put org-date-ovl 'face 'org-warning)
  12879. (org-detach-overlay org-date-ovl)
  12880. (defvar org-ans1) ; dynamically scoped parameter
  12881. (defvar org-ans2) ; dynamically scoped parameter
  12882. (defvar org-plain-time-of-day-regexp) ; defined below
  12883. (defvar org-overriding-default-time nil) ; dynamically scoped
  12884. (defvar org-read-date-overlay nil)
  12885. (defvar org-dcst nil) ; dynamically scoped
  12886. (defvar org-read-date-history nil)
  12887. (defvar org-read-date-final-answer nil)
  12888. (defun org-read-date (&optional with-time to-time from-string prompt
  12889. default-time default-input)
  12890. "Read a date, possibly a time, and make things smooth for the user.
  12891. The prompt will suggest to enter an ISO date, but you can also enter anything
  12892. which will at least partially be understood by `parse-time-string'.
  12893. Unrecognized parts of the date will default to the current day, month, year,
  12894. hour and minute. If this command is called to replace a timestamp at point,
  12895. of to enter the second timestamp of a range, the default time is taken
  12896. from the existing stamp. Furthermore, the command prefers the future,
  12897. so if you are giving a date where the year is not given, and the day-month
  12898. combination is already past in the current year, it will assume you
  12899. mean next year. For details, see the manual. A few examples:
  12900. 3-2-5 --> 2003-02-05
  12901. feb 15 --> currentyear-02-15
  12902. 2/15 --> currentyear-02-15
  12903. sep 12 9 --> 2009-09-12
  12904. 12:45 --> today 12:45
  12905. 22 sept 0:34 --> currentyear-09-22 0:34
  12906. 12 --> currentyear-currentmonth-12
  12907. Fri --> nearest Friday (today or later)
  12908. etc.
  12909. Furthermore you can specify a relative date by giving, as the *first* thing
  12910. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12911. change in days weeks, months, years.
  12912. With a single plus or minus, the date is relative to today. With a double
  12913. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12914. +4d --> four days from today
  12915. +4 --> same as above
  12916. +2w --> two weeks from today
  12917. ++5 --> five days from default date
  12918. The function understands only English month and weekday abbreviations,
  12919. but this can be configured with the variables `parse-time-months' and
  12920. `parse-time-weekdays'.
  12921. While prompting, a calendar is popped up - you can also select the
  12922. date with the mouse (button 1). The calendar shows a period of three
  12923. months. To scroll it to other months, use the keys `>' and `<'.
  12924. If you don't like the calendar, turn it off with
  12925. \(setq org-read-date-popup-calendar nil)
  12926. With optional argument TO-TIME, the date will immediately be converted
  12927. to an internal time.
  12928. With an optional argument WITH-TIME, the prompt will suggest to also
  12929. insert a time. Note that when WITH-TIME is not set, you can still
  12930. enter a time, and this function will inform the calling routine about
  12931. this change. The calling routine may then choose to change the format
  12932. used to insert the time stamp into the buffer to include the time.
  12933. With optional argument FROM-STRING, read from this string instead from
  12934. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12935. the time/date that is used for everything that is not specified by the
  12936. user."
  12937. (require 'parse-time)
  12938. (let* ((org-time-stamp-rounding-minutes
  12939. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12940. (org-dcst org-display-custom-times)
  12941. (ct (org-current-time))
  12942. (def (or org-overriding-default-time default-time ct))
  12943. (defdecode (decode-time def))
  12944. (dummy (progn
  12945. (when (< (nth 2 defdecode) org-extend-today-until)
  12946. (setcar (nthcdr 2 defdecode) -1)
  12947. (setcar (nthcdr 1 defdecode) 59)
  12948. (setq def (apply 'encode-time defdecode)
  12949. defdecode (decode-time def)))))
  12950. (calendar-frame-setup nil)
  12951. (calendar-setup nil)
  12952. (calendar-move-hook nil)
  12953. (calendar-view-diary-initially-flag nil)
  12954. (calendar-view-holidays-initially-flag nil)
  12955. (timestr (format-time-string
  12956. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12957. (prompt (concat (if prompt (concat prompt " ") "")
  12958. (format "Date+time [%s]: " timestr)))
  12959. ans (org-ans0 "") org-ans1 org-ans2 final)
  12960. (cond
  12961. (from-string (setq ans from-string))
  12962. (org-read-date-popup-calendar
  12963. (save-excursion
  12964. (save-window-excursion
  12965. (calendar)
  12966. (calendar-forward-day (- (time-to-days def)
  12967. (calendar-absolute-from-gregorian
  12968. (calendar-current-date))))
  12969. (org-eval-in-calendar nil t)
  12970. (let* ((old-map (current-local-map))
  12971. (map (copy-keymap calendar-mode-map))
  12972. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12973. (org-defkey map (kbd "RET") 'org-calendar-select)
  12974. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  12975. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  12976. (org-defkey minibuffer-local-map [(meta shift left)]
  12977. (lambda () (interactive)
  12978. (org-eval-in-calendar '(calendar-backward-month 1))))
  12979. (org-defkey minibuffer-local-map [(meta shift right)]
  12980. (lambda () (interactive)
  12981. (org-eval-in-calendar '(calendar-forward-month 1))))
  12982. (org-defkey minibuffer-local-map [(meta shift up)]
  12983. (lambda () (interactive)
  12984. (org-eval-in-calendar '(calendar-backward-year 1))))
  12985. (org-defkey minibuffer-local-map [(meta shift down)]
  12986. (lambda () (interactive)
  12987. (org-eval-in-calendar '(calendar-forward-year 1))))
  12988. (org-defkey minibuffer-local-map [?\e (shift left)]
  12989. (lambda () (interactive)
  12990. (org-eval-in-calendar '(calendar-backward-month 1))))
  12991. (org-defkey minibuffer-local-map [?\e (shift right)]
  12992. (lambda () (interactive)
  12993. (org-eval-in-calendar '(calendar-forward-month 1))))
  12994. (org-defkey minibuffer-local-map [?\e (shift up)]
  12995. (lambda () (interactive)
  12996. (org-eval-in-calendar '(calendar-backward-year 1))))
  12997. (org-defkey minibuffer-local-map [?\e (shift down)]
  12998. (lambda () (interactive)
  12999. (org-eval-in-calendar '(calendar-forward-year 1))))
  13000. (org-defkey minibuffer-local-map [(shift up)]
  13001. (lambda () (interactive)
  13002. (org-eval-in-calendar '(calendar-backward-week 1))))
  13003. (org-defkey minibuffer-local-map [(shift down)]
  13004. (lambda () (interactive)
  13005. (org-eval-in-calendar '(calendar-forward-week 1))))
  13006. (org-defkey minibuffer-local-map [(shift left)]
  13007. (lambda () (interactive)
  13008. (org-eval-in-calendar '(calendar-backward-day 1))))
  13009. (org-defkey minibuffer-local-map [(shift right)]
  13010. (lambda () (interactive)
  13011. (org-eval-in-calendar '(calendar-forward-day 1))))
  13012. (org-defkey minibuffer-local-map ">"
  13013. (lambda () (interactive)
  13014. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13015. (org-defkey minibuffer-local-map "<"
  13016. (lambda () (interactive)
  13017. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13018. (org-defkey minibuffer-local-map "\C-v"
  13019. (lambda () (interactive)
  13020. (org-eval-in-calendar
  13021. '(calendar-scroll-left-three-months 1))))
  13022. (org-defkey minibuffer-local-map "\M-v"
  13023. (lambda () (interactive)
  13024. (org-eval-in-calendar
  13025. '(calendar-scroll-right-three-months 1))))
  13026. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13027. (unwind-protect
  13028. (progn
  13029. (use-local-map map)
  13030. (add-hook 'post-command-hook 'org-read-date-display)
  13031. (setq org-ans0 (read-string prompt default-input
  13032. 'org-read-date-history nil))
  13033. ;; org-ans0: from prompt
  13034. ;; org-ans1: from mouse click
  13035. ;; org-ans2: from calendar motion
  13036. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13037. (remove-hook 'post-command-hook 'org-read-date-display)
  13038. (use-local-map old-map)
  13039. (when org-read-date-overlay
  13040. (delete-overlay org-read-date-overlay)
  13041. (setq org-read-date-overlay nil)))))))
  13042. (t ; Naked prompt only
  13043. (unwind-protect
  13044. (setq ans (read-string prompt default-input
  13045. 'org-read-date-history timestr))
  13046. (when org-read-date-overlay
  13047. (delete-overlay org-read-date-overlay)
  13048. (setq org-read-date-overlay nil)))))
  13049. (setq final (org-read-date-analyze ans def defdecode))
  13050. ;; One round trip to get rid of 34th of August and stuff like that....
  13051. (setq final (decode-time (apply 'encode-time final)))
  13052. (setq org-read-date-final-answer ans)
  13053. (if to-time
  13054. (apply 'encode-time final)
  13055. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13056. (format "%04d-%02d-%02d %02d:%02d"
  13057. (nth 5 final) (nth 4 final) (nth 3 final)
  13058. (nth 2 final) (nth 1 final))
  13059. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13060. (defvar def)
  13061. (defvar defdecode)
  13062. (defvar with-time)
  13063. (defvar org-read-date-analyze-futurep nil)
  13064. (defun org-read-date-display ()
  13065. "Display the current date prompt interpretation in the minibuffer."
  13066. (when org-read-date-display-live
  13067. (when org-read-date-overlay
  13068. (delete-overlay org-read-date-overlay))
  13069. (let ((p (point)))
  13070. (end-of-line 1)
  13071. (while (not (equal (buffer-substring
  13072. (max (point-min) (- (point) 4)) (point))
  13073. " "))
  13074. (insert " "))
  13075. (goto-char p))
  13076. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13077. " " (or org-ans1 org-ans2)))
  13078. (org-end-time-was-given nil)
  13079. (f (org-read-date-analyze ans def defdecode))
  13080. (fmts (if org-dcst
  13081. org-time-stamp-custom-formats
  13082. org-time-stamp-formats))
  13083. (fmt (if (or with-time
  13084. (and (boundp 'org-time-was-given) org-time-was-given))
  13085. (cdr fmts)
  13086. (car fmts)))
  13087. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13088. (when (and org-end-time-was-given
  13089. (string-match org-plain-time-of-day-regexp txt))
  13090. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13091. org-end-time-was-given
  13092. (substring txt (match-end 0)))))
  13093. (when org-read-date-analyze-futurep
  13094. (setq txt (concat txt " (=>F)")))
  13095. (setq org-read-date-overlay
  13096. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13097. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13098. (defun org-read-date-analyze (ans def defdecode)
  13099. "Analyze the combined answer of the date prompt."
  13100. ;; FIXME: cleanup and comment
  13101. (let ((nowdecode (decode-time (current-time)))
  13102. delta deltan deltaw deltadef year month day
  13103. hour minute second wday pm h2 m2 tl wday1
  13104. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13105. (setq org-read-date-analyze-futurep nil)
  13106. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13107. (setq ans "+0"))
  13108. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13109. (setq ans (replace-match "" t t ans)
  13110. deltan (car delta)
  13111. deltaw (nth 1 delta)
  13112. deltadef (nth 2 delta)))
  13113. ;; Check if there is an iso week date in there
  13114. ;; If yes, store the info and postpone interpreting it until the rest
  13115. ;; of the parsing is done
  13116. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13117. (setq iso-year (if (match-end 1)
  13118. (org-small-year-to-year
  13119. (string-to-number (match-string 1 ans))))
  13120. iso-weekday (if (match-end 3)
  13121. (string-to-number (match-string 3 ans)))
  13122. iso-week (string-to-number (match-string 2 ans)))
  13123. (setq ans (replace-match "" t t ans)))
  13124. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13125. (when (string-match
  13126. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13127. (setq year (if (match-end 2)
  13128. (string-to-number (match-string 2 ans))
  13129. (progn (setq kill-year t)
  13130. (string-to-number (format-time-string "%Y"))))
  13131. month (string-to-number (match-string 3 ans))
  13132. day (string-to-number (match-string 4 ans)))
  13133. (if (< year 100) (setq year (+ 2000 year)))
  13134. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13135. t nil ans)))
  13136. ;; Help matching american dates, like 5/30 or 5/30/7
  13137. (when (string-match
  13138. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13139. (setq year (if (match-end 4)
  13140. (string-to-number (match-string 4 ans))
  13141. (progn (setq kill-year t)
  13142. (string-to-number (format-time-string "%Y"))))
  13143. month (string-to-number (match-string 1 ans))
  13144. day (string-to-number (match-string 2 ans)))
  13145. (if (< year 100) (setq year (+ 2000 year)))
  13146. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13147. t nil ans)))
  13148. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13149. ;; If there is a time with am/pm, and *no* time without it, we convert
  13150. ;; so that matching will be successful.
  13151. (loop for i from 1 to 2 do ; twice, for end time as well
  13152. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13153. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13154. (setq hour (string-to-number (match-string 1 ans))
  13155. minute (if (match-end 3)
  13156. (string-to-number (match-string 3 ans))
  13157. 0)
  13158. pm (equal ?p
  13159. (string-to-char (downcase (match-string 4 ans)))))
  13160. (if (and (= hour 12) (not pm))
  13161. (setq hour 0)
  13162. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13163. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13164. t t ans))))
  13165. ;; Check if a time range is given as a duration
  13166. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13167. (setq hour (string-to-number (match-string 1 ans))
  13168. h2 (+ hour (string-to-number (match-string 3 ans)))
  13169. minute (string-to-number (match-string 2 ans))
  13170. m2 (+ minute (if (match-end 5) (string-to-number
  13171. (match-string 5 ans))0)))
  13172. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13173. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13174. t t ans)))
  13175. ;; Check if there is a time range
  13176. (when (boundp 'org-end-time-was-given)
  13177. (setq org-time-was-given nil)
  13178. (when (and (string-match org-plain-time-of-day-regexp ans)
  13179. (match-end 8))
  13180. (setq org-end-time-was-given (match-string 8 ans))
  13181. (setq ans (concat (substring ans 0 (match-beginning 7))
  13182. (substring ans (match-end 7))))))
  13183. (setq tl (parse-time-string ans)
  13184. day (or (nth 3 tl) (nth 3 defdecode))
  13185. month (or (nth 4 tl)
  13186. (if (and org-read-date-prefer-future
  13187. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13188. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13189. (nth 4 defdecode)))
  13190. year (or (and (not kill-year) (nth 5 tl))
  13191. (if (and org-read-date-prefer-future
  13192. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13193. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13194. (nth 5 defdecode)))
  13195. hour (or (nth 2 tl) (nth 2 defdecode))
  13196. minute (or (nth 1 tl) (nth 1 defdecode))
  13197. second (or (nth 0 tl) 0)
  13198. wday (nth 6 tl))
  13199. (when (and (eq org-read-date-prefer-future 'time)
  13200. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13201. (equal day (nth 3 nowdecode))
  13202. (equal month (nth 4 nowdecode))
  13203. (equal year (nth 5 nowdecode))
  13204. (nth 2 tl)
  13205. (or (< (nth 2 tl) (nth 2 nowdecode))
  13206. (and (= (nth 2 tl) (nth 2 nowdecode))
  13207. (nth 1 tl)
  13208. (< (nth 1 tl) (nth 1 nowdecode)))))
  13209. (setq day (1+ day)
  13210. futurep t))
  13211. ;; Special date definitions below
  13212. (cond
  13213. (iso-week
  13214. ;; There was an iso week
  13215. (require 'cal-iso)
  13216. (setq futurep nil)
  13217. (setq year (or iso-year year)
  13218. day (or iso-weekday wday 1)
  13219. wday nil ; to make sure that the trigger below does not match
  13220. iso-date (calendar-gregorian-from-absolute
  13221. (calendar-absolute-from-iso
  13222. (list iso-week day year))))
  13223. ; FIXME: Should we also push ISO weeks into the future?
  13224. ; (when (and org-read-date-prefer-future
  13225. ; (not iso-year)
  13226. ; (< (calendar-absolute-from-gregorian iso-date)
  13227. ; (time-to-days (current-time))))
  13228. ; (setq year (1+ year)
  13229. ; iso-date (calendar-gregorian-from-absolute
  13230. ; (calendar-absolute-from-iso
  13231. ; (list iso-week day year)))))
  13232. (setq month (car iso-date)
  13233. year (nth 2 iso-date)
  13234. day (nth 1 iso-date)))
  13235. (deltan
  13236. (setq futurep nil)
  13237. (unless deltadef
  13238. (let ((now (decode-time (current-time))))
  13239. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13240. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13241. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13242. ((equal deltaw "m") (setq month (+ month deltan)))
  13243. ((equal deltaw "y") (setq year (+ year deltan)))))
  13244. ((and wday (not (nth 3 tl)))
  13245. (setq futurep nil)
  13246. ;; Weekday was given, but no day, so pick that day in the week
  13247. ;; on or after the derived date.
  13248. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13249. (unless (equal wday wday1)
  13250. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13251. (if (and (boundp 'org-time-was-given)
  13252. (nth 2 tl))
  13253. (setq org-time-was-given t))
  13254. (if (< year 100) (setq year (+ 2000 year)))
  13255. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  13256. (setq org-read-date-analyze-futurep futurep)
  13257. (list second minute hour day month year)))
  13258. (defvar parse-time-weekdays)
  13259. (defun org-read-date-get-relative (s today default)
  13260. "Check string S for special relative date string.
  13261. TODAY and DEFAULT are internal times, for today and for a default.
  13262. Return shift list (N what def-flag)
  13263. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13264. N is the number of WHATs to shift.
  13265. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13266. the DEFAULT date rather than TODAY."
  13267. (when (and
  13268. (string-match
  13269. (concat
  13270. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13271. "\\([0-9]+\\)?"
  13272. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13273. "\\([ \t]\\|$\\)") s)
  13274. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13275. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13276. (string-to-char (substring (match-string 1 s) -1))
  13277. ?+))
  13278. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13279. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13280. (what (if (match-end 3) (match-string 3 s) "d"))
  13281. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13282. (date (if rel default today))
  13283. (wday (nth 6 (decode-time date)))
  13284. delta)
  13285. (if wday1
  13286. (progn
  13287. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13288. (if (= dir ?-) (setq delta (- delta 7)))
  13289. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13290. (list delta "d" rel))
  13291. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13292. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13293. "Turn a user-specified date into the internal representation.
  13294. The internal representation needed by the calendar is (month day year).
  13295. This is a wrapper to handle the brain-dead convention in calendar that
  13296. user function argument order change dependent on argument order."
  13297. (if (boundp 'calendar-date-style)
  13298. (cond
  13299. ((eq calendar-date-style 'american)
  13300. (list arg1 arg2 arg3))
  13301. ((eq calendar-date-style 'european)
  13302. (list arg2 arg1 arg3))
  13303. ((eq calendar-date-style 'iso)
  13304. (list arg2 arg3 arg1)))
  13305. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13306. (if (org-bound-and-true-p european-calendar-style)
  13307. (list arg2 arg1 arg3)
  13308. (list arg1 arg2 arg3)))))
  13309. (defun org-eval-in-calendar (form &optional keepdate)
  13310. "Eval FORM in the calendar window and return to current window.
  13311. Also, store the cursor date in variable org-ans2."
  13312. (let ((sf (selected-frame))
  13313. (sw (selected-window)))
  13314. (select-window (get-buffer-window "*Calendar*" t))
  13315. (eval form)
  13316. (when (and (not keepdate) (calendar-cursor-to-date))
  13317. (let* ((date (calendar-cursor-to-date))
  13318. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13319. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13320. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13321. (select-window sw)
  13322. (org-select-frame-set-input-focus sf)))
  13323. (defun org-calendar-select ()
  13324. "Return to `org-read-date' with the date currently selected.
  13325. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13326. (interactive)
  13327. (when (calendar-cursor-to-date)
  13328. (let* ((date (calendar-cursor-to-date))
  13329. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13330. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13331. (if (active-minibuffer-window) (exit-minibuffer))))
  13332. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13333. "Insert a date stamp for the date given by the internal TIME.
  13334. WITH-HM means use the stamp format that includes the time of the day.
  13335. INACTIVE means use square brackets instead of angular ones, so that the
  13336. stamp will not contribute to the agenda.
  13337. PRE and POST are optional strings to be inserted before and after the
  13338. stamp.
  13339. The command returns the inserted time stamp."
  13340. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13341. stamp)
  13342. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13343. (insert-before-markers (or pre ""))
  13344. (when (listp extra)
  13345. (setq extra (car extra))
  13346. (if (and (stringp extra)
  13347. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13348. (setq extra (format "-%02d:%02d"
  13349. (string-to-number (match-string 1 extra))
  13350. (string-to-number (match-string 2 extra))))
  13351. (setq extra nil)))
  13352. (when extra
  13353. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13354. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13355. (insert-before-markers (or post ""))
  13356. (setq org-last-inserted-timestamp stamp)))
  13357. (defun org-toggle-time-stamp-overlays ()
  13358. "Toggle the use of custom time stamp formats."
  13359. (interactive)
  13360. (setq org-display-custom-times (not org-display-custom-times))
  13361. (unless org-display-custom-times
  13362. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13363. (while (setq p (next-single-property-change p 'display))
  13364. (if (and (get-text-property p 'display)
  13365. (eq (get-text-property p 'face) 'org-date))
  13366. (remove-text-properties
  13367. p (setq p (next-single-property-change p 'display))
  13368. '(display t))))
  13369. (set-buffer-modified-p bmp)))
  13370. (if (featurep 'xemacs)
  13371. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13372. (org-restart-font-lock)
  13373. (setq org-table-may-need-update t)
  13374. (if org-display-custom-times
  13375. (message "Time stamps are overlayed with custom format")
  13376. (message "Time stamp overlays removed")))
  13377. (defun org-display-custom-time (beg end)
  13378. "Overlay modified time stamp format over timestamp between BEG and END."
  13379. (let* ((ts (buffer-substring beg end))
  13380. t1 w1 with-hm tf time str w2 (off 0))
  13381. (save-match-data
  13382. (setq t1 (org-parse-time-string ts t))
  13383. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13384. (setq off (- (match-end 0) (match-beginning 0)))))
  13385. (setq end (- end off))
  13386. (setq w1 (- end beg)
  13387. with-hm (and (nth 1 t1) (nth 2 t1))
  13388. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13389. time (org-fix-decoded-time t1)
  13390. str (org-add-props
  13391. (format-time-string
  13392. (substring tf 1 -1) (apply 'encode-time time))
  13393. nil 'mouse-face 'highlight)
  13394. w2 (length str))
  13395. (if (not (= w2 w1))
  13396. (add-text-properties (1+ beg) (+ 2 beg)
  13397. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13398. (if (featurep 'xemacs)
  13399. (progn
  13400. (put-text-property beg end 'invisible t)
  13401. (put-text-property beg end 'end-glyph (make-glyph str)))
  13402. (put-text-property beg end 'display str))))
  13403. (defun org-translate-time (string)
  13404. "Translate all timestamps in STRING to custom format.
  13405. But do this only if the variable `org-display-custom-times' is set."
  13406. (when org-display-custom-times
  13407. (save-match-data
  13408. (let* ((start 0)
  13409. (re org-ts-regexp-both)
  13410. t1 with-hm inactive tf time str beg end)
  13411. (while (setq start (string-match re string start))
  13412. (setq beg (match-beginning 0)
  13413. end (match-end 0)
  13414. t1 (save-match-data
  13415. (org-parse-time-string (substring string beg end) t))
  13416. with-hm (and (nth 1 t1) (nth 2 t1))
  13417. inactive (equal (substring string beg (1+ beg)) "[")
  13418. tf (funcall (if with-hm 'cdr 'car)
  13419. org-time-stamp-custom-formats)
  13420. time (org-fix-decoded-time t1)
  13421. str (format-time-string
  13422. (concat
  13423. (if inactive "[" "<") (substring tf 1 -1)
  13424. (if inactive "]" ">"))
  13425. (apply 'encode-time time))
  13426. string (replace-match str t t string)
  13427. start (+ start (length str)))))))
  13428. string)
  13429. (defun org-fix-decoded-time (time)
  13430. "Set 0 instead of nil for the first 6 elements of time.
  13431. Don't touch the rest."
  13432. (let ((n 0))
  13433. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13434. (defun org-days-to-time (timestamp-string)
  13435. "Difference between TIMESTAMP-STRING and now in days."
  13436. (- (time-to-days (org-time-string-to-time timestamp-string))
  13437. (time-to-days (current-time))))
  13438. (defun org-deadline-close (timestamp-string &optional ndays)
  13439. "Is the time in TIMESTAMP-STRING close to the current date?"
  13440. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13441. (and (< (org-days-to-time timestamp-string) ndays)
  13442. (not (org-entry-is-done-p))))
  13443. (defun org-get-wdays (ts)
  13444. "Get the deadline lead time appropriate for timestring TS."
  13445. (cond
  13446. ((<= org-deadline-warning-days 0)
  13447. ;; 0 or negative, enforce this value no matter what
  13448. (- org-deadline-warning-days))
  13449. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13450. ;; lead time is specified.
  13451. (floor (* (string-to-number (match-string 1 ts))
  13452. (cdr (assoc (match-string 2 ts)
  13453. '(("d" . 1) ("w" . 7)
  13454. ("m" . 30.4) ("y" . 365.25)))))))
  13455. ;; go for the default.
  13456. (t org-deadline-warning-days)))
  13457. (defun org-calendar-select-mouse (ev)
  13458. "Return to `org-read-date' with the date currently selected.
  13459. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13460. (interactive "e")
  13461. (mouse-set-point ev)
  13462. (when (calendar-cursor-to-date)
  13463. (let* ((date (calendar-cursor-to-date))
  13464. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13465. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13466. (if (active-minibuffer-window) (exit-minibuffer))))
  13467. (defun org-check-deadlines (ndays)
  13468. "Check if there are any deadlines due or past due.
  13469. A deadline is considered due if it happens within `org-deadline-warning-days'
  13470. days from today's date. If the deadline appears in an entry marked DONE,
  13471. it is not shown. The prefix arg NDAYS can be used to test that many
  13472. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13473. (interactive "P")
  13474. (let* ((org-warn-days
  13475. (cond
  13476. ((equal ndays '(4)) 100000)
  13477. (ndays (prefix-numeric-value ndays))
  13478. (t (abs org-deadline-warning-days))))
  13479. (case-fold-search nil)
  13480. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13481. (callback
  13482. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13483. (message "%d deadlines past-due or due within %d days"
  13484. (org-occur regexp nil callback)
  13485. org-warn-days)))
  13486. (defun org-check-before-date (date)
  13487. "Check if there are deadlines or scheduled entries before DATE."
  13488. (interactive (list (org-read-date)))
  13489. (let ((case-fold-search nil)
  13490. (regexp (concat "\\<\\(" org-deadline-string
  13491. "\\|" org-scheduled-string
  13492. "\\) *<\\([^>]+\\)>"))
  13493. (callback
  13494. (lambda () (time-less-p
  13495. (org-time-string-to-time (match-string 2))
  13496. (org-time-string-to-time date)))))
  13497. (message "%d entries before %s"
  13498. (org-occur regexp nil callback) date)))
  13499. (defun org-check-after-date (date)
  13500. "Check if there are deadlines or scheduled entries after DATE."
  13501. (interactive (list (org-read-date)))
  13502. (let ((case-fold-search nil)
  13503. (regexp (concat "\\<\\(" org-deadline-string
  13504. "\\|" org-scheduled-string
  13505. "\\) *<\\([^>]+\\)>"))
  13506. (callback
  13507. (lambda () (not
  13508. (time-less-p
  13509. (org-time-string-to-time (match-string 2))
  13510. (org-time-string-to-time date))))))
  13511. (message "%d entries after %s"
  13512. (org-occur regexp nil callback) date)))
  13513. (defun org-evaluate-time-range (&optional to-buffer)
  13514. "Evaluate a time range by computing the difference between start and end.
  13515. Normally the result is just printed in the echo area, but with prefix arg
  13516. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13517. If the time range is actually in a table, the result is inserted into the
  13518. next column.
  13519. For time difference computation, a year is assumed to be exactly 365
  13520. days in order to avoid rounding problems."
  13521. (interactive "P")
  13522. (or
  13523. (org-clock-update-time-maybe)
  13524. (save-excursion
  13525. (unless (org-at-date-range-p t)
  13526. (goto-char (point-at-bol))
  13527. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13528. (if (not (org-at-date-range-p t))
  13529. (error "Not at a time-stamp range, and none found in current line")))
  13530. (let* ((ts1 (match-string 1))
  13531. (ts2 (match-string 2))
  13532. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13533. (match-end (match-end 0))
  13534. (time1 (org-time-string-to-time ts1))
  13535. (time2 (org-time-string-to-time ts2))
  13536. (t1 (org-float-time time1))
  13537. (t2 (org-float-time time2))
  13538. (diff (abs (- t2 t1)))
  13539. (negative (< (- t2 t1) 0))
  13540. ;; (ys (floor (* 365 24 60 60)))
  13541. (ds (* 24 60 60))
  13542. (hs (* 60 60))
  13543. (fy "%dy %dd %02d:%02d")
  13544. (fy1 "%dy %dd")
  13545. (fd "%dd %02d:%02d")
  13546. (fd1 "%dd")
  13547. (fh "%02d:%02d")
  13548. y d h m align)
  13549. (if havetime
  13550. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13551. y 0
  13552. d (floor (/ diff ds)) diff (mod diff ds)
  13553. h (floor (/ diff hs)) diff (mod diff hs)
  13554. m (floor (/ diff 60)))
  13555. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13556. y 0
  13557. d (floor (+ (/ diff ds) 0.5))
  13558. h 0 m 0))
  13559. (if (not to-buffer)
  13560. (message "%s" (org-make-tdiff-string y d h m))
  13561. (if (org-at-table-p)
  13562. (progn
  13563. (goto-char match-end)
  13564. (setq align t)
  13565. (and (looking-at " *|") (goto-char (match-end 0))))
  13566. (goto-char match-end))
  13567. (if (looking-at
  13568. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13569. (replace-match ""))
  13570. (if negative (insert " -"))
  13571. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13572. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13573. (insert " " (format fh h m))))
  13574. (if align (org-table-align))
  13575. (message "Time difference inserted")))))
  13576. (defun org-make-tdiff-string (y d h m)
  13577. (let ((fmt "")
  13578. (l nil))
  13579. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13580. l (push y l)))
  13581. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13582. l (push d l)))
  13583. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13584. l (push h l)))
  13585. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13586. l (push m l)))
  13587. (apply 'format fmt (nreverse l))))
  13588. (defun org-time-string-to-time (s)
  13589. (apply 'encode-time (org-parse-time-string s)))
  13590. (defun org-time-string-to-seconds (s)
  13591. (org-float-time (org-time-string-to-time s)))
  13592. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13593. "Convert a time stamp to an absolute day number.
  13594. If there is a specifier for a cyclic time stamp, get the closest date to
  13595. DAYNR.
  13596. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13597. the variable date is bound by the calendar when this is called."
  13598. (cond
  13599. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13600. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13601. daynr
  13602. (+ daynr 1000)))
  13603. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13604. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13605. (time-to-days (current-time))) (match-string 0 s)
  13606. prefer show-all))
  13607. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13608. (defun org-days-to-iso-week (days)
  13609. "Return the iso week number."
  13610. (require 'cal-iso)
  13611. (car (calendar-iso-from-absolute days)))
  13612. (defun org-small-year-to-year (year)
  13613. "Convert 2-digit years into 4-digit years.
  13614. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13615. The year 2000 cannot be abbreviated. Any year larger than 99
  13616. is returned unchanged."
  13617. (if (< year 38)
  13618. (setq year (+ 2000 year))
  13619. (if (< year 100)
  13620. (setq year (+ 1900 year))))
  13621. year)
  13622. (defun org-time-from-absolute (d)
  13623. "Return the time corresponding to date D.
  13624. D may be an absolute day number, or a calendar-type list (month day year)."
  13625. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13626. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13627. (defun org-calendar-holiday ()
  13628. "List of holidays, for Diary display in Org-mode."
  13629. (require 'holidays)
  13630. (let ((hl (funcall
  13631. (if (fboundp 'calendar-check-holidays)
  13632. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13633. (if hl (mapconcat 'identity hl "; "))))
  13634. (defun org-diary-sexp-entry (sexp entry date)
  13635. "Process a SEXP diary ENTRY for DATE."
  13636. (require 'diary-lib)
  13637. (let ((result (if calendar-debug-sexp
  13638. (let ((stack-trace-on-error t))
  13639. (eval (car (read-from-string sexp))))
  13640. (condition-case nil
  13641. (eval (car (read-from-string sexp)))
  13642. (error
  13643. (beep)
  13644. (message "Bad sexp at line %d in %s: %s"
  13645. (org-current-line)
  13646. (buffer-file-name) sexp)
  13647. (sleep-for 2))))))
  13648. (cond ((stringp result) (split-string result "; "))
  13649. ((and (consp result)
  13650. (not (consp (cdr result)))
  13651. (stringp (cdr result))) (cdr result))
  13652. ((and (consp result)
  13653. (stringp (car result))) result)
  13654. (result entry)
  13655. (t nil))))
  13656. (defun org-diary-to-ical-string (frombuf)
  13657. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13658. This uses the icalendar.el library."
  13659. (let* ((tmpdir (if (featurep 'xemacs)
  13660. (temp-directory)
  13661. temporary-file-directory))
  13662. (tmpfile (make-temp-name
  13663. (expand-file-name "orgics" tmpdir)))
  13664. buf rtn b e)
  13665. (with-current-buffer frombuf
  13666. (icalendar-export-region (point-min) (point-max) tmpfile)
  13667. (setq buf (find-buffer-visiting tmpfile))
  13668. (set-buffer buf)
  13669. (goto-char (point-min))
  13670. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13671. (setq b (match-beginning 0)))
  13672. (goto-char (point-max))
  13673. (if (re-search-backward "^END:VEVENT" nil t)
  13674. (setq e (match-end 0)))
  13675. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13676. (kill-buffer buf)
  13677. (delete-file tmpfile)
  13678. rtn))
  13679. (defun org-closest-date (start current change prefer show-all)
  13680. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13681. When PREFER is `past' return a date that is either CURRENT or past.
  13682. When PREFER is `future', return a date that is either CURRENT or future.
  13683. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13684. ;; Make the proper lists from the dates
  13685. (catch 'exit
  13686. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13687. dn dw sday cday n1 n2 n0
  13688. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13689. (setq start (org-date-to-gregorian start)
  13690. current (org-date-to-gregorian
  13691. (if show-all
  13692. current
  13693. (time-to-days (current-time))))
  13694. sday (calendar-absolute-from-gregorian start)
  13695. cday (calendar-absolute-from-gregorian current))
  13696. (if (<= cday sday) (throw 'exit sday))
  13697. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13698. (setq dn (string-to-number (match-string 1 change))
  13699. dw (cdr (assoc (match-string 2 change) a1)))
  13700. (error "Invalid change specifier: %s" change))
  13701. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13702. (cond
  13703. ((eq dw 'day)
  13704. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13705. n2 (+ n1 dn)))
  13706. ((eq dw 'year)
  13707. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13708. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13709. (setq date1 (list m d y1)
  13710. n1 (calendar-absolute-from-gregorian date1)
  13711. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13712. n2 (calendar-absolute-from-gregorian date2)))
  13713. ((eq dw 'month)
  13714. ;; approx number of month between the two dates
  13715. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13716. ;; How often does dn fit in there?
  13717. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13718. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13719. m (+ m nm)
  13720. ny (floor (/ m 12))
  13721. y (+ y ny)
  13722. m (- m (* ny 12)))
  13723. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13724. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13725. (setq m2 (+ m dn) y2 y)
  13726. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13727. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13728. (while (<= n2 cday)
  13729. (setq n1 n2 m m2 y y2)
  13730. (setq m2 (+ m dn) y2 y)
  13731. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13732. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13733. ;; Make sure n1 is the earlier date
  13734. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13735. (if show-all
  13736. (cond
  13737. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13738. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13739. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13740. (cond
  13741. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13742. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13743. (t (if (= cday n1) n1 n2)))))))
  13744. (defun org-date-to-gregorian (date)
  13745. "Turn any specification of DATE into a Gregorian date for the calendar."
  13746. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13747. ((and (listp date) (= (length date) 3)) date)
  13748. ((stringp date)
  13749. (setq date (org-parse-time-string date))
  13750. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13751. ((listp date)
  13752. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13753. (defun org-parse-time-string (s &optional nodefault)
  13754. "Parse the standard Org-mode time string.
  13755. This should be a lot faster than the normal `parse-time-string'.
  13756. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13757. hour and minute fields will be nil if not given."
  13758. (if (string-match org-ts-regexp0 s)
  13759. (list 0
  13760. (if (or (match-beginning 8) (not nodefault))
  13761. (string-to-number (or (match-string 8 s) "0")))
  13762. (if (or (match-beginning 7) (not nodefault))
  13763. (string-to-number (or (match-string 7 s) "0")))
  13764. (string-to-number (match-string 4 s))
  13765. (string-to-number (match-string 3 s))
  13766. (string-to-number (match-string 2 s))
  13767. nil nil nil)
  13768. (error "Not a standard Org-mode time string: %s" s)))
  13769. (defun org-timestamp-up (&optional arg)
  13770. "Increase the date item at the cursor by one.
  13771. If the cursor is on the year, change the year. If it is on the month or
  13772. the day, change that.
  13773. With prefix ARG, change by that many units."
  13774. (interactive "p")
  13775. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  13776. (defun org-timestamp-down (&optional arg)
  13777. "Decrease the date item at the cursor by one.
  13778. If the cursor is on the year, change the year. If it is on the month or
  13779. the day, change that.
  13780. With prefix ARG, change by that many units."
  13781. (interactive "p")
  13782. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  13783. (defun org-timestamp-up-day (&optional arg)
  13784. "Increase the date in the time stamp by one day.
  13785. With prefix ARG, change that many days."
  13786. (interactive "p")
  13787. (if (and (not (org-at-timestamp-p t))
  13788. (org-on-heading-p))
  13789. (org-todo 'up)
  13790. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  13791. (defun org-timestamp-down-day (&optional arg)
  13792. "Decrease the date in the time stamp by one day.
  13793. With prefix ARG, change that many days."
  13794. (interactive "p")
  13795. (if (and (not (org-at-timestamp-p t))
  13796. (org-on-heading-p))
  13797. (org-todo 'down)
  13798. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  13799. (defun org-at-timestamp-p (&optional inactive-ok)
  13800. "Determine if the cursor is in or at a timestamp."
  13801. (interactive)
  13802. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13803. (pos (point))
  13804. (ans (or (looking-at tsr)
  13805. (save-excursion
  13806. (skip-chars-backward "^[<\n\r\t")
  13807. (if (> (point) (point-min)) (backward-char 1))
  13808. (and (looking-at tsr)
  13809. (> (- (match-end 0) pos) -1))))))
  13810. (and ans
  13811. (boundp 'org-ts-what)
  13812. (setq org-ts-what
  13813. (cond
  13814. ((= pos (match-beginning 0)) 'bracket)
  13815. ((= pos (1- (match-end 0))) 'bracket)
  13816. ((org-pos-in-match-range pos 2) 'year)
  13817. ((org-pos-in-match-range pos 3) 'month)
  13818. ((org-pos-in-match-range pos 7) 'hour)
  13819. ((org-pos-in-match-range pos 8) 'minute)
  13820. ((or (org-pos-in-match-range pos 4)
  13821. (org-pos-in-match-range pos 5)) 'day)
  13822. ((and (> pos (or (match-end 8) (match-end 5)))
  13823. (< pos (match-end 0)))
  13824. (- pos (or (match-end 8) (match-end 5))))
  13825. (t 'day))))
  13826. ans))
  13827. (defun org-toggle-timestamp-type ()
  13828. "Toggle the type (<active> or [inactive]) of a time stamp."
  13829. (interactive)
  13830. (when (org-at-timestamp-p t)
  13831. (let ((beg (match-beginning 0)) (end (match-end 0))
  13832. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13833. (save-excursion
  13834. (goto-char beg)
  13835. (while (re-search-forward "[][<>]" end t)
  13836. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13837. t t)))
  13838. (message "Timestamp is now %sactive"
  13839. (if (equal (char-after beg) ?<) "" "in")))))
  13840. (defun org-timestamp-change (n &optional what updown)
  13841. "Change the date in the time stamp at point.
  13842. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13843. `year', `minute', `second'. If WHAT is not given, the cursor position
  13844. in the timestamp determines what will be changed."
  13845. (let ((pos (point))
  13846. with-hm inactive
  13847. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13848. org-ts-what
  13849. extra rem
  13850. ts time time0)
  13851. (if (not (org-at-timestamp-p t))
  13852. (error "Not at a timestamp"))
  13853. (if (and (not what) (eq org-ts-what 'bracket))
  13854. (org-toggle-timestamp-type)
  13855. (if (and (not what) (not (eq org-ts-what 'day))
  13856. org-display-custom-times
  13857. (get-text-property (point) 'display)
  13858. (not (get-text-property (1- (point)) 'display)))
  13859. (setq org-ts-what 'day))
  13860. (setq org-ts-what (or what org-ts-what)
  13861. inactive (= (char-after (match-beginning 0)) ?\[)
  13862. ts (match-string 0))
  13863. (replace-match "")
  13864. (if (string-match
  13865. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13866. ts)
  13867. (setq extra (match-string 1 ts)))
  13868. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13869. (setq with-hm t))
  13870. (setq time0 (org-parse-time-string ts))
  13871. (when (and updown
  13872. (eq org-ts-what 'minute)
  13873. (not current-prefix-arg))
  13874. ;; This looks like s-up and s-down. Change by one rounding step.
  13875. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13876. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13877. (setcar (cdr time0) (+ (nth 1 time0)
  13878. (if (> n 0) (- rem) (- dm rem))))))
  13879. (setq time
  13880. (encode-time (or (car time0) 0)
  13881. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13882. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13883. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13884. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13885. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13886. (nthcdr 6 time0)))
  13887. (when (and (member org-ts-what '(hour minute))
  13888. extra
  13889. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13890. (setq extra (org-modify-ts-extra
  13891. extra
  13892. (if (eq org-ts-what 'hour) 2 5)
  13893. n dm)))
  13894. (when (integerp org-ts-what)
  13895. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13896. (if (eq what 'calendar)
  13897. (let ((cal-date (org-get-date-from-calendar)))
  13898. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13899. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13900. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13901. (setcar time0 (or (car time0) 0))
  13902. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13903. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13904. (setq time (apply 'encode-time time0))))
  13905. (setq org-last-changed-timestamp
  13906. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13907. (org-clock-update-time-maybe)
  13908. (goto-char pos)
  13909. ;; Try to recenter the calendar window, if any
  13910. (if (and org-calendar-follow-timestamp-change
  13911. (get-buffer-window "*Calendar*" t)
  13912. (memq org-ts-what '(day month year)))
  13913. (org-recenter-calendar (time-to-days time))))))
  13914. (defun org-modify-ts-extra (s pos n dm)
  13915. "Change the different parts of the lead-time and repeat fields in timestamp."
  13916. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13917. ng h m new rem)
  13918. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13919. (cond
  13920. ((or (org-pos-in-match-range pos 2)
  13921. (org-pos-in-match-range pos 3))
  13922. (setq m (string-to-number (match-string 3 s))
  13923. h (string-to-number (match-string 2 s)))
  13924. (if (org-pos-in-match-range pos 2)
  13925. (setq h (+ h n))
  13926. (setq n (* dm (org-no-warnings (signum n))))
  13927. (when (not (= 0 (setq rem (% m dm))))
  13928. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13929. (setq m (+ m n)))
  13930. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13931. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13932. (setq h (min 24 (max 0 h)))
  13933. (setq ng 1 new (format "-%02d:%02d" h m)))
  13934. ((org-pos-in-match-range pos 6)
  13935. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13936. ((org-pos-in-match-range pos 5)
  13937. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13938. ((org-pos-in-match-range pos 9)
  13939. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13940. ((org-pos-in-match-range pos 8)
  13941. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13942. (when ng
  13943. (setq s (concat
  13944. (substring s 0 (match-beginning ng))
  13945. new
  13946. (substring s (match-end ng))))))
  13947. s))
  13948. (defun org-recenter-calendar (date)
  13949. "If the calendar is visible, recenter it to DATE."
  13950. (let* ((win (selected-window))
  13951. (cwin (get-buffer-window "*Calendar*" t))
  13952. (calendar-move-hook nil))
  13953. (when cwin
  13954. (select-window cwin)
  13955. (calendar-goto-date (if (listp date) date
  13956. (calendar-gregorian-from-absolute date)))
  13957. (select-window win))))
  13958. (defun org-goto-calendar (&optional arg)
  13959. "Go to the Emacs calendar at the current date.
  13960. If there is a time stamp in the current line, go to that date.
  13961. A prefix ARG can be used to force the current date."
  13962. (interactive "P")
  13963. (let ((tsr org-ts-regexp) diff
  13964. (calendar-move-hook nil)
  13965. (calendar-view-holidays-initially-flag nil)
  13966. (calendar-view-diary-initially-flag nil))
  13967. (if (or (org-at-timestamp-p)
  13968. (save-excursion
  13969. (beginning-of-line 1)
  13970. (looking-at (concat ".*" tsr))))
  13971. (let ((d1 (time-to-days (current-time)))
  13972. (d2 (time-to-days
  13973. (org-time-string-to-time (match-string 1)))))
  13974. (setq diff (- d2 d1))))
  13975. (calendar)
  13976. (calendar-goto-today)
  13977. (if (and diff (not arg)) (calendar-forward-day diff))))
  13978. (defun org-get-date-from-calendar ()
  13979. "Return a list (month day year) of date at point in calendar."
  13980. (with-current-buffer "*Calendar*"
  13981. (save-match-data
  13982. (calendar-cursor-to-date))))
  13983. (defun org-date-from-calendar ()
  13984. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13985. If there is already a time stamp at the cursor position, update it."
  13986. (interactive)
  13987. (if (org-at-timestamp-p t)
  13988. (org-timestamp-change 0 'calendar)
  13989. (let ((cal-date (org-get-date-from-calendar)))
  13990. (org-insert-time-stamp
  13991. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13992. (defun org-minutes-to-hh:mm-string (m)
  13993. "Compute H:MM from a number of minutes."
  13994. (let ((h (/ m 60)))
  13995. (setq m (- m (* 60 h)))
  13996. (format org-time-clocksum-format h m)))
  13997. (defun org-hh:mm-string-to-minutes (s)
  13998. "Convert a string H:MM to a number of minutes.
  13999. If the string is just a number, interpret it as minutes.
  14000. In fact, the first hh:mm or number in the string will be taken,
  14001. there can be extra stuff in the string.
  14002. If no number is found, the return value is 0."
  14003. (cond
  14004. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14005. (+ (* (string-to-number (match-string 1 s)) 60)
  14006. (string-to-number (match-string 2 s))))
  14007. ((string-match "\\([0-9]+\\)" s)
  14008. (string-to-number (match-string 1 s)))
  14009. (t 0)))
  14010. ;;;; Files
  14011. (defun org-save-all-org-buffers ()
  14012. "Save all Org-mode buffers without user confirmation."
  14013. (interactive)
  14014. (message "Saving all Org-mode buffers...")
  14015. (save-some-buffers t 'org-mode-p)
  14016. (when (featurep 'org-id) (org-id-locations-save))
  14017. (message "Saving all Org-mode buffers... done"))
  14018. (defun org-revert-all-org-buffers ()
  14019. "Revert all Org-mode buffers.
  14020. Prompt for confirmation when there are unsaved changes.
  14021. Be sure you know what you are doing before letting this function
  14022. overwrite your changes.
  14023. This function is useful in a setup where one tracks org files
  14024. with a version control system, to revert on one machine after pulling
  14025. changes from another. I believe the procedure must be like this:
  14026. 1. M-x org-save-all-org-buffers
  14027. 2. Pull changes from the other machine, resolve conflicts
  14028. 3. M-x org-revert-all-org-buffers"
  14029. (interactive)
  14030. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14031. (error "Abort"))
  14032. (save-excursion
  14033. (save-window-excursion
  14034. (mapc
  14035. (lambda (b)
  14036. (when (and (with-current-buffer b (org-mode-p))
  14037. (with-current-buffer b buffer-file-name))
  14038. (switch-to-buffer b)
  14039. (revert-buffer t 'no-confirm)))
  14040. (buffer-list))
  14041. (when (and (featurep 'org-id) org-id-track-globally)
  14042. (org-id-locations-load)))))
  14043. ;;;; Agenda files
  14044. ;;;###autoload
  14045. (defun org-switchb (&optional arg)
  14046. "Switch between Org buffers.
  14047. With a prefix argument, restrict available to files.
  14048. With two prefix arguments, restrict available buffers to agenda files.
  14049. Defaults to `iswitchb' for buffer name completion.
  14050. Set `org-completion-use-ido' to make it use ido instead."
  14051. (interactive "P")
  14052. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14053. ((equal arg '(16)) (org-buffer-list 'agenda))
  14054. (t (org-buffer-list))))
  14055. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14056. (org-completion-use-ido org-completion-use-ido))
  14057. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14058. (setq org-completion-use-iswitchb t))
  14059. (switch-to-buffer
  14060. (org-icompleting-read "Org buffer: "
  14061. (mapcar 'list (mapcar 'buffer-name blist))
  14062. nil t))))
  14063. ;;; Define some older names previously used for this functionality
  14064. ;;;###autoload
  14065. (defalias 'org-ido-switchb 'org-switchb)
  14066. ;;;###autoload
  14067. (defalias 'org-iswitchb 'org-switchb)
  14068. (defun org-buffer-list (&optional predicate exclude-tmp)
  14069. "Return a list of Org buffers.
  14070. PREDICATE can be `export', `files' or `agenda'.
  14071. export restrict the list to Export buffers.
  14072. files restrict the list to buffers visiting Org files.
  14073. agenda restrict the list to buffers visiting agenda files.
  14074. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14075. (let* ((bfn nil)
  14076. (agenda-files (and (eq predicate 'agenda)
  14077. (mapcar 'file-truename (org-agenda-files t))))
  14078. (filter
  14079. (cond
  14080. ((eq predicate 'files)
  14081. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14082. ((eq predicate 'export)
  14083. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14084. ((eq predicate 'agenda)
  14085. (lambda (b)
  14086. (with-current-buffer b
  14087. (and (eq major-mode 'org-mode)
  14088. (setq bfn (buffer-file-name b))
  14089. (member (file-truename bfn) agenda-files)))))
  14090. (t (lambda (b) (with-current-buffer b
  14091. (or (eq major-mode 'org-mode)
  14092. (string-match "\*Org .*Export"
  14093. (buffer-name b)))))))))
  14094. (delq nil
  14095. (mapcar
  14096. (lambda(b)
  14097. (if (and (funcall filter b)
  14098. (or (not exclude-tmp)
  14099. (not (string-match "tmp" (buffer-name b)))))
  14100. b
  14101. nil))
  14102. (buffer-list)))))
  14103. (defun org-agenda-files (&optional unrestricted archives)
  14104. "Get the list of agenda files.
  14105. Optional UNRESTRICTED means return the full list even if a restriction
  14106. is currently in place.
  14107. When ARCHIVES is t, include all archive files that are really being
  14108. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14109. `org-agenda-archives-mode' is t."
  14110. (let ((files
  14111. (cond
  14112. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14113. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14114. ((listp org-agenda-files) org-agenda-files)
  14115. (t (error "Invalid value of `org-agenda-files'")))))
  14116. (setq files (apply 'append
  14117. (mapcar (lambda (f)
  14118. (if (file-directory-p f)
  14119. (directory-files
  14120. f t org-agenda-file-regexp)
  14121. (list f)))
  14122. files)))
  14123. (when org-agenda-skip-unavailable-files
  14124. (setq files (delq nil
  14125. (mapcar (function
  14126. (lambda (file)
  14127. (and (file-readable-p file) file)))
  14128. files))))
  14129. (when (or (eq archives t)
  14130. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14131. (setq files (org-add-archive-files files)))
  14132. files))
  14133. (defun org-agenda-file-p (&optional file)
  14134. "Return non-nil, if FILE is an agenda file.
  14135. If FILE is omitted, use the file associated with the current
  14136. buffer."
  14137. (member (or file (buffer-file-name))
  14138. (org-agenda-files t)))
  14139. (defun org-edit-agenda-file-list ()
  14140. "Edit the list of agenda files.
  14141. Depending on setup, this either uses customize to edit the variable
  14142. `org-agenda-files', or it visits the file that is holding the list. In the
  14143. latter case, the buffer is set up in a way that saving it automatically kills
  14144. the buffer and restores the previous window configuration."
  14145. (interactive)
  14146. (if (stringp org-agenda-files)
  14147. (let ((cw (current-window-configuration)))
  14148. (find-file org-agenda-files)
  14149. (org-set-local 'org-window-configuration cw)
  14150. (org-add-hook 'after-save-hook
  14151. (lambda ()
  14152. (set-window-configuration
  14153. (prog1 org-window-configuration
  14154. (kill-buffer (current-buffer))))
  14155. (org-install-agenda-files-menu)
  14156. (message "New agenda file list installed"))
  14157. nil 'local)
  14158. (message "%s" (substitute-command-keys
  14159. "Edit list and finish with \\[save-buffer]")))
  14160. (customize-variable 'org-agenda-files)))
  14161. (defun org-store-new-agenda-file-list (list)
  14162. "Set new value for the agenda file list and save it correctly."
  14163. (if (stringp org-agenda-files)
  14164. (let ((fe (org-read-agenda-file-list t)) b u)
  14165. (while (setq b (find-buffer-visiting org-agenda-files))
  14166. (kill-buffer b))
  14167. (with-temp-file org-agenda-files
  14168. (insert
  14169. (mapconcat
  14170. (lambda (f) ;; Keep un-expanded entries.
  14171. (if (setq u (assoc f fe))
  14172. (cdr u)
  14173. f))
  14174. list "\n")
  14175. "\n")))
  14176. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14177. (org-insert-mode-line-in-empty-file nil))
  14178. (setq org-agenda-files list)
  14179. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14180. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14181. "Read the list of agenda files from a file.
  14182. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14183. filenames, used by `org-store-new-agenda-file-list' to write back
  14184. un-expanded file names."
  14185. (when (file-directory-p org-agenda-files)
  14186. (error "`org-agenda-files' cannot be a single directory"))
  14187. (when (stringp org-agenda-files)
  14188. (with-temp-buffer
  14189. (insert-file-contents org-agenda-files)
  14190. (mapcar
  14191. (lambda (f)
  14192. (let ((e (expand-file-name (substitute-in-file-name f)
  14193. org-directory)))
  14194. (if pair-with-expansion
  14195. (cons e f)
  14196. e)))
  14197. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14198. ;;;###autoload
  14199. (defun org-cycle-agenda-files ()
  14200. "Cycle through the files in `org-agenda-files'.
  14201. If the current buffer visits an agenda file, find the next one in the list.
  14202. If the current buffer does not, find the first agenda file."
  14203. (interactive)
  14204. (let* ((fs (org-agenda-files t))
  14205. (files (append fs (list (car fs))))
  14206. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14207. file)
  14208. (unless files (error "No agenda files"))
  14209. (catch 'exit
  14210. (while (setq file (pop files))
  14211. (if (equal (file-truename file) tcf)
  14212. (when (car files)
  14213. (find-file (car files))
  14214. (throw 'exit t))))
  14215. (find-file (car fs)))
  14216. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14217. (defun org-agenda-file-to-front (&optional to-end)
  14218. "Move/add the current file to the top of the agenda file list.
  14219. If the file is not present in the list, it is added to the front. If it is
  14220. present, it is moved there. With optional argument TO-END, add/move to the
  14221. end of the list."
  14222. (interactive "P")
  14223. (let ((org-agenda-skip-unavailable-files nil)
  14224. (file-alist (mapcar (lambda (x)
  14225. (cons (file-truename x) x))
  14226. (org-agenda-files t)))
  14227. (ctf (file-truename buffer-file-name))
  14228. x had)
  14229. (setq x (assoc ctf file-alist) had x)
  14230. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14231. (if to-end
  14232. (setq file-alist (append (delq x file-alist) (list x)))
  14233. (setq file-alist (cons x (delq x file-alist))))
  14234. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14235. (org-install-agenda-files-menu)
  14236. (message "File %s to %s of agenda file list"
  14237. (if had "moved" "added") (if to-end "end" "front"))))
  14238. (defun org-remove-file (&optional file)
  14239. "Remove current file from the list of files in variable `org-agenda-files'.
  14240. These are the files which are being checked for agenda entries.
  14241. Optional argument FILE means use this file instead of the current."
  14242. (interactive)
  14243. (let* ((org-agenda-skip-unavailable-files nil)
  14244. (file (or file buffer-file-name))
  14245. (true-file (file-truename file))
  14246. (afile (abbreviate-file-name file))
  14247. (files (delq nil (mapcar
  14248. (lambda (x)
  14249. (if (equal true-file
  14250. (file-truename x))
  14251. nil x))
  14252. (org-agenda-files t)))))
  14253. (if (not (= (length files) (length (org-agenda-files t))))
  14254. (progn
  14255. (org-store-new-agenda-file-list files)
  14256. (org-install-agenda-files-menu)
  14257. (message "Removed file: %s" afile))
  14258. (message "File was not in list: %s (not removed)" afile))))
  14259. (defun org-file-menu-entry (file)
  14260. (vector file (list 'find-file file) t))
  14261. (defun org-check-agenda-file (file)
  14262. "Make sure FILE exists. If not, ask user what to do."
  14263. (when (not (file-exists-p file))
  14264. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14265. (abbreviate-file-name file))
  14266. (let ((r (downcase (read-char-exclusive))))
  14267. (cond
  14268. ((equal r ?r)
  14269. (org-remove-file file)
  14270. (throw 'nextfile t))
  14271. (t (error "Abort"))))))
  14272. (defun org-get-agenda-file-buffer (file)
  14273. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14274. it to the list of buffers which might be released later."
  14275. (let ((buf (org-find-base-buffer-visiting file)))
  14276. (if buf
  14277. buf ; just return it
  14278. ;; Make a new buffer and remember it
  14279. (setq buf (find-file-noselect file))
  14280. (if buf (push buf org-agenda-new-buffers))
  14281. buf)))
  14282. (defun org-release-buffers (blist)
  14283. "Release all buffers in list, asking the user for confirmation when needed.
  14284. When a buffer is unmodified, it is just killed. When modified, it is saved
  14285. \(if the user agrees) and then killed."
  14286. (let (buf file)
  14287. (while (setq buf (pop blist))
  14288. (setq file (buffer-file-name buf))
  14289. (when (and (buffer-modified-p buf)
  14290. file
  14291. (y-or-n-p (format "Save file %s? " file)))
  14292. (with-current-buffer buf (save-buffer)))
  14293. (kill-buffer buf))))
  14294. (defun org-prepare-agenda-buffers (files)
  14295. "Create buffers for all agenda files, protect archived trees and comments."
  14296. (interactive)
  14297. (let ((pa '(:org-archived t))
  14298. (pc '(:org-comment t))
  14299. (pall '(:org-archived t :org-comment t))
  14300. (inhibit-read-only t)
  14301. (rea (concat ":" org-archive-tag ":"))
  14302. bmp file re)
  14303. (save-excursion
  14304. (save-restriction
  14305. (while (setq file (pop files))
  14306. (catch 'nextfile
  14307. (if (bufferp file)
  14308. (set-buffer file)
  14309. (org-check-agenda-file file)
  14310. (set-buffer (org-get-agenda-file-buffer file)))
  14311. (widen)
  14312. (setq bmp (buffer-modified-p))
  14313. (org-refresh-category-properties)
  14314. (setq org-todo-keywords-for-agenda
  14315. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14316. (setq org-done-keywords-for-agenda
  14317. (append org-done-keywords-for-agenda org-done-keywords))
  14318. (setq org-todo-keyword-alist-for-agenda
  14319. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14320. (setq org-drawers-for-agenda
  14321. (append org-drawers-for-agenda org-drawers))
  14322. (setq org-tag-alist-for-agenda
  14323. (append org-tag-alist-for-agenda org-tag-alist))
  14324. (save-excursion
  14325. (remove-text-properties (point-min) (point-max) pall)
  14326. (when org-agenda-skip-archived-trees
  14327. (goto-char (point-min))
  14328. (while (re-search-forward rea nil t)
  14329. (if (org-on-heading-p t)
  14330. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14331. (goto-char (point-min))
  14332. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14333. (while (re-search-forward re nil t)
  14334. (add-text-properties
  14335. (match-beginning 0) (org-end-of-subtree t) pc)))
  14336. (set-buffer-modified-p bmp)))))
  14337. (setq org-todo-keywords-for-agenda
  14338. (org-uniquify org-todo-keywords-for-agenda))
  14339. (setq org-todo-keyword-alist-for-agenda
  14340. (org-uniquify org-todo-keyword-alist-for-agenda)
  14341. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14342. ;;;; Embedded LaTeX
  14343. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14344. "Keymap for the minor `org-cdlatex-mode'.")
  14345. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14346. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14347. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14348. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14349. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14350. (defvar org-cdlatex-texmathp-advice-is-done nil
  14351. "Flag remembering if we have applied the advice to texmathp already.")
  14352. (define-minor-mode org-cdlatex-mode
  14353. "Toggle the minor `org-cdlatex-mode'.
  14354. This mode supports entering LaTeX environment and math in LaTeX fragments
  14355. in Org-mode.
  14356. \\{org-cdlatex-mode-map}"
  14357. nil " OCDL" nil
  14358. (when org-cdlatex-mode (require 'cdlatex))
  14359. (unless org-cdlatex-texmathp-advice-is-done
  14360. (setq org-cdlatex-texmathp-advice-is-done t)
  14361. (defadvice texmathp (around org-math-always-on activate)
  14362. "Always return t in org-mode buffers.
  14363. This is because we want to insert math symbols without dollars even outside
  14364. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14365. an embedded LaTeX fragment, let texmathp do its job.
  14366. \\[org-cdlatex-mode-map]"
  14367. (interactive)
  14368. (let (p)
  14369. (cond
  14370. ((not (org-mode-p)) ad-do-it)
  14371. ((eq this-command 'cdlatex-math-symbol)
  14372. (setq ad-return-value t
  14373. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14374. (t
  14375. (let ((p (org-inside-LaTeX-fragment-p)))
  14376. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14377. (setq ad-return-value t
  14378. texmathp-why '("Org-mode embedded math" . 0))
  14379. (if p ad-do-it)))))))))
  14380. (defun turn-on-org-cdlatex ()
  14381. "Unconditionally turn on `org-cdlatex-mode'."
  14382. (org-cdlatex-mode 1))
  14383. (defun org-inside-LaTeX-fragment-p ()
  14384. "Test if point is inside a LaTeX fragment.
  14385. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14386. sequence appearing also before point.
  14387. Even though the matchers for math are configurable, this function assumes
  14388. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14389. delimiters are skipped when they have been removed by customization.
  14390. The return value is nil, or a cons cell with the delimiter and
  14391. and the position of this delimiter.
  14392. This function does a reasonably good job, but can locally be fooled by
  14393. for example currency specifications. For example it will assume being in
  14394. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14395. fragments that are properly closed, but during editing, we have to live
  14396. with the uncertainty caused by missing closing delimiters. This function
  14397. looks only before point, not after."
  14398. (catch 'exit
  14399. (let ((pos (point))
  14400. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14401. (lim (progn
  14402. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14403. (point)))
  14404. dd-on str (start 0) m re)
  14405. (goto-char pos)
  14406. (when dodollar
  14407. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14408. re (nth 1 (assoc "$" org-latex-regexps)))
  14409. (while (string-match re str start)
  14410. (cond
  14411. ((= (match-end 0) (length str))
  14412. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14413. ((= (match-end 0) (- (length str) 5))
  14414. (throw 'exit nil))
  14415. (t (setq start (match-end 0))))))
  14416. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14417. (goto-char pos)
  14418. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14419. (and (match-beginning 2) (throw 'exit nil))
  14420. ;; count $$
  14421. (while (re-search-backward "\\$\\$" lim t)
  14422. (setq dd-on (not dd-on)))
  14423. (goto-char pos)
  14424. (if dd-on (cons "$$" m))))))
  14425. (defun org-inside-latex-macro-p ()
  14426. "Is point inside a LaTeX macro or its arguments?"
  14427. (save-match-data
  14428. (org-in-regexp
  14429. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14430. (defun org-try-cdlatex-tab ()
  14431. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14432. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14433. - inside a LaTeX fragment, or
  14434. - after the first word in a line, where an abbreviation expansion could
  14435. insert a LaTeX environment."
  14436. (when org-cdlatex-mode
  14437. (cond
  14438. ((save-excursion
  14439. (skip-chars-backward "a-zA-Z0-9*")
  14440. (skip-chars-backward " \t")
  14441. (bolp))
  14442. (cdlatex-tab) t)
  14443. ((org-inside-LaTeX-fragment-p)
  14444. (cdlatex-tab) t)
  14445. (t nil))))
  14446. (defun org-cdlatex-underscore-caret (&optional arg)
  14447. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14448. Revert to the normal definition outside of these fragments."
  14449. (interactive "P")
  14450. (if (org-inside-LaTeX-fragment-p)
  14451. (call-interactively 'cdlatex-sub-superscript)
  14452. (let (org-cdlatex-mode)
  14453. (call-interactively (key-binding (vector last-input-event))))))
  14454. (defun org-cdlatex-math-modify (&optional arg)
  14455. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14456. Revert to the normal definition outside of these fragments."
  14457. (interactive "P")
  14458. (if (org-inside-LaTeX-fragment-p)
  14459. (call-interactively 'cdlatex-math-modify)
  14460. (let (org-cdlatex-mode)
  14461. (call-interactively (key-binding (vector last-input-event))))))
  14462. (defvar org-latex-fragment-image-overlays nil
  14463. "List of overlays carrying the images of latex fragments.")
  14464. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14465. (defun org-remove-latex-fragment-image-overlays ()
  14466. "Remove all overlays with LaTeX fragment images in current buffer."
  14467. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14468. (setq org-latex-fragment-image-overlays nil))
  14469. (defun org-preview-latex-fragment (&optional subtree)
  14470. "Preview the LaTeX fragment at point, or all locally or globally.
  14471. If the cursor is in a LaTeX fragment, create the image and overlay
  14472. it over the source code. If there is no fragment at point, display
  14473. all fragments in the current text, from one headline to the next. With
  14474. prefix SUBTREE, display all fragments in the current subtree. With a
  14475. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14476. the cursor is before the first headline,
  14477. display all fragments in the buffer.
  14478. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14479. (interactive "P")
  14480. (org-remove-latex-fragment-image-overlays)
  14481. (save-excursion
  14482. (save-restriction
  14483. (let (beg end at msg)
  14484. (cond
  14485. ((or (equal subtree '(16))
  14486. (not (save-excursion
  14487. (re-search-backward (concat "^" outline-regexp) nil t))))
  14488. (setq beg (point-min) end (point-max)
  14489. msg "Creating images for buffer...%s"))
  14490. ((equal subtree '(4))
  14491. (org-back-to-heading)
  14492. (setq beg (point) end (org-end-of-subtree t)
  14493. msg "Creating images for subtree...%s"))
  14494. (t
  14495. (if (setq at (org-inside-LaTeX-fragment-p))
  14496. (goto-char (max (point-min) (- (cdr at) 2)))
  14497. (org-back-to-heading))
  14498. (setq beg (point) end (progn (outline-next-heading) (point))
  14499. msg (if at "Creating image...%s"
  14500. "Creating images for entry...%s"))))
  14501. (message msg "")
  14502. (narrow-to-region beg end)
  14503. (goto-char beg)
  14504. (org-format-latex
  14505. (concat "ltxpng/" (file-name-sans-extension
  14506. (file-name-nondirectory
  14507. buffer-file-name)))
  14508. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14509. (message msg "done. Use `C-c C-c' to remove images.")))))
  14510. (defvar org-latex-regexps
  14511. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14512. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14513. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14514. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14515. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14516. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14517. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14518. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14519. "Regular expressions for matching embedded LaTeX.")
  14520. (defvar org-export-have-math nil) ;; dynamic scoping
  14521. (defun org-format-latex (prefix &optional dir overlays msg at
  14522. forbuffer processing-type)
  14523. "Replace LaTeX fragments with links to an image, and produce images.
  14524. Some of the options can be changed using the variable
  14525. `org-format-latex-options'."
  14526. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14527. (let* ((prefixnodir (file-name-nondirectory prefix))
  14528. (absprefix (expand-file-name prefix dir))
  14529. (todir (file-name-directory absprefix))
  14530. (opt org-format-latex-options)
  14531. (matchers (plist-get opt :matchers))
  14532. (re-list org-latex-regexps)
  14533. (org-format-latex-header-extra
  14534. (plist-get (org-infile-export-plist) :latex-header-extra))
  14535. (cnt 0) txt hash link beg end re e checkdir
  14536. executables-checked string
  14537. m n block linkfile movefile ov)
  14538. ;; Check the different regular expressions
  14539. (while (setq e (pop re-list))
  14540. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14541. block (if (nth 3 e) "\n\n" ""))
  14542. (when (member m matchers)
  14543. (goto-char (point-min))
  14544. (while (re-search-forward re nil t)
  14545. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14546. (not (get-text-property (match-beginning n)
  14547. 'org-protected))
  14548. (or (not overlays)
  14549. (not (eq (get-char-property (match-beginning n)
  14550. 'org-overlay-type)
  14551. 'org-latex-overlay))))
  14552. (setq org-export-have-math t)
  14553. (cond
  14554. ((eq processing-type 'verbatim)
  14555. ;; Leave the text verbatim, just protect it
  14556. (add-text-properties (match-beginning n) (match-end n)
  14557. '(org-protected t)))
  14558. ((eq processing-type 'mathjax)
  14559. ;; Prepare for MathJax processing
  14560. (setq string (match-string n))
  14561. (if (member m '("$" "$1"))
  14562. (save-excursion
  14563. (delete-region (match-beginning n) (match-end n))
  14564. (goto-char (match-beginning n))
  14565. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14566. "\\)")
  14567. '(org-protected t))))
  14568. (add-text-properties (match-beginning n) (match-end n)
  14569. '(org-protected t))))
  14570. ((or (eq processing-type 'dvipng) t)
  14571. ;; Process to an image
  14572. (setq txt (match-string n)
  14573. beg (match-beginning n) end (match-end n)
  14574. cnt (1+ cnt))
  14575. (let (print-length print-level) ; make sure full list is printed
  14576. (setq hash (sha1 (prin1-to-string
  14577. (list org-format-latex-header
  14578. org-format-latex-header-extra
  14579. org-export-latex-default-packages-alist
  14580. org-export-latex-packages-alist
  14581. org-format-latex-options
  14582. forbuffer txt)))
  14583. linkfile (format "%s_%s.png" prefix hash)
  14584. movefile (format "%s_%s.png" absprefix hash)))
  14585. (setq link (concat block "[[file:" linkfile "]]" block))
  14586. (if msg (message msg cnt))
  14587. (goto-char beg)
  14588. (unless checkdir ; make sure the directory exists
  14589. (setq checkdir t)
  14590. (or (file-directory-p todir) (make-directory todir t)))
  14591. (unless executables-checked
  14592. (org-check-external-command
  14593. "latex" "needed to convert LaTeX fragments to images")
  14594. (org-check-external-command
  14595. "dvipng" "needed to convert LaTeX fragments to images")
  14596. (setq executables-checked t))
  14597. (unless (file-exists-p movefile)
  14598. (org-create-formula-image
  14599. txt movefile opt forbuffer))
  14600. (if overlays
  14601. (progn
  14602. (mapc (lambda (o)
  14603. (if (eq (overlay-get o 'org-overlay-type)
  14604. 'org-latex-overlay)
  14605. (delete-overlay o)))
  14606. (overlays-in beg end))
  14607. (setq ov (make-overlay beg end))
  14608. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14609. (if (featurep 'xemacs)
  14610. (progn
  14611. (overlay-put ov 'invisible t)
  14612. (overlay-put
  14613. ov 'end-glyph
  14614. (make-glyph (vector 'png :file movefile))))
  14615. (overlay-put
  14616. ov 'display
  14617. (list 'image :type 'png :file movefile :ascent 'center)))
  14618. (push ov org-latex-fragment-image-overlays)
  14619. (goto-char end))
  14620. (delete-region beg end)
  14621. (insert (org-add-props link
  14622. (list 'org-latex-src
  14623. (replace-regexp-in-string
  14624. "\"" "" txt)))))))))))))
  14625. ;; This function borrows from Ganesh Swami's latex2png.el
  14626. (defun org-create-formula-image (string tofile options buffer)
  14627. "This calls dvipng."
  14628. (require 'org-latex)
  14629. (let* ((tmpdir (if (featurep 'xemacs)
  14630. (temp-directory)
  14631. temporary-file-directory))
  14632. (texfilebase (make-temp-name
  14633. (expand-file-name "orgtex" tmpdir)))
  14634. (texfile (concat texfilebase ".tex"))
  14635. (dvifile (concat texfilebase ".dvi"))
  14636. (pngfile (concat texfilebase ".png"))
  14637. (fnh (if (featurep 'xemacs)
  14638. (font-height (get-face-font 'default))
  14639. (face-attribute 'default :height nil)))
  14640. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14641. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14642. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14643. "Black"))
  14644. (bg (or (plist-get options (if buffer :background :html-background))
  14645. "Transparent")))
  14646. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14647. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14648. (with-temp-file texfile
  14649. (insert (org-splice-latex-header
  14650. org-format-latex-header
  14651. org-export-latex-default-packages-alist
  14652. org-export-latex-packages-alist t
  14653. org-format-latex-header-extra))
  14654. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14655. (require 'org-latex)
  14656. (org-export-latex-fix-inputenc))
  14657. (let ((dir default-directory))
  14658. (condition-case nil
  14659. (progn
  14660. (cd tmpdir)
  14661. (call-process "latex" nil nil nil texfile))
  14662. (error nil))
  14663. (cd dir))
  14664. (if (not (file-exists-p dvifile))
  14665. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14666. (condition-case nil
  14667. (call-process "dvipng" nil nil nil
  14668. "-fg" fg "-bg" bg
  14669. "-D" dpi
  14670. ;;"-x" scale "-y" scale
  14671. "-T" "tight"
  14672. "-o" pngfile
  14673. dvifile)
  14674. (error nil))
  14675. (if (not (file-exists-p pngfile))
  14676. (if org-format-latex-signal-error
  14677. (error "Failed to create png file from %s" texfile)
  14678. (message "Failed to create png file from %s" texfile)
  14679. nil)
  14680. ;; Use the requested file name and clean up
  14681. (copy-file pngfile tofile 'replace)
  14682. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14683. (delete-file (concat texfilebase e)))
  14684. pngfile))))
  14685. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14686. "Fill a LaTeX header template TPL.
  14687. In the template, the following place holders will be recognized:
  14688. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14689. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14690. [PACKAGES] \\usepackage statements for PKG
  14691. [NO-PACKAGES] do not include PKG
  14692. [EXTRA] the string EXTRA
  14693. [NO-EXTRA] do not include EXTRA
  14694. For backward compatibility, if both the positive and the negative place
  14695. holder is missing, the positive one (without the \"NO-\") will be
  14696. assumed to be present at the end of the template.
  14697. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14698. EXTRA is a string.
  14699. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14700. (let (rpl (end ""))
  14701. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14702. (setq rpl (if (or (match-end 1) (not def-pkg))
  14703. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14704. tpl (replace-match rpl t t tpl))
  14705. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14706. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14707. (setq rpl (if (or (match-end 1) (not pkg))
  14708. "" (org-latex-packages-to-string pkg snippets-p t))
  14709. tpl (replace-match rpl t t tpl))
  14710. (if pkg (setq end
  14711. (concat end "\n"
  14712. (org-latex-packages-to-string pkg snippets-p)))))
  14713. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14714. (setq rpl (if (or (match-end 1) (not extra))
  14715. "" (concat extra "\n"))
  14716. tpl (replace-match rpl t t tpl))
  14717. (if (and extra (string-match "\\S-" extra))
  14718. (setq end (concat end "\n" extra))))
  14719. (if (string-match "\\S-" end)
  14720. (concat tpl "\n" end)
  14721. tpl)))
  14722. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14723. "Turn an alist of packages into a string with the \\usepackage macros."
  14724. (setq pkg (mapconcat (lambda(p)
  14725. (cond
  14726. ((stringp p) p)
  14727. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14728. (format "%% Package %s omitted" (cadr p)))
  14729. ((equal "" (car p))
  14730. (format "\\usepackage{%s}" (cadr p)))
  14731. (t
  14732. (format "\\usepackage[%s]{%s}"
  14733. (car p) (cadr p)))))
  14734. pkg
  14735. "\n"))
  14736. (if newline (concat pkg "\n") pkg))
  14737. (defun org-dvipng-color (attr)
  14738. "Return an rgb color specification for dvipng."
  14739. (apply 'format "rgb %s %s %s"
  14740. (mapcar 'org-normalize-color
  14741. (color-values (face-attribute 'default attr nil)))))
  14742. (defun org-normalize-color (value)
  14743. "Return string to be used as color value for an RGB component."
  14744. (format "%g" (/ value 65535.0)))
  14745. ;; Image display
  14746. (defvar org-inline-image-overlays nil)
  14747. (make-variable-buffer-local 'org-inline-image-overlays)
  14748. (defun org-toggle-inline-images (&optional include-linked)
  14749. "Toggle the display of inline images.
  14750. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14751. (interactive "P")
  14752. (if org-inline-image-overlays
  14753. (progn
  14754. (org-remove-inline-images)
  14755. (message "Inline image display turned off"))
  14756. (org-display-inline-images include-linked)
  14757. (if org-inline-image-overlays
  14758. (message "%d images displayed inline"
  14759. (length org-inline-image-overlays))
  14760. (message "No images to display inline"))))
  14761. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14762. "Display inline images.
  14763. Normally only links without a description part are inlined, because this
  14764. is how it will work for export. When INCLUDE-LINKED is set, also links
  14765. with a description part will be inlined. This can be nice for a quick
  14766. look at those images, but it does not reflect what exported files will look
  14767. like.
  14768. When REFRESH is set, refresh existing images between BEG and END.
  14769. This will create new image displays only if necessary.
  14770. BEG and END default to the buffer boundaries."
  14771. (interactive "P")
  14772. (unless refresh
  14773. (org-remove-inline-images)
  14774. (clear-image-cache))
  14775. (save-excursion
  14776. (save-restriction
  14777. (widen)
  14778. (setq beg (or beg (point-min)) end (or end (point-max)))
  14779. (goto-char (point-min))
  14780. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  14781. (substring (org-image-file-name-regexp) 0 -2)
  14782. "\\)\\]" (if include-linked "" "\\]")))
  14783. old file ov img)
  14784. (while (re-search-forward re end t)
  14785. (setq old (get-char-property-and-overlay (match-beginning 1)
  14786. 'org-image-overlay))
  14787. (setq file (expand-file-name
  14788. (concat (or (match-string 3) "") (match-string 4))))
  14789. (when (file-exists-p file)
  14790. (if (and (car-safe old) refresh)
  14791. (image-refresh (overlay-get (cdr old) 'display))
  14792. (setq img (save-match-data (create-image file)))
  14793. (when img
  14794. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14795. (overlay-put ov 'display img)
  14796. (overlay-put ov 'face 'default)
  14797. (overlay-put ov 'org-image-overlay t)
  14798. (overlay-put ov 'modification-hooks
  14799. (list 'org-display-inline-modification-hook))
  14800. (push ov org-inline-image-overlays)))))))))
  14801. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  14802. "Remove inline-display overlay if a corresponding region is modified."
  14803. (let ((inhibit-modification-hooks t))
  14804. (when (and ov after)
  14805. (delete ov org-inline-image-overlays)
  14806. (delete-overlay ov))))
  14807. (defun org-remove-inline-images ()
  14808. "Remove inline display of images."
  14809. (interactive)
  14810. (mapc 'delete-overlay org-inline-image-overlays)
  14811. (setq org-inline-image-overlays nil))
  14812. ;;;; Key bindings
  14813. ;; Make `C-c C-x' a prefix key
  14814. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14815. ;; TAB key with modifiers
  14816. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14817. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14818. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14819. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  14820. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  14821. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  14822. ;; The following line is necessary under Suse GNU/Linux
  14823. (unless (featurep 'xemacs)
  14824. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14825. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14826. (define-key org-mode-map [backtab] 'org-shifttab)
  14827. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14828. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14829. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14830. ;; Cursor keys with modifiers
  14831. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14832. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14833. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14834. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14835. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14836. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14837. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14838. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14839. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14840. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14841. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14842. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14843. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14844. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14845. ;; Babel keys
  14846. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  14847. (mapc (lambda (pair)
  14848. (define-key org-babel-map (car pair) (cdr pair)))
  14849. org-babel-key-bindings)
  14850. ;;; Extra keys for tty access.
  14851. ;; We only set them when really needed because otherwise the
  14852. ;; menus don't show the simple keys
  14853. (when (or org-use-extra-keys
  14854. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14855. (not window-system))
  14856. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14857. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14858. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14859. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14860. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14861. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14862. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14863. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14864. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14865. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14866. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14867. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14868. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14869. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14870. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14871. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14872. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14873. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14874. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14875. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14876. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14877. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14878. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  14879. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14880. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14881. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14882. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14883. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14884. ;; All the other keys
  14885. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14886. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14887. (if (boundp 'narrow-map)
  14888. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14889. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14890. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14891. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14892. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14893. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14894. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14895. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14896. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14897. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14898. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14899. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14900. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14901. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14902. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14903. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14904. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14905. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14906. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14907. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14908. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14909. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14910. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14911. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14912. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14913. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14914. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14915. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14916. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14917. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14918. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14919. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14920. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14921. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14922. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14923. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14924. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14925. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14926. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14927. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14928. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14929. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14930. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14931. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14932. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14933. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14934. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14935. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14936. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14937. (org-defkey org-mode-map "\C-m" 'org-return)
  14938. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14939. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14940. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14941. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14942. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14943. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14944. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14945. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14946. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14947. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14948. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14949. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14950. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14951. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14952. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14953. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14954. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14955. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14956. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14957. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14958. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14959. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14960. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14961. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14962. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14963. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14964. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14965. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14966. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14967. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14968. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14969. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14970. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14971. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14972. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  14973. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  14974. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14975. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14976. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14977. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14978. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14979. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14980. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  14981. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14982. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14983. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14984. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  14985. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14986. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14987. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14988. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14989. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14990. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14991. (when (featurep 'xemacs)
  14992. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14993. (defconst org-speed-commands-default
  14994. '(
  14995. ("Outline Navigation")
  14996. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14997. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14998. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14999. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15000. ("u" . (org-speed-move-safe 'outline-up-heading))
  15001. ("j" . org-goto)
  15002. ("g" . (org-refile t))
  15003. ("Outline Visibility")
  15004. ("c" . org-cycle)
  15005. ("C" . org-shifttab)
  15006. (" " . org-display-outline-path)
  15007. ("Outline Structure Editing")
  15008. ("U" . org-shiftmetaup)
  15009. ("D" . org-shiftmetadown)
  15010. ("r" . org-metaright)
  15011. ("l" . org-metaleft)
  15012. ("R" . org-shiftmetaright)
  15013. ("L" . org-shiftmetaleft)
  15014. ("i" . (progn (forward-char 1) (call-interactively
  15015. 'org-insert-heading-respect-content)))
  15016. ("^" . org-sort)
  15017. ("w" . org-refile)
  15018. ("a" . org-archive-subtree-default-with-confirmation)
  15019. ("." . outline-mark-subtree)
  15020. ("Clock Commands")
  15021. ("I" . org-clock-in)
  15022. ("O" . org-clock-out)
  15023. ("Meta Data Editing")
  15024. ("t" . org-todo)
  15025. ("0" . (org-priority ?\ ))
  15026. ("1" . (org-priority ?A))
  15027. ("2" . (org-priority ?B))
  15028. ("3" . (org-priority ?C))
  15029. (";" . org-set-tags-command)
  15030. ("e" . org-set-effort)
  15031. ("Agenda Views etc")
  15032. ("v" . org-agenda)
  15033. ("/" . org-sparse-tree)
  15034. ("Misc")
  15035. ("o" . org-open-at-point)
  15036. ("?" . org-speed-command-help)
  15037. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15038. (">" . (org-agenda-remove-restriction-lock))
  15039. )
  15040. "The default speed commands.")
  15041. (defun org-print-speed-command (e)
  15042. (if (> (length (car e)) 1)
  15043. (progn
  15044. (princ "\n")
  15045. (princ (car e))
  15046. (princ "\n")
  15047. (princ (make-string (length (car e)) ?-))
  15048. (princ "\n"))
  15049. (princ (car e))
  15050. (princ " ")
  15051. (if (symbolp (cdr e))
  15052. (princ (symbol-name (cdr e)))
  15053. (prin1 (cdr e)))
  15054. (princ "\n")))
  15055. (defun org-speed-command-help ()
  15056. "Show the available speed commands."
  15057. (interactive)
  15058. (if (not org-use-speed-commands)
  15059. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15060. (with-output-to-temp-buffer "*Help*"
  15061. (princ "User-defined Speed commands\n===========================\n")
  15062. (mapc 'org-print-speed-command org-speed-commands-user)
  15063. (princ "\n")
  15064. (princ "Built-in Speed commands\n=======================\n")
  15065. (mapc 'org-print-speed-command org-speed-commands-default))
  15066. (with-current-buffer "*Help*"
  15067. (setq truncate-lines t))))
  15068. (defun org-speed-move-safe (cmd)
  15069. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15070. If not, return to the original position and throw an error."
  15071. (interactive)
  15072. (let ((pos (point)))
  15073. (call-interactively cmd)
  15074. (unless (and (bolp) (org-on-heading-p))
  15075. (goto-char pos)
  15076. (error "Boundary reached while executing %s" cmd))))
  15077. (defvar org-self-insert-command-undo-counter 0)
  15078. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15079. (defvar org-speed-command nil)
  15080. (defun org-speed-command-default-hook (keys)
  15081. "Hook for activating single-letter speed commands.
  15082. `org-speed-commands-default' specifies a minimal command set. Use
  15083. `org-speed-commands-user' for further customization."
  15084. (when (or (and (bolp) (looking-at outline-regexp))
  15085. (and (functionp org-use-speed-commands)
  15086. (funcall org-use-speed-commands)))
  15087. (cdr (assoc keys (append org-speed-commands-user
  15088. org-speed-commands-default)))))
  15089. (defun org-babel-speed-command-hook (keys)
  15090. "Hook for activating single-letter code block commands."
  15091. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15092. (cdr (assoc keys org-babel-key-bindings))))
  15093. (defcustom org-speed-command-hook
  15094. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15095. "Hook for activating speed commands at strategic locations.
  15096. Hook functions are called in sequence until a valid handler is
  15097. found.
  15098. Each hook takes a single argument, a user-pressed command key
  15099. which is also a `self-insert-command' from the global map.
  15100. Within the hook, examine the cursor position and the command key
  15101. and return nil or a valid handler as appropriate. Handler could
  15102. be one of an interactive command, a function, or a form.
  15103. Set `org-use-speed-commands' to non-nil value to enable this
  15104. hook. The default setting is `org-speed-command-default-hook'."
  15105. :group 'org-structure
  15106. :type 'hook)
  15107. (defun org-self-insert-command (N)
  15108. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15109. If the cursor is in a table looking at whitespace, the whitespace is
  15110. overwritten, and the table is not marked as requiring realignment."
  15111. (interactive "p")
  15112. (cond
  15113. ((and org-use-speed-commands
  15114. (setq org-speed-command
  15115. (run-hook-with-args-until-success
  15116. 'org-speed-command-hook (this-command-keys))))
  15117. (cond
  15118. ((commandp org-speed-command)
  15119. (setq this-command org-speed-command)
  15120. (call-interactively org-speed-command))
  15121. ((functionp org-speed-command)
  15122. (funcall org-speed-command))
  15123. ((and org-speed-command (listp org-speed-command))
  15124. (eval org-speed-command))
  15125. (t (let (org-use-speed-commands)
  15126. (call-interactively 'org-self-insert-command)))))
  15127. ((and
  15128. (org-table-p)
  15129. (progn
  15130. ;; check if we blank the field, and if that triggers align
  15131. (and (featurep 'org-table) org-table-auto-blank-field
  15132. (member last-command
  15133. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15134. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15135. ;; got extra space, this field does not determine column width
  15136. (let (org-table-may-need-update) (org-table-blank-field))
  15137. ;; no extra space, this field may determine column width
  15138. (org-table-blank-field)))
  15139. t)
  15140. (eq N 1)
  15141. (looking-at "[^|\n]* |"))
  15142. (let (org-table-may-need-update)
  15143. (goto-char (1- (match-end 0)))
  15144. (delete-backward-char 1)
  15145. (goto-char (match-beginning 0))
  15146. (self-insert-command N)))
  15147. (t
  15148. (setq org-table-may-need-update t)
  15149. (self-insert-command N)
  15150. (org-fix-tags-on-the-fly)
  15151. (if org-self-insert-cluster-for-undo
  15152. (if (not (eq last-command 'org-self-insert-command))
  15153. (setq org-self-insert-command-undo-counter 1)
  15154. (if (>= org-self-insert-command-undo-counter 20)
  15155. (setq org-self-insert-command-undo-counter 1)
  15156. (and (> org-self-insert-command-undo-counter 0)
  15157. buffer-undo-list
  15158. (not (cadr buffer-undo-list)) ; remove nil entry
  15159. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15160. (setq org-self-insert-command-undo-counter
  15161. (1+ org-self-insert-command-undo-counter))))))))
  15162. (defun org-fix-tags-on-the-fly ()
  15163. (when (and (equal (char-after (point-at-bol)) ?*)
  15164. (org-on-heading-p))
  15165. (org-align-tags-here org-tags-column)))
  15166. (defun org-delete-backward-char (N)
  15167. "Like `delete-backward-char', insert whitespace at field end in tables.
  15168. When deleting backwards, in tables this function will insert whitespace in
  15169. front of the next \"|\" separator, to keep the table aligned. The table will
  15170. still be marked for re-alignment if the field did fill the entire column,
  15171. because, in this case the deletion might narrow the column."
  15172. (interactive "p")
  15173. (if (and (org-table-p)
  15174. (eq N 1)
  15175. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15176. (looking-at ".*?|"))
  15177. (let ((pos (point))
  15178. (noalign (looking-at "[^|\n\r]* |"))
  15179. (c org-table-may-need-update))
  15180. (backward-delete-char N)
  15181. (if (not overwrite-mode)
  15182. (progn
  15183. (skip-chars-forward "^|")
  15184. (insert " ")
  15185. (goto-char (1- pos))))
  15186. ;; noalign: if there were two spaces at the end, this field
  15187. ;; does not determine the width of the column.
  15188. (if noalign (setq org-table-may-need-update c)))
  15189. (backward-delete-char N)
  15190. (org-fix-tags-on-the-fly)))
  15191. (defun org-delete-char (N)
  15192. "Like `delete-char', but insert whitespace at field end in tables.
  15193. When deleting characters, in tables this function will insert whitespace in
  15194. front of the next \"|\" separator, to keep the table aligned. The table will
  15195. still be marked for re-alignment if the field did fill the entire column,
  15196. because, in this case the deletion might narrow the column."
  15197. (interactive "p")
  15198. (if (and (org-table-p)
  15199. (not (bolp))
  15200. (not (= (char-after) ?|))
  15201. (eq N 1))
  15202. (if (looking-at ".*?|")
  15203. (let ((pos (point))
  15204. (noalign (looking-at "[^|\n\r]* |"))
  15205. (c org-table-may-need-update))
  15206. (replace-match (concat
  15207. (substring (match-string 0) 1 -1)
  15208. " |"))
  15209. (goto-char pos)
  15210. ;; noalign: if there were two spaces at the end, this field
  15211. ;; does not determine the width of the column.
  15212. (if noalign (setq org-table-may-need-update c)))
  15213. (delete-char N))
  15214. (delete-char N)
  15215. (org-fix-tags-on-the-fly)))
  15216. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15217. (put 'org-self-insert-command 'delete-selection t)
  15218. (put 'orgtbl-self-insert-command 'delete-selection t)
  15219. (put 'org-delete-char 'delete-selection 'supersede)
  15220. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15221. (put 'org-yank 'delete-selection 'yank)
  15222. ;; Make `flyspell-mode' delay after some commands
  15223. (put 'org-self-insert-command 'flyspell-delayed t)
  15224. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15225. (put 'org-delete-char 'flyspell-delayed t)
  15226. (put 'org-delete-backward-char 'flyspell-delayed t)
  15227. ;; Make pabbrev-mode expand after org-mode commands
  15228. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15229. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15230. ;; How to do this: Measure non-white length of current string
  15231. ;; If equal to column width, we should realign.
  15232. (defun org-remap (map &rest commands)
  15233. "In MAP, remap the functions given in COMMANDS.
  15234. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15235. (let (new old)
  15236. (while commands
  15237. (setq old (pop commands) new (pop commands))
  15238. (if (fboundp 'command-remapping)
  15239. (org-defkey map (vector 'remap old) new)
  15240. (substitute-key-definition old new map global-map)))))
  15241. (when (eq org-enable-table-editor 'optimized)
  15242. ;; If the user wants maximum table support, we need to hijack
  15243. ;; some standard editing functions
  15244. (org-remap org-mode-map
  15245. 'self-insert-command 'org-self-insert-command
  15246. 'delete-char 'org-delete-char
  15247. 'delete-backward-char 'org-delete-backward-char)
  15248. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15249. (defvar org-ctrl-c-ctrl-c-hook nil
  15250. "Hook for functions attaching themselves to `C-c C-c'.
  15251. This can be used to add additional functionality to the C-c C-c key which
  15252. executes context-dependent commands.
  15253. Each function will be called with no arguments. The function must check
  15254. if the context is appropriate for it to act. If yes, it should do its
  15255. thing and then return a non-nil value. If the context is wrong,
  15256. just do nothing and return nil.")
  15257. (defvar org-tab-first-hook nil
  15258. "Hook for functions to attach themselves to TAB.
  15259. See `org-ctrl-c-ctrl-c-hook' for more information.
  15260. This hook runs as the first action when TAB is pressed, even before
  15261. `org-cycle' messes around with the `outline-regexp' to cater for
  15262. inline tasks and plain list item folding.
  15263. If any function in this hook returns t, any other actions that
  15264. would have been caused by TAB (such as table field motion or visibility
  15265. cycling) will not occur.")
  15266. (defvar org-tab-after-check-for-table-hook nil
  15267. "Hook for functions to attach themselves to TAB.
  15268. See `org-ctrl-c-ctrl-c-hook' for more information.
  15269. This hook runs after it has been established that the cursor is not in a
  15270. table, but before checking if the cursor is in a headline or if global cycling
  15271. should be done.
  15272. If any function in this hook returns t, not other actions like visibility
  15273. cycling will be done.")
  15274. (defvar org-tab-after-check-for-cycling-hook nil
  15275. "Hook for functions to attach themselves to TAB.
  15276. See `org-ctrl-c-ctrl-c-hook' for more information.
  15277. This hook runs after it has been established that not table field motion and
  15278. not visibility should be done because of current context. This is probably
  15279. the place where a package like yasnippets can hook in.")
  15280. (defvar org-tab-before-tab-emulation-hook nil
  15281. "Hook for functions to attach themselves to TAB.
  15282. See `org-ctrl-c-ctrl-c-hook' for more information.
  15283. This hook runs after every other options for TAB have been exhausted, but
  15284. before indentation and \t insertion takes place.")
  15285. (defvar org-metaleft-hook nil
  15286. "Hook for functions attaching themselves to `M-left'.
  15287. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15288. (defvar org-metaright-hook nil
  15289. "Hook for functions attaching themselves to `M-right'.
  15290. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15291. (defvar org-metaup-hook nil
  15292. "Hook for functions attaching themselves to `M-up'.
  15293. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15294. (defvar org-metadown-hook nil
  15295. "Hook for functions attaching themselves to `M-down'.
  15296. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15297. (defvar org-shiftmetaleft-hook nil
  15298. "Hook for functions attaching themselves to `M-S-left'.
  15299. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15300. (defvar org-shiftmetaright-hook nil
  15301. "Hook for functions attaching themselves to `M-S-right'.
  15302. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15303. (defvar org-shiftmetaup-hook nil
  15304. "Hook for functions attaching themselves to `M-S-up'.
  15305. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15306. (defvar org-shiftmetadown-hook nil
  15307. "Hook for functions attaching themselves to `M-S-down'.
  15308. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15309. (defvar org-metareturn-hook nil
  15310. "Hook for functions attaching themselves to `M-RET'.
  15311. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15312. (defvar org-shiftup-hook nil
  15313. "Hook for functions attaching themselves to `S-up'.
  15314. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15315. (defvar org-shiftup-final-hook nil
  15316. "Hook for functions attaching themselves to `S-up'.
  15317. This one runs after all other options except shift-select have been excluded.
  15318. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15319. (defvar org-shiftdown-hook nil
  15320. "Hook for functions attaching themselves to `S-down'.
  15321. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15322. (defvar org-shiftdown-final-hook nil
  15323. "Hook for functions attaching themselves to `S-down'.
  15324. This one runs after all other options except shift-select have been excluded.
  15325. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15326. (defvar org-shiftleft-hook nil
  15327. "Hook for functions attaching themselves to `S-left'.
  15328. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15329. (defvar org-shiftleft-final-hook nil
  15330. "Hook for functions attaching themselves to `S-left'.
  15331. This one runs after all other options except shift-select have been excluded.
  15332. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15333. (defvar org-shiftright-hook nil
  15334. "Hook for functions attaching themselves to `S-right'.
  15335. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15336. (defvar org-shiftright-final-hook nil
  15337. "Hook for functions attaching themselves to `S-right'.
  15338. This one runs after all other options except shift-select have been excluded.
  15339. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15340. (defun org-modifier-cursor-error ()
  15341. "Throw an error, a modified cursor command was applied in wrong context."
  15342. (error "This command is active in special context like tables, headlines or items"))
  15343. (defun org-shiftselect-error ()
  15344. "Throw an error because Shift-Cursor command was applied in wrong context."
  15345. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15346. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15347. (error "This command works only in special context like headlines or timestamps")))
  15348. (defun org-call-for-shift-select (cmd)
  15349. (let ((this-command-keys-shift-translated t))
  15350. (call-interactively cmd)))
  15351. (defun org-shifttab (&optional arg)
  15352. "Global visibility cycling or move to previous table field.
  15353. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15354. on context.
  15355. See the individual commands for more information."
  15356. (interactive "P")
  15357. (cond
  15358. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15359. ((integerp arg)
  15360. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15361. (message "Content view to level: %d" arg)
  15362. (org-content (prefix-numeric-value arg2))
  15363. (setq org-cycle-global-status 'overview)))
  15364. (t (call-interactively 'org-global-cycle))))
  15365. (defun org-shiftmetaleft ()
  15366. "Promote subtree or delete table column.
  15367. Calls `org-promote-subtree', `org-outdent-item',
  15368. or `org-table-delete-column', depending on context.
  15369. See the individual commands for more information."
  15370. (interactive)
  15371. (cond
  15372. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15373. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15374. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15375. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15376. (t (org-modifier-cursor-error))))
  15377. (defun org-shiftmetaright ()
  15378. "Demote subtree or insert table column.
  15379. Calls `org-demote-subtree', `org-indent-item',
  15380. or `org-table-insert-column', depending on context.
  15381. See the individual commands for more information."
  15382. (interactive)
  15383. (cond
  15384. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15385. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15386. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15387. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15388. (t (org-modifier-cursor-error))))
  15389. (defun org-shiftmetaup (&optional arg)
  15390. "Move subtree up or kill table row.
  15391. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15392. `org-move-item-up' depending on context. See the individual commands
  15393. for more information."
  15394. (interactive "P")
  15395. (cond
  15396. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15397. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15398. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15399. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15400. (t (org-modifier-cursor-error))))
  15401. (defun org-shiftmetadown (&optional arg)
  15402. "Move subtree down or insert table row.
  15403. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15404. `org-move-item-down', depending on context. See the individual
  15405. commands for more information."
  15406. (interactive "P")
  15407. (cond
  15408. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15409. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15410. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15411. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15412. (t (org-modifier-cursor-error))))
  15413. (defsubst org-hidden-tree-error ()
  15414. (error
  15415. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15416. (defun org-metaleft (&optional arg)
  15417. "Promote heading or move table column to left.
  15418. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15419. With no specific context, calls the Emacs default `backward-word'.
  15420. See the individual commands for more information."
  15421. (interactive "P")
  15422. (cond
  15423. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15424. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15425. ((or (org-on-heading-p)
  15426. (and (org-region-active-p)
  15427. (save-excursion
  15428. (goto-char (region-beginning))
  15429. (org-on-heading-p))))
  15430. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15431. (call-interactively 'org-do-promote))
  15432. ((or (org-at-item-p)
  15433. (and (org-region-active-p)
  15434. (save-excursion
  15435. (goto-char (region-beginning))
  15436. (org-at-item-p))))
  15437. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15438. (call-interactively 'org-outdent-item))
  15439. (t (call-interactively 'backward-word))))
  15440. (defun org-metaright (&optional arg)
  15441. "Demote subtree or move table column to right.
  15442. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15443. With no specific context, calls the Emacs default `forward-word'.
  15444. See the individual commands for more information."
  15445. (interactive "P")
  15446. (cond
  15447. ((run-hook-with-args-until-success 'org-metaright-hook))
  15448. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15449. ((or (org-on-heading-p)
  15450. (and (org-region-active-p)
  15451. (save-excursion
  15452. (goto-char (region-beginning))
  15453. (org-on-heading-p))))
  15454. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15455. (call-interactively 'org-do-demote))
  15456. ((or (org-at-item-p)
  15457. (and (org-region-active-p)
  15458. (save-excursion
  15459. (goto-char (region-beginning))
  15460. (org-at-item-p))))
  15461. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15462. (call-interactively 'org-indent-item))
  15463. (t (call-interactively 'forward-word))))
  15464. (defun org-check-for-hidden (what)
  15465. "Check if there are hidden headlines/items in the current visual line.
  15466. WHAT can be either `headlines' or `items'. If the current line is
  15467. an outline or item heading and it has a folded subtree below it,
  15468. this function returns t, nil otherwise."
  15469. (let ((re (cond
  15470. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15471. ((eq what 'items) (concat "^" (org-item-re t)))
  15472. (t (error "This should not happen"))))
  15473. beg end)
  15474. (save-excursion
  15475. (catch 'exit
  15476. (unless (org-region-active-p)
  15477. (setq beg (point-at-bol))
  15478. (beginning-of-line 2)
  15479. (while (and (not (eobp)) ;; this is like `next-line'
  15480. (get-char-property (1- (point)) 'invisible))
  15481. (beginning-of-line 2))
  15482. (setq end (point))
  15483. (goto-char beg)
  15484. (goto-char (point-at-eol))
  15485. (setq end (max end (point)))
  15486. (while (re-search-forward re end t)
  15487. (if (get-char-property (match-beginning 0) 'invisible)
  15488. (throw 'exit t))))
  15489. nil))))
  15490. (defun org-metaup (&optional arg)
  15491. "Move subtree up or move table row up.
  15492. Calls `org-move-subtree-up' or `org-table-move-row' or
  15493. `org-move-item-up', depending on context. See the individual commands
  15494. for more information."
  15495. (interactive "P")
  15496. (cond
  15497. ((run-hook-with-args-until-success 'org-metaup-hook))
  15498. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15499. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15500. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15501. (t (transpose-lines 1) (beginning-of-line -1))))
  15502. (defun org-metadown (&optional arg)
  15503. "Move subtree down or move table row down.
  15504. Calls `org-move-subtree-down' or `org-table-move-row' or
  15505. `org-move-item-down', depending on context. See the individual
  15506. commands for more information."
  15507. (interactive "P")
  15508. (cond
  15509. ((run-hook-with-args-until-success 'org-metadown-hook))
  15510. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15511. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15512. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15513. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15514. (defun org-shiftup (&optional arg)
  15515. "Increase item in timestamp or increase priority of current headline.
  15516. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15517. depending on context. See the individual commands for more information."
  15518. (interactive "P")
  15519. (cond
  15520. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15521. ((and org-support-shift-select (org-region-active-p))
  15522. (org-call-for-shift-select 'previous-line))
  15523. ((org-at-timestamp-p t)
  15524. (call-interactively (if org-edit-timestamp-down-means-later
  15525. 'org-timestamp-down 'org-timestamp-up)))
  15526. ((and (not (eq org-support-shift-select 'always))
  15527. org-enable-priority-commands
  15528. (org-on-heading-p))
  15529. (call-interactively 'org-priority-up))
  15530. ((and (not org-support-shift-select) (org-at-item-p))
  15531. (call-interactively 'org-previous-item))
  15532. ((org-clocktable-try-shift 'up arg))
  15533. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15534. (org-support-shift-select
  15535. (org-call-for-shift-select 'previous-line))
  15536. (t (org-shiftselect-error))))
  15537. (defun org-shiftdown (&optional arg)
  15538. "Decrease item in timestamp or decrease priority of current headline.
  15539. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15540. depending on context. See the individual commands for more information."
  15541. (interactive "P")
  15542. (cond
  15543. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15544. ((and org-support-shift-select (org-region-active-p))
  15545. (org-call-for-shift-select 'next-line))
  15546. ((org-at-timestamp-p t)
  15547. (call-interactively (if org-edit-timestamp-down-means-later
  15548. 'org-timestamp-up 'org-timestamp-down)))
  15549. ((and (not (eq org-support-shift-select 'always))
  15550. org-enable-priority-commands
  15551. (org-on-heading-p))
  15552. (call-interactively 'org-priority-down))
  15553. ((and (not org-support-shift-select) (org-at-item-p))
  15554. (call-interactively 'org-next-item))
  15555. ((org-clocktable-try-shift 'down arg))
  15556. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15557. (org-support-shift-select
  15558. (org-call-for-shift-select 'next-line))
  15559. (t (org-shiftselect-error))))
  15560. (defun org-shiftright (&optional arg)
  15561. "Cycle the thing at point or in the current line, depending on context.
  15562. Depending on context, this does one of the following:
  15563. - switch a timestamp at point one day into the future
  15564. - on a headline, switch to the next TODO keyword.
  15565. - on an item, switch entire list to the next bullet type
  15566. - on a property line, switch to the next allowed value
  15567. - on a clocktable definition line, move time block into the future"
  15568. (interactive "P")
  15569. (cond
  15570. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15571. ((and org-support-shift-select (org-region-active-p))
  15572. (org-call-for-shift-select 'forward-char))
  15573. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15574. ((and (not (eq org-support-shift-select 'always))
  15575. (org-on-heading-p))
  15576. (let ((org-inhibit-logging
  15577. (not org-treat-S-cursor-todo-selection-as-state-change))
  15578. (org-inhibit-blocking
  15579. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15580. (org-call-with-arg 'org-todo 'right)))
  15581. ((or (and org-support-shift-select
  15582. (not (eq org-support-shift-select 'always))
  15583. (org-at-item-bullet-p))
  15584. (and (not org-support-shift-select) (org-at-item-p)))
  15585. (org-call-with-arg 'org-cycle-list-bullet nil))
  15586. ((and (not (eq org-support-shift-select 'always))
  15587. (org-at-property-p))
  15588. (call-interactively 'org-property-next-allowed-value))
  15589. ((org-clocktable-try-shift 'right arg))
  15590. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15591. (org-support-shift-select
  15592. (org-call-for-shift-select 'forward-char))
  15593. (t (org-shiftselect-error))))
  15594. (defun org-shiftleft (&optional arg)
  15595. "Cycle the thing at point or in the current line, depending on context.
  15596. Depending on context, this does one of the following:
  15597. - switch a timestamp at point one day into the past
  15598. - on a headline, switch to the previous TODO keyword.
  15599. - on an item, switch entire list to the previous bullet type
  15600. - on a property line, switch to the previous allowed value
  15601. - on a clocktable definition line, move time block into the past"
  15602. (interactive "P")
  15603. (cond
  15604. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15605. ((and org-support-shift-select (org-region-active-p))
  15606. (org-call-for-shift-select 'backward-char))
  15607. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15608. ((and (not (eq org-support-shift-select 'always))
  15609. (org-on-heading-p))
  15610. (let ((org-inhibit-logging
  15611. (not org-treat-S-cursor-todo-selection-as-state-change))
  15612. (org-inhibit-blocking
  15613. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15614. (org-call-with-arg 'org-todo 'left)))
  15615. ((or (and org-support-shift-select
  15616. (not (eq org-support-shift-select 'always))
  15617. (org-at-item-bullet-p))
  15618. (and (not org-support-shift-select) (org-at-item-p)))
  15619. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15620. ((and (not (eq org-support-shift-select 'always))
  15621. (org-at-property-p))
  15622. (call-interactively 'org-property-previous-allowed-value))
  15623. ((org-clocktable-try-shift 'left arg))
  15624. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15625. (org-support-shift-select
  15626. (org-call-for-shift-select 'backward-char))
  15627. (t (org-shiftselect-error))))
  15628. (defun org-shiftcontrolright ()
  15629. "Switch to next TODO set."
  15630. (interactive)
  15631. (cond
  15632. ((and org-support-shift-select (org-region-active-p))
  15633. (org-call-for-shift-select 'forward-word))
  15634. ((and (not (eq org-support-shift-select 'always))
  15635. (org-on-heading-p))
  15636. (org-call-with-arg 'org-todo 'nextset))
  15637. (org-support-shift-select
  15638. (org-call-for-shift-select 'forward-word))
  15639. (t (org-shiftselect-error))))
  15640. (defun org-shiftcontrolleft ()
  15641. "Switch to previous TODO set."
  15642. (interactive)
  15643. (cond
  15644. ((and org-support-shift-select (org-region-active-p))
  15645. (org-call-for-shift-select 'backward-word))
  15646. ((and (not (eq org-support-shift-select 'always))
  15647. (org-on-heading-p))
  15648. (org-call-with-arg 'org-todo 'previousset))
  15649. (org-support-shift-select
  15650. (org-call-for-shift-select 'backward-word))
  15651. (t (org-shiftselect-error))))
  15652. (defun org-ctrl-c-ret ()
  15653. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15654. (interactive)
  15655. (cond
  15656. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15657. (t (call-interactively 'org-insert-heading))))
  15658. (defun org-copy-special ()
  15659. "Copy region in table or copy current subtree.
  15660. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15661. See the individual commands for more information."
  15662. (interactive)
  15663. (call-interactively
  15664. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15665. (defun org-cut-special ()
  15666. "Cut region in table or cut current subtree.
  15667. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15668. See the individual commands for more information."
  15669. (interactive)
  15670. (call-interactively
  15671. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15672. (defun org-paste-special (arg)
  15673. "Paste rectangular region into table, or past subtree relative to level.
  15674. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15675. See the individual commands for more information."
  15676. (interactive "P")
  15677. (if (org-at-table-p)
  15678. (org-table-paste-rectangle)
  15679. (org-paste-subtree arg)))
  15680. (defun org-edit-special (&optional arg)
  15681. "Call a special editor for the stuff at point.
  15682. When at a table, call the formula editor with `org-table-edit-formulas'.
  15683. When at the first line of an src example, call `org-edit-src-code'.
  15684. When in an #+include line, visit the include file. Otherwise call
  15685. `ffap' to visit the file at point."
  15686. (interactive)
  15687. ;; possibly prep session before editing source
  15688. (when arg
  15689. (let* ((info (org-babel-get-src-block-info))
  15690. (lang (nth 0 info))
  15691. (params (nth 2 info))
  15692. (session (cdr (assoc :session params))))
  15693. (when (and info session) ;; we are in a source-code block with a session
  15694. (funcall
  15695. (intern (concat "org-babel-prep-session:" lang)) session params))))
  15696. (cond ;; proceed with `org-edit-special'
  15697. ((save-excursion
  15698. (beginning-of-line 1)
  15699. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15700. (find-file (org-trim (match-string 1))))
  15701. ((org-edit-src-code))
  15702. ((org-edit-fixed-width-region))
  15703. ((org-at-table.el-p)
  15704. (org-edit-src-code))
  15705. ((org-at-table-p)
  15706. (call-interactively 'org-table-edit-formulas))
  15707. (t (call-interactively 'ffap))))
  15708. (defun org-ctrl-c-ctrl-c (&optional arg)
  15709. "Set tags in headline, or update according to changed information at point.
  15710. This command does many different things, depending on context:
  15711. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15712. this is what we do.
  15713. - If the cursor is on a statistics cookie, update it.
  15714. - If the cursor is in a headline, prompt for tags and insert them
  15715. into the current line, aligned to `org-tags-column'. When called
  15716. with prefix arg, realign all tags in the current buffer.
  15717. - If the cursor is in one of the special #+KEYWORD lines, this
  15718. triggers scanning the buffer for these lines and updating the
  15719. information.
  15720. - If the cursor is inside a table, realign the table. This command
  15721. works even if the automatic table editor has been turned off.
  15722. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15723. the entire table.
  15724. - If the cursor is at a footnote reference or definition, jump to
  15725. the corresponding definition or references, respectively.
  15726. - If the cursor is a the beginning of a dynamic block, update it.
  15727. - If the current buffer is a capture buffer, close note and file it.
  15728. - If the cursor is on a <<<target>>>, update radio targets and
  15729. corresponding links in this buffer.
  15730. - If the cursor is on a numbered item in a plain list, renumber the
  15731. ordered list.
  15732. - If the cursor is on a checkbox, toggle it.
  15733. - If the cursor is on a code block, evaluate it. The variable
  15734. `org-confirm-babel-evaluate' can be used to control prompting
  15735. before code block evaluation, by default every code block
  15736. evaluation requires confirmation. Code block evaluation can be
  15737. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  15738. (interactive "P")
  15739. (let ((org-enable-table-editor t))
  15740. (cond
  15741. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15742. org-occur-highlights
  15743. org-latex-fragment-image-overlays)
  15744. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15745. (org-remove-occur-highlights)
  15746. (org-remove-latex-fragment-image-overlays)
  15747. (message "Temporary highlights/overlays removed from current buffer"))
  15748. ((and (local-variable-p 'org-finish-function (current-buffer))
  15749. (fboundp org-finish-function))
  15750. (funcall org-finish-function))
  15751. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15752. ((or (looking-at org-property-start-re)
  15753. (org-at-property-p))
  15754. (call-interactively 'org-property-action))
  15755. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15756. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15757. (or (org-on-heading-p) (org-at-item-p)))
  15758. (call-interactively 'org-update-statistics-cookies))
  15759. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15760. ((org-at-table.el-p)
  15761. (message "Use C-c ' to edit table.el tables"))
  15762. ((org-at-table-p)
  15763. (org-table-maybe-eval-formula)
  15764. (if arg
  15765. (call-interactively 'org-table-recalculate)
  15766. (org-table-maybe-recalculate-line))
  15767. (call-interactively 'org-table-align)
  15768. (orgtbl-send-table 'maybe))
  15769. ((or (org-footnote-at-reference-p)
  15770. (org-footnote-at-definition-p))
  15771. (call-interactively 'org-footnote-action))
  15772. ((org-at-item-checkbox-p)
  15773. (call-interactively 'org-list-repair)
  15774. (call-interactively 'org-toggle-checkbox)
  15775. (org-list-send-list 'maybe))
  15776. ((org-at-item-p)
  15777. (call-interactively 'org-list-repair)
  15778. (when arg (call-interactively 'org-toggle-checkbox))
  15779. (org-list-send-list 'maybe))
  15780. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15781. ;; Dynamic block
  15782. (beginning-of-line 1)
  15783. (save-excursion (org-update-dblock)))
  15784. ((save-excursion
  15785. (beginning-of-line 1)
  15786. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15787. (cond
  15788. ((equal (match-string 1) "TBLFM")
  15789. ;; Recalculate the table before this line
  15790. (save-excursion
  15791. (beginning-of-line 1)
  15792. (skip-chars-backward " \r\n\t")
  15793. (if (org-at-table-p)
  15794. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15795. (t
  15796. (let ((org-inhibit-startup-visibility-stuff t)
  15797. (org-startup-align-all-tables nil))
  15798. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15799. (message "Local setup has been refreshed"))))
  15800. ((org-clock-update-time-maybe))
  15801. (t (error "C-c C-c can do nothing useful at this location")))))
  15802. (defun org-mode-restart ()
  15803. "Restart Org-mode, to scan again for special lines.
  15804. Also updates the keyword regular expressions."
  15805. (interactive)
  15806. (org-mode)
  15807. (message "Org-mode restarted"))
  15808. (defun org-kill-note-or-show-branches ()
  15809. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15810. (interactive)
  15811. (if (not org-finish-function)
  15812. (progn
  15813. (hide-subtree)
  15814. (call-interactively 'show-branches))
  15815. (let ((org-note-abort t))
  15816. (funcall org-finish-function))))
  15817. (defun org-return (&optional indent)
  15818. "Goto next table row or insert a newline.
  15819. Calls `org-table-next-row' or `newline', depending on context.
  15820. See the individual commands for more information."
  15821. (interactive)
  15822. (cond
  15823. ((bobp) (if indent (newline-and-indent) (newline)))
  15824. ((org-at-table-p)
  15825. (org-table-justify-field-maybe)
  15826. (call-interactively 'org-table-next-row))
  15827. ((and org-return-follows-link
  15828. (eq (get-text-property (point) 'face) 'org-link))
  15829. (call-interactively 'org-open-at-point))
  15830. ((and (org-at-heading-p)
  15831. (looking-at
  15832. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  15833. (org-show-entry)
  15834. (end-of-line 1)
  15835. (newline))
  15836. (t (if indent (newline-and-indent) (newline)))))
  15837. (defun org-return-indent ()
  15838. "Goto next table row or insert a newline and indent.
  15839. Calls `org-table-next-row' or `newline-and-indent', depending on
  15840. context. See the individual commands for more information."
  15841. (interactive)
  15842. (org-return t))
  15843. (defun org-ctrl-c-star ()
  15844. "Compute table, or change heading status of lines.
  15845. Calls `org-table-recalculate' or `org-toggle-heading',
  15846. depending on context."
  15847. (interactive)
  15848. (cond
  15849. ((org-at-table-p)
  15850. (call-interactively 'org-table-recalculate))
  15851. (t
  15852. ;; Convert all lines in region to list items
  15853. (call-interactively 'org-toggle-heading))))
  15854. (defun org-ctrl-c-minus ()
  15855. "Insert separator line in table or modify bullet status of line.
  15856. Also turns a plain line or a region of lines into list items.
  15857. Calls `org-table-insert-hline', `org-toggle-item', or
  15858. `org-cycle-list-bullet', depending on context."
  15859. (interactive)
  15860. (cond
  15861. ((org-at-table-p)
  15862. (call-interactively 'org-table-insert-hline))
  15863. ((org-region-active-p)
  15864. (call-interactively 'org-toggle-item))
  15865. ((org-in-item-p)
  15866. (call-interactively 'org-cycle-list-bullet))
  15867. (t
  15868. (call-interactively 'org-toggle-item))))
  15869. (defun org-toggle-item ()
  15870. "Convert headings or normal lines to items, items to normal lines.
  15871. If there is no active region, only the current line is considered.
  15872. If the first line in the region is a headline, convert all headlines to items.
  15873. If the first line in the region is an item, convert all items to normal lines.
  15874. If the first line is normal text, add an item bullet to each line."
  15875. (interactive)
  15876. (let (l2 l beg end)
  15877. (if (org-region-active-p)
  15878. (setq beg (region-beginning) end (region-end))
  15879. (setq beg (point-at-bol)
  15880. end (min (1+ (point-at-eol)) (point-max))))
  15881. (save-excursion
  15882. (goto-char end)
  15883. (setq l2 (org-current-line))
  15884. (goto-char beg)
  15885. (beginning-of-line 1)
  15886. (setq l (1- (org-current-line)))
  15887. (if (org-at-item-p)
  15888. ;; We already have items, de-itemize
  15889. (while (< (setq l (1+ l)) l2)
  15890. (when (org-at-item-p)
  15891. (skip-chars-forward " \t")
  15892. (delete-region (point) (match-end 0)))
  15893. (beginning-of-line 2))
  15894. (if (org-on-heading-p)
  15895. ;; Headings, convert to items
  15896. (while (< (setq l (1+ l)) l2)
  15897. (if (looking-at org-outline-regexp)
  15898. (replace-match (org-list-bullet-string "-") t t))
  15899. (beginning-of-line 2))
  15900. ;; normal lines, turn them into items
  15901. (while (< (setq l (1+ l)) l2)
  15902. (unless (org-at-item-p)
  15903. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15904. (replace-match
  15905. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  15906. (beginning-of-line 2)))))))
  15907. (defun org-toggle-heading (&optional nstars)
  15908. "Convert headings to normal text, or items or text to headings.
  15909. If there is no active region, only the current line is considered.
  15910. If the first line is a heading, remove the stars from all headlines
  15911. in the region.
  15912. If the first line is a plain list item, turn all plain list items
  15913. into headings.
  15914. If the first line is a normal line, turn each and every line in the
  15915. region into a heading.
  15916. When converting a line into a heading, the number of stars is chosen
  15917. such that the lines become children of the current entry. However,
  15918. when a prefix argument is given, its value determines the number of
  15919. stars to add."
  15920. (interactive "P")
  15921. (let (l2 l itemp beg end)
  15922. (if (org-region-active-p)
  15923. (setq beg (region-beginning) end (region-end))
  15924. (setq beg (point-at-bol)
  15925. end (min (1+ (point-at-eol)) (point-max))))
  15926. (save-excursion
  15927. (goto-char end)
  15928. (setq l2 (org-current-line))
  15929. (goto-char beg)
  15930. (beginning-of-line 1)
  15931. (setq l (1- (org-current-line)))
  15932. (if (org-on-heading-p)
  15933. ;; We already have headlines, de-star them
  15934. (while (< (setq l (1+ l)) l2)
  15935. (when (org-on-heading-p t)
  15936. (and (looking-at outline-regexp) (replace-match "")))
  15937. (beginning-of-line 2))
  15938. (setq itemp (org-at-item-p))
  15939. (let* ((stars
  15940. (if nstars
  15941. (make-string (prefix-numeric-value current-prefix-arg)
  15942. ?*)
  15943. (save-excursion
  15944. (if (re-search-backward org-complex-heading-regexp nil t)
  15945. (match-string 1) ""))))
  15946. (add-stars (cond (nstars "")
  15947. ((equal stars "") "*")
  15948. (org-odd-levels-only "**")
  15949. (t "*")))
  15950. (rpl (concat stars add-stars " ")))
  15951. (while (< (setq l (1+ l)) l2)
  15952. (if itemp
  15953. (and (org-at-item-p) (replace-match rpl t t))
  15954. (unless (org-on-heading-p)
  15955. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15956. (replace-match (concat rpl (match-string 2))))))
  15957. (beginning-of-line 2)))))))
  15958. (defun org-meta-return (&optional arg)
  15959. "Insert a new heading or wrap a region in a table.
  15960. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15961. See the individual commands for more information."
  15962. (interactive "P")
  15963. (cond
  15964. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15965. ((org-at-table-p)
  15966. (call-interactively 'org-table-wrap-region))
  15967. (t (call-interactively 'org-insert-heading))))
  15968. ;;; Menu entries
  15969. ;; Define the Org-mode menus
  15970. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15971. '("Tbl"
  15972. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15973. ["Next Field" org-cycle (org-at-table-p)]
  15974. ["Previous Field" org-shifttab (org-at-table-p)]
  15975. ["Next Row" org-return (org-at-table-p)]
  15976. "--"
  15977. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15978. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15979. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15980. "--"
  15981. ("Column"
  15982. ["Move Column Left" org-metaleft (org-at-table-p)]
  15983. ["Move Column Right" org-metaright (org-at-table-p)]
  15984. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15985. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15986. ("Row"
  15987. ["Move Row Up" org-metaup (org-at-table-p)]
  15988. ["Move Row Down" org-metadown (org-at-table-p)]
  15989. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15990. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15991. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15992. "--"
  15993. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15994. ("Rectangle"
  15995. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15996. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15997. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15998. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15999. "--"
  16000. ("Calculate"
  16001. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16002. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16003. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16004. "--"
  16005. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16006. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16007. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16008. "--"
  16009. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16010. "--"
  16011. ["Sum Column/Rectangle" org-table-sum
  16012. (or (org-at-table-p) (org-region-active-p))]
  16013. ["Which Column?" org-table-current-column (org-at-table-p)])
  16014. ["Debug Formulas"
  16015. org-table-toggle-formula-debugger
  16016. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16017. ["Show Col/Row Numbers"
  16018. org-table-toggle-coordinate-overlays
  16019. :style toggle
  16020. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16021. "--"
  16022. ["Create" org-table-create (and (not (org-at-table-p))
  16023. org-enable-table-editor)]
  16024. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16025. ["Import from File" org-table-import (not (org-at-table-p))]
  16026. ["Export to File" org-table-export (org-at-table-p)]
  16027. "--"
  16028. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16029. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16030. '("Org"
  16031. ("Show/Hide"
  16032. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16033. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16034. ["Sparse Tree..." org-sparse-tree t]
  16035. ["Reveal Context" org-reveal t]
  16036. ["Show All" show-all t]
  16037. "--"
  16038. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16039. "--"
  16040. ["New Heading" org-insert-heading t]
  16041. ("Navigate Headings"
  16042. ["Up" outline-up-heading t]
  16043. ["Next" outline-next-visible-heading t]
  16044. ["Previous" outline-previous-visible-heading t]
  16045. ["Next Same Level" outline-forward-same-level t]
  16046. ["Previous Same Level" outline-backward-same-level t]
  16047. "--"
  16048. ["Jump" org-goto t])
  16049. ("Edit Structure"
  16050. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16051. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16052. "--"
  16053. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16054. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16055. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16056. "--"
  16057. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16058. "--"
  16059. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16060. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16061. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16062. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16063. "--"
  16064. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16065. "--"
  16066. ["Convert to odd levels" org-convert-to-odd-levels t]
  16067. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16068. ("Editing"
  16069. ["Emphasis..." org-emphasize t]
  16070. ["Edit Source Example" org-edit-special t]
  16071. "--"
  16072. ["Footnote new/jump" org-footnote-action t]
  16073. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16074. ("Archive"
  16075. ["Archive (default method)" org-archive-subtree-default t]
  16076. "--"
  16077. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16078. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16079. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16080. )
  16081. "--"
  16082. ("Hyperlinks"
  16083. ["Store Link (Global)" org-store-link t]
  16084. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16085. ["Insert Link" org-insert-link t]
  16086. ["Follow Link" org-open-at-point t]
  16087. "--"
  16088. ["Next link" org-next-link t]
  16089. ["Previous link" org-previous-link t]
  16090. "--"
  16091. ["Descriptive Links"
  16092. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16093. :style radio
  16094. :selected (member '(org-link) buffer-invisibility-spec)]
  16095. ["Literal Links"
  16096. (progn
  16097. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16098. :style radio
  16099. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16100. "--"
  16101. ("TODO Lists"
  16102. ["TODO/DONE/-" org-todo t]
  16103. ("Select keyword"
  16104. ["Next keyword" org-shiftright (org-on-heading-p)]
  16105. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16106. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16107. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16108. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16109. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16110. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16111. "--"
  16112. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16113. :selected org-enforce-todo-dependencies :style toggle :active t]
  16114. "Settings for tree at point"
  16115. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16116. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  16117. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16118. ["Do Children parallel" org-toggle-ordered-property :style radio
  16119. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  16120. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16121. "--"
  16122. ["Set Priority" org-priority t]
  16123. ["Priority Up" org-shiftup t]
  16124. ["Priority Down" org-shiftdown t]
  16125. "--"
  16126. ["Get news from all feeds" org-feed-update-all t]
  16127. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16128. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16129. ("TAGS and Properties"
  16130. ["Set Tags" org-set-tags-command t]
  16131. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16132. "--"
  16133. ["Set property" org-set-property t]
  16134. ["Column view of properties" org-columns t]
  16135. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16136. ("Dates and Scheduling"
  16137. ["Timestamp" org-time-stamp t]
  16138. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16139. ("Change Date"
  16140. ["1 Day Later" org-shiftright t]
  16141. ["1 Day Earlier" org-shiftleft t]
  16142. ["1 ... Later" org-shiftup t]
  16143. ["1 ... Earlier" org-shiftdown t])
  16144. ["Compute Time Range" org-evaluate-time-range t]
  16145. ["Schedule Item" org-schedule t]
  16146. ["Deadline" org-deadline t]
  16147. "--"
  16148. ["Custom time format" org-toggle-time-stamp-overlays
  16149. :style radio :selected org-display-custom-times]
  16150. "--"
  16151. ["Goto Calendar" org-goto-calendar t]
  16152. ["Date from Calendar" org-date-from-calendar t]
  16153. "--"
  16154. ["Start/Restart Timer" org-timer-start t]
  16155. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16156. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16157. ["Insert Timer String" org-timer t]
  16158. ["Insert Timer Item" org-timer-item t])
  16159. ("Logging work"
  16160. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16161. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16162. ["Clock out" org-clock-out t]
  16163. ["Clock cancel" org-clock-cancel t]
  16164. "--"
  16165. ["Mark as default task" org-clock-mark-default-task t]
  16166. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16167. ["Goto running clock" org-clock-goto t]
  16168. "--"
  16169. ["Display times" org-clock-display t]
  16170. ["Create clock table" org-clock-report t]
  16171. "--"
  16172. ["Record DONE time"
  16173. (progn (setq org-log-done (not org-log-done))
  16174. (message "Switching to %s will %s record a timestamp"
  16175. (car org-done-keywords)
  16176. (if org-log-done "automatically" "not")))
  16177. :style toggle :selected org-log-done])
  16178. "--"
  16179. ["Agenda Command..." org-agenda t]
  16180. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16181. ("File List for Agenda")
  16182. ("Special views current file"
  16183. ["TODO Tree" org-show-todo-tree t]
  16184. ["Check Deadlines" org-check-deadlines t]
  16185. ["Timeline" org-timeline t]
  16186. ["Tags/Property tree" org-match-sparse-tree t])
  16187. "--"
  16188. ["Export/Publish..." org-export t]
  16189. ("LaTeX"
  16190. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16191. :selected org-cdlatex-mode]
  16192. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16193. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16194. ["Modify math symbol" org-cdlatex-math-modify
  16195. (org-inside-LaTeX-fragment-p)]
  16196. ["Insert citation" org-reftex-citation t]
  16197. "--"
  16198. ["Template for BEAMER" org-insert-beamer-options-template t])
  16199. "--"
  16200. ("MobileOrg"
  16201. ["Push Files and Views" org-mobile-push t]
  16202. ["Get Captured and Flagged" org-mobile-pull t]
  16203. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16204. "--"
  16205. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16206. "--"
  16207. ("Documentation"
  16208. ["Show Version" org-version t]
  16209. ["Info Documentation" org-info t])
  16210. ("Customize"
  16211. ["Browse Org Group" org-customize t]
  16212. "--"
  16213. ["Expand This Menu" org-create-customize-menu
  16214. (fboundp 'customize-menu-create)])
  16215. ["Send bug report" org-submit-bug-report t]
  16216. "--"
  16217. ("Refresh/Reload"
  16218. ["Refresh setup current buffer" org-mode-restart t]
  16219. ["Reload Org (after update)" org-reload t]
  16220. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16221. ))
  16222. (defun org-info (&optional node)
  16223. "Read documentation for Org-mode in the info system.
  16224. With optional NODE, go directly to that node."
  16225. (interactive)
  16226. (info (format "(org)%s" (or node ""))))
  16227. ;;;###autoload
  16228. (defun org-submit-bug-report ()
  16229. "Submit a bug report on Org-mode via mail.
  16230. Don't hesitate to report any problems or inaccurate documentation.
  16231. If you don't have setup sending mail from (X)Emacs, please copy the
  16232. output buffer into your mail program, as it gives us important
  16233. information about your Org-mode version and configuration."
  16234. (interactive)
  16235. (require 'reporter)
  16236. (org-load-modules-maybe)
  16237. (org-require-autoloaded-modules)
  16238. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16239. (reporter-submit-bug-report
  16240. "emacs-orgmode@gnu.org"
  16241. (org-version)
  16242. (let (list)
  16243. (save-window-excursion
  16244. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16245. (delete-other-windows)
  16246. (erase-buffer)
  16247. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16248. We would like to add your full Org-mode and Outline configuration to the
  16249. bug report. This greatly simplifies the work of the maintainer and
  16250. other experts on the mailing list.
  16251. HOWEVER, some variables you have customized may contain private
  16252. information. The names of customers, colleagues, or friends, might
  16253. appear in the form of file names, tags, todo states, or search strings.
  16254. If you answer yes to the prompt, you might want to check and remove
  16255. such private information before sending the email.")
  16256. (add-text-properties (point-min) (point-max) '(face org-warning))
  16257. (when (yes-or-no-p "Include your Org-mode configuration ")
  16258. (mapatoms
  16259. (lambda (v)
  16260. (and (boundp v)
  16261. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16262. (or (and (symbol-value v)
  16263. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16264. (and
  16265. (get v 'custom-type) (get v 'standard-value)
  16266. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16267. (push v list)))))
  16268. (kill-buffer (get-buffer "*Warn about privacy*"))
  16269. list))
  16270. nil nil
  16271. "Remember to cover the basics, that is, what you expected to happen and
  16272. what in fact did happen. You don't know how to make a good report? See
  16273. http://orgmode.org/manual/Feedback.html#Feedback
  16274. Your bug report will be posted to the Org-mode mailing list.
  16275. ------------------------------------------------------------------------")
  16276. (save-excursion
  16277. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16278. (replace-match "\\1Bug: \\3 [\\2]")))))
  16279. (defun org-install-agenda-files-menu ()
  16280. (let ((bl (buffer-list)))
  16281. (save-excursion
  16282. (while bl
  16283. (set-buffer (pop bl))
  16284. (if (org-mode-p) (setq bl nil)))
  16285. (when (org-mode-p)
  16286. (easy-menu-change
  16287. '("Org") "File List for Agenda"
  16288. (append
  16289. (list
  16290. ["Edit File List" (org-edit-agenda-file-list) t]
  16291. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16292. ["Remove Current File from List" org-remove-file t]
  16293. ["Cycle through agenda files" org-cycle-agenda-files t]
  16294. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16295. "--")
  16296. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16297. ;;;; Documentation
  16298. ;;;###autoload
  16299. (defun org-require-autoloaded-modules ()
  16300. (interactive)
  16301. (mapc 'require
  16302. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16303. org-docbook org-exp org-html org-icalendar
  16304. org-id org-latex
  16305. org-publish org-remember org-table
  16306. org-timer org-xoxo)))
  16307. ;;;###autoload
  16308. (defun org-reload (&optional uncompiled)
  16309. "Reload all org lisp files.
  16310. With prefix arg UNCOMPILED, load the uncompiled versions."
  16311. (interactive "P")
  16312. (require 'find-func)
  16313. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16314. (dir-org (file-name-directory (org-find-library-name "org")))
  16315. (dir-org-contrib (ignore-errors
  16316. (file-name-directory
  16317. (org-find-library-name "org-contribdir"))))
  16318. (babel-files
  16319. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16320. (append (list nil "comint" "eval" "exp" "keys"
  16321. "lob" "ref" "table" "tangle")
  16322. (delq nil
  16323. (mapcar
  16324. (lambda (lang)
  16325. (when (cdr lang) (symbol-name (car lang))))
  16326. org-babel-load-languages)))))
  16327. (files
  16328. (append (directory-files dir-org t file-re)
  16329. babel-files
  16330. (and dir-org-contrib
  16331. (directory-files dir-org-contrib t file-re))))
  16332. (remove-re (concat (if (featurep 'xemacs)
  16333. "org-colview" "org-colview-xemacs")
  16334. "\\'")))
  16335. (setq files (mapcar 'file-name-sans-extension files))
  16336. (setq files (mapcar
  16337. (lambda (x) (if (string-match remove-re x) nil x))
  16338. files))
  16339. (setq files (delq nil files))
  16340. (mapc
  16341. (lambda (f)
  16342. (when (featurep (intern (file-name-nondirectory f)))
  16343. (if (and (not uncompiled)
  16344. (file-exists-p (concat f ".elc")))
  16345. (load (concat f ".elc") nil nil t)
  16346. (load (concat f ".el") nil nil t))))
  16347. files))
  16348. (org-version))
  16349. ;;;###autoload
  16350. (defun org-customize ()
  16351. "Call the customize function with org as argument."
  16352. (interactive)
  16353. (org-load-modules-maybe)
  16354. (org-require-autoloaded-modules)
  16355. (customize-browse 'org))
  16356. (defun org-create-customize-menu ()
  16357. "Create a full customization menu for Org-mode, insert it into the menu."
  16358. (interactive)
  16359. (org-load-modules-maybe)
  16360. (org-require-autoloaded-modules)
  16361. (if (fboundp 'customize-menu-create)
  16362. (progn
  16363. (easy-menu-change
  16364. '("Org") "Customize"
  16365. `(["Browse Org group" org-customize t]
  16366. "--"
  16367. ,(customize-menu-create 'org)
  16368. ["Set" Custom-set t]
  16369. ["Save" Custom-save t]
  16370. ["Reset to Current" Custom-reset-current t]
  16371. ["Reset to Saved" Custom-reset-saved t]
  16372. ["Reset to Standard Settings" Custom-reset-standard t]))
  16373. (message "\"Org\"-menu now contains full customization menu"))
  16374. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16375. ;;;; Miscellaneous stuff
  16376. ;;; Generally useful functions
  16377. (defun org-get-at-bol (property)
  16378. "Get text property PROPERTY at beginning of line."
  16379. (get-text-property (point-at-bol) property))
  16380. (defun org-find-text-property-in-string (prop s)
  16381. "Return the first non-nil value of property PROP in string S."
  16382. (or (get-text-property 0 prop s)
  16383. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16384. prop s)))
  16385. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16386. "Display the given MESSAGE as a warning."
  16387. (if (fboundp 'display-warning)
  16388. (display-warning 'org message
  16389. (if (featurep 'xemacs) 'warning :warning))
  16390. (let ((buf (get-buffer-create "*Org warnings*")))
  16391. (with-current-buffer buf
  16392. (goto-char (point-max))
  16393. (insert "Warning (Org): " message)
  16394. (unless (bolp)
  16395. (newline)))
  16396. (display-buffer buf)
  16397. (sit-for 0))))
  16398. (defun org-in-commented-line ()
  16399. "Is point in a line starting with `#'?"
  16400. (equal (char-after (point-at-bol)) ?#))
  16401. (defun org-in-indented-comment-line ()
  16402. "Is point in a line starting with `#' after some white space?"
  16403. (save-excursion
  16404. (save-match-data
  16405. (goto-char (point-at-bol))
  16406. (looking-at "[ \t]*#"))))
  16407. (defun org-in-verbatim-emphasis ()
  16408. (save-match-data
  16409. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16410. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16411. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16412. (if (and marker (marker-buffer marker)
  16413. (buffer-live-p (marker-buffer marker)))
  16414. (progn
  16415. (switch-to-buffer (marker-buffer marker))
  16416. (if (or (> marker (point-max)) (< marker (point-min)))
  16417. (widen))
  16418. (goto-char marker)
  16419. (org-show-context 'org-goto))
  16420. (if bookmark
  16421. (bookmark-jump bookmark)
  16422. (error "Cannot find location"))))
  16423. (defun org-quote-csv-field (s)
  16424. "Quote field for inclusion in CSV material."
  16425. (if (string-match "[\",]" s)
  16426. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16427. s))
  16428. (defun org-plist-delete (plist property)
  16429. "Delete PROPERTY from PLIST.
  16430. This is in contrast to merely setting it to 0."
  16431. (let (p)
  16432. (while plist
  16433. (if (not (eq property (car plist)))
  16434. (setq p (plist-put p (car plist) (nth 1 plist))))
  16435. (setq plist (cddr plist)))
  16436. p))
  16437. (defun org-force-self-insert (N)
  16438. "Needed to enforce self-insert under remapping."
  16439. (interactive "p")
  16440. (self-insert-command N))
  16441. (defun org-string-width (s)
  16442. "Compute width of string, ignoring invisible characters.
  16443. This ignores character with invisibility property `org-link', and also
  16444. characters with property `org-cwidth', because these will become invisible
  16445. upon the next fontification round."
  16446. (let (b l)
  16447. (when (or (eq t buffer-invisibility-spec)
  16448. (assq 'org-link buffer-invisibility-spec))
  16449. (while (setq b (text-property-any 0 (length s)
  16450. 'invisible 'org-link s))
  16451. (setq s (concat (substring s 0 b)
  16452. (substring s (or (next-single-property-change
  16453. b 'invisible s) (length s)))))))
  16454. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16455. (setq s (concat (substring s 0 b)
  16456. (substring s (or (next-single-property-change
  16457. b 'org-cwidth s) (length s))))))
  16458. (setq l (string-width s) b -1)
  16459. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16460. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16461. l))
  16462. (defun org-shorten-string (s maxlength)
  16463. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  16464. If the string is shorter or has length MAXLENGTH, just return the
  16465. original string. If it is longer, the functions finds a space in the
  16466. string, breaks this string off at that locations and adds three dots
  16467. as ellipsis. Including the ellipsis, the string will not be longer
  16468. than MAXLENGTH. If finding a good breaking point in the string does
  16469. not work, the string is just chopped off in the middle of a word
  16470. if necessary."
  16471. (if (<= (length s) maxlength)
  16472. s
  16473. (let* ((n (max (- maxlength 4) 1))
  16474. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  16475. (if (string-match re s)
  16476. (concat (match-string 1 s) "...")
  16477. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  16478. (defun org-get-indentation (&optional line)
  16479. "Get the indentation of the current line, interpreting tabs.
  16480. When LINE is given, assume it represents a line and compute its indentation."
  16481. (if line
  16482. (if (string-match "^ *" (org-remove-tabs line))
  16483. (match-end 0))
  16484. (save-excursion
  16485. (beginning-of-line 1)
  16486. (skip-chars-forward " \t")
  16487. (current-column))))
  16488. (defun org-remove-tabs (s &optional width)
  16489. "Replace tabulators in S with spaces.
  16490. Assumes that s is a single line, starting in column 0."
  16491. (setq width (or width tab-width))
  16492. (while (string-match "\t" s)
  16493. (setq s (replace-match
  16494. (make-string
  16495. (- (* width (/ (+ (match-beginning 0) width) width))
  16496. (match-beginning 0)) ?\ )
  16497. t t s)))
  16498. s)
  16499. (defun org-fix-indentation (line ind)
  16500. "Fix indentation in LINE.
  16501. IND is a cons cell with target and minimum indentation.
  16502. If the current indentation in LINE is smaller than the minimum,
  16503. leave it alone. If it is larger than ind, set it to the target."
  16504. (let* ((l (org-remove-tabs line))
  16505. (i (org-get-indentation l))
  16506. (i1 (car ind)) (i2 (cdr ind)))
  16507. (if (>= i i2) (setq l (substring line i2)))
  16508. (if (> i1 0)
  16509. (concat (make-string i1 ?\ ) l)
  16510. l)))
  16511. (defun org-remove-indentation (code &optional n)
  16512. "Remove the maximum common indentation from the lines in CODE.
  16513. N may optionally be the number of spaces to remove."
  16514. (with-temp-buffer
  16515. (insert code)
  16516. (org-do-remove-indentation n)
  16517. (buffer-string)))
  16518. (defun org-do-remove-indentation (&optional n)
  16519. "Remove the maximum common indentation from the buffer."
  16520. (untabify (point-min) (point-max))
  16521. (let ((min 10000) re)
  16522. (if n
  16523. (setq min n)
  16524. (goto-char (point-min))
  16525. (while (re-search-forward "^ *[^ \n]" nil t)
  16526. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16527. (unless (or (= min 0) (= min 10000))
  16528. (setq re (format "^ \\{%d\\}" min))
  16529. (goto-char (point-min))
  16530. (while (re-search-forward re nil t)
  16531. (replace-match "")
  16532. (end-of-line 1))
  16533. min)))
  16534. (defun org-fill-template (template alist)
  16535. "Find each %key of ALIST in TEMPLATE and replace it."
  16536. (let ((case-fold-search nil)
  16537. entry key value)
  16538. (setq alist (sort (copy-sequence alist)
  16539. (lambda (a b) (< (length (car a)) (length (car b))))))
  16540. (while (setq entry (pop alist))
  16541. (setq template
  16542. (replace-regexp-in-string
  16543. (concat "%" (regexp-quote (car entry)))
  16544. (cdr entry) template t t)))
  16545. template))
  16546. (defun org-base-buffer (buffer)
  16547. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16548. (if (not buffer)
  16549. buffer
  16550. (or (buffer-base-buffer buffer)
  16551. buffer)))
  16552. (defun org-trim (s)
  16553. "Remove whitespace at beginning and end of string."
  16554. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16555. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16556. s)
  16557. (defun org-wrap (string &optional width lines)
  16558. "Wrap string to either a number of lines, or a width in characters.
  16559. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16560. that costs. If there is a word longer than WIDTH, the text is actually
  16561. wrapped to the length of that word.
  16562. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16563. many lines, whatever width that takes.
  16564. The return value is a list of lines, without newlines at the end."
  16565. (let* ((words (org-split-string string "[ \t\n]+"))
  16566. (maxword (apply 'max (mapcar 'org-string-width words)))
  16567. w ll)
  16568. (cond (width
  16569. (org-do-wrap words (max maxword width)))
  16570. (lines
  16571. (setq w maxword)
  16572. (setq ll (org-do-wrap words maxword))
  16573. (if (<= (length ll) lines)
  16574. ll
  16575. (setq ll words)
  16576. (while (> (length ll) lines)
  16577. (setq w (1+ w))
  16578. (setq ll (org-do-wrap words w)))
  16579. ll))
  16580. (t (error "Cannot wrap this")))))
  16581. (defun org-do-wrap (words width)
  16582. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16583. (let (lines line)
  16584. (while words
  16585. (setq line (pop words))
  16586. (while (and words (< (+ (length line) (length (car words))) width))
  16587. (setq line (concat line " " (pop words))))
  16588. (setq lines (push line lines)))
  16589. (nreverse lines)))
  16590. (defun org-split-string (string &optional separators)
  16591. "Splits STRING into substrings at SEPARATORS.
  16592. No empty strings are returned if there are matches at the beginning
  16593. and end of string."
  16594. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16595. (start 0)
  16596. notfirst
  16597. (list nil))
  16598. (while (and (string-match rexp string
  16599. (if (and notfirst
  16600. (= start (match-beginning 0))
  16601. (< start (length string)))
  16602. (1+ start) start))
  16603. (< (match-beginning 0) (length string)))
  16604. (setq notfirst t)
  16605. (or (eq (match-beginning 0) 0)
  16606. (and (eq (match-beginning 0) (match-end 0))
  16607. (eq (match-beginning 0) start))
  16608. (setq list
  16609. (cons (substring string start (match-beginning 0))
  16610. list)))
  16611. (setq start (match-end 0)))
  16612. (or (eq start (length string))
  16613. (setq list
  16614. (cons (substring string start)
  16615. list)))
  16616. (nreverse list)))
  16617. (defun org-quote-vert (s)
  16618. "Replace \"|\" with \"\\vert\"."
  16619. (while (string-match "|" s)
  16620. (setq s (replace-match "\\vert" t t s)))
  16621. s)
  16622. (defun org-uuidgen-p (s)
  16623. "Is S an ID created by UUIDGEN?"
  16624. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16625. (defun org-context ()
  16626. "Return a list of contexts of the current cursor position.
  16627. If several contexts apply, all are returned.
  16628. Each context entry is a list with a symbol naming the context, and
  16629. two positions indicating start and end of the context. Possible
  16630. contexts are:
  16631. :headline anywhere in a headline
  16632. :headline-stars on the leading stars in a headline
  16633. :todo-keyword on a TODO keyword (including DONE) in a headline
  16634. :tags on the TAGS in a headline
  16635. :priority on the priority cookie in a headline
  16636. :item on the first line of a plain list item
  16637. :item-bullet on the bullet/number of a plain list item
  16638. :checkbox on the checkbox in a plain list item
  16639. :table in an org-mode table
  16640. :table-special on a special filed in a table
  16641. :table-table in a table.el table
  16642. :link on a hyperlink
  16643. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16644. :target on a <<target>>
  16645. :radio-target on a <<<radio-target>>>
  16646. :latex-fragment on a LaTeX fragment
  16647. :latex-preview on a LaTeX fragment with overlayed preview image
  16648. This function expects the position to be visible because it uses font-lock
  16649. faces as a help to recognize the following contexts: :table-special, :link,
  16650. and :keyword."
  16651. (let* ((f (get-text-property (point) 'face))
  16652. (faces (if (listp f) f (list f)))
  16653. (p (point)) clist o)
  16654. ;; First the large context
  16655. (cond
  16656. ((org-on-heading-p t)
  16657. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16658. (when (progn
  16659. (beginning-of-line 1)
  16660. (looking-at org-todo-line-tags-regexp))
  16661. (push (org-point-in-group p 1 :headline-stars) clist)
  16662. (push (org-point-in-group p 2 :todo-keyword) clist)
  16663. (push (org-point-in-group p 4 :tags) clist))
  16664. (goto-char p)
  16665. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16666. (if (looking-at "\\[#[A-Z0-9]\\]")
  16667. (push (org-point-in-group p 0 :priority) clist)))
  16668. ((org-at-item-p)
  16669. (push (org-point-in-group p 2 :item-bullet) clist)
  16670. (push (list :item (point-at-bol)
  16671. (save-excursion (org-end-of-item) (point)))
  16672. clist)
  16673. (and (org-at-item-checkbox-p)
  16674. (push (org-point-in-group p 0 :checkbox) clist)))
  16675. ((org-at-table-p)
  16676. (push (list :table (org-table-begin) (org-table-end)) clist)
  16677. (if (memq 'org-formula faces)
  16678. (push (list :table-special
  16679. (previous-single-property-change p 'face)
  16680. (next-single-property-change p 'face)) clist)))
  16681. ((org-at-table-p 'any)
  16682. (push (list :table-table) clist)))
  16683. (goto-char p)
  16684. ;; Now the small context
  16685. (cond
  16686. ((org-at-timestamp-p)
  16687. (push (org-point-in-group p 0 :timestamp) clist))
  16688. ((memq 'org-link faces)
  16689. (push (list :link
  16690. (previous-single-property-change p 'face)
  16691. (next-single-property-change p 'face)) clist))
  16692. ((memq 'org-special-keyword faces)
  16693. (push (list :keyword
  16694. (previous-single-property-change p 'face)
  16695. (next-single-property-change p 'face)) clist))
  16696. ((org-on-target-p)
  16697. (push (org-point-in-group p 0 :target) clist)
  16698. (goto-char (1- (match-beginning 0)))
  16699. (if (looking-at org-radio-target-regexp)
  16700. (push (org-point-in-group p 0 :radio-target) clist))
  16701. (goto-char p))
  16702. ((setq o (car (delq nil
  16703. (mapcar
  16704. (lambda (x)
  16705. (if (memq x org-latex-fragment-image-overlays) x))
  16706. (overlays-at (point))))))
  16707. (push (list :latex-fragment
  16708. (overlay-start o) (overlay-end o)) clist)
  16709. (push (list :latex-preview
  16710. (overlay-start o) (overlay-end o)) clist))
  16711. ((org-inside-LaTeX-fragment-p)
  16712. ;; FIXME: positions wrong.
  16713. (push (list :latex-fragment (point) (point)) clist)))
  16714. (setq clist (nreverse (delq nil clist)))
  16715. clist))
  16716. ;; FIXME: Compare with at-regexp-p Do we need both?
  16717. (defun org-in-regexp (re &optional nlines visually)
  16718. "Check if point is inside a match of regexp.
  16719. Normally only the current line is checked, but you can include NLINES extra
  16720. lines both before and after point into the search.
  16721. If VISUALLY is set, require that the cursor is not after the match but
  16722. really on, so that the block visually is on the match."
  16723. (catch 'exit
  16724. (let ((pos (point))
  16725. (eol (point-at-eol (+ 1 (or nlines 0))))
  16726. (inc (if visually 1 0)))
  16727. (save-excursion
  16728. (beginning-of-line (- 1 (or nlines 0)))
  16729. (while (re-search-forward re eol t)
  16730. (if (and (<= (match-beginning 0) pos)
  16731. (>= (+ inc (match-end 0)) pos))
  16732. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16733. (defun org-at-regexp-p (regexp)
  16734. "Is point inside a match of REGEXP in the current line?"
  16735. (catch 'exit
  16736. (save-excursion
  16737. (let ((pos (point)) (end (point-at-eol)))
  16738. (beginning-of-line 1)
  16739. (while (re-search-forward regexp end t)
  16740. (if (and (<= (match-beginning 0) pos)
  16741. (>= (match-end 0) pos))
  16742. (throw 'exit t)))
  16743. nil))))
  16744. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  16745. "Return t if the current point is between matches of START-RE and END-RE.
  16746. This will also return t if point is on one of the two matches or
  16747. in an unfinished block. END-RE can be a string or a form
  16748. returning a string.
  16749. An optional third argument bounds the search for START-RE. It
  16750. defaults to previous heading or `point-min'."
  16751. (let ((pos (point))
  16752. (limit (or bound (save-excursion (outline-previous-heading)))))
  16753. (save-excursion
  16754. ;; we're on a block when point is on start-re...
  16755. (or (org-at-regexp-p start-re)
  16756. ;; ... or start-re can be found above...
  16757. (and (re-search-backward start-re limit t)
  16758. ;; ... but no end-re between start-re and point.
  16759. (not (re-search-forward (eval end-re) pos t)))))))
  16760. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16761. "Call `multi-occur' with buffers for all agenda files."
  16762. (interactive "sOrg-files matching: \np")
  16763. (let* ((files (org-agenda-files))
  16764. (tnames (mapcar 'file-truename files))
  16765. (extra org-agenda-text-search-extra-files)
  16766. f)
  16767. (when (eq (car extra) 'agenda-archives)
  16768. (setq extra (cdr extra))
  16769. (setq files (org-add-archive-files files)))
  16770. (while (setq f (pop extra))
  16771. (unless (member (file-truename f) tnames)
  16772. (add-to-list 'files f 'append)
  16773. (add-to-list 'tnames (file-truename f) 'append)))
  16774. (multi-occur
  16775. (mapcar (lambda (x)
  16776. (with-current-buffer
  16777. (or (get-file-buffer x) (find-file-noselect x))
  16778. (widen)
  16779. (current-buffer)))
  16780. files)
  16781. regexp)))
  16782. (if (boundp 'occur-mode-find-occurrence-hook)
  16783. ;; Emacs 23
  16784. (add-hook 'occur-mode-find-occurrence-hook
  16785. (lambda ()
  16786. (when (org-mode-p)
  16787. (org-reveal))))
  16788. ;; Emacs 22
  16789. (defadvice occur-mode-goto-occurrence
  16790. (after org-occur-reveal activate)
  16791. (and (org-mode-p) (org-reveal)))
  16792. (defadvice occur-mode-goto-occurrence-other-window
  16793. (after org-occur-reveal activate)
  16794. (and (org-mode-p) (org-reveal)))
  16795. (defadvice occur-mode-display-occurrence
  16796. (after org-occur-reveal activate)
  16797. (when (org-mode-p)
  16798. (let ((pos (occur-mode-find-occurrence)))
  16799. (with-current-buffer (marker-buffer pos)
  16800. (save-excursion
  16801. (goto-char pos)
  16802. (org-reveal)))))))
  16803. (defun org-occur-link-in-agenda-files ()
  16804. "Create a link and search for it in the agendas.
  16805. The link is not stored in `org-stored-links', it is just created
  16806. for the search purpose."
  16807. (interactive)
  16808. (let ((link (condition-case nil
  16809. (org-store-link nil)
  16810. (error "Unable to create a link to here"))))
  16811. (org-occur-in-agenda-files (regexp-quote link))))
  16812. (defun org-uniquify (list)
  16813. "Remove duplicate elements from LIST."
  16814. (let (res)
  16815. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16816. res))
  16817. (defun org-delete-all (elts list)
  16818. "Remove all elements in ELTS from LIST."
  16819. (while elts
  16820. (setq list (delete (pop elts) list)))
  16821. list)
  16822. (defun org-count (cl-item cl-seq)
  16823. "Count the number of occurrences of ITEM in SEQ.
  16824. Taken from `count' in cl-seq.el with all keyword arguments removed."
  16825. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  16826. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  16827. (while (< cl-start cl-end)
  16828. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  16829. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  16830. (setq cl-start (1+ cl-start)))
  16831. cl-count))
  16832. (defun org-remove-if (predicate seq)
  16833. "Remove everything from SEQ that fulfills PREDICATE."
  16834. (let (res e)
  16835. (while seq
  16836. (setq e (pop seq))
  16837. (if (not (funcall predicate e)) (push e res)))
  16838. (nreverse res)))
  16839. (defun org-remove-if-not (predicate seq)
  16840. "Remove everything from SEQ that does not fulfill PREDICATE."
  16841. (let (res e)
  16842. (while seq
  16843. (setq e (pop seq))
  16844. (if (funcall predicate e) (push e res)))
  16845. (nreverse res)))
  16846. (defun org-back-over-empty-lines ()
  16847. "Move backwards over whitespace, to the beginning of the first empty line.
  16848. Returns the number of empty lines passed."
  16849. (let ((pos (point)))
  16850. (skip-chars-backward " \t\n\r")
  16851. (beginning-of-line 2)
  16852. (goto-char (min (point) pos))
  16853. (count-lines (point) pos)))
  16854. (defun org-skip-whitespace ()
  16855. (skip-chars-forward " \t\n\r"))
  16856. (defun org-point-in-group (point group &optional context)
  16857. "Check if POINT is in match-group GROUP.
  16858. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16859. match. If the match group does not exist or point is not inside it,
  16860. return nil."
  16861. (and (match-beginning group)
  16862. (>= point (match-beginning group))
  16863. (<= point (match-end group))
  16864. (if context
  16865. (list context (match-beginning group) (match-end group))
  16866. t)))
  16867. (defun org-switch-to-buffer-other-window (&rest args)
  16868. "Switch to buffer in a second window on the current frame.
  16869. In particular, do not allow pop-up frames.
  16870. Returns the newly created buffer."
  16871. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16872. special-display-function)
  16873. (apply 'switch-to-buffer-other-window args)))
  16874. (defun org-combine-plists (&rest plists)
  16875. "Create a single property list from all plists in PLISTS.
  16876. The process starts by copying the first list, and then setting properties
  16877. from the other lists. Settings in the last list are the most significant
  16878. ones and overrule settings in the other lists."
  16879. (let ((rtn (copy-sequence (pop plists)))
  16880. p v ls)
  16881. (while plists
  16882. (setq ls (pop plists))
  16883. (while ls
  16884. (setq p (pop ls) v (pop ls))
  16885. (setq rtn (plist-put rtn p v))))
  16886. rtn))
  16887. (defun org-move-line-down (arg)
  16888. "Move the current line down. With prefix argument, move it past ARG lines."
  16889. (interactive "p")
  16890. (let ((col (current-column))
  16891. beg end pos)
  16892. (beginning-of-line 1) (setq beg (point))
  16893. (beginning-of-line 2) (setq end (point))
  16894. (beginning-of-line (+ 1 arg))
  16895. (setq pos (move-marker (make-marker) (point)))
  16896. (insert (delete-and-extract-region beg end))
  16897. (goto-char pos)
  16898. (org-move-to-column col)))
  16899. (defun org-move-line-up (arg)
  16900. "Move the current line up. With prefix argument, move it past ARG lines."
  16901. (interactive "p")
  16902. (let ((col (current-column))
  16903. beg end pos)
  16904. (beginning-of-line 1) (setq beg (point))
  16905. (beginning-of-line 2) (setq end (point))
  16906. (beginning-of-line (- arg))
  16907. (setq pos (move-marker (make-marker) (point)))
  16908. (insert (delete-and-extract-region beg end))
  16909. (goto-char pos)
  16910. (org-move-to-column col)))
  16911. (defun org-replace-escapes (string table)
  16912. "Replace %-escapes in STRING with values in TABLE.
  16913. TABLE is an association list with keys like \"%a\" and string values.
  16914. The sequences in STRING may contain normal field width and padding information,
  16915. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16916. so values can contain further %-escapes if they are define later in TABLE."
  16917. (let ((tbl (copy-alist table))
  16918. (case-fold-search nil)
  16919. (pchg 0)
  16920. e re rpl)
  16921. (while (setq e (pop tbl))
  16922. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16923. (when (and (cdr e) (string-match re (cdr e)))
  16924. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16925. (safe "SREF"))
  16926. (add-text-properties 0 3 (list 'sref sref) safe)
  16927. (setcdr e (replace-match safe t t (cdr e)))))
  16928. (while (string-match re string)
  16929. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16930. (cdr e)))
  16931. (setq string (replace-match rpl t t string))))
  16932. (while (setq pchg (next-property-change pchg string))
  16933. (let ((sref (get-text-property pchg 'sref string)))
  16934. (when (and sref (string-match "SREF" string pchg))
  16935. (setq string (replace-match sref t t string)))))
  16936. string))
  16937. (defun org-sublist (list start end)
  16938. "Return a section of LIST, from START to END.
  16939. Counting starts at 1."
  16940. (let (rtn (c start))
  16941. (setq list (nthcdr (1- start) list))
  16942. (while (and list (<= c end))
  16943. (push (pop list) rtn)
  16944. (setq c (1+ c)))
  16945. (nreverse rtn)))
  16946. (defun org-find-base-buffer-visiting (file)
  16947. "Like `find-buffer-visiting' but always return the base buffer and
  16948. not an indirect buffer."
  16949. (let ((buf (or (get-file-buffer file)
  16950. (find-buffer-visiting file))))
  16951. (if buf
  16952. (or (buffer-base-buffer buf) buf)
  16953. nil)))
  16954. (defun org-image-file-name-regexp (&optional extensions)
  16955. "Return regexp matching the file names of images.
  16956. If EXTENSIONS is given, only match these."
  16957. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16958. (image-file-name-regexp)
  16959. (let ((image-file-name-extensions
  16960. (or extensions
  16961. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16962. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16963. (concat "\\."
  16964. (regexp-opt (nconc (mapcar 'upcase
  16965. image-file-name-extensions)
  16966. image-file-name-extensions)
  16967. t)
  16968. "\\'"))))
  16969. (defun org-file-image-p (file &optional extensions)
  16970. "Return non-nil if FILE is an image."
  16971. (save-match-data
  16972. (string-match (org-image-file-name-regexp extensions) file)))
  16973. (defun org-get-cursor-date ()
  16974. "Return the date at cursor in as a time.
  16975. This works in the calendar and in the agenda, anywhere else it just
  16976. returns the current time."
  16977. (let (date day defd)
  16978. (cond
  16979. ((eq major-mode 'calendar-mode)
  16980. (setq date (calendar-cursor-to-date)
  16981. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16982. ((eq major-mode 'org-agenda-mode)
  16983. (setq day (get-text-property (point) 'day))
  16984. (if day
  16985. (setq date (calendar-gregorian-from-absolute day)
  16986. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16987. (nth 2 date))))))
  16988. (or defd (current-time))))
  16989. (defvar org-agenda-action-marker (make-marker)
  16990. "Marker pointing to the entry for the next agenda action.")
  16991. (defun org-mark-entry-for-agenda-action ()
  16992. "Mark the current entry as target of an agenda action.
  16993. Agenda actions are actions executed from the agenda with the key `k',
  16994. which make use of the date at the cursor."
  16995. (interactive)
  16996. (move-marker org-agenda-action-marker
  16997. (save-excursion (org-back-to-heading t) (point))
  16998. (current-buffer))
  16999. (message
  17000. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17001. ;;; Paragraph filling stuff.
  17002. ;; We want this to be just right, so use the full arsenal.
  17003. (defun org-indent-line-function ()
  17004. "Indent line like previous, but further if previous was headline or item."
  17005. (interactive)
  17006. (let* ((pos (point))
  17007. (itemp (org-at-item-p))
  17008. (case-fold-search t)
  17009. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17010. (inline-task-p (and (featurep 'org-inlinetask)
  17011. (org-inlinetask-in-task-p)))
  17012. column bpos bcol tpos tcol)
  17013. ;; Find the previous relevant line
  17014. (beginning-of-line 1)
  17015. (cond
  17016. ;; Comments
  17017. ((looking-at "#") (setq column 0))
  17018. ;; Headings
  17019. ((looking-at "\\*+ ") (setq column 0))
  17020. ;; Drawers
  17021. ((and (looking-at "[ \t]*:END:")
  17022. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17023. (save-excursion
  17024. (goto-char (1- (match-beginning 1)))
  17025. (setq column (current-column))))
  17026. ;; Special blocks
  17027. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17028. (save-excursion
  17029. (re-search-backward
  17030. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17031. (setq column (org-get-indentation (match-string 0))))
  17032. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17033. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17034. (save-excursion
  17035. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17036. (setq column
  17037. (if (equal (downcase (match-string 1)) "src")
  17038. ;; src blocks: let `org-edit-src-exit' handle them
  17039. (org-get-indentation)
  17040. (org-get-indentation (match-string 0)))))
  17041. ;; Lists
  17042. ((org-in-item-p)
  17043. (org-beginning-of-item)
  17044. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\(:?\\[@\\(:?start:\\)?[0-9]+\\][ \t]*\\)?\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  17045. (setq bpos (match-beginning 1) tpos (match-end 0)
  17046. bcol (progn (goto-char bpos) (current-column))
  17047. tcol (progn (goto-char tpos) (current-column)))
  17048. (if (> tcol (+ bcol org-description-max-indent))
  17049. (setq tcol (+ bcol 5)))
  17050. (goto-char pos)
  17051. (setq column (if itemp (org-get-indentation) tcol)))
  17052. ;; This line has nothing special, look upside to get a clue about
  17053. ;; what to do.
  17054. (t
  17055. (beginning-of-line 0)
  17056. (while (and (not (bobp))
  17057. ;; skip comments, verbatim, empty lines, tables,
  17058. ;; inline tasks
  17059. (or (looking-at "[ \t]*[\n:#|]")
  17060. (and (org-in-item-p) (goto-char (org-list-top-point)))
  17061. (and (not inline-task-p)
  17062. (featurep 'org-inlinetask)
  17063. (org-inlinetask-in-task-p)))
  17064. (not (looking-at "[ \t]*:END:"))
  17065. (not (looking-at org-drawer-regexp)))
  17066. (beginning-of-line 0))
  17067. (cond
  17068. ;; There was an heading above.
  17069. ((looking-at "\\*+[ \t]+")
  17070. (if (not org-adapt-indentation)
  17071. (setq column 0)
  17072. (goto-char (match-end 0))
  17073. (setq column (current-column))))
  17074. ;; A drawer had started and is unfinished: indent consequently.
  17075. ((looking-at org-drawer-regexp)
  17076. (goto-char (1- (match-beginning 1)))
  17077. (setq column (current-column)))
  17078. ;; The drawer had ended: indent like its :END: line.
  17079. ((looking-at "\\([ \t]*\\):END:")
  17080. (goto-char (match-end 1))
  17081. (setq column (current-column)))
  17082. ;; Else, nothing noticeable found: get indentation and go on.
  17083. (t (setq column (org-get-indentation))))))
  17084. (goto-char pos)
  17085. (if (<= (current-column) (current-indentation))
  17086. (org-indent-line-to column)
  17087. (save-excursion (org-indent-line-to column)))
  17088. (setq column (current-column))
  17089. (beginning-of-line 1)
  17090. (if (looking-at
  17091. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17092. (replace-match (concat (match-string 1)
  17093. (format org-property-format
  17094. (match-string 2) (match-string 3)))
  17095. t t))
  17096. (org-move-to-column column)))
  17097. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17098. "Variable to store copy of `adaptive-fill-regexp'.
  17099. Since `adaptive-fill-regexp' is set to never match, we need to
  17100. store a backup of its value before entering `org-mode' so that
  17101. the functionality can be provided as a fall-back.")
  17102. (defun org-set-autofill-regexps ()
  17103. (interactive)
  17104. ;; In the paragraph separator we include headlines, because filling
  17105. ;; text in a line directly attached to a headline would otherwise
  17106. ;; fill the headline as well.
  17107. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17108. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17109. ;; The paragraph starter includes hand-formatted lists.
  17110. (org-set-local
  17111. 'paragraph-start
  17112. (concat
  17113. "\f" "\\|"
  17114. "[ ]*$" "\\|"
  17115. "\\*+ " "\\|"
  17116. "[ \t]*#" "\\|"
  17117. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  17118. "[ \t]*[:|]" "\\|"
  17119. "\\$\\$" "\\|"
  17120. "\\\\\\(begin\\|end\\|[][]\\)"))
  17121. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17122. ;; But only if the user has not turned off tables or fixed-width regions
  17123. (org-set-local
  17124. 'auto-fill-inhibit-regexp
  17125. (concat "\\*+ \\|#\\+"
  17126. "\\|[ \t]*" org-keyword-time-regexp
  17127. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17128. (concat
  17129. "\\|[ \t]*["
  17130. (if org-enable-table-editor "|" "")
  17131. (if org-enable-fixed-width-editor ":" "")
  17132. "]"))))
  17133. ;; We use our own fill-paragraph function, to make sure that tables
  17134. ;; and fixed-width regions are not wrapped. That function will pass
  17135. ;; through to `fill-paragraph' when appropriate.
  17136. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17137. ;; Adaptive filling: To get full control, first make sure that
  17138. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17139. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17140. (org-set-local 'org-adaptive-fill-regexp-backup
  17141. adaptive-fill-regexp))
  17142. (org-set-local 'adaptive-fill-regexp "\000")
  17143. (org-set-local 'adaptive-fill-function
  17144. 'org-adaptive-fill-function)
  17145. (org-set-local
  17146. 'align-mode-rules-list
  17147. '((org-in-buffer-settings
  17148. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17149. (modes . '(org-mode))))))
  17150. (defun org-fill-paragraph (&optional justify)
  17151. "Re-align a table, pass through to fill-paragraph if no table."
  17152. (let ((table-p (org-at-table-p))
  17153. (table.el-p (org-at-table.el-p)))
  17154. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17155. (save-excursion (goto-char (point-at-bol))
  17156. (looking-at outline-regexp)))
  17157. t) ; skip headlines
  17158. (table.el-p t) ; skip table.el tables
  17159. (table-p (org-table-align) t) ; align org-mode tables
  17160. (t nil)))) ; call paragraph-fill
  17161. ;; For reference, this is the default value of adaptive-fill-regexp
  17162. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17163. (defun org-adaptive-fill-function ()
  17164. "Return a fill prefix for org-mode files.
  17165. In particular, this makes sure hanging paragraphs for hand-formatted lists
  17166. work correctly."
  17167. (cond
  17168. ;; Comment line
  17169. ((looking-at "#[ \t]+")
  17170. (match-string-no-properties 0))
  17171. ;; Description list
  17172. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  17173. (save-excursion
  17174. (if (> (match-end 1) (+ (match-beginning 1)
  17175. org-description-max-indent))
  17176. (goto-char (+ (match-beginning 1) 5))
  17177. (goto-char (match-end 0)))
  17178. (make-string (current-column) ?\ )))
  17179. ;; Ordered or unordered list
  17180. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  17181. (save-excursion
  17182. (goto-char (match-end 0))
  17183. (make-string (current-column) ?\ )))
  17184. ;; Other text
  17185. ((looking-at org-adaptive-fill-regexp-backup)
  17186. (match-string-no-properties 0))))
  17187. ;;; Other stuff.
  17188. (defun org-toggle-fixed-width-section (arg)
  17189. "Toggle the fixed-width export.
  17190. If there is no active region, the QUOTE keyword at the current headline is
  17191. inserted or removed. When present, it causes the text between this headline
  17192. and the next to be exported as fixed-width text, and unmodified.
  17193. If there is an active region, this command adds or removes a colon as the
  17194. first character of this line. If the first character of a line is a colon,
  17195. this line is also exported in fixed-width font."
  17196. (interactive "P")
  17197. (let* ((cc 0)
  17198. (regionp (org-region-active-p))
  17199. (beg (if regionp (region-beginning) (point)))
  17200. (end (if regionp (region-end)))
  17201. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17202. (case-fold-search nil)
  17203. (re "[ \t]*\\(: \\)")
  17204. off)
  17205. (if regionp
  17206. (save-excursion
  17207. (goto-char beg)
  17208. (setq cc (current-column))
  17209. (beginning-of-line 1)
  17210. (setq off (looking-at re))
  17211. (while (> nlines 0)
  17212. (setq nlines (1- nlines))
  17213. (beginning-of-line 1)
  17214. (cond
  17215. (arg
  17216. (org-move-to-column cc t)
  17217. (insert ": \n")
  17218. (forward-line -1))
  17219. ((and off (looking-at re))
  17220. (replace-match "" t t nil 1))
  17221. ((not off) (org-move-to-column cc t) (insert ": ")))
  17222. (forward-line 1)))
  17223. (save-excursion
  17224. (org-back-to-heading)
  17225. (if (looking-at (concat outline-regexp
  17226. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17227. (replace-match "" t t nil 1)
  17228. (if (looking-at outline-regexp)
  17229. (progn
  17230. (goto-char (match-end 0))
  17231. (insert org-quote-string " "))))))))
  17232. (defun org-reftex-citation ()
  17233. "Use reftex-citation to insert a citation into the buffer.
  17234. This looks for a line like
  17235. #+BIBLIOGRAPHY: foo plain option:-d
  17236. and derives from it that foo.bib is the bibliography file relevant
  17237. for this document. It then installs the necessary environment for RefTeX
  17238. to work in this buffer and calls `reftex-citation' to insert a citation
  17239. into the buffer.
  17240. Export of such citations to both LaTeX and HTML is handled by the contributed
  17241. package org-exp-bibtex by Taru Karttunen."
  17242. (interactive)
  17243. (let ((reftex-docstruct-symbol 'rds)
  17244. (reftex-cite-format "\\cite{%l}")
  17245. rds bib)
  17246. (save-excursion
  17247. (save-restriction
  17248. (widen)
  17249. (let ((case-fold-search t)
  17250. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17251. (if (not (save-excursion
  17252. (or (re-search-forward re nil t)
  17253. (re-search-backward re nil t))))
  17254. (error "No bibliography defined in file")
  17255. (setq bib (concat (match-string 1) ".bib")
  17256. rds (list (list 'bib bib)))))))
  17257. (call-interactively 'reftex-citation)))
  17258. ;;;; Functions extending outline functionality
  17259. (defun org-beginning-of-line (&optional arg)
  17260. "Go to the beginning of the current line. If that is invisible, continue
  17261. to a visible line beginning. This makes the function of C-a more intuitive.
  17262. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17263. first attempt, and only move to after the tags when the cursor is already
  17264. beyond the end of the headline."
  17265. (interactive "P")
  17266. (let ((pos (point))
  17267. (special (if (consp org-special-ctrl-a/e)
  17268. (car org-special-ctrl-a/e)
  17269. org-special-ctrl-a/e))
  17270. refpos)
  17271. (if (org-bound-and-true-p line-move-visual)
  17272. (beginning-of-visual-line 1)
  17273. (beginning-of-line 1))
  17274. (if (and arg (fboundp 'move-beginning-of-line))
  17275. (call-interactively 'move-beginning-of-line)
  17276. (if (bobp)
  17277. nil
  17278. (backward-char 1)
  17279. (if (org-truely-invisible-p)
  17280. (while (and (not (bobp)) (org-truely-invisible-p))
  17281. (backward-char 1)
  17282. (beginning-of-line 1))
  17283. (forward-char 1))))
  17284. (when special
  17285. (cond
  17286. ((and (looking-at org-complex-heading-regexp)
  17287. (= (char-after (match-end 1)) ?\ ))
  17288. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17289. (point-at-eol)))
  17290. (goto-char
  17291. (if (eq special t)
  17292. (cond ((> pos refpos) refpos)
  17293. ((= pos (point)) refpos)
  17294. (t (point)))
  17295. (cond ((> pos (point)) (point))
  17296. ((not (eq last-command this-command)) (point))
  17297. (t refpos)))))
  17298. ((org-at-item-p)
  17299. (goto-char
  17300. (if (eq special t)
  17301. (cond ((> pos (match-end 4)) (match-end 4))
  17302. ((= pos (point)) (match-end 4))
  17303. (t (point)))
  17304. (cond ((> pos (point)) (point))
  17305. ((not (eq last-command this-command)) (point))
  17306. (t (match-end 4))))))))
  17307. (org-no-warnings
  17308. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17309. (defun org-end-of-line (&optional arg)
  17310. "Go to the end of the line.
  17311. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17312. first attempt, and only move to after the tags when the cursor is already
  17313. beyond the end of the headline."
  17314. (interactive "P")
  17315. (let ((special (if (consp org-special-ctrl-a/e)
  17316. (cdr org-special-ctrl-a/e)
  17317. org-special-ctrl-a/e)))
  17318. (if (or (not special)
  17319. (not (org-on-heading-p))
  17320. arg)
  17321. (call-interactively
  17322. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17323. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17324. (t 'end-of-line)))
  17325. (let ((pos (point)))
  17326. (beginning-of-line 1)
  17327. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  17328. (if (eq special t)
  17329. (if (or (< pos (match-beginning 1))
  17330. (= pos (match-end 0)))
  17331. (goto-char (match-beginning 1))
  17332. (goto-char (match-end 0)))
  17333. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17334. (goto-char (match-end 0))
  17335. (goto-char (match-beginning 1))))
  17336. (call-interactively (if (fboundp 'move-end-of-line)
  17337. 'move-end-of-line
  17338. 'end-of-line)))))
  17339. (org-no-warnings
  17340. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17341. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17342. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17343. (defun org-backward-sentence (&optional arg)
  17344. "Go to beginning of sentence, or beginning of table field.
  17345. This will call `backward-sentence' or `org-table-beginning-of-field',
  17346. depending on context."
  17347. (interactive "P")
  17348. (cond
  17349. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17350. (t (call-interactively 'backward-sentence))))
  17351. (defun org-forward-sentence (&optional arg)
  17352. "Go to end of sentence, or end of table field.
  17353. This will call `forward-sentence' or `org-table-end-of-field',
  17354. depending on context."
  17355. (interactive "P")
  17356. (cond
  17357. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17358. (t (call-interactively 'forward-sentence))))
  17359. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17360. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17361. (defun org-kill-line (&optional arg)
  17362. "Kill line, to tags or end of line."
  17363. (interactive "P")
  17364. (cond
  17365. ((or (not org-special-ctrl-k)
  17366. (bolp)
  17367. (not (org-on-heading-p)))
  17368. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17369. org-ctrl-k-protect-subtree)
  17370. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17371. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17372. (error "C-k aborted - would kill hidden subtree")))
  17373. (call-interactively 'kill-line))
  17374. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  17375. (kill-region (point) (match-beginning 1))
  17376. (org-set-tags nil t))
  17377. (t (kill-region (point) (point-at-eol)))))
  17378. (define-key org-mode-map "\C-k" 'org-kill-line)
  17379. (defun org-yank (&optional arg)
  17380. "Yank. If the kill is a subtree, treat it specially.
  17381. This command will look at the current kill and check if is a single
  17382. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17383. cursor is at the beginning of a line or after the stars of a currently
  17384. empty headline, then the yank is handled specially. How exactly depends
  17385. on the value of the following variables, both set by default.
  17386. org-yank-folded-subtrees
  17387. When set, the subtree(s) will be folded after insertion, but only
  17388. if doing so would now swallow text after the yanked text.
  17389. org-yank-adjusted-subtrees
  17390. When set, the subtree will be promoted or demoted in order to
  17391. fit into the local outline tree structure, which means that the level
  17392. will be adjusted so that it becomes the smaller one of the two
  17393. *visible* surrounding headings.
  17394. Any prefix to this command will cause `yank' to be called directly with
  17395. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17396. will just
  17397. plainly yank the text as it is.
  17398. \[1] The test checks if the first non-white line is a heading
  17399. and if there are no other headings with fewer stars."
  17400. (interactive "P")
  17401. (org-yank-generic 'yank arg))
  17402. (defun org-yank-generic (command arg)
  17403. "Perform some yank-like command.
  17404. This function implements the behavior described in the `org-yank'
  17405. documentation. However, it has been generalized to work for any
  17406. interactive command with similar behavior."
  17407. ;; pretend to be command COMMAND
  17408. (setq this-command command)
  17409. (if arg
  17410. (call-interactively command)
  17411. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17412. (and (org-kill-is-subtree-p)
  17413. (or (bolp)
  17414. (and (looking-at "[ \t]*$")
  17415. (string-match
  17416. "\\`\\*+\\'"
  17417. (buffer-substring (point-at-bol) (point)))))))
  17418. swallowp)
  17419. (cond
  17420. ((and subtreep org-yank-folded-subtrees)
  17421. (let ((beg (point))
  17422. end)
  17423. (if (and subtreep org-yank-adjusted-subtrees)
  17424. (org-paste-subtree nil nil 'for-yank)
  17425. (call-interactively command))
  17426. (setq end (point))
  17427. (goto-char beg)
  17428. (when (and (bolp) subtreep
  17429. (not (setq swallowp
  17430. (org-yank-folding-would-swallow-text beg end))))
  17431. (or (looking-at outline-regexp)
  17432. (re-search-forward (concat "^" outline-regexp) end t))
  17433. (while (and (< (point) end) (looking-at outline-regexp))
  17434. (hide-subtree)
  17435. (org-cycle-show-empty-lines 'folded)
  17436. (condition-case nil
  17437. (outline-forward-same-level 1)
  17438. (error (goto-char end)))))
  17439. (when swallowp
  17440. (message
  17441. "Inserted text not folded because that would swallow text"))
  17442. (goto-char end)
  17443. (skip-chars-forward " \t\n\r")
  17444. (beginning-of-line 1)
  17445. (push-mark beg 'nomsg)))
  17446. ((and subtreep org-yank-adjusted-subtrees)
  17447. (let ((beg (point-at-bol)))
  17448. (org-paste-subtree nil nil 'for-yank)
  17449. (push-mark beg 'nomsg)))
  17450. (t
  17451. (call-interactively command))))))
  17452. (defun org-yank-folding-would-swallow-text (beg end)
  17453. "Would hide-subtree at BEG swallow any text after END?"
  17454. (let (level)
  17455. (save-excursion
  17456. (goto-char beg)
  17457. (when (or (looking-at outline-regexp)
  17458. (re-search-forward (concat "^" outline-regexp) end t))
  17459. (setq level (org-outline-level)))
  17460. (goto-char end)
  17461. (skip-chars-forward " \t\r\n\v\f")
  17462. (if (or (eobp)
  17463. (and (bolp) (looking-at org-outline-regexp)
  17464. (<= (org-outline-level) level)))
  17465. nil ; Nothing would be swallowed
  17466. t)))) ; something would swallow
  17467. (define-key org-mode-map "\C-y" 'org-yank)
  17468. (defun org-invisible-p ()
  17469. "Check if point is at a character currently not visible."
  17470. ;; Early versions of noutline don't have `outline-invisible-p'.
  17471. (if (fboundp 'outline-invisible-p)
  17472. (outline-invisible-p)
  17473. (get-char-property (point) 'invisible)))
  17474. (defun org-truely-invisible-p ()
  17475. "Check if point is at a character currently not visible.
  17476. This version does not only check the character property, but also
  17477. `visible-mode'."
  17478. ;; Early versions of noutline don't have `outline-invisible-p'.
  17479. (if (org-bound-and-true-p visible-mode)
  17480. nil
  17481. (if (fboundp 'outline-invisible-p)
  17482. (outline-invisible-p)
  17483. (get-char-property (point) 'invisible))))
  17484. (defun org-invisible-p2 ()
  17485. "Check if point is at a character currently not visible."
  17486. (save-excursion
  17487. (if (and (eolp) (not (bobp))) (backward-char 1))
  17488. ;; Early versions of noutline don't have `outline-invisible-p'.
  17489. (if (fboundp 'outline-invisible-p)
  17490. (outline-invisible-p)
  17491. (get-char-property (point) 'invisible))))
  17492. (defun org-back-to-heading (&optional invisible-ok)
  17493. "Call `outline-back-to-heading', but provide a better error message."
  17494. (condition-case nil
  17495. (outline-back-to-heading invisible-ok)
  17496. (error (error "Before first headline at position %d in buffer %s"
  17497. (point) (current-buffer)))))
  17498. (defun org-beginning-of-defun ()
  17499. "Go to the beginning of the subtree, i.e. back to the heading."
  17500. (org-back-to-heading))
  17501. (defun org-end-of-defun ()
  17502. "Go to the end of the subtree."
  17503. (org-end-of-subtree nil t))
  17504. (defun org-before-first-heading-p ()
  17505. "Before first heading?"
  17506. (save-excursion
  17507. (null (re-search-backward "^\\*+ " nil t))))
  17508. (defun org-on-heading-p (&optional ignored)
  17509. (outline-on-heading-p t))
  17510. (defun org-at-heading-p (&optional ignored)
  17511. (outline-on-heading-p t))
  17512. (defun org-point-at-end-of-empty-headline ()
  17513. "If point is at the end of an empty headline, return t, else nil.
  17514. If the heading only contains a TODO keyword, it is still still considered
  17515. empty."
  17516. (and (looking-at "[ \t]*$")
  17517. (save-excursion
  17518. (beginning-of-line 1)
  17519. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17520. "\\)?[ \t]*$")))))
  17521. (defun org-at-heading-or-item-p ()
  17522. (or (org-on-heading-p) (org-at-item-p)))
  17523. (defun org-on-target-p ()
  17524. (or (org-in-regexp org-radio-target-regexp)
  17525. (org-in-regexp org-target-regexp)))
  17526. (defun org-up-heading-all (arg)
  17527. "Move to the heading line of which the present line is a subheading.
  17528. This function considers both visible and invisible heading lines.
  17529. With argument, move up ARG levels."
  17530. (if (fboundp 'outline-up-heading-all)
  17531. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17532. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17533. (defun org-up-heading-safe ()
  17534. "Move to the heading line of which the present line is a subheading.
  17535. This version will not throw an error. It will return the level of the
  17536. headline found, or nil if no higher level is found.
  17537. Also, this function will be a lot faster than `outline-up-heading',
  17538. because it relies on stars being the outline starters. This can really
  17539. make a significant difference in outlines with very many siblings."
  17540. (let (start-level re)
  17541. (org-back-to-heading t)
  17542. (setq start-level (funcall outline-level))
  17543. (if (equal start-level 1)
  17544. nil
  17545. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17546. (if (re-search-backward re nil t)
  17547. (funcall outline-level)))))
  17548. (defun org-first-sibling-p ()
  17549. "Is this heading the first child of its parents?"
  17550. (interactive)
  17551. (let ((re (concat "^" outline-regexp))
  17552. level l)
  17553. (unless (org-at-heading-p t)
  17554. (error "Not at a heading"))
  17555. (setq level (funcall outline-level))
  17556. (save-excursion
  17557. (if (not (re-search-backward re nil t))
  17558. t
  17559. (setq l (funcall outline-level))
  17560. (< l level)))))
  17561. (defun org-goto-sibling (&optional previous)
  17562. "Goto the next sibling, even if it is invisible.
  17563. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17564. when a sibling was found. When none is found, return nil and don't
  17565. move point."
  17566. (let ((fun (if previous 're-search-backward 're-search-forward))
  17567. (pos (point))
  17568. (re (concat "^" outline-regexp))
  17569. level l)
  17570. (when (condition-case nil (org-back-to-heading t) (error nil))
  17571. (setq level (funcall outline-level))
  17572. (catch 'exit
  17573. (or previous (forward-char 1))
  17574. (while (funcall fun re nil t)
  17575. (setq l (funcall outline-level))
  17576. (when (< l level) (goto-char pos) (throw 'exit nil))
  17577. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17578. (goto-char pos)
  17579. nil))))
  17580. (defun org-show-siblings ()
  17581. "Show all siblings of the current headline."
  17582. (save-excursion
  17583. (while (org-goto-sibling) (org-flag-heading nil)))
  17584. (save-excursion
  17585. (while (org-goto-sibling 'previous)
  17586. (org-flag-heading nil))))
  17587. (defun org-goto-first-child ()
  17588. "Goto the first child, even if it is invisible.
  17589. Return t when a child was found. Otherwise don't move point and
  17590. return nil."
  17591. (let (level (pos (point)) (re (concat "^" outline-regexp)))
  17592. (when (condition-case nil (org-back-to-heading t) (error nil))
  17593. (setq level (outline-level))
  17594. (forward-char 1)
  17595. (if (and (re-search-forward re nil t) (> (outline-level) level))
  17596. (progn (goto-char (match-beginning 0)) t)
  17597. (goto-char pos) nil))))
  17598. (defun org-show-hidden-entry ()
  17599. "Show an entry where even the heading is hidden."
  17600. (save-excursion
  17601. (org-show-entry)))
  17602. (defun org-flag-heading (flag &optional entry)
  17603. "Flag the current heading. FLAG non-nil means make invisible.
  17604. When ENTRY is non-nil, show the entire entry."
  17605. (save-excursion
  17606. (org-back-to-heading t)
  17607. ;; Check if we should show the entire entry
  17608. (if entry
  17609. (progn
  17610. (org-show-entry)
  17611. (save-excursion
  17612. (and (outline-next-heading)
  17613. (org-flag-heading nil))))
  17614. (outline-flag-region (max (point-min) (1- (point)))
  17615. (save-excursion (outline-end-of-heading) (point))
  17616. flag))))
  17617. (defun org-get-next-sibling ()
  17618. "Move to next heading of the same level, and return point.
  17619. If there is no such heading, return nil.
  17620. This is like outline-next-sibling, but invisible headings are ok."
  17621. (let ((level (funcall outline-level)))
  17622. (outline-next-heading)
  17623. (while (and (not (eobp)) (> (funcall outline-level) level))
  17624. (outline-next-heading))
  17625. (if (or (eobp) (< (funcall outline-level) level))
  17626. nil
  17627. (point))))
  17628. (defun org-get-last-sibling ()
  17629. "Move to previous heading of the same level, and return point.
  17630. If there is no such heading, return nil."
  17631. (let ((opoint (point))
  17632. (level (funcall outline-level)))
  17633. (outline-previous-heading)
  17634. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17635. (while (and (> (funcall outline-level) level)
  17636. (not (bobp)))
  17637. (outline-previous-heading))
  17638. (if (< (funcall outline-level) level)
  17639. nil
  17640. (point)))))
  17641. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17642. ;; This contains an exact copy of the original function, but it uses
  17643. ;; `org-back-to-heading', to make it work also in invisible
  17644. ;; trees. And is uses an invisible-OK argument.
  17645. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17646. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17647. ;; with many children and grandchildren etc, this can be much faster
  17648. ;; than the outline version.
  17649. (org-back-to-heading invisible-OK)
  17650. (let ((first t)
  17651. (level (funcall outline-level)))
  17652. (if (and (org-mode-p) (< level 1000))
  17653. ;; A true heading (not a plain list item), in Org-mode
  17654. ;; This means we can easily find the end by looking
  17655. ;; only for the right number of stars. Using a regexp to do
  17656. ;; this is so much faster than using a Lisp loop.
  17657. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17658. (forward-char 1)
  17659. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17660. ;; something else, do it the slow way
  17661. (while (and (not (eobp))
  17662. (or first (> (funcall outline-level) level)))
  17663. (setq first nil)
  17664. (outline-next-heading)))
  17665. (unless to-heading
  17666. (if (memq (preceding-char) '(?\n ?\^M))
  17667. (progn
  17668. ;; Go to end of line before heading
  17669. (forward-char -1)
  17670. (if (memq (preceding-char) '(?\n ?\^M))
  17671. ;; leave blank line before heading
  17672. (forward-char -1))))))
  17673. (point))
  17674. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17675. "Use Org version in org-mode, for dramatic speed-up."
  17676. (if (eq major-mode 'org-mode)
  17677. (progn
  17678. (org-end-of-subtree nil t)
  17679. (unless (eobp) (backward-char 1)))
  17680. ad-do-it))
  17681. (defun org-forward-same-level (arg &optional invisible-ok)
  17682. "Move forward to the arg'th subheading at same level as this one.
  17683. Stop at the first and last subheadings of a superior heading.
  17684. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  17685. it wil also look at invisible ones."
  17686. (interactive "p")
  17687. (org-back-to-heading invisible-ok)
  17688. (org-on-heading-p)
  17689. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17690. (re (format "^\\*\\{1,%d\\} " level))
  17691. l)
  17692. (forward-char 1)
  17693. (while (> arg 0)
  17694. (while (and (re-search-forward re nil 'move)
  17695. (setq l (- (match-end 0) (match-beginning 0) 1))
  17696. (= l level)
  17697. (not invisible-ok)
  17698. (progn (backward-char 1) (org-invisible-p)))
  17699. (if (< l level) (setq arg 1)))
  17700. (setq arg (1- arg)))
  17701. (beginning-of-line 1)))
  17702. (defun org-backward-same-level (arg &optional invisible-ok)
  17703. "Move backward to the arg'th subheading at same level as this one.
  17704. Stop at the first and last subheadings of a superior heading."
  17705. (interactive "p")
  17706. (org-back-to-heading)
  17707. (org-on-heading-p)
  17708. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17709. (re (format "^\\*\\{1,%d\\} " level))
  17710. l)
  17711. (while (> arg 0)
  17712. (while (and (re-search-backward re nil 'move)
  17713. (setq l (- (match-end 0) (match-beginning 0) 1))
  17714. (= l level)
  17715. (not invisible-ok)
  17716. (org-invisible-p))
  17717. (if (< l level) (setq arg 1)))
  17718. (setq arg (1- arg)))))
  17719. (defun org-show-subtree ()
  17720. "Show everything after this heading at deeper levels."
  17721. (outline-flag-region
  17722. (point)
  17723. (save-excursion
  17724. (org-end-of-subtree t t))
  17725. nil))
  17726. (defun org-show-entry ()
  17727. "Show the body directly following this heading.
  17728. Show the heading too, if it is currently invisible."
  17729. (interactive)
  17730. (save-excursion
  17731. (condition-case nil
  17732. (progn
  17733. (org-back-to-heading t)
  17734. (outline-flag-region
  17735. (max (point-min) (1- (point)))
  17736. (save-excursion
  17737. (if (re-search-forward
  17738. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17739. (match-beginning 1)
  17740. (point-max)))
  17741. nil)
  17742. (org-cycle-hide-drawers 'children))
  17743. (error nil))))
  17744. (defun org-make-options-regexp (kwds &optional extra)
  17745. "Make a regular expression for keyword lines."
  17746. (concat
  17747. "^"
  17748. "#?[ \t]*\\+\\("
  17749. (mapconcat 'regexp-quote kwds "\\|")
  17750. (if extra (concat "\\|" extra))
  17751. "\\):[ \t]*"
  17752. "\\(.*\\)"))
  17753. ;; Make isearch reveal the necessary context
  17754. (defun org-isearch-end ()
  17755. "Reveal context after isearch exits."
  17756. (when isearch-success ; only if search was successful
  17757. (if (featurep 'xemacs)
  17758. ;; Under XEmacs, the hook is run in the correct place,
  17759. ;; we directly show the context.
  17760. (org-show-context 'isearch)
  17761. ;; In Emacs the hook runs *before* restoring the overlays.
  17762. ;; So we have to use a one-time post-command-hook to do this.
  17763. ;; (Emacs 22 has a special variable, see function `org-mode')
  17764. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17765. isearch-mode-end-hook-quit)
  17766. ;; Only when the isearch was not quitted.
  17767. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17768. 'append 'local)))))
  17769. (defun org-isearch-post-command ()
  17770. "Remove self from hook, and show context."
  17771. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17772. (org-show-context 'isearch))
  17773. ;;;; Integration with and fixes for other packages
  17774. ;;; Imenu support
  17775. (defvar org-imenu-markers nil
  17776. "All markers currently used by Imenu.")
  17777. (make-variable-buffer-local 'org-imenu-markers)
  17778. (defun org-imenu-new-marker (&optional pos)
  17779. "Return a new marker for use by Imenu, and remember the marker."
  17780. (let ((m (make-marker)))
  17781. (move-marker m (or pos (point)))
  17782. (push m org-imenu-markers)
  17783. m))
  17784. (defun org-imenu-get-tree ()
  17785. "Produce the index for Imenu."
  17786. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17787. (setq org-imenu-markers nil)
  17788. (let* ((n org-imenu-depth)
  17789. (re (concat "^" outline-regexp))
  17790. (subs (make-vector (1+ n) nil))
  17791. (last-level 0)
  17792. m level head)
  17793. (save-excursion
  17794. (save-restriction
  17795. (widen)
  17796. (goto-char (point-max))
  17797. (while (re-search-backward re nil t)
  17798. (setq level (org-reduced-level (funcall outline-level)))
  17799. (when (<= level n)
  17800. (looking-at org-complex-heading-regexp)
  17801. (setq head (org-link-display-format
  17802. (org-match-string-no-properties 4))
  17803. m (org-imenu-new-marker))
  17804. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17805. (if (>= level last-level)
  17806. (push (cons head m) (aref subs level))
  17807. (push (cons head (aref subs (1+ level))) (aref subs level))
  17808. (loop for i from (1+ level) to n do (aset subs i nil)))
  17809. (setq last-level level)))))
  17810. (aref subs 1)))
  17811. (eval-after-load "imenu"
  17812. '(progn
  17813. (add-hook 'imenu-after-jump-hook
  17814. (lambda ()
  17815. (if (eq major-mode 'org-mode)
  17816. (org-show-context 'org-goto))))))
  17817. (defun org-link-display-format (link)
  17818. "Replace a link with either the description, or the link target
  17819. if no description is present"
  17820. (save-match-data
  17821. (if (string-match org-bracket-link-analytic-regexp link)
  17822. (replace-match (if (match-end 5)
  17823. (match-string 5 link)
  17824. (concat (match-string 1 link)
  17825. (match-string 3 link)))
  17826. nil t link)
  17827. link)))
  17828. ;; Speedbar support
  17829. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17830. "Overlay marking the agenda restriction line in speedbar.")
  17831. (overlay-put org-speedbar-restriction-lock-overlay
  17832. 'face 'org-agenda-restriction-lock)
  17833. (overlay-put org-speedbar-restriction-lock-overlay
  17834. 'help-echo "Agendas are currently limited to this item.")
  17835. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17836. (defun org-speedbar-set-agenda-restriction ()
  17837. "Restrict future agenda commands to the location at point in speedbar.
  17838. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17839. (interactive)
  17840. (require 'org-agenda)
  17841. (let (p m tp np dir txt)
  17842. (cond
  17843. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17844. 'org-imenu t))
  17845. (setq m (get-text-property p 'org-imenu-marker))
  17846. (with-current-buffer (marker-buffer m)
  17847. (goto-char m)
  17848. (org-agenda-set-restriction-lock 'subtree)))
  17849. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17850. 'speedbar-function 'speedbar-find-file))
  17851. (setq tp (previous-single-property-change
  17852. (1+ p) 'speedbar-function)
  17853. np (next-single-property-change
  17854. tp 'speedbar-function)
  17855. dir (speedbar-line-directory)
  17856. txt (buffer-substring-no-properties (or tp (point-min))
  17857. (or np (point-max))))
  17858. (with-current-buffer (find-file-noselect
  17859. (let ((default-directory dir))
  17860. (expand-file-name txt)))
  17861. (unless (org-mode-p)
  17862. (error "Cannot restrict to non-Org-mode file"))
  17863. (org-agenda-set-restriction-lock 'file)))
  17864. (t (error "Don't know how to restrict Org-mode's agenda")))
  17865. (move-overlay org-speedbar-restriction-lock-overlay
  17866. (point-at-bol) (point-at-eol))
  17867. (setq current-prefix-arg nil)
  17868. (org-agenda-maybe-redo)))
  17869. (eval-after-load "speedbar"
  17870. '(progn
  17871. (speedbar-add-supported-extension ".org")
  17872. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17873. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17874. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17875. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17876. (add-hook 'speedbar-visiting-tag-hook
  17877. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17878. ;;; Fixes and Hacks for problems with other packages
  17879. ;; Make flyspell not check words in links, to not mess up our keymap
  17880. (defun org-mode-flyspell-verify ()
  17881. "Don't let flyspell put overlays at active buttons."
  17882. (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
  17883. (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
  17884. (defun org-remove-flyspell-overlays-in (beg end)
  17885. "Remove flyspell overlays in region."
  17886. (and (org-bound-and-true-p flyspell-mode)
  17887. (fboundp 'flyspell-delete-region-overlays)
  17888. (flyspell-delete-region-overlays beg end))
  17889. (add-text-properties beg end '(org-no-flyspell t)))
  17890. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17891. (eval-after-load "bookmark"
  17892. '(if (boundp 'bookmark-after-jump-hook)
  17893. ;; We can use the hook
  17894. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17895. ;; Hook not available, use advice
  17896. (defadvice bookmark-jump (after org-make-visible activate)
  17897. "Make the position visible."
  17898. (org-bookmark-jump-unhide))))
  17899. ;; Make sure saveplace shows the location if it was hidden
  17900. (eval-after-load "saveplace"
  17901. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17902. "Make the position visible."
  17903. (org-bookmark-jump-unhide)))
  17904. ;; Make sure ecb shows the location if it was hidden
  17905. (eval-after-load "ecb"
  17906. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17907. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17908. (if (eq major-mode 'org-mode)
  17909. (org-show-context))))
  17910. (defun org-bookmark-jump-unhide ()
  17911. "Unhide the current position, to show the bookmark location."
  17912. (and (org-mode-p)
  17913. (or (org-invisible-p)
  17914. (save-excursion (goto-char (max (point-min) (1- (point))))
  17915. (org-invisible-p)))
  17916. (org-show-context 'bookmark-jump)))
  17917. ;; Make session.el ignore our circular variable
  17918. (eval-after-load "session"
  17919. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17920. ;;;; Experimental code
  17921. (defun org-closed-in-range ()
  17922. "Sparse tree of items closed in a certain time range.
  17923. Still experimental, may disappear in the future."
  17924. (interactive)
  17925. ;; Get the time interval from the user.
  17926. (let* ((time1 (org-float-time
  17927. (org-read-date nil 'to-time nil "Starting date: ")))
  17928. (time2 (org-float-time
  17929. (org-read-date nil 'to-time nil "End date:")))
  17930. ;; callback function
  17931. (callback (lambda ()
  17932. (let ((time
  17933. (org-float-time
  17934. (apply 'encode-time
  17935. (org-parse-time-string
  17936. (match-string 1))))))
  17937. ;; check if time in interval
  17938. (and (>= time time1) (<= time time2))))))
  17939. ;; make tree, check each match with the callback
  17940. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17941. ;;;; Finish up
  17942. (provide 'org)
  17943. (run-hooks 'org-load-hook)
  17944. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17945. ;;; org.el ends here