org.el 737 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682
  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.4
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-complete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. ;; babel
  97. (require 'ob)
  98. (require 'ob-table)
  99. (require 'ob-lob)
  100. (require 'ob-ref)
  101. (require 'ob-tangle)
  102. (require 'ob-comint)
  103. (require 'ob-keys)
  104. ;; load languages based on value of `org-babel-load-languages'
  105. (defvar org-babel-load-languages)
  106. ;;;###autoload
  107. (defun org-babel-do-load-languages (sym value)
  108. "Load the languages defined in `org-babel-load-languages'."
  109. (set-default sym value)
  110. (mapc (lambda (pair)
  111. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  112. (if active
  113. (progn
  114. (require (intern (concat "ob-" lang))))
  115. (progn
  116. (funcall 'fmakunbound
  117. (intern (concat "org-babel-execute:" lang)))
  118. (funcall 'fmakunbound
  119. (intern (concat "org-babel-expand-body:" lang)))))))
  120. org-babel-load-languages))
  121. (defcustom org-babel-load-languages '((emacs-lisp . t))
  122. "Languages which can be evaluated in Org-mode buffers.
  123. This list can be used to load support for any of the languages
  124. below, note that each language will depend on a different set of
  125. system executables and/or Emacs modes. When a language is
  126. \"loaded\", then code blocks in that language can be evaluated
  127. with `org-babel-execute-src-block' bound by default to C-c
  128. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  129. be set to remove code block evaluation from the C-c C-c
  130. keybinding. By default only Emacs Lisp (which has no
  131. requirements) is loaded."
  132. :group 'org-babel
  133. :set 'org-babel-do-load-languages
  134. :type '(alist :tag "Babel Languages"
  135. :key-type
  136. (choice
  137. (const :tag "C" C)
  138. (const :tag "R" R)
  139. (const :tag "Asymptote" asymptote)
  140. (const :tag "Calc" calc)
  141. (const :tag "Clojure" clojure)
  142. (const :tag "CSS" css)
  143. (const :tag "Ditaa" ditaa)
  144. (const :tag "Dot" dot)
  145. (const :tag "Emacs Lisp" emacs-lisp)
  146. (const :tag "Gnuplot" gnuplot)
  147. (const :tag "Haskell" haskell)
  148. (const :tag "Javascript" js)
  149. (const :tag "Latex" latex)
  150. (const :tag "Ledger" ledger)
  151. (const :tag "Matlab" matlab)
  152. (const :tag "Mscgen" mscgen)
  153. (const :tag "Ocaml" ocaml)
  154. (const :tag "Octave" octave)
  155. (const :tag "Org" org)
  156. (const :tag "Perl" perl)
  157. (const :tag "PlantUML" plantuml)
  158. (const :tag "Python" python)
  159. (const :tag "Ruby" ruby)
  160. (const :tag "Sass" sass)
  161. (const :tag "Scheme" scheme)
  162. (const :tag "Screen" screen)
  163. (const :tag "Shell Script" sh)
  164. (const :tag "Sql" sql)
  165. (const :tag "Sqlite" sqlite))
  166. :value-type (boolean :tag "Activate" :value t)))
  167. ;;;; Customization variables
  168. (defcustom org-clone-delete-id nil
  169. "Remove ID property of clones of a subtree.
  170. When non-nil, clones of a subtree don't inherit the ID property.
  171. Otherwise they inherit the ID property with a new unique
  172. identifier."
  173. :type 'boolean
  174. :group 'org-id)
  175. ;;; Version
  176. (defconst org-version "7.4"
  177. "The version number of the file org.el.")
  178. (defun org-version (&optional here)
  179. "Show the org-mode version in the echo area.
  180. With prefix arg HERE, insert it at point."
  181. (interactive "P")
  182. (let* ((origin default-directory)
  183. (version org-version)
  184. (git-version)
  185. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  186. (when (and (file-exists-p (expand-file-name ".git" dir))
  187. (executable-find "git"))
  188. (unwind-protect
  189. (progn
  190. (cd dir)
  191. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  192. (with-current-buffer "*Shell Command Output*"
  193. (goto-char (point-min))
  194. (setq git-version (buffer-substring (point) (point-at-eol))))
  195. (subst-char-in-string ?- ?. git-version t)
  196. (when (string-match "\\S-"
  197. (shell-command-to-string
  198. "git diff-index --name-only HEAD --"))
  199. (setq git-version (concat git-version ".dirty")))
  200. (setq version (concat version " (" git-version ")"))))
  201. (cd origin)))
  202. (setq version (format "Org-mode version %s" version))
  203. (if here (insert version))
  204. (message version)))
  205. ;;; Compatibility constants
  206. ;;; The custom variables
  207. (defgroup org nil
  208. "Outline-based notes management and organizer."
  209. :tag "Org"
  210. :group 'outlines
  211. :group 'calendar)
  212. (defcustom org-mode-hook nil
  213. "Mode hook for Org-mode, run after the mode was turned on."
  214. :group 'org
  215. :type 'hook)
  216. (defcustom org-load-hook nil
  217. "Hook that is run after org.el has been loaded."
  218. :group 'org
  219. :type 'hook)
  220. (defvar org-modules) ; defined below
  221. (defvar org-modules-loaded nil
  222. "Have the modules been loaded already?")
  223. (defun org-load-modules-maybe (&optional force)
  224. "Load all extensions listed in `org-modules'."
  225. (when (or force (not org-modules-loaded))
  226. (mapc (lambda (ext)
  227. (condition-case nil (require ext)
  228. (error (message "Problems while trying to load feature `%s'" ext))))
  229. org-modules)
  230. (setq org-modules-loaded t)))
  231. (defun org-set-modules (var value)
  232. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  233. (set var value)
  234. (when (featurep 'org)
  235. (org-load-modules-maybe 'force)))
  236. (when (org-bound-and-true-p org-modules)
  237. (let ((a (member 'org-infojs org-modules)))
  238. (and a (setcar a 'org-jsinfo))))
  239. (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)
  240. "Modules that should always be loaded together with org.el.
  241. If a description starts with <C>, the file is not part of Emacs
  242. and loading it will require that you have downloaded and properly installed
  243. the org-mode distribution.
  244. You can also use this system to load external packages (i.e. neither Org
  245. core modules, nor modules from the CONTRIB directory). Just add symbols
  246. to the end of the list. If the package is called org-xyz.el, then you need
  247. to add the symbol `xyz', and the package must have a call to
  248. (provide 'org-xyz)"
  249. :group 'org
  250. :set 'org-set-modules
  251. :type
  252. '(set :greedy t
  253. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  254. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  255. (const :tag " crypt: Encryption of subtrees" org-crypt)
  256. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  257. (const :tag " docview: Links to doc-view buffers" org-docview)
  258. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  259. (const :tag " id: Global IDs for identifying entries" org-id)
  260. (const :tag " info: Links to Info nodes" org-info)
  261. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  262. (const :tag " habit: Track your consistency with habits" org-habit)
  263. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  264. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  265. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  266. (const :tag " mew Links to Mew folders/messages" org-mew)
  267. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  268. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  269. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  270. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  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 eshell Support for links to working directories in eshell" org-eshell)
  284. (const :tag "C eval: Include command output as text" org-eval)
  285. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  286. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  287. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  288. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  289. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  290. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  291. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  292. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  293. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  294. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  295. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  296. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  297. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  298. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  299. (const :tag "C registry: A registry for Org-mode links" org-registry)
  300. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  301. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  302. (const :tag "C secretary: Team management with org-mode" org-secretary)
  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. %d date
  1159. %m message-id.
  1160. You may use normal field width specification between the % and the letter.
  1161. This is for example useful to limit the length of the subject.
  1162. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1163. :group 'org-link-store
  1164. :type 'string)
  1165. (defcustom org-from-is-user-regexp
  1166. (let (r1 r2)
  1167. (when (and user-mail-address (not (string= user-mail-address "")))
  1168. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1169. (when (and user-full-name (not (string= user-full-name "")))
  1170. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1171. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1172. "Regexp matched against the \"From:\" header of an email or usenet message.
  1173. It should match if the message is from the user him/herself."
  1174. :group 'org-link-store
  1175. :type 'regexp)
  1176. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1177. "Non-nil means storing a link to an Org file will use entry IDs.
  1178. Note that before this variable is even considered, org-id must be loaded,
  1179. so please customize `org-modules' and turn it on.
  1180. The variable can have the following values:
  1181. t Create an ID if needed to make a link to the current entry.
  1182. create-if-interactive
  1183. If `org-store-link' is called directly (interactively, as a user
  1184. command), do create an ID to support the link. But when doing the
  1185. job for remember, only use the ID if it already exists. The
  1186. purpose of this setting is to avoid proliferation of unwanted
  1187. IDs, just because you happen to be in an Org file when you
  1188. call `org-remember' that automatically and preemptively
  1189. creates a link. If you do want to get an ID link in a remember
  1190. template to an entry not having an ID, create it first by
  1191. explicitly creating a link to it, using `C-c C-l' first.
  1192. create-if-interactive-and-no-custom-id
  1193. Like create-if-interactive, but do not create an ID if there is
  1194. a CUSTOM_ID property defined in the entry. This is the default.
  1195. use-existing
  1196. Use existing ID, do not create one.
  1197. nil Never use an ID to make a link, instead link using a text search for
  1198. the headline text."
  1199. :group 'org-link-store
  1200. :type '(choice
  1201. (const :tag "Create ID to make link" t)
  1202. (const :tag "Create if storing link interactively"
  1203. create-if-interactive)
  1204. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1205. create-if-interactive-and-no-custom-id)
  1206. (const :tag "Only use existing" use-existing)
  1207. (const :tag "Do not use ID to create link" nil)))
  1208. (defcustom org-context-in-file-links t
  1209. "Non-nil means file links from `org-store-link' contain context.
  1210. A search string will be added to the file name with :: as separator and
  1211. used to find the context when the link is activated by the command
  1212. `org-open-at-point'. When this option is t, the entire active region
  1213. will be placed in the search string of the file link. If set to a
  1214. positive integer, only the first n lines of context will be stored.
  1215. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1216. negates this setting for the duration of the command."
  1217. :group 'org-link-store
  1218. :type '(choice boolean integer))
  1219. (defcustom org-keep-stored-link-after-insertion nil
  1220. "Non-nil means keep link in list for entire session.
  1221. The command `org-store-link' adds a link pointing to the current
  1222. location to an internal list. These links accumulate during a session.
  1223. The command `org-insert-link' can be used to insert links into any
  1224. Org-mode file (offering completion for all stored links). When this
  1225. option is nil, every link which has been inserted once using \\[org-insert-link]
  1226. will be removed from the list, to make completing the unused links
  1227. more efficient."
  1228. :group 'org-link-store
  1229. :type 'boolean)
  1230. (defgroup org-link-follow nil
  1231. "Options concerning following links in Org-mode."
  1232. :tag "Org Follow Link"
  1233. :group 'org-link)
  1234. (defcustom org-link-translation-function nil
  1235. "Function to translate links with different syntax to Org syntax.
  1236. This can be used to translate links created for example by the Planner
  1237. or emacs-wiki packages to Org syntax.
  1238. The function must accept two parameters, a TYPE containing the link
  1239. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1240. which is everything after the link protocol. It should return a cons
  1241. with possibly modified values of type and path.
  1242. Org contains a function for this, so if you set this variable to
  1243. `org-translate-link-from-planner', you should be able follow many
  1244. links created by planner."
  1245. :group 'org-link-follow
  1246. :type 'function)
  1247. (defcustom org-follow-link-hook nil
  1248. "Hook that is run after a link has been followed."
  1249. :group 'org-link-follow
  1250. :type 'hook)
  1251. (defcustom org-tab-follows-link nil
  1252. "Non-nil means on links TAB will follow the link.
  1253. Needs to be set before org.el is loaded.
  1254. This really should not be used, it does not make sense, and the
  1255. implementation is bad."
  1256. :group 'org-link-follow
  1257. :type 'boolean)
  1258. (defcustom org-return-follows-link nil
  1259. "Non-nil means on links RET will follow the link."
  1260. :group 'org-link-follow
  1261. :type 'boolean)
  1262. (defcustom org-mouse-1-follows-link
  1263. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1264. "Non-nil means mouse-1 on a link will follow the link.
  1265. A longer mouse click will still set point. Does not work on XEmacs.
  1266. Needs to be set before org.el is loaded."
  1267. :group 'org-link-follow
  1268. :type 'boolean)
  1269. (defcustom org-mark-ring-length 4
  1270. "Number of different positions to be recorded in the ring.
  1271. Changing this requires a restart of Emacs to work correctly."
  1272. :group 'org-link-follow
  1273. :type 'integer)
  1274. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1275. "Non-nil means internal links in Org files must exactly match a headline.
  1276. When nil, the link search tries to match a phrase will all words
  1277. in the search text."
  1278. :group 'org-link-follow
  1279. :type '(choice
  1280. (const :tag "Use fuzy text search" nil)
  1281. (const :tag "Match only exact headline" t)
  1282. (const :tag "Match extact headline or query to create it"
  1283. query-to-create)))
  1284. (defcustom org-link-frame-setup
  1285. '((vm . vm-visit-folder-other-frame)
  1286. (gnus . org-gnus-no-new-news)
  1287. (file . find-file-other-window)
  1288. (wl . wl-other-frame))
  1289. "Setup the frame configuration for following links.
  1290. When following a link with Emacs, it may often be useful to display
  1291. this link in another window or frame. This variable can be used to
  1292. set this up for the different types of links.
  1293. For VM, use any of
  1294. `vm-visit-folder'
  1295. `vm-visit-folder-other-window'
  1296. `vm-visit-folder-other-frame'
  1297. For Gnus, use any of
  1298. `gnus'
  1299. `gnus-other-frame'
  1300. `org-gnus-no-new-news'
  1301. For FILE, use any of
  1302. `find-file'
  1303. `find-file-other-window'
  1304. `find-file-other-frame'
  1305. For Wanderlust use any of
  1306. `wl'
  1307. `wl-other-frame'
  1308. For the calendar, use the variable `calendar-setup'.
  1309. For BBDB, it is currently only possible to display the matches in
  1310. another window."
  1311. :group 'org-link-follow
  1312. :type '(list
  1313. (cons (const vm)
  1314. (choice
  1315. (const vm-visit-folder)
  1316. (const vm-visit-folder-other-window)
  1317. (const vm-visit-folder-other-frame)))
  1318. (cons (const gnus)
  1319. (choice
  1320. (const gnus)
  1321. (const gnus-other-frame)
  1322. (const org-gnus-no-new-news)))
  1323. (cons (const file)
  1324. (choice
  1325. (const find-file)
  1326. (const find-file-other-window)
  1327. (const find-file-other-frame)))
  1328. (cons (const wl)
  1329. (choice
  1330. (const wl)
  1331. (const wl-other-frame)))))
  1332. (defcustom org-display-internal-link-with-indirect-buffer nil
  1333. "Non-nil means use indirect buffer to display infile links.
  1334. Activating internal links (from one location in a file to another location
  1335. in the same file) normally just jumps to the location. When the link is
  1336. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1337. is displayed in
  1338. another window. When this option is set, the other window actually displays
  1339. an indirect buffer clone of the current buffer, to avoid any visibility
  1340. changes to the current buffer."
  1341. :group 'org-link-follow
  1342. :type 'boolean)
  1343. (defcustom org-open-non-existing-files nil
  1344. "Non-nil means `org-open-file' will open non-existing files.
  1345. When nil, an error will be generated.
  1346. This variable applies only to external applications because they
  1347. might choke on non-existing files. If the link is to a file that
  1348. will be opened in Emacs, the variable is ignored."
  1349. :group 'org-link-follow
  1350. :type 'boolean)
  1351. (defcustom org-open-directory-means-index-dot-org nil
  1352. "Non-nil means a link to a directory really means to index.org.
  1353. When nil, following a directory link will run dired or open a finder/explorer
  1354. window on that directory."
  1355. :group 'org-link-follow
  1356. :type 'boolean)
  1357. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1358. "Function and arguments to call for following mailto links.
  1359. This is a list with the first element being a Lisp function, and the
  1360. remaining elements being arguments to the function. In string arguments,
  1361. %a will be replaced by the address, and %s will be replaced by the subject
  1362. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1363. :group 'org-link-follow
  1364. :type '(choice
  1365. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1366. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1367. (const :tag "message-mail" (message-mail "%a" "%s"))
  1368. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1369. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1370. "Non-nil means ask for confirmation before executing shell links.
  1371. Shell links can be dangerous: just think about a link
  1372. [[shell:rm -rf ~/*][Google Search]]
  1373. This link would show up in your Org-mode document as \"Google Search\",
  1374. but really it would remove your entire home directory.
  1375. Therefore we advise against setting this variable to nil.
  1376. Just change it to `y-or-n-p' if you want to confirm with a
  1377. single keystroke rather than having to type \"yes\"."
  1378. :group 'org-link-follow
  1379. :type '(choice
  1380. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1381. (const :tag "with y-or-n (faster)" y-or-n-p)
  1382. (const :tag "no confirmation (dangerous)" nil)))
  1383. (put 'org-confirm-shell-link-function
  1384. 'safe-local-variable
  1385. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1386. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1387. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1388. Elisp links can be dangerous: just think about a link
  1389. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1390. This link would show up in your Org-mode document as \"Google Search\",
  1391. but really it would remove your entire home directory.
  1392. Therefore we advise against setting this variable to nil.
  1393. Just change it to `y-or-n-p' if you want to confirm with a
  1394. single keystroke rather than having to type \"yes\"."
  1395. :group 'org-link-follow
  1396. :type '(choice
  1397. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1398. (const :tag "with y-or-n (faster)" y-or-n-p)
  1399. (const :tag "no confirmation (dangerous)" nil)))
  1400. (put 'org-confirm-shell-link-function
  1401. 'safe-local-variable
  1402. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1403. (defconst org-file-apps-defaults-gnu
  1404. '((remote . emacs)
  1405. (system . mailcap)
  1406. (t . mailcap))
  1407. "Default file applications on a UNIX or GNU/Linux system.
  1408. See `org-file-apps'.")
  1409. (defconst org-file-apps-defaults-macosx
  1410. '((remote . emacs)
  1411. (t . "open %s")
  1412. (system . "open %s")
  1413. ("ps.gz" . "gv %s")
  1414. ("eps.gz" . "gv %s")
  1415. ("dvi" . "xdvi %s")
  1416. ("fig" . "xfig %s"))
  1417. "Default file applications on a MacOS X system.
  1418. The system \"open\" is known as a default, but we use X11 applications
  1419. for some files for which the OS does not have a good default.
  1420. See `org-file-apps'.")
  1421. (defconst org-file-apps-defaults-windowsnt
  1422. (list
  1423. '(remote . emacs)
  1424. (cons t
  1425. (list (if (featurep 'xemacs)
  1426. 'mswindows-shell-execute
  1427. 'w32-shell-execute)
  1428. "open" 'file))
  1429. (cons 'system
  1430. (list (if (featurep 'xemacs)
  1431. 'mswindows-shell-execute
  1432. 'w32-shell-execute)
  1433. "open" 'file)))
  1434. "Default file applications on a Windows NT system.
  1435. The system \"open\" is used for most files.
  1436. See `org-file-apps'.")
  1437. (defcustom org-file-apps
  1438. '(
  1439. (auto-mode . emacs)
  1440. ("\\.mm\\'" . default)
  1441. ("\\.x?html?\\'" . default)
  1442. ("\\.pdf\\'" . default)
  1443. )
  1444. "External applications for opening `file:path' items in a document.
  1445. Org-mode uses system defaults for different file types, but
  1446. you can use this variable to set the application for a given file
  1447. extension. The entries in this list are cons cells where the car identifies
  1448. files and the cdr the corresponding command. Possible values for the
  1449. file identifier are
  1450. \"string\" A string as a file identifier can be interpreted in different
  1451. ways, depending on its contents:
  1452. - Alphanumeric characters only:
  1453. Match links with this file extension.
  1454. Example: (\"pdf\" . \"evince %s\")
  1455. to open PDFs with evince.
  1456. - Regular expression: Match links where the
  1457. filename matches the regexp. If you want to
  1458. use groups here, use shy groups.
  1459. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1460. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1461. to open *.html and *.xhtml with firefox.
  1462. - Regular expression which contains (non-shy) groups:
  1463. Match links where the whole link, including \"::\", and
  1464. anything after that, matches the regexp.
  1465. In a custom command string, %1, %2, etc. are replaced with
  1466. the parts of the link that were matched by the groups.
  1467. For backwards compatibility, if a command string is given
  1468. that does not use any of the group matches, this case is
  1469. handled identically to the second one (i.e. match against
  1470. file name only).
  1471. In a custom lisp form, you can access the group matches with
  1472. (match-string n link).
  1473. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1474. to open [[file:document.pdf::5]] with evince at page 5.
  1475. `directory' Matches a directory
  1476. `remote' Matches a remote file, accessible through tramp or efs.
  1477. Remote files most likely should be visited through Emacs
  1478. because external applications cannot handle such paths.
  1479. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1480. so all files Emacs knows how to handle. Using this with
  1481. command `emacs' will open most files in Emacs. Beware that this
  1482. will also open html files inside Emacs, unless you add
  1483. (\"html\" . default) to the list as well.
  1484. t Default for files not matched by any of the other options.
  1485. `system' The system command to open files, like `open' on Windows
  1486. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1487. that will be selected if you call `C-c C-o' with a double
  1488. \\[universal-argument] \\[universal-argument] prefix.
  1489. Possible values for the command are:
  1490. `emacs' The file will be visited by the current Emacs process.
  1491. `default' Use the default application for this file type, which is the
  1492. association for t in the list, most likely in the system-specific
  1493. part.
  1494. This can be used to overrule an unwanted setting in the
  1495. system-specific variable.
  1496. `system' Use the system command for opening files, like \"open\".
  1497. This command is specified by the entry whose car is `system'.
  1498. Most likely, the system-specific version of this variable
  1499. does define this command, but you can overrule/replace it
  1500. here.
  1501. string A command to be executed by a shell; %s will be replaced
  1502. by the path to the file.
  1503. sexp A Lisp form which will be evaluated. The file path will
  1504. be available in the Lisp variable `file'.
  1505. For more examples, see the system specific constants
  1506. `org-file-apps-defaults-macosx'
  1507. `org-file-apps-defaults-windowsnt'
  1508. `org-file-apps-defaults-gnu'."
  1509. :group 'org-link-follow
  1510. :type '(repeat
  1511. (cons (choice :value ""
  1512. (string :tag "Extension")
  1513. (const :tag "System command to open files" system)
  1514. (const :tag "Default for unrecognized files" t)
  1515. (const :tag "Remote file" remote)
  1516. (const :tag "Links to a directory" directory)
  1517. (const :tag "Any files that have Emacs modes"
  1518. auto-mode))
  1519. (choice :value ""
  1520. (const :tag "Visit with Emacs" emacs)
  1521. (const :tag "Use default" default)
  1522. (const :tag "Use the system command" system)
  1523. (string :tag "Command")
  1524. (sexp :tag "Lisp form")))))
  1525. (defgroup org-refile nil
  1526. "Options concerning refiling entries in Org-mode."
  1527. :tag "Org Refile"
  1528. :group 'org)
  1529. (defcustom org-directory "~/org"
  1530. "Directory with org files.
  1531. This is just a default location to look for Org files. There is no need
  1532. at all to put your files into this directory. It is only used in the
  1533. following situations:
  1534. 1. When a remember template specifies a target file that is not an
  1535. absolute path. The path will then be interpreted relative to
  1536. `org-directory'
  1537. 2. When a remember note is filed away in an interactive way (when exiting the
  1538. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1539. with `org-directory' as the default path."
  1540. :group 'org-refile
  1541. :group 'org-remember
  1542. :type 'directory)
  1543. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1544. "Default target for storing notes.
  1545. Used as a fall back file for org-remember.el and org-capture.el, for
  1546. templates that do not specify a target file."
  1547. :group 'org-refile
  1548. :group 'org-remember
  1549. :type '(choice
  1550. (const :tag "Default from remember-data-file" nil)
  1551. file))
  1552. (defcustom org-goto-interface 'outline
  1553. "The default interface to be used for `org-goto'.
  1554. Allowed values are:
  1555. outline The interface shows an outline of the relevant file
  1556. and the correct heading is found by moving through
  1557. the outline or by searching with incremental search.
  1558. outline-path-completion Headlines in the current buffer are offered via
  1559. completion. This is the interface also used by
  1560. the refile command."
  1561. :group 'org-refile
  1562. :type '(choice
  1563. (const :tag "Outline" outline)
  1564. (const :tag "Outline-path-completion" outline-path-completion)))
  1565. (defcustom org-goto-max-level 5
  1566. "Maximum target level when running `org-goto' with refile interface."
  1567. :group 'org-refile
  1568. :type 'integer)
  1569. (defcustom org-reverse-note-order nil
  1570. "Non-nil means store new notes at the beginning of a file or entry.
  1571. When nil, new notes will be filed to the end of a file or entry.
  1572. This can also be a list with cons cells of regular expressions that
  1573. are matched against file names, and values."
  1574. :group 'org-remember
  1575. :group 'org-refile
  1576. :type '(choice
  1577. (const :tag "Reverse always" t)
  1578. (const :tag "Reverse never" nil)
  1579. (repeat :tag "By file name regexp"
  1580. (cons regexp boolean))))
  1581. (defcustom org-log-refile nil
  1582. "Information to record when a task is refiled.
  1583. Possible values are:
  1584. nil Don't add anything
  1585. time Add a time stamp to the task
  1586. note Prompt for a note and add it with template `org-log-note-headings'
  1587. This option can also be set with on a per-file-basis with
  1588. #+STARTUP: nologrefile
  1589. #+STARTUP: logrefile
  1590. #+STARTUP: lognoterefile
  1591. You can have local logging settings for a subtree by setting the LOGGING
  1592. property to one or more of these keywords.
  1593. When bulk-refiling from the agenda, the value `note' is forbidden and
  1594. will temporarily be changed to `time'."
  1595. :group 'org-refile
  1596. :group 'org-progress
  1597. :type '(choice
  1598. (const :tag "No logging" nil)
  1599. (const :tag "Record timestamp" time)
  1600. (const :tag "Record timestamp with note." note)))
  1601. (defcustom org-refile-targets nil
  1602. "Targets for refiling entries with \\[org-refile].
  1603. This is list of cons cells. Each cell contains:
  1604. - a specification of the files to be considered, either a list of files,
  1605. or a symbol whose function or variable value will be used to retrieve
  1606. a file name or a list of file names. If you use `org-agenda-files' for
  1607. that, all agenda files will be scanned for targets. Nil means consider
  1608. headings in the current buffer.
  1609. - A specification of how to find candidate refile targets. This may be
  1610. any of:
  1611. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1612. This tag has to be present in all target headlines, inheritance will
  1613. not be considered.
  1614. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1615. todo keyword.
  1616. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1617. headlines that are refiling targets.
  1618. - a cons cell (:level . N). Any headline of level N is considered a target.
  1619. Note that, when `org-odd-levels-only' is set, level corresponds to
  1620. order in hierarchy, not to the number of stars.
  1621. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1622. Note that, when `org-odd-levels-only' is set, level corresponds to
  1623. order in hierarchy, not to the number of stars.
  1624. You can set the variable `org-refile-target-verify-function' to a function
  1625. to verify each headline found by the simple criteria above.
  1626. When this variable is nil, all top-level headlines in the current buffer
  1627. are used, equivalent to the value `((nil . (:level . 1))'."
  1628. :group 'org-refile
  1629. :type '(repeat
  1630. (cons
  1631. (choice :value org-agenda-files
  1632. (const :tag "All agenda files" org-agenda-files)
  1633. (const :tag "Current buffer" nil)
  1634. (function) (variable) (file))
  1635. (choice :tag "Identify target headline by"
  1636. (cons :tag "Specific tag" (const :value :tag) (string))
  1637. (cons :tag "TODO keyword" (const :value :todo) (string))
  1638. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1639. (cons :tag "Level number" (const :value :level) (integer))
  1640. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1641. (defcustom org-refile-target-verify-function nil
  1642. "Function to verify if the headline at point should be a refile target.
  1643. The function will be called without arguments, with point at the
  1644. beginning of the headline. It should return t and leave point
  1645. where it is if the headline is a valid target for refiling.
  1646. If the target should not be selected, the function must return nil.
  1647. In addition to this, it may move point to a place from where the search
  1648. should be continued. For example, the function may decide that the entire
  1649. subtree of the current entry should be excluded and move point to the end
  1650. of the subtree."
  1651. :group 'org-refile
  1652. :type 'function)
  1653. (defcustom org-refile-use-cache nil
  1654. "Non-nil means cache refile targets to speed up the process.
  1655. The cache for a particular file will be updated automatically when
  1656. the buffer has been killed, or when any of the marker used for flagging
  1657. refile targets no longer points at a live buffer.
  1658. If you have added new entries to a buffer that might themselves be targets,
  1659. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1660. find that easier, `C-u C-u C-u C-c C-w'."
  1661. :group 'org-refile
  1662. :type 'boolean)
  1663. (defcustom org-refile-use-outline-path nil
  1664. "Non-nil means provide refile targets as paths.
  1665. So a level 3 headline will be available as level1/level2/level3.
  1666. When the value is `file', also include the file name (without directory)
  1667. into the path. In this case, you can also stop the completion after
  1668. the file name, to get entries inserted as top level in the file.
  1669. When `full-file-path', include the full file path."
  1670. :group 'org-refile
  1671. :type '(choice
  1672. (const :tag "Not" nil)
  1673. (const :tag "Yes" t)
  1674. (const :tag "Start with file name" file)
  1675. (const :tag "Start with full file path" full-file-path)))
  1676. (defcustom org-outline-path-complete-in-steps t
  1677. "Non-nil means complete the outline path in hierarchical steps.
  1678. When Org-mode uses the refile interface to select an outline path
  1679. \(see variable `org-refile-use-outline-path'), the completion of
  1680. the path can be done is a single go, or if can be done in steps down
  1681. the headline hierarchy. Going in steps is probably the best if you
  1682. do not use a special completion package like `ido' or `icicles'.
  1683. However, when using these packages, going in one step can be very
  1684. fast, while still showing the whole path to the entry."
  1685. :group 'org-refile
  1686. :type 'boolean)
  1687. (defcustom org-refile-allow-creating-parent-nodes nil
  1688. "Non-nil means allow to create new nodes as refile targets.
  1689. New nodes are then created by adding \"/new node name\" to the completion
  1690. of an existing node. When the value of this variable is `confirm',
  1691. new node creation must be confirmed by the user (recommended)
  1692. When nil, the completion must match an existing entry.
  1693. Note that, if the new heading is not seen by the criteria
  1694. listed in `org-refile-targets', multiple instances of the same
  1695. heading would be created by trying again to file under the new
  1696. heading."
  1697. :group 'org-refile
  1698. :type '(choice
  1699. (const :tag "Never" nil)
  1700. (const :tag "Always" t)
  1701. (const :tag "Prompt for confirmation" confirm)))
  1702. (defgroup org-todo nil
  1703. "Options concerning TODO items in Org-mode."
  1704. :tag "Org TODO"
  1705. :group 'org)
  1706. (defgroup org-progress nil
  1707. "Options concerning Progress logging in Org-mode."
  1708. :tag "Org Progress"
  1709. :group 'org-time)
  1710. (defvar org-todo-interpretation-widgets
  1711. '(
  1712. (:tag "Sequence (cycling hits every state)" sequence)
  1713. (:tag "Type (cycling directly to DONE)" type))
  1714. "The available interpretation symbols for customizing `org-todo-keywords'.
  1715. Interested libraries should add to this list.")
  1716. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1717. "List of TODO entry keyword sequences and their interpretation.
  1718. \\<org-mode-map>This is a list of sequences.
  1719. Each sequence starts with a symbol, either `sequence' or `type',
  1720. indicating if the keywords should be interpreted as a sequence of
  1721. action steps, or as different types of TODO items. The first
  1722. keywords are states requiring action - these states will select a headline
  1723. for inclusion into the global TODO list Org-mode produces. If one of
  1724. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1725. signify that no further action is necessary. If \"|\" is not found,
  1726. the last keyword is treated as the only DONE state of the sequence.
  1727. The command \\[org-todo] cycles an entry through these states, and one
  1728. additional state where no keyword is present. For details about this
  1729. cycling, see the manual.
  1730. TODO keywords and interpretation can also be set on a per-file basis with
  1731. the special #+SEQ_TODO and #+TYP_TODO lines.
  1732. Each keyword can optionally specify a character for fast state selection
  1733. \(in combination with the variable `org-use-fast-todo-selection')
  1734. and specifiers for state change logging, using the same syntax
  1735. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1736. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1737. indicates to record a time stamp each time this state is selected.
  1738. Each keyword may also specify if a timestamp or a note should be
  1739. recorded when entering or leaving the state, by adding additional
  1740. characters in the parenthesis after the keyword. This looks like this:
  1741. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1742. record only the time of the state change. With X and Y being either
  1743. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1744. Y when leaving the state if and only if the *target* state does not
  1745. define X. You may omit any of the fast-selection key or X or /Y,
  1746. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1747. For backward compatibility, this variable may also be just a list
  1748. of keywords - in this case the interpretation (sequence or type) will be
  1749. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1750. :group 'org-todo
  1751. :group 'org-keywords
  1752. :type '(choice
  1753. (repeat :tag "Old syntax, just keywords"
  1754. (string :tag "Keyword"))
  1755. (repeat :tag "New syntax"
  1756. (cons
  1757. (choice
  1758. :tag "Interpretation"
  1759. ;;Quick and dirty way to see
  1760. ;;`org-todo-interpretations'. This takes the
  1761. ;;place of item arguments
  1762. :convert-widget
  1763. (lambda (widget)
  1764. (widget-put widget
  1765. :args (mapcar
  1766. #'(lambda (x)
  1767. (widget-convert
  1768. (cons 'const x)))
  1769. org-todo-interpretation-widgets))
  1770. widget))
  1771. (repeat
  1772. (string :tag "Keyword"))))))
  1773. (defvar org-todo-keywords-1 nil
  1774. "All TODO and DONE keywords active in a buffer.")
  1775. (make-variable-buffer-local 'org-todo-keywords-1)
  1776. (defvar org-todo-keywords-for-agenda nil)
  1777. (defvar org-done-keywords-for-agenda nil)
  1778. (defvar org-drawers-for-agenda nil)
  1779. (defvar org-todo-keyword-alist-for-agenda nil)
  1780. (defvar org-tag-alist-for-agenda nil)
  1781. (defvar org-agenda-contributing-files nil)
  1782. (defvar org-not-done-keywords nil)
  1783. (make-variable-buffer-local 'org-not-done-keywords)
  1784. (defvar org-done-keywords nil)
  1785. (make-variable-buffer-local 'org-done-keywords)
  1786. (defvar org-todo-heads nil)
  1787. (make-variable-buffer-local 'org-todo-heads)
  1788. (defvar org-todo-sets nil)
  1789. (make-variable-buffer-local 'org-todo-sets)
  1790. (defvar org-todo-log-states nil)
  1791. (make-variable-buffer-local 'org-todo-log-states)
  1792. (defvar org-todo-kwd-alist nil)
  1793. (make-variable-buffer-local 'org-todo-kwd-alist)
  1794. (defvar org-todo-key-alist nil)
  1795. (make-variable-buffer-local 'org-todo-key-alist)
  1796. (defvar org-todo-key-trigger nil)
  1797. (make-variable-buffer-local 'org-todo-key-trigger)
  1798. (defcustom org-todo-interpretation 'sequence
  1799. "Controls how TODO keywords are interpreted.
  1800. This variable is in principle obsolete and is only used for
  1801. backward compatibility, if the interpretation of todo keywords is
  1802. not given already in `org-todo-keywords'. See that variable for
  1803. more information."
  1804. :group 'org-todo
  1805. :group 'org-keywords
  1806. :type '(choice (const sequence)
  1807. (const type)))
  1808. (defcustom org-use-fast-todo-selection t
  1809. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1810. This variable describes if and under what circumstances the cycling
  1811. mechanism for TODO keywords will be replaced by a single-key, direct
  1812. selection scheme.
  1813. When nil, fast selection is never used.
  1814. When the symbol `prefix', it will be used when `org-todo' is called with
  1815. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1816. in an agenda buffer.
  1817. When t, fast selection is used by default. In this case, the prefix
  1818. argument forces cycling instead.
  1819. In all cases, the special interface is only used if access keys have actually
  1820. been assigned by the user, i.e. if keywords in the configuration are followed
  1821. by a letter in parenthesis, like TODO(t)."
  1822. :group 'org-todo
  1823. :type '(choice
  1824. (const :tag "Never" nil)
  1825. (const :tag "By default" t)
  1826. (const :tag "Only with C-u C-c C-t" prefix)))
  1827. (defcustom org-provide-todo-statistics t
  1828. "Non-nil means update todo statistics after insert and toggle.
  1829. ALL-HEADLINES means update todo statistics by including headlines
  1830. with no TODO keyword as well, counting them as not done.
  1831. A list of TODO keywords means the same, but skip keywords that are
  1832. not in this list.
  1833. When this is set, todo statistics is updated in the parent of the
  1834. current entry each time a todo state is changed."
  1835. :group 'org-todo
  1836. :type '(choice
  1837. (const :tag "Yes, only for TODO entries" t)
  1838. (const :tag "Yes, including all entries" 'all-headlines)
  1839. (repeat :tag "Yes, for TODOs in this list"
  1840. (string :tag "TODO keyword"))
  1841. (other :tag "No TODO statistics" nil)))
  1842. (defcustom org-hierarchical-todo-statistics t
  1843. "Non-nil means TODO statistics covers just direct children.
  1844. When nil, all entries in the subtree are considered.
  1845. This has only an effect if `org-provide-todo-statistics' is set.
  1846. To set this to nil for only a single subtree, use a COOKIE_DATA
  1847. property and include the word \"recursive\" into the value."
  1848. :group 'org-todo
  1849. :type 'boolean)
  1850. (defcustom org-after-todo-state-change-hook nil
  1851. "Hook which is run after the state of a TODO item was changed.
  1852. The new state (a string with a TODO keyword, or nil) is available in the
  1853. Lisp variable `state'."
  1854. :group 'org-todo
  1855. :type 'hook)
  1856. (defvar org-blocker-hook nil
  1857. "Hook for functions that are allowed to block a state change.
  1858. Each function gets as its single argument a property list, see
  1859. `org-trigger-hook' for more information about this list.
  1860. If any of the functions in this hook returns nil, the state change
  1861. is blocked.")
  1862. (defvar org-trigger-hook nil
  1863. "Hook for functions that are triggered by a state change.
  1864. Each function gets as its single argument a property list with at least
  1865. the following elements:
  1866. (:type type-of-change :position pos-at-entry-start
  1867. :from old-state :to new-state)
  1868. Depending on the type, more properties may be present.
  1869. This mechanism is currently implemented for:
  1870. TODO state changes
  1871. ------------------
  1872. :type todo-state-change
  1873. :from previous state (keyword as a string), or nil, or a symbol
  1874. 'todo' or 'done', to indicate the general type of state.
  1875. :to new state, like in :from")
  1876. (defcustom org-enforce-todo-dependencies nil
  1877. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1878. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1879. be blocked if any prior sibling is not yet done.
  1880. Finally, if the parent is blocked because of ordered siblings of its own,
  1881. the child will also be blocked.
  1882. This variable needs to be set before org.el is loaded, and you need to
  1883. restart Emacs after a change to make the change effective. The only way
  1884. to change is while Emacs is running is through the customize interface."
  1885. :set (lambda (var val)
  1886. (set var val)
  1887. (if val
  1888. (add-hook 'org-blocker-hook
  1889. 'org-block-todo-from-children-or-siblings-or-parent)
  1890. (remove-hook 'org-blocker-hook
  1891. 'org-block-todo-from-children-or-siblings-or-parent)))
  1892. :group 'org-todo
  1893. :type 'boolean)
  1894. (defcustom org-enforce-todo-checkbox-dependencies nil
  1895. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1896. When this is nil, checkboxes have no influence on switching TODO states.
  1897. When non-nil, you first need to check off all check boxes before the TODO
  1898. entry can be switched to DONE.
  1899. This variable needs to be set before org.el is loaded, and you need to
  1900. restart Emacs after a change to make the change effective. The only way
  1901. to change is while Emacs is running is through the customize interface."
  1902. :set (lambda (var val)
  1903. (set var val)
  1904. (if val
  1905. (add-hook 'org-blocker-hook
  1906. 'org-block-todo-from-checkboxes)
  1907. (remove-hook 'org-blocker-hook
  1908. 'org-block-todo-from-checkboxes)))
  1909. :group 'org-todo
  1910. :type 'boolean)
  1911. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1912. "Non-nil means inserting a TODO heading is treated as state change.
  1913. So when the command \\[org-insert-todo-heading] is used, state change
  1914. logging will apply if appropriate. When nil, the new TODO item will
  1915. be inserted directly, and no logging will take place."
  1916. :group 'org-todo
  1917. :type 'boolean)
  1918. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1919. "Non-nil means switching TODO states with S-cursor counts as state change.
  1920. This is the default behavior. However, setting this to nil allows a
  1921. convenient way to select a TODO state and bypass any logging associated
  1922. with that."
  1923. :group 'org-todo
  1924. :type 'boolean)
  1925. (defcustom org-todo-state-tags-triggers nil
  1926. "Tag changes that should be triggered by TODO state changes.
  1927. This is a list. Each entry is
  1928. (state-change (tag . flag) .......)
  1929. State-change can be a string with a state, and empty string to indicate the
  1930. state that has no TODO keyword, or it can be one of the symbols `todo'
  1931. or `done', meaning any not-done or done state, respectively."
  1932. :group 'org-todo
  1933. :group 'org-tags
  1934. :type '(repeat
  1935. (cons (choice :tag "When changing to"
  1936. (const :tag "Not-done state" todo)
  1937. (const :tag "Done state" done)
  1938. (string :tag "State"))
  1939. (repeat
  1940. (cons :tag "Tag action"
  1941. (string :tag "Tag")
  1942. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1943. (defcustom org-log-done nil
  1944. "Information to record when a task moves to the DONE state.
  1945. Possible values are:
  1946. nil Don't add anything, just change the keyword
  1947. time Add a time stamp to the task
  1948. note Prompt for a note and add it with template `org-log-note-headings'
  1949. This option can also be set with on a per-file-basis with
  1950. #+STARTUP: nologdone
  1951. #+STARTUP: logdone
  1952. #+STARTUP: lognotedone
  1953. You can have local logging settings for a subtree by setting the LOGGING
  1954. property to one or more of these keywords."
  1955. :group 'org-todo
  1956. :group 'org-progress
  1957. :type '(choice
  1958. (const :tag "No logging" nil)
  1959. (const :tag "Record CLOSED timestamp" time)
  1960. (const :tag "Record CLOSED timestamp with note." note)))
  1961. ;; Normalize old uses of org-log-done.
  1962. (cond
  1963. ((eq org-log-done t) (setq org-log-done 'time))
  1964. ((and (listp org-log-done) (memq 'done org-log-done))
  1965. (setq org-log-done 'note)))
  1966. (defcustom org-log-reschedule nil
  1967. "Information to record when the scheduling date of a tasks is modified.
  1968. Possible values are:
  1969. nil Don't add anything, just change the date
  1970. time Add a time stamp to the task
  1971. note Prompt for a note and add it with template `org-log-note-headings'
  1972. This option can also be set with on a per-file-basis with
  1973. #+STARTUP: nologreschedule
  1974. #+STARTUP: logreschedule
  1975. #+STARTUP: lognotereschedule"
  1976. :group 'org-todo
  1977. :group 'org-progress
  1978. :type '(choice
  1979. (const :tag "No logging" nil)
  1980. (const :tag "Record timestamp" time)
  1981. (const :tag "Record timestamp with note." note)))
  1982. (defcustom org-log-redeadline nil
  1983. "Information to record when the deadline date of a tasks is modified.
  1984. Possible values are:
  1985. nil Don't add anything, just change the date
  1986. time Add a time stamp to the task
  1987. note Prompt for a note and add it with template `org-log-note-headings'
  1988. This option can also be set with on a per-file-basis with
  1989. #+STARTUP: nologredeadline
  1990. #+STARTUP: logredeadline
  1991. #+STARTUP: lognoteredeadline
  1992. You can have local logging settings for a subtree by setting the LOGGING
  1993. property to one or more of these keywords."
  1994. :group 'org-todo
  1995. :group 'org-progress
  1996. :type '(choice
  1997. (const :tag "No logging" nil)
  1998. (const :tag "Record timestamp" time)
  1999. (const :tag "Record timestamp with note." note)))
  2000. (defcustom org-log-note-clock-out nil
  2001. "Non-nil means record a note when clocking out of an item.
  2002. This can also be configured on a per-file basis by adding one of
  2003. the following lines anywhere in the buffer:
  2004. #+STARTUP: lognoteclock-out
  2005. #+STARTUP: nolognoteclock-out"
  2006. :group 'org-todo
  2007. :group 'org-progress
  2008. :type 'boolean)
  2009. (defcustom org-log-done-with-time t
  2010. "Non-nil means the CLOSED time stamp will contain date and time.
  2011. When nil, only the date will be recorded."
  2012. :group 'org-progress
  2013. :type 'boolean)
  2014. (defcustom org-log-note-headings
  2015. '((done . "CLOSING NOTE %t")
  2016. (state . "State %-12s from %-12S %t")
  2017. (note . "Note taken on %t")
  2018. (reschedule . "Rescheduled from %S on %t")
  2019. (delschedule . "Not scheduled, was %S on %t")
  2020. (redeadline . "New deadline from %S on %t")
  2021. (deldeadline . "Removed deadline, was %S on %t")
  2022. (refile . "Refiled on %t")
  2023. (clock-out . ""))
  2024. "Headings for notes added to entries.
  2025. The value is an alist, with the car being a symbol indicating the note
  2026. context, and the cdr is the heading to be used. The heading may also be the
  2027. empty string.
  2028. %t in the heading will be replaced by a time stamp.
  2029. %T will be an active time stamp instead the default inactive one
  2030. %s will be replaced by the new TODO state, in double quotes.
  2031. %S will be replaced by the old TODO state, in double quotes.
  2032. %u will be replaced by the user name.
  2033. %U will be replaced by the full user name.
  2034. In fact, it is not a good idea to change the `state' entry, because
  2035. agenda log mode depends on the format of these entries."
  2036. :group 'org-todo
  2037. :group 'org-progress
  2038. :type '(list :greedy t
  2039. (cons (const :tag "Heading when closing an item" done) string)
  2040. (cons (const :tag
  2041. "Heading when changing todo state (todo sequence only)"
  2042. state) string)
  2043. (cons (const :tag "Heading when just taking a note" note) string)
  2044. (cons (const :tag "Heading when clocking out" clock-out) string)
  2045. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2046. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2047. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2048. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2049. (cons (const :tag "Heading when refiling" refile) string)))
  2050. (unless (assq 'note org-log-note-headings)
  2051. (push '(note . "%t") org-log-note-headings))
  2052. (defcustom org-log-into-drawer nil
  2053. "Non-nil means insert state change notes and time stamps into a drawer.
  2054. When nil, state changes notes will be inserted after the headline and
  2055. any scheduling and clock lines, but not inside a drawer.
  2056. The value of this variable should be the name of the drawer to use.
  2057. LOGBOOK is proposed at the default drawer for this purpose, you can
  2058. also set this to a string to define the drawer of your choice.
  2059. A value of t is also allowed, representing \"LOGBOOK\".
  2060. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2061. will be ignored.
  2062. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2063. a subtree."
  2064. :group 'org-todo
  2065. :group 'org-progress
  2066. :type '(choice
  2067. (const :tag "Not into a drawer" nil)
  2068. (const :tag "LOGBOOK" t)
  2069. (string :tag "Other")))
  2070. (if (fboundp 'defvaralias)
  2071. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2072. (defun org-log-into-drawer ()
  2073. "Return the value of `org-log-into-drawer', but let properties overrule.
  2074. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2075. used instead of the default value."
  2076. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  2077. (cond
  2078. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2079. ((equal p "t") "LOGBOOK")
  2080. (t p))))
  2081. (defcustom org-log-state-notes-insert-after-drawers nil
  2082. "Non-nil means insert state change notes after any drawers in entry.
  2083. Only the drawers that *immediately* follow the headline and the
  2084. deadline/scheduled line are skipped.
  2085. When nil, insert notes right after the heading and perhaps the line
  2086. with deadline/scheduling if present.
  2087. This variable will have no effect if `org-log-into-drawer' is
  2088. set."
  2089. :group 'org-todo
  2090. :group 'org-progress
  2091. :type 'boolean)
  2092. (defcustom org-log-states-order-reversed t
  2093. "Non-nil means the latest state note will be directly after heading.
  2094. When nil, the state change notes will be ordered according to time."
  2095. :group 'org-todo
  2096. :group 'org-progress
  2097. :type 'boolean)
  2098. (defcustom org-todo-repeat-to-state nil
  2099. "The TODO state to which a repeater should return the repeating task.
  2100. By default this is the first task in a TODO sequence, or the previous state
  2101. in a TODO_TYP set. But you can specify another task here.
  2102. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2103. :group 'org-todo
  2104. :type '(choice (const :tag "Head of sequence" nil)
  2105. (string :tag "Specific state")))
  2106. (defcustom org-log-repeat 'time
  2107. "Non-nil means record moving through the DONE state when triggering repeat.
  2108. An auto-repeating task is immediately switched back to TODO when
  2109. marked DONE. If you are not logging state changes (by adding \"@\"
  2110. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2111. record a closing note, there will be no record of the task moving
  2112. through DONE. This variable forces taking a note anyway.
  2113. nil Don't force a record
  2114. time Record a time stamp
  2115. note Record a note
  2116. This option can also be set with on a per-file-basis with
  2117. #+STARTUP: logrepeat
  2118. #+STARTUP: lognoterepeat
  2119. #+STARTUP: nologrepeat
  2120. You can have local logging settings for a subtree by setting the LOGGING
  2121. property to one or more of these keywords."
  2122. :group 'org-todo
  2123. :group 'org-progress
  2124. :type '(choice
  2125. (const :tag "Don't force a record" nil)
  2126. (const :tag "Force recording the DONE state" time)
  2127. (const :tag "Force recording a note with the DONE state" note)))
  2128. (defgroup org-priorities nil
  2129. "Priorities in Org-mode."
  2130. :tag "Org Priorities"
  2131. :group 'org-todo)
  2132. (defcustom org-enable-priority-commands t
  2133. "Non-nil means priority commands are active.
  2134. When nil, these commands will be disabled, so that you never accidentally
  2135. set a priority."
  2136. :group 'org-priorities
  2137. :type 'boolean)
  2138. (defcustom org-highest-priority ?A
  2139. "The highest priority of TODO items. A character like ?A, ?B etc.
  2140. Must have a smaller ASCII number than `org-lowest-priority'."
  2141. :group 'org-priorities
  2142. :type 'character)
  2143. (defcustom org-lowest-priority ?C
  2144. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2145. Must have a larger ASCII number than `org-highest-priority'."
  2146. :group 'org-priorities
  2147. :type 'character)
  2148. (defcustom org-default-priority ?B
  2149. "The default priority of TODO items.
  2150. This is the priority an item get if no explicit priority is given."
  2151. :group 'org-priorities
  2152. :type 'character)
  2153. (defcustom org-priority-start-cycle-with-default t
  2154. "Non-nil means start with default priority when starting to cycle.
  2155. When this is nil, the first step in the cycle will be (depending on the
  2156. command used) one higher or lower that the default priority."
  2157. :group 'org-priorities
  2158. :type 'boolean)
  2159. (defcustom org-get-priority-function nil
  2160. "Function to extract the priority from a string.
  2161. The string is normally the headline. If this is nil Org computes the
  2162. priority from the priority cookie like [#A] in the headline. It returns
  2163. an integer, increasing by 1000 for each priority level.
  2164. The user can set a different function here, which should take a string
  2165. as an argument and return the numeric priority."
  2166. :group 'org-priorities
  2167. :type 'function)
  2168. (defgroup org-time nil
  2169. "Options concerning time stamps and deadlines in Org-mode."
  2170. :tag "Org Time"
  2171. :group 'org)
  2172. (defcustom org-insert-labeled-timestamps-at-point nil
  2173. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2174. When nil, these labeled time stamps are forces into the second line of an
  2175. entry, just after the headline. When scheduling from the global TODO list,
  2176. the time stamp will always be forced into the second line."
  2177. :group 'org-time
  2178. :type 'boolean)
  2179. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2180. "Formats for `format-time-string' which are used for time stamps.
  2181. It is not recommended to change this constant.")
  2182. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2183. "Number of minutes to round time stamps to.
  2184. These are two values, the first applies when first creating a time stamp.
  2185. The second applies when changing it with the commands `S-up' and `S-down'.
  2186. When changing the time stamp, this means that it will change in steps
  2187. of N minutes, as given by the second value.
  2188. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2189. numbers should be factors of 60, so for example 5, 10, 15.
  2190. When this is larger than 1, you can still force an exact time stamp by using
  2191. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2192. and by using a prefix arg to `S-up/down' to specify the exact number
  2193. of minutes to shift."
  2194. :group 'org-time
  2195. :get '(lambda (var) ; Make sure both elements are there
  2196. (if (integerp (default-value var))
  2197. (list (default-value var) 5)
  2198. (default-value var)))
  2199. :type '(list
  2200. (integer :tag "when inserting times")
  2201. (integer :tag "when modifying times")))
  2202. ;; Normalize old customizations of this variable.
  2203. (when (integerp org-time-stamp-rounding-minutes)
  2204. (setq org-time-stamp-rounding-minutes
  2205. (list org-time-stamp-rounding-minutes
  2206. org-time-stamp-rounding-minutes)))
  2207. (defcustom org-display-custom-times nil
  2208. "Non-nil means overlay custom formats over all time stamps.
  2209. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2210. To turn this on on a per-file basis, insert anywhere in the file:
  2211. #+STARTUP: customtime"
  2212. :group 'org-time
  2213. :set 'set-default
  2214. :type 'sexp)
  2215. (make-variable-buffer-local 'org-display-custom-times)
  2216. (defcustom org-time-stamp-custom-formats
  2217. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2218. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2219. These are overlayed over the default ISO format if the variable
  2220. `org-display-custom-times' is set. Time like %H:%M should be at the
  2221. end of the second format. The custom formats are also honored by export
  2222. commands, if custom time display is turned on at the time of export."
  2223. :group 'org-time
  2224. :type 'sexp)
  2225. (defun org-time-stamp-format (&optional long inactive)
  2226. "Get the right format for a time string."
  2227. (let ((f (if long (cdr org-time-stamp-formats)
  2228. (car org-time-stamp-formats))))
  2229. (if inactive
  2230. (concat "[" (substring f 1 -1) "]")
  2231. f)))
  2232. (defcustom org-time-clocksum-format "%d:%02d"
  2233. "The format string used when creating CLOCKSUM lines.
  2234. This is also used when org-mode generates a time duration."
  2235. :group 'org-time
  2236. :type 'string)
  2237. (defcustom org-time-clocksum-use-fractional nil
  2238. "If non-nil, \\[org-clock-display] uses fractional times.
  2239. org-mode generates a time duration."
  2240. :group 'org-time
  2241. :type 'boolean)
  2242. (defcustom org-time-clocksum-fractional-format "%.2f"
  2243. "The format string used when creating CLOCKSUM lines, or when
  2244. org-mode generates a time duration."
  2245. :group 'org-time
  2246. :type 'string)
  2247. (defcustom org-deadline-warning-days 14
  2248. "No. of days before expiration during which a deadline becomes active.
  2249. This variable governs the display in sparse trees and in the agenda.
  2250. When 0 or negative, it means use this number (the absolute value of it)
  2251. even if a deadline has a different individual lead time specified.
  2252. Custom commands can set this variable in the options section."
  2253. :group 'org-time
  2254. :group 'org-agenda-daily/weekly
  2255. :type 'integer)
  2256. (defcustom org-read-date-prefer-future t
  2257. "Non-nil means assume future for incomplete date input from user.
  2258. This affects the following situations:
  2259. 1. The user gives a month but not a year.
  2260. For example, if it is April and you enter \"feb 2\", this will be read
  2261. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2262. 2. The user gives a day, but no month.
  2263. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2264. read this as the third of *next* month. However, if you enter \"17\",
  2265. it will be considered as *this* month.
  2266. If you set this variable to the symbol `time', then also the following
  2267. will work:
  2268. 3. If the user gives a time, but no day. If the time is before now,
  2269. to will be interpreted as tomorrow.
  2270. Currently none of this works for ISO week specifications.
  2271. When this option is nil, the current day, month and year will always be
  2272. used as defaults.
  2273. See also `org-agenda-jump-prefer-future'."
  2274. :group 'org-time
  2275. :type '(choice
  2276. (const :tag "Never" nil)
  2277. (const :tag "Check month and day" t)
  2278. (const :tag "Check month, day, and time" time)))
  2279. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2280. "Should the agenda jump command prefer the future for incomplete dates?
  2281. The default is to do the same as configured in `org-read-date-prefer-future'.
  2282. But you can alse set a deviating value here.
  2283. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2284. :group 'org-agenda
  2285. :group 'org-time
  2286. :type '(choice
  2287. (const :tag "Use org-read-date-prefer-future"
  2288. org-read-date-prefer-future)
  2289. (const :tag "Never" nil)
  2290. (const :tag "Always" t)))
  2291. (defcustom org-read-date-display-live t
  2292. "Non-nil means display current interpretation of date prompt live.
  2293. This display will be in an overlay, in the minibuffer."
  2294. :group 'org-time
  2295. :type 'boolean)
  2296. (defcustom org-read-date-popup-calendar t
  2297. "Non-nil means pop up a calendar when prompting for a date.
  2298. In the calendar, the date can be selected with mouse-1. However, the
  2299. minibuffer will also be active, and you can simply enter the date as well.
  2300. When nil, only the minibuffer will be available."
  2301. :group 'org-time
  2302. :type 'boolean)
  2303. (if (fboundp 'defvaralias)
  2304. (defvaralias 'org-popup-calendar-for-date-prompt
  2305. 'org-read-date-popup-calendar))
  2306. (defcustom org-read-date-minibuffer-setup-hook nil
  2307. "Hook to be used to set up keys for the date/time interface.
  2308. Add key definitions to `minibuffer-local-map', which will be a temporary
  2309. copy."
  2310. :group 'org-time
  2311. :type 'hook)
  2312. (defcustom org-extend-today-until 0
  2313. "The hour when your day really ends. Must be an integer.
  2314. This has influence for the following applications:
  2315. - When switching the agenda to \"today\". It it is still earlier than
  2316. the time given here, the day recognized as TODAY is actually yesterday.
  2317. - When a date is read from the user and it is still before the time given
  2318. here, the current date and time will be assumed to be yesterday, 23:59.
  2319. Also, timestamps inserted in remember templates follow this rule.
  2320. IMPORTANT: This is a feature whose implementation is and likely will
  2321. remain incomplete. Really, it is only here because past midnight seems to
  2322. be the favorite working time of John Wiegley :-)"
  2323. :group 'org-time
  2324. :type 'integer)
  2325. (defcustom org-edit-timestamp-down-means-later nil
  2326. "Non-nil means S-down will increase the time in a time stamp.
  2327. When nil, S-up will increase."
  2328. :group 'org-time
  2329. :type 'boolean)
  2330. (defcustom org-calendar-follow-timestamp-change t
  2331. "Non-nil means make the calendar window follow timestamp changes.
  2332. When a timestamp is modified and the calendar window is visible, it will be
  2333. moved to the new date."
  2334. :group 'org-time
  2335. :type 'boolean)
  2336. (defgroup org-tags nil
  2337. "Options concerning tags in Org-mode."
  2338. :tag "Org Tags"
  2339. :group 'org)
  2340. (defcustom org-tag-alist nil
  2341. "List of tags allowed in Org-mode files.
  2342. When this list is nil, Org-mode will base TAG input on what is already in the
  2343. buffer.
  2344. The value of this variable is an alist, the car of each entry must be a
  2345. keyword as a string, the cdr may be a character that is used to select
  2346. that tag through the fast-tag-selection interface.
  2347. See the manual for details."
  2348. :group 'org-tags
  2349. :type '(repeat
  2350. (choice
  2351. (cons (string :tag "Tag name")
  2352. (character :tag "Access char"))
  2353. (list :tag "Start radio group"
  2354. (const :startgroup)
  2355. (option (string :tag "Group description")))
  2356. (list :tag "End radio group"
  2357. (const :endgroup)
  2358. (option (string :tag "Group description")))
  2359. (const :tag "New line" (:newline)))))
  2360. (defcustom org-tag-persistent-alist nil
  2361. "List of tags that will always appear in all Org-mode files.
  2362. This is in addition to any in buffer settings or customizations
  2363. of `org-tag-alist'.
  2364. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2365. The value of this variable is an alist, the car of each entry must be a
  2366. keyword as a string, the cdr may be a character that is used to select
  2367. that tag through the fast-tag-selection interface.
  2368. See the manual for details.
  2369. To disable these tags on a per-file basis, insert anywhere in the file:
  2370. #+STARTUP: noptag"
  2371. :group 'org-tags
  2372. :type '(repeat
  2373. (choice
  2374. (cons (string :tag "Tag name")
  2375. (character :tag "Access char"))
  2376. (const :tag "Start radio group" (:startgroup))
  2377. (const :tag "End radio group" (:endgroup))
  2378. (const :tag "New line" (:newline)))))
  2379. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2380. "If non-nil, always offer completion for all tags of all agenda files.
  2381. Instead of customizing this variable directly, you might want to
  2382. set it locally for capture buffers, because there no list of
  2383. tags in that file can be created dynamically (there are none).
  2384. (add-hook 'org-capture-mode-hook
  2385. (lambda ()
  2386. (set (make-local-variable
  2387. 'org-complete-tags-always-offer-all-agenda-tags)
  2388. t)))"
  2389. :group 'org-tags
  2390. :type 'boolean)
  2391. (defvar org-file-tags nil
  2392. "List of tags that can be inherited by all entries in the file.
  2393. The tags will be inherited if the variable `org-use-tag-inheritance'
  2394. says they should be.
  2395. This variable is populated from #+FILETAGS lines.")
  2396. (defcustom org-use-fast-tag-selection 'auto
  2397. "Non-nil means use fast tag selection scheme.
  2398. This is a special interface to select and deselect tags with single keys.
  2399. When nil, fast selection is never used.
  2400. When the symbol `auto', fast selection is used if and only if selection
  2401. characters for tags have been configured, either through the variable
  2402. `org-tag-alist' or through a #+TAGS line in the buffer.
  2403. When t, fast selection is always used and selection keys are assigned
  2404. automatically if necessary."
  2405. :group 'org-tags
  2406. :type '(choice
  2407. (const :tag "Always" t)
  2408. (const :tag "Never" nil)
  2409. (const :tag "When selection characters are configured" 'auto)))
  2410. (defcustom org-fast-tag-selection-single-key nil
  2411. "Non-nil means fast tag selection exits after first change.
  2412. When nil, you have to press RET to exit it.
  2413. During fast tag selection, you can toggle this flag with `C-c'.
  2414. This variable can also have the value `expert'. In this case, the window
  2415. displaying the tags menu is not even shown, until you press C-c again."
  2416. :group 'org-tags
  2417. :type '(choice
  2418. (const :tag "No" nil)
  2419. (const :tag "Yes" t)
  2420. (const :tag "Expert" expert)))
  2421. (defvar org-fast-tag-selection-include-todo nil
  2422. "Non-nil means fast tags selection interface will also offer TODO states.
  2423. This is an undocumented feature, you should not rely on it.")
  2424. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2425. "The column to which tags should be indented in a headline.
  2426. If this number is positive, it specifies the column. If it is negative,
  2427. it means that the tags should be flushright to that column. For example,
  2428. -80 works well for a normal 80 character screen."
  2429. :group 'org-tags
  2430. :type 'integer)
  2431. (defcustom org-auto-align-tags t
  2432. "Non-nil means realign tags after pro/demotion of TODO state change.
  2433. These operations change the length of a headline and therefore shift
  2434. the tags around. With this options turned on, after each such operation
  2435. the tags are again aligned to `org-tags-column'."
  2436. :group 'org-tags
  2437. :type 'boolean)
  2438. (defcustom org-use-tag-inheritance t
  2439. "Non-nil means tags in levels apply also for sublevels.
  2440. When nil, only the tags directly given in a specific line apply there.
  2441. This may also be a list of tags that should be inherited, or a regexp that
  2442. matches tags that should be inherited. Additional control is possible
  2443. with the variable `org-tags-exclude-from-inheritance' which gives an
  2444. explicit list of tags to be excluded from inheritance., even if the value of
  2445. `org-use-tag-inheritance' would select it for inheritance.
  2446. If this option is t, a match early-on in a tree can lead to a large
  2447. number of matches in the subtree when constructing the agenda or creating
  2448. a sparse tree. If you only want to see the first match in a tree during
  2449. a search, check out the variable `org-tags-match-list-sublevels'."
  2450. :group 'org-tags
  2451. :type '(choice
  2452. (const :tag "Not" nil)
  2453. (const :tag "Always" t)
  2454. (repeat :tag "Specific tags" (string :tag "Tag"))
  2455. (regexp :tag "Tags matched by regexp")))
  2456. (defcustom org-tags-exclude-from-inheritance nil
  2457. "List of tags that should never be inherited.
  2458. This is a way to exclude a few tags from inheritance. For way to do
  2459. the opposite, to actively allow inheritance for selected tags,
  2460. see the variable `org-use-tag-inheritance'."
  2461. :group 'org-tags
  2462. :type '(repeat (string :tag "Tag")))
  2463. (defun org-tag-inherit-p (tag)
  2464. "Check if TAG is one that should be inherited."
  2465. (cond
  2466. ((member tag org-tags-exclude-from-inheritance) nil)
  2467. ((eq org-use-tag-inheritance t) t)
  2468. ((not org-use-tag-inheritance) nil)
  2469. ((stringp org-use-tag-inheritance)
  2470. (string-match org-use-tag-inheritance tag))
  2471. ((listp org-use-tag-inheritance)
  2472. (member tag org-use-tag-inheritance))
  2473. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2474. (defcustom org-tags-match-list-sublevels t
  2475. "Non-nil means list also sublevels of headlines matching a search.
  2476. This variable applies to tags/property searches, and also to stuck
  2477. projects because this search is based on a tags match as well.
  2478. When set to the symbol `indented', sublevels are indented with
  2479. leading dots.
  2480. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2481. the sublevels of a headline matching a tag search often also match
  2482. the same search. Listing all of them can create very long lists.
  2483. Setting this variable to nil causes subtrees of a match to be skipped.
  2484. This variable is semi-obsolete and probably should always be true. It
  2485. is better to limit inheritance to certain tags using the variables
  2486. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2487. :group 'org-tags
  2488. :type '(choice
  2489. (const :tag "No, don't list them" nil)
  2490. (const :tag "Yes, do list them" t)
  2491. (const :tag "List them, indented with leading dots" indented)))
  2492. (defcustom org-tags-sort-function nil
  2493. "When set, tags are sorted using this function as a comparator."
  2494. :group 'org-tags
  2495. :type '(choice
  2496. (const :tag "No sorting" nil)
  2497. (const :tag "Alphabetical" string<)
  2498. (const :tag "Reverse alphabetical" string>)
  2499. (function :tag "Custom function" nil)))
  2500. (defvar org-tags-history nil
  2501. "History of minibuffer reads for tags.")
  2502. (defvar org-last-tags-completion-table nil
  2503. "The last used completion table for tags.")
  2504. (defvar org-after-tags-change-hook nil
  2505. "Hook that is run after the tags in a line have changed.")
  2506. (defgroup org-properties nil
  2507. "Options concerning properties in Org-mode."
  2508. :tag "Org Properties"
  2509. :group 'org)
  2510. (defcustom org-property-format "%-10s %s"
  2511. "How property key/value pairs should be formatted by `indent-line'.
  2512. When `indent-line' hits a property definition, it will format the line
  2513. according to this format, mainly to make sure that the values are
  2514. lined-up with respect to each other."
  2515. :group 'org-properties
  2516. :type 'string)
  2517. (defcustom org-use-property-inheritance nil
  2518. "Non-nil means properties apply also for sublevels.
  2519. This setting is chiefly used during property searches. Turning it on can
  2520. cause significant overhead when doing a search, which is why it is not
  2521. on by default.
  2522. When nil, only the properties directly given in the current entry count.
  2523. When t, every property is inherited. The value may also be a list of
  2524. properties that should have inheritance, or a regular expression matching
  2525. properties that should be inherited.
  2526. However, note that some special properties use inheritance under special
  2527. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2528. and the properties ending in \"_ALL\" when they are used as descriptor
  2529. for valid values of a property.
  2530. Note for programmers:
  2531. When querying an entry with `org-entry-get', you can control if inheritance
  2532. should be used. By default, `org-entry-get' looks only at the local
  2533. properties. You can request inheritance by setting the inherit argument
  2534. to t (to force inheritance) or to `selective' (to respect the setting
  2535. in this variable)."
  2536. :group 'org-properties
  2537. :type '(choice
  2538. (const :tag "Not" nil)
  2539. (const :tag "Always" t)
  2540. (repeat :tag "Specific properties" (string :tag "Property"))
  2541. (regexp :tag "Properties matched by regexp")))
  2542. (defun org-property-inherit-p (property)
  2543. "Check if PROPERTY is one that should be inherited."
  2544. (cond
  2545. ((eq org-use-property-inheritance t) t)
  2546. ((not org-use-property-inheritance) nil)
  2547. ((stringp org-use-property-inheritance)
  2548. (string-match org-use-property-inheritance property))
  2549. ((listp org-use-property-inheritance)
  2550. (member property org-use-property-inheritance))
  2551. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2552. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2553. "The default column format, if no other format has been defined.
  2554. This variable can be set on the per-file basis by inserting a line
  2555. #+COLUMNS: %25ITEM ....."
  2556. :group 'org-properties
  2557. :type 'string)
  2558. (defcustom org-columns-ellipses ".."
  2559. "The ellipses to be used when a field in column view is truncated.
  2560. When this is the empty string, as many characters as possible are shown,
  2561. but then there will be no visual indication that the field has been truncated.
  2562. When this is a string of length N, the last N characters of a truncated
  2563. field are replaced by this string. If the column is narrower than the
  2564. ellipses string, only part of the ellipses string will be shown."
  2565. :group 'org-properties
  2566. :type 'string)
  2567. (defcustom org-columns-modify-value-for-display-function nil
  2568. "Function that modifies values for display in column view.
  2569. For example, it can be used to cut out a certain part from a time stamp.
  2570. The function must take 2 arguments:
  2571. column-title The title of the column (*not* the property name)
  2572. value The value that should be modified.
  2573. The function should return the value that should be displayed,
  2574. or nil if the normal value should be used."
  2575. :group 'org-properties
  2576. :type 'function)
  2577. (defcustom org-effort-property "Effort"
  2578. "The property that is being used to keep track of effort estimates.
  2579. Effort estimates given in this property need to have the format H:MM."
  2580. :group 'org-properties
  2581. :group 'org-progress
  2582. :type '(string :tag "Property"))
  2583. (defconst org-global-properties-fixed
  2584. '(("VISIBILITY_ALL" . "folded children content all")
  2585. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2586. "List of property/value pairs that can be inherited by any entry.
  2587. These are fixed values, for the preset properties. The user variable
  2588. that can be used to add to this list is `org-global-properties'.
  2589. The entries in this list are cons cells where the car is a property
  2590. name and cdr is a string with the value. If the value represents
  2591. multiple items like an \"_ALL\" property, separate the items by
  2592. spaces.")
  2593. (defcustom org-global-properties nil
  2594. "List of property/value pairs that can be inherited by any entry.
  2595. This list will be combined with the constant `org-global-properties-fixed'.
  2596. The entries in this list are cons cells where the car is a property
  2597. name and cdr is a string with the value.
  2598. You can set buffer-local values for the same purpose in the variable
  2599. `org-file-properties' this by adding lines like
  2600. #+PROPERTY: NAME VALUE"
  2601. :group 'org-properties
  2602. :type '(repeat
  2603. (cons (string :tag "Property")
  2604. (string :tag "Value"))))
  2605. (defvar org-file-properties nil
  2606. "List of property/value pairs that can be inherited by any entry.
  2607. Valid for the current buffer.
  2608. This variable is populated from #+PROPERTY lines.")
  2609. (make-variable-buffer-local 'org-file-properties)
  2610. (defgroup org-agenda nil
  2611. "Options concerning agenda views in Org-mode."
  2612. :tag "Org Agenda"
  2613. :group 'org)
  2614. (defvar org-category nil
  2615. "Variable used by org files to set a category for agenda display.
  2616. Such files should use a file variable to set it, for example
  2617. # -*- mode: org; org-category: \"ELisp\"
  2618. or contain a special line
  2619. #+CATEGORY: ELisp
  2620. If the file does not specify a category, then file's base name
  2621. is used instead.")
  2622. (make-variable-buffer-local 'org-category)
  2623. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2624. (defcustom org-agenda-files nil
  2625. "The files to be used for agenda display.
  2626. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2627. \\[org-remove-file]. You can also use customize to edit the list.
  2628. If an entry is a directory, all files in that directory that are matched by
  2629. `org-agenda-file-regexp' will be part of the file list.
  2630. If the value of the variable is not a list but a single file name, then
  2631. the list of agenda files is actually stored and maintained in that file, one
  2632. agenda file per line. In this file paths can be given relative to
  2633. `org-directory'. Tilde expansion and environment variable substitution
  2634. are also made."
  2635. :group 'org-agenda
  2636. :type '(choice
  2637. (repeat :tag "List of files and directories" file)
  2638. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2639. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2640. "Regular expression to match files for `org-agenda-files'.
  2641. If any element in the list in that variable contains a directory instead
  2642. of a normal file, all files in that directory that are matched by this
  2643. regular expression will be included."
  2644. :group 'org-agenda
  2645. :type 'regexp)
  2646. (defcustom org-agenda-text-search-extra-files nil
  2647. "List of extra files to be searched by text search commands.
  2648. These files will be search in addition to the agenda files by the
  2649. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2650. Note that these files will only be searched for text search commands,
  2651. not for the other agenda views like todo lists, tag searches or the weekly
  2652. agenda. This variable is intended to list notes and possibly archive files
  2653. that should also be searched by these two commands.
  2654. In fact, if the first element in the list is the symbol `agenda-archives',
  2655. than all archive files of all agenda files will be added to the search
  2656. scope."
  2657. :group 'org-agenda
  2658. :type '(set :greedy t
  2659. (const :tag "Agenda Archives" agenda-archives)
  2660. (repeat :inline t (file))))
  2661. (if (fboundp 'defvaralias)
  2662. (defvaralias 'org-agenda-multi-occur-extra-files
  2663. 'org-agenda-text-search-extra-files))
  2664. (defcustom org-agenda-skip-unavailable-files nil
  2665. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2666. A nil value means to remove them, after a query, from the list."
  2667. :group 'org-agenda
  2668. :type 'boolean)
  2669. (defcustom org-calendar-to-agenda-key [?c]
  2670. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2671. The command `org-calendar-goto-agenda' will be bound to this key. The
  2672. default is the character `c' because then `c' can be used to switch back and
  2673. forth between agenda and calendar."
  2674. :group 'org-agenda
  2675. :type 'sexp)
  2676. (defcustom org-calendar-agenda-action-key [?k]
  2677. "The key to be installed in `calendar-mode-map' for agenda-action.
  2678. The command `org-agenda-action' will be bound to this key. The
  2679. default is the character `k' because we use the same key in the agenda."
  2680. :group 'org-agenda
  2681. :type 'sexp)
  2682. (defcustom org-calendar-insert-diary-entry-key [?i]
  2683. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2684. This option is irrelevant until `org-agenda-diary-file' has been configured
  2685. to point to an Org-mode file. When that is the case, the command
  2686. `org-agenda-diary-entry' will be bound to the key given here, by default
  2687. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2688. if you want to continue doing this, you need to change this to a different
  2689. key."
  2690. :group 'org-agenda
  2691. :type 'sexp)
  2692. (defcustom org-agenda-diary-file 'diary-file
  2693. "File to which to add new entries with the `i' key in agenda and calendar.
  2694. When this is the symbol `diary-file', the functionality in the Emacs
  2695. calendar will be used to add entries to the `diary-file'. But when this
  2696. points to a file, `org-agenda-diary-entry' will be used instead."
  2697. :group 'org-agenda
  2698. :type '(choice
  2699. (const :tag "The standard Emacs diary file" diary-file)
  2700. (file :tag "Special Org file diary entries")))
  2701. (eval-after-load "calendar"
  2702. '(progn
  2703. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2704. 'org-calendar-goto-agenda)
  2705. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2706. 'org-agenda-action)
  2707. (add-hook 'calendar-mode-hook
  2708. (lambda ()
  2709. (unless (eq org-agenda-diary-file 'diary-file)
  2710. (define-key calendar-mode-map
  2711. org-calendar-insert-diary-entry-key
  2712. 'org-agenda-diary-entry))))))
  2713. (defgroup org-latex nil
  2714. "Options for embedding LaTeX code into Org-mode."
  2715. :tag "Org LaTeX"
  2716. :group 'org)
  2717. (defcustom org-format-latex-options
  2718. '(:foreground default :background default :scale 1.0
  2719. :html-foreground "Black" :html-background "Transparent"
  2720. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2721. "Options for creating images from LaTeX fragments.
  2722. This is a property list with the following properties:
  2723. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2724. `default' means use the foreground of the default face.
  2725. :background the background color, or \"Transparent\".
  2726. `default' means use the background of the default face.
  2727. :scale a scaling factor for the size of the images, to get more pixels
  2728. :html-foreground, :html-background, :html-scale
  2729. the same numbers for HTML export.
  2730. :matchers a list indicating which matchers should be used to
  2731. find LaTeX fragments. Valid members of this list are:
  2732. \"begin\" find environments
  2733. \"$1\" find single characters surrounded by $.$
  2734. \"$\" find math expressions surrounded by $...$
  2735. \"$$\" find math expressions surrounded by $$....$$
  2736. \"\\(\" find math expressions surrounded by \\(...\\)
  2737. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2738. :group 'org-latex
  2739. :type 'plist)
  2740. (defcustom org-format-latex-signal-error t
  2741. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2742. When nil, just push out a message."
  2743. :group 'org-latex
  2744. :type 'boolean)
  2745. (defcustom org-format-latex-header "\\documentclass{article}
  2746. \\usepackage[usenames]{color}
  2747. \\usepackage{amsmath}
  2748. \\usepackage[mathscr]{eucal}
  2749. \\pagestyle{empty} % do not remove
  2750. \[PACKAGES]
  2751. \[DEFAULT-PACKAGES]
  2752. % The settings below are copied from fullpage.sty
  2753. \\setlength{\\textwidth}{\\paperwidth}
  2754. \\addtolength{\\textwidth}{-3cm}
  2755. \\setlength{\\oddsidemargin}{1.5cm}
  2756. \\addtolength{\\oddsidemargin}{-2.54cm}
  2757. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2758. \\setlength{\\textheight}{\\paperheight}
  2759. \\addtolength{\\textheight}{-\\headheight}
  2760. \\addtolength{\\textheight}{-\\headsep}
  2761. \\addtolength{\\textheight}{-\\footskip}
  2762. \\addtolength{\\textheight}{-3cm}
  2763. \\setlength{\\topmargin}{1.5cm}
  2764. \\addtolength{\\topmargin}{-2.54cm}"
  2765. "The document header used for processing LaTeX fragments.
  2766. It is imperative that this header make sure that no page number
  2767. appears on the page. The package defined in the variables
  2768. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2769. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2770. will be appended."
  2771. :group 'org-latex
  2772. :type 'string)
  2773. (defvar org-format-latex-header-extra nil)
  2774. (defun org-set-packages-alist (var val)
  2775. "Set the packages alist and make sure it has 3 elements per entry."
  2776. (set var (mapcar (lambda (x)
  2777. (if (and (consp x) (= (length x) 2))
  2778. (list (car x) (nth 1 x) t)
  2779. x))
  2780. val)))
  2781. (defun org-get-packages-alist (var)
  2782. "Get the packages alist and make sure it has 3 elements per entry."
  2783. (mapcar (lambda (x)
  2784. (if (and (consp x) (= (length x) 2))
  2785. (list (car x) (nth 1 x) t)
  2786. x))
  2787. (default-value var)))
  2788. ;; The following variables are defined here because is it also used
  2789. ;; when formatting latex fragments. Originally it was part of the
  2790. ;; LaTeX exporter, which is why the name includes "export".
  2791. (defcustom org-export-latex-default-packages-alist
  2792. '(("AUTO" "inputenc" t)
  2793. ("T1" "fontenc" t)
  2794. ("" "fixltx2e" nil)
  2795. ("" "graphicx" t)
  2796. ("" "longtable" nil)
  2797. ("" "float" nil)
  2798. ("" "wrapfig" nil)
  2799. ("" "soul" t)
  2800. ("" "textcomp" t)
  2801. ("" "marvosym" t)
  2802. ("" "wasysym" t)
  2803. ("" "latexsym" t)
  2804. ("" "amssymb" t)
  2805. ("" "hyperref" nil)
  2806. "\\tolerance=1000"
  2807. )
  2808. "Alist of default packages to be inserted in the header.
  2809. Change this only if one of the packages here causes an incompatibility
  2810. with another package you are using.
  2811. The packages in this list are needed by one part or another of Org-mode
  2812. to function properly.
  2813. - inputenc, fontenc: for basic font and character selection
  2814. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2815. for interpreting the entities in `org-entities'. You can skip some of these
  2816. packages if you don't use any of the symbols in it.
  2817. - graphicx: for including images
  2818. - float, wrapfig: for figure placement
  2819. - longtable: for long tables
  2820. - hyperref: for cross references
  2821. Therefore you should not modify this variable unless you know what you
  2822. are doing. The one reason to change it anyway is that you might be loading
  2823. some other package that conflicts with one of the default packages.
  2824. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2825. If SNIPPET-FLAG is t, the package also needs to be included when
  2826. compiling LaTeX snippets into images for inclusion into HTML."
  2827. :group 'org-export-latex
  2828. :set 'org-set-packages-alist
  2829. :get 'org-get-packages-alist
  2830. :type '(repeat
  2831. (choice
  2832. (list :tag "options/package pair"
  2833. (string :tag "options")
  2834. (string :tag "package")
  2835. (boolean :tag "Snippet"))
  2836. (string :tag "A line of LaTeX"))))
  2837. (defcustom org-export-latex-packages-alist nil
  2838. "Alist of packages to be inserted in every LaTeX header.
  2839. These will be inserted after `org-export-latex-default-packages-alist'.
  2840. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2841. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2842. turning LaTeX snippets into images for inclusion into HTML.
  2843. Make sure that you only list packages here which:
  2844. - you want in every file
  2845. - do not conflict with the default packages in
  2846. `org-export-latex-default-packages-alist'
  2847. - do not conflict with the setup in `org-format-latex-header'."
  2848. :group 'org-export-latex
  2849. :set 'org-set-packages-alist
  2850. :get 'org-get-packages-alist
  2851. :type '(repeat
  2852. (choice
  2853. (list :tag "options/package pair"
  2854. (string :tag "options")
  2855. (string :tag "package")
  2856. (boolean :tag "Snippet"))
  2857. (string :tag "A line of LaTeX"))))
  2858. (defgroup org-appearance nil
  2859. "Settings for Org-mode appearance."
  2860. :tag "Org Appearance"
  2861. :group 'org)
  2862. (defcustom org-level-color-stars-only nil
  2863. "Non-nil means fontify only the stars in each headline.
  2864. When nil, the entire headline is fontified.
  2865. Changing it requires restart of `font-lock-mode' to become effective
  2866. also in regions already fontified."
  2867. :group 'org-appearance
  2868. :type 'boolean)
  2869. (defcustom org-hide-leading-stars nil
  2870. "Non-nil means hide the first N-1 stars in a headline.
  2871. This works by using the face `org-hide' for these stars. This
  2872. face is white for a light background, and black for a dark
  2873. background. You may have to customize the face `org-hide' to
  2874. make this work.
  2875. Changing it requires restart of `font-lock-mode' to become effective
  2876. also in regions already fontified.
  2877. You may also set this on a per-file basis by adding one of the following
  2878. lines to the buffer:
  2879. #+STARTUP: hidestars
  2880. #+STARTUP: showstars"
  2881. :group 'org-appearance
  2882. :type 'boolean)
  2883. (defcustom org-hidden-keywords nil
  2884. "List of keywords that should be hidden when typed in the org buffer.
  2885. For example, add #+TITLE to this list in order to make the
  2886. document title appear in the buffer without the initial #+TITLE:
  2887. keyword."
  2888. :group 'org-appearance
  2889. :type '(set (const :tag "#+AUTHOR" author)
  2890. (const :tag "#+DATE" date)
  2891. (const :tag "#+EMAIL" email)
  2892. (const :tag "#+TITLE" title)))
  2893. (defcustom org-fontify-done-headline nil
  2894. "Non-nil means change the face of a headline if it is marked DONE.
  2895. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2896. When this is non-nil, the headline after the keyword is set to the
  2897. `org-headline-done' as an additional indication."
  2898. :group 'org-appearance
  2899. :type 'boolean)
  2900. (defcustom org-fontify-emphasized-text t
  2901. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2902. Changing this variable requires a restart of Emacs to take effect."
  2903. :group 'org-appearance
  2904. :type 'boolean)
  2905. (defcustom org-fontify-whole-heading-line nil
  2906. "Non-nil means fontify the whole line for headings.
  2907. This is useful when setting a background color for the
  2908. org-level-* faces."
  2909. :group 'org-appearance
  2910. :type 'boolean)
  2911. (defcustom org-highlight-latex-fragments-and-specials nil
  2912. "Non-nil means fontify what is treated specially by the exporters."
  2913. :group 'org-appearance
  2914. :type 'boolean)
  2915. (defcustom org-hide-emphasis-markers nil
  2916. "Non-nil mean font-lock should hide the emphasis marker characters."
  2917. :group 'org-appearance
  2918. :type 'boolean)
  2919. (defcustom org-pretty-entities nil
  2920. "Non-nil means show entities as UTF8 characters.
  2921. When nil, the \\name form remains in the buffer."
  2922. :group 'org-appearance
  2923. :type 'boolean)
  2924. (defcustom org-pretty-entities-include-sub-superscripts t
  2925. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2926. :group 'org-appearance
  2927. :type 'boolean)
  2928. (defvar org-emph-re nil
  2929. "Regular expression for matching emphasis.
  2930. After a match, the match groups contain these elements:
  2931. 0 The match of the full regular expression, including the characters
  2932. before and after the proper match
  2933. 1 The character before the proper match, or empty at beginning of line
  2934. 2 The proper match, including the leading and trailing markers
  2935. 3 The leading marker like * or /, indicating the type of highlighting
  2936. 4 The text between the emphasis markers, not including the markers
  2937. 5 The character after the match, empty at the end of a line")
  2938. (defvar org-verbatim-re nil
  2939. "Regular expression for matching verbatim text.")
  2940. (defvar org-emphasis-regexp-components) ; defined just below
  2941. (defvar org-emphasis-alist) ; defined just below
  2942. (defun org-set-emph-re (var val)
  2943. "Set variable and compute the emphasis regular expression."
  2944. (set var val)
  2945. (when (and (boundp 'org-emphasis-alist)
  2946. (boundp 'org-emphasis-regexp-components)
  2947. org-emphasis-alist org-emphasis-regexp-components)
  2948. (let* ((e org-emphasis-regexp-components)
  2949. (pre (car e))
  2950. (post (nth 1 e))
  2951. (border (nth 2 e))
  2952. (body (nth 3 e))
  2953. (nl (nth 4 e))
  2954. (body1 (concat body "*?"))
  2955. (markers (mapconcat 'car org-emphasis-alist ""))
  2956. (vmarkers (mapconcat
  2957. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2958. org-emphasis-alist "")))
  2959. ;; make sure special characters appear at the right position in the class
  2960. (if (string-match "\\^" markers)
  2961. (setq markers (concat (replace-match "" t t markers) "^")))
  2962. (if (string-match "-" markers)
  2963. (setq markers (concat (replace-match "" t t markers) "-")))
  2964. (if (string-match "\\^" vmarkers)
  2965. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2966. (if (string-match "-" vmarkers)
  2967. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2968. (if (> nl 0)
  2969. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2970. (int-to-string nl) "\\}")))
  2971. ;; Make the regexp
  2972. (setq org-emph-re
  2973. (concat "\\([" pre "]\\|^\\)"
  2974. "\\("
  2975. "\\([" markers "]\\)"
  2976. "\\("
  2977. "[^" border "]\\|"
  2978. "[^" border "]"
  2979. body1
  2980. "[^" border "]"
  2981. "\\)"
  2982. "\\3\\)"
  2983. "\\([" post "]\\|$\\)"))
  2984. (setq org-verbatim-re
  2985. (concat "\\([" pre "]\\|^\\)"
  2986. "\\("
  2987. "\\([" vmarkers "]\\)"
  2988. "\\("
  2989. "[^" border "]\\|"
  2990. "[^" border "]"
  2991. body1
  2992. "[^" border "]"
  2993. "\\)"
  2994. "\\3\\)"
  2995. "\\([" post "]\\|$\\)")))))
  2996. (defcustom org-emphasis-regexp-components
  2997. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2998. "Components used to build the regular expression for emphasis.
  2999. This is a list with five entries. Terminology: In an emphasis string
  3000. like \" *strong word* \", we call the initial space PREMATCH, the final
  3001. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3002. and \"trong wor\" is the body. The different components in this variable
  3003. specify what is allowed/forbidden in each part:
  3004. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3005. post Chars allowed as postmatch. End of line will be allowed too.
  3006. border The chars *forbidden* as border characters.
  3007. body-regexp A regexp like \".\" to match a body character. Don't use
  3008. non-shy groups here, and don't allow newline here.
  3009. newline The maximum number of newlines allowed in an emphasis exp.
  3010. Use customize to modify this, or restart Emacs after changing it."
  3011. :group 'org-appearance
  3012. :set 'org-set-emph-re
  3013. :type '(list
  3014. (sexp :tag "Allowed chars in pre ")
  3015. (sexp :tag "Allowed chars in post ")
  3016. (sexp :tag "Forbidden chars in border ")
  3017. (sexp :tag "Regexp for body ")
  3018. (integer :tag "number of newlines allowed")
  3019. (option (boolean :tag "Please ignore this button"))))
  3020. (defcustom org-emphasis-alist
  3021. `(("*" bold "<b>" "</b>")
  3022. ("/" italic "<i>" "</i>")
  3023. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3024. ("=" org-code "<code>" "</code>" verbatim)
  3025. ("~" org-verbatim "<code>" "</code>" verbatim)
  3026. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3027. "<del>" "</del>")
  3028. )
  3029. "Special syntax for emphasized text.
  3030. Text starting and ending with a special character will be emphasized, for
  3031. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3032. characters, the face to be used by font-lock for highlighting in Org-mode
  3033. Emacs buffers, and the HTML tags to be used for this.
  3034. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3035. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3036. Use customize to modify this, or restart Emacs after changing it."
  3037. :group 'org-appearance
  3038. :set 'org-set-emph-re
  3039. :type '(repeat
  3040. (list
  3041. (string :tag "Marker character")
  3042. (choice
  3043. (face :tag "Font-lock-face")
  3044. (plist :tag "Face property list"))
  3045. (string :tag "HTML start tag")
  3046. (string :tag "HTML end tag")
  3047. (option (const verbatim)))))
  3048. (defvar org-protecting-blocks
  3049. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3050. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3051. This is needed for font-lock setup.")
  3052. ;;; Miscellaneous options
  3053. (defgroup org-completion nil
  3054. "Completion in Org-mode."
  3055. :tag "Org Completion"
  3056. :group 'org)
  3057. (defcustom org-completion-use-ido nil
  3058. "Non-nil means use ido completion wherever possible.
  3059. Note that `ido-mode' must be active for this variable to be relevant.
  3060. If you decide to turn this variable on, you might well want to turn off
  3061. `org-outline-path-complete-in-steps'.
  3062. See also `org-completion-use-iswitchb'."
  3063. :group 'org-completion
  3064. :type 'boolean)
  3065. (defcustom org-completion-use-iswitchb nil
  3066. "Non-nil means use iswitchb completion wherever possible.
  3067. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3068. If you decide to turn this variable on, you might well want to turn off
  3069. `org-outline-path-complete-in-steps'.
  3070. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3071. :group 'org-completion
  3072. :type 'boolean)
  3073. (defcustom org-completion-fallback-command 'hippie-expand
  3074. "The expansion command called by \\[pcomplete] in normal context.
  3075. Normal means, no org-mode-specific context."
  3076. :group 'org-completion
  3077. :type 'function)
  3078. ;;; Functions and variables from their packages
  3079. ;; Declared here to avoid compiler warnings
  3080. ;; XEmacs only
  3081. (defvar outline-mode-menu-heading)
  3082. (defvar outline-mode-menu-show)
  3083. (defvar outline-mode-menu-hide)
  3084. (defvar zmacs-regions) ; XEmacs regions
  3085. ;; Emacs only
  3086. (defvar mark-active)
  3087. ;; Various packages
  3088. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3089. (declare-function calendar-forward-day "cal-move" (arg))
  3090. (declare-function calendar-goto-date "cal-move" (date))
  3091. (declare-function calendar-goto-today "cal-move" ())
  3092. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3093. (defvar calc-embedded-close-formula)
  3094. (defvar calc-embedded-open-formula)
  3095. (declare-function cdlatex-tab "ext:cdlatex" ())
  3096. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3097. (defvar font-lock-unfontify-region-function)
  3098. (declare-function iswitchb-read-buffer "iswitchb"
  3099. (prompt &optional default require-match start matches-set))
  3100. (defvar iswitchb-temp-buflist)
  3101. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3102. (defvar org-agenda-tags-todo-honor-ignore-options)
  3103. (declare-function org-agenda-skip "org-agenda" ())
  3104. (declare-function
  3105. org-format-agenda-item "org-agenda"
  3106. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3107. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3108. (declare-function org-agenda-change-all-lines "org-agenda"
  3109. (newhead hdmarker &optional fixface just-this))
  3110. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3111. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3112. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3113. (beg end))
  3114. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3115. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3116. "org-agenda" (&optional end))
  3117. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3118. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3119. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3120. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3121. (declare-function org-indent-mode "org-indent" (&optional arg))
  3122. (declare-function parse-time-string "parse-time" (string))
  3123. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3124. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3125. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3126. (defvar remember-data-file)
  3127. (defvar texmathp-why)
  3128. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3129. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3130. (defvar w3m-current-url)
  3131. (defvar w3m-current-title)
  3132. (defvar org-latex-regexps)
  3133. ;;; Autoload and prepare some org modules
  3134. ;; Some table stuff that needs to be defined here, because it is used
  3135. ;; by the functions setting up org-mode or checking for table context.
  3136. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3137. "Detect an org-type or table-type table.")
  3138. (defconst org-table-line-regexp "^[ \t]*|"
  3139. "Detect an org-type table line.")
  3140. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3141. "Detect an org-type table line.")
  3142. (defconst org-table-hline-regexp "^[ \t]*|-"
  3143. "Detect an org-type table hline.")
  3144. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3145. "Detect a table-type table hline.")
  3146. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3147. "Detect the first line outside a table when searching from within it.
  3148. This works for both table types.")
  3149. ;; Autoload the functions in org-table.el that are needed by functions here.
  3150. (eval-and-compile
  3151. (org-autoload "org-table"
  3152. '(org-table-align org-table-begin org-table-blank-field
  3153. org-table-convert org-table-convert-region org-table-copy-down
  3154. org-table-copy-region org-table-create
  3155. org-table-create-or-convert-from-region
  3156. org-table-create-with-table.el org-table-current-dline
  3157. org-table-cut-region org-table-delete-column org-table-edit-field
  3158. org-table-edit-formulas org-table-end org-table-eval-formula
  3159. org-table-export org-table-field-info
  3160. org-table-get-stored-formulas org-table-goto-column
  3161. org-table-hline-and-move org-table-import org-table-insert-column
  3162. org-table-insert-hline org-table-insert-row org-table-iterate
  3163. org-table-justify-field-maybe org-table-kill-row
  3164. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3165. org-table-move-column org-table-move-column-left
  3166. org-table-move-column-right org-table-move-row
  3167. org-table-move-row-down org-table-move-row-up
  3168. org-table-next-field org-table-next-row org-table-paste-rectangle
  3169. org-table-previous-field org-table-recalculate
  3170. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3171. org-table-toggle-coordinate-overlays
  3172. org-table-toggle-formula-debugger org-table-wrap-region
  3173. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3174. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3175. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3176. (defun org-at-table-p (&optional table-type)
  3177. "Return t if the cursor is inside an org-type table.
  3178. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3179. (if org-enable-table-editor
  3180. (save-excursion
  3181. (beginning-of-line 1)
  3182. (looking-at (if table-type org-table-any-line-regexp
  3183. org-table-line-regexp)))
  3184. nil))
  3185. (defsubst org-table-p () (org-at-table-p))
  3186. (defun org-at-table.el-p ()
  3187. "Return t if and only if we are at a table.el table."
  3188. (and (org-at-table-p 'any)
  3189. (save-excursion
  3190. (goto-char (org-table-begin 'any))
  3191. (looking-at org-table1-hline-regexp))))
  3192. (defun org-table-recognize-table.el ()
  3193. "If there is a table.el table nearby, recognize it and move into it."
  3194. (if org-table-tab-recognizes-table.el
  3195. (if (org-at-table.el-p)
  3196. (progn
  3197. (beginning-of-line 1)
  3198. (if (looking-at org-table-dataline-regexp)
  3199. nil
  3200. (if (looking-at org-table1-hline-regexp)
  3201. (progn
  3202. (beginning-of-line 2)
  3203. (if (looking-at org-table-any-border-regexp)
  3204. (beginning-of-line -1)))))
  3205. (if (re-search-forward "|" (org-table-end t) t)
  3206. (progn
  3207. (require 'table)
  3208. (if (table--at-cell-p (point))
  3209. t
  3210. (message "recognizing table.el table...")
  3211. (table-recognize-table)
  3212. (message "recognizing table.el table...done")))
  3213. (error "This should not happen"))
  3214. t)
  3215. nil)
  3216. nil))
  3217. (defun org-at-table-hline-p ()
  3218. "Return t if the cursor is inside a hline in a table."
  3219. (if org-enable-table-editor
  3220. (save-excursion
  3221. (beginning-of-line 1)
  3222. (looking-at org-table-hline-regexp))
  3223. nil))
  3224. (defvar org-table-clean-did-remove-column nil)
  3225. (defun org-table-map-tables (function &optional quietly)
  3226. "Apply FUNCTION to the start of all tables in the buffer."
  3227. (save-excursion
  3228. (save-restriction
  3229. (widen)
  3230. (goto-char (point-min))
  3231. (while (re-search-forward org-table-any-line-regexp nil t)
  3232. (unless quietly
  3233. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3234. (beginning-of-line 1)
  3235. (when (looking-at org-table-line-regexp)
  3236. (save-excursion (funcall function))
  3237. (or (looking-at org-table-line-regexp)
  3238. (forward-char 1)))
  3239. (re-search-forward org-table-any-border-regexp nil 1))))
  3240. (unless quietly (message "Mapping tables: done")))
  3241. ;; Declare and autoload functions from org-exp.el & Co
  3242. (declare-function org-default-export-plist "org-exp")
  3243. (declare-function org-infile-export-plist "org-exp")
  3244. (declare-function org-get-current-options "org-exp")
  3245. (eval-and-compile
  3246. (org-autoload "org-exp"
  3247. '(org-export org-export-visible
  3248. org-insert-export-options-template
  3249. org-table-clean-before-export))
  3250. (org-autoload "org-ascii"
  3251. '(org-export-as-ascii org-export-ascii-preprocess
  3252. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3253. org-export-region-as-ascii))
  3254. (org-autoload "org-latex"
  3255. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3256. org-replace-region-by-latex org-export-region-as-latex
  3257. org-export-as-latex org-export-as-pdf
  3258. org-export-as-pdf-and-open))
  3259. (org-autoload "org-html"
  3260. '(org-export-as-html-and-open
  3261. org-export-as-html-batch org-export-as-html-to-buffer
  3262. org-replace-region-by-html org-export-region-as-html
  3263. org-export-as-html))
  3264. (org-autoload "org-docbook"
  3265. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3266. org-replace-region-by-docbook org-export-region-as-docbook
  3267. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3268. org-export-as-docbook))
  3269. (org-autoload "org-icalendar"
  3270. '(org-export-icalendar-this-file
  3271. org-export-icalendar-all-agenda-files
  3272. org-export-icalendar-combine-agenda-files))
  3273. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3274. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3275. ;; Declare and autoload functions from org-agenda.el
  3276. (eval-and-compile
  3277. (org-autoload "org-agenda"
  3278. '(org-agenda org-agenda-list org-search-view
  3279. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3280. org-diary org-agenda-to-appt
  3281. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3282. ;; Autoload org-remember
  3283. (eval-and-compile
  3284. (org-autoload "org-remember"
  3285. '(org-remember-insinuate org-remember-annotation
  3286. org-remember-apply-template org-remember org-remember-handler)))
  3287. (eval-and-compile
  3288. (org-autoload "org-capture"
  3289. '(org-capture org-capture-insert-template-here
  3290. org-capture-import-remember-templates)))
  3291. ;; Autoload org-clock.el
  3292. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3293. (beg end))
  3294. (declare-function org-clock-update-mode-line "org-clock" ())
  3295. (declare-function org-resolve-clocks "org-clock"
  3296. (&optional also-non-dangling-p prompt last-valid))
  3297. (defvar org-clock-start-time)
  3298. (defvar org-clock-marker (make-marker)
  3299. "Marker recording the last clock-in.")
  3300. (defvar org-clock-hd-marker (make-marker)
  3301. "Marker recording the last clock-in, but the headline position.")
  3302. (defvar org-clock-heading ""
  3303. "The heading of the current clock entry.")
  3304. (defun org-clock-is-active ()
  3305. "Return non-nil if clock is currently running.
  3306. The return value is actually the clock marker."
  3307. (marker-buffer org-clock-marker))
  3308. (eval-and-compile
  3309. (org-autoload
  3310. "org-clock"
  3311. '(org-clock-in org-clock-out org-clock-cancel
  3312. org-clock-goto org-clock-sum org-clock-display
  3313. org-clock-remove-overlays org-clock-report
  3314. org-clocktable-shift org-dblock-write:clocktable
  3315. org-get-clocktable org-resolve-clocks)))
  3316. (defun org-clock-update-time-maybe ()
  3317. "If this is a CLOCK line, update it and return t.
  3318. Otherwise, return nil."
  3319. (interactive)
  3320. (save-excursion
  3321. (beginning-of-line 1)
  3322. (skip-chars-forward " \t")
  3323. (when (looking-at org-clock-string)
  3324. (let ((re (concat "[ \t]*" org-clock-string
  3325. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3326. "\\([ \t]*=>.*\\)?\\)?"))
  3327. ts te h m s neg)
  3328. (cond
  3329. ((not (looking-at re))
  3330. nil)
  3331. ((not (match-end 2))
  3332. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3333. (> org-clock-marker (point))
  3334. (<= org-clock-marker (point-at-eol)))
  3335. ;; The clock is running here
  3336. (setq org-clock-start-time
  3337. (apply 'encode-time
  3338. (org-parse-time-string (match-string 1))))
  3339. (org-clock-update-mode-line)))
  3340. (t
  3341. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3342. (end-of-line 1)
  3343. (setq ts (match-string 1)
  3344. te (match-string 3))
  3345. (setq s (- (org-float-time
  3346. (apply 'encode-time (org-parse-time-string te)))
  3347. (org-float-time
  3348. (apply 'encode-time (org-parse-time-string ts))))
  3349. neg (< s 0)
  3350. s (abs s)
  3351. h (floor (/ s 3600))
  3352. s (- s (* 3600 h))
  3353. m (floor (/ s 60))
  3354. s (- s (* 60 s)))
  3355. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3356. t))))))
  3357. (defun org-check-running-clock ()
  3358. "Check if the current buffer contains the running clock.
  3359. If yes, offer to stop it and to save the buffer with the changes."
  3360. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3361. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3362. (buffer-name))))
  3363. (org-clock-out)
  3364. (when (y-or-n-p "Save changed buffer?")
  3365. (save-buffer))))
  3366. (defun org-clocktable-try-shift (dir n)
  3367. "Check if this line starts a clock table, if yes, shift the time block."
  3368. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3369. (org-clocktable-shift dir n)))
  3370. ;; Autoload org-timer.el
  3371. (eval-and-compile
  3372. (org-autoload
  3373. "org-timer"
  3374. '(org-timer-start org-timer org-timer-item
  3375. org-timer-change-times-in-region
  3376. org-timer-set-timer
  3377. org-timer-reset-timers
  3378. org-timer-show-remaining-time)))
  3379. ;; Autoload org-feed.el
  3380. (eval-and-compile
  3381. (org-autoload
  3382. "org-feed"
  3383. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3384. ;; Autoload org-indent.el
  3385. ;; Define the variable already here, to make sure we have it.
  3386. (defvar org-indent-mode nil
  3387. "Non-nil if Org-Indent mode is enabled.
  3388. Use the command `org-indent-mode' to change this variable.")
  3389. (eval-and-compile
  3390. (org-autoload
  3391. "org-indent"
  3392. '(org-indent-mode)))
  3393. ;; Autoload org-mobile.el
  3394. (eval-and-compile
  3395. (org-autoload
  3396. "org-mobile"
  3397. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3398. ;; Autoload archiving code
  3399. ;; The stuff that is needed for cycling and tags has to be defined here.
  3400. (defgroup org-archive nil
  3401. "Options concerning archiving in Org-mode."
  3402. :tag "Org Archive"
  3403. :group 'org-structure)
  3404. (defcustom org-archive-location "%s_archive::"
  3405. "The location where subtrees should be archived.
  3406. The value of this variable is a string, consisting of two parts,
  3407. separated by a double-colon. The first part is a filename and
  3408. the second part is a headline.
  3409. When the filename is omitted, archiving happens in the same file.
  3410. %s in the filename will be replaced by the current file
  3411. name (without the directory part). Archiving to a different file
  3412. is useful to keep archived entries from contributing to the
  3413. Org-mode Agenda.
  3414. The archived entries will be filed as subtrees of the specified
  3415. headline. When the headline is omitted, the subtrees are simply
  3416. filed away at the end of the file, as top-level entries. Also in
  3417. the heading you can use %s to represent the file name, this can be
  3418. useful when using the same archive for a number of different files.
  3419. Here are a few examples:
  3420. \"%s_archive::\"
  3421. If the current file is Projects.org, archive in file
  3422. Projects.org_archive, as top-level trees. This is the default.
  3423. \"::* Archived Tasks\"
  3424. Archive in the current file, under the top-level headline
  3425. \"* Archived Tasks\".
  3426. \"~/org/archive.org::\"
  3427. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3428. \"~/org/archive.org::From %s\"
  3429. Archive in file ~/org/archive.org (absolute path), under headlines
  3430. \"From FILENAME\" where file name is the current file name.
  3431. \"basement::** Finished Tasks\"
  3432. Archive in file ./basement (relative path), as level 3 trees
  3433. below the level 2 heading \"** Finished Tasks\".
  3434. You may set this option on a per-file basis by adding to the buffer a
  3435. line like
  3436. #+ARCHIVE: basement::** Finished Tasks
  3437. You may also define it locally for a subtree by setting an ARCHIVE property
  3438. in the entry. If such a property is found in an entry, or anywhere up
  3439. the hierarchy, it will be used."
  3440. :group 'org-archive
  3441. :type 'string)
  3442. (defcustom org-archive-tag "ARCHIVE"
  3443. "The tag that marks a subtree as archived.
  3444. An archived subtree does not open during visibility cycling, and does
  3445. not contribute to the agenda listings.
  3446. After changing this, font-lock must be restarted in the relevant buffers to
  3447. get the proper fontification."
  3448. :group 'org-archive
  3449. :group 'org-keywords
  3450. :type 'string)
  3451. (defcustom org-agenda-skip-archived-trees t
  3452. "Non-nil means the agenda will skip any items located in archived trees.
  3453. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3454. variable is no longer recommended, you should leave it at the value t.
  3455. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3456. :group 'org-archive
  3457. :group 'org-agenda-skip
  3458. :type 'boolean)
  3459. (defcustom org-columns-skip-archived-trees t
  3460. "Non-nil means ignore archived trees when creating column view."
  3461. :group 'org-archive
  3462. :group 'org-properties
  3463. :type 'boolean)
  3464. (defcustom org-cycle-open-archived-trees nil
  3465. "Non-nil means `org-cycle' will open archived trees.
  3466. An archived tree is a tree marked with the tag ARCHIVE.
  3467. When nil, archived trees will stay folded. You can still open them with
  3468. normal outline commands like `show-all', but not with the cycling commands."
  3469. :group 'org-archive
  3470. :group 'org-cycle
  3471. :type 'boolean)
  3472. (defcustom org-sparse-tree-open-archived-trees nil
  3473. "Non-nil means sparse tree construction shows matches in archived trees.
  3474. When nil, matches in these trees are highlighted, but the trees are kept in
  3475. collapsed state."
  3476. :group 'org-archive
  3477. :group 'org-sparse-trees
  3478. :type 'boolean)
  3479. (defun org-cycle-hide-archived-subtrees (state)
  3480. "Re-hide all archived subtrees after a visibility state change."
  3481. (when (and (not org-cycle-open-archived-trees)
  3482. (not (memq state '(overview folded))))
  3483. (save-excursion
  3484. (let* ((globalp (memq state '(contents all)))
  3485. (beg (if globalp (point-min) (point)))
  3486. (end (if globalp (point-max) (org-end-of-subtree t))))
  3487. (org-hide-archived-subtrees beg end)
  3488. (goto-char beg)
  3489. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3490. (message "%s" (substitute-command-keys
  3491. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3492. (defun org-force-cycle-archived ()
  3493. "Cycle subtree even if it is archived."
  3494. (interactive)
  3495. (setq this-command 'org-cycle)
  3496. (let ((org-cycle-open-archived-trees t))
  3497. (call-interactively 'org-cycle)))
  3498. (defun org-hide-archived-subtrees (beg end)
  3499. "Re-hide all archived subtrees after a visibility state change."
  3500. (save-excursion
  3501. (let* ((re (concat ":" org-archive-tag ":")))
  3502. (goto-char beg)
  3503. (while (re-search-forward re end t)
  3504. (when (org-on-heading-p)
  3505. (org-flag-subtree t)
  3506. (org-end-of-subtree t))))))
  3507. (defun org-flag-subtree (flag)
  3508. (save-excursion
  3509. (org-back-to-heading t)
  3510. (outline-end-of-heading)
  3511. (outline-flag-region (point)
  3512. (progn (org-end-of-subtree t) (point))
  3513. flag)))
  3514. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3515. (eval-and-compile
  3516. (org-autoload "org-archive"
  3517. '(org-add-archive-files org-archive-subtree
  3518. org-archive-to-archive-sibling org-toggle-archive-tag
  3519. org-archive-subtree-default
  3520. org-archive-subtree-default-with-confirmation)))
  3521. ;; Autoload Column View Code
  3522. (declare-function org-columns-number-to-string "org-colview")
  3523. (declare-function org-columns-get-format-and-top-level "org-colview")
  3524. (declare-function org-columns-compute "org-colview")
  3525. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3526. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3527. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3528. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3529. ;; Autoload ID code
  3530. (declare-function org-id-store-link "org-id")
  3531. (declare-function org-id-locations-load "org-id")
  3532. (declare-function org-id-locations-save "org-id")
  3533. (defvar org-id-track-globally)
  3534. (org-autoload "org-id"
  3535. '(org-id-get-create org-id-new org-id-copy org-id-get
  3536. org-id-get-with-outline-path-completion
  3537. org-id-get-with-outline-drilling org-id-store-link
  3538. org-id-goto org-id-find org-id-store-link))
  3539. ;; Autoload Plotting Code
  3540. (org-autoload "org-plot"
  3541. '(org-plot/gnuplot))
  3542. ;;; Variables for pre-computed regular expressions, all buffer local
  3543. (defvar org-drawer-regexp nil
  3544. "Matches first line of a hidden block.")
  3545. (make-variable-buffer-local 'org-drawer-regexp)
  3546. (defvar org-todo-regexp nil
  3547. "Matches any of the TODO state keywords.")
  3548. (make-variable-buffer-local 'org-todo-regexp)
  3549. (defvar org-not-done-regexp nil
  3550. "Matches any of the TODO state keywords except the last one.")
  3551. (make-variable-buffer-local 'org-not-done-regexp)
  3552. (defvar org-not-done-heading-regexp nil
  3553. "Matches a TODO headline that is not done.")
  3554. (make-variable-buffer-local 'org-not-done-regexp)
  3555. (defvar org-todo-line-regexp nil
  3556. "Matches a headline and puts TODO state into group 2 if present.")
  3557. (make-variable-buffer-local 'org-todo-line-regexp)
  3558. (defvar org-complex-heading-regexp nil
  3559. "Matches a headline and puts everything into groups:
  3560. group 1: the stars
  3561. group 2: The todo keyword, maybe
  3562. group 3: Priority cookie
  3563. group 4: True headline
  3564. group 5: Tags")
  3565. (make-variable-buffer-local 'org-complex-heading-regexp)
  3566. (defvar org-complex-heading-regexp-format nil
  3567. "Printf format to make regexp to match an exact headline.
  3568. This regexp will match the headline of any node which hase the exact
  3569. headline text that is put into the format, but may have any TODO state,
  3570. priority and tags.")
  3571. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3572. (defvar org-todo-line-tags-regexp nil
  3573. "Matches a headline and puts TODO state into group 2 if present.
  3574. Also put tags into group 4 if tags are present.")
  3575. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3576. (defvar org-nl-done-regexp nil
  3577. "Matches newline followed by a headline with the DONE keyword.")
  3578. (make-variable-buffer-local 'org-nl-done-regexp)
  3579. (defvar org-looking-at-done-regexp nil
  3580. "Matches the DONE keyword a point.")
  3581. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3582. (defvar org-ds-keyword-length 12
  3583. "Maximum length of the Deadline and SCHEDULED keywords.")
  3584. (make-variable-buffer-local 'org-ds-keyword-length)
  3585. (defvar org-deadline-regexp nil
  3586. "Matches the DEADLINE keyword.")
  3587. (make-variable-buffer-local 'org-deadline-regexp)
  3588. (defvar org-deadline-time-regexp nil
  3589. "Matches the DEADLINE keyword together with a time stamp.")
  3590. (make-variable-buffer-local 'org-deadline-time-regexp)
  3591. (defvar org-deadline-line-regexp nil
  3592. "Matches the DEADLINE keyword and the rest of the line.")
  3593. (make-variable-buffer-local 'org-deadline-line-regexp)
  3594. (defvar org-scheduled-regexp nil
  3595. "Matches the SCHEDULED keyword.")
  3596. (make-variable-buffer-local 'org-scheduled-regexp)
  3597. (defvar org-scheduled-time-regexp nil
  3598. "Matches the SCHEDULED keyword together with a time stamp.")
  3599. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3600. (defvar org-closed-time-regexp nil
  3601. "Matches the CLOSED keyword together with a time stamp.")
  3602. (make-variable-buffer-local 'org-closed-time-regexp)
  3603. (defvar org-keyword-time-regexp nil
  3604. "Matches any of the 4 keywords, together with the time stamp.")
  3605. (make-variable-buffer-local 'org-keyword-time-regexp)
  3606. (defvar org-keyword-time-not-clock-regexp nil
  3607. "Matches any of the 3 keywords, together with the time stamp.")
  3608. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3609. (defvar org-maybe-keyword-time-regexp nil
  3610. "Matches a timestamp, possibly preceded by a keyword.")
  3611. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3612. (defvar org-planning-or-clock-line-re nil
  3613. "Matches a line with planning or clock info.")
  3614. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3615. (defvar org-all-time-keywords nil
  3616. "List of time keywords.")
  3617. (make-variable-buffer-local 'org-all-time-keywords)
  3618. (defconst org-plain-time-of-day-regexp
  3619. (concat
  3620. "\\(\\<[012]?[0-9]"
  3621. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3622. "\\(--?"
  3623. "\\(\\<[012]?[0-9]"
  3624. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3625. "\\)?")
  3626. "Regular expression to match a plain time or time range.
  3627. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3628. groups carry important information:
  3629. 0 the full match
  3630. 1 the first time, range or not
  3631. 8 the second time, if it is a range.")
  3632. (defconst org-plain-time-extension-regexp
  3633. (concat
  3634. "\\(\\<[012]?[0-9]"
  3635. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3636. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3637. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3638. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3639. groups carry important information:
  3640. 0 the full match
  3641. 7 hours of duration
  3642. 9 minutes of duration")
  3643. (defconst org-stamp-time-of-day-regexp
  3644. (concat
  3645. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3646. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3647. "\\(--?"
  3648. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3649. "Regular expression to match a timestamp time or time range.
  3650. After a match, the following groups carry important information:
  3651. 0 the full match
  3652. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3653. 2 the first time, range or not
  3654. 4 the second time, if it is a range.")
  3655. (defconst org-startup-options
  3656. '(("fold" org-startup-folded t)
  3657. ("overview" org-startup-folded t)
  3658. ("nofold" org-startup-folded nil)
  3659. ("showall" org-startup-folded nil)
  3660. ("showeverything" org-startup-folded showeverything)
  3661. ("content" org-startup-folded content)
  3662. ("indent" org-startup-indented t)
  3663. ("noindent" org-startup-indented nil)
  3664. ("hidestars" org-hide-leading-stars t)
  3665. ("showstars" org-hide-leading-stars nil)
  3666. ("odd" org-odd-levels-only t)
  3667. ("oddeven" org-odd-levels-only nil)
  3668. ("align" org-startup-align-all-tables t)
  3669. ("noalign" org-startup-align-all-tables nil)
  3670. ("inlineimages" org-startup-with-inline-images t)
  3671. ("noinlineimages" org-startup-with-inline-images nil)
  3672. ("customtime" org-display-custom-times t)
  3673. ("logdone" org-log-done time)
  3674. ("lognotedone" org-log-done note)
  3675. ("nologdone" org-log-done nil)
  3676. ("lognoteclock-out" org-log-note-clock-out t)
  3677. ("nolognoteclock-out" org-log-note-clock-out nil)
  3678. ("logrepeat" org-log-repeat state)
  3679. ("lognoterepeat" org-log-repeat note)
  3680. ("nologrepeat" org-log-repeat nil)
  3681. ("logreschedule" org-log-reschedule time)
  3682. ("lognotereschedule" org-log-reschedule note)
  3683. ("nologreschedule" org-log-reschedule nil)
  3684. ("logredeadline" org-log-redeadline time)
  3685. ("lognoteredeadline" org-log-redeadline note)
  3686. ("nologredeadline" org-log-redeadline nil)
  3687. ("logrefile" org-log-refile time)
  3688. ("lognoterefile" org-log-refile note)
  3689. ("nologrefile" org-log-refile nil)
  3690. ("fninline" org-footnote-define-inline t)
  3691. ("nofninline" org-footnote-define-inline nil)
  3692. ("fnlocal" org-footnote-section nil)
  3693. ("fnauto" org-footnote-auto-label t)
  3694. ("fnprompt" org-footnote-auto-label nil)
  3695. ("fnconfirm" org-footnote-auto-label confirm)
  3696. ("fnplain" org-footnote-auto-label plain)
  3697. ("fnadjust" org-footnote-auto-adjust t)
  3698. ("nofnadjust" org-footnote-auto-adjust nil)
  3699. ("constcgs" constants-unit-system cgs)
  3700. ("constSI" constants-unit-system SI)
  3701. ("noptag" org-tag-persistent-alist nil)
  3702. ("hideblocks" org-hide-block-startup t)
  3703. ("nohideblocks" org-hide-block-startup nil)
  3704. ("beamer" org-startup-with-beamer-mode t)
  3705. ("entitiespretty" org-pretty-entities t)
  3706. ("entitiesplain" org-pretty-entities nil))
  3707. "Variable associated with STARTUP options for org-mode.
  3708. Each element is a list of three items: The startup options as written
  3709. in the #+STARTUP line, the corresponding variable, and the value to
  3710. set this variable to if the option is found. An optional forth element PUSH
  3711. means to push this value onto the list in the variable.")
  3712. (defun org-set-regexps-and-options ()
  3713. "Precompute regular expressions for current buffer."
  3714. (when (org-mode-p)
  3715. (org-set-local 'org-todo-kwd-alist nil)
  3716. (org-set-local 'org-todo-key-alist nil)
  3717. (org-set-local 'org-todo-key-trigger nil)
  3718. (org-set-local 'org-todo-keywords-1 nil)
  3719. (org-set-local 'org-done-keywords nil)
  3720. (org-set-local 'org-todo-heads nil)
  3721. (org-set-local 'org-todo-sets nil)
  3722. (org-set-local 'org-todo-log-states nil)
  3723. (org-set-local 'org-file-properties nil)
  3724. (org-set-local 'org-file-tags nil)
  3725. (let ((re (org-make-options-regexp
  3726. '("CATEGORY" "TODO" "COLUMNS"
  3727. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3728. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3729. "OPTIONS")
  3730. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3731. (splitre "[ \t]+")
  3732. (scripts org-use-sub-superscripts)
  3733. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3734. tail sep kws1 prio props ftags drawers beamer-p
  3735. ext-setup-or-nil setup-contents (start 0))
  3736. (save-excursion
  3737. (save-restriction
  3738. (widen)
  3739. (goto-char (point-min))
  3740. (while (or (and ext-setup-or-nil
  3741. (string-match re ext-setup-or-nil start)
  3742. (setq start (match-end 0)))
  3743. (and (setq ext-setup-or-nil nil start 0)
  3744. (re-search-forward re nil t)))
  3745. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3746. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3747. (if (stringp value) (setq value (org-trim value)))
  3748. (cond
  3749. ((equal key "CATEGORY")
  3750. (setq cat value))
  3751. ((member key '("SEQ_TODO" "TODO"))
  3752. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3753. ((equal key "TYP_TODO")
  3754. (push (cons 'type (org-split-string value splitre)) kwds))
  3755. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3756. ;; general TODO-like setup
  3757. (push (cons (intern (downcase (match-string 1 key)))
  3758. (org-split-string value splitre)) kwds))
  3759. ((equal key "TAGS")
  3760. (setq tags (append tags (if tags '("\\n") nil)
  3761. (org-split-string value splitre))))
  3762. ((equal key "COLUMNS")
  3763. (org-set-local 'org-columns-default-format value))
  3764. ((equal key "LINK")
  3765. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3766. (push (cons (match-string 1 value)
  3767. (org-trim (match-string 2 value)))
  3768. links)))
  3769. ((equal key "PRIORITIES")
  3770. (setq prio (org-split-string value " +")))
  3771. ((equal key "PROPERTY")
  3772. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3773. (push (cons (match-string 1 value) (match-string 2 value))
  3774. props)))
  3775. ((equal key "FILETAGS")
  3776. (when (string-match "\\S-" value)
  3777. (setq ftags
  3778. (append
  3779. ftags
  3780. (apply 'append
  3781. (mapcar (lambda (x) (org-split-string x ":"))
  3782. (org-split-string value)))))))
  3783. ((equal key "DRAWERS")
  3784. (setq drawers (org-split-string value splitre)))
  3785. ((equal key "CONSTANTS")
  3786. (setq const (append const (org-split-string value splitre))))
  3787. ((equal key "STARTUP")
  3788. (let ((opts (org-split-string value splitre))
  3789. l var val)
  3790. (while (setq l (pop opts))
  3791. (when (setq l (assoc l org-startup-options))
  3792. (setq var (nth 1 l) val (nth 2 l))
  3793. (if (not (nth 3 l))
  3794. (set (make-local-variable var) val)
  3795. (if (not (listp (symbol-value var)))
  3796. (set (make-local-variable var) nil))
  3797. (set (make-local-variable var) (symbol-value var))
  3798. (add-to-list var val))))))
  3799. ((equal key "ARCHIVE")
  3800. (setq arch value)
  3801. (remove-text-properties 0 (length arch)
  3802. '(face t fontified t) arch))
  3803. ((equal key "LATEX_CLASS")
  3804. (setq beamer-p (equal value "beamer")))
  3805. ((equal key "OPTIONS")
  3806. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3807. (setq scripts (read (match-string 2 value)))))
  3808. ((equal key "SETUPFILE")
  3809. (setq setup-contents (org-file-contents
  3810. (expand-file-name
  3811. (org-remove-double-quotes value))
  3812. 'noerror))
  3813. (if (not ext-setup-or-nil)
  3814. (setq ext-setup-or-nil setup-contents start 0)
  3815. (setq ext-setup-or-nil
  3816. (concat (substring ext-setup-or-nil 0 start)
  3817. "\n" setup-contents "\n"
  3818. (substring ext-setup-or-nil start)))))
  3819. ))))
  3820. (org-set-local 'org-use-sub-superscripts scripts)
  3821. (when cat
  3822. (org-set-local 'org-category (intern cat))
  3823. (push (cons "CATEGORY" cat) props))
  3824. (when prio
  3825. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3826. (setq prio (mapcar 'string-to-char prio))
  3827. (org-set-local 'org-highest-priority (nth 0 prio))
  3828. (org-set-local 'org-lowest-priority (nth 1 prio))
  3829. (org-set-local 'org-default-priority (nth 2 prio)))
  3830. (and props (org-set-local 'org-file-properties (nreverse props)))
  3831. (and ftags (org-set-local 'org-file-tags
  3832. (mapcar 'org-add-prop-inherited ftags)))
  3833. (and drawers (org-set-local 'org-drawers drawers))
  3834. (and arch (org-set-local 'org-archive-location arch))
  3835. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3836. ;; Process the TODO keywords
  3837. (unless kwds
  3838. ;; Use the global values as if they had been given locally.
  3839. (setq kwds (default-value 'org-todo-keywords))
  3840. (if (stringp (car kwds))
  3841. (setq kwds (list (cons org-todo-interpretation
  3842. (default-value 'org-todo-keywords)))))
  3843. (setq kwds (reverse kwds)))
  3844. (setq kwds (nreverse kwds))
  3845. (let (inter kws kw)
  3846. (while (setq kws (pop kwds))
  3847. (let ((kws (or
  3848. (run-hook-with-args-until-success
  3849. 'org-todo-setup-filter-hook kws)
  3850. kws)))
  3851. (setq inter (pop kws) sep (member "|" kws)
  3852. kws0 (delete "|" (copy-sequence kws))
  3853. kwsa nil
  3854. kws1 (mapcar
  3855. (lambda (x)
  3856. ;; 1 2
  3857. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3858. (progn
  3859. (setq kw (match-string 1 x)
  3860. key (and (match-end 2) (match-string 2 x))
  3861. log (org-extract-log-state-settings x))
  3862. (push (cons kw (and key (string-to-char key))) kwsa)
  3863. (and log (push log org-todo-log-states))
  3864. kw)
  3865. (error "Invalid TODO keyword %s" x)))
  3866. kws0)
  3867. kwsa (if kwsa (append '((:startgroup))
  3868. (nreverse kwsa)
  3869. '((:endgroup))))
  3870. hw (car kws1)
  3871. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3872. tail (list inter hw (car dws) (org-last dws))))
  3873. (add-to-list 'org-todo-heads hw 'append)
  3874. (push kws1 org-todo-sets)
  3875. (setq org-done-keywords (append org-done-keywords dws nil))
  3876. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3877. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3878. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3879. (setq org-todo-sets (nreverse org-todo-sets)
  3880. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3881. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3882. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3883. ;; Process the constants
  3884. (when const
  3885. (let (e cst)
  3886. (while (setq e (pop const))
  3887. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3888. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3889. (setq org-table-formula-constants-local cst)))
  3890. ;; Process the tags.
  3891. (when tags
  3892. (let (e tgs)
  3893. (while (setq e (pop tags))
  3894. (cond
  3895. ((equal e "{") (push '(:startgroup) tgs))
  3896. ((equal e "}") (push '(:endgroup) tgs))
  3897. ((equal e "\\n") (push '(:newline) tgs))
  3898. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3899. (push (cons (match-string 1 e)
  3900. (string-to-char (match-string 2 e)))
  3901. tgs))
  3902. (t (push (list e) tgs))))
  3903. (org-set-local 'org-tag-alist nil)
  3904. (while (setq e (pop tgs))
  3905. (or (and (stringp (car e))
  3906. (assoc (car e) org-tag-alist))
  3907. (push e org-tag-alist)))))
  3908. ;; Compute the regular expressions and other local variables
  3909. (if (not org-done-keywords)
  3910. (setq org-done-keywords (and org-todo-keywords-1
  3911. (list (org-last org-todo-keywords-1)))))
  3912. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3913. (length org-scheduled-string)
  3914. (length org-clock-string)
  3915. (length org-closed-string)))
  3916. org-drawer-regexp
  3917. (concat "^[ \t]*:\\("
  3918. (mapconcat 'regexp-quote org-drawers "\\|")
  3919. "\\):[ \t]*$")
  3920. org-not-done-keywords
  3921. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3922. org-todo-regexp
  3923. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3924. "\\|") "\\)\\>")
  3925. org-not-done-regexp
  3926. (concat "\\<\\("
  3927. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3928. "\\)\\>")
  3929. org-not-done-heading-regexp
  3930. (concat "^\\(\\*+\\)[ \t]+\\("
  3931. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3932. "\\)\\>")
  3933. org-todo-line-regexp
  3934. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3935. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3936. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3937. org-complex-heading-regexp
  3938. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3939. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3940. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3941. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  3942. org-complex-heading-regexp-format
  3943. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3944. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3945. "\\)\\>\\)?"
  3946. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3947. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3948. "[ \t]*\\(%s\\)"
  3949. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3950. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  3951. org-nl-done-regexp
  3952. (concat "\n\\*+[ \t]+"
  3953. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3954. "\\)" "\\>")
  3955. org-todo-line-tags-regexp
  3956. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3957. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3958. (org-re
  3959. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  3960. org-looking-at-done-regexp
  3961. (concat "^" "\\(?:"
  3962. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3963. "\\>")
  3964. org-deadline-regexp (concat "\\<" org-deadline-string)
  3965. org-deadline-time-regexp
  3966. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3967. org-deadline-line-regexp
  3968. (concat "\\<\\(" org-deadline-string "\\).*")
  3969. org-scheduled-regexp
  3970. (concat "\\<" org-scheduled-string)
  3971. org-scheduled-time-regexp
  3972. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3973. org-closed-time-regexp
  3974. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3975. org-keyword-time-regexp
  3976. (concat "\\<\\(" org-scheduled-string
  3977. "\\|" org-deadline-string
  3978. "\\|" org-closed-string
  3979. "\\|" org-clock-string "\\)"
  3980. " *[[<]\\([^]>]+\\)[]>]")
  3981. org-keyword-time-not-clock-regexp
  3982. (concat "\\<\\(" org-scheduled-string
  3983. "\\|" org-deadline-string
  3984. "\\|" org-closed-string
  3985. "\\)"
  3986. " *[[<]\\([^]>]+\\)[]>]")
  3987. org-maybe-keyword-time-regexp
  3988. (concat "\\(\\<\\(" org-scheduled-string
  3989. "\\|" org-deadline-string
  3990. "\\|" org-closed-string
  3991. "\\|" org-clock-string "\\)\\)?"
  3992. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3993. org-planning-or-clock-line-re
  3994. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3995. "\\|" org-deadline-string
  3996. "\\|" org-closed-string "\\|" org-clock-string
  3997. "\\)\\>\\)")
  3998. org-all-time-keywords
  3999. (mapcar (lambda (w) (substring w 0 -1))
  4000. (list org-scheduled-string org-deadline-string
  4001. org-clock-string org-closed-string))
  4002. )
  4003. (org-compute-latex-and-specials-regexp)
  4004. (org-set-font-lock-defaults))))
  4005. (defun org-file-contents (file &optional noerror)
  4006. "Return the contents of FILE, as a string."
  4007. (if (or (not file)
  4008. (not (file-readable-p file)))
  4009. (if noerror
  4010. (progn
  4011. (message "Cannot read file \"%s\"" file)
  4012. (ding) (sit-for 2)
  4013. "")
  4014. (error "Cannot read file \"%s\"" file))
  4015. (with-temp-buffer
  4016. (insert-file-contents file)
  4017. (buffer-string))))
  4018. (defun org-extract-log-state-settings (x)
  4019. "Extract the log state setting from a TODO keyword string.
  4020. This will extract info from a string like \"WAIT(w@/!)\"."
  4021. (let (kw key log1 log2)
  4022. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4023. (setq kw (match-string 1 x)
  4024. key (and (match-end 2) (match-string 2 x))
  4025. log1 (and (match-end 3) (match-string 3 x))
  4026. log2 (and (match-end 4) (match-string 4 x)))
  4027. (and (or log1 log2)
  4028. (list kw
  4029. (and log1 (if (equal log1 "!") 'time 'note))
  4030. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4031. (defun org-remove-keyword-keys (list)
  4032. "Remove a pair of parenthesis at the end of each string in LIST."
  4033. (mapcar (lambda (x)
  4034. (if (string-match "(.*)$" x)
  4035. (substring x 0 (match-beginning 0))
  4036. x))
  4037. list))
  4038. (defun org-assign-fast-keys (alist)
  4039. "Assign fast keys to a keyword-key alist.
  4040. Respect keys that are already there."
  4041. (let (new e (alt ?0))
  4042. (while (setq e (pop alist))
  4043. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4044. (cdr e)) ;; Key already assigned.
  4045. (push e new)
  4046. (let ((clist (string-to-list (downcase (car e))))
  4047. (used (append new alist)))
  4048. (when (= (car clist) ?@)
  4049. (pop clist))
  4050. (while (and clist (rassoc (car clist) used))
  4051. (pop clist))
  4052. (unless clist
  4053. (while (rassoc alt used)
  4054. (incf alt)))
  4055. (push (cons (car e) (or (car clist) alt)) new))))
  4056. (nreverse new)))
  4057. ;;; Some variables used in various places
  4058. (defvar org-window-configuration nil
  4059. "Used in various places to store a window configuration.")
  4060. (defvar org-selected-window nil
  4061. "Used in various places to store a window configuration.")
  4062. (defvar org-finish-function nil
  4063. "Function to be called when `C-c C-c' is used.
  4064. This is for getting out of special buffers like remember.")
  4065. ;; FIXME: Occasionally check by commenting these, to make sure
  4066. ;; no other functions uses these, forgetting to let-bind them.
  4067. (defvar entry)
  4068. (defvar last-state)
  4069. (defvar date)
  4070. ;; Defined somewhere in this file, but used before definition.
  4071. (defvar org-entities) ;; defined in org-entities.el
  4072. (defvar org-struct-menu)
  4073. (defvar org-org-menu)
  4074. (defvar org-tbl-menu)
  4075. ;;;; Define the Org-mode
  4076. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4077. (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"))
  4078. ;; We use a before-change function to check if a table might need
  4079. ;; an update.
  4080. (defvar org-table-may-need-update t
  4081. "Indicates that a table might need an update.
  4082. This variable is set by `org-before-change-function'.
  4083. `org-table-align' sets it back to nil.")
  4084. (defun org-before-change-function (beg end)
  4085. "Every change indicates that a table might need an update."
  4086. (setq org-table-may-need-update t))
  4087. (defvar org-mode-map)
  4088. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4089. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4090. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4091. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4092. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4093. (defvar org-table-buffer-is-an nil)
  4094. (defconst org-outline-regexp "\\*+ ")
  4095. ;;;###autoload
  4096. (define-derived-mode org-mode outline-mode "Org"
  4097. "Outline-based notes management and organizer, alias
  4098. \"Carsten's outline-mode for keeping track of everything.\"
  4099. Org-mode develops organizational tasks around a NOTES file which
  4100. contains information about projects as plain text. Org-mode is
  4101. implemented on top of outline-mode, which is ideal to keep the content
  4102. of large files well structured. It supports ToDo items, deadlines and
  4103. time stamps, which magically appear in the diary listing of the Emacs
  4104. calendar. Tables are easily created with a built-in table editor.
  4105. Plain text URL-like links connect to websites, emails (VM), Usenet
  4106. messages (Gnus), BBDB entries, and any files related to the project.
  4107. For printing and sharing of notes, an Org-mode file (or a part of it)
  4108. can be exported as a structured ASCII or HTML file.
  4109. The following commands are available:
  4110. \\{org-mode-map}"
  4111. ;; Get rid of Outline menus, they are not needed
  4112. ;; Need to do this here because define-derived-mode sets up
  4113. ;; the keymap so late. Still, it is a waste to call this each time
  4114. ;; we switch another buffer into org-mode.
  4115. (if (featurep 'xemacs)
  4116. (when (boundp 'outline-mode-menu-heading)
  4117. ;; Assume this is Greg's port, it uses easymenu
  4118. (easy-menu-remove outline-mode-menu-heading)
  4119. (easy-menu-remove outline-mode-menu-show)
  4120. (easy-menu-remove outline-mode-menu-hide))
  4121. (define-key org-mode-map [menu-bar headings] 'undefined)
  4122. (define-key org-mode-map [menu-bar hide] 'undefined)
  4123. (define-key org-mode-map [menu-bar show] 'undefined))
  4124. (org-load-modules-maybe)
  4125. (easy-menu-add org-org-menu)
  4126. (easy-menu-add org-tbl-menu)
  4127. (org-install-agenda-files-menu)
  4128. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4129. (add-to-invisibility-spec '(org-cwidth))
  4130. (add-to-invisibility-spec '(org-hide-block . t))
  4131. (when (featurep 'xemacs)
  4132. (org-set-local 'line-move-ignore-invisible t))
  4133. (org-set-local 'outline-regexp org-outline-regexp)
  4134. (org-set-local 'outline-level 'org-outline-level)
  4135. (when (and org-ellipsis
  4136. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4137. (fboundp 'make-glyph-code))
  4138. (unless org-display-table
  4139. (setq org-display-table (make-display-table)))
  4140. (set-display-table-slot
  4141. org-display-table 4
  4142. (vconcat (mapcar
  4143. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4144. org-ellipsis)))
  4145. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4146. (setq buffer-display-table org-display-table))
  4147. (org-set-regexps-and-options)
  4148. (when (and org-tag-faces (not org-tags-special-faces-re))
  4149. ;; tag faces set outside customize.... force initialization.
  4150. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4151. ;; Calc embedded
  4152. (org-set-local 'calc-embedded-open-mode "# ")
  4153. (modify-syntax-entry ?@ "w")
  4154. (if org-startup-truncated (setq truncate-lines t))
  4155. (org-set-local 'font-lock-unfontify-region-function
  4156. 'org-unfontify-region)
  4157. ;; Activate before-change-function
  4158. (org-set-local 'org-table-may-need-update t)
  4159. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4160. 'local)
  4161. ;; Check for running clock before killing a buffer
  4162. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4163. ;; Paragraphs and auto-filling
  4164. (org-set-autofill-regexps)
  4165. (setq indent-line-function 'org-indent-line-function)
  4166. (org-update-radio-target-regexp)
  4167. ;; Beginning/end of defun
  4168. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4169. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4170. ;; Make sure dependence stuff works reliably, even for users who set it
  4171. ;; too late :-(
  4172. (if org-enforce-todo-dependencies
  4173. (add-hook 'org-blocker-hook
  4174. 'org-block-todo-from-children-or-siblings-or-parent)
  4175. (remove-hook 'org-blocker-hook
  4176. 'org-block-todo-from-children-or-siblings-or-parent))
  4177. (if org-enforce-todo-checkbox-dependencies
  4178. (add-hook 'org-blocker-hook
  4179. 'org-block-todo-from-checkboxes)
  4180. (remove-hook 'org-blocker-hook
  4181. 'org-block-todo-from-checkboxes))
  4182. ;; Comment characters
  4183. (org-set-local 'comment-start "#")
  4184. (org-set-local 'comment-padding " ")
  4185. ;; Align options lines
  4186. (org-set-local
  4187. 'align-mode-rules-list
  4188. '((org-in-buffer-settings
  4189. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4190. (modes . '(org-mode)))))
  4191. ;; Imenu
  4192. (org-set-local 'imenu-create-index-function
  4193. 'org-imenu-get-tree)
  4194. ;; Make isearch reveal context
  4195. (if (or (featurep 'xemacs)
  4196. (not (boundp 'outline-isearch-open-invisible-function)))
  4197. ;; Emacs 21 and XEmacs make use of the hook
  4198. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4199. ;; Emacs 22 deals with this through a special variable
  4200. (org-set-local 'outline-isearch-open-invisible-function
  4201. (lambda (&rest ignore) (org-show-context 'isearch))))
  4202. ;; Turn on org-beamer-mode?
  4203. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4204. ;; Setup the pcomplete hooks
  4205. (set (make-local-variable 'pcomplete-command-completion-function)
  4206. 'org-complete-initial)
  4207. (set (make-local-variable 'pcomplete-command-name-function)
  4208. 'org-command-at-point)
  4209. (set (make-local-variable 'pcomplete-default-completion-function)
  4210. 'ignore)
  4211. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4212. 'org-parse-arguments)
  4213. (set (make-local-variable 'pcomplete-termination-string) "")
  4214. ;; If empty file that did not turn on org-mode automatically, make it to.
  4215. (if (and org-insert-mode-line-in-empty-file
  4216. (interactive-p)
  4217. (= (point-min) (point-max)))
  4218. (insert "# -*- mode: org -*-\n\n"))
  4219. (unless org-inhibit-startup
  4220. (when org-startup-align-all-tables
  4221. (let ((bmp (buffer-modified-p)))
  4222. (org-table-map-tables 'org-table-align 'quietly)
  4223. (set-buffer-modified-p bmp)))
  4224. (when org-startup-with-inline-images
  4225. (org-display-inline-images))
  4226. (when org-startup-indented
  4227. (require 'org-indent)
  4228. (org-indent-mode 1))
  4229. (unless org-inhibit-startup-visibility-stuff
  4230. (org-set-startup-visibility))))
  4231. (when (fboundp 'abbrev-table-put)
  4232. (abbrev-table-put org-mode-abbrev-table
  4233. :parents (list text-mode-abbrev-table)))
  4234. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4235. (defun org-current-time ()
  4236. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4237. (if (> (car org-time-stamp-rounding-minutes) 1)
  4238. (let ((r (car org-time-stamp-rounding-minutes))
  4239. (time (decode-time)))
  4240. (apply 'encode-time
  4241. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4242. (nthcdr 2 time))))
  4243. (current-time)))
  4244. (defun org-today ()
  4245. "Return today date, considering `org-extend-today-until'."
  4246. (time-to-days
  4247. (time-subtract (current-time)
  4248. (list 0 (* 3600 org-extend-today-until) 0))))
  4249. ;;;; Font-Lock stuff, including the activators
  4250. (defvar org-mouse-map (make-sparse-keymap))
  4251. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4252. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4253. (when org-mouse-1-follows-link
  4254. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4255. (when org-tab-follows-link
  4256. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4257. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4258. (require 'font-lock)
  4259. (defconst org-non-link-chars "]\t\n\r<>")
  4260. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4261. "shell" "elisp" "doi" "message"))
  4262. (defvar org-link-types-re nil
  4263. "Matches a link that has a url-like prefix like \"http:\"")
  4264. (defvar org-link-re-with-space nil
  4265. "Matches a link with spaces, optional angular brackets around it.")
  4266. (defvar org-link-re-with-space2 nil
  4267. "Matches a link with spaces, optional angular brackets around it.")
  4268. (defvar org-link-re-with-space3 nil
  4269. "Matches a link with spaces, only for internal part in bracket links.")
  4270. (defvar org-angle-link-re nil
  4271. "Matches link with angular brackets, spaces are allowed.")
  4272. (defvar org-plain-link-re nil
  4273. "Matches plain link, without spaces.")
  4274. (defvar org-bracket-link-regexp nil
  4275. "Matches a link in double brackets.")
  4276. (defvar org-bracket-link-analytic-regexp nil
  4277. "Regular expression used to analyze links.
  4278. Here is what the match groups contain after a match:
  4279. 1: http:
  4280. 2: http
  4281. 3: path
  4282. 4: [desc]
  4283. 5: desc")
  4284. (defvar org-bracket-link-analytic-regexp++ nil
  4285. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4286. (defvar org-any-link-re nil
  4287. "Regular expression matching any link.")
  4288. (defcustom org-match-sexp-depth 3
  4289. "Number of stacked braces for sub/superscript matching.
  4290. This has to be set before loading org.el to be effective."
  4291. :group 'org-export-translation ; ??????????????????????????/
  4292. :type 'integer)
  4293. (defun org-create-multibrace-regexp (left right n)
  4294. "Create a regular expression which will match a balanced sexp.
  4295. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4296. as single character strings.
  4297. The regexp returned will match the entire expression including the
  4298. delimiters. It will also define a single group which contains the
  4299. match except for the outermost delimiters. The maximum depth of
  4300. stacked delimiters is N. Escaping delimiters is not possible."
  4301. (let* ((nothing (concat "[^" left right "]*?"))
  4302. (or "\\|")
  4303. (re nothing)
  4304. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4305. (while (> n 1)
  4306. (setq n (1- n)
  4307. re (concat re or next)
  4308. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4309. (concat left "\\(" re "\\)" right)))
  4310. (defvar org-match-substring-regexp
  4311. (concat
  4312. "\\([^\\]\\)\\([_^]\\)\\("
  4313. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4314. "\\|"
  4315. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4316. "\\|"
  4317. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4318. "The regular expression matching a sub- or superscript.")
  4319. (defvar org-match-substring-with-braces-regexp
  4320. (concat
  4321. "\\([^\\]\\)\\([_^]\\)\\("
  4322. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4323. "\\)")
  4324. "The regular expression matching a sub- or superscript, forcing braces.")
  4325. (defun org-make-link-regexps ()
  4326. "Update the link regular expressions.
  4327. This should be called after the variable `org-link-types' has changed."
  4328. (setq org-link-types-re
  4329. (concat
  4330. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4331. org-link-re-with-space
  4332. (concat
  4333. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4334. "\\([^" org-non-link-chars " ]"
  4335. "[^" org-non-link-chars "]*"
  4336. "[^" org-non-link-chars " ]\\)>?")
  4337. org-link-re-with-space2
  4338. (concat
  4339. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4340. "\\([^" org-non-link-chars " ]"
  4341. "[^\t\n\r]*"
  4342. "[^" org-non-link-chars " ]\\)>?")
  4343. org-link-re-with-space3
  4344. (concat
  4345. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4346. "\\([^" org-non-link-chars " ]"
  4347. "[^\t\n\r]*\\)")
  4348. org-angle-link-re
  4349. (concat
  4350. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4351. "\\([^" org-non-link-chars " ]"
  4352. "[^" org-non-link-chars "]*"
  4353. "\\)>")
  4354. org-plain-link-re
  4355. (concat
  4356. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4357. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4358. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4359. org-bracket-link-regexp
  4360. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4361. org-bracket-link-analytic-regexp
  4362. (concat
  4363. "\\[\\["
  4364. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4365. "\\([^]]+\\)"
  4366. "\\]"
  4367. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4368. "\\]")
  4369. org-bracket-link-analytic-regexp++
  4370. (concat
  4371. "\\[\\["
  4372. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4373. "\\([^]]+\\)"
  4374. "\\]"
  4375. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4376. "\\]")
  4377. org-any-link-re
  4378. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4379. org-angle-link-re "\\)\\|\\("
  4380. org-plain-link-re "\\)")))
  4381. (org-make-link-regexps)
  4382. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4383. "Regular expression for fast time stamp matching.")
  4384. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4385. "Regular expression for fast time stamp matching.")
  4386. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4387. "Regular expression matching time strings for analysis.
  4388. This one does not require the space after the date, so it can be used
  4389. on a string that terminates immediately after the date.")
  4390. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4391. "Regular expression matching time strings for analysis.")
  4392. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4393. "Regular expression matching time stamps, with groups.")
  4394. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4395. "Regular expression matching time stamps (also [..]), with groups.")
  4396. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4397. "Regular expression matching a time stamp range.")
  4398. (defconst org-tr-regexp-both
  4399. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4400. "Regular expression matching a time stamp range.")
  4401. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4402. org-ts-regexp "\\)?")
  4403. "Regular expression matching a time stamp or time stamp range.")
  4404. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4405. org-ts-regexp-both "\\)?")
  4406. "Regular expression matching a time stamp or time stamp range.
  4407. The time stamps may be either active or inactive.")
  4408. (defvar org-emph-face nil)
  4409. (defun org-do-emphasis-faces (limit)
  4410. "Run through the buffer and add overlays to links."
  4411. (let (rtn a)
  4412. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4413. (if (not (= (char-after (match-beginning 3))
  4414. (char-after (match-beginning 4))))
  4415. (progn
  4416. (setq rtn t)
  4417. (setq a (assoc (match-string 3) org-emphasis-alist))
  4418. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4419. 'face
  4420. (nth 1 a))
  4421. (and (nth 4 a)
  4422. (org-remove-flyspell-overlays-in
  4423. (match-beginning 0) (match-end 0)))
  4424. (add-text-properties (match-beginning 2) (match-end 2)
  4425. '(font-lock-multiline t org-emphasis t))
  4426. (when org-hide-emphasis-markers
  4427. (add-text-properties (match-end 4) (match-beginning 5)
  4428. '(invisible org-link))
  4429. (add-text-properties (match-beginning 3) (match-end 3)
  4430. '(invisible org-link)))))
  4431. (backward-char 1))
  4432. rtn))
  4433. (defun org-emphasize (&optional char)
  4434. "Insert or change an emphasis, i.e. a font like bold or italic.
  4435. If there is an active region, change that region to a new emphasis.
  4436. If there is no region, just insert the marker characters and position
  4437. the cursor between them.
  4438. CHAR should be either the marker character, or the first character of the
  4439. HTML tag associated with that emphasis. If CHAR is a space, the means
  4440. to remove the emphasis of the selected region.
  4441. If char is not given (for example in an interactive call) it
  4442. will be prompted for."
  4443. (interactive)
  4444. (let ((eal org-emphasis-alist) e det
  4445. (erc org-emphasis-regexp-components)
  4446. (prompt "")
  4447. (string "") beg end move tag c s)
  4448. (if (org-region-active-p)
  4449. (setq beg (region-beginning) end (region-end)
  4450. string (buffer-substring beg end))
  4451. (setq move t))
  4452. (while (setq e (pop eal))
  4453. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4454. c (aref tag 0))
  4455. (push (cons c (string-to-char (car e))) det)
  4456. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4457. (substring tag 1)))))
  4458. (setq det (nreverse det))
  4459. (unless char
  4460. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4461. (setq char (read-char-exclusive)))
  4462. (setq char (or (cdr (assoc char det)) char))
  4463. (if (equal char ?\ )
  4464. (setq s "" move nil)
  4465. (unless (assoc (char-to-string char) org-emphasis-alist)
  4466. (error "No such emphasis marker: \"%c\"" char))
  4467. (setq s (char-to-string char)))
  4468. (while (and (> (length string) 1)
  4469. (equal (substring string 0 1) (substring string -1))
  4470. (assoc (substring string 0 1) org-emphasis-alist))
  4471. (setq string (substring string 1 -1)))
  4472. (setq string (concat s string s))
  4473. (if beg (delete-region beg end))
  4474. (unless (or (bolp)
  4475. (string-match (concat "[" (nth 0 erc) "\n]")
  4476. (char-to-string (char-before (point)))))
  4477. (insert " "))
  4478. (unless (or (eobp)
  4479. (string-match (concat "[" (nth 1 erc) "\n]")
  4480. (char-to-string (char-after (point)))))
  4481. (insert " ") (backward-char 1))
  4482. (insert string)
  4483. (and move (backward-char 1))))
  4484. (defconst org-nonsticky-props
  4485. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4486. (defsubst org-rear-nonsticky-at (pos)
  4487. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4488. (defun org-activate-plain-links (limit)
  4489. "Run through the buffer and add overlays to links."
  4490. (catch 'exit
  4491. (let (f)
  4492. (if (re-search-forward org-plain-link-re limit t)
  4493. (progn
  4494. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4495. (setq f (get-text-property (match-beginning 0) 'face))
  4496. (if (or (eq f 'org-tag)
  4497. (and (listp f) (memq 'org-tag f)))
  4498. nil
  4499. (add-text-properties (match-beginning 0) (match-end 0)
  4500. (list 'mouse-face 'highlight
  4501. 'face 'org-link
  4502. 'keymap org-mouse-map))
  4503. (org-rear-nonsticky-at (match-end 0)))
  4504. t)))))
  4505. (defun org-activate-code (limit)
  4506. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4507. (progn
  4508. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4509. (remove-text-properties (match-beginning 0) (match-end 0)
  4510. '(display t invisible t intangible t))
  4511. t)))
  4512. (defcustom org-src-fontify-natively nil
  4513. "When non-nil, fontify code in code blocks."
  4514. :type 'boolean
  4515. :group 'org-appearance
  4516. :group 'org-babel)
  4517. (defun org-fontify-meta-lines-and-blocks (limit)
  4518. "Fontify #+ lines and blocks, in the correct ways."
  4519. (let ((case-fold-search t))
  4520. (if (re-search-forward
  4521. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4522. limit t)
  4523. (let ((beg (match-beginning 0))
  4524. (block-start (match-end 0))
  4525. (block-end nil)
  4526. (lang (match-string 7))
  4527. (beg1 (line-beginning-position 2))
  4528. (dc1 (downcase (match-string 2)))
  4529. (dc3 (downcase (match-string 3)))
  4530. end end1 quoting block-type)
  4531. (cond
  4532. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4533. ;; a single line of backend-specific content
  4534. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4535. (remove-text-properties (match-beginning 0) (match-end 0)
  4536. '(display t invisible t intangible t))
  4537. (add-text-properties (match-beginning 1) (match-end 3)
  4538. '(font-lock-fontified t face org-meta-line))
  4539. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4540. '(font-lock-fontified t face org-block))
  4541. ; for backend-specific code
  4542. t)
  4543. ((and (match-end 4) (equal dc3 "begin"))
  4544. ;; Truly a block
  4545. (setq block-type (downcase (match-string 5))
  4546. quoting (member block-type org-protecting-blocks))
  4547. (when (re-search-forward
  4548. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4549. nil t) ;; on purpose, we look further than LIMIT
  4550. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4551. (setq block-end (match-beginning 0))
  4552. (when quoting
  4553. (remove-text-properties beg end
  4554. '(display t invisible t intangible t)))
  4555. (add-text-properties
  4556. beg end
  4557. '(font-lock-fontified t font-lock-multiline t))
  4558. (add-text-properties beg beg1 '(face org-meta-line))
  4559. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4560. ; for end_src
  4561. (cond
  4562. ((and lang org-src-fontify-natively)
  4563. (org-src-font-lock-fontify-block lang block-start block-end))
  4564. (quoting
  4565. (add-text-properties beg1 (+ end1 1) '(face
  4566. org-block)))
  4567. ; end of source block
  4568. ((not org-fontify-quote-and-verse-blocks))
  4569. ((string= block-type "quote")
  4570. (add-text-properties beg1 end1 '(face org-quote)))
  4571. ((string= block-type "verse")
  4572. (add-text-properties beg1 end1 '(face org-verse))))
  4573. t))
  4574. ((member dc1 '("title:" "author:" "email:" "date:"))
  4575. (add-text-properties
  4576. beg (match-end 3)
  4577. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4578. '(font-lock-fontified t invisible t)
  4579. '(font-lock-fontified t face org-document-info-keyword)))
  4580. (add-text-properties
  4581. (match-beginning 6) (match-end 6)
  4582. (if (string-equal dc1 "title:")
  4583. '(font-lock-fontified t face org-document-title)
  4584. '(font-lock-fontified t face org-document-info))))
  4585. ((not (member (char-after beg) '(?\ ?\t)))
  4586. ;; just any other in-buffer setting, but not indented
  4587. (add-text-properties
  4588. beg (match-end 0)
  4589. '(font-lock-fontified t face org-meta-line))
  4590. t)
  4591. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4592. "orgtbl:" "tblfm:" "tblname:" "result:"
  4593. "results:" "source:" "srcname:" "call:"))
  4594. (and (match-end 4) (equal dc3 "attr")))
  4595. (add-text-properties
  4596. beg (match-end 0)
  4597. '(font-lock-fontified t face org-meta-line))
  4598. t)
  4599. ((member dc3 '(" " ""))
  4600. (add-text-properties
  4601. beg (match-end 0)
  4602. '(font-lock-fontified t face font-lock-comment-face)))
  4603. (t nil))))))
  4604. (defun org-activate-angle-links (limit)
  4605. "Run through the buffer and add overlays to links."
  4606. (if (re-search-forward org-angle-link-re limit t)
  4607. (progn
  4608. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4609. (add-text-properties (match-beginning 0) (match-end 0)
  4610. (list 'mouse-face 'highlight
  4611. 'keymap org-mouse-map))
  4612. (org-rear-nonsticky-at (match-end 0))
  4613. t)))
  4614. (defun org-activate-footnote-links (limit)
  4615. "Run through the buffer and add overlays to links."
  4616. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4617. limit t)
  4618. (progn
  4619. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4620. (add-text-properties (match-beginning 2) (match-end 2)
  4621. (list 'mouse-face 'highlight
  4622. 'keymap org-mouse-map
  4623. 'help-echo
  4624. (if (= (point-at-bol) (match-beginning 2))
  4625. "Footnote definition"
  4626. "Footnote reference")
  4627. ))
  4628. (org-rear-nonsticky-at (match-end 2))
  4629. t)))
  4630. (defun org-activate-bracket-links (limit)
  4631. "Run through the buffer and add overlays to bracketed links."
  4632. (if (re-search-forward org-bracket-link-regexp limit t)
  4633. (let* ((help (concat "LINK: "
  4634. (org-match-string-no-properties 1)))
  4635. ;; FIXME: above we should remove the escapes.
  4636. ;; but that requires another match, protecting match data,
  4637. ;; a lot of overhead for font-lock.
  4638. (ip (org-maybe-intangible
  4639. (list 'invisible 'org-link
  4640. 'keymap org-mouse-map 'mouse-face 'highlight
  4641. 'font-lock-multiline t 'help-echo help)))
  4642. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4643. 'font-lock-multiline t 'help-echo help)))
  4644. ;; We need to remove the invisible property here. Table narrowing
  4645. ;; may have made some of this invisible.
  4646. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4647. (remove-text-properties (match-beginning 0) (match-end 0)
  4648. '(invisible nil))
  4649. (if (match-end 3)
  4650. (progn
  4651. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4652. (org-rear-nonsticky-at (match-beginning 3))
  4653. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4654. (org-rear-nonsticky-at (match-end 3))
  4655. (add-text-properties (match-end 3) (match-end 0) ip)
  4656. (org-rear-nonsticky-at (match-end 0)))
  4657. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4658. (org-rear-nonsticky-at (match-beginning 1))
  4659. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4660. (org-rear-nonsticky-at (match-end 1))
  4661. (add-text-properties (match-end 1) (match-end 0) ip)
  4662. (org-rear-nonsticky-at (match-end 0)))
  4663. t)))
  4664. (defun org-activate-dates (limit)
  4665. "Run through the buffer and add overlays to dates."
  4666. (if (re-search-forward org-tsr-regexp-both limit t)
  4667. (progn
  4668. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4669. (add-text-properties (match-beginning 0) (match-end 0)
  4670. (list 'mouse-face 'highlight
  4671. 'keymap org-mouse-map))
  4672. (org-rear-nonsticky-at (match-end 0))
  4673. (when org-display-custom-times
  4674. (if (match-end 3)
  4675. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4676. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4677. t)))
  4678. (defvar org-target-link-regexp nil
  4679. "Regular expression matching radio targets in plain text.")
  4680. (make-variable-buffer-local 'org-target-link-regexp)
  4681. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4682. "Regular expression matching a link target.")
  4683. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4684. "Regular expression matching a radio target.")
  4685. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4686. "Regular expression matching any target.")
  4687. (defun org-activate-target-links (limit)
  4688. "Run through the buffer and add overlays to target matches."
  4689. (when org-target-link-regexp
  4690. (let ((case-fold-search t))
  4691. (if (re-search-forward org-target-link-regexp limit t)
  4692. (progn
  4693. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4694. (add-text-properties (match-beginning 0) (match-end 0)
  4695. (list 'mouse-face 'highlight
  4696. 'keymap org-mouse-map
  4697. 'help-echo "Radio target link"
  4698. 'org-linked-text t))
  4699. (org-rear-nonsticky-at (match-end 0))
  4700. t)))))
  4701. (defun org-update-radio-target-regexp ()
  4702. "Find all radio targets in this file and update the regular expression."
  4703. (interactive)
  4704. (when (memq 'radio org-activate-links)
  4705. (setq org-target-link-regexp
  4706. (org-make-target-link-regexp (org-all-targets 'radio)))
  4707. (org-restart-font-lock)))
  4708. (defun org-hide-wide-columns (limit)
  4709. (let (s e)
  4710. (setq s (text-property-any (point) (or limit (point-max))
  4711. 'org-cwidth t))
  4712. (when s
  4713. (setq e (next-single-property-change s 'org-cwidth))
  4714. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4715. (goto-char e)
  4716. t)))
  4717. (defvar org-latex-and-specials-regexp nil
  4718. "Regular expression for highlighting export special stuff.")
  4719. (defvar org-match-substring-regexp)
  4720. (defvar org-match-substring-with-braces-regexp)
  4721. ;; This should be with the exporter code, but we also use if for font-locking
  4722. (defconst org-export-html-special-string-regexps
  4723. '(("\\\\-" . "&shy;")
  4724. ("---\\([^-]\\)" . "&mdash;\\1")
  4725. ("--\\([^-]\\)" . "&ndash;\\1")
  4726. ("\\.\\.\\." . "&hellip;"))
  4727. "Regular expressions for special string conversion.")
  4728. (defun org-compute-latex-and-specials-regexp ()
  4729. "Compute regular expression for stuff treated specially by exporters."
  4730. (if (not org-highlight-latex-fragments-and-specials)
  4731. (org-set-local 'org-latex-and-specials-regexp nil)
  4732. (require 'org-exp)
  4733. (let*
  4734. ((matchers (plist-get org-format-latex-options :matchers))
  4735. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4736. org-latex-regexps)))
  4737. (org-export-allow-BIND nil)
  4738. (options (org-combine-plists (org-default-export-plist)
  4739. (org-infile-export-plist)))
  4740. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4741. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4742. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4743. (org-export-html-expand (plist-get options :expand-quoted-html))
  4744. (org-export-with-special-strings (plist-get options :special-strings))
  4745. (re-sub
  4746. (cond
  4747. ((equal org-export-with-sub-superscripts '{})
  4748. (list org-match-substring-with-braces-regexp))
  4749. (org-export-with-sub-superscripts
  4750. (list org-match-substring-regexp))
  4751. (t nil)))
  4752. (re-latex
  4753. (if org-export-with-LaTeX-fragments
  4754. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4755. (re-macros
  4756. (if org-export-with-TeX-macros
  4757. (list (concat "\\\\"
  4758. (regexp-opt
  4759. (append
  4760. (delq nil
  4761. (mapcar 'car-safe
  4762. (append org-entities-user
  4763. org-entities)))
  4764. (if (boundp 'org-latex-entities)
  4765. (mapcar (lambda (x)
  4766. (or (car-safe x) x))
  4767. org-latex-entities)
  4768. nil))
  4769. 'words))) ; FIXME
  4770. ))
  4771. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4772. (re-special (if org-export-with-special-strings
  4773. (mapcar (lambda (x) (car x))
  4774. org-export-html-special-string-regexps)))
  4775. (re-rest
  4776. (delq nil
  4777. (list
  4778. (if org-export-html-expand "@<[^>\n]+>")
  4779. ))))
  4780. (org-set-local
  4781. 'org-latex-and-specials-regexp
  4782. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4783. re-rest) "\\|")))))
  4784. (defun org-do-latex-and-special-faces (limit)
  4785. "Run through the buffer and add overlays to links."
  4786. (when org-latex-and-specials-regexp
  4787. (let (rtn d)
  4788. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4789. limit t))
  4790. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4791. 'face))
  4792. '(org-code org-verbatim underline)))
  4793. (progn
  4794. (setq rtn t
  4795. d (cond ((member (char-after (1+ (match-beginning 0)))
  4796. '(?_ ?^)) 1)
  4797. (t 0)))
  4798. (font-lock-prepend-text-property
  4799. (+ d (match-beginning 0)) (match-end 0)
  4800. 'face 'org-latex-and-export-specials)
  4801. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4802. '(font-lock-multiline t)))))
  4803. rtn)))
  4804. (defun org-restart-font-lock ()
  4805. "Restart `font-lock-mode', to force refontification."
  4806. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4807. (font-lock-mode -1)
  4808. (font-lock-mode 1)))
  4809. (defun org-all-targets (&optional radio)
  4810. "Return a list of all targets in this file.
  4811. With optional argument RADIO, only find radio targets."
  4812. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4813. rtn)
  4814. (save-excursion
  4815. (goto-char (point-min))
  4816. (while (re-search-forward re nil t)
  4817. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4818. rtn)))
  4819. (defun org-make-target-link-regexp (targets)
  4820. "Make regular expression matching all strings in TARGETS.
  4821. The regular expression finds the targets also if there is a line break
  4822. between words."
  4823. (and targets
  4824. (concat
  4825. "\\<\\("
  4826. (mapconcat
  4827. (lambda (x)
  4828. (setq x (regexp-quote x))
  4829. (while (string-match " +" x)
  4830. (setq x (replace-match "\\s-+" t t x)))
  4831. x)
  4832. targets
  4833. "\\|")
  4834. "\\)\\>")))
  4835. (defun org-activate-tags (limit)
  4836. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4837. (progn
  4838. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4839. (add-text-properties (match-beginning 1) (match-end 1)
  4840. (list 'mouse-face 'highlight
  4841. 'keymap org-mouse-map))
  4842. (org-rear-nonsticky-at (match-end 1))
  4843. t)))
  4844. (defun org-outline-level ()
  4845. "Compute the outline level of the heading at point.
  4846. This function assumes that the cursor is at the beginning of a line matched
  4847. by `outline-regexp'. Otherwise it returns garbage.
  4848. If this is called at a normal headline, the level is the number of stars.
  4849. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4850. For plain list items, if they are matched by `outline-regexp', this returns
  4851. 1000 plus the line indentation."
  4852. (save-excursion
  4853. (looking-at outline-regexp)
  4854. (if (match-beginning 1)
  4855. (+ (org-get-string-indentation (match-string 1)) 1000)
  4856. (1- (- (match-end 0) (match-beginning 0))))))
  4857. (defvar org-font-lock-keywords nil)
  4858. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4859. "Regular expression matching a property line.")
  4860. (defvar org-font-lock-hook nil
  4861. "Functions to be called for special font lock stuff.")
  4862. (defvar org-font-lock-set-keywords-hook nil
  4863. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4864. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4865. it is installed to be used by font lock. This can be useful if something
  4866. needs to be inserted at a specific position in the font-lock sequence.")
  4867. (defun org-font-lock-hook (limit)
  4868. (run-hook-with-args 'org-font-lock-hook limit))
  4869. (defun org-set-font-lock-defaults ()
  4870. (let* ((em org-fontify-emphasized-text)
  4871. (lk org-activate-links)
  4872. (org-font-lock-extra-keywords
  4873. (list
  4874. ;; Call the hook
  4875. '(org-font-lock-hook)
  4876. ;; Headlines
  4877. `(,(if org-fontify-whole-heading-line
  4878. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4879. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4880. (1 (org-get-level-face 1))
  4881. (2 (org-get-level-face 2))
  4882. (3 (org-get-level-face 3)))
  4883. ;; Table lines
  4884. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4885. (1 'org-table t))
  4886. ;; Table internals
  4887. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4888. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4889. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4890. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  4891. ;; Drawers
  4892. (list org-drawer-regexp '(0 'org-special-keyword t))
  4893. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4894. ;; Properties
  4895. (list org-property-re
  4896. '(1 'org-special-keyword t)
  4897. '(3 'org-property-value t))
  4898. ;; Links
  4899. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4900. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4901. (if (memq 'plain lk) '(org-activate-plain-links))
  4902. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4903. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4904. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4905. (if (memq 'footnote lk) '(org-activate-footnote-links
  4906. (2 'org-footnote t)))
  4907. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4908. '(org-hide-wide-columns (0 nil append))
  4909. ;; TODO lines
  4910. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4911. '(1 (org-get-todo-face 1) t))
  4912. ;; DONE
  4913. (if org-fontify-done-headline
  4914. (list (concat "^[*]+ +\\<\\("
  4915. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4916. "\\)\\(.*\\)")
  4917. '(2 'org-headline-done t))
  4918. nil)
  4919. ;; Priorities
  4920. '(org-font-lock-add-priority-faces)
  4921. ;; Tags
  4922. '(org-font-lock-add-tag-faces)
  4923. ;; Special keywords
  4924. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4925. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4926. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4927. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4928. ;; Emphasis
  4929. (if em
  4930. (if (featurep 'xemacs)
  4931. '(org-do-emphasis-faces (0 nil append))
  4932. '(org-do-emphasis-faces)))
  4933. ;; Checkboxes
  4934. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4935. 1 'org-checkbox prepend)
  4936. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4937. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4938. (0 (org-get-checkbox-statistics-face) t)))
  4939. ;; Description list items
  4940. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(.*? ::\\)"
  4941. 2 'bold prepend)
  4942. ;; ARCHIVEd headings
  4943. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4944. '(1 'org-archived prepend))
  4945. ;; Specials
  4946. '(org-do-latex-and-special-faces)
  4947. '(org-fontify-entities)
  4948. '(org-raise-scripts)
  4949. ;; Code
  4950. '(org-activate-code (1 'org-code t))
  4951. ;; COMMENT
  4952. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4953. "\\|" org-quote-string "\\)\\>")
  4954. '(1 'org-special-keyword t))
  4955. '("^#.*" (0 'font-lock-comment-face t))
  4956. ;; Blocks and meta lines
  4957. '(org-fontify-meta-lines-and-blocks)
  4958. )))
  4959. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4960. (run-hooks 'org-font-lock-set-keywords-hook)
  4961. ;; Now set the full font-lock-keywords
  4962. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4963. (org-set-local 'font-lock-defaults
  4964. '(org-font-lock-keywords t nil nil backward-paragraph))
  4965. (kill-local-variable 'font-lock-keywords) nil))
  4966. (defun org-toggle-pretty-entities ()
  4967. "Toggle the composition display of entities as UTF8 characters."
  4968. (interactive)
  4969. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4970. (org-restart-font-lock)
  4971. (if org-pretty-entities
  4972. (message "Entities are displayed as UTF8 characers")
  4973. (save-restriction
  4974. (widen)
  4975. (org-decompose-region (point-min) (point-max))
  4976. (message "Entities are displayed plain"))))
  4977. (defun org-fontify-entities (limit)
  4978. "Find an entity to fontify."
  4979. (let (ee)
  4980. (when org-pretty-entities
  4981. (catch 'match
  4982. (while (re-search-forward
  4983. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  4984. limit t)
  4985. (if (and (not (org-in-indented-comment-line))
  4986. (setq ee (org-entity-get (match-string 1)))
  4987. (= (length (nth 6 ee)) 1))
  4988. (progn
  4989. (add-text-properties
  4990. (match-beginning 0) (match-end 1)
  4991. (list 'font-lock-fontified t))
  4992. (compose-region (match-beginning 0) (match-end 1)
  4993. (nth 6 ee) nil)
  4994. (backward-char 1)
  4995. (throw 'match t))))
  4996. nil))))
  4997. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4998. "Fontify string S like in Org-mode."
  4999. (with-temp-buffer
  5000. (insert s)
  5001. (let ((org-odd-levels-only odd-levels))
  5002. (org-mode)
  5003. (font-lock-fontify-buffer)
  5004. (buffer-string))))
  5005. (defvar org-m nil)
  5006. (defvar org-l nil)
  5007. (defvar org-f nil)
  5008. (defun org-get-level-face (n)
  5009. "Get the right face for match N in font-lock matching of headlines."
  5010. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5011. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5012. (if org-cycle-level-faces
  5013. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5014. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5015. (cond
  5016. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5017. ((eq n 2) org-f)
  5018. (t (if org-level-color-stars-only nil org-f))))
  5019. (defun org-get-todo-face (kwd)
  5020. "Get the right face for a TODO keyword KWD.
  5021. If KWD is a number, get the corresponding match group."
  5022. (if (numberp kwd) (setq kwd (match-string kwd)))
  5023. (or (org-face-from-face-or-color
  5024. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5025. (and (member kwd org-done-keywords) 'org-done)
  5026. 'org-todo))
  5027. (defun org-face-from-face-or-color (context inherit face-or-color)
  5028. "Create a face list that inherits INHERIT, but sets the foreground color.
  5029. When FACE-OR-COLOR is not a string, just return it."
  5030. (if (stringp face-or-color)
  5031. (list :inherit inherit
  5032. (cdr (assoc context org-faces-easy-properties))
  5033. face-or-color)
  5034. face-or-color))
  5035. (defun org-font-lock-add-tag-faces (limit)
  5036. "Add the special tag faces."
  5037. (when (and org-tag-faces org-tags-special-faces-re)
  5038. (while (re-search-forward org-tags-special-faces-re limit t)
  5039. (add-text-properties (match-beginning 1) (match-end 1)
  5040. (list 'face (org-get-tag-face 1)
  5041. 'font-lock-fontified t))
  5042. (backward-char 1))))
  5043. (defun org-font-lock-add-priority-faces (limit)
  5044. "Add the special priority faces."
  5045. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5046. (add-text-properties
  5047. (match-beginning 0) (match-end 0)
  5048. (list 'face (or (org-face-from-face-or-color
  5049. 'priority 'org-special-keyword
  5050. (cdr (assoc (char-after (match-beginning 1))
  5051. org-priority-faces)))
  5052. 'org-special-keyword)
  5053. 'font-lock-fontified t))))
  5054. (defun org-get-tag-face (kwd)
  5055. "Get the right face for a TODO keyword KWD.
  5056. If KWD is a number, get the corresponding match group."
  5057. (if (numberp kwd) (setq kwd (match-string kwd)))
  5058. (or (org-face-from-face-or-color
  5059. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5060. 'org-tag))
  5061. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5062. "Remove fontification and activation overlays from links."
  5063. (font-lock-default-unfontify-region beg end)
  5064. (let* ((buffer-undo-list t)
  5065. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5066. (inhibit-modification-hooks t)
  5067. deactivate-mark buffer-file-name buffer-file-truename)
  5068. (org-decompose-region beg end)
  5069. (remove-text-properties
  5070. beg end
  5071. (if org-indent-mode
  5072. ;; also remove line-prefix and wrap-prefix properties
  5073. '(mouse-face t keymap t org-linked-text t
  5074. invisible t intangible t
  5075. line-prefix t wrap-prefix t
  5076. org-no-flyspell t org-emphasis t)
  5077. '(mouse-face t keymap t org-linked-text t
  5078. invisible t intangible t
  5079. org-no-flyspell t org-emphasis t)))
  5080. (org-remove-font-lock-display-properties beg end)))
  5081. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5082. ((raise 0.3) (height 0.7))
  5083. ((raise -0.5))
  5084. ((raise 0.5)))
  5085. "Display properties for showing superscripts and subscripts.")
  5086. (defun org-remove-font-lock-display-properties (beg end)
  5087. "Remove specific display properties that have been added by font lock.
  5088. The will remove the raise properties that are used to show superscripts
  5089. and subscripts."
  5090. (let (next prop)
  5091. (while (< beg end)
  5092. (setq next (next-single-property-change beg 'display nil end)
  5093. prop (get-text-property beg 'display))
  5094. (if (member prop org-script-display)
  5095. (put-text-property beg next 'display nil))
  5096. (setq beg next))))
  5097. (defun org-raise-scripts (limit)
  5098. "Add raise properties to sub/superscripts."
  5099. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5100. (if (re-search-forward
  5101. (if (eq org-use-sub-superscripts t)
  5102. org-match-substring-regexp
  5103. org-match-substring-with-braces-regexp)
  5104. limit t)
  5105. (let* ((pos (point)) table-p comment-p
  5106. (mpos (match-beginning 3))
  5107. (emph-p (get-text-property mpos 'org-emphasis))
  5108. (link-p (get-text-property mpos 'mouse-face))
  5109. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5110. (goto-char (point-at-bol))
  5111. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5112. comment-p (org-looking-at-p "[ \t]*#"))
  5113. (goto-char pos)
  5114. ;; FIXME: Should we go back one character here, for a_b^c
  5115. ;; (goto-char (1- pos)) ;????????????????????
  5116. (if (or comment-p emph-p link-p keyw-p)
  5117. t
  5118. (put-text-property (match-beginning 3) (match-end 0)
  5119. 'display
  5120. (if (equal (char-after (match-beginning 2)) ?^)
  5121. (nth (if table-p 3 1) org-script-display)
  5122. (nth (if table-p 2 0) org-script-display)))
  5123. (add-text-properties (match-beginning 2) (match-end 2)
  5124. (list 'invisible t
  5125. 'org-dwidth t 'org-dwidth-n 1))
  5126. (if (and (eq (char-after (match-beginning 3)) ?{)
  5127. (eq (char-before (match-end 3)) ?}))
  5128. (progn
  5129. (add-text-properties
  5130. (match-beginning 3) (1+ (match-beginning 3))
  5131. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5132. (add-text-properties
  5133. (1- (match-end 3)) (match-end 3)
  5134. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5135. t)))))
  5136. ;;;; Visibility cycling, including org-goto and indirect buffer
  5137. ;;; Cycling
  5138. (defvar org-cycle-global-status nil)
  5139. (make-variable-buffer-local 'org-cycle-global-status)
  5140. (defvar org-cycle-subtree-status nil)
  5141. (make-variable-buffer-local 'org-cycle-subtree-status)
  5142. ;;;###autoload
  5143. (defvar org-inlinetask-min-level)
  5144. (defun org-cycle (&optional arg)
  5145. "TAB-action and visibility cycling for Org-mode.
  5146. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5147. is outline visibility cycling, but it also invokes other actions
  5148. in special contexts.
  5149. - When this function is called with a prefix argument, rotate the entire
  5150. buffer through 3 states (global cycling)
  5151. 1. OVERVIEW: Show only top-level headlines.
  5152. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5153. 3. SHOW ALL: Show everything.
  5154. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5155. determined by the variable `org-startup-folded', and by any VISIBILITY
  5156. properties in the buffer.
  5157. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5158. including any drawers.
  5159. - When inside a table, re-align the table and move to the next field.
  5160. - When point is at the beginning of a headline, rotate the subtree started
  5161. by this line through 3 different states (local cycling)
  5162. 1. FOLDED: Only the main headline is shown.
  5163. 2. CHILDREN: The main headline and the direct children are shown.
  5164. From this state, you can move to one of the children
  5165. and zoom in further.
  5166. 3. SUBTREE: Show the entire subtree, including body text.
  5167. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5168. - When point is at the beginning of an empty headline and the variable
  5169. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5170. of the headline by demoting and promoting it to likely levels. This
  5171. speeds up creation document structure by pressing TAB once or several
  5172. times right after creating a new headline.
  5173. - When there is a numeric prefix, go up to a heading with level ARG, do
  5174. a `show-subtree' and return to the previous cursor position. If ARG
  5175. is negative, go up that many levels.
  5176. - When point is not at the beginning of a headline, execute the global
  5177. binding for TAB, which is re-indenting the line. See the option
  5178. `org-cycle-emulate-tab' for details.
  5179. - Special case: if point is at the beginning of the buffer and there is
  5180. no headline in line 1, this function will act as if called with prefix arg
  5181. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5182. But only if also the variable `org-cycle-global-at-bob' is t."
  5183. (interactive "P")
  5184. (org-load-modules-maybe)
  5185. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5186. (and org-cycle-level-after-item/entry-creation
  5187. (or (org-cycle-level)
  5188. (org-cycle-item-indentation))))
  5189. (let* ((limit-level
  5190. (or org-cycle-max-level
  5191. (and (boundp 'org-inlinetask-min-level)
  5192. org-inlinetask-min-level
  5193. (1- org-inlinetask-min-level))))
  5194. (nstars (and limit-level
  5195. (if org-odd-levels-only
  5196. (and limit-level (1- (* limit-level 2)))
  5197. limit-level)))
  5198. (outline-regexp
  5199. (cond
  5200. ((not (org-mode-p)) outline-regexp)
  5201. ((or (eq org-cycle-include-plain-lists 'integrate)
  5202. (and org-cycle-include-plain-lists (org-at-item-p)))
  5203. (concat "\\(?:\\*"
  5204. (if nstars (format "\\{1,%d\\}" nstars) "+")
  5205. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  5206. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  5207. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5208. (not (looking-at outline-regexp))))
  5209. (org-cycle-hook
  5210. (if bob-special
  5211. (delq 'org-optimize-window-after-visibility-change
  5212. (copy-sequence org-cycle-hook))
  5213. org-cycle-hook))
  5214. (pos (point)))
  5215. (if (or bob-special (equal arg '(4)))
  5216. ;; special case: use global cycling
  5217. (setq arg t))
  5218. (cond
  5219. ((equal arg '(16))
  5220. (setq last-command 'dummy)
  5221. (org-set-startup-visibility)
  5222. (message "Startup visibility, plus VISIBILITY properties"))
  5223. ((equal arg '(64))
  5224. (show-all)
  5225. (message "Entire buffer visible, including drawers"))
  5226. ((org-at-table-p 'any)
  5227. ;; Enter the table or move to the next field in the table
  5228. (if (org-at-table.el-p)
  5229. (message "Use C-c ' to edit table.el tables")
  5230. (if arg (org-table-edit-field t)
  5231. (org-table-justify-field-maybe)
  5232. (call-interactively 'org-table-next-field))))
  5233. ((run-hook-with-args-until-success
  5234. 'org-tab-after-check-for-table-hook))
  5235. ((eq arg t) ;; Global cycling
  5236. (org-cycle-internal-global))
  5237. ((and org-drawers org-drawer-regexp
  5238. (save-excursion
  5239. (beginning-of-line 1)
  5240. (looking-at org-drawer-regexp)))
  5241. ;; Toggle block visibility
  5242. (org-flag-drawer
  5243. (not (get-char-property (match-end 0) 'invisible))))
  5244. ((integerp arg)
  5245. ;; Show-subtree, ARG levels up from here.
  5246. (save-excursion
  5247. (org-back-to-heading)
  5248. (outline-up-heading (if (< arg 0) (- arg)
  5249. (- (funcall outline-level) arg)))
  5250. (org-show-subtree)))
  5251. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5252. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5253. (org-cycle-internal-local))
  5254. ;; TAB emulation and template completion
  5255. (buffer-read-only (org-back-to-heading))
  5256. ((run-hook-with-args-until-success
  5257. 'org-tab-after-check-for-cycling-hook))
  5258. ((org-try-structure-completion))
  5259. ((org-try-cdlatex-tab))
  5260. ((run-hook-with-args-until-success
  5261. 'org-tab-before-tab-emulation-hook))
  5262. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5263. (or (not (bolp))
  5264. (not (looking-at outline-regexp))))
  5265. (call-interactively (global-key-binding "\t")))
  5266. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5267. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5268. (or (and (eq org-cycle-emulate-tab 'white)
  5269. (= (match-end 0) (point-at-eol)))
  5270. (and (eq org-cycle-emulate-tab 'whitestart)
  5271. (>= (match-end 0) pos))))
  5272. t
  5273. (eq org-cycle-emulate-tab t))
  5274. (call-interactively (global-key-binding "\t")))
  5275. (t (save-excursion
  5276. (org-back-to-heading)
  5277. (org-cycle)))))))
  5278. (defun org-cycle-internal-global ()
  5279. "Do the global cycling action."
  5280. (cond
  5281. ((and (eq last-command this-command)
  5282. (eq org-cycle-global-status 'overview))
  5283. ;; We just created the overview - now do table of contents
  5284. ;; This can be slow in very large buffers, so indicate action
  5285. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5286. (message "CONTENTS...")
  5287. (org-content)
  5288. (message "CONTENTS...done")
  5289. (setq org-cycle-global-status 'contents)
  5290. (run-hook-with-args 'org-cycle-hook 'contents))
  5291. ((and (eq last-command this-command)
  5292. (eq org-cycle-global-status 'contents))
  5293. ;; We just showed the table of contents - now show everything
  5294. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5295. (show-all)
  5296. (message "SHOW ALL")
  5297. (setq org-cycle-global-status 'all)
  5298. (run-hook-with-args 'org-cycle-hook 'all))
  5299. (t
  5300. ;; Default action: go to overview
  5301. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5302. (org-overview)
  5303. (message "OVERVIEW")
  5304. (setq org-cycle-global-status 'overview)
  5305. (run-hook-with-args 'org-cycle-hook 'overview))))
  5306. (defun org-cycle-internal-local ()
  5307. "Do the local cycling action."
  5308. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  5309. ;; First, some boundaries
  5310. (save-excursion
  5311. (org-back-to-heading)
  5312. (setq level (funcall outline-level))
  5313. (save-excursion
  5314. (beginning-of-line 2)
  5315. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  5316. ; XEmacs does not have `next-single-char-property-change'
  5317. ; I'm not sure about Emacs 21.
  5318. (while (and (not (eobp)) ;; this is like `next-line'
  5319. (get-char-property (1- (point)) 'invisible))
  5320. (beginning-of-line 2))
  5321. (while (and (not (eobp)) ;; this is like `next-line'
  5322. (get-char-property (1- (point)) 'invisible))
  5323. (goto-char (next-single-char-property-change (point) 'invisible))
  5324. (and (eolp) (beginning-of-line 2))))
  5325. (setq eol (point)))
  5326. (outline-end-of-heading) (setq eoh (point))
  5327. (save-excursion
  5328. (outline-next-heading)
  5329. (setq has-children (and (org-at-heading-p t)
  5330. (> (funcall outline-level) level))))
  5331. ;; if we're in a list, org-end-of-subtree is in fact org-end-of-item.
  5332. (if (org-at-item-p)
  5333. (setq eos (if (and (org-end-of-item) (bolp))
  5334. (1- (point))
  5335. (point)))
  5336. (org-end-of-subtree t)
  5337. (unless (eobp)
  5338. (skip-chars-forward " \t\n"))
  5339. (setq eos (if (eobp) (point) (1- (point))))))
  5340. ;; Find out what to do next and set `this-command'
  5341. (cond
  5342. ((= eos eoh)
  5343. ;; Nothing is hidden behind this heading
  5344. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5345. (message "EMPTY ENTRY")
  5346. (setq org-cycle-subtree-status nil)
  5347. (save-excursion
  5348. (goto-char eos)
  5349. (outline-next-heading)
  5350. (if (org-invisible-p) (org-flag-heading nil))))
  5351. ((and (or (>= eol eos)
  5352. (not (string-match "\\S-" (buffer-substring eol eos))))
  5353. (or has-children
  5354. (not (setq children-skipped
  5355. org-cycle-skip-children-state-if-no-children))))
  5356. ;; Entire subtree is hidden in one line: children view
  5357. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5358. (org-show-entry)
  5359. (show-children)
  5360. (message "CHILDREN")
  5361. (save-excursion
  5362. (goto-char eos)
  5363. (outline-next-heading)
  5364. (if (org-invisible-p) (org-flag-heading nil)))
  5365. (setq org-cycle-subtree-status 'children)
  5366. (run-hook-with-args 'org-cycle-hook 'children))
  5367. ((or children-skipped
  5368. (and (eq last-command this-command)
  5369. (eq org-cycle-subtree-status 'children)))
  5370. ;; We just showed the children, or no children are there,
  5371. ;; now show everything.
  5372. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5373. (outline-flag-region eoh eos nil)
  5374. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5375. (setq org-cycle-subtree-status 'subtree)
  5376. (run-hook-with-args 'org-cycle-hook 'subtree))
  5377. (t
  5378. ;; Default action: hide the subtree.
  5379. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5380. (outline-flag-region eoh eos t)
  5381. (message "FOLDED")
  5382. (setq org-cycle-subtree-status 'folded)
  5383. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5384. ;;;###autoload
  5385. (defun org-global-cycle (&optional arg)
  5386. "Cycle the global visibility. For details see `org-cycle'.
  5387. With \\[universal-argument] prefix arg, switch to startup visibility.
  5388. With a numeric prefix, show all headlines up to that level."
  5389. (interactive "P")
  5390. (let ((org-cycle-include-plain-lists
  5391. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5392. (cond
  5393. ((integerp arg)
  5394. (show-all)
  5395. (hide-sublevels arg)
  5396. (setq org-cycle-global-status 'contents))
  5397. ((equal arg '(4))
  5398. (org-set-startup-visibility)
  5399. (message "Startup visibility, plus VISIBILITY properties."))
  5400. (t
  5401. (org-cycle '(4))))))
  5402. (defun org-set-startup-visibility ()
  5403. "Set the visibility required by startup options and properties."
  5404. (cond
  5405. ((eq org-startup-folded t)
  5406. (org-cycle '(4)))
  5407. ((eq org-startup-folded 'content)
  5408. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5409. (org-cycle '(4)) (org-cycle '(4)))))
  5410. (unless (eq org-startup-folded 'showeverything)
  5411. (if org-hide-block-startup (org-hide-block-all))
  5412. (org-set-visibility-according-to-property 'no-cleanup)
  5413. (org-cycle-hide-archived-subtrees 'all)
  5414. (org-cycle-hide-drawers 'all)
  5415. (org-cycle-show-empty-lines t)))
  5416. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5417. "Switch subtree visibilities according to :VISIBILITY: property."
  5418. (interactive)
  5419. (let (org-show-entry-below state)
  5420. (save-excursion
  5421. (goto-char (point-min))
  5422. (while (re-search-forward
  5423. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5424. nil t)
  5425. (setq state (match-string 1))
  5426. (save-excursion
  5427. (org-back-to-heading t)
  5428. (hide-subtree)
  5429. (org-reveal)
  5430. (cond
  5431. ((equal state '("fold" "folded"))
  5432. (hide-subtree))
  5433. ((equal state "children")
  5434. (org-show-hidden-entry)
  5435. (show-children))
  5436. ((equal state "content")
  5437. (save-excursion
  5438. (save-restriction
  5439. (org-narrow-to-subtree)
  5440. (org-content))))
  5441. ((member state '("all" "showall"))
  5442. (show-subtree)))))
  5443. (unless no-cleanup
  5444. (org-cycle-hide-archived-subtrees 'all)
  5445. (org-cycle-hide-drawers 'all)
  5446. (org-cycle-show-empty-lines 'all)))))
  5447. (defun org-overview ()
  5448. "Switch to overview mode, showing only top-level headlines.
  5449. Really, this shows all headlines with level equal or greater than the level
  5450. of the first headline in the buffer. This is important, because if the
  5451. first headline is not level one, then (hide-sublevels 1) gives confusing
  5452. results."
  5453. (interactive)
  5454. (let ((level (save-excursion
  5455. (goto-char (point-min))
  5456. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5457. (progn
  5458. (goto-char (match-beginning 0))
  5459. (funcall outline-level))))))
  5460. (and level (hide-sublevels level))))
  5461. (defun org-content (&optional arg)
  5462. "Show all headlines in the buffer, like a table of contents.
  5463. With numerical argument N, show content up to level N."
  5464. (interactive "P")
  5465. (save-excursion
  5466. ;; Visit all headings and show their offspring
  5467. (and (integerp arg) (org-overview))
  5468. (goto-char (point-max))
  5469. (catch 'exit
  5470. (while (and (progn (condition-case nil
  5471. (outline-previous-visible-heading 1)
  5472. (error (goto-char (point-min))))
  5473. t)
  5474. (looking-at outline-regexp))
  5475. (if (integerp arg)
  5476. (show-children (1- arg))
  5477. (show-branches))
  5478. (if (bobp) (throw 'exit nil))))))
  5479. (defun org-optimize-window-after-visibility-change (state)
  5480. "Adjust the window after a change in outline visibility.
  5481. This function is the default value of the hook `org-cycle-hook'."
  5482. (when (get-buffer-window (current-buffer))
  5483. (cond
  5484. ((eq state 'content) nil)
  5485. ((eq state 'all) nil)
  5486. ((eq state 'folded) nil)
  5487. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5488. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5489. (defun org-remove-empty-overlays-at (pos)
  5490. "Remove outline overlays that do not contain non-white stuff."
  5491. (mapc
  5492. (lambda (o)
  5493. (and (eq 'outline (overlay-get o 'invisible))
  5494. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5495. (overlay-end o))))
  5496. (delete-overlay o)))
  5497. (overlays-at pos)))
  5498. (defun org-clean-visibility-after-subtree-move ()
  5499. "Fix visibility issues after moving a subtree."
  5500. ;; First, find a reasonable region to look at:
  5501. ;; Start two siblings above, end three below
  5502. (let* ((beg (save-excursion
  5503. (and (org-get-last-sibling)
  5504. (org-get-last-sibling))
  5505. (point)))
  5506. (end (save-excursion
  5507. (and (org-get-next-sibling)
  5508. (org-get-next-sibling)
  5509. (org-get-next-sibling))
  5510. (if (org-at-heading-p)
  5511. (point-at-eol)
  5512. (point))))
  5513. (level (looking-at "\\*+"))
  5514. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5515. (save-excursion
  5516. (save-restriction
  5517. (narrow-to-region beg end)
  5518. (when re
  5519. ;; Properly fold already folded siblings
  5520. (goto-char (point-min))
  5521. (while (re-search-forward re nil t)
  5522. (if (and (not (org-invisible-p))
  5523. (save-excursion
  5524. (goto-char (point-at-eol)) (org-invisible-p)))
  5525. (hide-entry))))
  5526. (org-cycle-show-empty-lines 'overview)
  5527. (org-cycle-hide-drawers 'overview)))))
  5528. (defun org-cycle-show-empty-lines (state)
  5529. "Show empty lines above all visible headlines.
  5530. The region to be covered depends on STATE when called through
  5531. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5532. entire buffer covered. Note that an empty line is only shown if there
  5533. are at least `org-cycle-separator-lines' empty lines before the headline."
  5534. (when (not (= org-cycle-separator-lines 0))
  5535. (save-excursion
  5536. (let* ((n (abs org-cycle-separator-lines))
  5537. (re (cond
  5538. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5539. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5540. (t (let ((ns (number-to-string (- n 2))))
  5541. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5542. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5543. beg end b e)
  5544. (cond
  5545. ((memq state '(overview contents t))
  5546. (setq beg (point-min) end (point-max)))
  5547. ((memq state '(children folded))
  5548. (setq beg (point) end (progn (org-end-of-subtree t t)
  5549. (beginning-of-line 2)
  5550. (point)))))
  5551. (when beg
  5552. (goto-char beg)
  5553. (while (re-search-forward re end t)
  5554. (unless (get-char-property (match-end 1) 'invisible)
  5555. (setq e (match-end 1))
  5556. (if (< org-cycle-separator-lines 0)
  5557. (setq b (save-excursion
  5558. (goto-char (match-beginning 0))
  5559. (org-back-over-empty-lines)
  5560. (if (save-excursion
  5561. (goto-char (max (point-min) (1- (point))))
  5562. (org-on-heading-p))
  5563. (1- (point))
  5564. (point))))
  5565. (setq b (match-beginning 1)))
  5566. (outline-flag-region b e nil)))))))
  5567. ;; Never hide empty lines at the end of the file.
  5568. (save-excursion
  5569. (goto-char (point-max))
  5570. (outline-previous-heading)
  5571. (outline-end-of-heading)
  5572. (if (and (looking-at "[ \t\n]+")
  5573. (= (match-end 0) (point-max)))
  5574. (outline-flag-region (point) (match-end 0) nil))))
  5575. (defun org-show-empty-lines-in-parent ()
  5576. "Move to the parent and re-show empty lines before visible headlines."
  5577. (save-excursion
  5578. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5579. (org-cycle-show-empty-lines context))))
  5580. (defun org-files-list ()
  5581. "Return `org-agenda-files' list, plus all open org-mode files.
  5582. This is useful for operations that need to scan all of a user's
  5583. open and agenda-wise Org files."
  5584. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5585. (dolist (buf (buffer-list))
  5586. (with-current-buffer buf
  5587. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5588. (let ((file (expand-file-name (buffer-file-name))))
  5589. (unless (member file files)
  5590. (push file files))))))
  5591. files))
  5592. (defsubst org-entry-beginning-position ()
  5593. "Return the beginning position of the current entry."
  5594. (save-excursion (outline-back-to-heading t) (point)))
  5595. (defsubst org-entry-end-position ()
  5596. "Return the end position of the current entry."
  5597. (save-excursion (outline-next-heading) (point)))
  5598. (defun org-cycle-hide-drawers (state)
  5599. "Re-hide all drawers after a visibility state change."
  5600. (when (and (org-mode-p)
  5601. (not (memq state '(overview folded contents))))
  5602. (save-excursion
  5603. (let* ((globalp (memq state '(contents all)))
  5604. (beg (if globalp (point-min) (point)))
  5605. (end (if globalp (point-max)
  5606. (if (eq state 'children)
  5607. (save-excursion (outline-next-heading) (point))
  5608. (org-end-of-subtree t)))))
  5609. (goto-char beg)
  5610. (while (re-search-forward org-drawer-regexp end t)
  5611. (org-flag-drawer t))))))
  5612. (defun org-flag-drawer (flag)
  5613. (save-excursion
  5614. (beginning-of-line 1)
  5615. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5616. (let ((b (match-end 0))
  5617. (outline-regexp org-outline-regexp))
  5618. (if (re-search-forward
  5619. "^[ \t]*:END:"
  5620. (save-excursion (outline-next-heading) (point)) t)
  5621. (outline-flag-region b (point-at-eol) flag)
  5622. (error ":END: line missing at position %s" b))))))
  5623. (defun org-subtree-end-visible-p ()
  5624. "Is the end of the current subtree visible?"
  5625. (pos-visible-in-window-p
  5626. (save-excursion (org-end-of-subtree t) (point))))
  5627. (defun org-first-headline-recenter (&optional N)
  5628. "Move cursor to the first headline and recenter the headline.
  5629. Optional argument N means put the headline into the Nth line of the window."
  5630. (goto-char (point-min))
  5631. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5632. (beginning-of-line)
  5633. (recenter (prefix-numeric-value N))))
  5634. ;;; Saving and restoring visibility
  5635. (defun org-outline-overlay-data (&optional use-markers)
  5636. "Return a list of the locations of all outline overlays.
  5637. These are overlays with the `invisible' property value `outline'.
  5638. The return value is a list of cons cells, with start and stop
  5639. positions for each overlay.
  5640. If USE-MARKERS is set, return the positions as markers."
  5641. (let (beg end)
  5642. (save-excursion
  5643. (save-restriction
  5644. (widen)
  5645. (delq nil
  5646. (mapcar (lambda (o)
  5647. (when (eq (overlay-get o 'invisible) 'outline)
  5648. (setq beg (overlay-start o)
  5649. end (overlay-end o))
  5650. (and beg end (> end beg)
  5651. (if use-markers
  5652. (cons (move-marker (make-marker) beg)
  5653. (move-marker (make-marker) end))
  5654. (cons beg end)))))
  5655. (overlays-in (point-min) (point-max))))))))
  5656. (defun org-set-outline-overlay-data (data)
  5657. "Create visibility overlays for all positions in DATA.
  5658. DATA should have been made by `org-outline-overlay-data'."
  5659. (let (o)
  5660. (save-excursion
  5661. (save-restriction
  5662. (widen)
  5663. (show-all)
  5664. (mapc (lambda (c)
  5665. (setq o (make-overlay (car c) (cdr c)))
  5666. (overlay-put o 'invisible 'outline))
  5667. data)))))
  5668. ;;; Folding of blocks
  5669. (defconst org-block-regexp
  5670. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5671. "Regular expression for hiding blocks.")
  5672. (defvar org-hide-block-overlays nil
  5673. "Overlays hiding blocks.")
  5674. (make-variable-buffer-local 'org-hide-block-overlays)
  5675. (defun org-block-map (function &optional start end)
  5676. "Call FUNCTION at the head of all source blocks in the current buffer.
  5677. Optional arguments START and END can be used to limit the range."
  5678. (let ((start (or start (point-min)))
  5679. (end (or end (point-max))))
  5680. (save-excursion
  5681. (goto-char start)
  5682. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5683. (save-excursion
  5684. (save-match-data
  5685. (goto-char (match-beginning 0))
  5686. (funcall function)))))))
  5687. (defun org-hide-block-toggle-all ()
  5688. "Toggle the visibility of all blocks in the current buffer."
  5689. (org-block-map #'org-hide-block-toggle))
  5690. (defun org-hide-block-all ()
  5691. "Fold all blocks in the current buffer."
  5692. (interactive)
  5693. (org-show-block-all)
  5694. (org-block-map #'org-hide-block-toggle-maybe))
  5695. (defun org-show-block-all ()
  5696. "Unfold all blocks in the current buffer."
  5697. (interactive)
  5698. (mapc 'delete-overlay org-hide-block-overlays)
  5699. (setq org-hide-block-overlays nil))
  5700. (defun org-hide-block-toggle-maybe ()
  5701. "Toggle visibility of block at point."
  5702. (interactive)
  5703. (let ((case-fold-search t))
  5704. (if (save-excursion
  5705. (beginning-of-line 1)
  5706. (looking-at org-block-regexp))
  5707. (progn (org-hide-block-toggle)
  5708. t) ;; to signal that we took action
  5709. nil))) ;; to signal that we did not
  5710. (defun org-hide-block-toggle (&optional force)
  5711. "Toggle the visibility of the current block."
  5712. (interactive)
  5713. (save-excursion
  5714. (beginning-of-line)
  5715. (if (re-search-forward org-block-regexp nil t)
  5716. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5717. (end (match-end 0)) ;; end of entire body
  5718. ov)
  5719. (if (memq t (mapcar (lambda (overlay)
  5720. (eq (overlay-get overlay 'invisible)
  5721. 'org-hide-block))
  5722. (overlays-at start)))
  5723. (if (or (not force) (eq force 'off))
  5724. (mapc (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. (overlays-at start)))
  5732. (setq ov (make-overlay start end))
  5733. (overlay-put ov 'invisible 'org-hide-block)
  5734. ;; make the block accessible to isearch
  5735. (overlay-put
  5736. ov 'isearch-open-invisible
  5737. (lambda (ov)
  5738. (when (member ov org-hide-block-overlays)
  5739. (setq org-hide-block-overlays
  5740. (delq ov org-hide-block-overlays)))
  5741. (when (eq (overlay-get ov 'invisible)
  5742. 'org-hide-block)
  5743. (delete-overlay ov))))
  5744. (push ov org-hide-block-overlays)))
  5745. (error "Not looking at a source block"))))
  5746. ;; org-tab-after-check-for-cycling-hook
  5747. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5748. ;; Remove overlays when changing major mode
  5749. (add-hook 'org-mode-hook
  5750. (lambda () (org-add-hook 'change-major-mode-hook
  5751. 'org-show-block-all 'append 'local)))
  5752. ;;; Org-goto
  5753. (defvar org-goto-window-configuration nil)
  5754. (defvar org-goto-marker nil)
  5755. (defvar org-goto-map
  5756. (let ((map (make-sparse-keymap)))
  5757. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5758. (while (setq cmd (pop cmds))
  5759. (substitute-key-definition cmd cmd map global-map)))
  5760. (suppress-keymap map)
  5761. (org-defkey map "\C-m" 'org-goto-ret)
  5762. (org-defkey map [(return)] 'org-goto-ret)
  5763. (org-defkey map [(left)] 'org-goto-left)
  5764. (org-defkey map [(right)] 'org-goto-right)
  5765. (org-defkey map [(control ?g)] 'org-goto-quit)
  5766. (org-defkey map "\C-i" 'org-cycle)
  5767. (org-defkey map [(tab)] 'org-cycle)
  5768. (org-defkey map [(down)] 'outline-next-visible-heading)
  5769. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5770. (if org-goto-auto-isearch
  5771. (if (fboundp 'define-key-after)
  5772. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5773. nil)
  5774. (org-defkey map "q" 'org-goto-quit)
  5775. (org-defkey map "n" 'outline-next-visible-heading)
  5776. (org-defkey map "p" 'outline-previous-visible-heading)
  5777. (org-defkey map "f" 'outline-forward-same-level)
  5778. (org-defkey map "b" 'outline-backward-same-level)
  5779. (org-defkey map "u" 'outline-up-heading))
  5780. (org-defkey map "/" 'org-occur)
  5781. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5782. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5783. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5784. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5785. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5786. map))
  5787. (defconst org-goto-help
  5788. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5789. RET=jump to location [Q]uit and return to previous location
  5790. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5791. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5792. ;; FIXME: Docstring does not mention both interfaces
  5793. (defun org-goto (&optional alternative-interface)
  5794. "Look up a different location in the current file, keeping current visibility.
  5795. When you want look-up or go to a different location in a document, the
  5796. fastest way is often to fold the entire buffer and then dive into the tree.
  5797. This method has the disadvantage, that the previous location will be folded,
  5798. which may not be what you want.
  5799. This command works around this by showing a copy of the current buffer
  5800. in an indirect buffer, in overview mode. You can dive into the tree in
  5801. that copy, use org-occur and incremental search to find a location.
  5802. When pressing RET or `Q', the command returns to the original buffer in
  5803. which the visibility is still unchanged. After RET is will also jump to
  5804. the location selected in the indirect buffer and expose the
  5805. the headline hierarchy above."
  5806. (interactive "P")
  5807. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5808. (org-refile-use-outline-path t)
  5809. (org-refile-target-verify-function nil)
  5810. (interface
  5811. (if (not alternative-interface)
  5812. org-goto-interface
  5813. (if (eq org-goto-interface 'outline)
  5814. 'outline-path-completion
  5815. 'outline)))
  5816. (org-goto-start-pos (point))
  5817. (selected-point
  5818. (if (eq interface 'outline)
  5819. (car (org-get-location (current-buffer) org-goto-help))
  5820. (let ((pa (org-refile-get-location "Goto: ")))
  5821. (org-refile-check-position pa)
  5822. (nth 3 pa)))))
  5823. (if selected-point
  5824. (progn
  5825. (org-mark-ring-push org-goto-start-pos)
  5826. (goto-char selected-point)
  5827. (if (or (org-invisible-p) (org-invisible-p2))
  5828. (org-show-context 'org-goto)))
  5829. (message "Quit"))))
  5830. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5831. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5832. (defvar org-goto-local-auto-isearch-map) ; defined below
  5833. (defun org-get-location (buf help)
  5834. "Let the user select a location in the Org-mode buffer BUF.
  5835. This function uses a recursive edit. It returns the selected position
  5836. or nil."
  5837. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5838. (isearch-hide-immediately nil)
  5839. (isearch-search-fun-function
  5840. (lambda () 'org-goto-local-search-headings))
  5841. (org-goto-selected-point org-goto-exit-command)
  5842. (pop-up-frames nil)
  5843. (special-display-buffer-names nil)
  5844. (special-display-regexps nil)
  5845. (special-display-function nil))
  5846. (save-excursion
  5847. (save-window-excursion
  5848. (delete-other-windows)
  5849. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5850. (switch-to-buffer
  5851. (condition-case nil
  5852. (make-indirect-buffer (current-buffer) "*org-goto*")
  5853. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5854. (with-output-to-temp-buffer "*Help*"
  5855. (princ help))
  5856. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5857. (setq buffer-read-only nil)
  5858. (let ((org-startup-truncated t)
  5859. (org-startup-folded nil)
  5860. (org-startup-align-all-tables nil))
  5861. (org-mode)
  5862. (org-overview))
  5863. (setq buffer-read-only t)
  5864. (if (and (boundp 'org-goto-start-pos)
  5865. (integer-or-marker-p org-goto-start-pos))
  5866. (let ((org-show-hierarchy-above t)
  5867. (org-show-siblings t)
  5868. (org-show-following-heading t))
  5869. (goto-char org-goto-start-pos)
  5870. (and (org-invisible-p) (org-show-context)))
  5871. (goto-char (point-min)))
  5872. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5873. (message "Select location and press RET")
  5874. (use-local-map org-goto-map)
  5875. (recursive-edit)
  5876. ))
  5877. (kill-buffer "*org-goto*")
  5878. (cons org-goto-selected-point org-goto-exit-command)))
  5879. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5880. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5881. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5882. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5883. (defun org-goto-local-search-headings (string bound noerror)
  5884. "Search and make sure that any matches are in headlines."
  5885. (catch 'return
  5886. (while (if isearch-forward
  5887. (search-forward string bound noerror)
  5888. (search-backward string bound noerror))
  5889. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5890. (and (member :headline context)
  5891. (not (member :tags context))))
  5892. (throw 'return (point))))))
  5893. (defun org-goto-local-auto-isearch ()
  5894. "Start isearch."
  5895. (interactive)
  5896. (goto-char (point-min))
  5897. (let ((keys (this-command-keys)))
  5898. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5899. (isearch-mode t)
  5900. (isearch-process-search-char (string-to-char keys)))))
  5901. (defun org-goto-ret (&optional arg)
  5902. "Finish `org-goto' by going to the new location."
  5903. (interactive "P")
  5904. (setq org-goto-selected-point (point)
  5905. org-goto-exit-command 'return)
  5906. (throw 'exit nil))
  5907. (defun org-goto-left ()
  5908. "Finish `org-goto' by going to the new location."
  5909. (interactive)
  5910. (if (org-on-heading-p)
  5911. (progn
  5912. (beginning-of-line 1)
  5913. (setq org-goto-selected-point (point)
  5914. org-goto-exit-command 'left)
  5915. (throw 'exit nil))
  5916. (error "Not on a heading")))
  5917. (defun org-goto-right ()
  5918. "Finish `org-goto' by going to the new location."
  5919. (interactive)
  5920. (if (org-on-heading-p)
  5921. (progn
  5922. (setq org-goto-selected-point (point)
  5923. org-goto-exit-command 'right)
  5924. (throw 'exit nil))
  5925. (error "Not on a heading")))
  5926. (defun org-goto-quit ()
  5927. "Finish `org-goto' without cursor motion."
  5928. (interactive)
  5929. (setq org-goto-selected-point nil)
  5930. (setq org-goto-exit-command 'quit)
  5931. (throw 'exit nil))
  5932. ;;; Indirect buffer display of subtrees
  5933. (defvar org-indirect-dedicated-frame nil
  5934. "This is the frame being used for indirect tree display.")
  5935. (defvar org-last-indirect-buffer nil)
  5936. (defun org-tree-to-indirect-buffer (&optional arg)
  5937. "Create indirect buffer and narrow it to current subtree.
  5938. With numerical prefix ARG, go up to this level and then take that tree.
  5939. If ARG is negative, go up that many levels.
  5940. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5941. indirect buffer previously made with this command, to avoid proliferation of
  5942. indirect buffers. However, when you call the command with a \
  5943. \\[universal-argument] prefix, or
  5944. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5945. is kept so that you can work with several indirect buffers at the same time.
  5946. If `org-indirect-buffer-display' is `dedicated-frame', the \
  5947. \\[universal-argument] prefix also
  5948. requests that a new frame be made for the new buffer, so that the dedicated
  5949. frame is not changed."
  5950. (interactive "P")
  5951. (let ((cbuf (current-buffer))
  5952. (cwin (selected-window))
  5953. (pos (point))
  5954. beg end level heading ibuf)
  5955. (save-excursion
  5956. (org-back-to-heading t)
  5957. (when (numberp arg)
  5958. (setq level (org-outline-level))
  5959. (if (< arg 0) (setq arg (+ level arg)))
  5960. (while (> (setq level (org-outline-level)) arg)
  5961. (outline-up-heading 1 t)))
  5962. (setq beg (point)
  5963. heading (org-get-heading))
  5964. (org-end-of-subtree t t)
  5965. (if (org-on-heading-p) (backward-char 1))
  5966. (setq end (point)))
  5967. (if (and (buffer-live-p org-last-indirect-buffer)
  5968. (not (eq org-indirect-buffer-display 'new-frame))
  5969. (not arg))
  5970. (kill-buffer org-last-indirect-buffer))
  5971. (setq ibuf (org-get-indirect-buffer cbuf)
  5972. org-last-indirect-buffer ibuf)
  5973. (cond
  5974. ((or (eq org-indirect-buffer-display 'new-frame)
  5975. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5976. (select-frame (make-frame))
  5977. (delete-other-windows)
  5978. (switch-to-buffer ibuf)
  5979. (org-set-frame-title heading))
  5980. ((eq org-indirect-buffer-display 'dedicated-frame)
  5981. (raise-frame
  5982. (select-frame (or (and org-indirect-dedicated-frame
  5983. (frame-live-p org-indirect-dedicated-frame)
  5984. org-indirect-dedicated-frame)
  5985. (setq org-indirect-dedicated-frame (make-frame)))))
  5986. (delete-other-windows)
  5987. (switch-to-buffer ibuf)
  5988. (org-set-frame-title (concat "Indirect: " heading)))
  5989. ((eq org-indirect-buffer-display 'current-window)
  5990. (switch-to-buffer ibuf))
  5991. ((eq org-indirect-buffer-display 'other-window)
  5992. (pop-to-buffer ibuf))
  5993. (t (error "Invalid value")))
  5994. (if (featurep 'xemacs)
  5995. (save-excursion (org-mode) (turn-on-font-lock)))
  5996. (narrow-to-region beg end)
  5997. (show-all)
  5998. (goto-char pos)
  5999. (and (window-live-p cwin) (select-window cwin))))
  6000. (defun org-get-indirect-buffer (&optional buffer)
  6001. (setq buffer (or buffer (current-buffer)))
  6002. (let ((n 1) (base (buffer-name buffer)) bname)
  6003. (while (buffer-live-p
  6004. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6005. (setq n (1+ n)))
  6006. (condition-case nil
  6007. (make-indirect-buffer buffer bname 'clone)
  6008. (error (make-indirect-buffer buffer bname)))))
  6009. (defun org-set-frame-title (title)
  6010. "Set the title of the current frame to the string TITLE."
  6011. ;; FIXME: how to name a single frame in XEmacs???
  6012. (unless (featurep 'xemacs)
  6013. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6014. ;;;; Structure editing
  6015. ;;; Inserting headlines
  6016. (defun org-previous-line-empty-p ()
  6017. (save-excursion
  6018. (and (not (bobp))
  6019. (or (beginning-of-line 0) t)
  6020. (save-match-data
  6021. (looking-at "[ \t]*$")))))
  6022. (defun org-insert-heading (&optional force-heading invisible-ok)
  6023. "Insert a new heading or item with same depth at point.
  6024. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6025. If point is at the beginning of a headline, insert a sibling before the
  6026. current headline. If point is not at the beginning, split the line,
  6027. create the new headline with the text in the current line after point
  6028. \(but see also the variable `org-M-RET-may-split-line').
  6029. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6030. This is important for non-interactive uses of the command."
  6031. (interactive "P")
  6032. (if (or (= (buffer-size) 0)
  6033. (and (not (save-excursion
  6034. (and (ignore-errors (org-back-to-heading invisible-ok))
  6035. (org-on-heading-p))))
  6036. (not (org-in-item-p))))
  6037. (progn
  6038. (insert "\n* ")
  6039. (run-hooks 'org-insert-heading-hook))
  6040. (when (or force-heading (not (org-insert-item)))
  6041. (let* ((empty-line-p nil)
  6042. (level nil)
  6043. (on-heading (org-on-heading-p))
  6044. (head (save-excursion
  6045. (condition-case nil
  6046. (progn
  6047. (org-back-to-heading invisible-ok)
  6048. (when (and (not on-heading)
  6049. (featurep 'org-inlinetask)
  6050. (integerp org-inlinetask-min-level)
  6051. (>= (length (match-string 0))
  6052. org-inlinetask-min-level))
  6053. ;; Find a heading level before the inline task
  6054. (while (and (setq level (org-up-heading-safe))
  6055. (>= level org-inlinetask-min-level)))
  6056. (if (org-on-heading-p)
  6057. (org-back-to-heading invisible-ok)
  6058. (error "This should not happen")))
  6059. (setq empty-line-p (org-previous-line-empty-p))
  6060. (match-string 0))
  6061. (error "*"))))
  6062. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6063. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6064. pos hide-previous previous-pos)
  6065. (cond
  6066. ((and (org-on-heading-p) (bolp)
  6067. (or (bobp)
  6068. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  6069. ;; insert before the current line
  6070. (open-line (if blank 2 1)))
  6071. ((and (bolp)
  6072. (not org-insert-heading-respect-content)
  6073. (or (bobp)
  6074. (save-excursion
  6075. (backward-char 1) (not (org-invisible-p)))))
  6076. ;; insert right here
  6077. nil)
  6078. (t
  6079. ;; somewhere in the line
  6080. (save-excursion
  6081. (setq previous-pos (point-at-bol))
  6082. (end-of-line)
  6083. (setq hide-previous (org-invisible-p)))
  6084. (and org-insert-heading-respect-content (org-show-subtree))
  6085. (let ((split
  6086. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6087. (save-excursion
  6088. (let ((p (point)))
  6089. (goto-char (point-at-bol))
  6090. (and (looking-at org-complex-heading-regexp)
  6091. (> p (match-beginning 4)))))))
  6092. tags pos)
  6093. (cond
  6094. (org-insert-heading-respect-content
  6095. (org-end-of-subtree nil t)
  6096. (when (featurep 'org-inlinetask)
  6097. (while (and (not (eobp))
  6098. (looking-at "\\(\\*+\\)[ \t]+")
  6099. (>= (length (match-string 1))
  6100. org-inlinetask-min-level))
  6101. (org-end-of-subtree nil t)))
  6102. (or (bolp) (newline))
  6103. (or (org-previous-line-empty-p)
  6104. (and blank (newline)))
  6105. (open-line 1))
  6106. ((org-on-heading-p)
  6107. (when hide-previous
  6108. (show-children)
  6109. (org-show-entry))
  6110. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6111. (setq tags (and (match-end 2) (match-string 2)))
  6112. (and (match-end 1)
  6113. (delete-region (match-beginning 1) (match-end 1)))
  6114. (setq pos (point-at-bol))
  6115. (or split (end-of-line 1))
  6116. (delete-horizontal-space)
  6117. (if (string-match "\\`\\*+\\'"
  6118. (buffer-substring (point-at-bol) (point)))
  6119. (insert " "))
  6120. (newline (if blank 2 1))
  6121. (when tags
  6122. (save-excursion
  6123. (goto-char pos)
  6124. (end-of-line 1)
  6125. (insert " " tags)
  6126. (org-set-tags nil 'align))))
  6127. (t
  6128. (or split (end-of-line 1))
  6129. (newline (if blank 2 1)))))))
  6130. (insert head) (just-one-space)
  6131. (setq pos (point))
  6132. (end-of-line 1)
  6133. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6134. (when (and org-insert-heading-respect-content hide-previous)
  6135. (save-excursion
  6136. (goto-char previous-pos)
  6137. (hide-subtree)))
  6138. (run-hooks 'org-insert-heading-hook)))))
  6139. (defun org-get-heading (&optional no-tags)
  6140. "Return the heading of the current entry, without the stars."
  6141. (save-excursion
  6142. (org-back-to-heading t)
  6143. (if (looking-at
  6144. (if no-tags
  6145. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6146. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6147. (match-string 1) "")))
  6148. (defun org-heading-components ()
  6149. "Return the components of the current heading.
  6150. This is a list with the following elements:
  6151. - the level as an integer
  6152. - the reduced level, different if `org-odd-levels-only' is set.
  6153. - the TODO keyword, or nil
  6154. - the priority character, like ?A, or nil if no priority is given
  6155. - the headline text itself, or the tags string if no headline text
  6156. - the tags string, or nil."
  6157. (save-excursion
  6158. (org-back-to-heading t)
  6159. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6160. (list (length (match-string 1))
  6161. (org-reduced-level (length (match-string 1)))
  6162. (org-match-string-no-properties 2)
  6163. (and (match-end 3) (aref (match-string 3) 2))
  6164. (org-match-string-no-properties 4)
  6165. (org-match-string-no-properties 5)))))
  6166. (defun org-get-entry ()
  6167. "Get the entry text, after heading, entire subtree."
  6168. (save-excursion
  6169. (org-back-to-heading t)
  6170. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6171. (defun org-insert-heading-after-current ()
  6172. "Insert a new heading with same level as current, after current subtree."
  6173. (interactive)
  6174. (org-back-to-heading)
  6175. (org-insert-heading)
  6176. (org-move-subtree-down)
  6177. (end-of-line 1))
  6178. (defun org-insert-heading-respect-content ()
  6179. (interactive)
  6180. (let ((org-insert-heading-respect-content t))
  6181. (org-insert-heading t)))
  6182. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6183. (interactive "P")
  6184. (let ((org-insert-heading-respect-content t))
  6185. (org-insert-todo-heading force-state t)))
  6186. (defun org-insert-todo-heading (arg &optional force-heading)
  6187. "Insert a new heading with the same level and TODO state as current heading.
  6188. If the heading has no TODO state, or if the state is DONE, use the first
  6189. state (TODO by default). Also with prefix arg, force first state."
  6190. (interactive "P")
  6191. (when (or force-heading (not (org-insert-item 'checkbox)))
  6192. (org-insert-heading force-heading)
  6193. (save-excursion
  6194. (org-back-to-heading)
  6195. (outline-previous-heading)
  6196. (looking-at org-todo-line-regexp))
  6197. (let*
  6198. ((new-mark-x
  6199. (if (or arg
  6200. (not (match-beginning 2))
  6201. (member (match-string 2) org-done-keywords))
  6202. (car org-todo-keywords-1)
  6203. (match-string 2)))
  6204. (new-mark
  6205. (or
  6206. (run-hook-with-args-until-success
  6207. 'org-todo-get-default-hook new-mark-x nil)
  6208. new-mark-x)))
  6209. (beginning-of-line 1)
  6210. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6211. (if org-treat-insert-todo-heading-as-state-change
  6212. (org-todo new-mark)
  6213. (insert new-mark " "))))
  6214. (when org-provide-todo-statistics
  6215. (org-update-parent-todo-statistics))))
  6216. (defun org-insert-subheading (arg)
  6217. "Insert a new subheading and demote it.
  6218. Works for outline headings and for plain lists alike."
  6219. (interactive "P")
  6220. (org-insert-heading arg)
  6221. (cond
  6222. ((org-on-heading-p) (org-do-demote))
  6223. ((org-at-item-p) (org-indent-item))))
  6224. (defun org-insert-todo-subheading (arg)
  6225. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6226. Works for outline headings and for plain lists alike."
  6227. (interactive "P")
  6228. (org-insert-todo-heading arg)
  6229. (cond
  6230. ((org-on-heading-p) (org-do-demote))
  6231. ((org-at-item-p) (org-indent-item))))
  6232. ;;; Promotion and Demotion
  6233. (defvar org-after-demote-entry-hook nil
  6234. "Hook run after an entry has been demoted.
  6235. The cursor will be at the beginning of the entry.
  6236. When a subtree is being demoted, the hook will be called for each node.")
  6237. (defvar org-after-promote-entry-hook nil
  6238. "Hook run after an entry has been promoted.
  6239. The cursor will be at the beginning of the entry.
  6240. When a subtree is being promoted, the hook will be called for each node.")
  6241. (defun org-promote-subtree ()
  6242. "Promote the entire subtree.
  6243. See also `org-promote'."
  6244. (interactive)
  6245. (save-excursion
  6246. (org-map-tree 'org-promote))
  6247. (org-fix-position-after-promote))
  6248. (defun org-demote-subtree ()
  6249. "Demote the entire subtree. See `org-demote'.
  6250. See also `org-promote'."
  6251. (interactive)
  6252. (save-excursion
  6253. (org-map-tree 'org-demote))
  6254. (org-fix-position-after-promote))
  6255. (defun org-do-promote ()
  6256. "Promote the current heading higher up the tree.
  6257. If the region is active in `transient-mark-mode', promote all headings
  6258. in the region."
  6259. (interactive)
  6260. (save-excursion
  6261. (if (org-region-active-p)
  6262. (org-map-region 'org-promote (region-beginning) (region-end))
  6263. (org-promote)))
  6264. (org-fix-position-after-promote))
  6265. (defun org-do-demote ()
  6266. "Demote the current heading lower down the tree.
  6267. If the region is active in `transient-mark-mode', demote all headings
  6268. in the region."
  6269. (interactive)
  6270. (save-excursion
  6271. (if (org-region-active-p)
  6272. (org-map-region 'org-demote (region-beginning) (region-end))
  6273. (org-demote)))
  6274. (org-fix-position-after-promote))
  6275. (defun org-fix-position-after-promote ()
  6276. "Make sure that after pro/demotion cursor position is right."
  6277. (let ((pos (point)))
  6278. (when (save-excursion
  6279. (beginning-of-line 1)
  6280. (looking-at org-todo-line-regexp)
  6281. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6282. (cond ((eobp) (insert " "))
  6283. ((eolp) (insert " "))
  6284. ((equal (char-after) ?\ ) (forward-char 1))))))
  6285. (defun org-current-level ()
  6286. "Return the level of the current entry, or nil if before the first headline.
  6287. The level is the number of stars at the beginning of the headline."
  6288. (save-excursion
  6289. (let ((outline-regexp (org-get-limited-outline-regexp)))
  6290. (condition-case nil
  6291. (progn
  6292. (org-back-to-heading t)
  6293. (funcall outline-level))
  6294. (error nil)))))
  6295. (defun org-get-previous-line-level ()
  6296. "Return the outline depth of the last headline before the current line.
  6297. Returns 0 for the first headline in the buffer, and nil if before the
  6298. first headline."
  6299. (let ((current-level (org-current-level))
  6300. (prev-level (when (> (line-number-at-pos) 1)
  6301. (save-excursion
  6302. (beginning-of-line 0)
  6303. (org-current-level)))))
  6304. (cond ((null current-level) nil) ; Before first headline
  6305. ((null prev-level) 0) ; At first headline
  6306. (prev-level))))
  6307. (defun org-reduced-level (l)
  6308. "Compute the effective level of a heading.
  6309. This takes into account the setting of `org-odd-levels-only'."
  6310. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6311. (defun org-level-increment ()
  6312. "Return the number of stars that will be added or removed at a
  6313. time to headlines when structure editing, based on the value of
  6314. `org-odd-levels-only'."
  6315. (if org-odd-levels-only 2 1))
  6316. (defun org-get-valid-level (level &optional change)
  6317. "Rectify a level change under the influence of `org-odd-levels-only'
  6318. LEVEL is a current level, CHANGE is by how much the level should be
  6319. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6320. even level numbers will become the next higher odd number."
  6321. (if org-odd-levels-only
  6322. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6323. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6324. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6325. (max 1 (+ level (or change 0)))))
  6326. (if (boundp 'define-obsolete-function-alias)
  6327. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6328. (define-obsolete-function-alias 'org-get-legal-level
  6329. 'org-get-valid-level)
  6330. (define-obsolete-function-alias 'org-get-legal-level
  6331. 'org-get-valid-level "23.1")))
  6332. (defun org-promote ()
  6333. "Promote the current heading higher up the tree.
  6334. If the region is active in `transient-mark-mode', promote all headings
  6335. in the region."
  6336. (org-back-to-heading t)
  6337. (let* ((level (save-match-data (funcall outline-level)))
  6338. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6339. (diff (abs (- level (length up-head) -1))))
  6340. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6341. (replace-match up-head nil t)
  6342. ;; Fixup tag positioning
  6343. (and org-auto-align-tags (org-set-tags nil t))
  6344. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6345. (run-hooks 'org-after-promote-entry-hook)))
  6346. (defun org-demote ()
  6347. "Demote the current heading lower down the tree.
  6348. If the region is active in `transient-mark-mode', demote all headings
  6349. in the region."
  6350. (org-back-to-heading t)
  6351. (let* ((level (save-match-data (funcall outline-level)))
  6352. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6353. (diff (abs (- level (length down-head) -1))))
  6354. (replace-match down-head nil t)
  6355. ;; Fixup tag positioning
  6356. (and org-auto-align-tags (org-set-tags nil t))
  6357. (if org-adapt-indentation (org-fixup-indentation diff))
  6358. (run-hooks 'org-after-demote-entry-hook)))
  6359. (defun org-cycle-level ()
  6360. "Cycle the level of an empty headline through possible states.
  6361. This goes first to child, then to parent, level, then up the hierarchy.
  6362. After top level, it switches back to sibling level."
  6363. (interactive)
  6364. (let ((org-adapt-indentation nil))
  6365. (when (org-point-at-end-of-empty-headline)
  6366. (setq this-command 'org-cycle-level) ; Only needed for caching
  6367. (let ((cur-level (org-current-level))
  6368. (prev-level (org-get-previous-line-level)))
  6369. (cond
  6370. ;; If first headline in file, promote to top-level.
  6371. ((= prev-level 0)
  6372. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6373. do (org-do-promote)))
  6374. ;; If same level as prev, demote one.
  6375. ((= prev-level cur-level)
  6376. (org-do-demote))
  6377. ;; If parent is top-level, promote to top level if not already.
  6378. ((= prev-level 1)
  6379. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6380. do (org-do-promote)))
  6381. ;; If top-level, return to prev-level.
  6382. ((= cur-level 1)
  6383. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6384. do (org-do-demote)))
  6385. ;; If less than prev-level, promote one.
  6386. ((< cur-level prev-level)
  6387. (org-do-promote))
  6388. ;; If deeper than prev-level, promote until higher than
  6389. ;; prev-level.
  6390. ((> cur-level prev-level)
  6391. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6392. do (org-do-promote))))
  6393. t))))
  6394. (defun org-map-tree (fun)
  6395. "Call FUN for every heading underneath the current one."
  6396. (org-back-to-heading)
  6397. (let ((level (funcall outline-level)))
  6398. (save-excursion
  6399. (funcall fun)
  6400. (while (and (progn
  6401. (outline-next-heading)
  6402. (> (funcall outline-level) level))
  6403. (not (eobp)))
  6404. (funcall fun)))))
  6405. (defun org-map-region (fun beg end)
  6406. "Call FUN for every heading between BEG and END."
  6407. (let ((org-ignore-region t))
  6408. (save-excursion
  6409. (setq end (copy-marker end))
  6410. (goto-char beg)
  6411. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6412. (< (point) end))
  6413. (funcall fun))
  6414. (while (and (progn
  6415. (outline-next-heading)
  6416. (< (point) end))
  6417. (not (eobp)))
  6418. (funcall fun)))))
  6419. (defun org-fixup-indentation (diff)
  6420. "Change the indentation in the current entry by DIFF.
  6421. However, if any line in the current entry has no indentation, or if it
  6422. would end up with no indentation after the change, nothing at all is done."
  6423. (save-excursion
  6424. (let ((end (save-excursion (outline-next-heading)
  6425. (point-marker)))
  6426. (prohibit (if (> diff 0)
  6427. "^\\S-"
  6428. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6429. col)
  6430. (unless (save-excursion (end-of-line 1)
  6431. (re-search-forward prohibit end t))
  6432. (while (and (< (point) end)
  6433. (re-search-forward "^[ \t]+" end t))
  6434. (goto-char (match-end 0))
  6435. (setq col (current-column))
  6436. (if (< diff 0) (replace-match ""))
  6437. (org-indent-to-column (+ diff col))))
  6438. (move-marker end nil))))
  6439. (defun org-convert-to-odd-levels ()
  6440. "Convert an org-mode file with all levels allowed to one with odd levels.
  6441. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6442. level 5 etc."
  6443. (interactive)
  6444. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6445. (let ((outline-regexp org-outline-regexp)
  6446. (outline-level 'org-outline-level)
  6447. (org-odd-levels-only nil) n)
  6448. (save-excursion
  6449. (goto-char (point-min))
  6450. (while (re-search-forward "^\\*\\*+ " nil t)
  6451. (setq n (- (length (match-string 0)) 2))
  6452. (while (>= (setq n (1- n)) 0)
  6453. (org-demote))
  6454. (end-of-line 1))))))
  6455. (defun org-convert-to-oddeven-levels ()
  6456. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6457. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6458. file contains a section with an even level, conversion would
  6459. destroy the structure of the file. An error is signaled in this
  6460. case."
  6461. (interactive)
  6462. (goto-char (point-min))
  6463. ;; First check if there are no even levels
  6464. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6465. (org-show-context t)
  6466. (error "Not all levels are odd in this file. Conversion not possible"))
  6467. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6468. (let ((outline-regexp org-outline-regexp)
  6469. (outline-level 'org-outline-level)
  6470. (org-odd-levels-only nil) n)
  6471. (save-excursion
  6472. (goto-char (point-min))
  6473. (while (re-search-forward "^\\*\\*+ " nil t)
  6474. (setq n (/ (1- (length (match-string 0))) 2))
  6475. (while (>= (setq n (1- n)) 0)
  6476. (org-promote))
  6477. (end-of-line 1))))))
  6478. (defun org-tr-level (n)
  6479. "Make N odd if required."
  6480. (if org-odd-levels-only (1+ (/ n 2)) n))
  6481. ;;; Vertical tree motion, cutting and pasting of subtrees
  6482. (defun org-move-subtree-up (&optional arg)
  6483. "Move the current subtree up past ARG headlines of the same level."
  6484. (interactive "p")
  6485. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6486. (defun org-move-subtree-down (&optional arg)
  6487. "Move the current subtree down past ARG headlines of the same level."
  6488. (interactive "p")
  6489. (setq arg (prefix-numeric-value arg))
  6490. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6491. 'org-get-last-sibling))
  6492. (ins-point (make-marker))
  6493. (cnt (abs arg))
  6494. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6495. ;; Select the tree
  6496. (org-back-to-heading)
  6497. (setq beg0 (point))
  6498. (save-excursion
  6499. (setq ne-beg (org-back-over-empty-lines))
  6500. (setq beg (point)))
  6501. (save-match-data
  6502. (save-excursion (outline-end-of-heading)
  6503. (setq folded (org-invisible-p)))
  6504. (outline-end-of-subtree))
  6505. (outline-next-heading)
  6506. (setq ne-end (org-back-over-empty-lines))
  6507. (setq end (point))
  6508. (goto-char beg0)
  6509. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6510. ;; include less whitespace
  6511. (save-excursion
  6512. (goto-char beg)
  6513. (forward-line (- ne-beg ne-end))
  6514. (setq beg (point))))
  6515. ;; Find insertion point, with error handling
  6516. (while (> cnt 0)
  6517. (or (and (funcall movfunc) (looking-at outline-regexp))
  6518. (progn (goto-char beg0)
  6519. (error "Cannot move past superior level or buffer limit")))
  6520. (setq cnt (1- cnt)))
  6521. (if (> arg 0)
  6522. ;; Moving forward - still need to move over subtree
  6523. (progn (org-end-of-subtree t t)
  6524. (save-excursion
  6525. (org-back-over-empty-lines)
  6526. (or (bolp) (newline)))))
  6527. (setq ne-ins (org-back-over-empty-lines))
  6528. (move-marker ins-point (point))
  6529. (setq txt (buffer-substring beg end))
  6530. (org-save-markers-in-region beg end)
  6531. (delete-region beg end)
  6532. (org-remove-empty-overlays-at beg)
  6533. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6534. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6535. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6536. (let ((bbb (point)))
  6537. (insert-before-markers txt)
  6538. (org-reinstall-markers-in-region bbb)
  6539. (move-marker ins-point bbb))
  6540. (or (bolp) (insert "\n"))
  6541. (setq ins-end (point))
  6542. (goto-char ins-point)
  6543. (org-skip-whitespace)
  6544. (when (and (< arg 0)
  6545. (org-first-sibling-p)
  6546. (> ne-ins ne-beg))
  6547. ;; Move whitespace back to beginning
  6548. (save-excursion
  6549. (goto-char ins-end)
  6550. (let ((kill-whole-line t))
  6551. (kill-line (- ne-ins ne-beg)) (point)))
  6552. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6553. (move-marker ins-point nil)
  6554. (if folded
  6555. (hide-subtree)
  6556. (org-show-entry)
  6557. (show-children)
  6558. (org-cycle-hide-drawers 'children))
  6559. (org-clean-visibility-after-subtree-move)))
  6560. (defvar org-subtree-clip ""
  6561. "Clipboard for cut and paste of subtrees.
  6562. This is actually only a copy of the kill, because we use the normal kill
  6563. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6564. (defvar org-subtree-clip-folded nil
  6565. "Was the last copied subtree folded?
  6566. This is used to fold the tree back after pasting.")
  6567. (defun org-cut-subtree (&optional n)
  6568. "Cut the current subtree into the clipboard.
  6569. With prefix arg N, cut this many sequential subtrees.
  6570. This is a short-hand for marking the subtree and then cutting it."
  6571. (interactive "p")
  6572. (org-copy-subtree n 'cut))
  6573. (defun org-copy-subtree (&optional n cut force-store-markers)
  6574. "Cut the current subtree into the clipboard.
  6575. With prefix arg N, cut this many sequential subtrees.
  6576. This is a short-hand for marking the subtree and then copying it.
  6577. If CUT is non-nil, actually cut the subtree.
  6578. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6579. of some markers in the region, even if CUT is non-nil. This is
  6580. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6581. (interactive "p")
  6582. (let (beg end folded (beg0 (point)))
  6583. (if (interactive-p)
  6584. (org-back-to-heading nil) ; take what looks like a subtree
  6585. (org-back-to-heading t)) ; take what is really there
  6586. (org-back-over-empty-lines)
  6587. (setq beg (point))
  6588. (skip-chars-forward " \t\r\n")
  6589. (save-match-data
  6590. (save-excursion (outline-end-of-heading)
  6591. (setq folded (org-invisible-p)))
  6592. (condition-case nil
  6593. (org-forward-same-level (1- n) t)
  6594. (error nil))
  6595. (org-end-of-subtree t t))
  6596. (org-back-over-empty-lines)
  6597. (setq end (point))
  6598. (goto-char beg0)
  6599. (when (> end beg)
  6600. (setq org-subtree-clip-folded folded)
  6601. (when (or cut force-store-markers)
  6602. (org-save-markers-in-region beg end))
  6603. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6604. (setq org-subtree-clip (current-kill 0))
  6605. (message "%s: Subtree(s) with %d characters"
  6606. (if cut "Cut" "Copied")
  6607. (length org-subtree-clip)))))
  6608. (defun org-paste-subtree (&optional level tree for-yank)
  6609. "Paste the clipboard as a subtree, with modification of headline level.
  6610. The entire subtree is promoted or demoted in order to match a new headline
  6611. level.
  6612. If the cursor is at the beginning of a headline, the same level as
  6613. that headline is used to paste the tree
  6614. If not, the new level is derived from the *visible* headings
  6615. before and after the insertion point, and taken to be the inferior headline
  6616. level of the two. So if the previous visible heading is level 3 and the
  6617. next is level 4 (or vice versa), level 4 will be used for insertion.
  6618. This makes sure that the subtree remains an independent subtree and does
  6619. not swallow low level entries.
  6620. You can also force a different level, either by using a numeric prefix
  6621. argument, or by inserting the heading marker by hand. For example, if the
  6622. cursor is after \"*****\", then the tree will be shifted to level 5.
  6623. If optional TREE is given, use this text instead of the kill ring.
  6624. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6625. move back over whitespace before inserting, and move point to the end of
  6626. the inserted text when done."
  6627. (interactive "P")
  6628. (setq tree (or tree (and kill-ring (current-kill 0))))
  6629. (unless (org-kill-is-subtree-p tree)
  6630. (error "%s"
  6631. (substitute-command-keys
  6632. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6633. (let* ((visp (not (org-invisible-p)))
  6634. (txt tree)
  6635. (^re (concat "^\\(" outline-regexp "\\)"))
  6636. (re (concat "\\(" outline-regexp "\\)"))
  6637. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6638. (old-level (if (string-match ^re txt)
  6639. (- (match-end 0) (match-beginning 0) 1)
  6640. -1))
  6641. (force-level (cond (level (prefix-numeric-value level))
  6642. ((and (looking-at "[ \t]*$")
  6643. (string-match
  6644. ^re_ (buffer-substring
  6645. (point-at-bol) (point))))
  6646. (- (match-end 1) (match-beginning 1)))
  6647. ((and (bolp)
  6648. (looking-at org-outline-regexp))
  6649. (- (match-end 0) (point) 1))
  6650. (t nil)))
  6651. (previous-level (save-excursion
  6652. (condition-case nil
  6653. (progn
  6654. (outline-previous-visible-heading 1)
  6655. (if (looking-at re)
  6656. (- (match-end 0) (match-beginning 0) 1)
  6657. 1))
  6658. (error 1))))
  6659. (next-level (save-excursion
  6660. (condition-case nil
  6661. (progn
  6662. (or (looking-at outline-regexp)
  6663. (outline-next-visible-heading 1))
  6664. (if (looking-at re)
  6665. (- (match-end 0) (match-beginning 0) 1)
  6666. 1))
  6667. (error 1))))
  6668. (new-level (or force-level (max previous-level next-level)))
  6669. (shift (if (or (= old-level -1)
  6670. (= new-level -1)
  6671. (= old-level new-level))
  6672. 0
  6673. (- new-level old-level)))
  6674. (delta (if (> shift 0) -1 1))
  6675. (func (if (> shift 0) 'org-demote 'org-promote))
  6676. (org-odd-levels-only nil)
  6677. beg end newend)
  6678. ;; Remove the forced level indicator
  6679. (if force-level
  6680. (delete-region (point-at-bol) (point)))
  6681. ;; Paste
  6682. (beginning-of-line 1)
  6683. (unless for-yank (org-back-over-empty-lines))
  6684. (setq beg (point))
  6685. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6686. (insert-before-markers txt)
  6687. (unless (string-match "\n\\'" txt) (insert "\n"))
  6688. (setq newend (point))
  6689. (org-reinstall-markers-in-region beg)
  6690. (setq end (point))
  6691. (goto-char beg)
  6692. (skip-chars-forward " \t\n\r")
  6693. (setq beg (point))
  6694. (if (and (org-invisible-p) visp)
  6695. (save-excursion (outline-show-heading)))
  6696. ;; Shift if necessary
  6697. (unless (= shift 0)
  6698. (save-restriction
  6699. (narrow-to-region beg end)
  6700. (while (not (= shift 0))
  6701. (org-map-region func (point-min) (point-max))
  6702. (setq shift (+ delta shift)))
  6703. (goto-char (point-min))
  6704. (setq newend (point-max))))
  6705. (when (or (interactive-p) for-yank)
  6706. (message "Clipboard pasted as level %d subtree" new-level))
  6707. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6708. kill-ring
  6709. (eq org-subtree-clip (current-kill 0))
  6710. org-subtree-clip-folded)
  6711. ;; The tree was folded before it was killed/copied
  6712. (hide-subtree))
  6713. (and for-yank (goto-char newend))))
  6714. (defun org-kill-is-subtree-p (&optional txt)
  6715. "Check if the current kill is an outline subtree, or a set of trees.
  6716. Returns nil if kill does not start with a headline, or if the first
  6717. headline level is not the largest headline level in the tree.
  6718. So this will actually accept several entries of equal levels as well,
  6719. which is OK for `org-paste-subtree'.
  6720. If optional TXT is given, check this string instead of the current kill."
  6721. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6722. (start-level (and kill
  6723. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6724. org-outline-regexp "\\)")
  6725. kill)
  6726. (- (match-end 2) (match-beginning 2) 1)))
  6727. (re (concat "^" org-outline-regexp))
  6728. (start (1+ (or (match-beginning 2) -1))))
  6729. (if (not start-level)
  6730. (progn
  6731. nil) ;; does not even start with a heading
  6732. (catch 'exit
  6733. (while (setq start (string-match re kill (1+ start)))
  6734. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6735. (throw 'exit nil)))
  6736. t))))
  6737. (defvar org-markers-to-move nil
  6738. "Markers that should be moved with a cut-and-paste operation.
  6739. Those markers are stored together with their positions relative to
  6740. the start of the region.")
  6741. (defun org-save-markers-in-region (beg end)
  6742. "Check markers in region.
  6743. If these markers are between BEG and END, record their position relative
  6744. to BEG, so that after moving the block of text, we can put the markers back
  6745. into place.
  6746. This function gets called just before an entry or tree gets cut from the
  6747. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6748. called immediately, to move the markers with the entries."
  6749. (setq org-markers-to-move nil)
  6750. (when (featurep 'org-clock)
  6751. (org-clock-save-markers-for-cut-and-paste beg end))
  6752. (when (featurep 'org-agenda)
  6753. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6754. (defun org-check-and-save-marker (marker beg end)
  6755. "Check if MARKER is between BEG and END.
  6756. If yes, remember the marker and the distance to BEG."
  6757. (when (and (marker-buffer marker)
  6758. (equal (marker-buffer marker) (current-buffer)))
  6759. (if (and (>= marker beg) (< marker end))
  6760. (push (cons marker (- marker beg)) org-markers-to-move))))
  6761. (defun org-reinstall-markers-in-region (beg)
  6762. "Move all remembered markers to their position relative to BEG."
  6763. (mapc (lambda (x)
  6764. (move-marker (car x) (+ beg (cdr x))))
  6765. org-markers-to-move)
  6766. (setq org-markers-to-move nil))
  6767. (defun org-narrow-to-subtree ()
  6768. "Narrow buffer to the current subtree."
  6769. (interactive)
  6770. (save-excursion
  6771. (save-match-data
  6772. (narrow-to-region
  6773. (progn (org-back-to-heading t) (point))
  6774. (progn (org-end-of-subtree t t)
  6775. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6776. (point))))))
  6777. (eval-when-compile
  6778. (defvar org-property-drawer-re))
  6779. (defvar org-property-start-re) ;; defined below
  6780. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6781. "Clone the task (subtree) at point N times.
  6782. The clones will be inserted as siblings.
  6783. In interactive use, the user will be prompted for the number of
  6784. clones to be produced, and for a time SHIFT, which may be a
  6785. repeater as used in time stamps, for example `+3d'.
  6786. When a valid repeater is given and the entry contains any time
  6787. stamps, the clones will become a sequence in time, with time
  6788. stamps in the subtree shifted for each clone produced. If SHIFT
  6789. is nil or the empty string, time stamps will be left alone. The
  6790. ID property of the original subtree is removed.
  6791. If the original subtree did contain time stamps with a repeater,
  6792. the following will happen:
  6793. - the repeater will be removed in each clone
  6794. - an additional clone will be produced, with the current, unshifted
  6795. date(s) in the entry.
  6796. - the original entry will be placed *after* all the clones, with
  6797. repeater intact.
  6798. - the start days in the repeater in the original entry will be shifted
  6799. to past the last clone.
  6800. I this way you can spell out a number of instances of a repeating task,
  6801. and still retain the repeater to cover future instances of the task."
  6802. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6803. (let (beg end template task idprop
  6804. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6805. (if (not (and (integerp n) (> n 0)))
  6806. (error "Invalid number of replications %s" n))
  6807. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6808. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6809. shift)))
  6810. (error "Invalid shift specification %s" shift))
  6811. (when doshift
  6812. (setq shift-n (string-to-number (match-string 1 shift))
  6813. shift-what (cdr (assoc (match-string 2 shift)
  6814. '(("d" . day) ("w" . week)
  6815. ("m" . month) ("y" . year))))))
  6816. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6817. (setq nmin 1 nmax n)
  6818. (org-back-to-heading t)
  6819. (setq beg (point))
  6820. (setq idprop (org-entry-get nil "ID"))
  6821. (org-end-of-subtree t t)
  6822. (or (bolp) (insert "\n"))
  6823. (setq end (point))
  6824. (setq template (buffer-substring beg end))
  6825. (when (and doshift
  6826. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6827. (delete-region beg end)
  6828. (setq end beg)
  6829. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6830. (goto-char end)
  6831. (loop for n from nmin to nmax do
  6832. ;; prepare clone
  6833. (with-temp-buffer
  6834. (insert template)
  6835. (org-mode)
  6836. (goto-char (point-min))
  6837. (and idprop (if org-clone-delete-id
  6838. (org-entry-delete nil "ID")
  6839. (org-id-get-create t)))
  6840. (while (re-search-forward org-property-start-re nil t)
  6841. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6842. (goto-char (point-min))
  6843. (when doshift
  6844. (while (re-search-forward org-ts-regexp-both nil t)
  6845. (org-timestamp-change (* n shift-n) shift-what))
  6846. (unless (= n n-no-remove)
  6847. (goto-char (point-min))
  6848. (while (re-search-forward org-ts-regexp nil t)
  6849. (save-excursion
  6850. (goto-char (match-beginning 0))
  6851. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6852. (delete-region (match-beginning 1) (match-end 1)))))))
  6853. (setq task (buffer-string)))
  6854. (insert task))
  6855. (goto-char beg)))
  6856. ;;; Outline Sorting
  6857. (defun org-sort (with-case)
  6858. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6859. Optional argument WITH-CASE means sort case-sensitively.
  6860. With a double prefix argument, also remove duplicate entries."
  6861. (interactive "P")
  6862. (cond
  6863. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6864. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6865. (t
  6866. (org-call-with-arg 'org-sort-entries with-case))))
  6867. (defun org-sort-remove-invisible (s)
  6868. (remove-text-properties 0 (length s) org-rm-props s)
  6869. (while (string-match org-bracket-link-regexp s)
  6870. (setq s (replace-match (if (match-end 2)
  6871. (match-string 3 s)
  6872. (match-string 1 s)) t t s)))
  6873. s)
  6874. (defvar org-priority-regexp) ; defined later in the file
  6875. (defvar org-after-sorting-entries-or-items-hook nil
  6876. "Hook that is run after a bunch of entries or items have been sorted.
  6877. When children are sorted, the cursor is in the parent line when this
  6878. hook gets called. When a region or a plain list is sorted, the cursor
  6879. will be in the first entry of the sorted region/list.")
  6880. (defun org-sort-entries
  6881. (&optional with-case sorting-type getkey-func compare-func property)
  6882. "Sort entries on a certain level of an outline tree.
  6883. If there is an active region, the entries in the region are sorted.
  6884. Else, if the cursor is before the first entry, sort the top-level items.
  6885. Else, the children of the entry at point are sorted.
  6886. Sorting can be alphabetically, numerically, by date/time as given by
  6887. a time stamp, by a property or by priority.
  6888. The command prompts for the sorting type unless it has been given to the
  6889. function through the SORTING-TYPE argument, which needs to be a character,
  6890. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6891. precise meaning of each character:
  6892. n Numerically, by converting the beginning of the entry/item to a number.
  6893. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6894. t By date/time, either the first active time stamp in the entry, or, if
  6895. none exist, by the first inactive one.
  6896. s By the scheduled date/time.
  6897. d By deadline date/time.
  6898. c By creation time, which is assumed to be the first inactive time stamp
  6899. at the beginning of a line.
  6900. p By priority according to the cookie.
  6901. r By the value of a property.
  6902. Capital letters will reverse the sort order.
  6903. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6904. called with point at the beginning of the record. It must return either
  6905. a string or a number that should serve as the sorting key for that record.
  6906. Comparing entries ignores case by default. However, with an optional argument
  6907. WITH-CASE, the sorting considers case as well."
  6908. (interactive "P")
  6909. (let ((case-func (if with-case 'identity 'downcase))
  6910. start beg end stars re re2
  6911. txt what tmp)
  6912. ;; Find beginning and end of region to sort
  6913. (cond
  6914. ((org-region-active-p)
  6915. ;; we will sort the region
  6916. (setq end (region-end)
  6917. what "region")
  6918. (goto-char (region-beginning))
  6919. (if (not (org-on-heading-p)) (outline-next-heading))
  6920. (setq start (point)))
  6921. ((or (org-on-heading-p)
  6922. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6923. ;; we will sort the children of the current headline
  6924. (org-back-to-heading)
  6925. (setq start (point)
  6926. end (progn (org-end-of-subtree t t)
  6927. (or (bolp) (insert "\n"))
  6928. (org-back-over-empty-lines)
  6929. (point))
  6930. what "children")
  6931. (goto-char start)
  6932. (show-subtree)
  6933. (outline-next-heading))
  6934. (t
  6935. ;; we will sort the top-level entries in this file
  6936. (goto-char (point-min))
  6937. (or (org-on-heading-p) (outline-next-heading))
  6938. (setq start (point))
  6939. (goto-char (point-max))
  6940. (beginning-of-line 1)
  6941. (when (looking-at ".*?\\S-")
  6942. ;; File ends in a non-white line
  6943. (end-of-line 1)
  6944. (insert "\n"))
  6945. (setq end (point-max))
  6946. (setq what "top-level")
  6947. (goto-char start)
  6948. (show-all)))
  6949. (setq beg (point))
  6950. (if (>= beg end) (error "Nothing to sort"))
  6951. (looking-at "\\(\\*+\\)")
  6952. (setq stars (match-string 1)
  6953. re (concat "^" (regexp-quote stars) " +")
  6954. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6955. txt (buffer-substring beg end))
  6956. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6957. (if (and (not (equal stars "*")) (string-match re2 txt))
  6958. (error "Region to sort contains a level above the first entry"))
  6959. (unless sorting-type
  6960. (message
  6961. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6962. [t]ime [s]cheduled [d]eadline [c]reated
  6963. A/N/T/S/D/C/P/O/F means reversed:"
  6964. what)
  6965. (setq sorting-type (read-char-exclusive))
  6966. (and (= (downcase sorting-type) ?f)
  6967. (setq getkey-func
  6968. (org-icompleting-read "Sort using function: "
  6969. obarray 'fboundp t nil nil))
  6970. (setq getkey-func (intern getkey-func)))
  6971. (and (= (downcase sorting-type) ?r)
  6972. (setq property
  6973. (org-icompleting-read "Property: "
  6974. (mapcar 'list (org-buffer-property-keys t))
  6975. nil t))))
  6976. (message "Sorting entries...")
  6977. (save-restriction
  6978. (narrow-to-region start end)
  6979. (let ((dcst (downcase sorting-type))
  6980. (case-fold-search nil)
  6981. (now (current-time)))
  6982. (sort-subr
  6983. (/= dcst sorting-type)
  6984. ;; This function moves to the beginning character of the "record" to
  6985. ;; be sorted.
  6986. (lambda nil
  6987. (if (re-search-forward re nil t)
  6988. (goto-char (match-beginning 0))
  6989. (goto-char (point-max))))
  6990. ;; This function moves to the last character of the "record" being
  6991. ;; sorted.
  6992. (lambda nil
  6993. (save-match-data
  6994. (condition-case nil
  6995. (outline-forward-same-level 1)
  6996. (error
  6997. (goto-char (point-max))))))
  6998. ;; This function returns the value that gets sorted against.
  6999. (lambda nil
  7000. (cond
  7001. ((= dcst ?n)
  7002. (if (looking-at org-complex-heading-regexp)
  7003. (string-to-number (match-string 4))
  7004. nil))
  7005. ((= dcst ?a)
  7006. (if (looking-at org-complex-heading-regexp)
  7007. (funcall case-func (match-string 4))
  7008. nil))
  7009. ((= dcst ?t)
  7010. (let ((end (save-excursion (outline-next-heading) (point))))
  7011. (if (or (re-search-forward org-ts-regexp end t)
  7012. (re-search-forward org-ts-regexp-both end t))
  7013. (org-time-string-to-seconds (match-string 0))
  7014. (org-float-time now))))
  7015. ((= dcst ?c)
  7016. (let ((end (save-excursion (outline-next-heading) (point))))
  7017. (if (re-search-forward
  7018. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7019. end t)
  7020. (org-time-string-to-seconds (match-string 0))
  7021. (org-float-time now))))
  7022. ((= dcst ?s)
  7023. (let ((end (save-excursion (outline-next-heading) (point))))
  7024. (if (re-search-forward org-scheduled-time-regexp end t)
  7025. (org-time-string-to-seconds (match-string 1))
  7026. (org-float-time now))))
  7027. ((= dcst ?d)
  7028. (let ((end (save-excursion (outline-next-heading) (point))))
  7029. (if (re-search-forward org-deadline-time-regexp end t)
  7030. (org-time-string-to-seconds (match-string 1))
  7031. (org-float-time now))))
  7032. ((= dcst ?p)
  7033. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7034. (string-to-char (match-string 2))
  7035. org-default-priority))
  7036. ((= dcst ?r)
  7037. (or (org-entry-get nil property) ""))
  7038. ((= dcst ?o)
  7039. (if (looking-at org-complex-heading-regexp)
  7040. (- 9999 (length (member (match-string 2)
  7041. org-todo-keywords-1)))))
  7042. ((= dcst ?f)
  7043. (if getkey-func
  7044. (progn
  7045. (setq tmp (funcall getkey-func))
  7046. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7047. tmp)
  7048. (error "Invalid key function `%s'" getkey-func)))
  7049. (t (error "Invalid sorting type `%c'" sorting-type))))
  7050. nil
  7051. (cond
  7052. ((= dcst ?a) 'string<)
  7053. ((= dcst ?f) compare-func)
  7054. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7055. (t nil)))))
  7056. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7057. (message "Sorting entries...done")))
  7058. (defun org-do-sort (table what &optional with-case sorting-type)
  7059. "Sort TABLE of WHAT according to SORTING-TYPE.
  7060. The user will be prompted for the SORTING-TYPE if the call to this
  7061. function does not specify it. WHAT is only for the prompt, to indicate
  7062. what is being sorted. The sorting key will be extracted from
  7063. the car of the elements of the table.
  7064. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7065. (unless sorting-type
  7066. (message
  7067. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7068. what)
  7069. (setq sorting-type (read-char-exclusive)))
  7070. (let ((dcst (downcase sorting-type))
  7071. extractfun comparefun)
  7072. ;; Define the appropriate functions
  7073. (cond
  7074. ((= dcst ?n)
  7075. (setq extractfun 'string-to-number
  7076. comparefun (if (= dcst sorting-type) '< '>)))
  7077. ((= dcst ?a)
  7078. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7079. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7080. comparefun (if (= dcst sorting-type)
  7081. 'string<
  7082. (lambda (a b) (and (not (string< a b))
  7083. (not (string= a b)))))))
  7084. ((= dcst ?t)
  7085. (setq extractfun
  7086. (lambda (x)
  7087. (if (or (string-match org-ts-regexp x)
  7088. (string-match org-ts-regexp-both x))
  7089. (org-float-time
  7090. (org-time-string-to-time (match-string 0 x)))
  7091. 0))
  7092. comparefun (if (= dcst sorting-type) '< '>)))
  7093. (t (error "Invalid sorting type `%c'" sorting-type)))
  7094. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7095. table)
  7096. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7097. ;;; The orgstruct minor mode
  7098. ;; Define a minor mode which can be used in other modes in order to
  7099. ;; integrate the org-mode structure editing commands.
  7100. ;; This is really a hack, because the org-mode structure commands use
  7101. ;; keys which normally belong to the major mode. Here is how it
  7102. ;; works: The minor mode defines all the keys necessary to operate the
  7103. ;; structure commands, but wraps the commands into a function which
  7104. ;; tests if the cursor is currently at a headline or a plain list
  7105. ;; item. If that is the case, the structure command is used,
  7106. ;; temporarily setting many Org-mode variables like regular
  7107. ;; expressions for filling etc. However, when any of those keys is
  7108. ;; used at a different location, function uses `key-binding' to look
  7109. ;; up if the key has an associated command in another currently active
  7110. ;; keymap (minor modes, major mode, global), and executes that
  7111. ;; command. There might be problems if any of the keys is otherwise
  7112. ;; used as a prefix key.
  7113. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7114. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7115. ;; addresses this by checking explicitly for both bindings.
  7116. (defvar orgstruct-mode-map (make-sparse-keymap)
  7117. "Keymap for the minor `orgstruct-mode'.")
  7118. (defvar org-local-vars nil
  7119. "List of local variables, for use by `orgstruct-mode'.")
  7120. ;;;###autoload
  7121. (define-minor-mode orgstruct-mode
  7122. "Toggle the minor mode `orgstruct-mode'.
  7123. This mode is for using Org-mode structure commands in other
  7124. modes. The following keys behave as if Org-mode were active, if
  7125. the cursor is on a headline, or on a plain list item (both as
  7126. defined by Org-mode).
  7127. M-up Move entry/item up
  7128. M-down Move entry/item down
  7129. M-left Promote
  7130. M-right Demote
  7131. M-S-up Move entry/item up
  7132. M-S-down Move entry/item down
  7133. M-S-left Promote subtree
  7134. M-S-right Demote subtree
  7135. M-q Fill paragraph and items like in Org-mode
  7136. C-c ^ Sort entries
  7137. C-c - Cycle list bullet
  7138. TAB Cycle item visibility
  7139. M-RET Insert new heading/item
  7140. S-M-RET Insert new TODO heading / Checkbox item
  7141. C-c C-c Set tags / toggle checkbox"
  7142. nil " OrgStruct" nil
  7143. (org-load-modules-maybe)
  7144. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7145. ;;;###autoload
  7146. (defun turn-on-orgstruct ()
  7147. "Unconditionally turn on `orgstruct-mode'."
  7148. (orgstruct-mode 1))
  7149. (defun orgstruct++-mode (&optional arg)
  7150. "Toggle `orgstruct-mode', the enhanced version of it.
  7151. In addition to setting orgstruct-mode, this also exports all indentation
  7152. and autofilling variables from org-mode into the buffer. It will also
  7153. recognize item context in multiline items.
  7154. Note that turning off orgstruct-mode will *not* remove the
  7155. indentation/paragraph settings. This can only be done by refreshing the
  7156. major mode, for example with \\[normal-mode]."
  7157. (interactive "P")
  7158. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7159. (if (< arg 1)
  7160. (orgstruct-mode -1)
  7161. (orgstruct-mode 1)
  7162. (let (var val)
  7163. (mapc
  7164. (lambda (x)
  7165. (when (string-match
  7166. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7167. (symbol-name (car x)))
  7168. (setq var (car x) val (nth 1 x))
  7169. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7170. org-local-vars)
  7171. (org-set-local 'orgstruct-is-++ t))))
  7172. (defvar orgstruct-is-++ nil
  7173. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7174. (make-variable-buffer-local 'orgstruct-is-++)
  7175. ;;;###autoload
  7176. (defun turn-on-orgstruct++ ()
  7177. "Unconditionally turn on `orgstruct++-mode'."
  7178. (orgstruct++-mode 1))
  7179. (defun orgstruct-error ()
  7180. "Error when there is no default binding for a structure key."
  7181. (interactive)
  7182. (error "This key has no function outside structure elements"))
  7183. (defun orgstruct-setup ()
  7184. "Setup orgstruct keymaps."
  7185. (let ((nfunc 0)
  7186. (bindings
  7187. (list
  7188. '([(meta up)] org-metaup)
  7189. '([(meta down)] org-metadown)
  7190. '([(meta left)] org-metaleft)
  7191. '([(meta right)] org-metaright)
  7192. '([(meta shift up)] org-shiftmetaup)
  7193. '([(meta shift down)] org-shiftmetadown)
  7194. '([(meta shift left)] org-shiftmetaleft)
  7195. '([(meta shift right)] org-shiftmetaright)
  7196. '([?\e (up)] org-metaup)
  7197. '([?\e (down)] org-metadown)
  7198. '([?\e (left)] org-metaleft)
  7199. '([?\e (right)] org-metaright)
  7200. '([?\e (shift up)] org-shiftmetaup)
  7201. '([?\e (shift down)] org-shiftmetadown)
  7202. '([?\e (shift left)] org-shiftmetaleft)
  7203. '([?\e (shift right)] org-shiftmetaright)
  7204. '([(shift up)] org-shiftup)
  7205. '([(shift down)] org-shiftdown)
  7206. '([(shift left)] org-shiftleft)
  7207. '([(shift right)] org-shiftright)
  7208. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7209. '("\M-q" fill-paragraph)
  7210. '("\C-c^" org-sort)
  7211. '("\C-c-" org-cycle-list-bullet)))
  7212. elt key fun cmd)
  7213. (while (setq elt (pop bindings))
  7214. (setq nfunc (1+ nfunc))
  7215. (setq key (org-key (car elt))
  7216. fun (nth 1 elt)
  7217. cmd (orgstruct-make-binding fun nfunc key))
  7218. (org-defkey orgstruct-mode-map key cmd))
  7219. ;; Special treatment needed for TAB and RET
  7220. (org-defkey orgstruct-mode-map [(tab)]
  7221. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7222. (org-defkey orgstruct-mode-map "\C-i"
  7223. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7224. (org-defkey orgstruct-mode-map "\M-\C-m"
  7225. (orgstruct-make-binding 'org-insert-heading 105
  7226. "\M-\C-m" [(meta return)]))
  7227. (org-defkey orgstruct-mode-map [(meta return)]
  7228. (orgstruct-make-binding 'org-insert-heading 106
  7229. [(meta return)] "\M-\C-m"))
  7230. (org-defkey orgstruct-mode-map [(shift meta return)]
  7231. (orgstruct-make-binding 'org-insert-todo-heading 107
  7232. [(meta return)] "\M-\C-m"))
  7233. (org-defkey orgstruct-mode-map "\e\C-m"
  7234. (orgstruct-make-binding 'org-insert-heading 108
  7235. "\e\C-m" [?\e (return)]))
  7236. (org-defkey orgstruct-mode-map [?\e (return)]
  7237. (orgstruct-make-binding 'org-insert-heading 109
  7238. [?\e (return)] "\e\C-m"))
  7239. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7240. (orgstruct-make-binding 'org-insert-todo-heading 110
  7241. [?\e (return)] "\e\C-m"))
  7242. (unless org-local-vars
  7243. (setq org-local-vars (org-get-local-variables)))
  7244. t))
  7245. (defun orgstruct-make-binding (fun n &rest keys)
  7246. "Create a function for binding in the structure minor mode.
  7247. FUN is the command to call inside a table. N is used to create a unique
  7248. command name. KEYS are keys that should be checked in for a command
  7249. to execute outside of tables."
  7250. (eval
  7251. (list 'defun
  7252. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7253. '(arg)
  7254. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7255. "Outside of structure, run the binding of `"
  7256. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7257. "'.")
  7258. '(interactive "p")
  7259. (list 'if
  7260. `(org-context-p 'headline 'item
  7261. (and orgstruct-is-++
  7262. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7263. 'item-body))
  7264. (list 'org-run-like-in-org-mode (list 'quote fun))
  7265. (list 'let '(orgstruct-mode)
  7266. (list 'call-interactively
  7267. (append '(or)
  7268. (mapcar (lambda (k)
  7269. (list 'key-binding k))
  7270. keys)
  7271. '('orgstruct-error))))))))
  7272. (defun org-context-p (&rest contexts)
  7273. "Check if local context is any of CONTEXTS.
  7274. Possible values in the list of contexts are `table', `headline', and `item'."
  7275. (let ((pos (point)))
  7276. (goto-char (point-at-bol))
  7277. (prog1 (or (and (memq 'table contexts)
  7278. (looking-at "[ \t]*|"))
  7279. (and (memq 'headline contexts)
  7280. ;;????????? (looking-at "\\*+"))
  7281. (looking-at outline-regexp))
  7282. (and (memq 'item contexts)
  7283. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7284. (and (memq 'item-body contexts)
  7285. (org-in-item-p)))
  7286. (goto-char pos))))
  7287. (defun org-get-local-variables ()
  7288. "Return a list of all local variables in an org-mode buffer."
  7289. (let (varlist)
  7290. (with-current-buffer (get-buffer-create "*Org tmp*")
  7291. (erase-buffer)
  7292. (org-mode)
  7293. (setq varlist (buffer-local-variables)))
  7294. (kill-buffer "*Org tmp*")
  7295. (delq nil
  7296. (mapcar
  7297. (lambda (x)
  7298. (setq x
  7299. (if (symbolp x)
  7300. (list x)
  7301. (list (car x) (list 'quote (cdr x)))))
  7302. (if (string-match
  7303. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7304. (symbol-name (car x)))
  7305. x nil))
  7306. varlist))))
  7307. ;;;###autoload
  7308. (defun org-run-like-in-org-mode (cmd)
  7309. "Run a command, pretending that the current buffer is in Org-mode.
  7310. This will temporarily bind local variables that are typically bound in
  7311. Org-mode to the values they have in Org-mode, and then interactively
  7312. call CMD."
  7313. (org-load-modules-maybe)
  7314. (unless org-local-vars
  7315. (setq org-local-vars (org-get-local-variables)))
  7316. (eval (list 'let org-local-vars
  7317. (list 'call-interactively (list 'quote cmd)))))
  7318. ;;;; Archiving
  7319. (defun org-get-category (&optional pos force-refresh)
  7320. "Get the category applying to position POS."
  7321. (if force-refresh (org-refresh-category-properties))
  7322. (let ((pos (or pos (point))))
  7323. (or (get-text-property pos 'org-category)
  7324. (progn (org-refresh-category-properties)
  7325. (get-text-property pos 'org-category)))))
  7326. (defun org-refresh-category-properties ()
  7327. "Refresh category text properties in the buffer."
  7328. (let ((def-cat (cond
  7329. ((null org-category)
  7330. (if buffer-file-name
  7331. (file-name-sans-extension
  7332. (file-name-nondirectory buffer-file-name))
  7333. "???"))
  7334. ((symbolp org-category) (symbol-name org-category))
  7335. (t org-category)))
  7336. beg end cat pos optionp)
  7337. (org-unmodified
  7338. (save-excursion
  7339. (save-restriction
  7340. (widen)
  7341. (goto-char (point-min))
  7342. (put-text-property (point) (point-max) 'org-category def-cat)
  7343. (while (re-search-forward
  7344. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7345. (setq pos (match-end 0)
  7346. optionp (equal (char-after (match-beginning 0)) ?#)
  7347. cat (org-trim (match-string 2)))
  7348. (if optionp
  7349. (setq beg (point-at-bol) end (point-max))
  7350. (org-back-to-heading t)
  7351. (setq beg (point) end (org-end-of-subtree t t)))
  7352. (put-text-property beg end 'org-category cat)
  7353. (goto-char pos)))))))
  7354. ;;;; Link Stuff
  7355. ;;; Link abbreviations
  7356. (defun org-link-expand-abbrev (link)
  7357. "Apply replacements as defined in `org-link-abbrev-alist."
  7358. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7359. (let* ((key (match-string 1 link))
  7360. (as (or (assoc key org-link-abbrev-alist-local)
  7361. (assoc key org-link-abbrev-alist)))
  7362. (tag (and (match-end 2) (match-string 3 link)))
  7363. rpl)
  7364. (if (not as)
  7365. link
  7366. (setq rpl (cdr as))
  7367. (cond
  7368. ((symbolp rpl) (funcall rpl tag))
  7369. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7370. ((string-match "%h" rpl)
  7371. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7372. (t (concat rpl tag)))))
  7373. link))
  7374. ;;; Storing and inserting links
  7375. (defvar org-insert-link-history nil
  7376. "Minibuffer history for links inserted with `org-insert-link'.")
  7377. (defvar org-stored-links nil
  7378. "Contains the links stored with `org-store-link'.")
  7379. (defvar org-store-link-plist nil
  7380. "Plist with info about the most recently link created with `org-store-link'.")
  7381. (defvar org-link-protocols nil
  7382. "Link protocols added to Org-mode using `org-add-link-type'.")
  7383. (defvar org-store-link-functions nil
  7384. "List of functions that are called to create and store a link.
  7385. Each function will be called in turn until one returns a non-nil
  7386. value. Each function should check if it is responsible for creating
  7387. this link (for example by looking at the major mode).
  7388. If not, it must exit and return nil.
  7389. If yes, it should return a non-nil value after a calling
  7390. `org-store-link-props' with a list of properties and values.
  7391. Special properties are:
  7392. :type The link prefix, like \"http\". This must be given.
  7393. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7394. This is obligatory as well.
  7395. :description Optional default description for the second pair
  7396. of brackets in an Org-mode link. The user can still change
  7397. this when inserting this link into an Org-mode buffer.
  7398. In addition to these, any additional properties can be specified
  7399. and then used in remember templates.")
  7400. (defun org-add-link-type (type &optional follow export)
  7401. "Add TYPE to the list of `org-link-types'.
  7402. Re-compute all regular expressions depending on `org-link-types'
  7403. FOLLOW and EXPORT are two functions.
  7404. FOLLOW should take the link path as the single argument and do whatever
  7405. is necessary to follow the link, for example find a file or display
  7406. a mail message.
  7407. EXPORT should format the link path for export to one of the export formats.
  7408. It should be a function accepting three arguments:
  7409. path the path of the link, the text after the prefix (like \"http:\")
  7410. desc the description of the link, if any, nil if there was no description
  7411. format the export format, a symbol like `html' or `latex' or `ascii'..
  7412. The function may use the FORMAT information to return different values
  7413. depending on the format. The return value will be put literally into
  7414. the exported file. If the return value is nil, this means Org should
  7415. do what it normally does with links which do not have EXPORT defined.
  7416. Org-mode has a built-in default for exporting links. If you are happy with
  7417. this default, there is no need to define an export function for the link
  7418. type. For a simple example of an export function, see `org-bbdb.el'."
  7419. (add-to-list 'org-link-types type t)
  7420. (org-make-link-regexps)
  7421. (if (assoc type org-link-protocols)
  7422. (setcdr (assoc type org-link-protocols) (list follow export))
  7423. (push (list type follow export) org-link-protocols)))
  7424. (defvar org-agenda-buffer-name)
  7425. ;;;###autoload
  7426. (defun org-store-link (arg)
  7427. "\\<org-mode-map>Store an org-link to the current location.
  7428. This link is added to `org-stored-links' and can later be inserted
  7429. into an org-buffer with \\[org-insert-link].
  7430. For some link types, a prefix arg is interpreted:
  7431. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7432. For file links, arg negates `org-context-in-file-links'."
  7433. (interactive "P")
  7434. (org-load-modules-maybe)
  7435. (setq org-store-link-plist nil) ; reset
  7436. (let ((outline-regexp (org-get-limited-outline-regexp))
  7437. link cpltxt desc description search txt custom-id agenda-link)
  7438. (cond
  7439. ((run-hook-with-args-until-success 'org-store-link-functions)
  7440. (setq link (plist-get org-store-link-plist :link)
  7441. desc (or (plist-get org-store-link-plist :description) link)))
  7442. ((equal (buffer-name) "*Org Edit Src Example*")
  7443. (let (label gc)
  7444. (while (or (not label)
  7445. (save-excursion
  7446. (save-restriction
  7447. (widen)
  7448. (goto-char (point-min))
  7449. (re-search-forward
  7450. (regexp-quote (format org-coderef-label-format label))
  7451. nil t))))
  7452. (when label (message "Label exists already") (sit-for 2))
  7453. (setq label (read-string "Code line label: " label)))
  7454. (end-of-line 1)
  7455. (setq link (format org-coderef-label-format label))
  7456. (setq gc (- 79 (length link)))
  7457. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7458. (insert link)
  7459. (setq link (concat "(" label ")") desc nil)))
  7460. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7461. ;; We are in the agenda, link to referenced location
  7462. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7463. (get-text-property (point) 'org-marker))))
  7464. (when m
  7465. (org-with-point-at m
  7466. (setq agenda-link
  7467. (if (interactive-p)
  7468. (call-interactively 'org-store-link)
  7469. (org-store-link nil)))))))
  7470. ((eq major-mode 'calendar-mode)
  7471. (let ((cd (calendar-cursor-to-date)))
  7472. (setq link
  7473. (format-time-string
  7474. (car org-time-stamp-formats)
  7475. (apply 'encode-time
  7476. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7477. nil nil nil))))
  7478. (org-store-link-props :type "calendar" :date cd)))
  7479. ((eq major-mode 'w3-mode)
  7480. (setq cpltxt (if (and (buffer-name)
  7481. (not (string-match "Untitled" (buffer-name))))
  7482. (buffer-name)
  7483. (url-view-url t))
  7484. link (org-make-link (url-view-url t)))
  7485. (org-store-link-props :type "w3" :url (url-view-url t)))
  7486. ((eq major-mode 'w3m-mode)
  7487. (setq cpltxt (or w3m-current-title w3m-current-url)
  7488. link (org-make-link w3m-current-url))
  7489. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7490. ((setq search (run-hook-with-args-until-success
  7491. 'org-create-file-search-functions))
  7492. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7493. "::" search))
  7494. (setq cpltxt (or description link)))
  7495. ((eq major-mode 'image-mode)
  7496. (setq cpltxt (concat "file:"
  7497. (abbreviate-file-name buffer-file-name))
  7498. link (org-make-link cpltxt))
  7499. (org-store-link-props :type "image" :file buffer-file-name))
  7500. ((eq major-mode 'dired-mode)
  7501. ;; link to the file in the current line
  7502. (let ((file (dired-get-filename nil t)))
  7503. (setq file (if file
  7504. (abbreviate-file-name
  7505. (expand-file-name (dired-get-filename nil t)))
  7506. ;; otherwise, no file so use current directory.
  7507. default-directory))
  7508. (setq cpltxt (concat "file:" file)
  7509. link (org-make-link cpltxt))))
  7510. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7511. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7512. (cond
  7513. ((org-in-regexp "<<\\(.*?\\)>>")
  7514. (setq cpltxt
  7515. (concat "file:"
  7516. (abbreviate-file-name
  7517. (buffer-file-name (buffer-base-buffer)))
  7518. "::" (match-string 1))
  7519. link (org-make-link cpltxt)))
  7520. ((and (featurep 'org-id)
  7521. (or (eq org-link-to-org-use-id t)
  7522. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7523. (interactive-p))
  7524. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7525. (interactive-p)
  7526. (not custom-id))
  7527. (and org-link-to-org-use-id
  7528. (condition-case nil
  7529. (org-entry-get nil "ID")
  7530. (error nil)))))
  7531. ;; We can make a link using the ID.
  7532. (setq link (condition-case nil
  7533. (prog1 (org-id-store-link)
  7534. (setq desc (plist-get org-store-link-plist
  7535. :description)))
  7536. (error
  7537. ;; probably before first headline, link to file only
  7538. (concat "file:"
  7539. (abbreviate-file-name
  7540. (buffer-file-name (buffer-base-buffer))))))))
  7541. (t
  7542. ;; Just link to current headline
  7543. (setq cpltxt (concat "file:"
  7544. (abbreviate-file-name
  7545. (buffer-file-name (buffer-base-buffer)))))
  7546. ;; Add a context search string
  7547. (when (org-xor org-context-in-file-links arg)
  7548. (setq txt (cond
  7549. ((org-on-heading-p) nil)
  7550. ((org-region-active-p)
  7551. (buffer-substring (region-beginning) (region-end)))
  7552. (t nil)))
  7553. (when (or (null txt) (string-match "\\S-" txt))
  7554. (setq cpltxt
  7555. (concat cpltxt "::"
  7556. (condition-case nil
  7557. (org-make-org-heading-search-string txt)
  7558. (error "")))
  7559. desc (or (nth 4 (ignore-errors
  7560. (org-heading-components))) "NONE"))))
  7561. (if (string-match "::\\'" cpltxt)
  7562. (setq cpltxt (substring cpltxt 0 -2)))
  7563. (setq link (org-make-link cpltxt)))))
  7564. ((buffer-file-name (buffer-base-buffer))
  7565. ;; Just link to this file here.
  7566. (setq cpltxt (concat "file:"
  7567. (abbreviate-file-name
  7568. (buffer-file-name (buffer-base-buffer)))))
  7569. ;; Add a context string
  7570. (when (org-xor org-context-in-file-links arg)
  7571. (setq txt (if (org-region-active-p)
  7572. (buffer-substring (region-beginning) (region-end))
  7573. (buffer-substring (point-at-bol) (point-at-eol))))
  7574. ;; Only use search option if there is some text.
  7575. (when (string-match "\\S-" txt)
  7576. (setq cpltxt
  7577. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7578. desc "NONE")))
  7579. (setq link (org-make-link cpltxt)))
  7580. ((interactive-p)
  7581. (error "Cannot link to a buffer which is not visiting a file"))
  7582. (t (setq link nil)))
  7583. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7584. (setq link (or link cpltxt)
  7585. desc (or desc cpltxt))
  7586. (if (equal desc "NONE") (setq desc nil))
  7587. (if (and (or (interactive-p) executing-kbd-macro) link)
  7588. (progn
  7589. (setq org-stored-links
  7590. (cons (list link desc) org-stored-links))
  7591. (message "Stored: %s" (or desc link))
  7592. (when custom-id
  7593. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7594. "::#" custom-id))
  7595. (setq org-stored-links
  7596. (cons (list link desc) org-stored-links))))
  7597. (or agenda-link (and link (org-make-link-string link desc))))))
  7598. (defun org-store-link-props (&rest plist)
  7599. "Store link properties, extract names and addresses."
  7600. (let (x adr)
  7601. (when (setq x (plist-get plist :from))
  7602. (setq adr (mail-extract-address-components x))
  7603. (setq plist (plist-put plist :fromname (car adr)))
  7604. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7605. (when (setq x (plist-get plist :to))
  7606. (setq adr (mail-extract-address-components x))
  7607. (setq plist (plist-put plist :toname (car adr)))
  7608. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7609. (let ((from (plist-get plist :from))
  7610. (to (plist-get plist :to)))
  7611. (when (and from to org-from-is-user-regexp)
  7612. (setq plist
  7613. (plist-put plist :fromto
  7614. (if (string-match org-from-is-user-regexp from)
  7615. (concat "to %t")
  7616. (concat "from %f"))))))
  7617. (setq org-store-link-plist plist))
  7618. (defun org-add-link-props (&rest plist)
  7619. "Add these properties to the link property list."
  7620. (let (key value)
  7621. (while plist
  7622. (setq key (pop plist) value (pop plist))
  7623. (setq org-store-link-plist
  7624. (plist-put org-store-link-plist key value)))))
  7625. (defun org-email-link-description (&optional fmt)
  7626. "Return the description part of an email link.
  7627. This takes information from `org-store-link-plist' and formats it
  7628. according to FMT (default from `org-email-link-description-format')."
  7629. (setq fmt (or fmt org-email-link-description-format))
  7630. (let* ((p org-store-link-plist)
  7631. (to (plist-get p :toaddress))
  7632. (from (plist-get p :fromaddress))
  7633. (table
  7634. (list
  7635. (cons "%c" (plist-get p :fromto))
  7636. (cons "%F" (plist-get p :from))
  7637. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7638. (cons "%T" (plist-get p :to))
  7639. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7640. (cons "%s" (plist-get p :subject))
  7641. (cons "%d" (plist-get p :date))
  7642. (cons "%m" (plist-get p :message-id)))))
  7643. (when (string-match "%c" fmt)
  7644. ;; Check if the user wrote this message
  7645. (if (and org-from-is-user-regexp from to
  7646. (save-match-data (string-match org-from-is-user-regexp from)))
  7647. (setq fmt (replace-match "to %t" t t fmt))
  7648. (setq fmt (replace-match "from %f" t t fmt))))
  7649. (org-replace-escapes fmt table)))
  7650. (defun org-make-org-heading-search-string (&optional string heading)
  7651. "Make search string for STRING or current headline."
  7652. (interactive)
  7653. (let ((s (or string (org-get-heading)))
  7654. (lines org-context-in-file-links))
  7655. (unless (and string (not heading))
  7656. ;; We are using a headline, clean up garbage in there.
  7657. (if (string-match org-todo-regexp s)
  7658. (setq s (replace-match "" t t s)))
  7659. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7660. (setq s (replace-match "" t t s)))
  7661. (setq s (org-trim s))
  7662. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7663. org-comment-string "\\)") s)
  7664. (setq s (replace-match "" t t s)))
  7665. (while (string-match org-ts-regexp s)
  7666. (setq s (replace-match "" t t s))))
  7667. (or string (setq s (concat "*" s))) ; Add * for headlines
  7668. (when (and string (integerp lines) (> lines 0))
  7669. (let ((slines (org-split-string s "\n")))
  7670. (when (< lines (length slines))
  7671. (setq s (mapconcat
  7672. 'identity
  7673. (reverse (nthcdr (- (length slines) lines)
  7674. (reverse slines))) "\n")))))
  7675. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7676. (defun org-make-link (&rest strings)
  7677. "Concatenate STRINGS."
  7678. (apply 'concat strings))
  7679. (defun org-make-link-string (link &optional description)
  7680. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7681. (unless (string-match "\\S-" link)
  7682. (error "Empty link"))
  7683. (when (and description
  7684. (stringp description)
  7685. (not (string-match "\\S-" description)))
  7686. (setq description nil))
  7687. (when (stringp description)
  7688. ;; Remove brackets from the description, they are fatal.
  7689. (while (string-match "\\[" description)
  7690. (setq description (replace-match "{" t t description)))
  7691. (while (string-match "\\]" description)
  7692. (setq description (replace-match "}" t t description))))
  7693. (when (equal (org-link-escape link) description)
  7694. ;; No description needed, it is identical
  7695. (setq description nil))
  7696. (when (and (not description)
  7697. (not (equal link (org-link-escape link))))
  7698. (setq description (org-extract-attributes link)))
  7699. (setq link (if (string-match org-link-types-re link)
  7700. (concat (match-string 1 link)
  7701. (org-link-escape (substring link (match-end 1))))
  7702. (org-link-escape link)))
  7703. (concat "[[" link "]"
  7704. (if description (concat "[" description "]") "")
  7705. "]"))
  7706. (defconst org-link-escape-chars
  7707. '((?\ . "%20")
  7708. (?\[ . "%5B")
  7709. (?\] . "%5D")
  7710. (?\340 . "%E0") ; `a
  7711. (?\342 . "%E2") ; ^a
  7712. (?\347 . "%E7") ; ,c
  7713. (?\350 . "%E8") ; `e
  7714. (?\351 . "%E9") ; 'e
  7715. (?\352 . "%EA") ; ^e
  7716. (?\356 . "%EE") ; ^i
  7717. (?\364 . "%F4") ; ^o
  7718. (?\371 . "%F9") ; `u
  7719. (?\373 . "%FB") ; ^u
  7720. (?\; . "%3B")
  7721. ;; (?? . "%3F")
  7722. (?= . "%3D")
  7723. (?+ . "%2B")
  7724. )
  7725. "Association list of escapes for some characters problematic in links.
  7726. This is the list that is used for internal purposes.")
  7727. (defvar org-url-encoding-use-url-hexify nil)
  7728. (defconst org-link-escape-chars-browser
  7729. '((?\ . "%20")) ; 32 for the SPC char
  7730. "Association list of escapes for some characters problematic in links.
  7731. This is the list that is used before handing over to the browser.")
  7732. (defun org-link-escape (text &optional table)
  7733. "Escape characters in TEXT that are problematic for links."
  7734. (if (and org-url-encoding-use-url-hexify (not table))
  7735. (url-hexify-string text)
  7736. (setq table (or table org-link-escape-chars))
  7737. (when text
  7738. (let ((re (mapconcat (lambda (x) (regexp-quote
  7739. (char-to-string (car x))))
  7740. table "\\|")))
  7741. (while (string-match re text)
  7742. (setq text
  7743. (replace-match
  7744. (cdr (assoc (string-to-char (match-string 0 text))
  7745. table))
  7746. t t text)))
  7747. text))))
  7748. (defun org-link-unescape (text &optional table)
  7749. "Reverse the action of `org-link-escape'."
  7750. (if (and org-url-encoding-use-url-hexify (not table))
  7751. (url-unhex-string text)
  7752. (setq table (or table org-link-escape-chars))
  7753. (when text
  7754. (let ((case-fold-search t)
  7755. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7756. table "\\|")))
  7757. (while (string-match re text)
  7758. (setq text
  7759. (replace-match
  7760. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7761. table)))
  7762. t t text)))
  7763. text))))
  7764. (defun org-xor (a b)
  7765. "Exclusive or."
  7766. (if a (not b) b))
  7767. (defun org-fixup-message-id-for-http (s)
  7768. "Replace special characters in a message id, so it can be used in an http query."
  7769. (when (string-match "%" s)
  7770. (setq s (mapconcat (lambda (c)
  7771. (if (eq c ?%)
  7772. "%25"
  7773. (char-to-string c)))
  7774. s "")))
  7775. (while (string-match "<" s)
  7776. (setq s (replace-match "%3C" t t s)))
  7777. (while (string-match ">" s)
  7778. (setq s (replace-match "%3E" t t s)))
  7779. (while (string-match "@" s)
  7780. (setq s (replace-match "%40" t t s)))
  7781. s)
  7782. ;;;###autoload
  7783. (defun org-insert-link-global ()
  7784. "Insert a link like Org-mode does.
  7785. This command can be called in any mode to insert a link in Org-mode syntax."
  7786. (interactive)
  7787. (org-load-modules-maybe)
  7788. (org-run-like-in-org-mode 'org-insert-link))
  7789. (defun org-insert-link (&optional complete-file link-location)
  7790. "Insert a link. At the prompt, enter the link.
  7791. Completion can be used to insert any of the link protocol prefixes like
  7792. http or ftp in use.
  7793. The history can be used to select a link previously stored with
  7794. `org-store-link'. When the empty string is entered (i.e. if you just
  7795. press RET at the prompt), the link defaults to the most recently
  7796. stored link. As SPC triggers completion in the minibuffer, you need to
  7797. use M-SPC or C-q SPC to force the insertion of a space character.
  7798. You will also be prompted for a description, and if one is given, it will
  7799. be displayed in the buffer instead of the link.
  7800. If there is already a link at point, this command will allow you to edit link
  7801. and description parts.
  7802. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7803. be selected using completion. The path to the file will be relative to the
  7804. current directory if the file is in the current directory or a subdirectory.
  7805. Otherwise, the link will be the absolute path as completed in the minibuffer
  7806. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7807. option `org-link-file-path-type'.
  7808. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7809. the current directory or below.
  7810. With three \\[universal-argument] prefixes, negate the meaning of
  7811. `org-keep-stored-link-after-insertion'.
  7812. If `org-make-link-description-function' is non-nil, this function will be
  7813. called with the link target, and the result will be the default
  7814. link description.
  7815. If the LINK-LOCATION parameter is non-nil, this value will be
  7816. used as the link location instead of reading one interactively."
  7817. (interactive "P")
  7818. (let* ((wcf (current-window-configuration))
  7819. (region (if (org-region-active-p)
  7820. (buffer-substring (region-beginning) (region-end))))
  7821. (remove (and region (list (region-beginning) (region-end))))
  7822. (desc region)
  7823. tmphist ; byte-compile incorrectly complains about this
  7824. (link link-location)
  7825. entry file all-prefixes)
  7826. (cond
  7827. (link-location) ; specified by arg, just use it.
  7828. ((org-in-regexp org-bracket-link-regexp 1)
  7829. ;; We do have a link at point, and we are going to edit it.
  7830. (setq remove (list (match-beginning 0) (match-end 0)))
  7831. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7832. (setq link (read-string "Link: "
  7833. (org-link-unescape
  7834. (org-match-string-no-properties 1)))))
  7835. ((or (org-in-regexp org-angle-link-re)
  7836. (org-in-regexp org-plain-link-re))
  7837. ;; Convert to bracket link
  7838. (setq remove (list (match-beginning 0) (match-end 0))
  7839. link (read-string "Link: "
  7840. (org-remove-angle-brackets (match-string 0)))))
  7841. ((member complete-file '((4) (16)))
  7842. ;; Completing read for file names.
  7843. (setq link (org-file-complete-link complete-file)))
  7844. (t
  7845. ;; Read link, with completion for stored links.
  7846. (with-output-to-temp-buffer "*Org Links*"
  7847. (princ "Insert a link.
  7848. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7849. (when org-stored-links
  7850. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7851. (princ (mapconcat
  7852. (lambda (x)
  7853. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7854. (reverse org-stored-links) "\n"))))
  7855. (let ((cw (selected-window)))
  7856. (select-window (get-buffer-window "*Org Links*" 'visible))
  7857. (setq truncate-lines t)
  7858. (unless (pos-visible-in-window-p (point-max))
  7859. (org-fit-window-to-buffer))
  7860. (and (window-live-p cw) (select-window cw)))
  7861. ;; Fake a link history, containing the stored links.
  7862. (setq tmphist (append (mapcar 'car org-stored-links)
  7863. org-insert-link-history))
  7864. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7865. (mapcar 'car org-link-abbrev-alist)
  7866. org-link-types))
  7867. (unwind-protect
  7868. (progn
  7869. (setq link
  7870. (let ((org-completion-use-ido nil)
  7871. (org-completion-use-iswitchb nil))
  7872. (org-completing-read
  7873. "Link: "
  7874. (append
  7875. (mapcar (lambda (x) (list (concat x ":")))
  7876. all-prefixes)
  7877. (mapcar 'car org-stored-links))
  7878. nil nil nil
  7879. 'tmphist
  7880. (car (car org-stored-links)))))
  7881. (if (not (string-match "\\S-" link))
  7882. (error "No link selected"))
  7883. (if (or (member link all-prefixes)
  7884. (and (equal ":" (substring link -1))
  7885. (member (substring link 0 -1) all-prefixes)
  7886. (setq link (substring link 0 -1))))
  7887. (setq link (org-link-try-special-completion link))))
  7888. (set-window-configuration wcf)
  7889. (kill-buffer "*Org Links*"))
  7890. (setq entry (assoc link org-stored-links))
  7891. (or entry (push link org-insert-link-history))
  7892. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7893. (not org-keep-stored-link-after-insertion))
  7894. (setq org-stored-links (delq (assoc link org-stored-links)
  7895. org-stored-links)))
  7896. (setq desc (or desc (nth 1 entry)))))
  7897. (if (string-match org-plain-link-re link)
  7898. ;; URL-like link, normalize the use of angular brackets.
  7899. (setq link (org-make-link (org-remove-angle-brackets link))))
  7900. ;; Check if we are linking to the current file with a search option
  7901. ;; If yes, simplify the link by using only the search option.
  7902. (when (and buffer-file-name
  7903. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7904. (let* ((path (match-string 1 link))
  7905. (case-fold-search nil)
  7906. (search (match-string 2 link)))
  7907. (save-match-data
  7908. (if (equal (file-truename buffer-file-name) (file-truename path))
  7909. ;; We are linking to this same file, with a search option
  7910. (setq link search)))))
  7911. ;; Check if we can/should use a relative path. If yes, simplify the link
  7912. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7913. (let* ((type (match-string 1 link))
  7914. (path (match-string 2 link))
  7915. (origpath path)
  7916. (case-fold-search nil))
  7917. (cond
  7918. ((or (eq org-link-file-path-type 'absolute)
  7919. (equal complete-file '(16)))
  7920. (setq path (abbreviate-file-name (expand-file-name path))))
  7921. ((eq org-link-file-path-type 'noabbrev)
  7922. (setq path (expand-file-name path)))
  7923. ((eq org-link-file-path-type 'relative)
  7924. (setq path (file-relative-name path)))
  7925. (t
  7926. (save-match-data
  7927. (if (string-match (concat "^" (regexp-quote
  7928. (expand-file-name
  7929. (file-name-as-directory
  7930. default-directory))))
  7931. (expand-file-name path))
  7932. ;; We are linking a file with relative path name.
  7933. (setq path (substring (expand-file-name path)
  7934. (match-end 0)))
  7935. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7936. (setq link (concat type path))
  7937. (if (equal desc origpath)
  7938. (setq desc path))))
  7939. (if org-make-link-description-function
  7940. (setq desc (funcall org-make-link-description-function link desc)))
  7941. (setq desc (read-string "Description: " desc))
  7942. (unless (string-match "\\S-" desc) (setq desc nil))
  7943. (if remove (apply 'delete-region remove))
  7944. (insert (org-make-link-string link desc))))
  7945. (defun org-link-try-special-completion (type)
  7946. "If there is completion support for link type TYPE, offer it."
  7947. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7948. (if (functionp fun)
  7949. (funcall fun)
  7950. (read-string "Link (no completion support): " (concat type ":")))))
  7951. (defun org-file-complete-link (&optional arg)
  7952. "Create a file link using completion."
  7953. (let (file link)
  7954. (setq file (read-file-name "File: "))
  7955. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7956. (pwd1 (file-name-as-directory (abbreviate-file-name
  7957. (expand-file-name ".")))))
  7958. (cond
  7959. ((equal arg '(16))
  7960. (setq link (org-make-link
  7961. "file:"
  7962. (abbreviate-file-name (expand-file-name file)))))
  7963. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7964. (setq link (org-make-link "file:" (match-string 1 file))))
  7965. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7966. (expand-file-name file))
  7967. (setq link (org-make-link
  7968. "file:" (match-string 1 (expand-file-name file)))))
  7969. (t (setq link (org-make-link "file:" file)))))
  7970. link))
  7971. (defun org-completing-read (&rest args)
  7972. "Completing-read with SPACE being a normal character."
  7973. (let ((minibuffer-local-completion-map
  7974. (copy-keymap minibuffer-local-completion-map)))
  7975. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7976. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7977. (apply 'org-icompleting-read args)))
  7978. (defun org-completing-read-no-i (&rest args)
  7979. (let (org-completion-use-ido org-completion-use-iswitchb)
  7980. (apply 'org-completing-read args)))
  7981. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7982. "Use iswitch as a completing-read replacement to choose from choices.
  7983. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7984. from."
  7985. (let* ((iswitchb-use-virtual-buffers nil)
  7986. (iswitchb-make-buflist-hook
  7987. (lambda ()
  7988. (setq iswitchb-temp-buflist choices))))
  7989. (iswitchb-read-buffer prompt)))
  7990. (defun org-icompleting-read (&rest args)
  7991. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7992. (org-without-partial-completion
  7993. (if (and org-completion-use-ido
  7994. (fboundp 'ido-completing-read)
  7995. (boundp 'ido-mode) ido-mode
  7996. (listp (second args)))
  7997. (let ((ido-enter-matching-directory nil))
  7998. (apply 'ido-completing-read (concat (car args))
  7999. (if (consp (car (nth 1 args)))
  8000. (mapcar (lambda (x) (car x)) (nth 1 args))
  8001. (nth 1 args))
  8002. (cddr args)))
  8003. (if (and org-completion-use-iswitchb
  8004. (boundp 'iswitchb-mode) iswitchb-mode
  8005. (listp (second args)))
  8006. (apply 'org-iswitchb-completing-read (concat (car args))
  8007. (if (consp (car (nth 1 args)))
  8008. (mapcar (lambda (x) (car x)) (nth 1 args))
  8009. (nth 1 args))
  8010. (cddr args))
  8011. (apply 'completing-read args)))))
  8012. (defun org-extract-attributes (s)
  8013. "Extract the attributes cookie from a string and set as text property."
  8014. (let (a attr (start 0) key value)
  8015. (save-match-data
  8016. (when (string-match "{{\\([^}]+\\)}}$" s)
  8017. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8018. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8019. (setq key (match-string 1 a) value (match-string 2 a)
  8020. start (match-end 0)
  8021. attr (plist-put attr (intern key) value))))
  8022. (org-add-props s nil 'org-attr attr))
  8023. s))
  8024. (defun org-extract-attributes-from-string (tag)
  8025. (let (key value attr)
  8026. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8027. (setq key (match-string 1 tag) value (match-string 2 tag)
  8028. tag (replace-match "" t t tag)
  8029. attr (plist-put attr (intern key) value)))
  8030. (cons tag attr)))
  8031. (defun org-attributes-to-string (plist)
  8032. "Format a property list into an HTML attribute list."
  8033. (let ((s "") key value)
  8034. (while plist
  8035. (setq key (pop plist) value (pop plist))
  8036. (and value
  8037. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8038. s))
  8039. ;;; Opening/following a link
  8040. (defvar org-link-search-failed nil)
  8041. (defvar org-open-link-functions nil
  8042. "Hook for functions finding a plain text link.
  8043. These functions must take a single argument, the link content.
  8044. They will be called for links that look like [[link text][description]]
  8045. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8046. like a filename (e.g. \"./blue.png\").
  8047. These functions will be called *before* Org attempts to resolve the
  8048. link by doing text searches in the current buffer - so if you want a
  8049. link \"[[target]]\" to still find \"<<target>>\", your function should
  8050. handle this as a special case.
  8051. When the function does handle the link, it must return a non-nil value.
  8052. If it decides that it is not responsible for this link, it must return
  8053. nil to indicate that that Org-mode can continue with other options
  8054. like exact and fuzzy text search.")
  8055. (defun org-next-link ()
  8056. "Move forward to the next link.
  8057. If the link is in hidden text, expose it."
  8058. (interactive)
  8059. (when (and org-link-search-failed (eq this-command last-command))
  8060. (goto-char (point-min))
  8061. (message "Link search wrapped back to beginning of buffer"))
  8062. (setq org-link-search-failed nil)
  8063. (let* ((pos (point))
  8064. (ct (org-context))
  8065. (a (assoc :link ct)))
  8066. (if a (goto-char (nth 2 a)))
  8067. (if (re-search-forward org-any-link-re nil t)
  8068. (progn
  8069. (goto-char (match-beginning 0))
  8070. (if (org-invisible-p) (org-show-context)))
  8071. (goto-char pos)
  8072. (setq org-link-search-failed t)
  8073. (error "No further link found"))))
  8074. (defun org-previous-link ()
  8075. "Move backward to the previous link.
  8076. If the link is in hidden text, expose it."
  8077. (interactive)
  8078. (when (and org-link-search-failed (eq this-command last-command))
  8079. (goto-char (point-max))
  8080. (message "Link search wrapped back to end of buffer"))
  8081. (setq org-link-search-failed nil)
  8082. (let* ((pos (point))
  8083. (ct (org-context))
  8084. (a (assoc :link ct)))
  8085. (if a (goto-char (nth 1 a)))
  8086. (if (re-search-backward org-any-link-re nil t)
  8087. (progn
  8088. (goto-char (match-beginning 0))
  8089. (if (org-invisible-p) (org-show-context)))
  8090. (goto-char pos)
  8091. (setq org-link-search-failed t)
  8092. (error "No further link found"))))
  8093. (defun org-translate-link (s)
  8094. "Translate a link string if a translation function has been defined."
  8095. (if (and org-link-translation-function
  8096. (fboundp org-link-translation-function)
  8097. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8098. (progn
  8099. (setq s (funcall org-link-translation-function
  8100. (match-string 1) (match-string 2)))
  8101. (concat (car s) ":" (cdr s)))
  8102. s))
  8103. (defun org-translate-link-from-planner (type path)
  8104. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8105. This is still an experimental function, your mileage may vary."
  8106. (cond
  8107. ((member type '("http" "https" "news" "ftp"))
  8108. ;; standard Internet links are the same.
  8109. nil)
  8110. ((and (equal type "irc") (string-match "^//" path))
  8111. ;; Planner has two / at the beginning of an irc link, we have 1.
  8112. ;; We should have zero, actually....
  8113. (setq path (substring path 1)))
  8114. ((and (equal type "lisp") (string-match "^/" path))
  8115. ;; Planner has a slash, we do not.
  8116. (setq type "elisp" path (substring path 1)))
  8117. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8118. ;; A typical message link. Planner has the id after the final slash,
  8119. ;; we separate it with a hash mark
  8120. (setq path (concat (match-string 1 path) "#"
  8121. (org-remove-angle-brackets (match-string 2 path)))))
  8122. )
  8123. (cons type path))
  8124. (defun org-find-file-at-mouse (ev)
  8125. "Open file link or URL at mouse."
  8126. (interactive "e")
  8127. (mouse-set-point ev)
  8128. (org-open-at-point 'in-emacs))
  8129. (defun org-open-at-mouse (ev)
  8130. "Open file link or URL at mouse."
  8131. (interactive "e")
  8132. (mouse-set-point ev)
  8133. (if (eq major-mode 'org-agenda-mode)
  8134. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8135. (org-open-at-point))
  8136. (defvar org-window-config-before-follow-link nil
  8137. "The window configuration before following a link.
  8138. This is saved in case the need arises to restore it.")
  8139. (defvar org-open-link-marker (make-marker)
  8140. "Marker pointing to the location where `org-open-at-point; was called.")
  8141. ;;;###autoload
  8142. (defun org-open-at-point-global ()
  8143. "Follow a link like Org-mode does.
  8144. This command can be called in any mode to follow a link that has
  8145. Org-mode syntax."
  8146. (interactive)
  8147. (org-run-like-in-org-mode 'org-open-at-point))
  8148. ;;;###autoload
  8149. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8150. "Open a link in the string S, as if it was in Org-mode."
  8151. (interactive "sLink: \nP")
  8152. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8153. (with-temp-buffer
  8154. (let ((org-inhibit-startup t))
  8155. (org-mode)
  8156. (insert s)
  8157. (goto-char (point-min))
  8158. (when reference-buffer
  8159. (setq org-link-abbrev-alist-local
  8160. (with-current-buffer reference-buffer
  8161. org-link-abbrev-alist-local)))
  8162. (org-open-at-point arg reference-buffer)))))
  8163. (defvar org-open-at-point-functions nil
  8164. "Hook that is run when following a link at point.
  8165. Functions in this hook must return t if they identify and follow
  8166. a link at point. If they don't find anything interesting at point,
  8167. they must return nil.")
  8168. (defun org-open-at-point (&optional in-emacs reference-buffer)
  8169. "Open link at or after point.
  8170. If there is no link at point, this function will search forward up to
  8171. the end of the current line.
  8172. Normally, files will be opened by an appropriate application. If the
  8173. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  8174. With a double prefix argument, try to open outside of Emacs, in the
  8175. application the system uses for this file type."
  8176. (interactive "P")
  8177. ;; if in a code block, then open the block's results
  8178. (unless (call-interactively #'org-babel-open-src-block-result)
  8179. (org-load-modules-maybe)
  8180. (move-marker org-open-link-marker (point))
  8181. (setq org-window-config-before-follow-link (current-window-configuration))
  8182. (org-remove-occur-highlights nil nil t)
  8183. (cond
  8184. ((and (org-on-heading-p)
  8185. (not (org-in-regexp
  8186. (concat org-plain-link-re "\\|"
  8187. org-bracket-link-regexp "\\|"
  8188. org-angle-link-re "\\|"
  8189. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8190. (not (get-text-property (point) 'org-linked-text)))
  8191. (or (org-offer-links-in-entry in-emacs)
  8192. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8193. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8194. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8195. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8196. (not (org-in-regexp org-bracket-link-regexp)))
  8197. (org-footnote-action))
  8198. (t
  8199. (let (type path link line search (pos (point)))
  8200. (catch 'match
  8201. (save-excursion
  8202. (skip-chars-forward "^]\n\r")
  8203. (when (org-in-regexp org-bracket-link-regexp 1)
  8204. (setq link (org-extract-attributes
  8205. (org-link-unescape (org-match-string-no-properties 1))))
  8206. (while (string-match " *\n *" link)
  8207. (setq link (replace-match " " t t link)))
  8208. (setq link (org-link-expand-abbrev link))
  8209. (cond
  8210. ((or (file-name-absolute-p link)
  8211. (string-match "^\\.\\.?/" link))
  8212. (setq type "file" path link))
  8213. ((string-match org-link-re-with-space3 link)
  8214. (setq type (match-string 1 link) path (match-string 2 link)))
  8215. (t (setq type "thisfile" path link)))
  8216. (throw 'match t)))
  8217. (when (get-text-property (point) 'org-linked-text)
  8218. (setq type "thisfile"
  8219. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8220. (1+ (point)) (point))
  8221. path (buffer-substring
  8222. (previous-single-property-change pos 'org-linked-text)
  8223. (next-single-property-change pos 'org-linked-text)))
  8224. (throw 'match t))
  8225. (save-excursion
  8226. (when (or (org-in-regexp org-angle-link-re)
  8227. (org-in-regexp org-plain-link-re))
  8228. (setq type (match-string 1) path (match-string 2))
  8229. (throw 'match t)))
  8230. (save-excursion
  8231. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8232. (setq type "tags"
  8233. path (match-string 1))
  8234. (while (string-match ":" path)
  8235. (setq path (replace-match "+" t t path)))
  8236. (throw 'match t)))
  8237. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8238. (setq type "tree-match"
  8239. path (match-string 1))
  8240. (throw 'match t)))
  8241. (unless path
  8242. (error "No link found"))
  8243. ;; switch back to reference buffer
  8244. ;; needed when if called in a temporary buffer through
  8245. ;; org-open-link-from-string
  8246. (with-current-buffer (or reference-buffer (current-buffer))
  8247. ;; Remove any trailing spaces in path
  8248. (if (string-match " +\\'" path)
  8249. (setq path (replace-match "" t t path)))
  8250. (if (and org-link-translation-function
  8251. (fboundp org-link-translation-function))
  8252. ;; Check if we need to translate the link
  8253. (let ((tmp (funcall org-link-translation-function type path)))
  8254. (setq type (car tmp) path (cdr tmp))))
  8255. (cond
  8256. ((assoc type org-link-protocols)
  8257. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8258. ((equal type "mailto")
  8259. (let ((cmd (car org-link-mailto-program))
  8260. (args (cdr org-link-mailto-program)) args1
  8261. (address path) (subject "") a)
  8262. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8263. (setq address (match-string 1 path)
  8264. subject (org-link-escape (match-string 2 path))))
  8265. (while args
  8266. (cond
  8267. ((not (stringp (car args))) (push (pop args) args1))
  8268. (t (setq a (pop args))
  8269. (if (string-match "%a" a)
  8270. (setq a (replace-match address t t a)))
  8271. (if (string-match "%s" a)
  8272. (setq a (replace-match subject t t a)))
  8273. (push a args1))))
  8274. (apply cmd (nreverse args1))))
  8275. ((member type '("http" "https" "ftp" "news"))
  8276. (browse-url (concat type ":" (org-link-escape
  8277. path org-link-escape-chars-browser))))
  8278. ((string= type "doi")
  8279. (browse-url (concat "http://dx.doi.org/"
  8280. (org-link-escape
  8281. path org-link-escape-chars-browser))))
  8282. ((member type '("message"))
  8283. (browse-url (concat type ":" path)))
  8284. ((string= type "tags")
  8285. (org-tags-view in-emacs path))
  8286. ((string= type "tree-match")
  8287. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8288. ((string= type "file")
  8289. (if (string-match "::\\([0-9]+\\)\\'" path)
  8290. (setq line (string-to-number (match-string 1 path))
  8291. path (substring path 0 (match-beginning 0)))
  8292. (if (string-match "::\\(.+\\)\\'" path)
  8293. (setq search (match-string 1 path)
  8294. path (substring path 0 (match-beginning 0)))))
  8295. (if (string-match "[*?{]" (file-name-nondirectory path))
  8296. (dired path)
  8297. (org-open-file path in-emacs line search)))
  8298. ((string= type "shell")
  8299. (let ((cmd path))
  8300. (if (or (not org-confirm-shell-link-function)
  8301. (funcall org-confirm-shell-link-function
  8302. (format "Execute \"%s\" in shell? "
  8303. (org-add-props cmd nil
  8304. 'face 'org-warning))))
  8305. (progn
  8306. (message "Executing %s" cmd)
  8307. (shell-command cmd))
  8308. (error "Abort"))))
  8309. ((string= type "elisp")
  8310. (let ((cmd path))
  8311. (if (or (not org-confirm-elisp-link-function)
  8312. (funcall org-confirm-elisp-link-function
  8313. (format "Execute \"%s\" as elisp? "
  8314. (org-add-props cmd nil
  8315. 'face 'org-warning))))
  8316. (message "%s => %s" cmd
  8317. (if (equal (string-to-char cmd) ?\()
  8318. (eval (read cmd))
  8319. (call-interactively (read cmd))))
  8320. (error "Abort"))))
  8321. ((and (string= type "thisfile")
  8322. (run-hook-with-args-until-success
  8323. 'org-open-link-functions path)))
  8324. ((string= type "thisfile")
  8325. (if in-emacs
  8326. (switch-to-buffer-other-window
  8327. (org-get-buffer-for-internal-link (current-buffer)))
  8328. (org-mark-ring-push))
  8329. (let ((cmd `(org-link-search
  8330. ,path
  8331. ,(cond ((equal in-emacs '(4)) 'occur)
  8332. ((equal in-emacs '(16)) 'org-occur)
  8333. (t nil))
  8334. ,pos)))
  8335. (condition-case nil (eval cmd)
  8336. (error (progn (widen) (eval cmd))))))
  8337. (t
  8338. (browse-url-at-point)))))))
  8339. (move-marker org-open-link-marker nil)
  8340. (run-hook-with-args 'org-follow-link-hook)))
  8341. (defun org-offer-links-in-entry (&optional nth zero)
  8342. "Offer links in the current entry and follow the selected link.
  8343. If there is only one link, follow it immediately as well.
  8344. If NTH is an integer, immediately pick the NTH link found.
  8345. If ZERO is a string, check also this string for a link, and if
  8346. there is one, offer it as link number zero."
  8347. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8348. "\\(" org-angle-link-re "\\)\\|"
  8349. "\\(" org-plain-link-re "\\)"))
  8350. (cnt ?0)
  8351. (in-emacs (if (integerp nth) nil nth))
  8352. have-zero end links link c)
  8353. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8354. (push (match-string 0 zero) links)
  8355. (setq cnt (1- cnt) have-zero t))
  8356. (save-excursion
  8357. (org-back-to-heading t)
  8358. (setq end (save-excursion (outline-next-heading) (point)))
  8359. (while (re-search-forward re end t)
  8360. (push (match-string 0) links))
  8361. (setq links (org-uniquify (reverse links))))
  8362. (cond
  8363. ((null links)
  8364. (message "No links"))
  8365. ((equal (length links) 1)
  8366. (setq link (list (car links))))
  8367. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8368. (setq link (nth (if have-zero nth (1- nth)) links)))
  8369. (t ; we have to select a link
  8370. (save-excursion
  8371. (save-window-excursion
  8372. (delete-other-windows)
  8373. (with-output-to-temp-buffer "*Select Link*"
  8374. (mapc (lambda (l)
  8375. (if (not (string-match org-bracket-link-regexp l))
  8376. (princ (format "[%c] %s\n" (incf cnt)
  8377. (org-remove-angle-brackets l)))
  8378. (if (match-end 3)
  8379. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8380. (match-string 3 l) (match-string 1 l)))
  8381. (princ (format "[%c] %s\n" (incf cnt)
  8382. (match-string 1 l))))))
  8383. links))
  8384. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8385. (message "Select link to open, RET to open all:")
  8386. (setq c (read-char-exclusive))
  8387. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8388. (when (equal c ?q) (error "Abort"))
  8389. (if (equal c ?\C-m)
  8390. (setq link links)
  8391. (setq nth (- c ?0))
  8392. (if have-zero (setq nth (1+ nth)))
  8393. (unless (and (integerp nth) (>= (length links) nth))
  8394. (error "Invalid link selection"))
  8395. (setq link (list (nth (1- nth) links))))))
  8396. (if link
  8397. (let ((buf (current-buffer)))
  8398. (dolist (l link)
  8399. (org-open-link-from-string l in-emacs buf))
  8400. t)
  8401. nil)))
  8402. ;; Add special file links that specify the way of opening
  8403. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8404. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8405. (defun org-open-file-with-system (path)
  8406. "Open file at PATH using the system way of opening it."
  8407. (org-open-file path 'system))
  8408. (defun org-open-file-with-emacs (path)
  8409. "Open file at PATH in Emacs."
  8410. (org-open-file path 'emacs))
  8411. (defun org-remove-file-link-modifiers ()
  8412. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8413. (goto-char (point-min))
  8414. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8415. (org-if-unprotected
  8416. (replace-match "file:" t t))))
  8417. (eval-after-load "org-exp"
  8418. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8419. 'org-remove-file-link-modifiers))
  8420. ;;;; Time estimates
  8421. (defun org-get-effort (&optional pom)
  8422. "Get the effort estimate for the current entry."
  8423. (org-entry-get pom org-effort-property))
  8424. ;;; File search
  8425. (defvar org-create-file-search-functions nil
  8426. "List of functions to construct the right search string for a file link.
  8427. These functions are called in turn with point at the location to
  8428. which the link should point.
  8429. A function in the hook should first test if it would like to
  8430. handle this file type, for example by checking the `major-mode'
  8431. or the file extension. If it decides not to handle this file, it
  8432. should just return nil to give other functions a chance. If it
  8433. does handle the file, it must return the search string to be used
  8434. when following the link. The search string will be part of the
  8435. file link, given after a double colon, and `org-open-at-point'
  8436. will automatically search for it. If special measures must be
  8437. taken to make the search successful, another function should be
  8438. added to the companion hook `org-execute-file-search-functions',
  8439. which see.
  8440. A function in this hook may also use `setq' to set the variable
  8441. `description' to provide a suggestion for the descriptive text to
  8442. be used for this link when it gets inserted into an Org-mode
  8443. buffer with \\[org-insert-link].")
  8444. (defvar org-execute-file-search-functions nil
  8445. "List of functions to execute a file search triggered by a link.
  8446. Functions added to this hook must accept a single argument, the
  8447. search string that was part of the file link, the part after the
  8448. double colon. The function must first check if it would like to
  8449. handle this search, for example by checking the `major-mode' or
  8450. the file extension. If it decides not to handle this search, it
  8451. should just return nil to give other functions a chance. If it
  8452. does handle the search, it must return a non-nil value to keep
  8453. other functions from trying.
  8454. Each function can access the current prefix argument through the
  8455. variable `current-prefix-argument'. Note that a single prefix is
  8456. used to force opening a link in Emacs, so it may be good to only
  8457. use a numeric or double prefix to guide the search function.
  8458. In case this is needed, a function in this hook can also restore
  8459. the window configuration before `org-open-at-point' was called using:
  8460. (set-window-configuration org-window-config-before-follow-link)")
  8461. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8462. (defun org-link-search (s &optional type avoid-pos)
  8463. "Search for a link search option.
  8464. If S is surrounded by forward slashes, it is interpreted as a
  8465. regular expression. In org-mode files, this will create an `org-occur'
  8466. sparse tree. In ordinary files, `occur' will be used to list matches.
  8467. If the current buffer is in `dired-mode', grep will be used to search
  8468. in all files. If AVOID-POS is given, ignore matches near that position."
  8469. (let ((case-fold-search t)
  8470. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8471. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8472. (append '(("") (" ") ("\t") ("\n"))
  8473. org-emphasis-alist)
  8474. "\\|") "\\)"))
  8475. (pos (point))
  8476. (pre nil) (post nil)
  8477. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8478. (cond
  8479. ;; First check if there are any special search functions
  8480. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8481. ;; Now try the builtin stuff
  8482. ((and (equal (string-to-char s0) ?#)
  8483. (> (length s0) 1)
  8484. (save-excursion
  8485. (goto-char (point-min))
  8486. (and
  8487. (re-search-forward
  8488. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8489. (setq type 'dedicated
  8490. pos (match-beginning 0))))
  8491. ;; There is an exact target for this
  8492. (goto-char pos)
  8493. (org-back-to-heading t)))
  8494. ((save-excursion
  8495. (goto-char (point-min))
  8496. (and
  8497. (re-search-forward
  8498. (concat "<<" (regexp-quote s0) ">>") nil t)
  8499. (setq type 'dedicated
  8500. pos (match-beginning 0))))
  8501. ;; There is an exact target for this
  8502. (goto-char pos))
  8503. ((and (string-match "^(\\(.*\\))$" s0)
  8504. (save-excursion
  8505. (goto-char (point-min))
  8506. (and
  8507. (re-search-forward
  8508. (concat "[^[]" (regexp-quote
  8509. (format org-coderef-label-format
  8510. (match-string 1 s0))))
  8511. nil t)
  8512. (setq type 'dedicated
  8513. pos (1+ (match-beginning 0))))))
  8514. ;; There is a coderef target for this
  8515. (goto-char pos))
  8516. ((string-match "^/\\(.*\\)/$" s)
  8517. ;; A regular expression
  8518. (cond
  8519. ((org-mode-p)
  8520. (org-occur (match-string 1 s)))
  8521. ;;((eq major-mode 'dired-mode)
  8522. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8523. (t (org-do-occur (match-string 1 s)))))
  8524. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8525. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8526. (goto-char (point-min))
  8527. (cond
  8528. ((let (case-fold-search)
  8529. (re-search-forward (format org-complex-heading-regexp-format
  8530. (regexp-quote s))
  8531. nil t))
  8532. ;; OK, found a match
  8533. (setq type 'dedicated)
  8534. (goto-char (match-beginning 0)))
  8535. ((and (not org-link-search-inhibit-query)
  8536. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8537. (y-or-n-p "No match - create this as a new heading? "))
  8538. (goto-char (point-max))
  8539. (or (bolp) (newline))
  8540. (insert "* " s "\n")
  8541. (beginning-of-line 0))
  8542. (t
  8543. (goto-char pos)
  8544. (error "No match"))))
  8545. (t
  8546. ;; A normal search string
  8547. (when (equal (string-to-char s) ?*)
  8548. ;; Anchor on headlines, post may include tags.
  8549. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8550. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8551. s (substring s 1)))
  8552. (remove-text-properties
  8553. 0 (length s)
  8554. '(face nil mouse-face nil keymap nil fontified nil) s)
  8555. ;; Make a series of regular expressions to find a match
  8556. (setq words (org-split-string s "[ \n\r\t]+")
  8557. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8558. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8559. "\\)" markers)
  8560. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8561. re2a (concat "[ \t\r\n]" re2a_)
  8562. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8563. re4 (concat "[^a-zA-Z_]" re4_)
  8564. re1 (concat pre re2 post)
  8565. re3 (concat pre (if pre re4_ re4) post)
  8566. re5 (concat pre ".*" re4)
  8567. re2 (concat pre re2)
  8568. re2a (concat pre (if pre re2a_ re2a))
  8569. re4 (concat pre (if pre re4_ re4))
  8570. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8571. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8572. re5 "\\)"
  8573. ))
  8574. (cond
  8575. ((eq type 'org-occur) (org-occur reall))
  8576. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8577. (t (goto-char (point-min))
  8578. (setq type 'fuzzy)
  8579. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8580. (org-search-not-self 1 re1 nil t)
  8581. (org-search-not-self 1 re2 nil t)
  8582. (org-search-not-self 1 re2a nil t)
  8583. (org-search-not-self 1 re3 nil t)
  8584. (org-search-not-self 1 re4 nil t)
  8585. (org-search-not-self 1 re5 nil t)
  8586. )
  8587. (goto-char (match-beginning 1))
  8588. (goto-char pos)
  8589. (error "No match"))))))
  8590. (and (org-mode-p) (org-show-context 'link-search))
  8591. type))
  8592. (defun org-search-not-self (group &rest args)
  8593. "Execute `re-search-forward', but only accept matches that do not
  8594. enclose the position of `org-open-link-marker'."
  8595. (let ((m org-open-link-marker))
  8596. (catch 'exit
  8597. (while (apply 're-search-forward args)
  8598. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8599. (goto-char (match-end group))
  8600. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8601. (> (match-beginning 0) (marker-position m))
  8602. (< (match-end 0) (marker-position m)))
  8603. (save-match-data
  8604. (or (not (org-in-regexp
  8605. org-bracket-link-analytic-regexp 1))
  8606. (not (match-end 4)) ; no description
  8607. (and (<= (match-beginning 4) (point))
  8608. (>= (match-end 4) (point))))))
  8609. (throw 'exit (point))))))))
  8610. (defun org-get-buffer-for-internal-link (buffer)
  8611. "Return a buffer to be used for displaying the link target of internal links."
  8612. (cond
  8613. ((not org-display-internal-link-with-indirect-buffer)
  8614. buffer)
  8615. ((string-match "(Clone)$" (buffer-name buffer))
  8616. (message "Buffer is already a clone, not making another one")
  8617. ;; we also do not modify visibility in this case
  8618. buffer)
  8619. (t ; make a new indirect buffer for displaying the link
  8620. (let* ((bn (buffer-name buffer))
  8621. (ibn (concat bn "(Clone)"))
  8622. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8623. (with-current-buffer ib (org-overview))
  8624. ib))))
  8625. (defun org-do-occur (regexp &optional cleanup)
  8626. "Call the Emacs command `occur'.
  8627. If CLEANUP is non-nil, remove the printout of the regular expression
  8628. in the *Occur* buffer. This is useful if the regex is long and not useful
  8629. to read."
  8630. (occur regexp)
  8631. (when cleanup
  8632. (let ((cwin (selected-window)) win beg end)
  8633. (when (setq win (get-buffer-window "*Occur*"))
  8634. (select-window win))
  8635. (goto-char (point-min))
  8636. (when (re-search-forward "match[a-z]+" nil t)
  8637. (setq beg (match-end 0))
  8638. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8639. (setq end (1- (match-beginning 0)))))
  8640. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8641. (goto-char (point-min))
  8642. (select-window cwin))))
  8643. ;;; The mark ring for links jumps
  8644. (defvar org-mark-ring nil
  8645. "Mark ring for positions before jumps in Org-mode.")
  8646. (defvar org-mark-ring-last-goto nil
  8647. "Last position in the mark ring used to go back.")
  8648. ;; Fill and close the ring
  8649. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8650. (loop for i from 1 to org-mark-ring-length do
  8651. (push (make-marker) org-mark-ring))
  8652. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8653. org-mark-ring)
  8654. (defun org-mark-ring-push (&optional pos buffer)
  8655. "Put the current position or POS into the mark ring and rotate it."
  8656. (interactive)
  8657. (setq pos (or pos (point)))
  8658. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8659. (move-marker (car org-mark-ring)
  8660. (or pos (point))
  8661. (or buffer (current-buffer)))
  8662. (message "%s"
  8663. (substitute-command-keys
  8664. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8665. (defun org-mark-ring-goto (&optional n)
  8666. "Jump to the previous position in the mark ring.
  8667. With prefix arg N, jump back that many stored positions. When
  8668. called several times in succession, walk through the entire ring.
  8669. Org-mode commands jumping to a different position in the current file,
  8670. or to another Org-mode file, automatically push the old position
  8671. onto the ring."
  8672. (interactive "p")
  8673. (let (p m)
  8674. (if (eq last-command this-command)
  8675. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8676. (setq p org-mark-ring))
  8677. (setq org-mark-ring-last-goto p)
  8678. (setq m (car p))
  8679. (switch-to-buffer (marker-buffer m))
  8680. (goto-char m)
  8681. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8682. (defun org-remove-angle-brackets (s)
  8683. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8684. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8685. s)
  8686. (defun org-add-angle-brackets (s)
  8687. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8688. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8689. s)
  8690. (defun org-remove-double-quotes (s)
  8691. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8692. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8693. s)
  8694. ;;; Following specific links
  8695. (defun org-follow-timestamp-link ()
  8696. (cond
  8697. ((org-at-date-range-p t)
  8698. (let ((org-agenda-start-on-weekday)
  8699. (t1 (match-string 1))
  8700. (t2 (match-string 2)))
  8701. (setq t1 (time-to-days (org-time-string-to-time t1))
  8702. t2 (time-to-days (org-time-string-to-time t2)))
  8703. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8704. ((org-at-timestamp-p t)
  8705. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8706. (substring (match-string 1) 0 10)))
  8707. 1))
  8708. (t (error "This should not happen"))))
  8709. ;;; Following file links
  8710. (defvar org-wait nil)
  8711. (defun org-open-file (path &optional in-emacs line search)
  8712. "Open the file at PATH.
  8713. First, this expands any special file name abbreviations. Then the
  8714. configuration variable `org-file-apps' is checked if it contains an
  8715. entry for this file type, and if yes, the corresponding command is launched.
  8716. If no application is found, Emacs simply visits the file.
  8717. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8718. With a double \\[universal-argument] \\[universal-argument] \
  8719. prefix arg, Org tries to avoid opening in Emacs
  8720. and to use an external application to visit the file.
  8721. Optional LINE specifies a line to go to, optional SEARCH a string
  8722. to search for. If LINE or SEARCH is given, the file will be
  8723. opened in Emacs, unless an entry from org-file-apps that makes
  8724. use of groups in a regexp matches.
  8725. If the file does not exist, an error is thrown."
  8726. (let* ((file (if (equal path "")
  8727. buffer-file-name
  8728. (substitute-in-file-name (expand-file-name path))))
  8729. (file-apps (append org-file-apps (org-default-apps)))
  8730. (apps (org-remove-if
  8731. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8732. (apps-dlink (org-remove-if-not
  8733. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8734. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8735. (dirp (if remp nil (file-directory-p file)))
  8736. (file (if (and dirp org-open-directory-means-index-dot-org)
  8737. (concat (file-name-as-directory file) "index.org")
  8738. file))
  8739. (a-m-a-p (assq 'auto-mode apps))
  8740. (dfile (downcase file))
  8741. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8742. (link (cond ((and (eq line nil)
  8743. (eq search nil))
  8744. file)
  8745. (line
  8746. (concat file "::" (number-to-string line)))
  8747. (search
  8748. (concat file "::" search))))
  8749. (dlink (downcase link))
  8750. (old-buffer (current-buffer))
  8751. (old-pos (point))
  8752. (old-mode major-mode)
  8753. ext cmd link-match-data)
  8754. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8755. (setq ext (match-string 1 dfile))
  8756. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8757. (setq ext (match-string 1 dfile))))
  8758. (cond
  8759. ((member in-emacs '((16) system))
  8760. (setq cmd (cdr (assoc 'system apps))))
  8761. (in-emacs (setq cmd 'emacs))
  8762. (t
  8763. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8764. (and dirp (cdr (assoc 'directory apps)))
  8765. ; first, try matching against apps-dlink
  8766. ; if we get a match here, store the match data for later
  8767. (let ((match (assoc-default dlink apps-dlink
  8768. 'string-match)))
  8769. (if match
  8770. (progn (setq link-match-data (match-data))
  8771. match)
  8772. (progn (setq in-emacs (or in-emacs line search))
  8773. nil))) ; if we have no match in apps-dlink,
  8774. ; always open the file in emacs if line or search
  8775. ; is given (for backwards compatibility)
  8776. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8777. 'string-match)
  8778. (cdr (assoc ext apps))
  8779. (cdr (assoc t apps))))))
  8780. (when (eq cmd 'system)
  8781. (setq cmd (cdr (assoc 'system apps))))
  8782. (when (eq cmd 'default)
  8783. (setq cmd (cdr (assoc t apps))))
  8784. (when (eq cmd 'mailcap)
  8785. (require 'mailcap)
  8786. (mailcap-parse-mailcaps)
  8787. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8788. (command (mailcap-mime-info mime-type)))
  8789. (if (stringp command)
  8790. (setq cmd command)
  8791. (setq cmd 'emacs))))
  8792. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8793. (not (file-exists-p file))
  8794. (not org-open-non-existing-files))
  8795. (error "No such file: %s" file))
  8796. (cond
  8797. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8798. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8799. (while (string-match "['\"]%s['\"]" cmd)
  8800. (setq cmd (replace-match "%s" t t cmd)))
  8801. (while (string-match "%s" cmd)
  8802. (setq cmd (replace-match
  8803. (save-match-data
  8804. (shell-quote-argument
  8805. (convert-standard-filename file)))
  8806. t t cmd)))
  8807. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8808. (save-match-data
  8809. (let ((match-index 1)
  8810. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8811. (set-match-data link-match-data)
  8812. (while (<= match-index number-of-groups)
  8813. (let ((regex (concat "%" (number-to-string match-index)))
  8814. (replace-with (match-string match-index dlink)))
  8815. (while (string-match regex cmd)
  8816. (setq cmd (replace-match replace-with t t cmd))))
  8817. (setq match-index (+ match-index 1)))))
  8818. (save-window-excursion
  8819. (start-process-shell-command cmd nil cmd)
  8820. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8821. ))
  8822. ((or (stringp cmd)
  8823. (eq cmd 'emacs))
  8824. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8825. (widen)
  8826. (if line (org-goto-line line)
  8827. (if search (org-link-search search))))
  8828. ((consp cmd)
  8829. (let ((file (convert-standard-filename file)))
  8830. (save-match-data
  8831. (set-match-data link-match-data)
  8832. (eval cmd))))
  8833. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8834. (and (org-mode-p) (eq old-mode 'org-mode)
  8835. (or (not (equal old-buffer (current-buffer)))
  8836. (not (equal old-pos (point))))
  8837. (org-mark-ring-push old-pos old-buffer))))
  8838. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8839. "This function returns non-nil if `entry' uses a regular
  8840. expression which should be matched against the whole link by
  8841. org-open-file.
  8842. It assumes that is the case when the entry uses a regular
  8843. expression which has at least one grouping construct and the
  8844. action is either a lisp form or a command string containing
  8845. '%1', i.e. using at least one subexpression match as a
  8846. parameter."
  8847. (let ((selector (car entry))
  8848. (action (cdr entry)))
  8849. (if (stringp selector)
  8850. (and (> (regexp-opt-depth selector) 0)
  8851. (or (and (stringp action)
  8852. (string-match "%[0-9]" action))
  8853. (consp action)))
  8854. nil)))
  8855. (defun org-default-apps ()
  8856. "Return the default applications for this operating system."
  8857. (cond
  8858. ((eq system-type 'darwin)
  8859. org-file-apps-defaults-macosx)
  8860. ((eq system-type 'windows-nt)
  8861. org-file-apps-defaults-windowsnt)
  8862. (t org-file-apps-defaults-gnu)))
  8863. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8864. "Convert extensions to regular expressions in the cars of LIST.
  8865. Also, weed out any non-string entries, because the return value is used
  8866. only for regexp matching.
  8867. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8868. point to the symbol `emacs', indicating that the file should
  8869. be opened in Emacs."
  8870. (append
  8871. (delq nil
  8872. (mapcar (lambda (x)
  8873. (if (not (stringp (car x)))
  8874. nil
  8875. (if (string-match "\\W" (car x))
  8876. x
  8877. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8878. list))
  8879. (if add-auto-mode
  8880. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8881. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8882. (defun org-file-remote-p (file)
  8883. "Test whether FILE specifies a location on a remote system.
  8884. Return non-nil if the location is indeed remote.
  8885. For example, the filename \"/user@host:/foo\" specifies a location
  8886. on the system \"/user@host:\"."
  8887. (cond ((fboundp 'file-remote-p)
  8888. (file-remote-p file))
  8889. ((fboundp 'tramp-handle-file-remote-p)
  8890. (tramp-handle-file-remote-p file))
  8891. ((and (boundp 'ange-ftp-name-format)
  8892. (string-match (car ange-ftp-name-format) file))
  8893. t)
  8894. (t nil)))
  8895. ;;;; Refiling
  8896. (defun org-get-org-file ()
  8897. "Read a filename, with default directory `org-directory'."
  8898. (let ((default (or org-default-notes-file remember-data-file)))
  8899. (read-file-name (format "File name [%s]: " default)
  8900. (file-name-as-directory org-directory)
  8901. default)))
  8902. (defun org-notes-order-reversed-p ()
  8903. "Check if the current file should receive notes in reversed order."
  8904. (cond
  8905. ((not org-reverse-note-order) nil)
  8906. ((eq t org-reverse-note-order) t)
  8907. ((not (listp org-reverse-note-order)) nil)
  8908. (t (catch 'exit
  8909. (let ((all org-reverse-note-order)
  8910. entry)
  8911. (while (setq entry (pop all))
  8912. (if (string-match (car entry) buffer-file-name)
  8913. (throw 'exit (cdr entry))))
  8914. nil)))))
  8915. (defvar org-refile-target-table nil
  8916. "The list of refile targets, created by `org-refile'.")
  8917. (defvar org-agenda-new-buffers nil
  8918. "Buffers created to visit agenda files.")
  8919. (defvar org-refile-cache nil
  8920. "Cache for refile targets.")
  8921. (defvar org-refile-markers nil
  8922. "All the markers used for caching refile locations.")
  8923. (defun org-refile-marker (pos)
  8924. "Get a new refile marker, but only if caching is in use."
  8925. (if (not org-refile-use-cache)
  8926. pos
  8927. (let ((m (make-marker)))
  8928. (move-marker m pos)
  8929. (push m org-refile-markers)
  8930. m)))
  8931. (defun org-refile-cache-clear ()
  8932. "Clear the refile cache and disable all the markers."
  8933. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8934. (setq org-refile-markers nil)
  8935. (setq org-refile-cache nil)
  8936. (message "Refile cache has been cleared"))
  8937. (defun org-refile-cache-check-set (set)
  8938. "Check if all the markers in the cache still have live buffers."
  8939. (let (marker)
  8940. (catch 'exit
  8941. (while (and set (setq marker (nth 3 (pop set))))
  8942. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8943. (when (and marker (null (marker-buffer marker)))
  8944. (message "not found") (sit-for 3)
  8945. (throw 'exit nil)))
  8946. t)))
  8947. (defun org-refile-cache-put (set &rest identifiers)
  8948. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8949. (let* ((key (sha1 (prin1-to-string identifiers)))
  8950. (entry (assoc key org-refile-cache)))
  8951. (if entry
  8952. (setcdr entry set)
  8953. (push (cons key set) org-refile-cache))))
  8954. (defun org-refile-cache-get (&rest identifiers)
  8955. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8956. (cond
  8957. ((not org-refile-cache) nil)
  8958. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8959. (t
  8960. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8961. org-refile-cache))))
  8962. (and set (org-refile-cache-check-set set) set)))))
  8963. (defun org-get-refile-targets (&optional default-buffer)
  8964. "Produce a table with refile targets."
  8965. (let ((case-fold-search nil)
  8966. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8967. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8968. targets tgs txt re files f desc descre fast-path-p level pos0)
  8969. (message "Getting targets...")
  8970. (with-current-buffer (or default-buffer (current-buffer))
  8971. (while (setq entry (pop entries))
  8972. (setq files (car entry) desc (cdr entry))
  8973. (setq fast-path-p nil)
  8974. (cond
  8975. ((null files) (setq files (list (current-buffer))))
  8976. ((eq files 'org-agenda-files)
  8977. (setq files (org-agenda-files 'unrestricted)))
  8978. ((and (symbolp files) (fboundp files))
  8979. (setq files (funcall files)))
  8980. ((and (symbolp files) (boundp files))
  8981. (setq files (symbol-value files))))
  8982. (if (stringp files) (setq files (list files)))
  8983. (cond
  8984. ((eq (car desc) :tag)
  8985. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8986. ((eq (car desc) :todo)
  8987. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8988. ((eq (car desc) :regexp)
  8989. (setq descre (cdr desc)))
  8990. ((eq (car desc) :level)
  8991. (setq descre (concat "^\\*\\{" (number-to-string
  8992. (if org-odd-levels-only
  8993. (1- (* 2 (cdr desc)))
  8994. (cdr desc)))
  8995. "\\}[ \t]")))
  8996. ((eq (car desc) :maxlevel)
  8997. (setq fast-path-p t)
  8998. (setq descre (concat "^\\*\\{1," (number-to-string
  8999. (if org-odd-levels-only
  9000. (1- (* 2 (cdr desc)))
  9001. (cdr desc)))
  9002. "\\}[ \t]")))
  9003. (t (error "Bad refiling target description %s" desc)))
  9004. (while (setq f (pop files))
  9005. (with-current-buffer
  9006. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9007. (or
  9008. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9009. (progn
  9010. (if (bufferp f) (setq f (buffer-file-name
  9011. (buffer-base-buffer f))))
  9012. (setq f (and f (expand-file-name f)))
  9013. (if (eq org-refile-use-outline-path 'file)
  9014. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9015. (save-excursion
  9016. (save-restriction
  9017. (widen)
  9018. (goto-char (point-min))
  9019. (while (re-search-forward descre nil t)
  9020. (goto-char (setq pos0 (point-at-bol)))
  9021. (catch 'next
  9022. (when org-refile-target-verify-function
  9023. (save-match-data
  9024. (or (funcall org-refile-target-verify-function)
  9025. (throw 'next t))))
  9026. (when (looking-at org-complex-heading-regexp)
  9027. (setq level (org-reduced-level
  9028. (- (match-end 1) (match-beginning 1)))
  9029. txt (org-link-display-format (match-string 4))
  9030. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9031. re (format org-complex-heading-regexp-format
  9032. (regexp-quote (match-string 4))))
  9033. (when org-refile-use-outline-path
  9034. (setq txt (mapconcat
  9035. 'org-protect-slash
  9036. (append
  9037. (if (eq org-refile-use-outline-path
  9038. 'file)
  9039. (list (file-name-nondirectory
  9040. (buffer-file-name
  9041. (buffer-base-buffer))))
  9042. (if (eq org-refile-use-outline-path
  9043. 'full-file-path)
  9044. (list (buffer-file-name
  9045. (buffer-base-buffer)))))
  9046. (org-get-outline-path fast-path-p
  9047. level txt)
  9048. (list txt))
  9049. "/")))
  9050. (push (list txt f re (org-refile-marker (point)))
  9051. tgs)))
  9052. (when (= (point) pos0)
  9053. ;; verification function has not moved point
  9054. (goto-char (point-at-eol))))))))
  9055. (when org-refile-use-cache
  9056. (org-refile-cache-put tgs (buffer-file-name) descre))
  9057. (setq targets (append tgs targets))
  9058. ))))
  9059. (message "Getting targets...done")
  9060. (nreverse targets)))
  9061. (defun org-protect-slash (s)
  9062. (while (string-match "/" s)
  9063. (setq s (replace-match "\\" t t s)))
  9064. s)
  9065. (defvar org-olpa (make-vector 20 nil))
  9066. (defun org-get-outline-path (&optional fastp level heading)
  9067. "Return the outline path to the current entry, as a list.
  9068. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9069. routine which makes outline path derivations for an entire file,
  9070. avoiding backtracing. Refile target collection makes use of that."
  9071. (if fastp
  9072. (progn
  9073. (if (> level 19)
  9074. (error "Outline path failure, more than 19 levels"))
  9075. (loop for i from level upto 19 do
  9076. (aset org-olpa i nil))
  9077. (prog1
  9078. (delq nil (append org-olpa nil))
  9079. (aset org-olpa level heading)))
  9080. (let (rtn case-fold-search)
  9081. (save-excursion
  9082. (save-restriction
  9083. (widen)
  9084. (while (org-up-heading-safe)
  9085. (when (looking-at org-complex-heading-regexp)
  9086. (push (org-match-string-no-properties 4) rtn)))
  9087. rtn)))))
  9088. (defun org-format-outline-path (path &optional width prefix)
  9089. "Format the outline path PATH for display.
  9090. Width is the maximum number of characters that is available.
  9091. Prefix is a prefix to be included in the returned string,
  9092. such as the file name."
  9093. (setq width (or width 79))
  9094. (if prefix (setq width (- width (length prefix))))
  9095. (if (not path)
  9096. (or prefix "")
  9097. (let* ((nsteps (length path))
  9098. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9099. (maxwidth (if (<= total-width width)
  9100. 10000 ;; everything fits
  9101. ;; we need to shorten the level headings
  9102. (/ (- width nsteps) nsteps)))
  9103. (org-odd-levels-only nil)
  9104. (n 0)
  9105. (total (1+ (length prefix))))
  9106. (setq maxwidth (max maxwidth 10))
  9107. (concat prefix
  9108. (mapconcat
  9109. (lambda (h)
  9110. (setq n (1+ n))
  9111. (if (and (= n nsteps) (< maxwidth 10000))
  9112. (setq maxwidth (- total-width total)))
  9113. (if (< (length h) maxwidth)
  9114. (progn (setq total (+ total (length h) 1)) h)
  9115. (setq h (substring h 0 (- maxwidth 2))
  9116. total (+ total maxwidth 1))
  9117. (if (string-match "[ \t]+\\'" h)
  9118. (setq h (substring h 0 (match-beginning 0))))
  9119. (setq h (concat h "..")))
  9120. (org-add-props h nil 'face
  9121. (nth (% (1- n) org-n-level-faces)
  9122. org-level-faces))
  9123. h)
  9124. path "/")))))
  9125. (defun org-display-outline-path (&optional file current)
  9126. "Display the current outline path in the echo area."
  9127. (interactive "P")
  9128. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9129. (case-fold-search nil)
  9130. (path (and (org-mode-p) (org-get-outline-path))))
  9131. (if current (setq path (append path
  9132. (save-excursion
  9133. (org-back-to-heading t)
  9134. (if (looking-at org-complex-heading-regexp)
  9135. (list (match-string 4)))))))
  9136. (message "%s"
  9137. (org-format-outline-path
  9138. path
  9139. (1- (frame-width))
  9140. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9141. (defvar org-refile-history nil
  9142. "History for refiling operations.")
  9143. (defvar org-after-refile-insert-hook nil
  9144. "Hook run after `org-refile' has inserted its stuff at the new location.
  9145. Note that this is still *before* the stuff will be removed from
  9146. the *old* location.")
  9147. (defvar org-capture-last-stored-marker)
  9148. (defun org-refile (&optional goto default-buffer rfloc)
  9149. "Move the entry at point to another heading.
  9150. The list of target headings is compiled using the information in
  9151. `org-refile-targets', which see. This list is created before each use
  9152. and will therefore always be up-to-date.
  9153. At the target location, the entry is filed as a subitem of the target heading.
  9154. Depending on `org-reverse-note-order', the new subitem will either be the
  9155. first or the last subitem.
  9156. If there is an active region, all entries in that region will be moved.
  9157. However, the region must fulfill the requirement that the first heading
  9158. is the first one sets the top-level of the moved text - at most siblings
  9159. below it are allowed.
  9160. With prefix arg GOTO, the command will only visit the target location,
  9161. not actually move anything.
  9162. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9163. go to the location where the last refiling
  9164. operation has put the subtree.
  9165. With a prefix argument of `2', refile to the running clock.
  9166. RFLOC can be a refile location obtained in a different way.
  9167. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9168. If you are using target caching (see `org-refile-use-cache'),
  9169. You have to clear the target cache in order to find new targets.
  9170. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9171. (interactive "P")
  9172. (if (member goto '(0 (64)))
  9173. (org-refile-cache-clear)
  9174. (let* ((cbuf (current-buffer))
  9175. (regionp (org-region-active-p))
  9176. (region-start (and regionp (region-beginning)))
  9177. (region-end (and regionp (region-end)))
  9178. (region-length (and regionp (- region-end region-start)))
  9179. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9180. pos it nbuf file re level reversed)
  9181. (setq last-command nil)
  9182. (when regionp
  9183. (goto-char region-start)
  9184. (or (bolp) (goto-char (point-at-bol)))
  9185. (setq region-start (point))
  9186. (unless (org-kill-is-subtree-p
  9187. (buffer-substring region-start region-end))
  9188. (error "The region is not a (sequence of) subtree(s)")))
  9189. (if (equal goto '(16))
  9190. (org-refile-goto-last-stored)
  9191. (when (or
  9192. (and (equal goto 2)
  9193. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9194. (prog1
  9195. (setq it (list (or org-clock-heading "running clock")
  9196. (buffer-file-name
  9197. (marker-buffer org-clock-hd-marker))
  9198. ""
  9199. (marker-position org-clock-hd-marker)))
  9200. (setq goto nil)))
  9201. (setq it (or rfloc
  9202. (save-excursion
  9203. (org-refile-get-location
  9204. (if goto "Goto: " "Refile to: ") default-buffer
  9205. org-refile-allow-creating-parent-nodes)))))
  9206. (setq file (nth 1 it)
  9207. re (nth 2 it)
  9208. pos (nth 3 it))
  9209. (if (and (not goto)
  9210. pos
  9211. (equal (buffer-file-name) file)
  9212. (if regionp
  9213. (and (>= pos region-start)
  9214. (<= pos region-end))
  9215. (and (>= pos (point))
  9216. (< pos (save-excursion
  9217. (org-end-of-subtree t t))))))
  9218. (error "Cannot refile to position inside the tree or region"))
  9219. (setq nbuf (or (find-buffer-visiting file)
  9220. (find-file-noselect file)))
  9221. (if goto
  9222. (progn
  9223. (switch-to-buffer nbuf)
  9224. (goto-char pos)
  9225. (org-show-context 'org-goto))
  9226. (if regionp
  9227. (progn
  9228. (org-kill-new (buffer-substring region-start region-end))
  9229. (org-save-markers-in-region region-start region-end))
  9230. (org-copy-subtree 1 nil t))
  9231. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9232. (find-file-noselect file)))
  9233. (setq reversed (org-notes-order-reversed-p))
  9234. (save-excursion
  9235. (save-restriction
  9236. (widen)
  9237. (if pos
  9238. (progn
  9239. (goto-char pos)
  9240. (looking-at outline-regexp)
  9241. (setq level (org-get-valid-level (funcall outline-level) 1))
  9242. (goto-char
  9243. (if reversed
  9244. (or (outline-next-heading) (point-max))
  9245. (or (save-excursion (org-get-next-sibling))
  9246. (org-end-of-subtree t t)
  9247. (point-max)))))
  9248. (setq level 1)
  9249. (if (not reversed)
  9250. (goto-char (point-max))
  9251. (goto-char (point-min))
  9252. (or (outline-next-heading) (goto-char (point-max)))))
  9253. (if (not (bolp)) (newline))
  9254. (org-paste-subtree level)
  9255. (when org-log-refile
  9256. (org-add-log-setup 'refile nil nil 'findpos
  9257. org-log-refile)
  9258. (unless (eq org-log-refile 'note)
  9259. (save-excursion (org-add-log-note))))
  9260. (and org-auto-align-tags (org-set-tags nil t))
  9261. (bookmark-set "org-refile-last-stored")
  9262. ;; If we are refiling for capture, make sure that the
  9263. ;; last-capture pointers point here
  9264. (when (org-bound-and-true-p org-refile-for-capture)
  9265. (bookmark-set "org-capture-last-stored-marker")
  9266. (move-marker org-capture-last-stored-marker (point)))
  9267. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9268. (run-hooks 'org-after-refile-insert-hook))))
  9269. (if regionp
  9270. (delete-region (point) (+ (point) region-length))
  9271. (org-cut-subtree))
  9272. (when (featurep 'org-inlinetask)
  9273. (org-inlinetask-remove-END-maybe))
  9274. (setq org-markers-to-move nil)
  9275. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9276. (defun org-refile-goto-last-stored ()
  9277. "Go to the location where the last refile was stored."
  9278. (interactive)
  9279. (bookmark-jump "org-refile-last-stored")
  9280. (message "This is the location of the last refile"))
  9281. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9282. "Prompt the user for a refile location, using PROMPT."
  9283. (let ((org-refile-targets org-refile-targets)
  9284. (org-refile-use-outline-path org-refile-use-outline-path))
  9285. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  9286. (unless org-refile-target-table
  9287. (error "No refile targets"))
  9288. (let* ((cbuf (current-buffer))
  9289. (partial-completion-mode nil)
  9290. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9291. (cfunc (if (and org-refile-use-outline-path
  9292. org-outline-path-complete-in-steps)
  9293. 'org-olpath-completing-read
  9294. 'org-icompleting-read))
  9295. (extra (if org-refile-use-outline-path "/" ""))
  9296. (filename (and cfn (expand-file-name cfn)))
  9297. (tbl (mapcar
  9298. (lambda (x)
  9299. (if (and (not (member org-refile-use-outline-path
  9300. '(file full-file-path)))
  9301. (not (equal filename (nth 1 x))))
  9302. (cons (concat (car x) extra " ("
  9303. (file-name-nondirectory (nth 1 x)) ")")
  9304. (cdr x))
  9305. (cons (concat (car x) extra) (cdr x))))
  9306. org-refile-target-table))
  9307. (completion-ignore-case t)
  9308. pa answ parent-target child parent old-hist)
  9309. (setq old-hist org-refile-history)
  9310. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9311. nil 'org-refile-history))
  9312. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9313. (org-refile-check-position pa)
  9314. (if pa
  9315. (progn
  9316. (when (or (not org-refile-history)
  9317. (not (eq old-hist org-refile-history))
  9318. (not (equal (car pa) (car org-refile-history))))
  9319. (setq org-refile-history
  9320. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9321. org-refile-history
  9322. (cdr org-refile-history))))
  9323. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9324. (pop org-refile-history)))
  9325. pa)
  9326. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9327. (progn
  9328. (setq parent (match-string 1 answ)
  9329. child (match-string 2 answ))
  9330. (setq parent-target (or (assoc parent tbl)
  9331. (assoc (concat parent "/") tbl)))
  9332. (when (and parent-target
  9333. (or (eq new-nodes t)
  9334. (and (eq new-nodes 'confirm)
  9335. (y-or-n-p (format "Create new node \"%s\"? "
  9336. child)))))
  9337. (org-refile-new-child parent-target child)))
  9338. (error "Invalid target location")))))
  9339. (defun org-refile-check-position (refile-pointer)
  9340. "Check if the refile pointer matches the readline to which it points."
  9341. (let* ((file (nth 1 refile-pointer))
  9342. (re (nth 2 refile-pointer))
  9343. (pos (nth 3 refile-pointer))
  9344. buffer)
  9345. (when (org-string-nw-p re)
  9346. (setq buffer (if (markerp pos)
  9347. (marker-buffer pos)
  9348. (or (find-buffer-visiting file)
  9349. (find-file-noselect file))))
  9350. (with-current-buffer buffer
  9351. (save-excursion
  9352. (save-restriction
  9353. (widen)
  9354. (goto-char pos)
  9355. (beginning-of-line 1)
  9356. (unless (org-looking-at-p re)
  9357. (error "Invalid refile position, please rebuild the cache"))))))))
  9358. (defun org-refile-new-child (parent-target child)
  9359. "Use refile target PARENT-TARGET to add new CHILD below it."
  9360. (unless parent-target
  9361. (error "Cannot find parent for new node"))
  9362. (let ((file (nth 1 parent-target))
  9363. (pos (nth 3 parent-target))
  9364. level)
  9365. (with-current-buffer (or (find-buffer-visiting file)
  9366. (find-file-noselect file))
  9367. (save-excursion
  9368. (save-restriction
  9369. (widen)
  9370. (if pos
  9371. (goto-char pos)
  9372. (goto-char (point-max))
  9373. (if (not (bolp)) (newline)))
  9374. (when (looking-at outline-regexp)
  9375. (setq level (funcall outline-level))
  9376. (org-end-of-subtree t t))
  9377. (org-back-over-empty-lines)
  9378. (insert "\n" (make-string
  9379. (if pos (org-get-valid-level level 1) 1) ?*)
  9380. " " child "\n")
  9381. (beginning-of-line 0)
  9382. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9383. (defun org-olpath-completing-read (prompt collection &rest args)
  9384. "Read an outline path like a file name."
  9385. (let ((thetable collection)
  9386. (org-completion-use-ido nil) ; does not work with ido.
  9387. (org-completion-use-iswitchb nil)) ; or iswitchb
  9388. (apply
  9389. 'org-icompleting-read prompt
  9390. (lambda (string predicate &optional flag)
  9391. (let (rtn r f (l (length string)))
  9392. (cond
  9393. ((eq flag nil)
  9394. ;; try completion
  9395. (try-completion string thetable))
  9396. ((eq flag t)
  9397. ;; all-completions
  9398. (setq rtn (all-completions string thetable predicate))
  9399. (mapcar
  9400. (lambda (x)
  9401. (setq r (substring x l))
  9402. (if (string-match " ([^)]*)$" x)
  9403. (setq f (match-string 0 x))
  9404. (setq f ""))
  9405. (if (string-match "/" r)
  9406. (concat string (substring r 0 (match-end 0)) f)
  9407. x))
  9408. rtn))
  9409. ((eq flag 'lambda)
  9410. ;; exact match?
  9411. (assoc string thetable)))
  9412. ))
  9413. args)))
  9414. ;;;; Dynamic blocks
  9415. (defun org-find-dblock (name)
  9416. "Find the first dynamic block with name NAME in the buffer.
  9417. If not found, stay at current position and return nil."
  9418. (let (pos)
  9419. (save-excursion
  9420. (goto-char (point-min))
  9421. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9422. nil t)
  9423. (match-beginning 0))))
  9424. (if pos (goto-char pos))
  9425. pos))
  9426. (defconst org-dblock-start-re
  9427. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9428. "Matches the start line of a dynamic block, with parameters.")
  9429. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9430. "Matches the end of a dynamic block.")
  9431. (defun org-create-dblock (plist)
  9432. "Create a dynamic block section, with parameters taken from PLIST.
  9433. PLIST must contain a :name entry which is used as name of the block."
  9434. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9435. (end-of-line 1)
  9436. (newline))
  9437. (let ((col (current-column))
  9438. (name (plist-get plist :name)))
  9439. (insert "#+BEGIN: " name)
  9440. (while plist
  9441. (if (eq (car plist) :name)
  9442. (setq plist (cddr plist))
  9443. (insert " " (prin1-to-string (pop plist)))))
  9444. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9445. (beginning-of-line -2)))
  9446. (defun org-prepare-dblock ()
  9447. "Prepare dynamic block for refresh.
  9448. This empties the block, puts the cursor at the insert position and returns
  9449. the property list including an extra property :name with the block name."
  9450. (unless (looking-at org-dblock-start-re)
  9451. (error "Not at a dynamic block"))
  9452. (let* ((begdel (1+ (match-end 0)))
  9453. (name (org-no-properties (match-string 1)))
  9454. (params (append (list :name name)
  9455. (read (concat "(" (match-string 3) ")")))))
  9456. (save-excursion
  9457. (beginning-of-line 1)
  9458. (skip-chars-forward " \t")
  9459. (setq params (plist-put params :indentation-column (current-column))))
  9460. (unless (re-search-forward org-dblock-end-re nil t)
  9461. (error "Dynamic block not terminated"))
  9462. (setq params
  9463. (append params
  9464. (list :content (buffer-substring
  9465. begdel (match-beginning 0)))))
  9466. (delete-region begdel (match-beginning 0))
  9467. (goto-char begdel)
  9468. (open-line 1)
  9469. params))
  9470. (defun org-map-dblocks (&optional command)
  9471. "Apply COMMAND to all dynamic blocks in the current buffer.
  9472. If COMMAND is not given, use `org-update-dblock'."
  9473. (let ((cmd (or command 'org-update-dblock)))
  9474. (save-excursion
  9475. (goto-char (point-min))
  9476. (while (re-search-forward org-dblock-start-re nil t)
  9477. (goto-char (match-beginning 0))
  9478. (save-excursion
  9479. (condition-case nil
  9480. (funcall cmd)
  9481. (error (message "Error during update of dynamic block"))))
  9482. (unless (re-search-forward org-dblock-end-re nil t)
  9483. (error "Dynamic block not terminated"))))))
  9484. (defun org-dblock-update (&optional arg)
  9485. "User command for updating dynamic blocks.
  9486. Update the dynamic block at point. With prefix ARG, update all dynamic
  9487. blocks in the buffer."
  9488. (interactive "P")
  9489. (if arg
  9490. (org-update-all-dblocks)
  9491. (or (looking-at org-dblock-start-re)
  9492. (org-beginning-of-dblock))
  9493. (org-update-dblock)))
  9494. (defun org-update-dblock ()
  9495. "Update the dynamic block at point.
  9496. This means to empty the block, parse for parameters and then call
  9497. the correct writing function."
  9498. (interactive)
  9499. (save-window-excursion
  9500. (let* ((pos (point))
  9501. (line (org-current-line))
  9502. (params (org-prepare-dblock))
  9503. (name (plist-get params :name))
  9504. (indent (plist-get params :indentation-column))
  9505. (cmd (intern (concat "org-dblock-write:" name))))
  9506. (message "Updating dynamic block `%s' at line %d..." name line)
  9507. (funcall cmd params)
  9508. (message "Updating dynamic block `%s' at line %d...done" name line)
  9509. (goto-char pos)
  9510. (when (and indent (> indent 0))
  9511. (setq indent (make-string indent ?\ ))
  9512. (save-excursion
  9513. (org-beginning-of-dblock)
  9514. (forward-line 1)
  9515. (while (not (looking-at org-dblock-end-re))
  9516. (insert indent)
  9517. (beginning-of-line 2))
  9518. (when (looking-at org-dblock-end-re)
  9519. (and (looking-at "[ \t]+")
  9520. (replace-match ""))
  9521. (insert indent)))))))
  9522. (defun org-beginning-of-dblock ()
  9523. "Find the beginning of the dynamic block at point.
  9524. Error if there is no such block at point."
  9525. (let ((pos (point))
  9526. beg)
  9527. (end-of-line 1)
  9528. (if (and (re-search-backward org-dblock-start-re nil t)
  9529. (setq beg (match-beginning 0))
  9530. (re-search-forward org-dblock-end-re nil t)
  9531. (> (match-end 0) pos))
  9532. (goto-char beg)
  9533. (goto-char pos)
  9534. (error "Not in a dynamic block"))))
  9535. (defun org-update-all-dblocks ()
  9536. "Update all dynamic blocks in the buffer.
  9537. This function can be used in a hook."
  9538. (interactive)
  9539. (when (org-mode-p)
  9540. (org-map-dblocks 'org-update-dblock)))
  9541. ;;;; Completion
  9542. (defconst org-additional-option-like-keywords
  9543. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9544. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9545. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9546. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9547. "BEGIN:" "END:"
  9548. "ORGTBL" "TBLFM:" "TBLNAME:"
  9549. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9550. "BEGIN_QUOTE" "END_QUOTE"
  9551. "BEGIN_VERSE" "END_VERSE"
  9552. "BEGIN_CENTER" "END_CENTER"
  9553. "BEGIN_SRC" "END_SRC"
  9554. "BEGIN_RESULT" "END_RESULT"
  9555. "SOURCE:" "SRCNAME:" "FUNCTION:"
  9556. "RESULTS:"
  9557. "HEADER:" "HEADERS:"
  9558. "BABEL:"
  9559. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9560. "CAPTION:" "LABEL:"
  9561. "SETUPFILE:"
  9562. "INCLUDE:"
  9563. "BIND:"
  9564. "MACRO:"))
  9565. (defcustom org-structure-template-alist
  9566. '(
  9567. ("s" "#+begin_src ?\n\n#+end_src"
  9568. "<src lang=\"?\">\n\n</src>")
  9569. ("e" "#+begin_example\n?\n#+end_example"
  9570. "<example>\n?\n</example>")
  9571. ("q" "#+begin_quote\n?\n#+end_quote"
  9572. "<quote>\n?\n</quote>")
  9573. ("v" "#+begin_verse\n?\n#+end_verse"
  9574. "<verse>\n?\n/verse>")
  9575. ("c" "#+begin_center\n?\n#+end_center"
  9576. "<center>\n?\n/center>")
  9577. ("l" "#+begin_latex\n?\n#+end_latex"
  9578. "<literal style=\"latex\">\n?\n</literal>")
  9579. ("L" "#+latex: "
  9580. "<literal style=\"latex\">?</literal>")
  9581. ("h" "#+begin_html\n?\n#+end_html"
  9582. "<literal style=\"html\">\n?\n</literal>")
  9583. ("H" "#+html: "
  9584. "<literal style=\"html\">?</literal>")
  9585. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9586. ("A" "#+ascii: ")
  9587. ("i" "#+include %file ?"
  9588. "<include file=%file markup=\"?\">")
  9589. )
  9590. "Structure completion elements.
  9591. This is a list of abbreviation keys and values. The value gets inserted
  9592. if you type `<' followed by the key and then press the completion key,
  9593. usually `M-TAB'. %file will be replaced by a file name after prompting
  9594. for the file using completion.
  9595. There are two templates for each key, the first uses the original Org syntax,
  9596. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9597. the default when the /org-mtags.el/ module has been loaded. See also the
  9598. variable `org-mtags-prefer-muse-templates'.
  9599. This is an experimental feature, it is undecided if it is going to stay in."
  9600. :group 'org-completion
  9601. :type '(repeat
  9602. (string :tag "Key")
  9603. (string :tag "Template")
  9604. (string :tag "Muse Template")))
  9605. (defun org-try-structure-completion ()
  9606. "Try to complete a structure template before point.
  9607. This looks for strings like \"<e\" on an otherwise empty line and
  9608. expands them."
  9609. (let ((l (buffer-substring (point-at-bol) (point)))
  9610. a)
  9611. (when (and (looking-at "[ \t]*$")
  9612. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9613. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9614. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9615. (match-beginning 1)) a)
  9616. t)))
  9617. (defun org-complete-expand-structure-template (start cell)
  9618. "Expand a structure template."
  9619. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9620. (rpl (nth (if musep 2 1) cell))
  9621. (ind ""))
  9622. (delete-region start (point))
  9623. (when (string-match "\\`#\\+" rpl)
  9624. (cond
  9625. ((bolp))
  9626. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9627. (setq ind (buffer-substring (point-at-bol) (point))))
  9628. (t (newline))))
  9629. (setq start (point))
  9630. (if (string-match "%file" rpl)
  9631. (setq rpl (replace-match
  9632. (concat
  9633. "\""
  9634. (save-match-data
  9635. (abbreviate-file-name (read-file-name "Include file: ")))
  9636. "\"")
  9637. t t rpl)))
  9638. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9639. (concat "\n" ind)))
  9640. (insert rpl)
  9641. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9642. ;;;; TODO, DEADLINE, Comments
  9643. (defun org-toggle-comment ()
  9644. "Change the COMMENT state of an entry."
  9645. (interactive)
  9646. (save-excursion
  9647. (org-back-to-heading)
  9648. (let (case-fold-search)
  9649. (if (looking-at (concat outline-regexp
  9650. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9651. (replace-match "" t t nil 1)
  9652. (if (looking-at outline-regexp)
  9653. (progn
  9654. (goto-char (match-end 0))
  9655. (insert org-comment-string " ")))))))
  9656. (defvar org-last-todo-state-is-todo nil
  9657. "This is non-nil when the last TODO state change led to a TODO state.
  9658. If the last change removed the TODO tag or switched to DONE, then
  9659. this is nil.")
  9660. (defvar org-setting-tags nil) ; dynamically skipped
  9661. (defvar org-todo-setup-filter-hook nil
  9662. "Hook for functions that pre-filter todo specs.
  9663. Each function takes a todo spec and returns either nil or the spec
  9664. transformed into canonical form." )
  9665. (defvar org-todo-get-default-hook nil
  9666. "Hook for functions that get a default item for todo.
  9667. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9668. nil or a string to be used for the todo mark." )
  9669. (defvar org-agenda-headline-snapshot-before-repeat)
  9670. (defun org-todo (&optional arg)
  9671. "Change the TODO state of an item.
  9672. The state of an item is given by a keyword at the start of the heading,
  9673. like
  9674. *** TODO Write paper
  9675. *** DONE Call mom
  9676. The different keywords are specified in the variable `org-todo-keywords'.
  9677. By default the available states are \"TODO\" and \"DONE\".
  9678. So for this example: when the item starts with TODO, it is changed to DONE.
  9679. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9680. DONE are present, add TODO at the beginning of the heading.
  9681. With \\[universal-argument] prefix arg, use completion to determine the new \
  9682. state.
  9683. With numeric prefix arg, switch to that state.
  9684. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9685. keywords (nextset).
  9686. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9687. For calling through lisp, arg is also interpreted in the following way:
  9688. 'none -> empty state
  9689. \"\"(empty string) -> switch to empty state
  9690. 'done -> switch to DONE
  9691. 'nextset -> switch to the next set of keywords
  9692. 'previousset -> switch to the previous set of keywords
  9693. \"WAITING\" -> switch to the specified keyword, but only if it
  9694. really is a member of `org-todo-keywords'."
  9695. (interactive "P")
  9696. (if (equal arg '(16)) (setq arg 'nextset))
  9697. (let ((org-blocker-hook org-blocker-hook)
  9698. (case-fold-search nil))
  9699. (when (equal arg '(64))
  9700. (setq arg nil org-blocker-hook nil))
  9701. (when (and org-blocker-hook
  9702. (or org-inhibit-blocking
  9703. (org-entry-get nil "NOBLOCKING")))
  9704. (setq org-blocker-hook nil))
  9705. (save-excursion
  9706. (catch 'exit
  9707. (org-back-to-heading t)
  9708. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9709. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9710. (looking-at " *"))
  9711. (let* ((match-data (match-data))
  9712. (startpos (point-at-bol))
  9713. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9714. (org-log-done org-log-done)
  9715. (org-log-repeat org-log-repeat)
  9716. (org-todo-log-states org-todo-log-states)
  9717. (this (match-string 1))
  9718. (hl-pos (match-beginning 0))
  9719. (head (org-get-todo-sequence-head this))
  9720. (ass (assoc head org-todo-kwd-alist))
  9721. (interpret (nth 1 ass))
  9722. (done-word (nth 3 ass))
  9723. (final-done-word (nth 4 ass))
  9724. (last-state (or this ""))
  9725. (completion-ignore-case t)
  9726. (member (member this org-todo-keywords-1))
  9727. (tail (cdr member))
  9728. (state (cond
  9729. ((and org-todo-key-trigger
  9730. (or (and (equal arg '(4))
  9731. (eq org-use-fast-todo-selection 'prefix))
  9732. (and (not arg) org-use-fast-todo-selection
  9733. (not (eq org-use-fast-todo-selection
  9734. 'prefix)))))
  9735. ;; Use fast selection
  9736. (org-fast-todo-selection))
  9737. ((and (equal arg '(4))
  9738. (or (not org-use-fast-todo-selection)
  9739. (not org-todo-key-trigger)))
  9740. ;; Read a state with completion
  9741. (org-icompleting-read
  9742. "State: " (mapcar (lambda(x) (list x))
  9743. org-todo-keywords-1)
  9744. nil t))
  9745. ((eq arg 'right)
  9746. (if this
  9747. (if tail (car tail) nil)
  9748. (car org-todo-keywords-1)))
  9749. ((eq arg 'left)
  9750. (if (equal member org-todo-keywords-1)
  9751. nil
  9752. (if this
  9753. (nth (- (length org-todo-keywords-1)
  9754. (length tail) 2)
  9755. org-todo-keywords-1)
  9756. (org-last org-todo-keywords-1))))
  9757. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9758. (setq arg nil))) ; hack to fall back to cycling
  9759. (arg
  9760. ;; user or caller requests a specific state
  9761. (cond
  9762. ((equal arg "") nil)
  9763. ((eq arg 'none) nil)
  9764. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9765. ((eq arg 'nextset)
  9766. (or (car (cdr (member head org-todo-heads)))
  9767. (car org-todo-heads)))
  9768. ((eq arg 'previousset)
  9769. (let ((org-todo-heads (reverse org-todo-heads)))
  9770. (or (car (cdr (member head org-todo-heads)))
  9771. (car org-todo-heads))))
  9772. ((car (member arg org-todo-keywords-1)))
  9773. ((stringp arg)
  9774. (error "State `%s' not valid in this file" arg))
  9775. ((nth (1- (prefix-numeric-value arg))
  9776. org-todo-keywords-1))))
  9777. ((null member) (or head (car org-todo-keywords-1)))
  9778. ((equal this final-done-word) nil) ;; -> make empty
  9779. ((null tail) nil) ;; -> first entry
  9780. ((memq interpret '(type priority))
  9781. (if (eq this-command last-command)
  9782. (car tail)
  9783. (if (> (length tail) 0)
  9784. (or done-word (car org-done-keywords))
  9785. nil)))
  9786. (t
  9787. (car tail))))
  9788. (state (or
  9789. (run-hook-with-args-until-success
  9790. 'org-todo-get-default-hook state last-state)
  9791. state))
  9792. (next (if state (concat " " state " ") " "))
  9793. (change-plist (list :type 'todo-state-change :from this :to state
  9794. :position startpos))
  9795. dolog now-done-p)
  9796. (when org-blocker-hook
  9797. (setq org-last-todo-state-is-todo
  9798. (not (member this org-done-keywords)))
  9799. (unless (save-excursion
  9800. (save-match-data
  9801. (run-hook-with-args-until-failure
  9802. 'org-blocker-hook change-plist)))
  9803. (if (interactive-p)
  9804. (error "TODO state change from %s to %s blocked" this state)
  9805. ;; fail silently
  9806. (message "TODO state change from %s to %s blocked" this state)
  9807. (throw 'exit nil))))
  9808. (store-match-data match-data)
  9809. (replace-match next t t)
  9810. (unless (pos-visible-in-window-p hl-pos)
  9811. (message "TODO state changed to %s" (org-trim next)))
  9812. (unless head
  9813. (setq head (org-get-todo-sequence-head state)
  9814. ass (assoc head org-todo-kwd-alist)
  9815. interpret (nth 1 ass)
  9816. done-word (nth 3 ass)
  9817. final-done-word (nth 4 ass)))
  9818. (when (memq arg '(nextset previousset))
  9819. (message "Keyword-Set %d/%d: %s"
  9820. (- (length org-todo-sets) -1
  9821. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9822. (length org-todo-sets)
  9823. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9824. (setq org-last-todo-state-is-todo
  9825. (not (member state org-done-keywords)))
  9826. (setq now-done-p (and (member state org-done-keywords)
  9827. (not (member this org-done-keywords))))
  9828. (and logging (org-local-logging logging))
  9829. (when (and (or org-todo-log-states org-log-done)
  9830. (not (eq org-inhibit-logging t))
  9831. (not (memq arg '(nextset previousset))))
  9832. ;; we need to look at recording a time and note
  9833. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9834. (nth 2 (assoc this org-todo-log-states))))
  9835. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9836. (setq dolog 'time))
  9837. (when (and state
  9838. (member state org-not-done-keywords)
  9839. (not (member this org-not-done-keywords)))
  9840. ;; This is now a todo state and was not one before
  9841. ;; If there was a CLOSED time stamp, get rid of it.
  9842. (org-add-planning-info nil nil 'closed))
  9843. (when (and now-done-p org-log-done)
  9844. ;; It is now done, and it was not done before
  9845. (org-add-planning-info 'closed (org-current-time))
  9846. (if (and (not dolog) (eq 'note org-log-done))
  9847. (org-add-log-setup 'done state this 'findpos 'note)))
  9848. (when (and state dolog)
  9849. ;; This is a non-nil state, and we need to log it
  9850. (org-add-log-setup 'state state this 'findpos dolog)))
  9851. ;; Fixup tag positioning
  9852. (org-todo-trigger-tag-changes state)
  9853. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9854. (when org-provide-todo-statistics
  9855. (org-update-parent-todo-statistics))
  9856. (run-hooks 'org-after-todo-state-change-hook)
  9857. (if (and arg (not (member state org-done-keywords)))
  9858. (setq head (org-get-todo-sequence-head state)))
  9859. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9860. ;; Do we need to trigger a repeat?
  9861. (when now-done-p
  9862. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9863. ;; This is for the agenda, take a snapshot of the headline.
  9864. (save-match-data
  9865. (setq org-agenda-headline-snapshot-before-repeat
  9866. (org-get-heading))))
  9867. (org-auto-repeat-maybe state))
  9868. ;; Fixup cursor location if close to the keyword
  9869. (if (and (outline-on-heading-p)
  9870. (not (bolp))
  9871. (save-excursion (beginning-of-line 1)
  9872. (looking-at org-todo-line-regexp))
  9873. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9874. (progn
  9875. (goto-char (or (match-end 2) (match-end 1)))
  9876. (and (looking-at " ") (just-one-space))))
  9877. (when org-trigger-hook
  9878. (save-excursion
  9879. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9880. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9881. "Block turning an entry into a TODO, using the hierarchy.
  9882. This checks whether the current task should be blocked from state
  9883. changes. Such blocking occurs when:
  9884. 1. The task has children which are not all in a completed state.
  9885. 2. A task has a parent with the property :ORDERED:, and there
  9886. are siblings prior to the current task with incomplete
  9887. status.
  9888. 3. The parent of the task is blocked because it has siblings that should
  9889. be done first, or is child of a block grandparent TODO entry."
  9890. (if (not org-enforce-todo-dependencies)
  9891. t ; if locally turned off don't block
  9892. (catch 'dont-block
  9893. ;; If this is not a todo state change, or if this entry is already DONE,
  9894. ;; do not block
  9895. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9896. (member (plist-get change-plist :from)
  9897. (cons 'done org-done-keywords))
  9898. (member (plist-get change-plist :to)
  9899. (cons 'todo org-not-done-keywords))
  9900. (not (plist-get change-plist :to)))
  9901. (throw 'dont-block t))
  9902. ;; If this task has children, and any are undone, it's blocked
  9903. (save-excursion
  9904. (org-back-to-heading t)
  9905. (let ((this-level (funcall outline-level)))
  9906. (outline-next-heading)
  9907. (let ((child-level (funcall outline-level)))
  9908. (while (and (not (eobp))
  9909. (> child-level this-level))
  9910. ;; this todo has children, check whether they are all
  9911. ;; completed
  9912. (if (and (not (org-entry-is-done-p))
  9913. (org-entry-is-todo-p))
  9914. (throw 'dont-block nil))
  9915. (outline-next-heading)
  9916. (setq child-level (funcall outline-level))))))
  9917. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9918. ;; any previous siblings are undone, it's blocked
  9919. (save-excursion
  9920. (org-back-to-heading t)
  9921. (let* ((pos (point))
  9922. (parent-pos (and (org-up-heading-safe) (point))))
  9923. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9924. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9925. (forward-line 1)
  9926. (re-search-forward org-not-done-heading-regexp pos t))
  9927. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9928. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9929. (while t
  9930. (goto-char parent-pos)
  9931. (if (not (looking-at org-not-done-heading-regexp))
  9932. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9933. (setq pos (point))
  9934. (setq parent-pos (and (org-up-heading-safe) (point)))
  9935. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9936. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9937. (forward-line 1)
  9938. (re-search-forward org-not-done-heading-regexp pos t))
  9939. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9940. (defcustom org-track-ordered-property-with-tag nil
  9941. "Should the ORDERED property also be shown as a tag?
  9942. The ORDERED property decides if an entry should require subtasks to be
  9943. completed in sequence. Since a property is not very visible, setting
  9944. this option means that toggling the ORDERED property with the command
  9945. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9946. not relevant for the behavior, but it makes things more visible.
  9947. Note that toggling the tag with tags commands will not change the property
  9948. and therefore not influence behavior!
  9949. This can be t, meaning the tag ORDERED should be used, It can also be a
  9950. string to select a different tag for this task."
  9951. :group 'org-todo
  9952. :type '(choice
  9953. (const :tag "No tracking" nil)
  9954. (const :tag "Track with ORDERED tag" t)
  9955. (string :tag "Use other tag")))
  9956. (defun org-toggle-ordered-property ()
  9957. "Toggle the ORDERED property of the current entry.
  9958. For better visibility, you can track the value of this property with a tag.
  9959. See variable `org-track-ordered-property-with-tag'."
  9960. (interactive)
  9961. (let* ((t1 org-track-ordered-property-with-tag)
  9962. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9963. (save-excursion
  9964. (org-back-to-heading)
  9965. (if (org-entry-get nil "ORDERED")
  9966. (progn
  9967. (org-delete-property "ORDERED")
  9968. (and tag (org-toggle-tag tag 'off))
  9969. (message "Subtasks can be completed in arbitrary order"))
  9970. (org-entry-put nil "ORDERED" "t")
  9971. (and tag (org-toggle-tag tag 'on))
  9972. (message "Subtasks must be completed in sequence")))))
  9973. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9974. (defun org-block-todo-from-checkboxes (change-plist)
  9975. "Block turning an entry into a TODO, using checkboxes.
  9976. This checks whether the current task should be blocked from state
  9977. changes because there are unchecked boxes in this entry."
  9978. (if (not org-enforce-todo-checkbox-dependencies)
  9979. t ; if locally turned off don't block
  9980. (catch 'dont-block
  9981. ;; If this is not a todo state change, or if this entry is already DONE,
  9982. ;; do not block
  9983. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9984. (member (plist-get change-plist :from)
  9985. (cons 'done org-done-keywords))
  9986. (member (plist-get change-plist :to)
  9987. (cons 'todo org-not-done-keywords))
  9988. (not (plist-get change-plist :to)))
  9989. (throw 'dont-block t))
  9990. ;; If this task has checkboxes that are not checked, it's blocked
  9991. (save-excursion
  9992. (org-back-to-heading t)
  9993. (let ((beg (point)) end)
  9994. (outline-next-heading)
  9995. (setq end (point))
  9996. (goto-char beg)
  9997. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9998. end t)
  9999. (progn
  10000. (if (boundp 'org-blocked-by-checkboxes)
  10001. (setq org-blocked-by-checkboxes t))
  10002. (throw 'dont-block nil)))))
  10003. t))) ; do not block
  10004. (defun org-entry-blocked-p ()
  10005. "Is the current entry blocked?"
  10006. (if (org-entry-get nil "NOBLOCKING")
  10007. nil ;; Never block this entry
  10008. (not
  10009. (run-hook-with-args-until-failure
  10010. 'org-blocker-hook
  10011. (list :type 'todo-state-change
  10012. :position (point)
  10013. :from 'todo
  10014. :to 'done)))))
  10015. (defun org-update-statistics-cookies (all)
  10016. "Update the statistics cookie, either from TODO or from checkboxes.
  10017. This should be called with the cursor in a line with a statistics cookie."
  10018. (interactive "P")
  10019. (if all
  10020. (progn
  10021. (org-update-checkbox-count 'all)
  10022. (org-map-entries 'org-update-parent-todo-statistics))
  10023. (if (not (org-on-heading-p))
  10024. (org-update-checkbox-count)
  10025. (let ((pos (move-marker (make-marker) (point)))
  10026. end l1 l2)
  10027. (ignore-errors (org-back-to-heading t))
  10028. (if (not (org-on-heading-p))
  10029. (org-update-checkbox-count)
  10030. (setq l1 (org-outline-level))
  10031. (setq end (save-excursion
  10032. (outline-next-heading)
  10033. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10034. (point)))
  10035. (if (and (save-excursion
  10036. (re-search-forward
  10037. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10038. (not (save-excursion (re-search-forward
  10039. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10040. (org-update-checkbox-count)
  10041. (if (and l2 (> l2 l1))
  10042. (progn
  10043. (goto-char end)
  10044. (org-update-parent-todo-statistics))
  10045. (goto-char pos)
  10046. (beginning-of-line 1)
  10047. (while (re-search-forward
  10048. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10049. (point-at-eol) t)
  10050. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10051. (goto-char pos)
  10052. (move-marker pos nil)))))
  10053. (defvar org-entry-property-inherited-from) ;; defined below
  10054. (defun org-update-parent-todo-statistics ()
  10055. "Update any statistics cookie in the parent of the current headline.
  10056. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10057. the entire subtree and this will travel up the hierarchy and update
  10058. statistics everywhere."
  10059. (interactive)
  10060. (let* ((lim 0) prop
  10061. (recursive (or (not org-hierarchical-todo-statistics)
  10062. (string-match
  10063. "\\<recursive\\>"
  10064. (or (setq prop (org-entry-get
  10065. nil "COOKIE_DATA" 'inherit)) ""))))
  10066. (lim (or (and prop (marker-position
  10067. org-entry-property-inherited-from))
  10068. lim))
  10069. (first t)
  10070. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10071. level ltoggle l1 new ndel
  10072. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  10073. (catch 'exit
  10074. (save-excursion
  10075. (beginning-of-line 1)
  10076. (if (org-at-heading-p)
  10077. (setq ltoggle (funcall outline-level))
  10078. (error "This should not happen"))
  10079. (while (and (setq level (org-up-heading-safe))
  10080. (or recursive first)
  10081. (>= (point) lim))
  10082. (setq first nil cookie-present nil)
  10083. (unless (and level
  10084. (not (string-match
  10085. "\\<checkbox\\>"
  10086. (downcase
  10087. (or (org-entry-get
  10088. nil "COOKIE_DATA")
  10089. "")))))
  10090. (throw 'exit nil))
  10091. (while (re-search-forward box-re (point-at-eol) t)
  10092. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10093. (setq is-percent (match-end 2))
  10094. (save-match-data
  10095. (unless (outline-next-heading) (throw 'exit nil))
  10096. (while (and (looking-at org-complex-heading-regexp)
  10097. (> (setq l1 (length (match-string 1))) level))
  10098. (setq kwd (and (or recursive (= l1 ltoggle))
  10099. (match-string 2)))
  10100. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10101. (and (listp org-provide-todo-statistics)
  10102. (or (member kwd org-provide-todo-statistics)
  10103. (member kwd org-done-keywords))))
  10104. (setq cnt-all (1+ cnt-all))
  10105. (if (eq org-provide-todo-statistics t)
  10106. (and kwd (setq cnt-all (1+ cnt-all)))))
  10107. (and (member kwd org-done-keywords)
  10108. (setq cnt-done (1+ cnt-done)))
  10109. (outline-next-heading)))
  10110. (setq new
  10111. (if is-percent
  10112. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10113. (format "[%d/%d]" cnt-done cnt-all))
  10114. ndel (- (match-end 0) (match-beginning 0)))
  10115. (goto-char (match-beginning 0))
  10116. (insert new)
  10117. (delete-region (point) (+ (point) ndel)))
  10118. (when cookie-present
  10119. (run-hook-with-args 'org-after-todo-statistics-hook
  10120. cnt-done (- cnt-all cnt-done))))))
  10121. (run-hooks 'org-todo-statistics-hook)))
  10122. (defvar org-after-todo-statistics-hook nil
  10123. "Hook that is called after a TODO statistics cookie has been updated.
  10124. Each function is called with two arguments: the number of not-done entries
  10125. and the number of done entries.
  10126. For example, the following function, when added to this hook, will switch
  10127. an entry to DONE when all children are done, and back to TODO when new
  10128. entries are set to a TODO status. Note that this hook is only called
  10129. when there is a statistics cookie in the headline!
  10130. (defun org-summary-todo (n-done n-not-done)
  10131. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10132. (let (org-log-done org-log-states) ; turn off logging
  10133. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10134. ")
  10135. (defvar org-todo-statistics-hook nil
  10136. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10137. This hook runs even if there is no statistics cookie present, in which case
  10138. `org-after-todo-statistics-hook' would not run.")
  10139. (defun org-todo-trigger-tag-changes (state)
  10140. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10141. (let ((l org-todo-state-tags-triggers)
  10142. changes)
  10143. (when (or (not state) (equal state ""))
  10144. (setq changes (append changes (cdr (assoc "" l)))))
  10145. (when (and (stringp state) (> (length state) 0))
  10146. (setq changes (append changes (cdr (assoc state l)))))
  10147. (when (member state org-not-done-keywords)
  10148. (setq changes (append changes (cdr (assoc 'todo l)))))
  10149. (when (member state org-done-keywords)
  10150. (setq changes (append changes (cdr (assoc 'done l)))))
  10151. (dolist (c changes)
  10152. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10153. (defun org-local-logging (value)
  10154. "Get logging settings from a property VALUE."
  10155. (let* (words w a)
  10156. ;; directly set the variables, they are already local.
  10157. (setq org-log-done nil
  10158. org-log-repeat nil
  10159. org-todo-log-states nil)
  10160. (setq words (org-split-string value))
  10161. (while (setq w (pop words))
  10162. (cond
  10163. ((setq a (assoc w org-startup-options))
  10164. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10165. (set (nth 1 a) (nth 2 a))))
  10166. ((setq a (org-extract-log-state-settings w))
  10167. (and (member (car a) org-todo-keywords-1)
  10168. (push a org-todo-log-states)))))))
  10169. (defun org-get-todo-sequence-head (kwd)
  10170. "Return the head of the TODO sequence to which KWD belongs.
  10171. If KWD is not set, check if there is a text property remembering the
  10172. right sequence."
  10173. (let (p)
  10174. (cond
  10175. ((not kwd)
  10176. (or (get-text-property (point-at-bol) 'org-todo-head)
  10177. (progn
  10178. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10179. nil (point-at-eol)))
  10180. (get-text-property p 'org-todo-head))))
  10181. ((not (member kwd org-todo-keywords-1))
  10182. (car org-todo-keywords-1))
  10183. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10184. (defun org-fast-todo-selection ()
  10185. "Fast TODO keyword selection with single keys.
  10186. Returns the new TODO keyword, or nil if no state change should occur."
  10187. (let* ((fulltable org-todo-key-alist)
  10188. (done-keywords org-done-keywords) ;; needed for the faces.
  10189. (maxlen (apply 'max (mapcar
  10190. (lambda (x)
  10191. (if (stringp (car x)) (string-width (car x)) 0))
  10192. fulltable)))
  10193. (expert nil)
  10194. (fwidth (+ maxlen 3 1 3))
  10195. (ncol (/ (- (window-width) 4) fwidth))
  10196. tg cnt e c tbl
  10197. groups ingroup)
  10198. (save-excursion
  10199. (save-window-excursion
  10200. (if expert
  10201. (set-buffer (get-buffer-create " *Org todo*"))
  10202. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10203. (erase-buffer)
  10204. (org-set-local 'org-done-keywords done-keywords)
  10205. (setq tbl fulltable cnt 0)
  10206. (while (setq e (pop tbl))
  10207. (cond
  10208. ((equal e '(:startgroup))
  10209. (push '() groups) (setq ingroup t)
  10210. (when (not (= cnt 0))
  10211. (setq cnt 0)
  10212. (insert "\n"))
  10213. (insert "{ "))
  10214. ((equal e '(:endgroup))
  10215. (setq ingroup nil cnt 0)
  10216. (insert "}\n"))
  10217. ((equal e '(:newline))
  10218. (when (not (= cnt 0))
  10219. (setq cnt 0)
  10220. (insert "\n")
  10221. (setq e (car tbl))
  10222. (while (equal (car tbl) '(:newline))
  10223. (insert "\n")
  10224. (setq tbl (cdr tbl)))))
  10225. (t
  10226. (setq tg (car e) c (cdr e))
  10227. (if ingroup (push tg (car groups)))
  10228. (setq tg (org-add-props tg nil 'face
  10229. (org-get-todo-face tg)))
  10230. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10231. (insert "[" c "] " tg (make-string
  10232. (- fwidth 4 (length tg)) ?\ ))
  10233. (when (= (setq cnt (1+ cnt)) ncol)
  10234. (insert "\n")
  10235. (if ingroup (insert " "))
  10236. (setq cnt 0)))))
  10237. (insert "\n")
  10238. (goto-char (point-min))
  10239. (if (not expert) (org-fit-window-to-buffer))
  10240. (message "[a-z..]:Set [SPC]:clear")
  10241. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10242. (cond
  10243. ((or (= c ?\C-g)
  10244. (and (= c ?q) (not (rassoc c fulltable))))
  10245. (setq quit-flag t))
  10246. ((= c ?\ ) nil)
  10247. ((setq e (rassoc c fulltable) tg (car e))
  10248. tg)
  10249. (t (setq quit-flag t)))))))
  10250. (defun org-entry-is-todo-p ()
  10251. (member (org-get-todo-state) org-not-done-keywords))
  10252. (defun org-entry-is-done-p ()
  10253. (member (org-get-todo-state) org-done-keywords))
  10254. (defun org-get-todo-state ()
  10255. (save-excursion
  10256. (org-back-to-heading t)
  10257. (and (looking-at org-todo-line-regexp)
  10258. (match-end 2)
  10259. (match-string 2))))
  10260. (defun org-at-date-range-p (&optional inactive-ok)
  10261. "Is the cursor inside a date range?"
  10262. (interactive)
  10263. (save-excursion
  10264. (catch 'exit
  10265. (let ((pos (point)))
  10266. (skip-chars-backward "^[<\r\n")
  10267. (skip-chars-backward "<[")
  10268. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10269. (>= (match-end 0) pos)
  10270. (throw 'exit t))
  10271. (skip-chars-backward "^<[\r\n")
  10272. (skip-chars-backward "<[")
  10273. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10274. (>= (match-end 0) pos)
  10275. (throw 'exit t)))
  10276. nil)))
  10277. (defun org-get-repeat (&optional tagline)
  10278. "Check if there is a deadline/schedule with repeater in this entry."
  10279. (save-match-data
  10280. (save-excursion
  10281. (org-back-to-heading t)
  10282. (and (re-search-forward (if tagline
  10283. (concat tagline "\\s-*" org-repeat-re)
  10284. org-repeat-re)
  10285. (org-entry-end-position) t)
  10286. (match-string-no-properties 1)))))
  10287. (defvar org-last-changed-timestamp)
  10288. (defvar org-last-inserted-timestamp)
  10289. (defvar org-log-post-message)
  10290. (defvar org-log-note-purpose)
  10291. (defvar org-log-note-how)
  10292. (defvar org-log-note-extra)
  10293. (defun org-auto-repeat-maybe (done-word)
  10294. "Check if the current headline contains a repeated deadline/schedule.
  10295. If yes, set TODO state back to what it was and change the base date
  10296. of repeating deadline/scheduled time stamps to new date.
  10297. This function is run automatically after each state change to a DONE state."
  10298. ;; last-state is dynamically scoped into this function
  10299. (let* ((repeat (org-get-repeat))
  10300. (aa (assoc last-state org-todo-kwd-alist))
  10301. (interpret (nth 1 aa))
  10302. (head (nth 2 aa))
  10303. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10304. (msg "Entry repeats: ")
  10305. (org-log-done nil)
  10306. (org-todo-log-states nil)
  10307. re type n what ts time to-state)
  10308. (when repeat
  10309. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10310. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10311. org-todo-repeat-to-state))
  10312. (unless (and to-state (member to-state org-todo-keywords-1))
  10313. (setq to-state (if (eq interpret 'type) last-state head)))
  10314. (org-todo to-state)
  10315. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10316. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10317. (org-time-stamp-format t t))))
  10318. (when org-log-repeat
  10319. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10320. (memq 'org-add-log-note post-command-hook))
  10321. ;; OK, we are already setup for some record
  10322. (if (eq org-log-repeat 'note)
  10323. ;; make sure we take a note, not only a time stamp
  10324. (setq org-log-note-how 'note))
  10325. ;; Set up for taking a record
  10326. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10327. last-state
  10328. 'findpos org-log-repeat)))
  10329. (org-back-to-heading t)
  10330. (org-add-planning-info nil nil 'closed)
  10331. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10332. org-deadline-time-regexp "\\)\\|\\("
  10333. org-ts-regexp "\\)"))
  10334. (while (re-search-forward
  10335. re (save-excursion (outline-next-heading) (point)) t)
  10336. (setq type (if (match-end 1) org-scheduled-string
  10337. (if (match-end 3) org-deadline-string "Plain:"))
  10338. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10339. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10340. (setq n (string-to-number (match-string 2 ts))
  10341. what (match-string 3 ts))
  10342. (if (equal what "w") (setq n (* n 7) what "d"))
  10343. ;; Preparation, see if we need to modify the start date for the change
  10344. (when (match-end 1)
  10345. (setq time (save-match-data (org-time-string-to-time ts)))
  10346. (cond
  10347. ((equal (match-string 1 ts) ".")
  10348. ;; Shift starting date to today
  10349. (org-timestamp-change
  10350. (- (time-to-days (current-time)) (time-to-days time))
  10351. 'day))
  10352. ((equal (match-string 1 ts) "+")
  10353. (let ((nshiftmax 10) (nshift 0))
  10354. (while (or (= nshift 0)
  10355. (<= (time-to-days time)
  10356. (time-to-days (current-time))))
  10357. (when (= (incf nshift) nshiftmax)
  10358. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10359. (error "Abort")))
  10360. (org-timestamp-change n (cdr (assoc what whata)))
  10361. (org-at-timestamp-p t)
  10362. (setq ts (match-string 1))
  10363. (setq time (save-match-data (org-time-string-to-time ts)))))
  10364. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10365. ;; rematch, so that we have everything in place for the real shift
  10366. (org-at-timestamp-p t)
  10367. (setq ts (match-string 1))
  10368. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10369. (org-timestamp-change n (cdr (assoc what whata)))
  10370. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10371. (setq org-log-post-message msg)
  10372. (message "%s" msg))))
  10373. (defun org-show-todo-tree (arg)
  10374. "Make a compact tree which shows all headlines marked with TODO.
  10375. The tree will show the lines where the regexp matches, and all higher
  10376. headlines above the match.
  10377. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10378. With a numeric prefix N, construct a sparse tree for the Nth element
  10379. of `org-todo-keywords-1'."
  10380. (interactive "P")
  10381. (let ((case-fold-search nil)
  10382. (kwd-re
  10383. (cond ((null arg) org-not-done-regexp)
  10384. ((equal arg '(4))
  10385. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10386. (mapcar 'list org-todo-keywords-1))))
  10387. (concat "\\("
  10388. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10389. "\\)\\>")))
  10390. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10391. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10392. org-todo-keywords-1)))
  10393. (t (error "Invalid prefix argument: %s" arg)))))
  10394. (message "%d TODO entries found"
  10395. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10396. (defun org-deadline (&optional remove time)
  10397. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10398. With argument REMOVE, remove any deadline from the item.
  10399. When TIME is set, it should be an internal time specification, and the
  10400. scheduling will use the corresponding date."
  10401. (interactive "P")
  10402. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10403. (repeater (and old-date
  10404. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10405. (match-string 1 old-date))))
  10406. (if remove
  10407. (progn
  10408. (when (and old-date org-log-redeadline)
  10409. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10410. org-log-redeadline))
  10411. (org-remove-timestamp-with-keyword org-deadline-string)
  10412. (message "Item no longer has a deadline."))
  10413. (org-add-planning-info 'deadline time 'closed)
  10414. (when (and old-date org-log-redeadline
  10415. (not (equal old-date
  10416. (substring org-last-inserted-timestamp 1 -1))))
  10417. (org-add-log-setup 'redeadline nil old-date 'findpos
  10418. org-log-redeadline))
  10419. (when repeater
  10420. (save-excursion
  10421. (org-back-to-heading t)
  10422. (when (re-search-forward (concat org-deadline-string " "
  10423. org-last-inserted-timestamp)
  10424. (save-excursion
  10425. (outline-next-heading) (point)) t)
  10426. (goto-char (1- (match-end 0)))
  10427. (insert " " repeater)
  10428. (setq org-last-inserted-timestamp
  10429. (concat (substring org-last-inserted-timestamp 0 -1)
  10430. " " repeater
  10431. (substring org-last-inserted-timestamp -1))))))
  10432. (message "Deadline on %s" org-last-inserted-timestamp))))
  10433. (defun org-schedule (&optional remove time)
  10434. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10435. With argument REMOVE, remove any scheduling date from the item.
  10436. When TIME is set, it should be an internal time specification, and the
  10437. scheduling will use the corresponding date."
  10438. (interactive "P")
  10439. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10440. (repeater (and old-date
  10441. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10442. (match-string 1 old-date))))
  10443. (if remove
  10444. (progn
  10445. (when (and old-date org-log-reschedule)
  10446. (org-add-log-setup 'delschedule nil old-date 'findpos
  10447. org-log-reschedule))
  10448. (org-remove-timestamp-with-keyword org-scheduled-string)
  10449. (message "Item is no longer scheduled."))
  10450. (org-add-planning-info 'scheduled time 'closed)
  10451. (when (and old-date org-log-reschedule
  10452. (not (equal old-date
  10453. (substring org-last-inserted-timestamp 1 -1))))
  10454. (org-add-log-setup 'reschedule nil old-date 'findpos
  10455. org-log-reschedule))
  10456. (when repeater
  10457. (save-excursion
  10458. (org-back-to-heading t)
  10459. (when (re-search-forward (concat org-scheduled-string " "
  10460. org-last-inserted-timestamp)
  10461. (save-excursion
  10462. (outline-next-heading) (point)) t)
  10463. (goto-char (1- (match-end 0)))
  10464. (insert " " repeater)
  10465. (setq org-last-inserted-timestamp
  10466. (concat (substring org-last-inserted-timestamp 0 -1)
  10467. " " repeater
  10468. (substring org-last-inserted-timestamp -1))))))
  10469. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10470. (defun org-get-scheduled-time (pom &optional inherit)
  10471. "Get the scheduled time as a time tuple, of a format suitable
  10472. for calling org-schedule with, or if there is no scheduling,
  10473. returns nil."
  10474. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10475. (when time
  10476. (apply 'encode-time (org-parse-time-string time)))))
  10477. (defun org-get-deadline-time (pom &optional inherit)
  10478. "Get the deadline as a time tuple, of a format suitable for
  10479. calling org-deadline with, or if there is no scheduling, returns
  10480. nil."
  10481. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10482. (when time
  10483. (apply 'encode-time (org-parse-time-string time)))))
  10484. (defun org-remove-timestamp-with-keyword (keyword)
  10485. "Remove all time stamps with KEYWORD in the current entry."
  10486. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10487. beg)
  10488. (save-excursion
  10489. (org-back-to-heading t)
  10490. (setq beg (point))
  10491. (outline-next-heading)
  10492. (while (re-search-backward re beg t)
  10493. (replace-match "")
  10494. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10495. (equal (char-before) ?\ ))
  10496. (backward-delete-char 1)
  10497. (if (string-match "^[ \t]*$" (buffer-substring
  10498. (point-at-bol) (point-at-eol)))
  10499. (delete-region (point-at-bol)
  10500. (min (point-max) (1+ (point-at-eol))))))))))
  10501. (defun org-add-planning-info (what &optional time &rest remove)
  10502. "Insert new timestamp with keyword in the line directly after the headline.
  10503. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10504. If non is given, the user is prompted for a date.
  10505. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10506. be removed."
  10507. (interactive)
  10508. (let (org-time-was-given org-end-time-was-given ts
  10509. end default-time default-input)
  10510. (catch 'exit
  10511. (when (and (not time) (memq what '(scheduled deadline)))
  10512. ;; Try to get a default date/time from existing timestamp
  10513. (save-excursion
  10514. (org-back-to-heading t)
  10515. (setq end (save-excursion (outline-next-heading) (point)))
  10516. (when (re-search-forward (if (eq what 'scheduled)
  10517. org-scheduled-time-regexp
  10518. org-deadline-time-regexp)
  10519. end t)
  10520. (setq ts (match-string 1)
  10521. default-time
  10522. (apply 'encode-time (org-parse-time-string ts))
  10523. default-input (and ts (org-get-compact-tod ts))))))
  10524. (when what
  10525. ;; If necessary, get the time from the user
  10526. (setq time (or time (org-read-date nil 'to-time nil nil
  10527. default-time default-input))))
  10528. (when (and org-insert-labeled-timestamps-at-point
  10529. (member what '(scheduled deadline)))
  10530. (insert
  10531. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10532. (org-insert-time-stamp time org-time-was-given
  10533. nil nil nil (list org-end-time-was-given))
  10534. (setq what nil))
  10535. (save-excursion
  10536. (save-restriction
  10537. (let (col list elt ts buffer-invisibility-spec)
  10538. (org-back-to-heading t)
  10539. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10540. (goto-char (match-end 1))
  10541. (setq col (current-column))
  10542. (goto-char (match-end 0))
  10543. (if (eobp) (insert "\n") (forward-char 1))
  10544. (when (and (not what)
  10545. (not (looking-at
  10546. (concat "[ \t]*"
  10547. org-keyword-time-not-clock-regexp))))
  10548. ;; Nothing to add, nothing to remove...... :-)
  10549. (throw 'exit nil))
  10550. (if (and (not (looking-at outline-regexp))
  10551. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10552. "[^\r\n]*"))
  10553. (not (equal (match-string 1) org-clock-string)))
  10554. (narrow-to-region (match-beginning 0) (match-end 0))
  10555. (insert-before-markers "\n")
  10556. (backward-char 1)
  10557. (narrow-to-region (point) (point))
  10558. (and org-adapt-indentation (org-indent-to-column col)))
  10559. ;; Check if we have to remove something.
  10560. (setq list (cons what remove))
  10561. (while list
  10562. (setq elt (pop list))
  10563. (goto-char (point-min))
  10564. (when (or (and (eq elt 'scheduled)
  10565. (re-search-forward org-scheduled-time-regexp nil t))
  10566. (and (eq elt 'deadline)
  10567. (re-search-forward org-deadline-time-regexp nil t))
  10568. (and (eq elt 'closed)
  10569. (re-search-forward org-closed-time-regexp nil t)))
  10570. (replace-match "")
  10571. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10572. (skip-chars-backward " ")
  10573. (if (looking-at " +") (replace-match ""))))
  10574. (goto-char (point-max))
  10575. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10576. (when what
  10577. (insert
  10578. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10579. (cond ((eq what 'scheduled) org-scheduled-string)
  10580. ((eq what 'deadline) org-deadline-string)
  10581. ((eq what 'closed) org-closed-string))
  10582. " ")
  10583. (setq ts (org-insert-time-stamp
  10584. time
  10585. (or org-time-was-given
  10586. (and (eq what 'closed) org-log-done-with-time))
  10587. (eq what 'closed)
  10588. nil nil (list org-end-time-was-given)))
  10589. (end-of-line 1))
  10590. (goto-char (point-min))
  10591. (widen)
  10592. (if (and (looking-at "[ \t]*\n")
  10593. (equal (char-before) ?\n))
  10594. (delete-region (1- (point)) (point-at-eol)))
  10595. ts))))))
  10596. (defvar org-log-note-marker (make-marker))
  10597. (defvar org-log-note-purpose nil)
  10598. (defvar org-log-note-state nil)
  10599. (defvar org-log-note-previous-state nil)
  10600. (defvar org-log-note-how nil)
  10601. (defvar org-log-note-extra nil)
  10602. (defvar org-log-note-window-configuration nil)
  10603. (defvar org-log-note-return-to (make-marker))
  10604. (defvar org-log-post-message nil
  10605. "Message to be displayed after a log note has been stored.
  10606. The auto-repeater uses this.")
  10607. (defun org-add-note ()
  10608. "Add a note to the current entry.
  10609. This is done in the same way as adding a state change note."
  10610. (interactive)
  10611. (org-add-log-setup 'note nil nil 'findpos nil))
  10612. (defvar org-property-end-re)
  10613. (defun org-add-log-setup (&optional purpose state prev-state
  10614. findpos how extra)
  10615. "Set up the post command hook to take a note.
  10616. If this is about to TODO state change, the new state is expected in STATE.
  10617. When FINDPOS is non-nil, find the correct position for the note in
  10618. the current entry. If not, assume that it can be inserted at point.
  10619. HOW is an indicator what kind of note should be created.
  10620. EXTRA is additional text that will be inserted into the notes buffer."
  10621. (let* ((org-log-into-drawer (org-log-into-drawer))
  10622. (drawer (cond ((stringp org-log-into-drawer)
  10623. org-log-into-drawer)
  10624. (org-log-into-drawer "LOGBOOK")
  10625. (t nil))))
  10626. (save-restriction
  10627. (save-excursion
  10628. (when findpos
  10629. (org-back-to-heading t)
  10630. (narrow-to-region (point) (save-excursion
  10631. (outline-next-heading) (point)))
  10632. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10633. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10634. "[^\r\n]*\\)?"))
  10635. (goto-char (match-end 0))
  10636. (cond
  10637. (drawer
  10638. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10639. nil t)
  10640. (progn
  10641. (goto-char (match-end 0))
  10642. (or org-log-states-order-reversed
  10643. (and (re-search-forward org-property-end-re nil t)
  10644. (goto-char (1- (match-beginning 0))))))
  10645. (insert "\n:" drawer ":\n:END:")
  10646. (beginning-of-line 0)
  10647. (org-indent-line-function)
  10648. (beginning-of-line 2)
  10649. (org-indent-line-function)
  10650. (end-of-line 0)))
  10651. ((and org-log-state-notes-insert-after-drawers
  10652. (save-excursion
  10653. (forward-line) (looking-at org-drawer-regexp)))
  10654. (forward-line)
  10655. (while (looking-at org-drawer-regexp)
  10656. (goto-char (match-end 0))
  10657. (re-search-forward org-property-end-re (point-max) t)
  10658. (forward-line))
  10659. (forward-line -1)))
  10660. (unless org-log-states-order-reversed
  10661. (and (= (char-after) ?\n) (forward-char 1))
  10662. (org-skip-over-state-notes)
  10663. (skip-chars-backward " \t\n\r")))
  10664. (move-marker org-log-note-marker (point))
  10665. (setq org-log-note-purpose purpose
  10666. org-log-note-state state
  10667. org-log-note-previous-state prev-state
  10668. org-log-note-how how
  10669. org-log-note-extra extra)
  10670. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10671. (defun org-skip-over-state-notes ()
  10672. "Skip past the list of State notes in an entry."
  10673. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10674. (when (org-in-item-p)
  10675. (let ((limit (org-list-bottom-point)))
  10676. (while (looking-at "[ \t]*- State")
  10677. (goto-char (or (org-get-next-item (point) limit)
  10678. (org-get-end-of-item limit)))))))
  10679. (defun org-add-log-note (&optional purpose)
  10680. "Pop up a window for taking a note, and add this note later at point."
  10681. (remove-hook 'post-command-hook 'org-add-log-note)
  10682. (setq org-log-note-window-configuration (current-window-configuration))
  10683. (delete-other-windows)
  10684. (move-marker org-log-note-return-to (point))
  10685. (switch-to-buffer (marker-buffer org-log-note-marker))
  10686. (goto-char org-log-note-marker)
  10687. (org-switch-to-buffer-other-window "*Org Note*")
  10688. (erase-buffer)
  10689. (if (memq org-log-note-how '(time state))
  10690. (let (current-prefix-arg) (org-store-log-note))
  10691. (let ((org-inhibit-startup t)) (org-mode))
  10692. (insert (format "# Insert note for %s.
  10693. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10694. (cond
  10695. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10696. ((eq org-log-note-purpose 'done) "closed todo item")
  10697. ((eq org-log-note-purpose 'state)
  10698. (format "state change from \"%s\" to \"%s\""
  10699. (or org-log-note-previous-state "")
  10700. (or org-log-note-state "")))
  10701. ((eq org-log-note-purpose 'reschedule)
  10702. "rescheduling")
  10703. ((eq org-log-note-purpose 'delschedule)
  10704. "no longer scheduled")
  10705. ((eq org-log-note-purpose 'redeadline)
  10706. "changing deadline")
  10707. ((eq org-log-note-purpose 'deldeadline)
  10708. "removing deadline")
  10709. ((eq org-log-note-purpose 'refile)
  10710. "refiling")
  10711. ((eq org-log-note-purpose 'note)
  10712. "this entry")
  10713. (t (error "This should not happen")))))
  10714. (if org-log-note-extra (insert org-log-note-extra))
  10715. (org-set-local 'org-finish-function 'org-store-log-note)))
  10716. (defvar org-note-abort nil) ; dynamically scoped
  10717. (defun org-store-log-note ()
  10718. "Finish taking a log note, and insert it to where it belongs."
  10719. (let ((txt (buffer-string))
  10720. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10721. lines ind bul)
  10722. (kill-buffer (current-buffer))
  10723. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10724. (setq txt (replace-match "" t t txt)))
  10725. (if (string-match "\\s-+\\'" txt)
  10726. (setq txt (replace-match "" t t txt)))
  10727. (setq lines (org-split-string txt "\n"))
  10728. (when (and note (string-match "\\S-" note))
  10729. (setq note
  10730. (org-replace-escapes
  10731. note
  10732. (list (cons "%u" (user-login-name))
  10733. (cons "%U" user-full-name)
  10734. (cons "%t" (format-time-string
  10735. (org-time-stamp-format 'long 'inactive)
  10736. (current-time)))
  10737. (cons "%T" (format-time-string
  10738. (org-time-stamp-format 'long nil)
  10739. (current-time)))
  10740. (cons "%s" (if org-log-note-state
  10741. (concat "\"" org-log-note-state "\"")
  10742. ""))
  10743. (cons "%S" (if org-log-note-previous-state
  10744. (concat "\"" org-log-note-previous-state "\"")
  10745. "\"\"")))))
  10746. (if lines (setq note (concat note " \\\\")))
  10747. (push note lines))
  10748. (when (or current-prefix-arg org-note-abort)
  10749. (when org-log-into-drawer
  10750. (org-remove-empty-drawer-at
  10751. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10752. org-log-note-marker))
  10753. (setq lines nil))
  10754. (when lines
  10755. (with-current-buffer (marker-buffer org-log-note-marker)
  10756. (save-excursion
  10757. (goto-char org-log-note-marker)
  10758. (move-marker org-log-note-marker nil)
  10759. (end-of-line 1)
  10760. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10761. (setq ind (save-excursion
  10762. (if (org-in-item-p)
  10763. (progn
  10764. (goto-char (org-list-top-point))
  10765. (org-get-indentation))
  10766. (skip-chars-backward " \r\t\n")
  10767. (cond
  10768. ((and (org-at-heading-p)
  10769. org-adapt-indentation)
  10770. (1+ (org-current-level)))
  10771. ((org-at-heading-p) 0)
  10772. (t (org-get-indentation))))))
  10773. (setq bul (org-list-bullet-string "-"))
  10774. (org-indent-line-to ind)
  10775. (insert bul (pop lines))
  10776. (let ((ind-body (+ (length bul) ind)))
  10777. (while lines
  10778. (insert "\n")
  10779. (org-indent-line-to ind-body)
  10780. (insert (pop lines))))
  10781. (message "Note stored")
  10782. (org-back-to-heading t)
  10783. (org-cycle-hide-drawers 'children)))))
  10784. (set-window-configuration org-log-note-window-configuration)
  10785. (with-current-buffer (marker-buffer org-log-note-return-to)
  10786. (goto-char org-log-note-return-to))
  10787. (move-marker org-log-note-return-to nil)
  10788. (and org-log-post-message (message "%s" org-log-post-message)))
  10789. (defun org-remove-empty-drawer-at (drawer pos)
  10790. "Remove an empty drawer DRAWER at position POS.
  10791. POS may also be a marker."
  10792. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10793. (save-excursion
  10794. (save-restriction
  10795. (widen)
  10796. (goto-char pos)
  10797. (if (org-in-regexp
  10798. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10799. (replace-match ""))))))
  10800. (defun org-sparse-tree (&optional arg)
  10801. "Create a sparse tree, prompt for the details.
  10802. This command can create sparse trees. You first need to select the type
  10803. of match used to create the tree:
  10804. t Show all TODO entries.
  10805. T Show entries with a specific TODO keyword.
  10806. m Show entries selected by a tags/property match.
  10807. p Enter a property name and its value (both with completion on existing
  10808. names/values) and show entries with that property.
  10809. r Show entries matching a regular expression (`/' can be used as well)
  10810. d Show deadlines due within `org-deadline-warning-days'.
  10811. b Show deadlines and scheduled items before a date.
  10812. a Show deadlines and scheduled items after a date."
  10813. (interactive "P")
  10814. (let (ans kwd value)
  10815. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  10816. (setq ans (read-char-exclusive))
  10817. (cond
  10818. ((equal ans ?d)
  10819. (call-interactively 'org-check-deadlines))
  10820. ((equal ans ?b)
  10821. (call-interactively 'org-check-before-date))
  10822. ((equal ans ?a)
  10823. (call-interactively 'org-check-after-date))
  10824. ((equal ans ?t)
  10825. (org-show-todo-tree nil))
  10826. ((equal ans ?T)
  10827. (org-show-todo-tree '(4)))
  10828. ((member ans '(?T ?m))
  10829. (call-interactively 'org-match-sparse-tree))
  10830. ((member ans '(?p ?P))
  10831. (setq kwd (org-icompleting-read "Property: "
  10832. (mapcar 'list (org-buffer-property-keys))))
  10833. (setq value (org-icompleting-read "Value: "
  10834. (mapcar 'list (org-property-values kwd))))
  10835. (unless (string-match "\\`{.*}\\'" value)
  10836. (setq value (concat "\"" value "\"")))
  10837. (org-match-sparse-tree arg (concat kwd "=" value)))
  10838. ((member ans '(?r ?R ?/))
  10839. (call-interactively 'org-occur))
  10840. (t (error "No such sparse tree command \"%c\"" ans)))))
  10841. (defvar org-occur-highlights nil
  10842. "List of overlays used for occur matches.")
  10843. (make-variable-buffer-local 'org-occur-highlights)
  10844. (defvar org-occur-parameters nil
  10845. "Parameters of the active org-occur calls.
  10846. This is a list, each call to org-occur pushes as cons cell,
  10847. containing the regular expression and the callback, onto the list.
  10848. The list can contain several entries if `org-occur' has been called
  10849. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10850. will only contain one set of parameters. When the highlights are
  10851. removed (for example with `C-c C-c', or with the next edit (depending
  10852. on `org-remove-highlights-with-change'), this variable is emptied
  10853. as well.")
  10854. (make-variable-buffer-local 'org-occur-parameters)
  10855. (defun org-occur (regexp &optional keep-previous callback)
  10856. "Make a compact tree which shows all matches of REGEXP.
  10857. The tree will show the lines where the regexp matches, and all higher
  10858. headlines above the match. It will also show the heading after the match,
  10859. to make sure editing the matching entry is easy.
  10860. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10861. call to `org-occur' will be kept, to allow stacking of calls to this
  10862. command.
  10863. If CALLBACK is non-nil, it is a function which is called to confirm
  10864. that the match should indeed be shown."
  10865. (interactive "sRegexp: \nP")
  10866. (when (equal regexp "")
  10867. (error "Regexp cannot be empty"))
  10868. (unless keep-previous
  10869. (org-remove-occur-highlights nil nil t))
  10870. (push (cons regexp callback) org-occur-parameters)
  10871. (let ((cnt 0))
  10872. (save-excursion
  10873. (goto-char (point-min))
  10874. (if (or (not keep-previous) ; do not want to keep
  10875. (not org-occur-highlights)) ; no previous matches
  10876. ;; hide everything
  10877. (org-overview))
  10878. (while (re-search-forward regexp nil t)
  10879. (when (or (not callback)
  10880. (save-match-data (funcall callback)))
  10881. (setq cnt (1+ cnt))
  10882. (when org-highlight-sparse-tree-matches
  10883. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10884. (org-show-context 'occur-tree))))
  10885. (when org-remove-highlights-with-change
  10886. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10887. nil 'local))
  10888. (unless org-sparse-tree-open-archived-trees
  10889. (org-hide-archived-subtrees (point-min) (point-max)))
  10890. (run-hooks 'org-occur-hook)
  10891. (if (interactive-p)
  10892. (message "%d match(es) for regexp %s" cnt regexp))
  10893. cnt))
  10894. (defun org-show-context (&optional key)
  10895. "Make sure point and context are visible.
  10896. How much context is shown depends upon the variables
  10897. `org-show-hierarchy-above', `org-show-following-heading'. and
  10898. `org-show-siblings'."
  10899. (let ((heading-p (org-on-heading-p t))
  10900. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10901. (following-p (org-get-alist-option org-show-following-heading key))
  10902. (entry-p (org-get-alist-option org-show-entry-below key))
  10903. (siblings-p (org-get-alist-option org-show-siblings key)))
  10904. (catch 'exit
  10905. ;; Show heading or entry text
  10906. (if (and heading-p (not entry-p))
  10907. (org-flag-heading nil) ; only show the heading
  10908. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10909. (org-show-hidden-entry))) ; show entire entry
  10910. (when following-p
  10911. ;; Show next sibling, or heading below text
  10912. (save-excursion
  10913. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10914. (org-flag-heading nil))))
  10915. (when siblings-p (org-show-siblings))
  10916. (when hierarchy-p
  10917. ;; show all higher headings, possibly with siblings
  10918. (save-excursion
  10919. (while (and (condition-case nil
  10920. (progn (org-up-heading-all 1) t)
  10921. (error nil))
  10922. (not (bobp)))
  10923. (org-flag-heading nil)
  10924. (when siblings-p (org-show-siblings))))))))
  10925. (defvar org-reveal-start-hook nil
  10926. "Hook run before revealing a location.")
  10927. (defun org-reveal (&optional siblings)
  10928. "Show current entry, hierarchy above it, and the following headline.
  10929. This can be used to show a consistent set of context around locations
  10930. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10931. not t for the search context.
  10932. With optional argument SIBLINGS, on each level of the hierarchy all
  10933. siblings are shown. This repairs the tree structure to what it would
  10934. look like when opened with hierarchical calls to `org-cycle'.
  10935. With double optional argument \\[universal-argument] \\[universal-argument], \
  10936. go to the parent and show the
  10937. entire tree."
  10938. (interactive "P")
  10939. (run-hooks 'org-reveal-start-hook)
  10940. (let ((org-show-hierarchy-above t)
  10941. (org-show-following-heading t)
  10942. (org-show-siblings (if siblings t org-show-siblings)))
  10943. (org-show-context nil))
  10944. (when (equal siblings '(16))
  10945. (save-excursion
  10946. (when (org-up-heading-safe)
  10947. (org-show-subtree)
  10948. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10949. (defun org-highlight-new-match (beg end)
  10950. "Highlight from BEG to END and mark the highlight is an occur headline."
  10951. (let ((ov (make-overlay beg end)))
  10952. (overlay-put ov 'face 'secondary-selection)
  10953. (push ov org-occur-highlights)))
  10954. (defun org-remove-occur-highlights (&optional beg end noremove)
  10955. "Remove the occur highlights from the buffer.
  10956. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10957. from the `before-change-functions' in the current buffer."
  10958. (interactive)
  10959. (unless org-inhibit-highlight-removal
  10960. (mapc 'delete-overlay org-occur-highlights)
  10961. (setq org-occur-highlights nil)
  10962. (setq org-occur-parameters nil)
  10963. (unless noremove
  10964. (remove-hook 'before-change-functions
  10965. 'org-remove-occur-highlights 'local))))
  10966. ;;;; Priorities
  10967. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10968. "Regular expression matching the priority indicator.")
  10969. (defvar org-remove-priority-next-time nil)
  10970. (defun org-priority-up ()
  10971. "Increase the priority of the current item."
  10972. (interactive)
  10973. (org-priority 'up))
  10974. (defun org-priority-down ()
  10975. "Decrease the priority of the current item."
  10976. (interactive)
  10977. (org-priority 'down))
  10978. (defun org-priority (&optional action)
  10979. "Change the priority of an item by ARG.
  10980. ACTION can be `set', `up', `down', or a character."
  10981. (interactive)
  10982. (unless org-enable-priority-commands
  10983. (error "Priority commands are disabled"))
  10984. (setq action (or action 'set))
  10985. (let (current new news have remove)
  10986. (save-excursion
  10987. (org-back-to-heading t)
  10988. (if (looking-at org-priority-regexp)
  10989. (setq current (string-to-char (match-string 2))
  10990. have t)
  10991. (setq current org-default-priority))
  10992. (cond
  10993. ((eq action 'remove)
  10994. (setq remove t new ?\ ))
  10995. ((or (eq action 'set)
  10996. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10997. (if (not (eq action 'set))
  10998. (setq new action)
  10999. (message "Priority %c-%c, SPC to remove: "
  11000. org-highest-priority org-lowest-priority)
  11001. (save-match-data
  11002. (setq new (read-char-exclusive))))
  11003. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11004. (= (upcase org-lowest-priority) org-lowest-priority))
  11005. (setq new (upcase new)))
  11006. (cond ((equal new ?\ ) (setq remove t))
  11007. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11008. (error "Priority must be between `%c' and `%c'"
  11009. org-highest-priority org-lowest-priority))))
  11010. ((eq action 'up)
  11011. (if (and (not have) (eq last-command this-command))
  11012. (setq new org-lowest-priority)
  11013. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11014. org-default-priority (1- current)))))
  11015. ((eq action 'down)
  11016. (if (and (not have) (eq last-command this-command))
  11017. (setq new org-highest-priority)
  11018. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11019. org-default-priority (1+ current)))))
  11020. (t (error "Invalid action")))
  11021. (if (or (< (upcase new) org-highest-priority)
  11022. (> (upcase new) org-lowest-priority))
  11023. (setq remove t))
  11024. (setq news (format "%c" new))
  11025. (if have
  11026. (if remove
  11027. (replace-match "" t t nil 1)
  11028. (replace-match news t t nil 2))
  11029. (if remove
  11030. (error "No priority cookie found in line")
  11031. (let ((case-fold-search nil))
  11032. (looking-at org-todo-line-regexp))
  11033. (if (match-end 2)
  11034. (progn
  11035. (goto-char (match-end 2))
  11036. (insert " [#" news "]"))
  11037. (goto-char (match-beginning 3))
  11038. (insert "[#" news "] "))))
  11039. (org-preserve-lc (org-set-tags nil 'align)))
  11040. (if remove
  11041. (message "Priority removed")
  11042. (message "Priority of current item set to %s" news))))
  11043. (defun org-get-priority (s)
  11044. "Find priority cookie and return priority."
  11045. (if (functionp org-get-priority-function)
  11046. (funcall org-get-priority-function)
  11047. (save-match-data
  11048. (if (not (string-match org-priority-regexp s))
  11049. (* 1000 (- org-lowest-priority org-default-priority))
  11050. (* 1000 (- org-lowest-priority
  11051. (string-to-char (match-string 2 s))))))))
  11052. ;;;; Tags
  11053. (defvar org-agenda-archives-mode)
  11054. (defvar org-map-continue-from nil
  11055. "Position from where mapping should continue.
  11056. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11057. (defvar org-scanner-tags nil
  11058. "The current tag list while the tags scanner is running.")
  11059. (defvar org-trust-scanner-tags nil
  11060. "Should `org-get-tags-at' use the tags fro the scanner.
  11061. This is for internal dynamical scoping only.
  11062. When this is non-nil, the function `org-get-tags-at' will return the value
  11063. of `org-scanner-tags' instead of building the list by itself. This
  11064. can lead to large speed-ups when the tags scanner is used in a file with
  11065. many entries, and when the list of tags is retrieved, for example to
  11066. obtain a list of properties. Building the tags list for each entry in such
  11067. a file becomes an N^2 operation - but with this variable set, it scales
  11068. as N.")
  11069. (defun org-scan-tags (action matcher &optional todo-only)
  11070. "Scan headline tags with inheritance and produce output ACTION.
  11071. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11072. or `agenda' to produce an entry list for an agenda view. It can also be
  11073. a Lisp form or a function that should be called at each matched headline, in
  11074. this case the return value is a list of all return values from these calls.
  11075. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11076. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11077. only lines with a TODO keyword are included in the output."
  11078. (require 'org-agenda)
  11079. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11080. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11081. (org-re
  11082. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11083. (props (list 'face 'default
  11084. 'done-face 'org-agenda-done
  11085. 'undone-face 'default
  11086. 'mouse-face 'highlight
  11087. 'org-not-done-regexp org-not-done-regexp
  11088. 'org-todo-regexp org-todo-regexp
  11089. 'help-echo
  11090. (format "mouse-2 or RET jump to org file %s"
  11091. (abbreviate-file-name
  11092. (or (buffer-file-name (buffer-base-buffer))
  11093. (buffer-name (buffer-base-buffer)))))))
  11094. (case-fold-search nil)
  11095. (org-map-continue-from nil)
  11096. lspos tags tags-list
  11097. (tags-alist (list (cons 0 org-file-tags)))
  11098. (llast 0) rtn rtn1 level category i txt
  11099. todo marker entry priority)
  11100. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11101. (setq action (list 'lambda nil action)))
  11102. (save-excursion
  11103. (goto-char (point-min))
  11104. (when (eq action 'sparse-tree)
  11105. (org-overview)
  11106. (org-remove-occur-highlights))
  11107. (while (re-search-forward re nil t)
  11108. (catch :skip
  11109. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11110. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11111. (goto-char (setq lspos (match-beginning 0)))
  11112. (setq level (org-reduced-level (funcall outline-level))
  11113. category (org-get-category))
  11114. (setq i llast llast level)
  11115. ;; remove tag lists from same and sublevels
  11116. (while (>= i level)
  11117. (when (setq entry (assoc i tags-alist))
  11118. (setq tags-alist (delete entry tags-alist)))
  11119. (setq i (1- i)))
  11120. ;; add the next tags
  11121. (when tags
  11122. (setq tags (org-split-string tags ":")
  11123. tags-alist
  11124. (cons (cons level tags) tags-alist)))
  11125. ;; compile tags for current headline
  11126. (setq tags-list
  11127. (if org-use-tag-inheritance
  11128. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11129. tags)
  11130. org-scanner-tags tags-list)
  11131. (when org-use-tag-inheritance
  11132. (setcdr (car tags-alist)
  11133. (mapcar (lambda (x)
  11134. (setq x (copy-sequence x))
  11135. (org-add-prop-inherited x))
  11136. (cdar tags-alist))))
  11137. (when (and tags org-use-tag-inheritance
  11138. (or (not (eq t org-use-tag-inheritance))
  11139. org-tags-exclude-from-inheritance))
  11140. ;; selective inheritance, remove uninherited ones
  11141. (setcdr (car tags-alist)
  11142. (org-remove-uniherited-tags (cdar tags-alist))))
  11143. (when (and (or (not todo-only)
  11144. (and (member todo org-not-done-keywords)
  11145. (or (not org-agenda-tags-todo-honor-ignore-options)
  11146. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11147. (let ((case-fold-search t)) (eval matcher))
  11148. (or
  11149. (not (member org-archive-tag tags-list))
  11150. ;; we have an archive tag, should we use this anyway?
  11151. (or (not org-agenda-skip-archived-trees)
  11152. (and (eq action 'agenda) org-agenda-archives-mode))))
  11153. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11154. ;; select this headline
  11155. (cond
  11156. ((eq action 'sparse-tree)
  11157. (and org-highlight-sparse-tree-matches
  11158. (org-get-heading) (match-end 0)
  11159. (org-highlight-new-match
  11160. (match-beginning 0) (match-beginning 1)))
  11161. (org-show-context 'tags-tree))
  11162. ((eq action 'agenda)
  11163. (setq txt (org-format-agenda-item
  11164. ""
  11165. (concat
  11166. (if (eq org-tags-match-list-sublevels 'indented)
  11167. (make-string (1- level) ?.) "")
  11168. (org-get-heading))
  11169. category
  11170. tags-list
  11171. )
  11172. priority (org-get-priority txt))
  11173. (goto-char lspos)
  11174. (setq marker (org-agenda-new-marker))
  11175. (org-add-props txt props
  11176. 'org-marker marker 'org-hd-marker marker 'org-category category
  11177. 'todo-state todo
  11178. 'priority priority 'type "tagsmatch")
  11179. (push txt rtn))
  11180. ((functionp action)
  11181. (setq org-map-continue-from nil)
  11182. (save-excursion
  11183. (setq rtn1 (funcall action))
  11184. (push rtn1 rtn)))
  11185. (t (error "Invalid action")))
  11186. ;; if we are to skip sublevels, jump to end of subtree
  11187. (unless org-tags-match-list-sublevels
  11188. (org-end-of-subtree t)
  11189. (backward-char 1))))
  11190. ;; Get the correct position from where to continue
  11191. (if org-map-continue-from
  11192. (goto-char org-map-continue-from)
  11193. (and (= (point) lspos) (end-of-line 1)))))
  11194. (when (and (eq action 'sparse-tree)
  11195. (not org-sparse-tree-open-archived-trees))
  11196. (org-hide-archived-subtrees (point-min) (point-max)))
  11197. (nreverse rtn)))
  11198. (defun org-remove-uniherited-tags (tags)
  11199. "Remove all tags that are not inherited from the list TAGS."
  11200. (cond
  11201. ((eq org-use-tag-inheritance t)
  11202. (if org-tags-exclude-from-inheritance
  11203. (org-delete-all org-tags-exclude-from-inheritance tags)
  11204. tags))
  11205. ((not org-use-tag-inheritance) nil)
  11206. ((stringp org-use-tag-inheritance)
  11207. (delq nil (mapcar
  11208. (lambda (x)
  11209. (if (and (string-match org-use-tag-inheritance x)
  11210. (not (member x org-tags-exclude-from-inheritance)))
  11211. x nil))
  11212. tags)))
  11213. ((listp org-use-tag-inheritance)
  11214. (delq nil (mapcar
  11215. (lambda (x)
  11216. (if (member x org-use-tag-inheritance) x nil))
  11217. tags)))))
  11218. (defvar todo-only) ;; dynamically scoped
  11219. (defun org-match-sparse-tree (&optional todo-only match)
  11220. "Create a sparse tree according to tags string MATCH.
  11221. MATCH can contain positive and negative selection of tags, like
  11222. \"+WORK+URGENT-WITHBOSS\".
  11223. If optional argument TODO-ONLY is non-nil, only select lines that are
  11224. also TODO lines."
  11225. (interactive "P")
  11226. (org-prepare-agenda-buffers (list (current-buffer)))
  11227. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11228. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11229. (defvar org-cached-props nil)
  11230. (defun org-cached-entry-get (pom property)
  11231. (if (or (eq t org-use-property-inheritance)
  11232. (and (stringp org-use-property-inheritance)
  11233. (string-match org-use-property-inheritance property))
  11234. (and (listp org-use-property-inheritance)
  11235. (member property org-use-property-inheritance)))
  11236. ;; Caching is not possible, check it directly
  11237. (org-entry-get pom property 'inherit)
  11238. ;; Get all properties, so that we can do complicated checks easily
  11239. (cdr (assoc property (or org-cached-props
  11240. (setq org-cached-props
  11241. (org-entry-properties pom)))))))
  11242. (defun org-global-tags-completion-table (&optional files)
  11243. "Return the list of all tags in all agenda buffer/files.
  11244. Optional FILES argument is a list of files to which can be used
  11245. instead of the agenda files."
  11246. (save-excursion
  11247. (org-uniquify
  11248. (delq nil
  11249. (apply 'append
  11250. (mapcar
  11251. (lambda (file)
  11252. (set-buffer (find-file-noselect file))
  11253. (append (org-get-buffer-tags)
  11254. (mapcar (lambda (x) (if (stringp (car-safe x))
  11255. (list (car-safe x)) nil))
  11256. org-tag-alist)))
  11257. (if (and files (car files))
  11258. files
  11259. (org-agenda-files))))))))
  11260. (defun org-make-tags-matcher (match)
  11261. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11262. ;; todo-only is scoped dynamically into this function, and the function
  11263. ;; may change it if the matcher asks for it.
  11264. (unless match
  11265. ;; Get a new match request, with completion
  11266. (let ((org-last-tags-completion-table
  11267. (org-global-tags-completion-table)))
  11268. (setq match (org-completing-read-no-i
  11269. "Match: " 'org-tags-completion-function nil nil nil
  11270. 'org-tags-history))))
  11271. ;; Parse the string and create a lisp form
  11272. (let ((match0 match)
  11273. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11274. minus tag mm
  11275. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11276. orterms term orlist re-p str-p level-p level-op time-p
  11277. prop-p pn pv po gv rest)
  11278. (if (string-match "/+" match)
  11279. ;; match contains also a todo-matching request
  11280. (progn
  11281. (setq tagsmatch (substring match 0 (match-beginning 0))
  11282. todomatch (substring match (match-end 0)))
  11283. (if (string-match "^!" todomatch)
  11284. (setq todo-only t todomatch (substring todomatch 1)))
  11285. (if (string-match "^\\s-*$" todomatch)
  11286. (setq todomatch nil)))
  11287. ;; only matching tags
  11288. (setq tagsmatch match todomatch nil))
  11289. ;; Make the tags matcher
  11290. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11291. (setq tagsmatcher t)
  11292. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11293. (while (setq term (pop orterms))
  11294. (while (and (equal (substring term -1) "\\") orterms)
  11295. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11296. (while (string-match re term)
  11297. (setq rest (substring term (match-end 0))
  11298. minus (and (match-end 1)
  11299. (equal (match-string 1 term) "-"))
  11300. tag (save-match-data (replace-regexp-in-string
  11301. "\\\\-" "-"
  11302. (match-string 2 term)))
  11303. re-p (equal (string-to-char tag) ?{)
  11304. level-p (match-end 4)
  11305. prop-p (match-end 5)
  11306. mm (cond
  11307. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11308. (level-p
  11309. (setq level-op (org-op-to-function (match-string 3 term)))
  11310. `(,level-op level ,(string-to-number
  11311. (match-string 4 term))))
  11312. (prop-p
  11313. (setq pn (match-string 5 term)
  11314. po (match-string 6 term)
  11315. pv (match-string 7 term)
  11316. re-p (equal (string-to-char pv) ?{)
  11317. str-p (equal (string-to-char pv) ?\")
  11318. time-p (save-match-data
  11319. (string-match "^\"[[<].*[]>]\"$" pv))
  11320. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11321. (if time-p (setq pv (org-matcher-time pv)))
  11322. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11323. (cond
  11324. ((equal pn "CATEGORY")
  11325. (setq gv '(get-text-property (point) 'org-category)))
  11326. ((equal pn "TODO")
  11327. (setq gv 'todo))
  11328. (t
  11329. (setq gv `(org-cached-entry-get nil ,pn))))
  11330. (if re-p
  11331. (if (eq po 'org<>)
  11332. `(not (string-match ,pv (or ,gv "")))
  11333. `(string-match ,pv (or ,gv "")))
  11334. (if str-p
  11335. `(,po (or ,gv "") ,pv)
  11336. `(,po (string-to-number (or ,gv ""))
  11337. ,(string-to-number pv) ))))
  11338. (t `(member ,tag tags-list)))
  11339. mm (if minus (list 'not mm) mm)
  11340. term rest)
  11341. (push mm tagsmatcher))
  11342. (push (if (> (length tagsmatcher) 1)
  11343. (cons 'and tagsmatcher)
  11344. (car tagsmatcher))
  11345. orlist)
  11346. (setq tagsmatcher nil))
  11347. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11348. (setq tagsmatcher
  11349. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11350. ;; Make the todo matcher
  11351. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11352. (setq todomatcher t)
  11353. (setq orterms (org-split-string todomatch "|") orlist nil)
  11354. (while (setq term (pop orterms))
  11355. (while (string-match re term)
  11356. (setq minus (and (match-end 1)
  11357. (equal (match-string 1 term) "-"))
  11358. kwd (match-string 2 term)
  11359. re-p (equal (string-to-char kwd) ?{)
  11360. term (substring term (match-end 0))
  11361. mm (if re-p
  11362. `(string-match ,(substring kwd 1 -1) todo)
  11363. (list 'equal 'todo kwd))
  11364. mm (if minus (list 'not mm) mm))
  11365. (push mm todomatcher))
  11366. (push (if (> (length todomatcher) 1)
  11367. (cons 'and todomatcher)
  11368. (car todomatcher))
  11369. orlist)
  11370. (setq todomatcher nil))
  11371. (setq todomatcher (if (> (length orlist) 1)
  11372. (cons 'or orlist) (car orlist))))
  11373. ;; Return the string and lisp forms of the matcher
  11374. (setq matcher (if todomatcher
  11375. (list 'and tagsmatcher todomatcher)
  11376. tagsmatcher))
  11377. (cons match0 matcher)))
  11378. (defun org-op-to-function (op &optional stringp)
  11379. "Turn an operator into the appropriate function."
  11380. (setq op
  11381. (cond
  11382. ((equal op "<" ) '(< string< org-time<))
  11383. ((equal op ">" ) '(> org-string> org-time>))
  11384. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11385. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11386. ((member op '("=" "==")) '(= string= org-time=))
  11387. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11388. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11389. (defun org<> (a b) (not (= a b)))
  11390. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11391. (defun org-string>= (a b) (not (string< a b)))
  11392. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11393. (defun org-string<> (a b) (not (string= a b)))
  11394. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11395. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11396. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11397. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11398. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11399. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11400. (defun org-2ft (s)
  11401. "Convert S to a floating point time.
  11402. If S is already a number, just return it. If it is a string, parse
  11403. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11404. (cond
  11405. ((numberp s) s)
  11406. ((stringp s)
  11407. (condition-case nil
  11408. (float-time (apply 'encode-time (org-parse-time-string s)))
  11409. (error 0.)))
  11410. (t 0.)))
  11411. (defun org-time-today ()
  11412. "Time in seconds today at 0:00.
  11413. Returns the float number of seconds since the beginning of the
  11414. epoch to the beginning of today (00:00)."
  11415. (float-time (apply 'encode-time
  11416. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11417. (defun org-matcher-time (s)
  11418. "Interpret a time comparison value."
  11419. (save-match-data
  11420. (cond
  11421. ((string= s "<now>") (float-time))
  11422. ((string= s "<today>") (org-time-today))
  11423. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11424. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11425. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11426. (+ (org-time-today)
  11427. (* (string-to-number (match-string 1 s))
  11428. (cdr (assoc (match-string 2 s)
  11429. '(("d" . 86400.0) ("w" . 604800.0)
  11430. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11431. (t (org-2ft s)))))
  11432. (defun org-match-any-p (re list)
  11433. "Does re match any element of list?"
  11434. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11435. (delq nil list))
  11436. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11437. (defvar org-tags-overlay (make-overlay 1 1))
  11438. (org-detach-overlay org-tags-overlay)
  11439. (defun org-get-local-tags-at (&optional pos)
  11440. "Get a list of tags defined in the current headline."
  11441. (org-get-tags-at pos 'local))
  11442. (defun org-get-local-tags ()
  11443. "Get a list of tags defined in the current headline."
  11444. (org-get-tags-at nil 'local))
  11445. (defun org-get-tags-at (&optional pos local)
  11446. "Get a list of all headline tags applicable at POS.
  11447. POS defaults to point. If tags are inherited, the list contains
  11448. the targets in the same sequence as the headlines appear, i.e.
  11449. the tags of the current headline come last.
  11450. When LOCAL is non-nil, only return tags from the current headline,
  11451. ignore inherited ones."
  11452. (interactive)
  11453. (if (and org-trust-scanner-tags
  11454. (or (not pos) (equal pos (point)))
  11455. (not local))
  11456. org-scanner-tags
  11457. (let (tags ltags lastpos parent)
  11458. (save-excursion
  11459. (save-restriction
  11460. (widen)
  11461. (goto-char (or pos (point)))
  11462. (save-match-data
  11463. (catch 'done
  11464. (condition-case nil
  11465. (progn
  11466. (org-back-to-heading t)
  11467. (while (not (equal lastpos (point)))
  11468. (setq lastpos (point))
  11469. (when (looking-at
  11470. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11471. (setq ltags (org-split-string
  11472. (org-match-string-no-properties 1) ":"))
  11473. (when parent
  11474. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11475. (setq tags (append
  11476. (if parent
  11477. (org-remove-uniherited-tags ltags)
  11478. ltags)
  11479. tags)))
  11480. (or org-use-tag-inheritance (throw 'done t))
  11481. (if local (throw 'done t))
  11482. (or (org-up-heading-safe) (error nil))
  11483. (setq parent t)))
  11484. (error nil)))))
  11485. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11486. (defun org-add-prop-inherited (s)
  11487. (add-text-properties 0 (length s) '(inherited t) s)
  11488. s)
  11489. (defun org-toggle-tag (tag &optional onoff)
  11490. "Toggle the tag TAG for the current line.
  11491. If ONOFF is `on' or `off', don't toggle but set to this state."
  11492. (let (res current)
  11493. (save-excursion
  11494. (org-back-to-heading t)
  11495. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11496. (point-at-eol) t)
  11497. (progn
  11498. (setq current (match-string 1))
  11499. (replace-match ""))
  11500. (setq current ""))
  11501. (setq current (nreverse (org-split-string current ":")))
  11502. (cond
  11503. ((eq onoff 'on)
  11504. (setq res t)
  11505. (or (member tag current) (push tag current)))
  11506. ((eq onoff 'off)
  11507. (or (not (member tag current)) (setq current (delete tag current))))
  11508. (t (if (member tag current)
  11509. (setq current (delete tag current))
  11510. (setq res t)
  11511. (push tag current))))
  11512. (end-of-line 1)
  11513. (if current
  11514. (progn
  11515. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11516. (org-set-tags nil t))
  11517. (delete-horizontal-space))
  11518. (run-hooks 'org-after-tags-change-hook))
  11519. res))
  11520. (defun org-align-tags-here (to-col)
  11521. ;; Assumes that this is a headline
  11522. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11523. (beginning-of-line 1)
  11524. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11525. (< pos (match-beginning 2)))
  11526. (progn
  11527. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11528. (goto-char (match-beginning 1))
  11529. (insert " ")
  11530. (delete-region (point) (1+ (match-beginning 2)))
  11531. (setq ncol (max (1+ (current-column))
  11532. (1+ col)
  11533. (if (> to-col 0)
  11534. to-col
  11535. (- (abs to-col) tags-l))))
  11536. (setq p (point))
  11537. (insert (make-string (- ncol (current-column)) ?\ ))
  11538. (setq ncol (current-column))
  11539. (when indent-tabs-mode (tabify p (point-at-eol)))
  11540. (org-move-to-column (min ncol col) t))
  11541. (goto-char pos))))
  11542. (defun org-set-tags-command (&optional arg just-align)
  11543. "Call the set-tags command for the current entry."
  11544. (interactive "P")
  11545. (if (org-on-heading-p)
  11546. (org-set-tags arg just-align)
  11547. (save-excursion
  11548. (org-back-to-heading t)
  11549. (org-set-tags arg just-align))))
  11550. (defun org-set-tags-to (data)
  11551. "Set the tags of the current entry to DATA, replacing the current tags.
  11552. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11553. If DATA is nil or the empty string, any tags will be removed."
  11554. (interactive "sTags: ")
  11555. (setq data
  11556. (cond
  11557. ((eq data nil) "")
  11558. ((equal data "") "")
  11559. ((stringp data)
  11560. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11561. ":"))
  11562. ((listp data)
  11563. (concat ":" (mapconcat 'identity data ":") ":"))
  11564. (t nil)))
  11565. (when data
  11566. (save-excursion
  11567. (org-back-to-heading t)
  11568. (when (looking-at org-complex-heading-regexp)
  11569. (if (match-end 5)
  11570. (progn
  11571. (goto-char (match-beginning 5))
  11572. (insert data)
  11573. (delete-region (point) (point-at-eol))
  11574. (org-set-tags nil 'align))
  11575. (goto-char (point-at-eol))
  11576. (insert " " data)
  11577. (org-set-tags nil 'align)))
  11578. (beginning-of-line 1)
  11579. (if (looking-at ".*?\\([ \t]+\\)$")
  11580. (delete-region (match-beginning 1) (match-end 1))))))
  11581. (defun org-align-all-tags ()
  11582. "Align the tags i all headings."
  11583. (interactive)
  11584. (save-excursion
  11585. (or (ignore-errors (org-back-to-heading t))
  11586. (outline-next-heading))
  11587. (if (org-on-heading-p)
  11588. (org-set-tags t)
  11589. (message "No headings"))))
  11590. (defvar org-indent-indentation-per-level)
  11591. (defun org-set-tags (&optional arg just-align)
  11592. "Set the tags for the current headline.
  11593. With prefix ARG, realign all tags in headings in the current buffer."
  11594. (interactive "P")
  11595. (let* ((re (concat "^" outline-regexp))
  11596. (current (org-get-tags-string))
  11597. (col (current-column))
  11598. (org-setting-tags t)
  11599. table current-tags inherited-tags ; computed below when needed
  11600. tags p0 c0 c1 rpl di tc level)
  11601. (if arg
  11602. (save-excursion
  11603. (goto-char (point-min))
  11604. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11605. (while (re-search-forward re nil t)
  11606. (org-set-tags nil t)
  11607. (end-of-line 1)))
  11608. (message "All tags realigned to column %d" org-tags-column))
  11609. (if just-align
  11610. (setq tags current)
  11611. ;; Get a new set of tags from the user
  11612. (save-excursion
  11613. (setq table (append org-tag-persistent-alist
  11614. (or org-tag-alist (org-get-buffer-tags))
  11615. (and
  11616. org-complete-tags-always-offer-all-agenda-tags
  11617. (org-global-tags-completion-table
  11618. (org-agenda-files))))
  11619. org-last-tags-completion-table table
  11620. current-tags (org-split-string current ":")
  11621. inherited-tags (nreverse
  11622. (nthcdr (length current-tags)
  11623. (nreverse (org-get-tags-at))))
  11624. tags
  11625. (if (or (eq t org-use-fast-tag-selection)
  11626. (and org-use-fast-tag-selection
  11627. (delq nil (mapcar 'cdr table))))
  11628. (org-fast-tag-selection
  11629. current-tags inherited-tags table
  11630. (if org-fast-tag-selection-include-todo
  11631. org-todo-key-alist))
  11632. (let ((org-add-colon-after-tag-completion t))
  11633. (org-trim
  11634. (org-without-partial-completion
  11635. (org-icompleting-read "Tags: "
  11636. 'org-tags-completion-function
  11637. nil nil current 'org-tags-history)))))))
  11638. (while (string-match "[-+&]+" tags)
  11639. ;; No boolean logic, just a list
  11640. (setq tags (replace-match ":" t t tags))))
  11641. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  11642. (if org-tags-sort-function
  11643. (setq tags (mapconcat 'identity
  11644. (sort (org-split-string
  11645. tags (org-re "[^[:alnum:]_@#%]+"))
  11646. org-tags-sort-function) ":")))
  11647. (if (string-match "\\`[\t ]*\\'" tags)
  11648. (setq tags "")
  11649. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11650. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11651. ;; Insert new tags at the correct column
  11652. (beginning-of-line 1)
  11653. (setq level (or (and (looking-at org-outline-regexp)
  11654. (- (match-end 0) (point) 1))
  11655. 1))
  11656. (cond
  11657. ((and (equal current "") (equal tags "")))
  11658. ((re-search-forward
  11659. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11660. (point-at-eol) t)
  11661. (if (equal tags "")
  11662. (setq rpl "")
  11663. (goto-char (match-beginning 0))
  11664. (setq c0 (current-column)
  11665. ;; compute offset for the case of org-indent-mode active
  11666. di (if org-indent-mode
  11667. (* (1- org-indent-indentation-per-level) (1- level))
  11668. 0)
  11669. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11670. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11671. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11672. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11673. (replace-match rpl t t)
  11674. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11675. tags)
  11676. (t (error "Tags alignment failed")))
  11677. (org-move-to-column col)
  11678. (unless just-align
  11679. (run-hooks 'org-after-tags-change-hook)))))
  11680. (defun org-change-tag-in-region (beg end tag off)
  11681. "Add or remove TAG for each entry in the region.
  11682. This works in the agenda, and also in an org-mode buffer."
  11683. (interactive
  11684. (list (region-beginning) (region-end)
  11685. (let ((org-last-tags-completion-table
  11686. (if (org-mode-p)
  11687. (org-get-buffer-tags)
  11688. (org-global-tags-completion-table))))
  11689. (org-icompleting-read
  11690. "Tag: " 'org-tags-completion-function nil nil nil
  11691. 'org-tags-history))
  11692. (progn
  11693. (message "[s]et or [r]emove? ")
  11694. (equal (read-char-exclusive) ?r))))
  11695. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11696. (let ((agendap (equal major-mode 'org-agenda-mode))
  11697. l1 l2 m buf pos newhead (cnt 0))
  11698. (goto-char end)
  11699. (setq l2 (1- (org-current-line)))
  11700. (goto-char beg)
  11701. (setq l1 (org-current-line))
  11702. (loop for l from l1 to l2 do
  11703. (org-goto-line l)
  11704. (setq m (get-text-property (point) 'org-hd-marker))
  11705. (when (or (and (org-mode-p) (org-on-heading-p))
  11706. (and agendap m))
  11707. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11708. pos (if agendap m (point)))
  11709. (with-current-buffer buf
  11710. (save-excursion
  11711. (save-restriction
  11712. (goto-char pos)
  11713. (setq cnt (1+ cnt))
  11714. (org-toggle-tag tag (if off 'off 'on))
  11715. (setq newhead (org-get-heading)))))
  11716. (and agendap (org-agenda-change-all-lines newhead m))))
  11717. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11718. (defun org-tags-completion-function (string predicate &optional flag)
  11719. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11720. (confirm (lambda (x) (stringp (car x)))))
  11721. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11722. (setq s1 (match-string 1 string)
  11723. s2 (match-string 2 string))
  11724. (setq s1 "" s2 string))
  11725. (cond
  11726. ((eq flag nil)
  11727. ;; try completion
  11728. (setq rtn (try-completion s2 ctable confirm))
  11729. (if (stringp rtn)
  11730. (setq rtn
  11731. (concat s1 s2 (substring rtn (length s2))
  11732. (if (and org-add-colon-after-tag-completion
  11733. (assoc rtn ctable))
  11734. ":" ""))))
  11735. rtn)
  11736. ((eq flag t)
  11737. ;; all-completions
  11738. (all-completions s2 ctable confirm)
  11739. )
  11740. ((eq flag 'lambda)
  11741. ;; exact match?
  11742. (assoc s2 ctable)))
  11743. ))
  11744. (defun org-fast-tag-insert (kwd tags face &optional end)
  11745. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11746. (insert (format "%-12s" (concat kwd ":"))
  11747. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11748. (or end "")))
  11749. (defun org-fast-tag-show-exit (flag)
  11750. (save-excursion
  11751. (org-goto-line 3)
  11752. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11753. (replace-match ""))
  11754. (when flag
  11755. (end-of-line 1)
  11756. (org-move-to-column (- (window-width) 19) t)
  11757. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11758. (defun org-set-current-tags-overlay (current prefix)
  11759. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11760. (if (featurep 'xemacs)
  11761. (org-overlay-display org-tags-overlay (concat prefix s)
  11762. 'secondary-selection)
  11763. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11764. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11765. (defvar org-last-tag-selection-key nil)
  11766. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11767. "Fast tag selection with single keys.
  11768. CURRENT is the current list of tags in the headline, INHERITED is the
  11769. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11770. possibly with grouping information. TODO-TABLE is a similar table with
  11771. TODO keywords, should these have keys assigned to them.
  11772. If the keys are nil, a-z are automatically assigned.
  11773. Returns the new tags string, or nil to not change the current settings."
  11774. (let* ((fulltable (append table todo-table))
  11775. (maxlen (apply 'max (mapcar
  11776. (lambda (x)
  11777. (if (stringp (car x)) (string-width (car x)) 0))
  11778. fulltable)))
  11779. (buf (current-buffer))
  11780. (expert (eq org-fast-tag-selection-single-key 'expert))
  11781. (buffer-tags nil)
  11782. (fwidth (+ maxlen 3 1 3))
  11783. (ncol (/ (- (window-width) 4) fwidth))
  11784. (i-face 'org-done)
  11785. (c-face 'org-todo)
  11786. tg cnt e c char c1 c2 ntable tbl rtn
  11787. ov-start ov-end ov-prefix
  11788. (exit-after-next org-fast-tag-selection-single-key)
  11789. (done-keywords org-done-keywords)
  11790. groups ingroup)
  11791. (save-excursion
  11792. (beginning-of-line 1)
  11793. (if (looking-at
  11794. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11795. (setq ov-start (match-beginning 1)
  11796. ov-end (match-end 1)
  11797. ov-prefix "")
  11798. (setq ov-start (1- (point-at-eol))
  11799. ov-end (1+ ov-start))
  11800. (skip-chars-forward "^\n\r")
  11801. (setq ov-prefix
  11802. (concat
  11803. (buffer-substring (1- (point)) (point))
  11804. (if (> (current-column) org-tags-column)
  11805. " "
  11806. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11807. (move-overlay org-tags-overlay ov-start ov-end)
  11808. (save-window-excursion
  11809. (if expert
  11810. (set-buffer (get-buffer-create " *Org tags*"))
  11811. (delete-other-windows)
  11812. (split-window-vertically)
  11813. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11814. (erase-buffer)
  11815. (org-set-local 'org-done-keywords done-keywords)
  11816. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11817. (org-fast-tag-insert "Current" current c-face "\n\n")
  11818. (org-fast-tag-show-exit exit-after-next)
  11819. (org-set-current-tags-overlay current ov-prefix)
  11820. (setq tbl fulltable char ?a cnt 0)
  11821. (while (setq e (pop tbl))
  11822. (cond
  11823. ((equal (car e) :startgroup)
  11824. (push '() groups) (setq ingroup t)
  11825. (when (not (= cnt 0))
  11826. (setq cnt 0)
  11827. (insert "\n"))
  11828. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11829. ((equal (car e) :endgroup)
  11830. (setq ingroup nil cnt 0)
  11831. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11832. ((equal e '(:newline))
  11833. (when (not (= cnt 0))
  11834. (setq cnt 0)
  11835. (insert "\n")
  11836. (setq e (car tbl))
  11837. (while (equal (car tbl) '(:newline))
  11838. (insert "\n")
  11839. (setq tbl (cdr tbl)))))
  11840. (t
  11841. (setq tg (copy-sequence (car e)) c2 nil)
  11842. (if (cdr e)
  11843. (setq c (cdr e))
  11844. ;; automatically assign a character.
  11845. (setq c1 (string-to-char
  11846. (downcase (substring
  11847. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11848. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11849. (while (or (rassoc char ntable) (rassoc char table))
  11850. (setq char (1+ char)))
  11851. (setq c2 c1))
  11852. (setq c (or c2 char)))
  11853. (if ingroup (push tg (car groups)))
  11854. (setq tg (org-add-props tg nil 'face
  11855. (cond
  11856. ((not (assoc tg table))
  11857. (org-get-todo-face tg))
  11858. ((member tg current) c-face)
  11859. ((member tg inherited) i-face)
  11860. (t nil))))
  11861. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11862. (insert "[" c "] " tg (make-string
  11863. (- fwidth 4 (length tg)) ?\ ))
  11864. (push (cons tg c) ntable)
  11865. (when (= (setq cnt (1+ cnt)) ncol)
  11866. (insert "\n")
  11867. (if ingroup (insert " "))
  11868. (setq cnt 0)))))
  11869. (setq ntable (nreverse ntable))
  11870. (insert "\n")
  11871. (goto-char (point-min))
  11872. (if (not expert) (org-fit-window-to-buffer))
  11873. (setq rtn
  11874. (catch 'exit
  11875. (while t
  11876. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11877. (if (not groups) "no " "")
  11878. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11879. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11880. (setq org-last-tag-selection-key c)
  11881. (cond
  11882. ((= c ?\r) (throw 'exit t))
  11883. ((= c ?!)
  11884. (setq groups (not groups))
  11885. (goto-char (point-min))
  11886. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11887. ((= c ?\C-c)
  11888. (if (not expert)
  11889. (org-fast-tag-show-exit
  11890. (setq exit-after-next (not exit-after-next)))
  11891. (setq expert nil)
  11892. (delete-other-windows)
  11893. (set-window-buffer (split-window-vertically) " *Org tags*")
  11894. (org-switch-to-buffer-other-window " *Org tags*")
  11895. (org-fit-window-to-buffer)))
  11896. ((or (= c ?\C-g)
  11897. (and (= c ?q) (not (rassoc c ntable))))
  11898. (org-detach-overlay org-tags-overlay)
  11899. (setq quit-flag t))
  11900. ((= c ?\ )
  11901. (setq current nil)
  11902. (if exit-after-next (setq exit-after-next 'now)))
  11903. ((= c ?\t)
  11904. (condition-case nil
  11905. (setq tg (org-icompleting-read
  11906. "Tag: "
  11907. (or buffer-tags
  11908. (with-current-buffer buf
  11909. (org-get-buffer-tags)))))
  11910. (quit (setq tg "")))
  11911. (when (string-match "\\S-" tg)
  11912. (add-to-list 'buffer-tags (list tg))
  11913. (if (member tg current)
  11914. (setq current (delete tg current))
  11915. (push tg current)))
  11916. (if exit-after-next (setq exit-after-next 'now)))
  11917. ((setq e (rassoc c todo-table) tg (car e))
  11918. (with-current-buffer buf
  11919. (save-excursion (org-todo tg)))
  11920. (if exit-after-next (setq exit-after-next 'now)))
  11921. ((setq e (rassoc c ntable) tg (car e))
  11922. (if (member tg current)
  11923. (setq current (delete tg current))
  11924. (loop for g in groups do
  11925. (if (member tg g)
  11926. (mapc (lambda (x)
  11927. (setq current (delete x current)))
  11928. g)))
  11929. (push tg current))
  11930. (if exit-after-next (setq exit-after-next 'now))))
  11931. ;; Create a sorted list
  11932. (setq current
  11933. (sort current
  11934. (lambda (a b)
  11935. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11936. (if (eq exit-after-next 'now) (throw 'exit t))
  11937. (goto-char (point-min))
  11938. (beginning-of-line 2)
  11939. (delete-region (point) (point-at-eol))
  11940. (org-fast-tag-insert "Current" current c-face)
  11941. (org-set-current-tags-overlay current ov-prefix)
  11942. (while (re-search-forward
  11943. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  11944. (setq tg (match-string 1))
  11945. (add-text-properties
  11946. (match-beginning 1) (match-end 1)
  11947. (list 'face
  11948. (cond
  11949. ((member tg current) c-face)
  11950. ((member tg inherited) i-face)
  11951. (t (get-text-property (match-beginning 1) 'face))))))
  11952. (goto-char (point-min)))))
  11953. (org-detach-overlay org-tags-overlay)
  11954. (if rtn
  11955. (mapconcat 'identity current ":")
  11956. nil))))
  11957. (defun org-get-tags-string ()
  11958. "Get the TAGS string in the current headline."
  11959. (unless (org-on-heading-p t)
  11960. (error "Not on a heading"))
  11961. (save-excursion
  11962. (beginning-of-line 1)
  11963. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11964. (org-match-string-no-properties 1)
  11965. "")))
  11966. (defun org-get-tags ()
  11967. "Get the list of tags specified in the current headline."
  11968. (org-split-string (org-get-tags-string) ":"))
  11969. (defun org-get-buffer-tags ()
  11970. "Get a table of all tags used in the buffer, for completion."
  11971. (let (tags)
  11972. (save-excursion
  11973. (goto-char (point-min))
  11974. (while (re-search-forward
  11975. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  11976. (when (equal (char-after (point-at-bol 0)) ?*)
  11977. (mapc (lambda (x) (add-to-list 'tags x))
  11978. (org-split-string (org-match-string-no-properties 1) ":")))))
  11979. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11980. (mapcar 'list tags)))
  11981. ;;;; The mapping API
  11982. ;;;###autoload
  11983. (defun org-map-entries (func &optional match scope &rest skip)
  11984. "Call FUNC at each headline selected by MATCH in SCOPE.
  11985. FUNC is a function or a lisp form. The function will be called without
  11986. arguments, with the cursor positioned at the beginning of the headline.
  11987. The return values of all calls to the function will be collected and
  11988. returned as a list.
  11989. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11990. does not need to preserve point. After evaluation, the cursor will be
  11991. moved to the end of the line (presumably of the headline of the
  11992. processed entry) and search continues from there. Under some
  11993. circumstances, this may not produce the wanted results. For example,
  11994. if you have removed (e.g. archived) the current (sub)tree it could
  11995. mean that the next entry will be skipped entirely. In such cases, you
  11996. can specify the position from where search should continue by making
  11997. FUNC set the variable `org-map-continue-from' to the desired buffer
  11998. position.
  11999. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12000. Only headlines that are matched by this query will be considered during
  12001. the iteration. When MATCH is nil or t, all headlines will be
  12002. visited by the iteration.
  12003. SCOPE determines the scope of this command. It can be any of:
  12004. nil The current buffer, respecting the restriction if any
  12005. tree The subtree started with the entry at point
  12006. file The current buffer, without restriction
  12007. file-with-archives
  12008. The current buffer, and any archives associated with it
  12009. agenda All agenda files
  12010. agenda-with-archives
  12011. All agenda files with any archive files associated with them
  12012. \(file1 file2 ...)
  12013. If this is a list, all files in the list will be scanned
  12014. The remaining args are treated as settings for the skipping facilities of
  12015. the scanner. The following items can be given here:
  12016. archive skip trees with the archive tag.
  12017. comment skip trees with the COMMENT keyword
  12018. function or Emacs Lisp form:
  12019. will be used as value for `org-agenda-skip-function', so whenever
  12020. the function returns t, FUNC will not be called for that
  12021. entry and search will continue from the point where the
  12022. function leaves it.
  12023. If your function needs to retrieve the tags including inherited tags
  12024. at the *current* entry, you can use the value of the variable
  12025. `org-scanner-tags' which will be much faster than getting the value
  12026. with `org-get-tags-at'. If your function gets properties with
  12027. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12028. to t around the call to `org-entry-properties' to get the same speedup.
  12029. Note that if your function moves around to retrieve tags and properties at
  12030. a *different* entry, you cannot use these techniques."
  12031. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12032. (org-agenda-skip-archived-trees (memq 'archive skip))
  12033. (org-agenda-skip-comment-trees (memq 'comment skip))
  12034. (org-agenda-skip-function
  12035. (car (org-delete-all '(comment archive) skip)))
  12036. (org-tags-match-list-sublevels t)
  12037. matcher file res
  12038. org-todo-keywords-for-agenda
  12039. org-done-keywords-for-agenda
  12040. org-todo-keyword-alist-for-agenda
  12041. org-drawers-for-agenda
  12042. org-tag-alist-for-agenda)
  12043. (cond
  12044. ((eq match t) (setq matcher t))
  12045. ((eq match nil) (setq matcher t))
  12046. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12047. (save-excursion
  12048. (save-restriction
  12049. (when (eq scope 'tree)
  12050. (org-back-to-heading t)
  12051. (org-narrow-to-subtree)
  12052. (setq scope nil))
  12053. (if (not scope)
  12054. (progn
  12055. (org-prepare-agenda-buffers
  12056. (list (buffer-file-name (current-buffer))))
  12057. (setq res (org-scan-tags func matcher)))
  12058. ;; Get the right scope
  12059. (cond
  12060. ((and scope (listp scope) (symbolp (car scope)))
  12061. (setq scope (eval scope)))
  12062. ((eq scope 'agenda)
  12063. (setq scope (org-agenda-files t)))
  12064. ((eq scope 'agenda-with-archives)
  12065. (setq scope (org-agenda-files t))
  12066. (setq scope (org-add-archive-files scope)))
  12067. ((eq scope 'file)
  12068. (setq scope (list (buffer-file-name))))
  12069. ((eq scope 'file-with-archives)
  12070. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12071. (org-prepare-agenda-buffers scope)
  12072. (while (setq file (pop scope))
  12073. (with-current-buffer (org-find-base-buffer-visiting file)
  12074. (save-excursion
  12075. (save-restriction
  12076. (widen)
  12077. (goto-char (point-min))
  12078. (setq res (append res (org-scan-tags func matcher))))))))))
  12079. res))
  12080. ;;;; Properties
  12081. ;;; Setting and retrieving properties
  12082. (defconst org-special-properties
  12083. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12084. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE")
  12085. "The special properties valid in Org-mode.
  12086. These are properties that are not defined in the property drawer,
  12087. but in some other way.")
  12088. (defconst org-default-properties
  12089. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12090. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12091. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12092. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12093. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12094. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12095. "Some properties that are used by Org-mode for various purposes.
  12096. Being in this list makes sure that they are offered for completion.")
  12097. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12098. "Regular expression matching the first line of a property drawer.")
  12099. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12100. "Regular expression matching the last line of a property drawer.")
  12101. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12102. "Regular expression matching the first line of a property drawer.")
  12103. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12104. "Regular expression matching the first line of a property drawer.")
  12105. (defconst org-property-drawer-re
  12106. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12107. org-property-end-re "\\)\n?")
  12108. "Matches an entire property drawer.")
  12109. (defconst org-clock-drawer-re
  12110. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12111. org-property-end-re "\\)\n?")
  12112. "Matches an entire clock drawer.")
  12113. (defun org-property-action ()
  12114. "Do an action on properties."
  12115. (interactive)
  12116. (let (c)
  12117. (org-at-property-p)
  12118. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12119. (setq c (read-char-exclusive))
  12120. (cond
  12121. ((equal c ?s)
  12122. (call-interactively 'org-set-property))
  12123. ((equal c ?d)
  12124. (call-interactively 'org-delete-property))
  12125. ((equal c ?D)
  12126. (call-interactively 'org-delete-property-globally))
  12127. ((equal c ?c)
  12128. (call-interactively 'org-compute-property-at-point))
  12129. (t (error "No such property action %c" c)))))
  12130. (defun org-set-effort (&optional value)
  12131. "Set the effort property of the current entry.
  12132. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12133. allowed value."
  12134. (interactive "P")
  12135. (if (equal value 0) (setq value 10))
  12136. (let* ((completion-ignore-case t)
  12137. (prop org-effort-property)
  12138. (cur (org-entry-get nil prop))
  12139. (allowed (org-property-get-allowed-values nil prop 'table))
  12140. (existing (mapcar 'list (org-property-values prop)))
  12141. rpl
  12142. (val (cond
  12143. ((stringp value) value)
  12144. ((and allowed (integerp value))
  12145. (or (car (nth (1- value) allowed))
  12146. (car (org-last allowed))))
  12147. (allowed
  12148. (message "Select 1-9,0, [RET%s]: %s"
  12149. (if cur (concat "=" cur) "")
  12150. (mapconcat 'car allowed " "))
  12151. (setq rpl (read-char-exclusive))
  12152. (if (equal rpl ?\r)
  12153. cur
  12154. (setq rpl (- rpl ?0))
  12155. (if (equal rpl 0) (setq rpl 10))
  12156. (if (and (> rpl 0) (<= rpl (length allowed)))
  12157. (car (nth (1- rpl) allowed))
  12158. (org-completing-read "Effort: " allowed nil))))
  12159. (t
  12160. (let (org-completion-use-ido org-completion-use-iswitchb)
  12161. (org-completing-read
  12162. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12163. (concat "[" cur "]") "")
  12164. ": ")
  12165. existing nil nil "" nil cur))))))
  12166. (unless (equal (org-entry-get nil prop) val)
  12167. (org-entry-put nil prop val))
  12168. (message "%s is now %s" prop val)))
  12169. (defun org-at-property-p ()
  12170. "Is cursor inside a property drawer?"
  12171. (save-excursion
  12172. (beginning-of-line 1)
  12173. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12174. (save-match-data ;; Used by calling procedures
  12175. (let ((p (point))
  12176. (range (unless (org-before-first-heading-p)
  12177. (org-get-property-block))))
  12178. (and range (<= (car range) p) (< p (cdr range))))))))
  12179. (defun org-get-property-block (&optional beg end force)
  12180. "Return the (beg . end) range of the body of the property drawer.
  12181. BEG and END can be beginning and end of subtree, if not given
  12182. they will be found.
  12183. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12184. (catch 'exit
  12185. (save-excursion
  12186. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12187. (end (or end (progn (outline-next-heading) (point)))))
  12188. (goto-char beg)
  12189. (if (re-search-forward org-property-start-re end t)
  12190. (setq beg (1+ (match-end 0)))
  12191. (if force
  12192. (save-excursion
  12193. (org-insert-property-drawer)
  12194. (setq end (progn (outline-next-heading) (point))))
  12195. (throw 'exit nil))
  12196. (goto-char beg)
  12197. (if (re-search-forward org-property-start-re end t)
  12198. (setq beg (1+ (match-end 0)))))
  12199. (if (re-search-forward org-property-end-re end t)
  12200. (setq end (match-beginning 0))
  12201. (or force (throw 'exit nil))
  12202. (goto-char beg)
  12203. (setq end beg)
  12204. (org-indent-line-function)
  12205. (insert ":END:\n"))
  12206. (cons beg end)))))
  12207. (defun org-entry-properties (&optional pom which specific)
  12208. "Get all properties of the entry at point-or-marker POM.
  12209. This includes the TODO keyword, the tags, time strings for deadline,
  12210. scheduled, and clocking, and any additional properties defined in the
  12211. entry. The return value is an alist, keys may occur multiple times
  12212. if the property key was used several times.
  12213. POM may also be nil, in which case the current entry is used.
  12214. If WHICH is nil or `all', get all properties. If WHICH is
  12215. `special' or `standard', only get that subclass. If WHICH
  12216. is a string only get exactly this property. SPECIFIC can be a string, the
  12217. specific property we are interested in. Specifying it can speed
  12218. things up because then unnecessary parsing is avoided."
  12219. (setq which (or which 'all))
  12220. (org-with-point-at pom
  12221. (let ((clockstr (substring org-clock-string 0 -1))
  12222. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12223. (case-fold-search nil)
  12224. beg end range props sum-props key key1 value string clocksum)
  12225. (save-excursion
  12226. (when (condition-case nil
  12227. (and (org-mode-p) (org-back-to-heading t))
  12228. (error nil))
  12229. (setq beg (point))
  12230. (setq sum-props (get-text-property (point) 'org-summaries))
  12231. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12232. (outline-next-heading)
  12233. (setq end (point))
  12234. (when (memq which '(all special))
  12235. ;; Get the special properties, like TODO and tags
  12236. (goto-char beg)
  12237. (when (and (or (not specific) (string= specific "TODO"))
  12238. (looking-at org-todo-line-regexp) (match-end 2))
  12239. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12240. (when (and (or (not specific) (string= specific "PRIORITY"))
  12241. (looking-at org-priority-regexp))
  12242. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12243. (when (or (not specific) (string= specific "FILE"))
  12244. (push (cons "FILE" buffer-file-name) props))
  12245. (when (and (or (not specific) (string= specific "TAGS"))
  12246. (setq value (org-get-tags-string))
  12247. (string-match "\\S-" value))
  12248. (push (cons "TAGS" value) props))
  12249. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12250. (setq value (org-get-tags-at)))
  12251. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12252. ":"))
  12253. props))
  12254. (when (or (not specific) (string= specific "BLOCKED"))
  12255. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12256. (when (or (not specific)
  12257. (member specific
  12258. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12259. "TIMESTAMP" "TIMESTAMP_IA")))
  12260. (catch 'match
  12261. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12262. (setq key (if (match-end 1)
  12263. (substring (org-match-string-no-properties 1)
  12264. 0 -1))
  12265. string (if (equal key clockstr)
  12266. (org-no-properties
  12267. (org-trim
  12268. (buffer-substring
  12269. (match-beginning 3) (goto-char
  12270. (point-at-eol)))))
  12271. (substring (org-match-string-no-properties 3)
  12272. 1 -1)))
  12273. ;; Get the correct property name from the key. This is
  12274. ;; necessary if the user has configured time keywords.
  12275. (setq key1 (concat key ":"))
  12276. (cond
  12277. ((not key)
  12278. (setq key
  12279. (if (= (char-after (match-beginning 3)) ?\[)
  12280. "TIMESTAMP_IA" "TIMESTAMP")))
  12281. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12282. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12283. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12284. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12285. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12286. (progn
  12287. (push (cons key string) props)
  12288. ;; no need to search further if match is found
  12289. (throw 'match t))
  12290. (when (or (equal key "CLOCK") (not (assoc key props)))
  12291. (push (cons key string) props))))))
  12292. )
  12293. (when (memq which '(all standard))
  12294. ;; Get the standard properties, like :PROP: ...
  12295. (setq range (org-get-property-block beg end))
  12296. (when range
  12297. (goto-char (car range))
  12298. (while (re-search-forward
  12299. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12300. (cdr range) t)
  12301. (setq key (org-match-string-no-properties 1)
  12302. value (org-trim (or (org-match-string-no-properties 2) "")))
  12303. (unless (member key excluded)
  12304. (push (cons key (or value "")) props)))))
  12305. (if clocksum
  12306. (push (cons "CLOCKSUM"
  12307. (org-columns-number-to-string (/ (float clocksum) 60.)
  12308. 'add_times))
  12309. props))
  12310. (unless (assoc "CATEGORY" props)
  12311. (push (cons "CATEGORY" (org-get-category)) props))
  12312. (append sum-props (nreverse props)))))))
  12313. (defun org-entry-get (pom property &optional inherit literal-nil)
  12314. "Get value of PROPERTY for entry at point-or-marker POM.
  12315. If INHERIT is non-nil and the entry does not have the property,
  12316. then also check higher levels of the hierarchy.
  12317. If INHERIT is the symbol `selective', use inheritance only if the setting
  12318. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12319. If the property is present but empty, the return value is the empty string.
  12320. If the property is not present at all, nil is returned.
  12321. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12322. do not interpret it as the list atom nil. This is used for inheritance
  12323. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12324. (org-with-point-at pom
  12325. (if (and inherit (if (eq inherit 'selective)
  12326. (org-property-inherit-p property)
  12327. t))
  12328. (org-entry-get-with-inheritance property literal-nil)
  12329. (if (member property org-special-properties)
  12330. ;; We need a special property. Use `org-entry-properties' to
  12331. ;; retrieve it, but specify the wanted property
  12332. (cdr (assoc property (org-entry-properties nil 'special property)))
  12333. (let ((range (org-get-property-block)))
  12334. (if (and range
  12335. (goto-char (car range))
  12336. (re-search-forward
  12337. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12338. (cdr range) t))
  12339. ;; Found the property, return it.
  12340. (if (match-end 1)
  12341. (if literal-nil
  12342. (org-match-string-no-properties 1)
  12343. (org-not-nil (org-match-string-no-properties 1)))
  12344. "")))))))
  12345. (defun org-property-or-variable-value (var &optional inherit)
  12346. "Check if there is a property fixing the value of VAR.
  12347. If yes, return this value. If not, return the current value of the variable."
  12348. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12349. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12350. (read prop)
  12351. (symbol-value var))))
  12352. (defun org-entry-delete (pom property)
  12353. "Delete the property PROPERTY from entry at point-or-marker POM."
  12354. (org-with-point-at pom
  12355. (if (member property org-special-properties)
  12356. nil ; cannot delete these properties.
  12357. (let ((range (org-get-property-block)))
  12358. (if (and range
  12359. (goto-char (car range))
  12360. (re-search-forward
  12361. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12362. (cdr range) t))
  12363. (progn
  12364. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12365. t)
  12366. nil)))))
  12367. ;; Multi-values properties are properties that contain multiple values
  12368. ;; These values are assumed to be single words, separated by whitespace.
  12369. (defun org-entry-add-to-multivalued-property (pom property value)
  12370. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12371. (let* ((old (org-entry-get pom property))
  12372. (values (and old (org-split-string old "[ \t]"))))
  12373. (setq value (org-entry-protect-space value))
  12374. (unless (member value values)
  12375. (setq values (cons value values))
  12376. (org-entry-put pom property
  12377. (mapconcat 'identity values " ")))))
  12378. (defun org-entry-remove-from-multivalued-property (pom property value)
  12379. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12380. (let* ((old (org-entry-get pom property))
  12381. (values (and old (org-split-string old "[ \t]"))))
  12382. (setq value (org-entry-protect-space value))
  12383. (when (member value values)
  12384. (setq values (delete value values))
  12385. (org-entry-put pom property
  12386. (mapconcat 'identity values " ")))))
  12387. (defun org-entry-member-in-multivalued-property (pom property value)
  12388. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12389. (let* ((old (org-entry-get pom property))
  12390. (values (and old (org-split-string old "[ \t]"))))
  12391. (setq value (org-entry-protect-space value))
  12392. (member value values)))
  12393. (defun org-entry-get-multivalued-property (pom property)
  12394. "Return a list of values in a multivalued property."
  12395. (let* ((value (org-entry-get pom property))
  12396. (values (and value (org-split-string value "[ \t]"))))
  12397. (mapcar 'org-entry-restore-space values)))
  12398. (defun org-entry-put-multivalued-property (pom property &rest values)
  12399. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12400. VALUES should be a list of strings. Spaces will be protected."
  12401. (org-entry-put pom property
  12402. (mapconcat 'org-entry-protect-space values " "))
  12403. (let* ((value (org-entry-get pom property))
  12404. (values (and value (org-split-string value "[ \t]"))))
  12405. (mapcar 'org-entry-restore-space values)))
  12406. (defun org-entry-protect-space (s)
  12407. "Protect spaces and newline in string S."
  12408. (while (string-match " " s)
  12409. (setq s (replace-match "%20" t t s)))
  12410. (while (string-match "\n" s)
  12411. (setq s (replace-match "%0A" t t s)))
  12412. s)
  12413. (defun org-entry-restore-space (s)
  12414. "Restore spaces and newline in string S."
  12415. (while (string-match "%20" s)
  12416. (setq s (replace-match " " t t s)))
  12417. (while (string-match "%0A" s)
  12418. (setq s (replace-match "\n" t t s)))
  12419. s)
  12420. (defvar org-entry-property-inherited-from (make-marker)
  12421. "Marker pointing to the entry from where a property was inherited.
  12422. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12423. location of the entry where the inheritance search matched. If there was
  12424. no match, the marker will point nowhere.
  12425. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12426. is set.")
  12427. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12428. "Get entry property, and search higher levels if not present.
  12429. The search will stop at the first ancestor which has the property defined.
  12430. If the value found is \"nil\", return nil to show that the property
  12431. should be considered as undefined (this is the meaning of nil here).
  12432. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12433. (move-marker org-entry-property-inherited-from nil)
  12434. (let (tmp)
  12435. (save-excursion
  12436. (save-restriction
  12437. (widen)
  12438. (catch 'ex
  12439. (while t
  12440. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12441. (org-back-to-heading t)
  12442. (move-marker org-entry-property-inherited-from (point))
  12443. (throw 'ex tmp))
  12444. (or (org-up-heading-safe) (throw 'ex nil)))))
  12445. (setq tmp (or tmp
  12446. (cdr (assoc property org-file-properties))
  12447. (cdr (assoc property org-global-properties))
  12448. (cdr (assoc property org-global-properties-fixed))))
  12449. (if literal-nil tmp (org-not-nil tmp)))))
  12450. (defvar org-property-changed-functions nil
  12451. "Hook called when the value of a property has changed.
  12452. Each hook function should accept two arguments, the name of the property
  12453. and the new value.")
  12454. (defun org-entry-put (pom property value)
  12455. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12456. (org-with-point-at pom
  12457. (org-back-to-heading t)
  12458. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12459. range)
  12460. (cond
  12461. ((equal property "TODO")
  12462. (when (and (stringp value) (string-match "\\S-" value)
  12463. (not (member value org-todo-keywords-1)))
  12464. (error "\"%s\" is not a valid TODO state" value))
  12465. (if (or (not value)
  12466. (not (string-match "\\S-" value)))
  12467. (setq value 'none))
  12468. (org-todo value)
  12469. (org-set-tags nil 'align))
  12470. ((equal property "PRIORITY")
  12471. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12472. (string-to-char value) ?\ ))
  12473. (org-set-tags nil 'align))
  12474. ((equal property "SCHEDULED")
  12475. (if (re-search-forward org-scheduled-time-regexp end t)
  12476. (cond
  12477. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12478. ((eq value 'later) (org-timestamp-change 1 'day))
  12479. (t (call-interactively 'org-schedule)))
  12480. (call-interactively 'org-schedule)))
  12481. ((equal property "DEADLINE")
  12482. (if (re-search-forward org-deadline-time-regexp end t)
  12483. (cond
  12484. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12485. ((eq value 'later) (org-timestamp-change 1 'day))
  12486. (t (call-interactively 'org-deadline)))
  12487. (call-interactively 'org-deadline)))
  12488. ((member property org-special-properties)
  12489. (error "The %s property can not yet be set with `org-entry-put'"
  12490. property))
  12491. (t ; a non-special property
  12492. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12493. (setq range (org-get-property-block beg end 'force))
  12494. (goto-char (car range))
  12495. (if (re-search-forward
  12496. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12497. (progn
  12498. (delete-region (match-beginning 1) (match-end 1))
  12499. (goto-char (match-beginning 1)))
  12500. (goto-char (cdr range))
  12501. (insert "\n")
  12502. (backward-char 1)
  12503. (org-indent-line-function)
  12504. (insert ":" property ":"))
  12505. (and value (insert " " value))
  12506. (org-indent-line-function)))))
  12507. (run-hook-with-args 'org-property-changed-functions property value)))
  12508. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12509. "Get all property keys in the current buffer.
  12510. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12511. like tags and TODO state.
  12512. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12513. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12514. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12515. formats in the current buffer."
  12516. (let (rtn range cfmt s p)
  12517. (save-excursion
  12518. (save-restriction
  12519. (widen)
  12520. (goto-char (point-min))
  12521. (while (re-search-forward org-property-start-re nil t)
  12522. (setq range (org-get-property-block))
  12523. (goto-char (car range))
  12524. (while (re-search-forward
  12525. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12526. (cdr range) t)
  12527. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12528. (outline-next-heading))))
  12529. (when include-specials
  12530. (setq rtn (append org-special-properties rtn)))
  12531. (when include-defaults
  12532. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12533. (add-to-list 'rtn org-effort-property))
  12534. (when include-columns
  12535. (save-excursion
  12536. (save-restriction
  12537. (widen)
  12538. (goto-char (point-min))
  12539. (while (re-search-forward
  12540. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12541. nil t)
  12542. (setq cfmt (match-string 2) s 0)
  12543. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12544. cfmt s)
  12545. (setq s (match-end 0)
  12546. p (match-string 1 cfmt))
  12547. (unless (or (equal p "ITEM")
  12548. (member p org-special-properties))
  12549. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12550. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12551. (defun org-property-values (key)
  12552. "Return a list of all values of property KEY."
  12553. (save-excursion
  12554. (save-restriction
  12555. (widen)
  12556. (goto-char (point-min))
  12557. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12558. values)
  12559. (while (re-search-forward re nil t)
  12560. (add-to-list 'values (org-trim (match-string 1))))
  12561. (delete "" values)))))
  12562. (defun org-insert-property-drawer ()
  12563. "Insert a property drawer into the current entry."
  12564. (interactive)
  12565. (org-back-to-heading t)
  12566. (looking-at outline-regexp)
  12567. (let ((indent (if org-adapt-indentation
  12568. (- (match-end 0)(match-beginning 0))
  12569. 0))
  12570. (beg (point))
  12571. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12572. end hiddenp)
  12573. (outline-next-heading)
  12574. (setq end (point))
  12575. (goto-char beg)
  12576. (while (re-search-forward re end t))
  12577. (setq hiddenp (org-invisible-p))
  12578. (end-of-line 1)
  12579. (and (equal (char-after) ?\n) (forward-char 1))
  12580. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12581. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12582. ;; just skip this line
  12583. (beginning-of-line 2)
  12584. ;; Drawer start, find the end
  12585. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12586. (beginning-of-line 1)))
  12587. (org-skip-over-state-notes)
  12588. (skip-chars-backward " \t\n\r")
  12589. (if (eq (char-before) ?*) (forward-char 1))
  12590. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12591. (beginning-of-line 0)
  12592. (org-indent-to-column indent)
  12593. (beginning-of-line 2)
  12594. (org-indent-to-column indent)
  12595. (beginning-of-line 0)
  12596. (if hiddenp
  12597. (save-excursion
  12598. (org-back-to-heading t)
  12599. (hide-entry))
  12600. (org-flag-drawer t))))
  12601. (defvar org-property-set-functions-alist nil
  12602. "Property set function alist.
  12603. Each entry should have the following format:
  12604. (PROPERTY . READ-FUNCTION)
  12605. The read function will be called with the same argument as
  12606. `org-completing-read.")
  12607. (defun org-set-property-function (property)
  12608. "Get the function that should be used to set PROPERTY.
  12609. This is computed according to `org-property-set-functions-alist'."
  12610. (or (cdr (assoc property org-property-set-functions-alist))
  12611. 'org-completing-read))
  12612. (defun org-read-property-value (property)
  12613. "Read PROPERTY value from user."
  12614. (let* ((completion-ignore-case t)
  12615. (allowed (org-property-get-allowed-values nil property 'table))
  12616. (cur (org-entry-get nil property))
  12617. (prompt (concat property " value"
  12618. (if (and cur (string-match "\\S-" cur))
  12619. (concat " [" cur "]") "") ": "))
  12620. (set-function (org-set-property-function property))
  12621. (val (if allowed
  12622. (funcall set-function prompt allowed nil
  12623. (not (get-text-property 0 'org-unrestricted
  12624. (caar allowed))))
  12625. (let (org-completion-use-ido org-completion-use-iswitchb)
  12626. (funcall set-function prompt
  12627. (mapcar 'list (org-property-values property))
  12628. nil nil "" nil cur)))))
  12629. (if (equal val "")
  12630. cur
  12631. val)))
  12632. (defun org-read-property-name ()
  12633. "Read a property name."
  12634. (let* ((completion-ignore-case t)
  12635. (keys (org-buffer-property-keys nil t t))
  12636. (property (org-icompleting-read "Property: " (mapcar 'list keys))))
  12637. (if (member property keys)
  12638. property
  12639. (or (cdr (assoc (downcase property)
  12640. (mapcar (lambda (x) (cons (downcase x) x))
  12641. keys)))
  12642. property))))
  12643. (defun org-set-property (property value)
  12644. "In the current entry, set PROPERTY to VALUE.
  12645. When called interactively, this will prompt for a property name, offering
  12646. completion on existing and default properties. And then it will prompt
  12647. for a value, offering completion either on allowed values (via an inherited
  12648. xxx_ALL property) or on existing values in other instances of this property
  12649. in the current file."
  12650. (interactive (list nil nil))
  12651. (let* ((property (or property (org-read-property-name)))
  12652. (value (or value (org-read-property-value property))))
  12653. (unless (equal (org-entry-get nil property) value)
  12654. (org-entry-put nil property value))))
  12655. (defun org-delete-property (property)
  12656. "In the current entry, delete PROPERTY."
  12657. (interactive
  12658. (let* ((completion-ignore-case t)
  12659. (prop (org-icompleting-read "Property: "
  12660. (org-entry-properties nil 'standard))))
  12661. (list prop)))
  12662. (message "Property %s %s" property
  12663. (if (org-entry-delete nil property)
  12664. "deleted"
  12665. "was not present in the entry")))
  12666. (defun org-delete-property-globally (property)
  12667. "Remove PROPERTY globally, from all entries."
  12668. (interactive
  12669. (let* ((completion-ignore-case t)
  12670. (prop (org-icompleting-read
  12671. "Globally remove property: "
  12672. (mapcar 'list (org-buffer-property-keys)))))
  12673. (list prop)))
  12674. (save-excursion
  12675. (save-restriction
  12676. (widen)
  12677. (goto-char (point-min))
  12678. (let ((cnt 0))
  12679. (while (re-search-forward
  12680. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12681. nil t)
  12682. (setq cnt (1+ cnt))
  12683. (replace-match ""))
  12684. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12685. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12686. (defun org-compute-property-at-point ()
  12687. "Compute the property at point.
  12688. This looks for an enclosing column format, extracts the operator and
  12689. then applies it to the property in the column format's scope."
  12690. (interactive)
  12691. (unless (org-at-property-p)
  12692. (error "Not at a property"))
  12693. (let ((prop (org-match-string-no-properties 2)))
  12694. (org-columns-get-format-and-top-level)
  12695. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12696. (error "No operator defined for property %s" prop))
  12697. (org-columns-compute prop)))
  12698. (defvar org-property-allowed-value-functions nil
  12699. "Hook for functions supplying allowed values for a specific property.
  12700. The functions must take a single argument, the name of the property, and
  12701. return a flat list of allowed values. If \":ETC\" is one of
  12702. the values, this means that these values are intended as defaults for
  12703. completion, but that other values should be allowed too.
  12704. The functions must return nil if they are not responsible for this
  12705. property.")
  12706. (defun org-property-get-allowed-values (pom property &optional table)
  12707. "Get allowed values for the property PROPERTY.
  12708. When TABLE is non-nil, return an alist that can directly be used for
  12709. completion."
  12710. (let (vals)
  12711. (cond
  12712. ((equal property "TODO")
  12713. (setq vals (org-with-point-at pom
  12714. (append org-todo-keywords-1 '("")))))
  12715. ((equal property "PRIORITY")
  12716. (let ((n org-lowest-priority))
  12717. (while (>= n org-highest-priority)
  12718. (push (char-to-string n) vals)
  12719. (setq n (1- n)))))
  12720. ((member property org-special-properties))
  12721. ((setq vals (run-hook-with-args-until-success
  12722. 'org-property-allowed-value-functions property)))
  12723. (t
  12724. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12725. (when (and vals (string-match "\\S-" vals))
  12726. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12727. (setq vals (mapcar (lambda (x)
  12728. (cond ((stringp x) x)
  12729. ((numberp x) (number-to-string x))
  12730. ((symbolp x) (symbol-name x))
  12731. (t "???")))
  12732. vals)))))
  12733. (when (member ":ETC" vals)
  12734. (setq vals (remove ":ETC" vals))
  12735. (org-add-props (car vals) '(org-unrestricted t)))
  12736. (if table (mapcar 'list vals) vals)))
  12737. (defun org-property-previous-allowed-value (&optional previous)
  12738. "Switch to the next allowed value for this property."
  12739. (interactive)
  12740. (org-property-next-allowed-value t))
  12741. (defun org-property-next-allowed-value (&optional previous)
  12742. "Switch to the next allowed value for this property."
  12743. (interactive)
  12744. (unless (org-at-property-p)
  12745. (error "Not at a property"))
  12746. (let* ((key (match-string 2))
  12747. (value (match-string 3))
  12748. (allowed (or (org-property-get-allowed-values (point) key)
  12749. (and (member value '("[ ]" "[-]" "[X]"))
  12750. '("[ ]" "[X]"))))
  12751. nval)
  12752. (unless allowed
  12753. (error "Allowed values for this property have not been defined"))
  12754. (if previous (setq allowed (reverse allowed)))
  12755. (if (member value allowed)
  12756. (setq nval (car (cdr (member value allowed)))))
  12757. (setq nval (or nval (car allowed)))
  12758. (if (equal nval value)
  12759. (error "Only one allowed value for this property"))
  12760. (org-at-property-p)
  12761. (replace-match (concat " :" key ": " nval) t t)
  12762. (org-indent-line-function)
  12763. (beginning-of-line 1)
  12764. (skip-chars-forward " \t")
  12765. (run-hook-with-args 'org-property-changed-functions key nval)))
  12766. (defun org-find-olp (path &optional this-buffer)
  12767. "Return a marker pointing to the entry at outline path OLP.
  12768. If anything goes wrong, throw an error.
  12769. You can wrap this call to catch the error like this:
  12770. (condition-case msg
  12771. (org-mobile-locate-entry (match-string 4))
  12772. (error (nth 1 msg)))
  12773. The return value will then be either a string with the error message,
  12774. or a marker if everything is OK.
  12775. If THIS-BUFFER is set, the outline path does not contain a file,
  12776. only headings."
  12777. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12778. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12779. (level 1)
  12780. (lmin 1)
  12781. (lmax 1)
  12782. limit re end found pos heading cnt)
  12783. (unless buffer (error "File not found :%s" file))
  12784. (with-current-buffer buffer
  12785. (save-excursion
  12786. (save-restriction
  12787. (widen)
  12788. (setq limit (point-max))
  12789. (goto-char (point-min))
  12790. (while (setq heading (pop path))
  12791. (setq re (format org-complex-heading-regexp-format
  12792. (regexp-quote heading)))
  12793. (setq cnt 0 pos (point))
  12794. (while (re-search-forward re end t)
  12795. (setq level (- (match-end 1) (match-beginning 1)))
  12796. (if (and (>= level lmin) (<= level lmax))
  12797. (setq found (match-beginning 0) cnt (1+ cnt))))
  12798. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12799. lmax heading))
  12800. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12801. lmax heading))
  12802. (goto-char found)
  12803. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12804. (setq end (save-excursion (org-end-of-subtree t t))))
  12805. (when (org-on-heading-p)
  12806. (move-marker (make-marker) (point))))))))
  12807. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  12808. "Find node HEADING in BUFFER.
  12809. Return a marker to the heading if it was found, or nil if not.
  12810. If POS-ONLY is set, return just the position instead of a marker.
  12811. The heading text must match exact, but it may have a TODO keyword,
  12812. a priority cookie and tags in the standard locations."
  12813. (with-current-buffer (or buffer (current-buffer))
  12814. (save-excursion
  12815. (save-restriction
  12816. (widen)
  12817. (goto-char (point-min))
  12818. (let (case-fold-search)
  12819. (if (re-search-forward
  12820. (format org-complex-heading-regexp-format
  12821. (regexp-quote heading)) nil t)
  12822. (if pos-only
  12823. (match-beginning 0)
  12824. (move-marker (make-marker) (match-beginning 0)))))))))
  12825. (defun org-find-exact-heading-in-directory (heading &optional dir)
  12826. "Find Org node headline HEADING in all .org files in directory DIR.
  12827. When the target headline is found, return a marker to this location."
  12828. (let ((files (directory-files (or dir default-directory)
  12829. nil "\\`[^.#].*\\.org\\'"))
  12830. file visiting m buffer)
  12831. (catch 'found
  12832. (while (setq file (pop files))
  12833. (message "trying %s" file)
  12834. (setq visiting (org-find-base-buffer-visiting file))
  12835. (setq buffer (or visiting (find-file-noselect file)))
  12836. (setq m (org-find-exact-headline-in-buffer
  12837. heading buffer))
  12838. (when (and (not m) (not visiting)) (kill-buffer buffer))
  12839. (and m (throw 'found m))))))
  12840. (defun org-find-entry-with-id (ident)
  12841. "Locate the entry that contains the ID property with exact value IDENT.
  12842. IDENT can be a string, a symbol or a number, this function will search for
  12843. the string representation of it.
  12844. Return the position where this entry starts, or nil if there is no such entry."
  12845. (interactive "sID: ")
  12846. (let ((id (cond
  12847. ((stringp ident) ident)
  12848. ((symbol-name ident) (symbol-name ident))
  12849. ((numberp ident) (number-to-string ident))
  12850. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12851. (case-fold-search nil))
  12852. (save-excursion
  12853. (save-restriction
  12854. (widen)
  12855. (goto-char (point-min))
  12856. (when (re-search-forward
  12857. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12858. nil t)
  12859. (org-back-to-heading t)
  12860. (point))))))
  12861. ;;;; Timestamps
  12862. (defvar org-last-changed-timestamp nil)
  12863. (defvar org-last-inserted-timestamp nil
  12864. "The last time stamp inserted with `org-insert-time-stamp'.")
  12865. (defvar org-time-was-given) ; dynamically scoped parameter
  12866. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12867. (defvar org-ts-what) ; dynamically scoped parameter
  12868. (defun org-time-stamp (arg &optional inactive)
  12869. "Prompt for a date/time and insert a time stamp.
  12870. If the user specifies a time like HH:MM, or if this command is called
  12871. with a prefix argument, the time stamp will contain date and time.
  12872. Otherwise, only the date will be included. All parts of a date not
  12873. specified by the user will be filled in from the current date/time.
  12874. So if you press just return without typing anything, the time stamp
  12875. will represent the current date/time. If there is already a timestamp
  12876. at the cursor, it will be modified."
  12877. (interactive "P")
  12878. (let* ((ts nil)
  12879. (default-time
  12880. ;; Default time is either today, or, when entering a range,
  12881. ;; the range start.
  12882. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12883. (save-excursion
  12884. (re-search-backward
  12885. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12886. (- (point) 20) t)))
  12887. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12888. (current-time)))
  12889. (default-input (and ts (org-get-compact-tod ts)))
  12890. org-time-was-given org-end-time-was-given time)
  12891. (cond
  12892. ((and (org-at-timestamp-p t)
  12893. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12894. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12895. (insert "--")
  12896. (setq time (let ((this-command this-command))
  12897. (org-read-date arg 'totime nil nil
  12898. default-time default-input)))
  12899. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12900. ((org-at-timestamp-p t)
  12901. (setq time (let ((this-command this-command))
  12902. (org-read-date arg 'totime nil nil default-time default-input)))
  12903. (when (org-at-timestamp-p t) ; just to get the match data
  12904. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12905. (replace-match "")
  12906. (setq org-last-changed-timestamp
  12907. (org-insert-time-stamp
  12908. time (or org-time-was-given arg)
  12909. inactive nil nil (list org-end-time-was-given))))
  12910. (message "Timestamp updated"))
  12911. (t
  12912. (setq time (let ((this-command this-command))
  12913. (org-read-date arg 'totime nil nil default-time default-input)))
  12914. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12915. nil nil (list org-end-time-was-given))))))
  12916. ;; FIXME: can we use this for something else, like computing time differences?
  12917. (defun org-get-compact-tod (s)
  12918. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12919. (let* ((t1 (match-string 1 s))
  12920. (h1 (string-to-number (match-string 2 s)))
  12921. (m1 (string-to-number (match-string 3 s)))
  12922. (t2 (and (match-end 4) (match-string 5 s)))
  12923. (h2 (and t2 (string-to-number (match-string 6 s))))
  12924. (m2 (and t2 (string-to-number (match-string 7 s))))
  12925. dh dm)
  12926. (if (not t2)
  12927. t1
  12928. (setq dh (- h2 h1) dm (- m2 m1))
  12929. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12930. (concat t1 "+" (number-to-string dh)
  12931. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12932. (defun org-time-stamp-inactive (&optional arg)
  12933. "Insert an inactive time stamp.
  12934. An inactive time stamp is enclosed in square brackets instead of angle
  12935. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12936. does not link to the calendar and cannot be changed with the S-cursor keys.
  12937. So these are more for recording a certain time/date."
  12938. (interactive "P")
  12939. (org-time-stamp arg 'inactive))
  12940. (defvar org-date-ovl (make-overlay 1 1))
  12941. (overlay-put org-date-ovl 'face 'org-warning)
  12942. (org-detach-overlay org-date-ovl)
  12943. (defvar org-ans1) ; dynamically scoped parameter
  12944. (defvar org-ans2) ; dynamically scoped parameter
  12945. (defvar org-plain-time-of-day-regexp) ; defined below
  12946. (defvar org-overriding-default-time nil) ; dynamically scoped
  12947. (defvar org-read-date-overlay nil)
  12948. (defvar org-dcst nil) ; dynamically scoped
  12949. (defvar org-read-date-history nil)
  12950. (defvar org-read-date-final-answer nil)
  12951. (defun org-read-date (&optional with-time to-time from-string prompt
  12952. default-time default-input)
  12953. "Read a date, possibly a time, and make things smooth for the user.
  12954. The prompt will suggest to enter an ISO date, but you can also enter anything
  12955. which will at least partially be understood by `parse-time-string'.
  12956. Unrecognized parts of the date will default to the current day, month, year,
  12957. hour and minute. If this command is called to replace a timestamp at point,
  12958. of to enter the second timestamp of a range, the default time is taken
  12959. from the existing stamp. Furthermore, the command prefers the future,
  12960. so if you are giving a date where the year is not given, and the day-month
  12961. combination is already past in the current year, it will assume you
  12962. mean next year. For details, see the manual. A few examples:
  12963. 3-2-5 --> 2003-02-05
  12964. feb 15 --> currentyear-02-15
  12965. 2/15 --> currentyear-02-15
  12966. sep 12 9 --> 2009-09-12
  12967. 12:45 --> today 12:45
  12968. 22 sept 0:34 --> currentyear-09-22 0:34
  12969. 12 --> currentyear-currentmonth-12
  12970. Fri --> nearest Friday (today or later)
  12971. etc.
  12972. Furthermore you can specify a relative date by giving, as the *first* thing
  12973. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12974. change in days weeks, months, years.
  12975. With a single plus or minus, the date is relative to today. With a double
  12976. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12977. +4d --> four days from today
  12978. +4 --> same as above
  12979. +2w --> two weeks from today
  12980. ++5 --> five days from default date
  12981. The function understands only English month and weekday abbreviations,
  12982. but this can be configured with the variables `parse-time-months' and
  12983. `parse-time-weekdays'.
  12984. While prompting, a calendar is popped up - you can also select the
  12985. date with the mouse (button 1). The calendar shows a period of three
  12986. months. To scroll it to other months, use the keys `>' and `<'.
  12987. If you don't like the calendar, turn it off with
  12988. \(setq org-read-date-popup-calendar nil)
  12989. With optional argument TO-TIME, the date will immediately be converted
  12990. to an internal time.
  12991. With an optional argument WITH-TIME, the prompt will suggest to also
  12992. insert a time. Note that when WITH-TIME is not set, you can still
  12993. enter a time, and this function will inform the calling routine about
  12994. this change. The calling routine may then choose to change the format
  12995. used to insert the time stamp into the buffer to include the time.
  12996. With optional argument FROM-STRING, read from this string instead from
  12997. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12998. the time/date that is used for everything that is not specified by the
  12999. user."
  13000. (require 'parse-time)
  13001. (let* ((org-time-stamp-rounding-minutes
  13002. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13003. (org-dcst org-display-custom-times)
  13004. (ct (org-current-time))
  13005. (def (or org-overriding-default-time default-time ct))
  13006. (defdecode (decode-time def))
  13007. (dummy (progn
  13008. (when (< (nth 2 defdecode) org-extend-today-until)
  13009. (setcar (nthcdr 2 defdecode) -1)
  13010. (setcar (nthcdr 1 defdecode) 59)
  13011. (setq def (apply 'encode-time defdecode)
  13012. defdecode (decode-time def)))))
  13013. (calendar-frame-setup nil)
  13014. (calendar-setup nil)
  13015. (calendar-move-hook nil)
  13016. (calendar-view-diary-initially-flag nil)
  13017. (calendar-view-holidays-initially-flag nil)
  13018. (timestr (format-time-string
  13019. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13020. (prompt (concat (if prompt (concat prompt " ") "")
  13021. (format "Date+time [%s]: " timestr)))
  13022. ans (org-ans0 "") org-ans1 org-ans2 final)
  13023. (cond
  13024. (from-string (setq ans from-string))
  13025. (org-read-date-popup-calendar
  13026. (save-excursion
  13027. (save-window-excursion
  13028. (calendar)
  13029. (calendar-forward-day (- (time-to-days def)
  13030. (calendar-absolute-from-gregorian
  13031. (calendar-current-date))))
  13032. (org-eval-in-calendar nil t)
  13033. (let* ((old-map (current-local-map))
  13034. (map (copy-keymap calendar-mode-map))
  13035. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13036. (org-defkey map (kbd "RET") 'org-calendar-select)
  13037. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13038. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13039. (org-defkey minibuffer-local-map [(meta shift left)]
  13040. (lambda () (interactive)
  13041. (org-eval-in-calendar '(calendar-backward-month 1))))
  13042. (org-defkey minibuffer-local-map [(meta shift right)]
  13043. (lambda () (interactive)
  13044. (org-eval-in-calendar '(calendar-forward-month 1))))
  13045. (org-defkey minibuffer-local-map [(meta shift up)]
  13046. (lambda () (interactive)
  13047. (org-eval-in-calendar '(calendar-backward-year 1))))
  13048. (org-defkey minibuffer-local-map [(meta shift down)]
  13049. (lambda () (interactive)
  13050. (org-eval-in-calendar '(calendar-forward-year 1))))
  13051. (org-defkey minibuffer-local-map [?\e (shift left)]
  13052. (lambda () (interactive)
  13053. (org-eval-in-calendar '(calendar-backward-month 1))))
  13054. (org-defkey minibuffer-local-map [?\e (shift right)]
  13055. (lambda () (interactive)
  13056. (org-eval-in-calendar '(calendar-forward-month 1))))
  13057. (org-defkey minibuffer-local-map [?\e (shift up)]
  13058. (lambda () (interactive)
  13059. (org-eval-in-calendar '(calendar-backward-year 1))))
  13060. (org-defkey minibuffer-local-map [?\e (shift down)]
  13061. (lambda () (interactive)
  13062. (org-eval-in-calendar '(calendar-forward-year 1))))
  13063. (org-defkey minibuffer-local-map [(shift up)]
  13064. (lambda () (interactive)
  13065. (org-eval-in-calendar '(calendar-backward-week 1))))
  13066. (org-defkey minibuffer-local-map [(shift down)]
  13067. (lambda () (interactive)
  13068. (org-eval-in-calendar '(calendar-forward-week 1))))
  13069. (org-defkey minibuffer-local-map [(shift left)]
  13070. (lambda () (interactive)
  13071. (org-eval-in-calendar '(calendar-backward-day 1))))
  13072. (org-defkey minibuffer-local-map [(shift right)]
  13073. (lambda () (interactive)
  13074. (org-eval-in-calendar '(calendar-forward-day 1))))
  13075. (org-defkey minibuffer-local-map ">"
  13076. (lambda () (interactive)
  13077. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13078. (org-defkey minibuffer-local-map "<"
  13079. (lambda () (interactive)
  13080. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13081. (org-defkey minibuffer-local-map "\C-v"
  13082. (lambda () (interactive)
  13083. (org-eval-in-calendar
  13084. '(calendar-scroll-left-three-months 1))))
  13085. (org-defkey minibuffer-local-map "\M-v"
  13086. (lambda () (interactive)
  13087. (org-eval-in-calendar
  13088. '(calendar-scroll-right-three-months 1))))
  13089. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13090. (unwind-protect
  13091. (progn
  13092. (use-local-map map)
  13093. (add-hook 'post-command-hook 'org-read-date-display)
  13094. (setq org-ans0 (read-string prompt default-input
  13095. 'org-read-date-history nil))
  13096. ;; org-ans0: from prompt
  13097. ;; org-ans1: from mouse click
  13098. ;; org-ans2: from calendar motion
  13099. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13100. (remove-hook 'post-command-hook 'org-read-date-display)
  13101. (use-local-map old-map)
  13102. (when org-read-date-overlay
  13103. (delete-overlay org-read-date-overlay)
  13104. (setq org-read-date-overlay nil)))))))
  13105. (t ; Naked prompt only
  13106. (unwind-protect
  13107. (setq ans (read-string prompt default-input
  13108. 'org-read-date-history timestr))
  13109. (when org-read-date-overlay
  13110. (delete-overlay org-read-date-overlay)
  13111. (setq org-read-date-overlay nil)))))
  13112. (setq final (org-read-date-analyze ans def defdecode))
  13113. ;; One round trip to get rid of 34th of August and stuff like that....
  13114. (setq final (decode-time (apply 'encode-time final)))
  13115. (setq org-read-date-final-answer ans)
  13116. (if to-time
  13117. (apply 'encode-time final)
  13118. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13119. (format "%04d-%02d-%02d %02d:%02d"
  13120. (nth 5 final) (nth 4 final) (nth 3 final)
  13121. (nth 2 final) (nth 1 final))
  13122. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13123. (defvar def)
  13124. (defvar defdecode)
  13125. (defvar with-time)
  13126. (defvar org-read-date-analyze-futurep nil)
  13127. (defun org-read-date-display ()
  13128. "Display the current date prompt interpretation in the minibuffer."
  13129. (when org-read-date-display-live
  13130. (when org-read-date-overlay
  13131. (delete-overlay org-read-date-overlay))
  13132. (let ((p (point)))
  13133. (end-of-line 1)
  13134. (while (not (equal (buffer-substring
  13135. (max (point-min) (- (point) 4)) (point))
  13136. " "))
  13137. (insert " "))
  13138. (goto-char p))
  13139. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13140. " " (or org-ans1 org-ans2)))
  13141. (org-end-time-was-given nil)
  13142. (f (org-read-date-analyze ans def defdecode))
  13143. (fmts (if org-dcst
  13144. org-time-stamp-custom-formats
  13145. org-time-stamp-formats))
  13146. (fmt (if (or with-time
  13147. (and (boundp 'org-time-was-given) org-time-was-given))
  13148. (cdr fmts)
  13149. (car fmts)))
  13150. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13151. (when (and org-end-time-was-given
  13152. (string-match org-plain-time-of-day-regexp txt))
  13153. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13154. org-end-time-was-given
  13155. (substring txt (match-end 0)))))
  13156. (when org-read-date-analyze-futurep
  13157. (setq txt (concat txt " (=>F)")))
  13158. (setq org-read-date-overlay
  13159. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13160. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13161. (defun org-read-date-analyze (ans def defdecode)
  13162. "Analyze the combined answer of the date prompt."
  13163. ;; FIXME: cleanup and comment
  13164. (let ((nowdecode (decode-time (current-time)))
  13165. delta deltan deltaw deltadef year month day
  13166. hour minute second wday pm h2 m2 tl wday1
  13167. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13168. (setq org-read-date-analyze-futurep nil)
  13169. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13170. (setq ans "+0"))
  13171. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13172. (setq ans (replace-match "" t t ans)
  13173. deltan (car delta)
  13174. deltaw (nth 1 delta)
  13175. deltadef (nth 2 delta)))
  13176. ;; Check if there is an iso week date in there
  13177. ;; If yes, store the info and postpone interpreting it until the rest
  13178. ;; of the parsing is done
  13179. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13180. (setq iso-year (if (match-end 1)
  13181. (org-small-year-to-year
  13182. (string-to-number (match-string 1 ans))))
  13183. iso-weekday (if (match-end 3)
  13184. (string-to-number (match-string 3 ans)))
  13185. iso-week (string-to-number (match-string 2 ans)))
  13186. (setq ans (replace-match "" t t ans)))
  13187. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13188. (when (string-match
  13189. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13190. (setq year (if (match-end 2)
  13191. (string-to-number (match-string 2 ans))
  13192. (progn (setq kill-year t)
  13193. (string-to-number (format-time-string "%Y"))))
  13194. month (string-to-number (match-string 3 ans))
  13195. day (string-to-number (match-string 4 ans)))
  13196. (if (< year 100) (setq year (+ 2000 year)))
  13197. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13198. t nil ans)))
  13199. ;; Help matching american dates, like 5/30 or 5/30/7
  13200. (when (string-match
  13201. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13202. (setq year (if (match-end 4)
  13203. (string-to-number (match-string 4 ans))
  13204. (progn (setq kill-year t)
  13205. (string-to-number (format-time-string "%Y"))))
  13206. month (string-to-number (match-string 1 ans))
  13207. day (string-to-number (match-string 2 ans)))
  13208. (if (< year 100) (setq year (+ 2000 year)))
  13209. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13210. t nil ans)))
  13211. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13212. ;; If there is a time with am/pm, and *no* time without it, we convert
  13213. ;; so that matching will be successful.
  13214. (loop for i from 1 to 2 do ; twice, for end time as well
  13215. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13216. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13217. (setq hour (string-to-number (match-string 1 ans))
  13218. minute (if (match-end 3)
  13219. (string-to-number (match-string 3 ans))
  13220. 0)
  13221. pm (equal ?p
  13222. (string-to-char (downcase (match-string 4 ans)))))
  13223. (if (and (= hour 12) (not pm))
  13224. (setq hour 0)
  13225. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13226. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13227. t t ans))))
  13228. ;; Check if a time range is given as a duration
  13229. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13230. (setq hour (string-to-number (match-string 1 ans))
  13231. h2 (+ hour (string-to-number (match-string 3 ans)))
  13232. minute (string-to-number (match-string 2 ans))
  13233. m2 (+ minute (if (match-end 5) (string-to-number
  13234. (match-string 5 ans))0)))
  13235. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13236. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13237. t t ans)))
  13238. ;; Check if there is a time range
  13239. (when (boundp 'org-end-time-was-given)
  13240. (setq org-time-was-given nil)
  13241. (when (and (string-match org-plain-time-of-day-regexp ans)
  13242. (match-end 8))
  13243. (setq org-end-time-was-given (match-string 8 ans))
  13244. (setq ans (concat (substring ans 0 (match-beginning 7))
  13245. (substring ans (match-end 7))))))
  13246. (setq tl (parse-time-string ans)
  13247. day (or (nth 3 tl) (nth 3 defdecode))
  13248. month (or (nth 4 tl)
  13249. (if (and org-read-date-prefer-future
  13250. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13251. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13252. (nth 4 defdecode)))
  13253. year (or (and (not kill-year) (nth 5 tl))
  13254. (if (and org-read-date-prefer-future
  13255. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13256. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13257. (nth 5 defdecode)))
  13258. hour (or (nth 2 tl) (nth 2 defdecode))
  13259. minute (or (nth 1 tl) (nth 1 defdecode))
  13260. second (or (nth 0 tl) 0)
  13261. wday (nth 6 tl))
  13262. (when (and (eq org-read-date-prefer-future 'time)
  13263. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13264. (equal day (nth 3 nowdecode))
  13265. (equal month (nth 4 nowdecode))
  13266. (equal year (nth 5 nowdecode))
  13267. (nth 2 tl)
  13268. (or (< (nth 2 tl) (nth 2 nowdecode))
  13269. (and (= (nth 2 tl) (nth 2 nowdecode))
  13270. (nth 1 tl)
  13271. (< (nth 1 tl) (nth 1 nowdecode)))))
  13272. (setq day (1+ day)
  13273. futurep t))
  13274. ;; Special date definitions below
  13275. (cond
  13276. (iso-week
  13277. ;; There was an iso week
  13278. (require 'cal-iso)
  13279. (setq futurep nil)
  13280. (setq year (or iso-year year)
  13281. day (or iso-weekday wday 1)
  13282. wday nil ; to make sure that the trigger below does not match
  13283. iso-date (calendar-gregorian-from-absolute
  13284. (calendar-absolute-from-iso
  13285. (list iso-week day year))))
  13286. ; FIXME: Should we also push ISO weeks into the future?
  13287. ; (when (and org-read-date-prefer-future
  13288. ; (not iso-year)
  13289. ; (< (calendar-absolute-from-gregorian iso-date)
  13290. ; (time-to-days (current-time))))
  13291. ; (setq year (1+ year)
  13292. ; iso-date (calendar-gregorian-from-absolute
  13293. ; (calendar-absolute-from-iso
  13294. ; (list iso-week day year)))))
  13295. (setq month (car iso-date)
  13296. year (nth 2 iso-date)
  13297. day (nth 1 iso-date)))
  13298. (deltan
  13299. (setq futurep nil)
  13300. (unless deltadef
  13301. (let ((now (decode-time (current-time))))
  13302. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13303. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13304. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13305. ((equal deltaw "m") (setq month (+ month deltan)))
  13306. ((equal deltaw "y") (setq year (+ year deltan)))))
  13307. ((and wday (not (nth 3 tl)))
  13308. (setq futurep nil)
  13309. ;; Weekday was given, but no day, so pick that day in the week
  13310. ;; on or after the derived date.
  13311. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13312. (unless (equal wday wday1)
  13313. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13314. (if (and (boundp 'org-time-was-given)
  13315. (nth 2 tl))
  13316. (setq org-time-was-given t))
  13317. (if (< year 100) (setq year (+ 2000 year)))
  13318. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  13319. (setq org-read-date-analyze-futurep futurep)
  13320. (list second minute hour day month year)))
  13321. (defvar parse-time-weekdays)
  13322. (defun org-read-date-get-relative (s today default)
  13323. "Check string S for special relative date string.
  13324. TODAY and DEFAULT are internal times, for today and for a default.
  13325. Return shift list (N what def-flag)
  13326. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13327. N is the number of WHATs to shift.
  13328. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13329. the DEFAULT date rather than TODAY."
  13330. (when (and
  13331. (string-match
  13332. (concat
  13333. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13334. "\\([0-9]+\\)?"
  13335. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13336. "\\([ \t]\\|$\\)") s)
  13337. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13338. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13339. (string-to-char (substring (match-string 1 s) -1))
  13340. ?+))
  13341. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13342. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13343. (what (if (match-end 3) (match-string 3 s) "d"))
  13344. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13345. (date (if rel default today))
  13346. (wday (nth 6 (decode-time date)))
  13347. delta)
  13348. (if wday1
  13349. (progn
  13350. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13351. (if (= dir ?-) (setq delta (- delta 7)))
  13352. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13353. (list delta "d" rel))
  13354. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13355. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13356. "Turn a user-specified date into the internal representation.
  13357. The internal representation needed by the calendar is (month day year).
  13358. This is a wrapper to handle the brain-dead convention in calendar that
  13359. user function argument order change dependent on argument order."
  13360. (if (boundp 'calendar-date-style)
  13361. (cond
  13362. ((eq calendar-date-style 'american)
  13363. (list arg1 arg2 arg3))
  13364. ((eq calendar-date-style 'european)
  13365. (list arg2 arg1 arg3))
  13366. ((eq calendar-date-style 'iso)
  13367. (list arg2 arg3 arg1)))
  13368. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13369. (if (org-bound-and-true-p european-calendar-style)
  13370. (list arg2 arg1 arg3)
  13371. (list arg1 arg2 arg3)))))
  13372. (defun org-eval-in-calendar (form &optional keepdate)
  13373. "Eval FORM in the calendar window and return to current window.
  13374. Also, store the cursor date in variable org-ans2."
  13375. (let ((sf (selected-frame))
  13376. (sw (selected-window)))
  13377. (select-window (get-buffer-window "*Calendar*" t))
  13378. (eval form)
  13379. (when (and (not keepdate) (calendar-cursor-to-date))
  13380. (let* ((date (calendar-cursor-to-date))
  13381. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13382. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13383. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13384. (select-window sw)
  13385. (org-select-frame-set-input-focus sf)))
  13386. (defun org-calendar-select ()
  13387. "Return to `org-read-date' with the date currently selected.
  13388. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13389. (interactive)
  13390. (when (calendar-cursor-to-date)
  13391. (let* ((date (calendar-cursor-to-date))
  13392. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13393. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13394. (if (active-minibuffer-window) (exit-minibuffer))))
  13395. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13396. "Insert a date stamp for the date given by the internal TIME.
  13397. WITH-HM means use the stamp format that includes the time of the day.
  13398. INACTIVE means use square brackets instead of angular ones, so that the
  13399. stamp will not contribute to the agenda.
  13400. PRE and POST are optional strings to be inserted before and after the
  13401. stamp.
  13402. The command returns the inserted time stamp."
  13403. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13404. stamp)
  13405. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13406. (insert-before-markers (or pre ""))
  13407. (when (listp extra)
  13408. (setq extra (car extra))
  13409. (if (and (stringp extra)
  13410. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13411. (setq extra (format "-%02d:%02d"
  13412. (string-to-number (match-string 1 extra))
  13413. (string-to-number (match-string 2 extra))))
  13414. (setq extra nil)))
  13415. (when extra
  13416. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13417. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13418. (insert-before-markers (or post ""))
  13419. (setq org-last-inserted-timestamp stamp)))
  13420. (defun org-toggle-time-stamp-overlays ()
  13421. "Toggle the use of custom time stamp formats."
  13422. (interactive)
  13423. (setq org-display-custom-times (not org-display-custom-times))
  13424. (unless org-display-custom-times
  13425. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13426. (while (setq p (next-single-property-change p 'display))
  13427. (if (and (get-text-property p 'display)
  13428. (eq (get-text-property p 'face) 'org-date))
  13429. (remove-text-properties
  13430. p (setq p (next-single-property-change p 'display))
  13431. '(display t))))
  13432. (set-buffer-modified-p bmp)))
  13433. (if (featurep 'xemacs)
  13434. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13435. (org-restart-font-lock)
  13436. (setq org-table-may-need-update t)
  13437. (if org-display-custom-times
  13438. (message "Time stamps are overlayed with custom format")
  13439. (message "Time stamp overlays removed")))
  13440. (defun org-display-custom-time (beg end)
  13441. "Overlay modified time stamp format over timestamp between BEG and END."
  13442. (let* ((ts (buffer-substring beg end))
  13443. t1 w1 with-hm tf time str w2 (off 0))
  13444. (save-match-data
  13445. (setq t1 (org-parse-time-string ts t))
  13446. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13447. (setq off (- (match-end 0) (match-beginning 0)))))
  13448. (setq end (- end off))
  13449. (setq w1 (- end beg)
  13450. with-hm (and (nth 1 t1) (nth 2 t1))
  13451. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13452. time (org-fix-decoded-time t1)
  13453. str (org-add-props
  13454. (format-time-string
  13455. (substring tf 1 -1) (apply 'encode-time time))
  13456. nil 'mouse-face 'highlight)
  13457. w2 (length str))
  13458. (if (not (= w2 w1))
  13459. (add-text-properties (1+ beg) (+ 2 beg)
  13460. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13461. (if (featurep 'xemacs)
  13462. (progn
  13463. (put-text-property beg end 'invisible t)
  13464. (put-text-property beg end 'end-glyph (make-glyph str)))
  13465. (put-text-property beg end 'display str))))
  13466. (defun org-translate-time (string)
  13467. "Translate all timestamps in STRING to custom format.
  13468. But do this only if the variable `org-display-custom-times' is set."
  13469. (when org-display-custom-times
  13470. (save-match-data
  13471. (let* ((start 0)
  13472. (re org-ts-regexp-both)
  13473. t1 with-hm inactive tf time str beg end)
  13474. (while (setq start (string-match re string start))
  13475. (setq beg (match-beginning 0)
  13476. end (match-end 0)
  13477. t1 (save-match-data
  13478. (org-parse-time-string (substring string beg end) t))
  13479. with-hm (and (nth 1 t1) (nth 2 t1))
  13480. inactive (equal (substring string beg (1+ beg)) "[")
  13481. tf (funcall (if with-hm 'cdr 'car)
  13482. org-time-stamp-custom-formats)
  13483. time (org-fix-decoded-time t1)
  13484. str (format-time-string
  13485. (concat
  13486. (if inactive "[" "<") (substring tf 1 -1)
  13487. (if inactive "]" ">"))
  13488. (apply 'encode-time time))
  13489. string (replace-match str t t string)
  13490. start (+ start (length str)))))))
  13491. string)
  13492. (defun org-fix-decoded-time (time)
  13493. "Set 0 instead of nil for the first 6 elements of time.
  13494. Don't touch the rest."
  13495. (let ((n 0))
  13496. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13497. (defun org-days-to-time (timestamp-string)
  13498. "Difference between TIMESTAMP-STRING and now in days."
  13499. (- (time-to-days (org-time-string-to-time timestamp-string))
  13500. (time-to-days (current-time))))
  13501. (defun org-deadline-close (timestamp-string &optional ndays)
  13502. "Is the time in TIMESTAMP-STRING close to the current date?"
  13503. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13504. (and (< (org-days-to-time timestamp-string) ndays)
  13505. (not (org-entry-is-done-p))))
  13506. (defun org-get-wdays (ts)
  13507. "Get the deadline lead time appropriate for timestring TS."
  13508. (cond
  13509. ((<= org-deadline-warning-days 0)
  13510. ;; 0 or negative, enforce this value no matter what
  13511. (- org-deadline-warning-days))
  13512. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13513. ;; lead time is specified.
  13514. (floor (* (string-to-number (match-string 1 ts))
  13515. (cdr (assoc (match-string 2 ts)
  13516. '(("d" . 1) ("w" . 7)
  13517. ("m" . 30.4) ("y" . 365.25)))))))
  13518. ;; go for the default.
  13519. (t org-deadline-warning-days)))
  13520. (defun org-calendar-select-mouse (ev)
  13521. "Return to `org-read-date' with the date currently selected.
  13522. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13523. (interactive "e")
  13524. (mouse-set-point ev)
  13525. (when (calendar-cursor-to-date)
  13526. (let* ((date (calendar-cursor-to-date))
  13527. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13528. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13529. (if (active-minibuffer-window) (exit-minibuffer))))
  13530. (defun org-check-deadlines (ndays)
  13531. "Check if there are any deadlines due or past due.
  13532. A deadline is considered due if it happens within `org-deadline-warning-days'
  13533. days from today's date. If the deadline appears in an entry marked DONE,
  13534. it is not shown. The prefix arg NDAYS can be used to test that many
  13535. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13536. (interactive "P")
  13537. (let* ((org-warn-days
  13538. (cond
  13539. ((equal ndays '(4)) 100000)
  13540. (ndays (prefix-numeric-value ndays))
  13541. (t (abs org-deadline-warning-days))))
  13542. (case-fold-search nil)
  13543. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13544. (callback
  13545. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13546. (message "%d deadlines past-due or due within %d days"
  13547. (org-occur regexp nil callback)
  13548. org-warn-days)))
  13549. (defun org-check-before-date (date)
  13550. "Check if there are deadlines or scheduled entries before DATE."
  13551. (interactive (list (org-read-date)))
  13552. (let ((case-fold-search nil)
  13553. (regexp (concat "\\<\\(" org-deadline-string
  13554. "\\|" org-scheduled-string
  13555. "\\) *<\\([^>]+\\)>"))
  13556. (callback
  13557. (lambda () (time-less-p
  13558. (org-time-string-to-time (match-string 2))
  13559. (org-time-string-to-time date)))))
  13560. (message "%d entries before %s"
  13561. (org-occur regexp nil callback) date)))
  13562. (defun org-check-after-date (date)
  13563. "Check if there are deadlines or scheduled entries after DATE."
  13564. (interactive (list (org-read-date)))
  13565. (let ((case-fold-search nil)
  13566. (regexp (concat "\\<\\(" org-deadline-string
  13567. "\\|" org-scheduled-string
  13568. "\\) *<\\([^>]+\\)>"))
  13569. (callback
  13570. (lambda () (not
  13571. (time-less-p
  13572. (org-time-string-to-time (match-string 2))
  13573. (org-time-string-to-time date))))))
  13574. (message "%d entries after %s"
  13575. (org-occur regexp nil callback) date)))
  13576. (defun org-evaluate-time-range (&optional to-buffer)
  13577. "Evaluate a time range by computing the difference between start and end.
  13578. Normally the result is just printed in the echo area, but with prefix arg
  13579. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13580. If the time range is actually in a table, the result is inserted into the
  13581. next column.
  13582. For time difference computation, a year is assumed to be exactly 365
  13583. days in order to avoid rounding problems."
  13584. (interactive "P")
  13585. (or
  13586. (org-clock-update-time-maybe)
  13587. (save-excursion
  13588. (unless (org-at-date-range-p t)
  13589. (goto-char (point-at-bol))
  13590. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13591. (if (not (org-at-date-range-p t))
  13592. (error "Not at a time-stamp range, and none found in current line")))
  13593. (let* ((ts1 (match-string 1))
  13594. (ts2 (match-string 2))
  13595. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13596. (match-end (match-end 0))
  13597. (time1 (org-time-string-to-time ts1))
  13598. (time2 (org-time-string-to-time ts2))
  13599. (t1 (org-float-time time1))
  13600. (t2 (org-float-time time2))
  13601. (diff (abs (- t2 t1)))
  13602. (negative (< (- t2 t1) 0))
  13603. ;; (ys (floor (* 365 24 60 60)))
  13604. (ds (* 24 60 60))
  13605. (hs (* 60 60))
  13606. (fy "%dy %dd %02d:%02d")
  13607. (fy1 "%dy %dd")
  13608. (fd "%dd %02d:%02d")
  13609. (fd1 "%dd")
  13610. (fh "%02d:%02d")
  13611. y d h m align)
  13612. (if havetime
  13613. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13614. y 0
  13615. d (floor (/ diff ds)) diff (mod diff ds)
  13616. h (floor (/ diff hs)) diff (mod diff hs)
  13617. m (floor (/ diff 60)))
  13618. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13619. y 0
  13620. d (floor (+ (/ diff ds) 0.5))
  13621. h 0 m 0))
  13622. (if (not to-buffer)
  13623. (message "%s" (org-make-tdiff-string y d h m))
  13624. (if (org-at-table-p)
  13625. (progn
  13626. (goto-char match-end)
  13627. (setq align t)
  13628. (and (looking-at " *|") (goto-char (match-end 0))))
  13629. (goto-char match-end))
  13630. (if (looking-at
  13631. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13632. (replace-match ""))
  13633. (if negative (insert " -"))
  13634. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13635. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13636. (insert " " (format fh h m))))
  13637. (if align (org-table-align))
  13638. (message "Time difference inserted")))))
  13639. (defun org-make-tdiff-string (y d h m)
  13640. (let ((fmt "")
  13641. (l nil))
  13642. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13643. l (push y l)))
  13644. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13645. l (push d l)))
  13646. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13647. l (push h l)))
  13648. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13649. l (push m l)))
  13650. (apply 'format fmt (nreverse l))))
  13651. (defun org-time-string-to-time (s)
  13652. (apply 'encode-time (org-parse-time-string s)))
  13653. (defun org-time-string-to-seconds (s)
  13654. (org-float-time (org-time-string-to-time s)))
  13655. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13656. "Convert a time stamp to an absolute day number.
  13657. If there is a specifier for a cyclic time stamp, get the closest date to
  13658. DAYNR.
  13659. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13660. the variable date is bound by the calendar when this is called."
  13661. (cond
  13662. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13663. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13664. daynr
  13665. (+ daynr 1000)))
  13666. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13667. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13668. (time-to-days (current-time))) (match-string 0 s)
  13669. prefer show-all))
  13670. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13671. (defun org-days-to-iso-week (days)
  13672. "Return the iso week number."
  13673. (require 'cal-iso)
  13674. (car (calendar-iso-from-absolute days)))
  13675. (defun org-small-year-to-year (year)
  13676. "Convert 2-digit years into 4-digit years.
  13677. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13678. The year 2000 cannot be abbreviated. Any year larger than 99
  13679. is returned unchanged."
  13680. (if (< year 38)
  13681. (setq year (+ 2000 year))
  13682. (if (< year 100)
  13683. (setq year (+ 1900 year))))
  13684. year)
  13685. (defun org-time-from-absolute (d)
  13686. "Return the time corresponding to date D.
  13687. D may be an absolute day number, or a calendar-type list (month day year)."
  13688. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13689. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13690. (defun org-calendar-holiday ()
  13691. "List of holidays, for Diary display in Org-mode."
  13692. (require 'holidays)
  13693. (let ((hl (funcall
  13694. (if (fboundp 'calendar-check-holidays)
  13695. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13696. (if hl (mapconcat 'identity hl "; "))))
  13697. (defun org-diary-sexp-entry (sexp entry date)
  13698. "Process a SEXP diary ENTRY for DATE."
  13699. (require 'diary-lib)
  13700. (let ((result (if calendar-debug-sexp
  13701. (let ((stack-trace-on-error t))
  13702. (eval (car (read-from-string sexp))))
  13703. (condition-case nil
  13704. (eval (car (read-from-string sexp)))
  13705. (error
  13706. (beep)
  13707. (message "Bad sexp at line %d in %s: %s"
  13708. (org-current-line)
  13709. (buffer-file-name) sexp)
  13710. (sleep-for 2))))))
  13711. (cond ((stringp result) (split-string result "; "))
  13712. ((and (consp result)
  13713. (not (consp (cdr result)))
  13714. (stringp (cdr result))) (cdr result))
  13715. ((and (consp result)
  13716. (stringp (car result))) result)
  13717. (result entry)
  13718. (t nil))))
  13719. (defun org-diary-to-ical-string (frombuf)
  13720. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13721. This uses the icalendar.el library."
  13722. (let* ((tmpdir (if (featurep 'xemacs)
  13723. (temp-directory)
  13724. temporary-file-directory))
  13725. (tmpfile (make-temp-name
  13726. (expand-file-name "orgics" tmpdir)))
  13727. buf rtn b e)
  13728. (with-current-buffer frombuf
  13729. (icalendar-export-region (point-min) (point-max) tmpfile)
  13730. (setq buf (find-buffer-visiting tmpfile))
  13731. (set-buffer buf)
  13732. (goto-char (point-min))
  13733. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13734. (setq b (match-beginning 0)))
  13735. (goto-char (point-max))
  13736. (if (re-search-backward "^END:VEVENT" nil t)
  13737. (setq e (match-end 0)))
  13738. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13739. (kill-buffer buf)
  13740. (delete-file tmpfile)
  13741. rtn))
  13742. (defun org-closest-date (start current change prefer show-all)
  13743. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13744. When PREFER is `past' return a date that is either CURRENT or past.
  13745. When PREFER is `future', return a date that is either CURRENT or future.
  13746. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13747. ;; Make the proper lists from the dates
  13748. (catch 'exit
  13749. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13750. dn dw sday cday n1 n2 n0
  13751. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13752. (setq start (org-date-to-gregorian start)
  13753. current (org-date-to-gregorian
  13754. (if show-all
  13755. current
  13756. (time-to-days (current-time))))
  13757. sday (calendar-absolute-from-gregorian start)
  13758. cday (calendar-absolute-from-gregorian current))
  13759. (if (<= cday sday) (throw 'exit sday))
  13760. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13761. (setq dn (string-to-number (match-string 1 change))
  13762. dw (cdr (assoc (match-string 2 change) a1)))
  13763. (error "Invalid change specifier: %s" change))
  13764. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13765. (cond
  13766. ((eq dw 'day)
  13767. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13768. n2 (+ n1 dn)))
  13769. ((eq dw 'year)
  13770. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13771. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13772. (setq date1 (list m d y1)
  13773. n1 (calendar-absolute-from-gregorian date1)
  13774. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13775. n2 (calendar-absolute-from-gregorian date2)))
  13776. ((eq dw 'month)
  13777. ;; approx number of month between the two dates
  13778. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13779. ;; How often does dn fit in there?
  13780. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13781. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13782. m (+ m nm)
  13783. ny (floor (/ m 12))
  13784. y (+ y ny)
  13785. m (- m (* ny 12)))
  13786. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13787. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13788. (setq m2 (+ m dn) y2 y)
  13789. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13790. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13791. (while (<= n2 cday)
  13792. (setq n1 n2 m m2 y y2)
  13793. (setq m2 (+ m dn) y2 y)
  13794. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13795. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13796. ;; Make sure n1 is the earlier date
  13797. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13798. (if show-all
  13799. (cond
  13800. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13801. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13802. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13803. (cond
  13804. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13805. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13806. (t (if (= cday n1) n1 n2)))))))
  13807. (defun org-date-to-gregorian (date)
  13808. "Turn any specification of DATE into a Gregorian date for the calendar."
  13809. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13810. ((and (listp date) (= (length date) 3)) date)
  13811. ((stringp date)
  13812. (setq date (org-parse-time-string date))
  13813. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13814. ((listp date)
  13815. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13816. (defun org-parse-time-string (s &optional nodefault)
  13817. "Parse the standard Org-mode time string.
  13818. This should be a lot faster than the normal `parse-time-string'.
  13819. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13820. hour and minute fields will be nil if not given."
  13821. (if (string-match org-ts-regexp0 s)
  13822. (list 0
  13823. (if (or (match-beginning 8) (not nodefault))
  13824. (string-to-number (or (match-string 8 s) "0")))
  13825. (if (or (match-beginning 7) (not nodefault))
  13826. (string-to-number (or (match-string 7 s) "0")))
  13827. (string-to-number (match-string 4 s))
  13828. (string-to-number (match-string 3 s))
  13829. (string-to-number (match-string 2 s))
  13830. nil nil nil)
  13831. (error "Not a standard Org-mode time string: %s" s)))
  13832. (defun org-timestamp-up (&optional arg)
  13833. "Increase the date item at the cursor by one.
  13834. If the cursor is on the year, change the year. If it is on the month or
  13835. the day, change that.
  13836. With prefix ARG, change by that many units."
  13837. (interactive "p")
  13838. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  13839. (defun org-timestamp-down (&optional arg)
  13840. "Decrease the date item at the cursor by one.
  13841. If the cursor is on the year, change the year. If it is on the month or
  13842. the day, change that.
  13843. With prefix ARG, change by that many units."
  13844. (interactive "p")
  13845. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  13846. (defun org-timestamp-up-day (&optional arg)
  13847. "Increase the date in the time stamp by one day.
  13848. With prefix ARG, change that many days."
  13849. (interactive "p")
  13850. (if (and (not (org-at-timestamp-p t))
  13851. (org-on-heading-p))
  13852. (org-todo 'up)
  13853. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  13854. (defun org-timestamp-down-day (&optional arg)
  13855. "Decrease the date in the time stamp by one day.
  13856. With prefix ARG, change that many days."
  13857. (interactive "p")
  13858. (if (and (not (org-at-timestamp-p t))
  13859. (org-on-heading-p))
  13860. (org-todo 'down)
  13861. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  13862. (defun org-at-timestamp-p (&optional inactive-ok)
  13863. "Determine if the cursor is in or at a timestamp."
  13864. (interactive)
  13865. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13866. (pos (point))
  13867. (ans (or (looking-at tsr)
  13868. (save-excursion
  13869. (skip-chars-backward "^[<\n\r\t")
  13870. (if (> (point) (point-min)) (backward-char 1))
  13871. (and (looking-at tsr)
  13872. (> (- (match-end 0) pos) -1))))))
  13873. (and ans
  13874. (boundp 'org-ts-what)
  13875. (setq org-ts-what
  13876. (cond
  13877. ((= pos (match-beginning 0)) 'bracket)
  13878. ((= pos (1- (match-end 0))) 'bracket)
  13879. ((org-pos-in-match-range pos 2) 'year)
  13880. ((org-pos-in-match-range pos 3) 'month)
  13881. ((org-pos-in-match-range pos 7) 'hour)
  13882. ((org-pos-in-match-range pos 8) 'minute)
  13883. ((or (org-pos-in-match-range pos 4)
  13884. (org-pos-in-match-range pos 5)) 'day)
  13885. ((and (> pos (or (match-end 8) (match-end 5)))
  13886. (< pos (match-end 0)))
  13887. (- pos (or (match-end 8) (match-end 5))))
  13888. (t 'day))))
  13889. ans))
  13890. (defun org-toggle-timestamp-type ()
  13891. "Toggle the type (<active> or [inactive]) of a time stamp."
  13892. (interactive)
  13893. (when (org-at-timestamp-p t)
  13894. (let ((beg (match-beginning 0)) (end (match-end 0))
  13895. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13896. (save-excursion
  13897. (goto-char beg)
  13898. (while (re-search-forward "[][<>]" end t)
  13899. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13900. t t)))
  13901. (message "Timestamp is now %sactive"
  13902. (if (equal (char-after beg) ?<) "" "in")))))
  13903. (defun org-timestamp-change (n &optional what updown)
  13904. "Change the date in the time stamp at point.
  13905. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13906. `year', `minute', `second'. If WHAT is not given, the cursor position
  13907. in the timestamp determines what will be changed."
  13908. (let ((pos (point))
  13909. with-hm inactive
  13910. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13911. org-ts-what
  13912. extra rem
  13913. ts time time0)
  13914. (if (not (org-at-timestamp-p t))
  13915. (error "Not at a timestamp"))
  13916. (if (and (not what) (eq org-ts-what 'bracket))
  13917. (org-toggle-timestamp-type)
  13918. (if (and (not what) (not (eq org-ts-what 'day))
  13919. org-display-custom-times
  13920. (get-text-property (point) 'display)
  13921. (not (get-text-property (1- (point)) 'display)))
  13922. (setq org-ts-what 'day))
  13923. (setq org-ts-what (or what org-ts-what)
  13924. inactive (= (char-after (match-beginning 0)) ?\[)
  13925. ts (match-string 0))
  13926. (replace-match "")
  13927. (if (string-match
  13928. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13929. ts)
  13930. (setq extra (match-string 1 ts)))
  13931. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13932. (setq with-hm t))
  13933. (setq time0 (org-parse-time-string ts))
  13934. (when (and updown
  13935. (eq org-ts-what 'minute)
  13936. (not current-prefix-arg))
  13937. ;; This looks like s-up and s-down. Change by one rounding step.
  13938. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13939. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13940. (setcar (cdr time0) (+ (nth 1 time0)
  13941. (if (> n 0) (- rem) (- dm rem))))))
  13942. (setq time
  13943. (encode-time (or (car time0) 0)
  13944. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13945. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13946. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13947. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13948. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13949. (nthcdr 6 time0)))
  13950. (when (and (member org-ts-what '(hour minute))
  13951. extra
  13952. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13953. (setq extra (org-modify-ts-extra
  13954. extra
  13955. (if (eq org-ts-what 'hour) 2 5)
  13956. n dm)))
  13957. (when (integerp org-ts-what)
  13958. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13959. (if (eq what 'calendar)
  13960. (let ((cal-date (org-get-date-from-calendar)))
  13961. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13962. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13963. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13964. (setcar time0 (or (car time0) 0))
  13965. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13966. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13967. (setq time (apply 'encode-time time0))))
  13968. (setq org-last-changed-timestamp
  13969. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13970. (org-clock-update-time-maybe)
  13971. (goto-char pos)
  13972. ;; Try to recenter the calendar window, if any
  13973. (if (and org-calendar-follow-timestamp-change
  13974. (get-buffer-window "*Calendar*" t)
  13975. (memq org-ts-what '(day month year)))
  13976. (org-recenter-calendar (time-to-days time))))))
  13977. (defun org-modify-ts-extra (s pos n dm)
  13978. "Change the different parts of the lead-time and repeat fields in timestamp."
  13979. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13980. ng h m new rem)
  13981. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13982. (cond
  13983. ((or (org-pos-in-match-range pos 2)
  13984. (org-pos-in-match-range pos 3))
  13985. (setq m (string-to-number (match-string 3 s))
  13986. h (string-to-number (match-string 2 s)))
  13987. (if (org-pos-in-match-range pos 2)
  13988. (setq h (+ h n))
  13989. (setq n (* dm (org-no-warnings (signum n))))
  13990. (when (not (= 0 (setq rem (% m dm))))
  13991. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13992. (setq m (+ m n)))
  13993. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13994. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13995. (setq h (min 24 (max 0 h)))
  13996. (setq ng 1 new (format "-%02d:%02d" h m)))
  13997. ((org-pos-in-match-range pos 6)
  13998. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13999. ((org-pos-in-match-range pos 5)
  14000. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14001. ((org-pos-in-match-range pos 9)
  14002. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14003. ((org-pos-in-match-range pos 8)
  14004. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14005. (when ng
  14006. (setq s (concat
  14007. (substring s 0 (match-beginning ng))
  14008. new
  14009. (substring s (match-end ng))))))
  14010. s))
  14011. (defun org-recenter-calendar (date)
  14012. "If the calendar is visible, recenter it to DATE."
  14013. (let* ((win (selected-window))
  14014. (cwin (get-buffer-window "*Calendar*" t))
  14015. (calendar-move-hook nil))
  14016. (when cwin
  14017. (select-window cwin)
  14018. (calendar-goto-date (if (listp date) date
  14019. (calendar-gregorian-from-absolute date)))
  14020. (select-window win))))
  14021. (defun org-goto-calendar (&optional arg)
  14022. "Go to the Emacs calendar at the current date.
  14023. If there is a time stamp in the current line, go to that date.
  14024. A prefix ARG can be used to force the current date."
  14025. (interactive "P")
  14026. (let ((tsr org-ts-regexp) diff
  14027. (calendar-move-hook nil)
  14028. (calendar-view-holidays-initially-flag nil)
  14029. (calendar-view-diary-initially-flag nil))
  14030. (if (or (org-at-timestamp-p)
  14031. (save-excursion
  14032. (beginning-of-line 1)
  14033. (looking-at (concat ".*" tsr))))
  14034. (let ((d1 (time-to-days (current-time)))
  14035. (d2 (time-to-days
  14036. (org-time-string-to-time (match-string 1)))))
  14037. (setq diff (- d2 d1))))
  14038. (calendar)
  14039. (calendar-goto-today)
  14040. (if (and diff (not arg)) (calendar-forward-day diff))))
  14041. (defun org-get-date-from-calendar ()
  14042. "Return a list (month day year) of date at point in calendar."
  14043. (with-current-buffer "*Calendar*"
  14044. (save-match-data
  14045. (calendar-cursor-to-date))))
  14046. (defun org-date-from-calendar ()
  14047. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14048. If there is already a time stamp at the cursor position, update it."
  14049. (interactive)
  14050. (if (org-at-timestamp-p t)
  14051. (org-timestamp-change 0 'calendar)
  14052. (let ((cal-date (org-get-date-from-calendar)))
  14053. (org-insert-time-stamp
  14054. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14055. (defun org-minutes-to-hh:mm-string (m)
  14056. "Compute H:MM from a number of minutes."
  14057. (let ((h (/ m 60)))
  14058. (setq m (- m (* 60 h)))
  14059. (format org-time-clocksum-format h m)))
  14060. (defun org-hh:mm-string-to-minutes (s)
  14061. "Convert a string H:MM to a number of minutes.
  14062. If the string is just a number, interpret it as minutes.
  14063. In fact, the first hh:mm or number in the string will be taken,
  14064. there can be extra stuff in the string.
  14065. If no number is found, the return value is 0."
  14066. (cond
  14067. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14068. (+ (* (string-to-number (match-string 1 s)) 60)
  14069. (string-to-number (match-string 2 s))))
  14070. ((string-match "\\([0-9]+\\)" s)
  14071. (string-to-number (match-string 1 s)))
  14072. (t 0)))
  14073. ;;;; Files
  14074. (defun org-save-all-org-buffers ()
  14075. "Save all Org-mode buffers without user confirmation."
  14076. (interactive)
  14077. (message "Saving all Org-mode buffers...")
  14078. (save-some-buffers t 'org-mode-p)
  14079. (when (featurep 'org-id) (org-id-locations-save))
  14080. (message "Saving all Org-mode buffers... done"))
  14081. (defun org-revert-all-org-buffers ()
  14082. "Revert all Org-mode buffers.
  14083. Prompt for confirmation when there are unsaved changes.
  14084. Be sure you know what you are doing before letting this function
  14085. overwrite your changes.
  14086. This function is useful in a setup where one tracks org files
  14087. with a version control system, to revert on one machine after pulling
  14088. changes from another. I believe the procedure must be like this:
  14089. 1. M-x org-save-all-org-buffers
  14090. 2. Pull changes from the other machine, resolve conflicts
  14091. 3. M-x org-revert-all-org-buffers"
  14092. (interactive)
  14093. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14094. (error "Abort"))
  14095. (save-excursion
  14096. (save-window-excursion
  14097. (mapc
  14098. (lambda (b)
  14099. (when (and (with-current-buffer b (org-mode-p))
  14100. (with-current-buffer b buffer-file-name))
  14101. (switch-to-buffer b)
  14102. (revert-buffer t 'no-confirm)))
  14103. (buffer-list))
  14104. (when (and (featurep 'org-id) org-id-track-globally)
  14105. (org-id-locations-load)))))
  14106. ;;;; Agenda files
  14107. ;;;###autoload
  14108. (defun org-switchb (&optional arg)
  14109. "Switch between Org buffers.
  14110. With a prefix argument, restrict available to files.
  14111. With two prefix arguments, restrict available buffers to agenda files.
  14112. Defaults to `iswitchb' for buffer name completion.
  14113. Set `org-completion-use-ido' to make it use ido instead."
  14114. (interactive "P")
  14115. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14116. ((equal arg '(16)) (org-buffer-list 'agenda))
  14117. (t (org-buffer-list))))
  14118. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14119. (org-completion-use-ido org-completion-use-ido))
  14120. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14121. (setq org-completion-use-iswitchb t))
  14122. (switch-to-buffer
  14123. (org-icompleting-read "Org buffer: "
  14124. (mapcar 'list (mapcar 'buffer-name blist))
  14125. nil t))))
  14126. ;;; Define some older names previously used for this functionality
  14127. ;;;###autoload
  14128. (defalias 'org-ido-switchb 'org-switchb)
  14129. ;;;###autoload
  14130. (defalias 'org-iswitchb 'org-switchb)
  14131. (defun org-buffer-list (&optional predicate exclude-tmp)
  14132. "Return a list of Org buffers.
  14133. PREDICATE can be `export', `files' or `agenda'.
  14134. export restrict the list to Export buffers.
  14135. files restrict the list to buffers visiting Org files.
  14136. agenda restrict the list to buffers visiting agenda files.
  14137. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14138. (let* ((bfn nil)
  14139. (agenda-files (and (eq predicate 'agenda)
  14140. (mapcar 'file-truename (org-agenda-files t))))
  14141. (filter
  14142. (cond
  14143. ((eq predicate 'files)
  14144. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14145. ((eq predicate 'export)
  14146. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14147. ((eq predicate 'agenda)
  14148. (lambda (b)
  14149. (with-current-buffer b
  14150. (and (eq major-mode 'org-mode)
  14151. (setq bfn (buffer-file-name b))
  14152. (member (file-truename bfn) agenda-files)))))
  14153. (t (lambda (b) (with-current-buffer b
  14154. (or (eq major-mode 'org-mode)
  14155. (string-match "\*Org .*Export"
  14156. (buffer-name b)))))))))
  14157. (delq nil
  14158. (mapcar
  14159. (lambda(b)
  14160. (if (and (funcall filter b)
  14161. (or (not exclude-tmp)
  14162. (not (string-match "tmp" (buffer-name b)))))
  14163. b
  14164. nil))
  14165. (buffer-list)))))
  14166. (defun org-agenda-files (&optional unrestricted archives)
  14167. "Get the list of agenda files.
  14168. Optional UNRESTRICTED means return the full list even if a restriction
  14169. is currently in place.
  14170. When ARCHIVES is t, include all archive files that are really being
  14171. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14172. `org-agenda-archives-mode' is t."
  14173. (let ((files
  14174. (cond
  14175. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14176. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14177. ((listp org-agenda-files) org-agenda-files)
  14178. (t (error "Invalid value of `org-agenda-files'")))))
  14179. (setq files (apply 'append
  14180. (mapcar (lambda (f)
  14181. (if (file-directory-p f)
  14182. (directory-files
  14183. f t org-agenda-file-regexp)
  14184. (list f)))
  14185. files)))
  14186. (when org-agenda-skip-unavailable-files
  14187. (setq files (delq nil
  14188. (mapcar (function
  14189. (lambda (file)
  14190. (and (file-readable-p file) file)))
  14191. files))))
  14192. (when (or (eq archives t)
  14193. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14194. (setq files (org-add-archive-files files)))
  14195. files))
  14196. (defun org-agenda-file-p (&optional file)
  14197. "Return non-nil, if FILE is an agenda file.
  14198. If FILE is omitted, use the file associated with the current
  14199. buffer."
  14200. (member (or file (buffer-file-name))
  14201. (org-agenda-files t)))
  14202. (defun org-edit-agenda-file-list ()
  14203. "Edit the list of agenda files.
  14204. Depending on setup, this either uses customize to edit the variable
  14205. `org-agenda-files', or it visits the file that is holding the list. In the
  14206. latter case, the buffer is set up in a way that saving it automatically kills
  14207. the buffer and restores the previous window configuration."
  14208. (interactive)
  14209. (if (stringp org-agenda-files)
  14210. (let ((cw (current-window-configuration)))
  14211. (find-file org-agenda-files)
  14212. (org-set-local 'org-window-configuration cw)
  14213. (org-add-hook 'after-save-hook
  14214. (lambda ()
  14215. (set-window-configuration
  14216. (prog1 org-window-configuration
  14217. (kill-buffer (current-buffer))))
  14218. (org-install-agenda-files-menu)
  14219. (message "New agenda file list installed"))
  14220. nil 'local)
  14221. (message "%s" (substitute-command-keys
  14222. "Edit list and finish with \\[save-buffer]")))
  14223. (customize-variable 'org-agenda-files)))
  14224. (defun org-store-new-agenda-file-list (list)
  14225. "Set new value for the agenda file list and save it correctly."
  14226. (if (stringp org-agenda-files)
  14227. (let ((fe (org-read-agenda-file-list t)) b u)
  14228. (while (setq b (find-buffer-visiting org-agenda-files))
  14229. (kill-buffer b))
  14230. (with-temp-file org-agenda-files
  14231. (insert
  14232. (mapconcat
  14233. (lambda (f) ;; Keep un-expanded entries.
  14234. (if (setq u (assoc f fe))
  14235. (cdr u)
  14236. f))
  14237. list "\n")
  14238. "\n")))
  14239. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14240. (org-insert-mode-line-in-empty-file nil))
  14241. (setq org-agenda-files list)
  14242. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14243. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14244. "Read the list of agenda files from a file.
  14245. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14246. filenames, used by `org-store-new-agenda-file-list' to write back
  14247. un-expanded file names."
  14248. (when (file-directory-p org-agenda-files)
  14249. (error "`org-agenda-files' cannot be a single directory"))
  14250. (when (stringp org-agenda-files)
  14251. (with-temp-buffer
  14252. (insert-file-contents org-agenda-files)
  14253. (mapcar
  14254. (lambda (f)
  14255. (let ((e (expand-file-name (substitute-in-file-name f)
  14256. org-directory)))
  14257. (if pair-with-expansion
  14258. (cons e f)
  14259. e)))
  14260. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14261. ;;;###autoload
  14262. (defun org-cycle-agenda-files ()
  14263. "Cycle through the files in `org-agenda-files'.
  14264. If the current buffer visits an agenda file, find the next one in the list.
  14265. If the current buffer does not, find the first agenda file."
  14266. (interactive)
  14267. (let* ((fs (org-agenda-files t))
  14268. (files (append fs (list (car fs))))
  14269. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14270. file)
  14271. (unless files (error "No agenda files"))
  14272. (catch 'exit
  14273. (while (setq file (pop files))
  14274. (if (equal (file-truename file) tcf)
  14275. (when (car files)
  14276. (find-file (car files))
  14277. (throw 'exit t))))
  14278. (find-file (car fs)))
  14279. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14280. (defun org-agenda-file-to-front (&optional to-end)
  14281. "Move/add the current file to the top of the agenda file list.
  14282. If the file is not present in the list, it is added to the front. If it is
  14283. present, it is moved there. With optional argument TO-END, add/move to the
  14284. end of the list."
  14285. (interactive "P")
  14286. (let ((org-agenda-skip-unavailable-files nil)
  14287. (file-alist (mapcar (lambda (x)
  14288. (cons (file-truename x) x))
  14289. (org-agenda-files t)))
  14290. (ctf (file-truename buffer-file-name))
  14291. x had)
  14292. (setq x (assoc ctf file-alist) had x)
  14293. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14294. (if to-end
  14295. (setq file-alist (append (delq x file-alist) (list x)))
  14296. (setq file-alist (cons x (delq x file-alist))))
  14297. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14298. (org-install-agenda-files-menu)
  14299. (message "File %s to %s of agenda file list"
  14300. (if had "moved" "added") (if to-end "end" "front"))))
  14301. (defun org-remove-file (&optional file)
  14302. "Remove current file from the list of files in variable `org-agenda-files'.
  14303. These are the files which are being checked for agenda entries.
  14304. Optional argument FILE means use this file instead of the current."
  14305. (interactive)
  14306. (let* ((org-agenda-skip-unavailable-files nil)
  14307. (file (or file buffer-file-name))
  14308. (true-file (file-truename file))
  14309. (afile (abbreviate-file-name file))
  14310. (files (delq nil (mapcar
  14311. (lambda (x)
  14312. (if (equal true-file
  14313. (file-truename x))
  14314. nil x))
  14315. (org-agenda-files t)))))
  14316. (if (not (= (length files) (length (org-agenda-files t))))
  14317. (progn
  14318. (org-store-new-agenda-file-list files)
  14319. (org-install-agenda-files-menu)
  14320. (message "Removed file: %s" afile))
  14321. (message "File was not in list: %s (not removed)" afile))))
  14322. (defun org-file-menu-entry (file)
  14323. (vector file (list 'find-file file) t))
  14324. (defun org-check-agenda-file (file)
  14325. "Make sure FILE exists. If not, ask user what to do."
  14326. (when (not (file-exists-p file))
  14327. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14328. (abbreviate-file-name file))
  14329. (let ((r (downcase (read-char-exclusive))))
  14330. (cond
  14331. ((equal r ?r)
  14332. (org-remove-file file)
  14333. (throw 'nextfile t))
  14334. (t (error "Abort"))))))
  14335. (defun org-get-agenda-file-buffer (file)
  14336. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14337. it to the list of buffers which might be released later."
  14338. (let ((buf (org-find-base-buffer-visiting file)))
  14339. (if buf
  14340. buf ; just return it
  14341. ;; Make a new buffer and remember it
  14342. (setq buf (find-file-noselect file))
  14343. (if buf (push buf org-agenda-new-buffers))
  14344. buf)))
  14345. (defun org-release-buffers (blist)
  14346. "Release all buffers in list, asking the user for confirmation when needed.
  14347. When a buffer is unmodified, it is just killed. When modified, it is saved
  14348. \(if the user agrees) and then killed."
  14349. (let (buf file)
  14350. (while (setq buf (pop blist))
  14351. (setq file (buffer-file-name buf))
  14352. (when (and (buffer-modified-p buf)
  14353. file
  14354. (y-or-n-p (format "Save file %s? " file)))
  14355. (with-current-buffer buf (save-buffer)))
  14356. (kill-buffer buf))))
  14357. (defun org-prepare-agenda-buffers (files)
  14358. "Create buffers for all agenda files, protect archived trees and comments."
  14359. (interactive)
  14360. (let ((pa '(:org-archived t))
  14361. (pc '(:org-comment t))
  14362. (pall '(:org-archived t :org-comment t))
  14363. (inhibit-read-only t)
  14364. (rea (concat ":" org-archive-tag ":"))
  14365. bmp file re)
  14366. (save-excursion
  14367. (save-restriction
  14368. (while (setq file (pop files))
  14369. (catch 'nextfile
  14370. (if (bufferp file)
  14371. (set-buffer file)
  14372. (org-check-agenda-file file)
  14373. (set-buffer (org-get-agenda-file-buffer file)))
  14374. (widen)
  14375. (setq bmp (buffer-modified-p))
  14376. (org-refresh-category-properties)
  14377. (setq org-todo-keywords-for-agenda
  14378. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14379. (setq org-done-keywords-for-agenda
  14380. (append org-done-keywords-for-agenda org-done-keywords))
  14381. (setq org-todo-keyword-alist-for-agenda
  14382. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14383. (setq org-drawers-for-agenda
  14384. (append org-drawers-for-agenda org-drawers))
  14385. (setq org-tag-alist-for-agenda
  14386. (append org-tag-alist-for-agenda org-tag-alist))
  14387. (save-excursion
  14388. (remove-text-properties (point-min) (point-max) pall)
  14389. (when org-agenda-skip-archived-trees
  14390. (goto-char (point-min))
  14391. (while (re-search-forward rea nil t)
  14392. (if (org-on-heading-p t)
  14393. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14394. (goto-char (point-min))
  14395. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14396. (while (re-search-forward re nil t)
  14397. (add-text-properties
  14398. (match-beginning 0) (org-end-of-subtree t) pc)))
  14399. (set-buffer-modified-p bmp)))))
  14400. (setq org-todo-keywords-for-agenda
  14401. (org-uniquify org-todo-keywords-for-agenda))
  14402. (setq org-todo-keyword-alist-for-agenda
  14403. (org-uniquify org-todo-keyword-alist-for-agenda)
  14404. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14405. ;;;; Embedded LaTeX
  14406. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14407. "Keymap for the minor `org-cdlatex-mode'.")
  14408. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14409. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14410. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14411. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14412. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14413. (defvar org-cdlatex-texmathp-advice-is-done nil
  14414. "Flag remembering if we have applied the advice to texmathp already.")
  14415. (define-minor-mode org-cdlatex-mode
  14416. "Toggle the minor `org-cdlatex-mode'.
  14417. This mode supports entering LaTeX environment and math in LaTeX fragments
  14418. in Org-mode.
  14419. \\{org-cdlatex-mode-map}"
  14420. nil " OCDL" nil
  14421. (when org-cdlatex-mode (require 'cdlatex))
  14422. (unless org-cdlatex-texmathp-advice-is-done
  14423. (setq org-cdlatex-texmathp-advice-is-done t)
  14424. (defadvice texmathp (around org-math-always-on activate)
  14425. "Always return t in org-mode buffers.
  14426. This is because we want to insert math symbols without dollars even outside
  14427. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14428. an embedded LaTeX fragment, let texmathp do its job.
  14429. \\[org-cdlatex-mode-map]"
  14430. (interactive)
  14431. (let (p)
  14432. (cond
  14433. ((not (org-mode-p)) ad-do-it)
  14434. ((eq this-command 'cdlatex-math-symbol)
  14435. (setq ad-return-value t
  14436. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14437. (t
  14438. (let ((p (org-inside-LaTeX-fragment-p)))
  14439. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14440. (setq ad-return-value t
  14441. texmathp-why '("Org-mode embedded math" . 0))
  14442. (if p ad-do-it)))))))))
  14443. (defun turn-on-org-cdlatex ()
  14444. "Unconditionally turn on `org-cdlatex-mode'."
  14445. (org-cdlatex-mode 1))
  14446. (defun org-inside-LaTeX-fragment-p ()
  14447. "Test if point is inside a LaTeX fragment.
  14448. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14449. sequence appearing also before point.
  14450. Even though the matchers for math are configurable, this function assumes
  14451. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14452. delimiters are skipped when they have been removed by customization.
  14453. The return value is nil, or a cons cell with the delimiter and
  14454. and the position of this delimiter.
  14455. This function does a reasonably good job, but can locally be fooled by
  14456. for example currency specifications. For example it will assume being in
  14457. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14458. fragments that are properly closed, but during editing, we have to live
  14459. with the uncertainty caused by missing closing delimiters. This function
  14460. looks only before point, not after."
  14461. (catch 'exit
  14462. (let ((pos (point))
  14463. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14464. (lim (progn
  14465. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14466. (point)))
  14467. dd-on str (start 0) m re)
  14468. (goto-char pos)
  14469. (when dodollar
  14470. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14471. re (nth 1 (assoc "$" org-latex-regexps)))
  14472. (while (string-match re str start)
  14473. (cond
  14474. ((= (match-end 0) (length str))
  14475. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14476. ((= (match-end 0) (- (length str) 5))
  14477. (throw 'exit nil))
  14478. (t (setq start (match-end 0))))))
  14479. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14480. (goto-char pos)
  14481. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14482. (and (match-beginning 2) (throw 'exit nil))
  14483. ;; count $$
  14484. (while (re-search-backward "\\$\\$" lim t)
  14485. (setq dd-on (not dd-on)))
  14486. (goto-char pos)
  14487. (if dd-on (cons "$$" m))))))
  14488. (defun org-inside-latex-macro-p ()
  14489. "Is point inside a LaTeX macro or its arguments?"
  14490. (save-match-data
  14491. (org-in-regexp
  14492. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14493. (defun org-try-cdlatex-tab ()
  14494. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14495. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14496. - inside a LaTeX fragment, or
  14497. - after the first word in a line, where an abbreviation expansion could
  14498. insert a LaTeX environment."
  14499. (when org-cdlatex-mode
  14500. (cond
  14501. ((save-excursion
  14502. (skip-chars-backward "a-zA-Z0-9*")
  14503. (skip-chars-backward " \t")
  14504. (bolp))
  14505. (cdlatex-tab) t)
  14506. ((org-inside-LaTeX-fragment-p)
  14507. (cdlatex-tab) t)
  14508. (t nil))))
  14509. (defun org-cdlatex-underscore-caret (&optional arg)
  14510. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14511. Revert to the normal definition outside of these fragments."
  14512. (interactive "P")
  14513. (if (org-inside-LaTeX-fragment-p)
  14514. (call-interactively 'cdlatex-sub-superscript)
  14515. (let (org-cdlatex-mode)
  14516. (call-interactively (key-binding (vector last-input-event))))))
  14517. (defun org-cdlatex-math-modify (&optional arg)
  14518. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14519. Revert to the normal definition outside of these fragments."
  14520. (interactive "P")
  14521. (if (org-inside-LaTeX-fragment-p)
  14522. (call-interactively 'cdlatex-math-modify)
  14523. (let (org-cdlatex-mode)
  14524. (call-interactively (key-binding (vector last-input-event))))))
  14525. (defvar org-latex-fragment-image-overlays nil
  14526. "List of overlays carrying the images of latex fragments.")
  14527. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14528. (defun org-remove-latex-fragment-image-overlays ()
  14529. "Remove all overlays with LaTeX fragment images in current buffer."
  14530. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14531. (setq org-latex-fragment-image-overlays nil))
  14532. (defun org-preview-latex-fragment (&optional subtree)
  14533. "Preview the LaTeX fragment at point, or all locally or globally.
  14534. If the cursor is in a LaTeX fragment, create the image and overlay
  14535. it over the source code. If there is no fragment at point, display
  14536. all fragments in the current text, from one headline to the next. With
  14537. prefix SUBTREE, display all fragments in the current subtree. With a
  14538. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14539. the cursor is before the first headline,
  14540. display all fragments in the buffer.
  14541. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14542. (interactive "P")
  14543. (org-remove-latex-fragment-image-overlays)
  14544. (save-excursion
  14545. (save-restriction
  14546. (let (beg end at msg)
  14547. (cond
  14548. ((or (equal subtree '(16))
  14549. (not (save-excursion
  14550. (re-search-backward (concat "^" outline-regexp) nil t))))
  14551. (setq beg (point-min) end (point-max)
  14552. msg "Creating images for buffer...%s"))
  14553. ((equal subtree '(4))
  14554. (org-back-to-heading)
  14555. (setq beg (point) end (org-end-of-subtree t)
  14556. msg "Creating images for subtree...%s"))
  14557. (t
  14558. (if (setq at (org-inside-LaTeX-fragment-p))
  14559. (goto-char (max (point-min) (- (cdr at) 2)))
  14560. (org-back-to-heading))
  14561. (setq beg (point) end (progn (outline-next-heading) (point))
  14562. msg (if at "Creating image...%s"
  14563. "Creating images for entry...%s"))))
  14564. (message msg "")
  14565. (narrow-to-region beg end)
  14566. (goto-char beg)
  14567. (org-format-latex
  14568. (concat "ltxpng/" (file-name-sans-extension
  14569. (file-name-nondirectory
  14570. buffer-file-name)))
  14571. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14572. (message msg "done. Use `C-c C-c' to remove images.")))))
  14573. (defvar org-latex-regexps
  14574. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14575. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14576. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14577. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14578. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14579. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14580. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14581. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14582. "Regular expressions for matching embedded LaTeX.")
  14583. (defvar org-export-have-math nil) ;; dynamic scoping
  14584. (defun org-format-latex (prefix &optional dir overlays msg at
  14585. forbuffer processing-type)
  14586. "Replace LaTeX fragments with links to an image, and produce images.
  14587. Some of the options can be changed using the variable
  14588. `org-format-latex-options'."
  14589. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14590. (let* ((prefixnodir (file-name-nondirectory prefix))
  14591. (absprefix (expand-file-name prefix dir))
  14592. (todir (file-name-directory absprefix))
  14593. (opt org-format-latex-options)
  14594. (matchers (plist-get opt :matchers))
  14595. (re-list org-latex-regexps)
  14596. (org-format-latex-header-extra
  14597. (plist-get (org-infile-export-plist) :latex-header-extra))
  14598. (cnt 0) txt hash link beg end re e checkdir
  14599. executables-checked string
  14600. m n block linkfile movefile ov)
  14601. ;; Check the different regular expressions
  14602. (while (setq e (pop re-list))
  14603. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14604. block (if (nth 3 e) "\n\n" ""))
  14605. (when (member m matchers)
  14606. (goto-char (point-min))
  14607. (while (re-search-forward re nil t)
  14608. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14609. (not (get-text-property (match-beginning n)
  14610. 'org-protected))
  14611. (or (not overlays)
  14612. (not (eq (get-char-property (match-beginning n)
  14613. 'org-overlay-type)
  14614. 'org-latex-overlay))))
  14615. (setq org-export-have-math t)
  14616. (cond
  14617. ((eq processing-type 'verbatim)
  14618. ;; Leave the text verbatim, just protect it
  14619. (add-text-properties (match-beginning n) (match-end n)
  14620. '(org-protected t)))
  14621. ((eq processing-type 'mathjax)
  14622. ;; Prepare for MathJax processing
  14623. (setq string (match-string n))
  14624. (if (member m '("$" "$1"))
  14625. (save-excursion
  14626. (delete-region (match-beginning n) (match-end n))
  14627. (goto-char (match-beginning n))
  14628. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14629. "\\)")
  14630. '(org-protected t))))
  14631. (add-text-properties (match-beginning n) (match-end n)
  14632. '(org-protected t))))
  14633. ((or (eq processing-type 'dvipng) t)
  14634. ;; Process to an image
  14635. (setq txt (match-string n)
  14636. beg (match-beginning n) end (match-end n)
  14637. cnt (1+ cnt))
  14638. (let (print-length print-level) ; make sure full list is printed
  14639. (setq hash (sha1 (prin1-to-string
  14640. (list org-format-latex-header
  14641. org-format-latex-header-extra
  14642. org-export-latex-default-packages-alist
  14643. org-export-latex-packages-alist
  14644. org-format-latex-options
  14645. forbuffer txt)))
  14646. linkfile (format "%s_%s.png" prefix hash)
  14647. movefile (format "%s_%s.png" absprefix hash)))
  14648. (setq link (concat block "[[file:" linkfile "]]" block))
  14649. (if msg (message msg cnt))
  14650. (goto-char beg)
  14651. (unless checkdir ; make sure the directory exists
  14652. (setq checkdir t)
  14653. (or (file-directory-p todir) (make-directory todir t)))
  14654. (unless executables-checked
  14655. (org-check-external-command
  14656. "latex" "needed to convert LaTeX fragments to images")
  14657. (org-check-external-command
  14658. "dvipng" "needed to convert LaTeX fragments to images")
  14659. (setq executables-checked t))
  14660. (unless (file-exists-p movefile)
  14661. (org-create-formula-image
  14662. txt movefile opt forbuffer))
  14663. (if overlays
  14664. (progn
  14665. (mapc (lambda (o)
  14666. (if (eq (overlay-get o 'org-overlay-type)
  14667. 'org-latex-overlay)
  14668. (delete-overlay o)))
  14669. (overlays-in beg end))
  14670. (setq ov (make-overlay beg end))
  14671. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14672. (if (featurep 'xemacs)
  14673. (progn
  14674. (overlay-put ov 'invisible t)
  14675. (overlay-put
  14676. ov 'end-glyph
  14677. (make-glyph (vector 'png :file movefile))))
  14678. (overlay-put
  14679. ov 'display
  14680. (list 'image :type 'png :file movefile :ascent 'center)))
  14681. (push ov org-latex-fragment-image-overlays)
  14682. (goto-char end))
  14683. (delete-region beg end)
  14684. (insert (org-add-props link
  14685. (list 'org-latex-src
  14686. (replace-regexp-in-string
  14687. "\"" "" txt)))))))))))))
  14688. ;; This function borrows from Ganesh Swami's latex2png.el
  14689. (defun org-create-formula-image (string tofile options buffer)
  14690. "This calls dvipng."
  14691. (require 'org-latex)
  14692. (let* ((tmpdir (if (featurep 'xemacs)
  14693. (temp-directory)
  14694. temporary-file-directory))
  14695. (texfilebase (make-temp-name
  14696. (expand-file-name "orgtex" tmpdir)))
  14697. (texfile (concat texfilebase ".tex"))
  14698. (dvifile (concat texfilebase ".dvi"))
  14699. (pngfile (concat texfilebase ".png"))
  14700. (fnh (if (featurep 'xemacs)
  14701. (font-height (get-face-font 'default))
  14702. (face-attribute 'default :height nil)))
  14703. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14704. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14705. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14706. "Black"))
  14707. (bg (or (plist-get options (if buffer :background :html-background))
  14708. "Transparent")))
  14709. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14710. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14711. (with-temp-file texfile
  14712. (insert (org-splice-latex-header
  14713. org-format-latex-header
  14714. org-export-latex-default-packages-alist
  14715. org-export-latex-packages-alist t
  14716. org-format-latex-header-extra))
  14717. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14718. (require 'org-latex)
  14719. (org-export-latex-fix-inputenc))
  14720. (let ((dir default-directory))
  14721. (condition-case nil
  14722. (progn
  14723. (cd tmpdir)
  14724. (call-process "latex" nil nil nil texfile))
  14725. (error nil))
  14726. (cd dir))
  14727. (if (not (file-exists-p dvifile))
  14728. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14729. (condition-case nil
  14730. (call-process "dvipng" nil nil nil
  14731. "-fg" fg "-bg" bg
  14732. "-D" dpi
  14733. ;;"-x" scale "-y" scale
  14734. "-T" "tight"
  14735. "-o" pngfile
  14736. dvifile)
  14737. (error nil))
  14738. (if (not (file-exists-p pngfile))
  14739. (if org-format-latex-signal-error
  14740. (error "Failed to create png file from %s" texfile)
  14741. (message "Failed to create png file from %s" texfile)
  14742. nil)
  14743. ;; Use the requested file name and clean up
  14744. (copy-file pngfile tofile 'replace)
  14745. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14746. (delete-file (concat texfilebase e)))
  14747. pngfile))))
  14748. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14749. "Fill a LaTeX header template TPL.
  14750. In the template, the following place holders will be recognized:
  14751. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14752. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14753. [PACKAGES] \\usepackage statements for PKG
  14754. [NO-PACKAGES] do not include PKG
  14755. [EXTRA] the string EXTRA
  14756. [NO-EXTRA] do not include EXTRA
  14757. For backward compatibility, if both the positive and the negative place
  14758. holder is missing, the positive one (without the \"NO-\") will be
  14759. assumed to be present at the end of the template.
  14760. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14761. EXTRA is a string.
  14762. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14763. (let (rpl (end ""))
  14764. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14765. (setq rpl (if (or (match-end 1) (not def-pkg))
  14766. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14767. tpl (replace-match rpl t t tpl))
  14768. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14769. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14770. (setq rpl (if (or (match-end 1) (not pkg))
  14771. "" (org-latex-packages-to-string pkg snippets-p t))
  14772. tpl (replace-match rpl t t tpl))
  14773. (if pkg (setq end
  14774. (concat end "\n"
  14775. (org-latex-packages-to-string pkg snippets-p)))))
  14776. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14777. (setq rpl (if (or (match-end 1) (not extra))
  14778. "" (concat extra "\n"))
  14779. tpl (replace-match rpl t t tpl))
  14780. (if (and extra (string-match "\\S-" extra))
  14781. (setq end (concat end "\n" extra))))
  14782. (if (string-match "\\S-" end)
  14783. (concat tpl "\n" end)
  14784. tpl)))
  14785. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14786. "Turn an alist of packages into a string with the \\usepackage macros."
  14787. (setq pkg (mapconcat (lambda(p)
  14788. (cond
  14789. ((stringp p) p)
  14790. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14791. (format "%% Package %s omitted" (cadr p)))
  14792. ((equal "" (car p))
  14793. (format "\\usepackage{%s}" (cadr p)))
  14794. (t
  14795. (format "\\usepackage[%s]{%s}"
  14796. (car p) (cadr p)))))
  14797. pkg
  14798. "\n"))
  14799. (if newline (concat pkg "\n") pkg))
  14800. (defun org-dvipng-color (attr)
  14801. "Return an rgb color specification for dvipng."
  14802. (apply 'format "rgb %s %s %s"
  14803. (mapcar 'org-normalize-color
  14804. (color-values (face-attribute 'default attr nil)))))
  14805. (defun org-normalize-color (value)
  14806. "Return string to be used as color value for an RGB component."
  14807. (format "%g" (/ value 65535.0)))
  14808. ;; Image display
  14809. (defvar org-inline-image-overlays nil)
  14810. (make-variable-buffer-local 'org-inline-image-overlays)
  14811. (defun org-toggle-inline-images (&optional include-linked)
  14812. "Toggle the display of inline images.
  14813. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14814. (interactive "P")
  14815. (if org-inline-image-overlays
  14816. (progn
  14817. (org-remove-inline-images)
  14818. (message "Inline image display turned off"))
  14819. (org-display-inline-images include-linked)
  14820. (if org-inline-image-overlays
  14821. (message "%d images displayed inline"
  14822. (length org-inline-image-overlays))
  14823. (message "No images to display inline"))))
  14824. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14825. "Display inline images.
  14826. Normally only links without a description part are inlined, because this
  14827. is how it will work for export. When INCLUDE-LINKED is set, also links
  14828. with a description part will be inlined. This can be nice for a quick
  14829. look at those images, but it does not reflect what exported files will look
  14830. like.
  14831. When REFRESH is set, refresh existing images between BEG and END.
  14832. This will create new image displays only if necessary.
  14833. BEG and END default to the buffer boundaries."
  14834. (interactive "P")
  14835. (unless refresh
  14836. (org-remove-inline-images)
  14837. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  14838. (save-excursion
  14839. (save-restriction
  14840. (widen)
  14841. (setq beg (or beg (point-min)) end (or end (point-max)))
  14842. (goto-char (point-min))
  14843. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  14844. (substring (org-image-file-name-regexp) 0 -2)
  14845. "\\)\\]" (if include-linked "" "\\]")))
  14846. old file ov img)
  14847. (while (re-search-forward re end t)
  14848. (setq old (get-char-property-and-overlay (match-beginning 1)
  14849. 'org-image-overlay))
  14850. (setq file (expand-file-name
  14851. (concat (or (match-string 3) "") (match-string 4))))
  14852. (when (file-exists-p file)
  14853. (if (and (car-safe old) refresh)
  14854. (image-refresh (overlay-get (cdr old) 'display))
  14855. (setq img (save-match-data (create-image file)))
  14856. (when img
  14857. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14858. (overlay-put ov 'display img)
  14859. (overlay-put ov 'face 'default)
  14860. (overlay-put ov 'org-image-overlay t)
  14861. (overlay-put ov 'modification-hooks
  14862. (list 'org-display-inline-modification-hook))
  14863. (push ov org-inline-image-overlays)))))))))
  14864. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  14865. "Remove inline-display overlay if a corresponding region is modified."
  14866. (let ((inhibit-modification-hooks t))
  14867. (when (and ov after)
  14868. (delete ov org-inline-image-overlays)
  14869. (delete-overlay ov))))
  14870. (defun org-remove-inline-images ()
  14871. "Remove inline display of images."
  14872. (interactive)
  14873. (mapc 'delete-overlay org-inline-image-overlays)
  14874. (setq org-inline-image-overlays nil))
  14875. ;;;; Key bindings
  14876. ;; Make `C-c C-x' a prefix key
  14877. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14878. ;; TAB key with modifiers
  14879. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14880. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14881. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14882. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  14883. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  14884. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  14885. ;; The following line is necessary under Suse GNU/Linux
  14886. (unless (featurep 'xemacs)
  14887. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14888. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14889. (define-key org-mode-map [backtab] 'org-shifttab)
  14890. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14891. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14892. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14893. ;; Cursor keys with modifiers
  14894. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14895. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14896. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14897. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14898. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14899. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14900. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14901. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14902. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14903. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14904. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14905. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14906. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14907. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14908. ;; Babel keys
  14909. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  14910. (mapc (lambda (pair)
  14911. (define-key org-babel-map (car pair) (cdr pair)))
  14912. org-babel-key-bindings)
  14913. ;;; Extra keys for tty access.
  14914. ;; We only set them when really needed because otherwise the
  14915. ;; menus don't show the simple keys
  14916. (when (or org-use-extra-keys
  14917. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14918. (not window-system))
  14919. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14920. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14921. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14922. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14923. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14924. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14925. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14926. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14927. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14928. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14929. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14930. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14931. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14932. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14933. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14934. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14935. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14936. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14937. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14938. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14939. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14940. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14941. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  14942. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14943. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14944. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14945. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14946. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14947. ;; All the other keys
  14948. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14949. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14950. (if (boundp 'narrow-map)
  14951. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14952. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14953. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14954. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14955. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14956. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14957. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14958. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14959. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14960. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14961. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14962. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14963. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14964. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14965. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14966. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14967. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14968. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14969. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14970. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14971. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14972. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14973. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14974. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14975. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14976. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14977. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14978. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14979. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14980. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14981. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14982. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14983. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14984. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14985. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14986. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14987. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14988. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14989. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14990. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14991. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14992. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14993. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14994. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14995. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14996. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14997. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14998. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14999. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15000. (org-defkey org-mode-map "\C-m" 'org-return)
  15001. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15002. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15003. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15004. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15005. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15006. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15007. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15008. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15009. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15010. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15011. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15012. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15013. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15014. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15015. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15016. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15017. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15018. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15019. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15020. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15021. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15022. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15023. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15024. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15025. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15026. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15027. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15028. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15029. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15030. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15031. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15032. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15033. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15034. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15035. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15036. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15037. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15038. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15039. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15040. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15041. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15042. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15043. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15044. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15045. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15046. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15047. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15048. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15049. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15050. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15051. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15052. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15053. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15054. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15055. (when (featurep 'xemacs)
  15056. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15057. (defconst org-speed-commands-default
  15058. '(
  15059. ("Outline Navigation")
  15060. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15061. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15062. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15063. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15064. ("u" . (org-speed-move-safe 'outline-up-heading))
  15065. ("j" . org-goto)
  15066. ("g" . (org-refile t))
  15067. ("Outline Visibility")
  15068. ("c" . org-cycle)
  15069. ("C" . org-shifttab)
  15070. (" " . org-display-outline-path)
  15071. ("Outline Structure Editing")
  15072. ("U" . org-shiftmetaup)
  15073. ("D" . org-shiftmetadown)
  15074. ("r" . org-metaright)
  15075. ("l" . org-metaleft)
  15076. ("R" . org-shiftmetaright)
  15077. ("L" . org-shiftmetaleft)
  15078. ("i" . (progn (forward-char 1) (call-interactively
  15079. 'org-insert-heading-respect-content)))
  15080. ("^" . org-sort)
  15081. ("w" . org-refile)
  15082. ("a" . org-archive-subtree-default-with-confirmation)
  15083. ("." . org-mark-subtree)
  15084. ("Clock Commands")
  15085. ("I" . org-clock-in)
  15086. ("O" . org-clock-out)
  15087. ("Meta Data Editing")
  15088. ("t" . org-todo)
  15089. ("0" . (org-priority ?\ ))
  15090. ("1" . (org-priority ?A))
  15091. ("2" . (org-priority ?B))
  15092. ("3" . (org-priority ?C))
  15093. (";" . org-set-tags-command)
  15094. ("e" . org-set-effort)
  15095. ("Agenda Views etc")
  15096. ("v" . org-agenda)
  15097. ("/" . org-sparse-tree)
  15098. ("Misc")
  15099. ("o" . org-open-at-point)
  15100. ("?" . org-speed-command-help)
  15101. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15102. (">" . (org-agenda-remove-restriction-lock))
  15103. )
  15104. "The default speed commands.")
  15105. (defun org-print-speed-command (e)
  15106. (if (> (length (car e)) 1)
  15107. (progn
  15108. (princ "\n")
  15109. (princ (car e))
  15110. (princ "\n")
  15111. (princ (make-string (length (car e)) ?-))
  15112. (princ "\n"))
  15113. (princ (car e))
  15114. (princ " ")
  15115. (if (symbolp (cdr e))
  15116. (princ (symbol-name (cdr e)))
  15117. (prin1 (cdr e)))
  15118. (princ "\n")))
  15119. (defun org-speed-command-help ()
  15120. "Show the available speed commands."
  15121. (interactive)
  15122. (if (not org-use-speed-commands)
  15123. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15124. (with-output-to-temp-buffer "*Help*"
  15125. (princ "User-defined Speed commands\n===========================\n")
  15126. (mapc 'org-print-speed-command org-speed-commands-user)
  15127. (princ "\n")
  15128. (princ "Built-in Speed commands\n=======================\n")
  15129. (mapc 'org-print-speed-command org-speed-commands-default))
  15130. (with-current-buffer "*Help*"
  15131. (setq truncate-lines t))))
  15132. (defun org-speed-move-safe (cmd)
  15133. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15134. If not, return to the original position and throw an error."
  15135. (interactive)
  15136. (let ((pos (point)))
  15137. (call-interactively cmd)
  15138. (unless (and (bolp) (org-on-heading-p))
  15139. (goto-char pos)
  15140. (error "Boundary reached while executing %s" cmd))))
  15141. (defvar org-self-insert-command-undo-counter 0)
  15142. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15143. (defvar org-speed-command nil)
  15144. (defun org-speed-command-default-hook (keys)
  15145. "Hook for activating single-letter speed commands.
  15146. `org-speed-commands-default' specifies a minimal command set. Use
  15147. `org-speed-commands-user' for further customization."
  15148. (when (or (and (bolp) (looking-at outline-regexp))
  15149. (and (functionp org-use-speed-commands)
  15150. (funcall org-use-speed-commands)))
  15151. (cdr (assoc keys (append org-speed-commands-user
  15152. org-speed-commands-default)))))
  15153. (defun org-babel-speed-command-hook (keys)
  15154. "Hook for activating single-letter code block commands."
  15155. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15156. (cdr (assoc keys org-babel-key-bindings))))
  15157. (defcustom org-speed-command-hook
  15158. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15159. "Hook for activating speed commands at strategic locations.
  15160. Hook functions are called in sequence until a valid handler is
  15161. found.
  15162. Each hook takes a single argument, a user-pressed command key
  15163. which is also a `self-insert-command' from the global map.
  15164. Within the hook, examine the cursor position and the command key
  15165. and return nil or a valid handler as appropriate. Handler could
  15166. be one of an interactive command, a function, or a form.
  15167. Set `org-use-speed-commands' to non-nil value to enable this
  15168. hook. The default setting is `org-speed-command-default-hook'."
  15169. :group 'org-structure
  15170. :type 'hook)
  15171. (defun org-self-insert-command (N)
  15172. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15173. If the cursor is in a table looking at whitespace, the whitespace is
  15174. overwritten, and the table is not marked as requiring realignment."
  15175. (interactive "p")
  15176. (cond
  15177. ((and org-use-speed-commands
  15178. (setq org-speed-command
  15179. (run-hook-with-args-until-success
  15180. 'org-speed-command-hook (this-command-keys))))
  15181. (cond
  15182. ((commandp org-speed-command)
  15183. (setq this-command org-speed-command)
  15184. (call-interactively org-speed-command))
  15185. ((functionp org-speed-command)
  15186. (funcall org-speed-command))
  15187. ((and org-speed-command (listp org-speed-command))
  15188. (eval org-speed-command))
  15189. (t (let (org-use-speed-commands)
  15190. (call-interactively 'org-self-insert-command)))))
  15191. ((and
  15192. (org-table-p)
  15193. (progn
  15194. ;; check if we blank the field, and if that triggers align
  15195. (and (featurep 'org-table) org-table-auto-blank-field
  15196. (member last-command
  15197. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15198. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15199. ;; got extra space, this field does not determine column width
  15200. (let (org-table-may-need-update) (org-table-blank-field))
  15201. ;; no extra space, this field may determine column width
  15202. (org-table-blank-field)))
  15203. t)
  15204. (eq N 1)
  15205. (looking-at "[^|\n]* |"))
  15206. (let (org-table-may-need-update)
  15207. (goto-char (1- (match-end 0)))
  15208. (delete-backward-char 1)
  15209. (goto-char (match-beginning 0))
  15210. (self-insert-command N)))
  15211. (t
  15212. (setq org-table-may-need-update t)
  15213. (self-insert-command N)
  15214. (org-fix-tags-on-the-fly)
  15215. (if org-self-insert-cluster-for-undo
  15216. (if (not (eq last-command 'org-self-insert-command))
  15217. (setq org-self-insert-command-undo-counter 1)
  15218. (if (>= org-self-insert-command-undo-counter 20)
  15219. (setq org-self-insert-command-undo-counter 1)
  15220. (and (> org-self-insert-command-undo-counter 0)
  15221. buffer-undo-list
  15222. (not (cadr buffer-undo-list)) ; remove nil entry
  15223. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15224. (setq org-self-insert-command-undo-counter
  15225. (1+ org-self-insert-command-undo-counter))))))))
  15226. (defun org-fix-tags-on-the-fly ()
  15227. (when (and (equal (char-after (point-at-bol)) ?*)
  15228. (org-on-heading-p))
  15229. (org-align-tags-here org-tags-column)))
  15230. (defun org-delete-backward-char (N)
  15231. "Like `delete-backward-char', insert whitespace at field end in tables.
  15232. When deleting backwards, in tables this function will insert whitespace in
  15233. front of the next \"|\" separator, to keep the table aligned. The table will
  15234. still be marked for re-alignment if the field did fill the entire column,
  15235. because, in this case the deletion might narrow the column."
  15236. (interactive "p")
  15237. (if (and (org-table-p)
  15238. (eq N 1)
  15239. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15240. (looking-at ".*?|"))
  15241. (let ((pos (point))
  15242. (noalign (looking-at "[^|\n\r]* |"))
  15243. (c org-table-may-need-update))
  15244. (backward-delete-char N)
  15245. (if (not overwrite-mode)
  15246. (progn
  15247. (skip-chars-forward "^|")
  15248. (insert " ")
  15249. (goto-char (1- pos))))
  15250. ;; noalign: if there were two spaces at the end, this field
  15251. ;; does not determine the width of the column.
  15252. (if noalign (setq org-table-may-need-update c)))
  15253. (backward-delete-char N)
  15254. (org-fix-tags-on-the-fly)))
  15255. (defun org-delete-char (N)
  15256. "Like `delete-char', but insert whitespace at field end in tables.
  15257. When deleting characters, in tables this function will insert whitespace in
  15258. front of the next \"|\" separator, to keep the table aligned. The table will
  15259. still be marked for re-alignment if the field did fill the entire column,
  15260. because, in this case the deletion might narrow the column."
  15261. (interactive "p")
  15262. (if (and (org-table-p)
  15263. (not (bolp))
  15264. (not (= (char-after) ?|))
  15265. (eq N 1))
  15266. (if (looking-at ".*?|")
  15267. (let ((pos (point))
  15268. (noalign (looking-at "[^|\n\r]* |"))
  15269. (c org-table-may-need-update))
  15270. (replace-match (concat
  15271. (substring (match-string 0) 1 -1)
  15272. " |"))
  15273. (goto-char pos)
  15274. ;; noalign: if there were two spaces at the end, this field
  15275. ;; does not determine the width of the column.
  15276. (if noalign (setq org-table-may-need-update c)))
  15277. (delete-char N))
  15278. (delete-char N)
  15279. (org-fix-tags-on-the-fly)))
  15280. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15281. (put 'org-self-insert-command 'delete-selection t)
  15282. (put 'orgtbl-self-insert-command 'delete-selection t)
  15283. (put 'org-delete-char 'delete-selection 'supersede)
  15284. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15285. (put 'org-yank 'delete-selection 'yank)
  15286. ;; Make `flyspell-mode' delay after some commands
  15287. (put 'org-self-insert-command 'flyspell-delayed t)
  15288. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15289. (put 'org-delete-char 'flyspell-delayed t)
  15290. (put 'org-delete-backward-char 'flyspell-delayed t)
  15291. ;; Make pabbrev-mode expand after org-mode commands
  15292. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15293. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15294. ;; How to do this: Measure non-white length of current string
  15295. ;; If equal to column width, we should realign.
  15296. (defun org-remap (map &rest commands)
  15297. "In MAP, remap the functions given in COMMANDS.
  15298. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15299. (let (new old)
  15300. (while commands
  15301. (setq old (pop commands) new (pop commands))
  15302. (if (fboundp 'command-remapping)
  15303. (org-defkey map (vector 'remap old) new)
  15304. (substitute-key-definition old new map global-map)))))
  15305. (when (eq org-enable-table-editor 'optimized)
  15306. ;; If the user wants maximum table support, we need to hijack
  15307. ;; some standard editing functions
  15308. (org-remap org-mode-map
  15309. 'self-insert-command 'org-self-insert-command
  15310. 'delete-char 'org-delete-char
  15311. 'delete-backward-char 'org-delete-backward-char)
  15312. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15313. (defvar org-ctrl-c-ctrl-c-hook nil
  15314. "Hook for functions attaching themselves to `C-c C-c'.
  15315. This can be used to add additional functionality to the C-c C-c key which
  15316. executes context-dependent commands.
  15317. Each function will be called with no arguments. The function must check
  15318. if the context is appropriate for it to act. If yes, it should do its
  15319. thing and then return a non-nil value. If the context is wrong,
  15320. just do nothing and return nil.")
  15321. (defvar org-tab-first-hook nil
  15322. "Hook for functions to attach themselves to TAB.
  15323. See `org-ctrl-c-ctrl-c-hook' for more information.
  15324. This hook runs as the first action when TAB is pressed, even before
  15325. `org-cycle' messes around with the `outline-regexp' to cater for
  15326. inline tasks and plain list item folding.
  15327. If any function in this hook returns t, any other actions that
  15328. would have been caused by TAB (such as table field motion or visibility
  15329. cycling) will not occur.")
  15330. (defvar org-tab-after-check-for-table-hook nil
  15331. "Hook for functions to attach themselves to TAB.
  15332. See `org-ctrl-c-ctrl-c-hook' for more information.
  15333. This hook runs after it has been established that the cursor is not in a
  15334. table, but before checking if the cursor is in a headline or if global cycling
  15335. should be done.
  15336. If any function in this hook returns t, not other actions like visibility
  15337. cycling will be done.")
  15338. (defvar org-tab-after-check-for-cycling-hook nil
  15339. "Hook for functions to attach themselves to TAB.
  15340. See `org-ctrl-c-ctrl-c-hook' for more information.
  15341. This hook runs after it has been established that not table field motion and
  15342. not visibility should be done because of current context. This is probably
  15343. the place where a package like yasnippets can hook in.")
  15344. (defvar org-tab-before-tab-emulation-hook nil
  15345. "Hook for functions to attach themselves to TAB.
  15346. See `org-ctrl-c-ctrl-c-hook' for more information.
  15347. This hook runs after every other options for TAB have been exhausted, but
  15348. before indentation and \t insertion takes place.")
  15349. (defvar org-metaleft-hook nil
  15350. "Hook for functions attaching themselves to `M-left'.
  15351. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15352. (defvar org-metaright-hook nil
  15353. "Hook for functions attaching themselves to `M-right'.
  15354. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15355. (defvar org-metaup-hook nil
  15356. "Hook for functions attaching themselves to `M-up'.
  15357. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15358. (defvar org-metadown-hook nil
  15359. "Hook for functions attaching themselves to `M-down'.
  15360. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15361. (defvar org-shiftmetaleft-hook nil
  15362. "Hook for functions attaching themselves to `M-S-left'.
  15363. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15364. (defvar org-shiftmetaright-hook nil
  15365. "Hook for functions attaching themselves to `M-S-right'.
  15366. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15367. (defvar org-shiftmetaup-hook nil
  15368. "Hook for functions attaching themselves to `M-S-up'.
  15369. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15370. (defvar org-shiftmetadown-hook nil
  15371. "Hook for functions attaching themselves to `M-S-down'.
  15372. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15373. (defvar org-metareturn-hook nil
  15374. "Hook for functions attaching themselves to `M-RET'.
  15375. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15376. (defvar org-shiftup-hook nil
  15377. "Hook for functions attaching themselves to `S-up'.
  15378. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15379. (defvar org-shiftup-final-hook nil
  15380. "Hook for functions attaching themselves to `S-up'.
  15381. This one runs after all other options except shift-select have been excluded.
  15382. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15383. (defvar org-shiftdown-hook nil
  15384. "Hook for functions attaching themselves to `S-down'.
  15385. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15386. (defvar org-shiftdown-final-hook nil
  15387. "Hook for functions attaching themselves to `S-down'.
  15388. This one runs after all other options except shift-select have been excluded.
  15389. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15390. (defvar org-shiftleft-hook nil
  15391. "Hook for functions attaching themselves to `S-left'.
  15392. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15393. (defvar org-shiftleft-final-hook nil
  15394. "Hook for functions attaching themselves to `S-left'.
  15395. This one runs after all other options except shift-select have been excluded.
  15396. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15397. (defvar org-shiftright-hook nil
  15398. "Hook for functions attaching themselves to `S-right'.
  15399. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15400. (defvar org-shiftright-final-hook nil
  15401. "Hook for functions attaching themselves to `S-right'.
  15402. This one runs after all other options except shift-select have been excluded.
  15403. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15404. (defun org-modifier-cursor-error ()
  15405. "Throw an error, a modified cursor command was applied in wrong context."
  15406. (error "This command is active in special context like tables, headlines or items"))
  15407. (defun org-shiftselect-error ()
  15408. "Throw an error because Shift-Cursor command was applied in wrong context."
  15409. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15410. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15411. (error "This command works only in special context like headlines or timestamps")))
  15412. (defun org-call-for-shift-select (cmd)
  15413. (let ((this-command-keys-shift-translated t))
  15414. (call-interactively cmd)))
  15415. (defun org-shifttab (&optional arg)
  15416. "Global visibility cycling or move to previous table field.
  15417. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15418. on context.
  15419. See the individual commands for more information."
  15420. (interactive "P")
  15421. (cond
  15422. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15423. ((integerp arg)
  15424. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15425. (message "Content view to level: %d" arg)
  15426. (org-content (prefix-numeric-value arg2))
  15427. (setq org-cycle-global-status 'overview)))
  15428. (t (call-interactively 'org-global-cycle))))
  15429. (defun org-shiftmetaleft ()
  15430. "Promote subtree or delete table column.
  15431. Calls `org-promote-subtree', `org-outdent-item',
  15432. or `org-table-delete-column', depending on context.
  15433. See the individual commands for more information."
  15434. (interactive)
  15435. (cond
  15436. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15437. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15438. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15439. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15440. (t (org-modifier-cursor-error))))
  15441. (defun org-shiftmetaright ()
  15442. "Demote subtree or insert table column.
  15443. Calls `org-demote-subtree', `org-indent-item',
  15444. or `org-table-insert-column', depending on context.
  15445. See the individual commands for more information."
  15446. (interactive)
  15447. (cond
  15448. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15449. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15450. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15451. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15452. (t (org-modifier-cursor-error))))
  15453. (defun org-shiftmetaup (&optional arg)
  15454. "Move subtree up or kill table row.
  15455. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15456. `org-move-item-up' depending on context. See the individual commands
  15457. for more information."
  15458. (interactive "P")
  15459. (cond
  15460. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15461. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15462. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15463. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15464. (t (org-modifier-cursor-error))))
  15465. (defun org-shiftmetadown (&optional arg)
  15466. "Move subtree down or insert table row.
  15467. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15468. `org-move-item-down', depending on context. See the individual
  15469. commands for more information."
  15470. (interactive "P")
  15471. (cond
  15472. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15473. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15474. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15475. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15476. (t (org-modifier-cursor-error))))
  15477. (defsubst org-hidden-tree-error ()
  15478. (error
  15479. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15480. (defun org-metaleft (&optional arg)
  15481. "Promote heading or move table column to left.
  15482. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15483. With no specific context, calls the Emacs default `backward-word'.
  15484. See the individual commands for more information."
  15485. (interactive "P")
  15486. (cond
  15487. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15488. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15489. ((or (org-on-heading-p)
  15490. (and (org-region-active-p)
  15491. (save-excursion
  15492. (goto-char (region-beginning))
  15493. (org-on-heading-p))))
  15494. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15495. (call-interactively 'org-do-promote))
  15496. ((or (org-at-item-p)
  15497. (and (org-region-active-p)
  15498. (save-excursion
  15499. (goto-char (region-beginning))
  15500. (org-at-item-p))))
  15501. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15502. (call-interactively 'org-outdent-item))
  15503. (t (call-interactively 'backward-word))))
  15504. (defun org-metaright (&optional arg)
  15505. "Demote subtree or move table column to right.
  15506. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15507. With no specific context, calls the Emacs default `forward-word'.
  15508. See the individual commands for more information."
  15509. (interactive "P")
  15510. (cond
  15511. ((run-hook-with-args-until-success 'org-metaright-hook))
  15512. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15513. ((or (org-on-heading-p)
  15514. (and (org-region-active-p)
  15515. (save-excursion
  15516. (goto-char (region-beginning))
  15517. (org-on-heading-p))))
  15518. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15519. (call-interactively 'org-do-demote))
  15520. ((or (org-at-item-p)
  15521. (and (org-region-active-p)
  15522. (save-excursion
  15523. (goto-char (region-beginning))
  15524. (org-at-item-p))))
  15525. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15526. (call-interactively 'org-indent-item))
  15527. (t (call-interactively 'forward-word))))
  15528. (defun org-check-for-hidden (what)
  15529. "Check if there are hidden headlines/items in the current visual line.
  15530. WHAT can be either `headlines' or `items'. If the current line is
  15531. an outline or item heading and it has a folded subtree below it,
  15532. this function returns t, nil otherwise."
  15533. (let ((re (cond
  15534. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15535. ((eq what 'items) (concat "^" (org-item-re t)))
  15536. (t (error "This should not happen"))))
  15537. beg end)
  15538. (save-excursion
  15539. (catch 'exit
  15540. (unless (org-region-active-p)
  15541. (setq beg (point-at-bol))
  15542. (beginning-of-line 2)
  15543. (while (and (not (eobp)) ;; this is like `next-line'
  15544. (get-char-property (1- (point)) 'invisible))
  15545. (beginning-of-line 2))
  15546. (setq end (point))
  15547. (goto-char beg)
  15548. (goto-char (point-at-eol))
  15549. (setq end (max end (point)))
  15550. (while (re-search-forward re end t)
  15551. (if (get-char-property (match-beginning 0) 'invisible)
  15552. (throw 'exit t))))
  15553. nil))))
  15554. (defun org-metaup (&optional arg)
  15555. "Move subtree up or move table row up.
  15556. Calls `org-move-subtree-up' or `org-table-move-row' or
  15557. `org-move-item-up', depending on context. See the individual commands
  15558. for more information."
  15559. (interactive "P")
  15560. (cond
  15561. ((run-hook-with-args-until-success 'org-metaup-hook))
  15562. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15563. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15564. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15565. (t (transpose-lines 1) (beginning-of-line -1))))
  15566. (defun org-metadown (&optional arg)
  15567. "Move subtree down or move table row down.
  15568. Calls `org-move-subtree-down' or `org-table-move-row' or
  15569. `org-move-item-down', depending on context. See the individual
  15570. commands for more information."
  15571. (interactive "P")
  15572. (cond
  15573. ((run-hook-with-args-until-success 'org-metadown-hook))
  15574. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15575. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15576. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15577. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15578. (defun org-shiftup (&optional arg)
  15579. "Increase item in timestamp or increase priority of current headline.
  15580. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15581. depending on context. See the individual commands for more information."
  15582. (interactive "P")
  15583. (cond
  15584. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15585. ((and org-support-shift-select (org-region-active-p))
  15586. (org-call-for-shift-select 'previous-line))
  15587. ((org-at-timestamp-p t)
  15588. (call-interactively (if org-edit-timestamp-down-means-later
  15589. 'org-timestamp-down 'org-timestamp-up)))
  15590. ((and (not (eq org-support-shift-select 'always))
  15591. org-enable-priority-commands
  15592. (org-on-heading-p))
  15593. (call-interactively 'org-priority-up))
  15594. ((and (not org-support-shift-select) (org-at-item-p))
  15595. (call-interactively 'org-previous-item))
  15596. ((org-clocktable-try-shift 'up arg))
  15597. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15598. (org-support-shift-select
  15599. (org-call-for-shift-select 'previous-line))
  15600. (t (org-shiftselect-error))))
  15601. (defun org-shiftdown (&optional arg)
  15602. "Decrease item in timestamp or decrease priority of current headline.
  15603. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15604. depending on context. See the individual commands for more information."
  15605. (interactive "P")
  15606. (cond
  15607. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15608. ((and org-support-shift-select (org-region-active-p))
  15609. (org-call-for-shift-select 'next-line))
  15610. ((org-at-timestamp-p t)
  15611. (call-interactively (if org-edit-timestamp-down-means-later
  15612. 'org-timestamp-up 'org-timestamp-down)))
  15613. ((and (not (eq org-support-shift-select 'always))
  15614. org-enable-priority-commands
  15615. (org-on-heading-p))
  15616. (call-interactively 'org-priority-down))
  15617. ((and (not org-support-shift-select) (org-at-item-p))
  15618. (call-interactively 'org-next-item))
  15619. ((org-clocktable-try-shift 'down arg))
  15620. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15621. (org-support-shift-select
  15622. (org-call-for-shift-select 'next-line))
  15623. (t (org-shiftselect-error))))
  15624. (defun org-shiftright (&optional arg)
  15625. "Cycle the thing at point or in the current line, depending on context.
  15626. Depending on context, this does one of the following:
  15627. - switch a timestamp at point one day into the future
  15628. - on a headline, switch to the next TODO keyword.
  15629. - on an item, switch entire list to the next bullet type
  15630. - on a property line, switch to the next allowed value
  15631. - on a clocktable definition line, move time block into the future"
  15632. (interactive "P")
  15633. (cond
  15634. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15635. ((and org-support-shift-select (org-region-active-p))
  15636. (org-call-for-shift-select 'forward-char))
  15637. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15638. ((and (not (eq org-support-shift-select 'always))
  15639. (org-on-heading-p))
  15640. (let ((org-inhibit-logging
  15641. (not org-treat-S-cursor-todo-selection-as-state-change))
  15642. (org-inhibit-blocking
  15643. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15644. (org-call-with-arg 'org-todo 'right)))
  15645. ((or (and org-support-shift-select
  15646. (not (eq org-support-shift-select 'always))
  15647. (org-at-item-bullet-p))
  15648. (and (not org-support-shift-select) (org-at-item-p)))
  15649. (org-call-with-arg 'org-cycle-list-bullet nil))
  15650. ((and (not (eq org-support-shift-select 'always))
  15651. (org-at-property-p))
  15652. (call-interactively 'org-property-next-allowed-value))
  15653. ((org-clocktable-try-shift 'right arg))
  15654. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15655. (org-support-shift-select
  15656. (org-call-for-shift-select 'forward-char))
  15657. (t (org-shiftselect-error))))
  15658. (defun org-shiftleft (&optional arg)
  15659. "Cycle the thing at point or in the current line, depending on context.
  15660. Depending on context, this does one of the following:
  15661. - switch a timestamp at point one day into the past
  15662. - on a headline, switch to the previous TODO keyword.
  15663. - on an item, switch entire list to the previous bullet type
  15664. - on a property line, switch to the previous allowed value
  15665. - on a clocktable definition line, move time block into the past"
  15666. (interactive "P")
  15667. (cond
  15668. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15669. ((and org-support-shift-select (org-region-active-p))
  15670. (org-call-for-shift-select 'backward-char))
  15671. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15672. ((and (not (eq org-support-shift-select 'always))
  15673. (org-on-heading-p))
  15674. (let ((org-inhibit-logging
  15675. (not org-treat-S-cursor-todo-selection-as-state-change))
  15676. (org-inhibit-blocking
  15677. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15678. (org-call-with-arg 'org-todo 'left)))
  15679. ((or (and org-support-shift-select
  15680. (not (eq org-support-shift-select 'always))
  15681. (org-at-item-bullet-p))
  15682. (and (not org-support-shift-select) (org-at-item-p)))
  15683. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15684. ((and (not (eq org-support-shift-select 'always))
  15685. (org-at-property-p))
  15686. (call-interactively 'org-property-previous-allowed-value))
  15687. ((org-clocktable-try-shift 'left arg))
  15688. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15689. (org-support-shift-select
  15690. (org-call-for-shift-select 'backward-char))
  15691. (t (org-shiftselect-error))))
  15692. (defun org-shiftcontrolright ()
  15693. "Switch to next TODO set."
  15694. (interactive)
  15695. (cond
  15696. ((and org-support-shift-select (org-region-active-p))
  15697. (org-call-for-shift-select 'forward-word))
  15698. ((and (not (eq org-support-shift-select 'always))
  15699. (org-on-heading-p))
  15700. (org-call-with-arg 'org-todo 'nextset))
  15701. (org-support-shift-select
  15702. (org-call-for-shift-select 'forward-word))
  15703. (t (org-shiftselect-error))))
  15704. (defun org-shiftcontrolleft ()
  15705. "Switch to previous TODO set."
  15706. (interactive)
  15707. (cond
  15708. ((and org-support-shift-select (org-region-active-p))
  15709. (org-call-for-shift-select 'backward-word))
  15710. ((and (not (eq org-support-shift-select 'always))
  15711. (org-on-heading-p))
  15712. (org-call-with-arg 'org-todo 'previousset))
  15713. (org-support-shift-select
  15714. (org-call-for-shift-select 'backward-word))
  15715. (t (org-shiftselect-error))))
  15716. (defun org-ctrl-c-ret ()
  15717. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15718. (interactive)
  15719. (cond
  15720. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15721. (t (call-interactively 'org-insert-heading))))
  15722. (defun org-copy-special ()
  15723. "Copy region in table or copy current subtree.
  15724. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15725. See the individual commands for more information."
  15726. (interactive)
  15727. (call-interactively
  15728. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15729. (defun org-cut-special ()
  15730. "Cut region in table or cut current subtree.
  15731. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15732. See the individual commands for more information."
  15733. (interactive)
  15734. (call-interactively
  15735. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15736. (defun org-paste-special (arg)
  15737. "Paste rectangular region into table, or past subtree relative to level.
  15738. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15739. See the individual commands for more information."
  15740. (interactive "P")
  15741. (if (org-at-table-p)
  15742. (org-table-paste-rectangle)
  15743. (org-paste-subtree arg)))
  15744. (defun org-edit-special (&optional arg)
  15745. "Call a special editor for the stuff at point.
  15746. When at a table, call the formula editor with `org-table-edit-formulas'.
  15747. When at the first line of an src example, call `org-edit-src-code'.
  15748. When in an #+include line, visit the include file. Otherwise call
  15749. `ffap' to visit the file at point."
  15750. (interactive)
  15751. ;; possibly prep session before editing source
  15752. (when arg
  15753. (let* ((info (org-babel-get-src-block-info))
  15754. (lang (nth 0 info))
  15755. (params (nth 2 info))
  15756. (session (cdr (assoc :session params))))
  15757. (when (and info session) ;; we are in a source-code block with a session
  15758. (funcall
  15759. (intern (concat "org-babel-prep-session:" lang)) session params))))
  15760. (cond ;; proceed with `org-edit-special'
  15761. ((save-excursion
  15762. (beginning-of-line 1)
  15763. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15764. (find-file (org-trim (match-string 1))))
  15765. ((org-edit-src-code))
  15766. ((org-edit-fixed-width-region))
  15767. ((org-at-table.el-p)
  15768. (org-edit-src-code))
  15769. ((or (org-at-table-p)
  15770. (save-excursion
  15771. (beginning-of-line 1)
  15772. (looking-at "[ \t]*#\\+TBLFM:")))
  15773. (call-interactively 'org-table-edit-formulas))
  15774. (t (call-interactively 'ffap))))
  15775. (defun org-ctrl-c-ctrl-c (&optional arg)
  15776. "Set tags in headline, or update according to changed information at point.
  15777. This command does many different things, depending on context:
  15778. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15779. this is what we do.
  15780. - If the cursor is on a statistics cookie, update it.
  15781. - If the cursor is in a headline, prompt for tags and insert them
  15782. into the current line, aligned to `org-tags-column'. When called
  15783. with prefix arg, realign all tags in the current buffer.
  15784. - If the cursor is in one of the special #+KEYWORD lines, this
  15785. triggers scanning the buffer for these lines and updating the
  15786. information.
  15787. - If the cursor is inside a table, realign the table. This command
  15788. works even if the automatic table editor has been turned off.
  15789. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15790. the entire table.
  15791. - If the cursor is at a footnote reference or definition, jump to
  15792. the corresponding definition or references, respectively.
  15793. - If the cursor is a the beginning of a dynamic block, update it.
  15794. - If the current buffer is a capture buffer, close note and file it.
  15795. - If the cursor is on a <<<target>>>, update radio targets and
  15796. corresponding links in this buffer.
  15797. - If the cursor is on a numbered item in a plain list, renumber the
  15798. ordered list.
  15799. - If the cursor is on a checkbox, toggle it.
  15800. - If the cursor is on a code block, evaluate it. The variable
  15801. `org-confirm-babel-evaluate' can be used to control prompting
  15802. before code block evaluation, by default every code block
  15803. evaluation requires confirmation. Code block evaluation can be
  15804. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  15805. (interactive "P")
  15806. (let ((org-enable-table-editor t))
  15807. (cond
  15808. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15809. org-occur-highlights
  15810. org-latex-fragment-image-overlays)
  15811. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15812. (org-remove-occur-highlights)
  15813. (org-remove-latex-fragment-image-overlays)
  15814. (message "Temporary highlights/overlays removed from current buffer"))
  15815. ((and (local-variable-p 'org-finish-function (current-buffer))
  15816. (fboundp org-finish-function))
  15817. (funcall org-finish-function))
  15818. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15819. ((or (looking-at org-property-start-re)
  15820. (org-at-property-p))
  15821. (call-interactively 'org-property-action))
  15822. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15823. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15824. (or (org-on-heading-p) (org-at-item-p)))
  15825. (call-interactively 'org-update-statistics-cookies))
  15826. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15827. ((org-at-table.el-p)
  15828. (message "Use C-c ' to edit table.el tables"))
  15829. ((org-at-table-p)
  15830. (org-table-maybe-eval-formula)
  15831. (if arg
  15832. (call-interactively 'org-table-recalculate)
  15833. (org-table-maybe-recalculate-line))
  15834. (call-interactively 'org-table-align)
  15835. (orgtbl-send-table 'maybe))
  15836. ((or (org-footnote-at-reference-p)
  15837. (org-footnote-at-definition-p))
  15838. (call-interactively 'org-footnote-action))
  15839. ((org-at-item-checkbox-p)
  15840. (call-interactively 'org-list-repair)
  15841. (call-interactively 'org-toggle-checkbox)
  15842. (org-list-send-list 'maybe))
  15843. ((org-at-item-p)
  15844. (call-interactively 'org-list-repair)
  15845. (when arg (call-interactively 'org-toggle-checkbox))
  15846. (org-list-send-list 'maybe))
  15847. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15848. ;; Dynamic block
  15849. (beginning-of-line 1)
  15850. (save-excursion (org-update-dblock)))
  15851. ((save-excursion
  15852. (beginning-of-line 1)
  15853. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15854. (cond
  15855. ((equal (match-string 1) "TBLFM")
  15856. ;; Recalculate the table before this line
  15857. (save-excursion
  15858. (beginning-of-line 1)
  15859. (skip-chars-backward " \r\n\t")
  15860. (if (org-at-table-p)
  15861. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15862. (t
  15863. (let ((org-inhibit-startup-visibility-stuff t)
  15864. (org-startup-align-all-tables nil))
  15865. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15866. (message "Local setup has been refreshed"))))
  15867. ((org-clock-update-time-maybe))
  15868. (t (error "C-c C-c can do nothing useful at this location")))))
  15869. (defun org-mode-restart ()
  15870. "Restart Org-mode, to scan again for special lines.
  15871. Also updates the keyword regular expressions."
  15872. (interactive)
  15873. (org-mode)
  15874. (message "Org-mode restarted"))
  15875. (defun org-kill-note-or-show-branches ()
  15876. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15877. (interactive)
  15878. (if (not org-finish-function)
  15879. (progn
  15880. (hide-subtree)
  15881. (call-interactively 'show-branches))
  15882. (let ((org-note-abort t))
  15883. (funcall org-finish-function))))
  15884. (defun org-return (&optional indent)
  15885. "Goto next table row or insert a newline.
  15886. Calls `org-table-next-row' or `newline', depending on context.
  15887. See the individual commands for more information."
  15888. (interactive)
  15889. (cond
  15890. ((bobp) (if indent (newline-and-indent) (newline)))
  15891. ((org-at-table-p)
  15892. (org-table-justify-field-maybe)
  15893. (call-interactively 'org-table-next-row))
  15894. ((and org-return-follows-link
  15895. (eq (get-text-property (point) 'face) 'org-link))
  15896. (call-interactively 'org-open-at-point))
  15897. ((and (org-at-heading-p)
  15898. (looking-at
  15899. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  15900. (org-show-entry)
  15901. (end-of-line 1)
  15902. (newline))
  15903. (t (if indent (newline-and-indent) (newline)))))
  15904. (defun org-return-indent ()
  15905. "Goto next table row or insert a newline and indent.
  15906. Calls `org-table-next-row' or `newline-and-indent', depending on
  15907. context. See the individual commands for more information."
  15908. (interactive)
  15909. (org-return t))
  15910. (defun org-ctrl-c-star ()
  15911. "Compute table, or change heading status of lines.
  15912. Calls `org-table-recalculate' or `org-toggle-heading',
  15913. depending on context."
  15914. (interactive)
  15915. (cond
  15916. ((org-at-table-p)
  15917. (call-interactively 'org-table-recalculate))
  15918. (t
  15919. ;; Convert all lines in region to list items
  15920. (call-interactively 'org-toggle-heading))))
  15921. (defun org-ctrl-c-minus ()
  15922. "Insert separator line in table or modify bullet status of line.
  15923. Also turns a plain line or a region of lines into list items.
  15924. Calls `org-table-insert-hline', `org-toggle-item', or
  15925. `org-cycle-list-bullet', depending on context."
  15926. (interactive)
  15927. (cond
  15928. ((org-at-table-p)
  15929. (call-interactively 'org-table-insert-hline))
  15930. ((org-region-active-p)
  15931. (call-interactively 'org-toggle-item))
  15932. ((org-in-item-p)
  15933. (call-interactively 'org-cycle-list-bullet))
  15934. (t
  15935. (call-interactively 'org-toggle-item))))
  15936. (defun org-toggle-item ()
  15937. "Convert headings or normal lines to items, items to normal lines.
  15938. If there is no active region, only the current line is considered.
  15939. If the first line in the region is a headline, convert all headlines to items.
  15940. If the first line in the region is an item, convert all items to normal lines.
  15941. If the first line is normal text, add an item bullet to each line."
  15942. (interactive)
  15943. (let (l2 l beg end)
  15944. (if (org-region-active-p)
  15945. (setq beg (region-beginning) end (region-end))
  15946. (setq beg (point-at-bol)
  15947. end (min (1+ (point-at-eol)) (point-max))))
  15948. (save-excursion
  15949. (goto-char end)
  15950. (setq l2 (org-current-line))
  15951. (goto-char beg)
  15952. (beginning-of-line 1)
  15953. (setq l (1- (org-current-line)))
  15954. (if (org-at-item-p)
  15955. ;; We already have items, de-itemize
  15956. (while (< (setq l (1+ l)) l2)
  15957. (when (org-at-item-p)
  15958. (skip-chars-forward " \t")
  15959. (delete-region (point) (match-end 0)))
  15960. (beginning-of-line 2))
  15961. (if (org-on-heading-p)
  15962. ;; Headings, convert to items
  15963. (while (< (setq l (1+ l)) l2)
  15964. (if (looking-at org-outline-regexp)
  15965. (replace-match (org-list-bullet-string "-") t t))
  15966. (beginning-of-line 2))
  15967. ;; normal lines, turn them into items
  15968. (while (< (setq l (1+ l)) l2)
  15969. (unless (org-at-item-p)
  15970. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15971. (replace-match
  15972. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  15973. (beginning-of-line 2)))))))
  15974. (defun org-toggle-heading (&optional nstars)
  15975. "Convert headings to normal text, or items or text to headings.
  15976. If there is no active region, only the current line is considered.
  15977. If the first line is a heading, remove the stars from all headlines
  15978. in the region.
  15979. If the first line is a plain list item, turn all plain list items
  15980. into headings.
  15981. If the first line is a normal line, turn each and every line in the
  15982. region into a heading.
  15983. When converting a line into a heading, the number of stars is chosen
  15984. such that the lines become children of the current entry. However,
  15985. when a prefix argument is given, its value determines the number of
  15986. stars to add."
  15987. (interactive "P")
  15988. (let (l2 l itemp beg end)
  15989. (if (org-region-active-p)
  15990. (setq beg (region-beginning) end (region-end))
  15991. (setq beg (point-at-bol)
  15992. end (min (1+ (point-at-eol)) (point-max))))
  15993. (save-excursion
  15994. (goto-char end)
  15995. (setq l2 (org-current-line))
  15996. (goto-char beg)
  15997. (beginning-of-line 1)
  15998. (setq l (1- (org-current-line)))
  15999. (if (org-on-heading-p)
  16000. ;; We already have headlines, de-star them
  16001. (while (< (setq l (1+ l)) l2)
  16002. (when (org-on-heading-p t)
  16003. (and (looking-at outline-regexp) (replace-match "")))
  16004. (beginning-of-line 2))
  16005. (setq itemp (org-at-item-p))
  16006. (let* ((stars
  16007. (if nstars
  16008. (make-string (prefix-numeric-value current-prefix-arg)
  16009. ?*)
  16010. (save-excursion
  16011. (if (re-search-backward org-complex-heading-regexp nil t)
  16012. (match-string 1) ""))))
  16013. (add-stars (cond (nstars "")
  16014. ((equal stars "") "*")
  16015. (org-odd-levels-only "**")
  16016. (t "*")))
  16017. (rpl (concat stars add-stars " ")))
  16018. (while (< (setq l (1+ l)) l2)
  16019. (if itemp
  16020. (and (org-at-item-p) (replace-match rpl t t))
  16021. (unless (org-on-heading-p)
  16022. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16023. (replace-match (concat rpl (match-string 2))))))
  16024. (beginning-of-line 2)))))))
  16025. (defun org-meta-return (&optional arg)
  16026. "Insert a new heading or wrap a region in a table.
  16027. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16028. See the individual commands for more information."
  16029. (interactive "P")
  16030. (cond
  16031. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16032. ((org-at-table-p)
  16033. (call-interactively 'org-table-wrap-region))
  16034. (t (call-interactively 'org-insert-heading))))
  16035. ;;; Menu entries
  16036. ;; Define the Org-mode menus
  16037. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16038. '("Tbl"
  16039. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16040. ["Next Field" org-cycle (org-at-table-p)]
  16041. ["Previous Field" org-shifttab (org-at-table-p)]
  16042. ["Next Row" org-return (org-at-table-p)]
  16043. "--"
  16044. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16045. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16046. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16047. "--"
  16048. ("Column"
  16049. ["Move Column Left" org-metaleft (org-at-table-p)]
  16050. ["Move Column Right" org-metaright (org-at-table-p)]
  16051. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16052. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16053. ("Row"
  16054. ["Move Row Up" org-metaup (org-at-table-p)]
  16055. ["Move Row Down" org-metadown (org-at-table-p)]
  16056. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16057. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16058. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16059. "--"
  16060. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16061. ("Rectangle"
  16062. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16063. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16064. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16065. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16066. "--"
  16067. ("Calculate"
  16068. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16069. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16070. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16071. "--"
  16072. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16073. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16074. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16075. "--"
  16076. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16077. "--"
  16078. ["Sum Column/Rectangle" org-table-sum
  16079. (or (org-at-table-p) (org-region-active-p))]
  16080. ["Which Column?" org-table-current-column (org-at-table-p)])
  16081. ["Debug Formulas"
  16082. org-table-toggle-formula-debugger
  16083. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16084. ["Show Col/Row Numbers"
  16085. org-table-toggle-coordinate-overlays
  16086. :style toggle
  16087. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16088. "--"
  16089. ["Create" org-table-create (and (not (org-at-table-p))
  16090. org-enable-table-editor)]
  16091. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16092. ["Import from File" org-table-import (not (org-at-table-p))]
  16093. ["Export to File" org-table-export (org-at-table-p)]
  16094. "--"
  16095. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16096. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16097. '("Org"
  16098. ("Show/Hide"
  16099. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16100. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16101. ["Sparse Tree..." org-sparse-tree t]
  16102. ["Reveal Context" org-reveal t]
  16103. ["Show All" show-all t]
  16104. "--"
  16105. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16106. "--"
  16107. ["New Heading" org-insert-heading t]
  16108. ("Navigate Headings"
  16109. ["Up" outline-up-heading t]
  16110. ["Next" outline-next-visible-heading t]
  16111. ["Previous" outline-previous-visible-heading t]
  16112. ["Next Same Level" outline-forward-same-level t]
  16113. ["Previous Same Level" outline-backward-same-level t]
  16114. "--"
  16115. ["Jump" org-goto t])
  16116. ("Edit Structure"
  16117. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16118. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16119. "--"
  16120. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16121. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16122. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16123. "--"
  16124. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16125. "--"
  16126. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16127. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16128. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16129. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16130. "--"
  16131. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16132. "--"
  16133. ["Convert to odd levels" org-convert-to-odd-levels t]
  16134. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16135. ("Editing"
  16136. ["Emphasis..." org-emphasize t]
  16137. ["Edit Source Example" org-edit-special t]
  16138. "--"
  16139. ["Footnote new/jump" org-footnote-action t]
  16140. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16141. ("Archive"
  16142. ["Archive (default method)" org-archive-subtree-default t]
  16143. "--"
  16144. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16145. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16146. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16147. )
  16148. "--"
  16149. ("Hyperlinks"
  16150. ["Store Link (Global)" org-store-link t]
  16151. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16152. ["Insert Link" org-insert-link t]
  16153. ["Follow Link" org-open-at-point t]
  16154. "--"
  16155. ["Next link" org-next-link t]
  16156. ["Previous link" org-previous-link t]
  16157. "--"
  16158. ["Descriptive Links"
  16159. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16160. :style radio
  16161. :selected (member '(org-link) buffer-invisibility-spec)]
  16162. ["Literal Links"
  16163. (progn
  16164. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16165. :style radio
  16166. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16167. "--"
  16168. ("TODO Lists"
  16169. ["TODO/DONE/-" org-todo t]
  16170. ("Select keyword"
  16171. ["Next keyword" org-shiftright (org-on-heading-p)]
  16172. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16173. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16174. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16175. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16176. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16177. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16178. "--"
  16179. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16180. :selected org-enforce-todo-dependencies :style toggle :active t]
  16181. "Settings for tree at point"
  16182. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16183. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  16184. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16185. ["Do Children parallel" org-toggle-ordered-property :style radio
  16186. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  16187. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16188. "--"
  16189. ["Set Priority" org-priority t]
  16190. ["Priority Up" org-shiftup t]
  16191. ["Priority Down" org-shiftdown t]
  16192. "--"
  16193. ["Get news from all feeds" org-feed-update-all t]
  16194. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16195. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16196. ("TAGS and Properties"
  16197. ["Set Tags" org-set-tags-command t]
  16198. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16199. "--"
  16200. ["Set property" org-set-property t]
  16201. ["Column view of properties" org-columns t]
  16202. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16203. ("Dates and Scheduling"
  16204. ["Timestamp" org-time-stamp t]
  16205. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16206. ("Change Date"
  16207. ["1 Day Later" org-shiftright t]
  16208. ["1 Day Earlier" org-shiftleft t]
  16209. ["1 ... Later" org-shiftup t]
  16210. ["1 ... Earlier" org-shiftdown t])
  16211. ["Compute Time Range" org-evaluate-time-range t]
  16212. ["Schedule Item" org-schedule t]
  16213. ["Deadline" org-deadline t]
  16214. "--"
  16215. ["Custom time format" org-toggle-time-stamp-overlays
  16216. :style radio :selected org-display-custom-times]
  16217. "--"
  16218. ["Goto Calendar" org-goto-calendar t]
  16219. ["Date from Calendar" org-date-from-calendar t]
  16220. "--"
  16221. ["Start/Restart Timer" org-timer-start t]
  16222. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16223. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16224. ["Insert Timer String" org-timer t]
  16225. ["Insert Timer Item" org-timer-item t])
  16226. ("Logging work"
  16227. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16228. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16229. ["Clock out" org-clock-out t]
  16230. ["Clock cancel" org-clock-cancel t]
  16231. "--"
  16232. ["Mark as default task" org-clock-mark-default-task t]
  16233. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16234. ["Goto running clock" org-clock-goto t]
  16235. "--"
  16236. ["Display times" org-clock-display t]
  16237. ["Create clock table" org-clock-report t]
  16238. "--"
  16239. ["Record DONE time"
  16240. (progn (setq org-log-done (not org-log-done))
  16241. (message "Switching to %s will %s record a timestamp"
  16242. (car org-done-keywords)
  16243. (if org-log-done "automatically" "not")))
  16244. :style toggle :selected org-log-done])
  16245. "--"
  16246. ["Agenda Command..." org-agenda t]
  16247. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16248. ("File List for Agenda")
  16249. ("Special views current file"
  16250. ["TODO Tree" org-show-todo-tree t]
  16251. ["Check Deadlines" org-check-deadlines t]
  16252. ["Timeline" org-timeline t]
  16253. ["Tags/Property tree" org-match-sparse-tree t])
  16254. "--"
  16255. ["Export/Publish..." org-export t]
  16256. ("LaTeX"
  16257. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16258. :selected org-cdlatex-mode]
  16259. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16260. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16261. ["Modify math symbol" org-cdlatex-math-modify
  16262. (org-inside-LaTeX-fragment-p)]
  16263. ["Insert citation" org-reftex-citation t]
  16264. "--"
  16265. ["Template for BEAMER" org-insert-beamer-options-template t])
  16266. "--"
  16267. ("MobileOrg"
  16268. ["Push Files and Views" org-mobile-push t]
  16269. ["Get Captured and Flagged" org-mobile-pull t]
  16270. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16271. "--"
  16272. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16273. "--"
  16274. ("Documentation"
  16275. ["Show Version" org-version t]
  16276. ["Info Documentation" org-info t])
  16277. ("Customize"
  16278. ["Browse Org Group" org-customize t]
  16279. "--"
  16280. ["Expand This Menu" org-create-customize-menu
  16281. (fboundp 'customize-menu-create)])
  16282. ["Send bug report" org-submit-bug-report t]
  16283. "--"
  16284. ("Refresh/Reload"
  16285. ["Refresh setup current buffer" org-mode-restart t]
  16286. ["Reload Org (after update)" org-reload t]
  16287. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16288. ))
  16289. (defun org-info (&optional node)
  16290. "Read documentation for Org-mode in the info system.
  16291. With optional NODE, go directly to that node."
  16292. (interactive)
  16293. (info (format "(org)%s" (or node ""))))
  16294. ;;;###autoload
  16295. (defun org-submit-bug-report ()
  16296. "Submit a bug report on Org-mode via mail.
  16297. Don't hesitate to report any problems or inaccurate documentation.
  16298. If you don't have setup sending mail from (X)Emacs, please copy the
  16299. output buffer into your mail program, as it gives us important
  16300. information about your Org-mode version and configuration."
  16301. (interactive)
  16302. (require 'reporter)
  16303. (org-load-modules-maybe)
  16304. (org-require-autoloaded-modules)
  16305. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16306. (reporter-submit-bug-report
  16307. "emacs-orgmode@gnu.org"
  16308. (org-version)
  16309. (let (list)
  16310. (save-window-excursion
  16311. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16312. (delete-other-windows)
  16313. (erase-buffer)
  16314. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16315. We would like to add your full Org-mode and Outline configuration to the
  16316. bug report. This greatly simplifies the work of the maintainer and
  16317. other experts on the mailing list.
  16318. HOWEVER, some variables you have customized may contain private
  16319. information. The names of customers, colleagues, or friends, might
  16320. appear in the form of file names, tags, todo states, or search strings.
  16321. If you answer yes to the prompt, you might want to check and remove
  16322. such private information before sending the email.")
  16323. (add-text-properties (point-min) (point-max) '(face org-warning))
  16324. (when (yes-or-no-p "Include your Org-mode configuration ")
  16325. (mapatoms
  16326. (lambda (v)
  16327. (and (boundp v)
  16328. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16329. (or (and (symbol-value v)
  16330. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16331. (and
  16332. (get v 'custom-type) (get v 'standard-value)
  16333. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16334. (push v list)))))
  16335. (kill-buffer (get-buffer "*Warn about privacy*"))
  16336. list))
  16337. nil nil
  16338. "Remember to cover the basics, that is, what you expected to happen and
  16339. what in fact did happen. You don't know how to make a good report? See
  16340. http://orgmode.org/manual/Feedback.html#Feedback
  16341. Your bug report will be posted to the Org-mode mailing list.
  16342. ------------------------------------------------------------------------")
  16343. (save-excursion
  16344. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16345. (replace-match "\\1Bug: \\3 [\\2]")))))
  16346. (defun org-install-agenda-files-menu ()
  16347. (let ((bl (buffer-list)))
  16348. (save-excursion
  16349. (while bl
  16350. (set-buffer (pop bl))
  16351. (if (org-mode-p) (setq bl nil)))
  16352. (when (org-mode-p)
  16353. (easy-menu-change
  16354. '("Org") "File List for Agenda"
  16355. (append
  16356. (list
  16357. ["Edit File List" (org-edit-agenda-file-list) t]
  16358. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16359. ["Remove Current File from List" org-remove-file t]
  16360. ["Cycle through agenda files" org-cycle-agenda-files t]
  16361. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16362. "--")
  16363. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16364. ;;;; Documentation
  16365. ;;;###autoload
  16366. (defun org-require-autoloaded-modules ()
  16367. (interactive)
  16368. (mapc 'require
  16369. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16370. org-docbook org-exp org-html org-icalendar
  16371. org-id org-latex
  16372. org-publish org-remember org-table
  16373. org-timer org-xoxo)))
  16374. ;;;###autoload
  16375. (defun org-reload (&optional uncompiled)
  16376. "Reload all org lisp files.
  16377. With prefix arg UNCOMPILED, load the uncompiled versions."
  16378. (interactive "P")
  16379. (require 'find-func)
  16380. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16381. (dir-org (file-name-directory (org-find-library-name "org")))
  16382. (dir-org-contrib (ignore-errors
  16383. (file-name-directory
  16384. (org-find-library-name "org-contribdir"))))
  16385. (babel-files
  16386. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16387. (append (list nil "comint" "eval" "exp" "keys"
  16388. "lob" "ref" "table" "tangle")
  16389. (delq nil
  16390. (mapcar
  16391. (lambda (lang)
  16392. (when (cdr lang) (symbol-name (car lang))))
  16393. org-babel-load-languages)))))
  16394. (files
  16395. (append (directory-files dir-org t file-re)
  16396. babel-files
  16397. (and dir-org-contrib
  16398. (directory-files dir-org-contrib t file-re))))
  16399. (remove-re (concat (if (featurep 'xemacs)
  16400. "org-colview" "org-colview-xemacs")
  16401. "\\'")))
  16402. (setq files (mapcar 'file-name-sans-extension files))
  16403. (setq files (mapcar
  16404. (lambda (x) (if (string-match remove-re x) nil x))
  16405. files))
  16406. (setq files (delq nil files))
  16407. (mapc
  16408. (lambda (f)
  16409. (when (featurep (intern (file-name-nondirectory f)))
  16410. (if (and (not uncompiled)
  16411. (file-exists-p (concat f ".elc")))
  16412. (load (concat f ".elc") nil nil t)
  16413. (load (concat f ".el") nil nil t))))
  16414. files))
  16415. (org-version))
  16416. ;;;###autoload
  16417. (defun org-customize ()
  16418. "Call the customize function with org as argument."
  16419. (interactive)
  16420. (org-load-modules-maybe)
  16421. (org-require-autoloaded-modules)
  16422. (customize-browse 'org))
  16423. (defun org-create-customize-menu ()
  16424. "Create a full customization menu for Org-mode, insert it into the menu."
  16425. (interactive)
  16426. (org-load-modules-maybe)
  16427. (org-require-autoloaded-modules)
  16428. (if (fboundp 'customize-menu-create)
  16429. (progn
  16430. (easy-menu-change
  16431. '("Org") "Customize"
  16432. `(["Browse Org group" org-customize t]
  16433. "--"
  16434. ,(customize-menu-create 'org)
  16435. ["Set" Custom-set t]
  16436. ["Save" Custom-save t]
  16437. ["Reset to Current" Custom-reset-current t]
  16438. ["Reset to Saved" Custom-reset-saved t]
  16439. ["Reset to Standard Settings" Custom-reset-standard t]))
  16440. (message "\"Org\"-menu now contains full customization menu"))
  16441. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16442. ;;;; Miscellaneous stuff
  16443. ;;; Generally useful functions
  16444. (defun org-get-at-bol (property)
  16445. "Get text property PROPERTY at beginning of line."
  16446. (get-text-property (point-at-bol) property))
  16447. (defun org-find-text-property-in-string (prop s)
  16448. "Return the first non-nil value of property PROP in string S."
  16449. (or (get-text-property 0 prop s)
  16450. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16451. prop s)))
  16452. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16453. "Display the given MESSAGE as a warning."
  16454. (if (fboundp 'display-warning)
  16455. (display-warning 'org message
  16456. (if (featurep 'xemacs) 'warning :warning))
  16457. (let ((buf (get-buffer-create "*Org warnings*")))
  16458. (with-current-buffer buf
  16459. (goto-char (point-max))
  16460. (insert "Warning (Org): " message)
  16461. (unless (bolp)
  16462. (newline)))
  16463. (display-buffer buf)
  16464. (sit-for 0))))
  16465. (defun org-in-commented-line ()
  16466. "Is point in a line starting with `#'?"
  16467. (equal (char-after (point-at-bol)) ?#))
  16468. (defun org-in-indented-comment-line ()
  16469. "Is point in a line starting with `#' after some white space?"
  16470. (save-excursion
  16471. (save-match-data
  16472. (goto-char (point-at-bol))
  16473. (looking-at "[ \t]*#"))))
  16474. (defun org-in-verbatim-emphasis ()
  16475. (save-match-data
  16476. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16477. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16478. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16479. (if (and marker (marker-buffer marker)
  16480. (buffer-live-p (marker-buffer marker)))
  16481. (progn
  16482. (switch-to-buffer (marker-buffer marker))
  16483. (if (or (> marker (point-max)) (< marker (point-min)))
  16484. (widen))
  16485. (goto-char marker)
  16486. (org-show-context 'org-goto))
  16487. (if bookmark
  16488. (bookmark-jump bookmark)
  16489. (error "Cannot find location"))))
  16490. (defun org-quote-csv-field (s)
  16491. "Quote field for inclusion in CSV material."
  16492. (if (string-match "[\",]" s)
  16493. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16494. s))
  16495. (defun org-force-self-insert (N)
  16496. "Needed to enforce self-insert under remapping."
  16497. (interactive "p")
  16498. (self-insert-command N))
  16499. (defun org-string-width (s)
  16500. "Compute width of string, ignoring invisible characters.
  16501. This ignores character with invisibility property `org-link', and also
  16502. characters with property `org-cwidth', because these will become invisible
  16503. upon the next fontification round."
  16504. (let (b l)
  16505. (when (or (eq t buffer-invisibility-spec)
  16506. (assq 'org-link buffer-invisibility-spec))
  16507. (while (setq b (text-property-any 0 (length s)
  16508. 'invisible 'org-link s))
  16509. (setq s (concat (substring s 0 b)
  16510. (substring s (or (next-single-property-change
  16511. b 'invisible s) (length s)))))))
  16512. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16513. (setq s (concat (substring s 0 b)
  16514. (substring s (or (next-single-property-change
  16515. b 'org-cwidth s) (length s))))))
  16516. (setq l (string-width s) b -1)
  16517. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16518. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16519. l))
  16520. (defun org-shorten-string (s maxlength)
  16521. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  16522. If the string is shorter or has length MAXLENGTH, just return the
  16523. original string. If it is longer, the functions finds a space in the
  16524. string, breaks this string off at that locations and adds three dots
  16525. as ellipsis. Including the ellipsis, the string will not be longer
  16526. than MAXLENGTH. If finding a good breaking point in the string does
  16527. not work, the string is just chopped off in the middle of a word
  16528. if necessary."
  16529. (if (<= (length s) maxlength)
  16530. s
  16531. (let* ((n (max (- maxlength 4) 1))
  16532. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  16533. (if (string-match re s)
  16534. (concat (match-string 1 s) "...")
  16535. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  16536. (defun org-get-indentation (&optional line)
  16537. "Get the indentation of the current line, interpreting tabs.
  16538. When LINE is given, assume it represents a line and compute its indentation."
  16539. (if line
  16540. (if (string-match "^ *" (org-remove-tabs line))
  16541. (match-end 0))
  16542. (save-excursion
  16543. (beginning-of-line 1)
  16544. (skip-chars-forward " \t")
  16545. (current-column))))
  16546. (defun org-remove-tabs (s &optional width)
  16547. "Replace tabulators in S with spaces.
  16548. Assumes that s is a single line, starting in column 0."
  16549. (setq width (or width tab-width))
  16550. (while (string-match "\t" s)
  16551. (setq s (replace-match
  16552. (make-string
  16553. (- (* width (/ (+ (match-beginning 0) width) width))
  16554. (match-beginning 0)) ?\ )
  16555. t t s)))
  16556. s)
  16557. (defun org-fix-indentation (line ind)
  16558. "Fix indentation in LINE.
  16559. IND is a cons cell with target and minimum indentation.
  16560. If the current indentation in LINE is smaller than the minimum,
  16561. leave it alone. If it is larger than ind, set it to the target."
  16562. (let* ((l (org-remove-tabs line))
  16563. (i (org-get-indentation l))
  16564. (i1 (car ind)) (i2 (cdr ind)))
  16565. (if (>= i i2) (setq l (substring line i2)))
  16566. (if (> i1 0)
  16567. (concat (make-string i1 ?\ ) l)
  16568. l)))
  16569. (defun org-remove-indentation (code &optional n)
  16570. "Remove the maximum common indentation from the lines in CODE.
  16571. N may optionally be the number of spaces to remove."
  16572. (with-temp-buffer
  16573. (insert code)
  16574. (org-do-remove-indentation n)
  16575. (buffer-string)))
  16576. (defun org-do-remove-indentation (&optional n)
  16577. "Remove the maximum common indentation from the buffer."
  16578. (untabify (point-min) (point-max))
  16579. (let ((min 10000) re)
  16580. (if n
  16581. (setq min n)
  16582. (goto-char (point-min))
  16583. (while (re-search-forward "^ *[^ \n]" nil t)
  16584. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16585. (unless (or (= min 0) (= min 10000))
  16586. (setq re (format "^ \\{%d\\}" min))
  16587. (goto-char (point-min))
  16588. (while (re-search-forward re nil t)
  16589. (replace-match "")
  16590. (end-of-line 1))
  16591. min)))
  16592. (defun org-fill-template (template alist)
  16593. "Find each %key of ALIST in TEMPLATE and replace it."
  16594. (let ((case-fold-search nil)
  16595. entry key value)
  16596. (setq alist (sort (copy-sequence alist)
  16597. (lambda (a b) (< (length (car a)) (length (car b))))))
  16598. (while (setq entry (pop alist))
  16599. (setq template
  16600. (replace-regexp-in-string
  16601. (concat "%" (regexp-quote (car entry)))
  16602. (cdr entry) template t t)))
  16603. template))
  16604. (defun org-base-buffer (buffer)
  16605. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16606. (if (not buffer)
  16607. buffer
  16608. (or (buffer-base-buffer buffer)
  16609. buffer)))
  16610. (defun org-trim (s)
  16611. "Remove whitespace at beginning and end of string."
  16612. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16613. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16614. s)
  16615. (defun org-wrap (string &optional width lines)
  16616. "Wrap string to either a number of lines, or a width in characters.
  16617. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16618. that costs. If there is a word longer than WIDTH, the text is actually
  16619. wrapped to the length of that word.
  16620. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16621. many lines, whatever width that takes.
  16622. The return value is a list of lines, without newlines at the end."
  16623. (let* ((words (org-split-string string "[ \t\n]+"))
  16624. (maxword (apply 'max (mapcar 'org-string-width words)))
  16625. w ll)
  16626. (cond (width
  16627. (org-do-wrap words (max maxword width)))
  16628. (lines
  16629. (setq w maxword)
  16630. (setq ll (org-do-wrap words maxword))
  16631. (if (<= (length ll) lines)
  16632. ll
  16633. (setq ll words)
  16634. (while (> (length ll) lines)
  16635. (setq w (1+ w))
  16636. (setq ll (org-do-wrap words w)))
  16637. ll))
  16638. (t (error "Cannot wrap this")))))
  16639. (defun org-do-wrap (words width)
  16640. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16641. (let (lines line)
  16642. (while words
  16643. (setq line (pop words))
  16644. (while (and words (< (+ (length line) (length (car words))) width))
  16645. (setq line (concat line " " (pop words))))
  16646. (setq lines (push line lines)))
  16647. (nreverse lines)))
  16648. (defun org-split-string (string &optional separators)
  16649. "Splits STRING into substrings at SEPARATORS.
  16650. No empty strings are returned if there are matches at the beginning
  16651. and end of string."
  16652. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16653. (start 0)
  16654. notfirst
  16655. (list nil))
  16656. (while (and (string-match rexp string
  16657. (if (and notfirst
  16658. (= start (match-beginning 0))
  16659. (< start (length string)))
  16660. (1+ start) start))
  16661. (< (match-beginning 0) (length string)))
  16662. (setq notfirst t)
  16663. (or (eq (match-beginning 0) 0)
  16664. (and (eq (match-beginning 0) (match-end 0))
  16665. (eq (match-beginning 0) start))
  16666. (setq list
  16667. (cons (substring string start (match-beginning 0))
  16668. list)))
  16669. (setq start (match-end 0)))
  16670. (or (eq start (length string))
  16671. (setq list
  16672. (cons (substring string start)
  16673. list)))
  16674. (nreverse list)))
  16675. (defun org-quote-vert (s)
  16676. "Replace \"|\" with \"\\vert\"."
  16677. (while (string-match "|" s)
  16678. (setq s (replace-match "\\vert" t t s)))
  16679. s)
  16680. (defun org-uuidgen-p (s)
  16681. "Is S an ID created by UUIDGEN?"
  16682. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16683. (defun org-context ()
  16684. "Return a list of contexts of the current cursor position.
  16685. If several contexts apply, all are returned.
  16686. Each context entry is a list with a symbol naming the context, and
  16687. two positions indicating start and end of the context. Possible
  16688. contexts are:
  16689. :headline anywhere in a headline
  16690. :headline-stars on the leading stars in a headline
  16691. :todo-keyword on a TODO keyword (including DONE) in a headline
  16692. :tags on the TAGS in a headline
  16693. :priority on the priority cookie in a headline
  16694. :item on the first line of a plain list item
  16695. :item-bullet on the bullet/number of a plain list item
  16696. :checkbox on the checkbox in a plain list item
  16697. :table in an org-mode table
  16698. :table-special on a special filed in a table
  16699. :table-table in a table.el table
  16700. :link on a hyperlink
  16701. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16702. :target on a <<target>>
  16703. :radio-target on a <<<radio-target>>>
  16704. :latex-fragment on a LaTeX fragment
  16705. :latex-preview on a LaTeX fragment with overlayed preview image
  16706. This function expects the position to be visible because it uses font-lock
  16707. faces as a help to recognize the following contexts: :table-special, :link,
  16708. and :keyword."
  16709. (let* ((f (get-text-property (point) 'face))
  16710. (faces (if (listp f) f (list f)))
  16711. (p (point)) clist o)
  16712. ;; First the large context
  16713. (cond
  16714. ((org-on-heading-p t)
  16715. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16716. (when (progn
  16717. (beginning-of-line 1)
  16718. (looking-at org-todo-line-tags-regexp))
  16719. (push (org-point-in-group p 1 :headline-stars) clist)
  16720. (push (org-point-in-group p 2 :todo-keyword) clist)
  16721. (push (org-point-in-group p 4 :tags) clist))
  16722. (goto-char p)
  16723. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16724. (if (looking-at "\\[#[A-Z0-9]\\]")
  16725. (push (org-point-in-group p 0 :priority) clist)))
  16726. ((org-at-item-p)
  16727. (push (org-point-in-group p 2 :item-bullet) clist)
  16728. (push (list :item (point-at-bol)
  16729. (save-excursion (org-end-of-item) (point)))
  16730. clist)
  16731. (and (org-at-item-checkbox-p)
  16732. (push (org-point-in-group p 0 :checkbox) clist)))
  16733. ((org-at-table-p)
  16734. (push (list :table (org-table-begin) (org-table-end)) clist)
  16735. (if (memq 'org-formula faces)
  16736. (push (list :table-special
  16737. (previous-single-property-change p 'face)
  16738. (next-single-property-change p 'face)) clist)))
  16739. ((org-at-table-p 'any)
  16740. (push (list :table-table) clist)))
  16741. (goto-char p)
  16742. ;; Now the small context
  16743. (cond
  16744. ((org-at-timestamp-p)
  16745. (push (org-point-in-group p 0 :timestamp) clist))
  16746. ((memq 'org-link faces)
  16747. (push (list :link
  16748. (previous-single-property-change p 'face)
  16749. (next-single-property-change p 'face)) clist))
  16750. ((memq 'org-special-keyword faces)
  16751. (push (list :keyword
  16752. (previous-single-property-change p 'face)
  16753. (next-single-property-change p 'face)) clist))
  16754. ((org-on-target-p)
  16755. (push (org-point-in-group p 0 :target) clist)
  16756. (goto-char (1- (match-beginning 0)))
  16757. (if (looking-at org-radio-target-regexp)
  16758. (push (org-point-in-group p 0 :radio-target) clist))
  16759. (goto-char p))
  16760. ((setq o (car (delq nil
  16761. (mapcar
  16762. (lambda (x)
  16763. (if (memq x org-latex-fragment-image-overlays) x))
  16764. (overlays-at (point))))))
  16765. (push (list :latex-fragment
  16766. (overlay-start o) (overlay-end o)) clist)
  16767. (push (list :latex-preview
  16768. (overlay-start o) (overlay-end o)) clist))
  16769. ((org-inside-LaTeX-fragment-p)
  16770. ;; FIXME: positions wrong.
  16771. (push (list :latex-fragment (point) (point)) clist)))
  16772. (setq clist (nreverse (delq nil clist)))
  16773. clist))
  16774. ;; FIXME: Compare with at-regexp-p Do we need both?
  16775. (defun org-in-regexp (re &optional nlines visually)
  16776. "Check if point is inside a match of regexp.
  16777. Normally only the current line is checked, but you can include NLINES extra
  16778. lines both before and after point into the search.
  16779. If VISUALLY is set, require that the cursor is not after the match but
  16780. really on, so that the block visually is on the match."
  16781. (catch 'exit
  16782. (let ((pos (point))
  16783. (eol (point-at-eol (+ 1 (or nlines 0))))
  16784. (inc (if visually 1 0)))
  16785. (save-excursion
  16786. (beginning-of-line (- 1 (or nlines 0)))
  16787. (while (re-search-forward re eol t)
  16788. (if (and (<= (match-beginning 0) pos)
  16789. (>= (+ inc (match-end 0)) pos))
  16790. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16791. (defun org-at-regexp-p (regexp)
  16792. "Is point inside a match of REGEXP in the current line?"
  16793. (catch 'exit
  16794. (save-excursion
  16795. (let ((pos (point)) (end (point-at-eol)))
  16796. (beginning-of-line 1)
  16797. (while (re-search-forward regexp end t)
  16798. (if (and (<= (match-beginning 0) pos)
  16799. (>= (match-end 0) pos))
  16800. (throw 'exit t)))
  16801. nil))))
  16802. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  16803. "Return t if the current point is between matches of START-RE and END-RE.
  16804. This will also return t if point is on one of the two matches or
  16805. in an unfinished block. END-RE can be a string or a form
  16806. returning a string.
  16807. An optional third argument bounds the search for START-RE. It
  16808. defaults to previous heading or `point-min'."
  16809. (let ((pos (point))
  16810. (limit (or bound (save-excursion (outline-previous-heading)))))
  16811. (save-excursion
  16812. ;; we're on a block when point is on start-re...
  16813. (or (org-at-regexp-p start-re)
  16814. ;; ... or start-re can be found above...
  16815. (and (re-search-backward start-re limit t)
  16816. ;; ... but no end-re between start-re and point.
  16817. (not (re-search-forward (eval end-re) pos t)))))))
  16818. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16819. "Call `multi-occur' with buffers for all agenda files."
  16820. (interactive "sOrg-files matching: \np")
  16821. (let* ((files (org-agenda-files))
  16822. (tnames (mapcar 'file-truename files))
  16823. (extra org-agenda-text-search-extra-files)
  16824. f)
  16825. (when (eq (car extra) 'agenda-archives)
  16826. (setq extra (cdr extra))
  16827. (setq files (org-add-archive-files files)))
  16828. (while (setq f (pop extra))
  16829. (unless (member (file-truename f) tnames)
  16830. (add-to-list 'files f 'append)
  16831. (add-to-list 'tnames (file-truename f) 'append)))
  16832. (multi-occur
  16833. (mapcar (lambda (x)
  16834. (with-current-buffer
  16835. (or (get-file-buffer x) (find-file-noselect x))
  16836. (widen)
  16837. (current-buffer)))
  16838. files)
  16839. regexp)))
  16840. (if (boundp 'occur-mode-find-occurrence-hook)
  16841. ;; Emacs 23
  16842. (add-hook 'occur-mode-find-occurrence-hook
  16843. (lambda ()
  16844. (when (org-mode-p)
  16845. (org-reveal))))
  16846. ;; Emacs 22
  16847. (defadvice occur-mode-goto-occurrence
  16848. (after org-occur-reveal activate)
  16849. (and (org-mode-p) (org-reveal)))
  16850. (defadvice occur-mode-goto-occurrence-other-window
  16851. (after org-occur-reveal activate)
  16852. (and (org-mode-p) (org-reveal)))
  16853. (defadvice occur-mode-display-occurrence
  16854. (after org-occur-reveal activate)
  16855. (when (org-mode-p)
  16856. (let ((pos (occur-mode-find-occurrence)))
  16857. (with-current-buffer (marker-buffer pos)
  16858. (save-excursion
  16859. (goto-char pos)
  16860. (org-reveal)))))))
  16861. (defun org-occur-link-in-agenda-files ()
  16862. "Create a link and search for it in the agendas.
  16863. The link is not stored in `org-stored-links', it is just created
  16864. for the search purpose."
  16865. (interactive)
  16866. (let ((link (condition-case nil
  16867. (org-store-link nil)
  16868. (error "Unable to create a link to here"))))
  16869. (org-occur-in-agenda-files (regexp-quote link))))
  16870. (defun org-uniquify (list)
  16871. "Remove duplicate elements from LIST."
  16872. (let (res)
  16873. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16874. res))
  16875. (defun org-delete-all (elts list)
  16876. "Remove all elements in ELTS from LIST."
  16877. (while elts
  16878. (setq list (delete (pop elts) list)))
  16879. list)
  16880. (defun org-count (cl-item cl-seq)
  16881. "Count the number of occurrences of ITEM in SEQ.
  16882. Taken from `count' in cl-seq.el with all keyword arguments removed."
  16883. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  16884. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  16885. (while (< cl-start cl-end)
  16886. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  16887. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  16888. (setq cl-start (1+ cl-start)))
  16889. cl-count))
  16890. (defun org-remove-if (predicate seq)
  16891. "Remove everything from SEQ that fulfills PREDICATE."
  16892. (let (res e)
  16893. (while seq
  16894. (setq e (pop seq))
  16895. (if (not (funcall predicate e)) (push e res)))
  16896. (nreverse res)))
  16897. (defun org-remove-if-not (predicate seq)
  16898. "Remove everything from SEQ that does not fulfill PREDICATE."
  16899. (let (res e)
  16900. (while seq
  16901. (setq e (pop seq))
  16902. (if (funcall predicate e) (push e res)))
  16903. (nreverse res)))
  16904. (defun org-back-over-empty-lines ()
  16905. "Move backwards over whitespace, to the beginning of the first empty line.
  16906. Returns the number of empty lines passed."
  16907. (let ((pos (point)))
  16908. (skip-chars-backward " \t\n\r")
  16909. (beginning-of-line 2)
  16910. (goto-char (min (point) pos))
  16911. (count-lines (point) pos)))
  16912. (defun org-skip-whitespace ()
  16913. (skip-chars-forward " \t\n\r"))
  16914. (defun org-point-in-group (point group &optional context)
  16915. "Check if POINT is in match-group GROUP.
  16916. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16917. match. If the match group does not exist or point is not inside it,
  16918. return nil."
  16919. (and (match-beginning group)
  16920. (>= point (match-beginning group))
  16921. (<= point (match-end group))
  16922. (if context
  16923. (list context (match-beginning group) (match-end group))
  16924. t)))
  16925. (defun org-switch-to-buffer-other-window (&rest args)
  16926. "Switch to buffer in a second window on the current frame.
  16927. In particular, do not allow pop-up frames.
  16928. Returns the newly created buffer."
  16929. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16930. special-display-function)
  16931. (apply 'switch-to-buffer-other-window args)))
  16932. (defun org-combine-plists (&rest plists)
  16933. "Create a single property list from all plists in PLISTS.
  16934. The process starts by copying the first list, and then setting properties
  16935. from the other lists. Settings in the last list are the most significant
  16936. ones and overrule settings in the other lists."
  16937. (let ((rtn (copy-sequence (pop plists)))
  16938. p v ls)
  16939. (while plists
  16940. (setq ls (pop plists))
  16941. (while ls
  16942. (setq p (pop ls) v (pop ls))
  16943. (setq rtn (plist-put rtn p v))))
  16944. rtn))
  16945. (defun org-move-line-down (arg)
  16946. "Move the current line down. With prefix argument, move it past ARG lines."
  16947. (interactive "p")
  16948. (let ((col (current-column))
  16949. beg end pos)
  16950. (beginning-of-line 1) (setq beg (point))
  16951. (beginning-of-line 2) (setq end (point))
  16952. (beginning-of-line (+ 1 arg))
  16953. (setq pos (move-marker (make-marker) (point)))
  16954. (insert (delete-and-extract-region beg end))
  16955. (goto-char pos)
  16956. (org-move-to-column col)))
  16957. (defun org-move-line-up (arg)
  16958. "Move the current line up. With prefix argument, move it past ARG lines."
  16959. (interactive "p")
  16960. (let ((col (current-column))
  16961. beg end pos)
  16962. (beginning-of-line 1) (setq beg (point))
  16963. (beginning-of-line 2) (setq end (point))
  16964. (beginning-of-line (- arg))
  16965. (setq pos (move-marker (make-marker) (point)))
  16966. (insert (delete-and-extract-region beg end))
  16967. (goto-char pos)
  16968. (org-move-to-column col)))
  16969. (defun org-replace-escapes (string table)
  16970. "Replace %-escapes in STRING with values in TABLE.
  16971. TABLE is an association list with keys like \"%a\" and string values.
  16972. The sequences in STRING may contain normal field width and padding information,
  16973. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16974. so values can contain further %-escapes if they are define later in TABLE."
  16975. (let ((tbl (copy-alist table))
  16976. (case-fold-search nil)
  16977. (pchg 0)
  16978. e re rpl)
  16979. (while (setq e (pop tbl))
  16980. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16981. (when (and (cdr e) (string-match re (cdr e)))
  16982. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16983. (safe "SREF"))
  16984. (add-text-properties 0 3 (list 'sref sref) safe)
  16985. (setcdr e (replace-match safe t t (cdr e)))))
  16986. (while (string-match re string)
  16987. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16988. (cdr e)))
  16989. (setq string (replace-match rpl t t string))))
  16990. (while (setq pchg (next-property-change pchg string))
  16991. (let ((sref (get-text-property pchg 'sref string)))
  16992. (when (and sref (string-match "SREF" string pchg))
  16993. (setq string (replace-match sref t t string)))))
  16994. string))
  16995. (defun org-sublist (list start end)
  16996. "Return a section of LIST, from START to END.
  16997. Counting starts at 1."
  16998. (let (rtn (c start))
  16999. (setq list (nthcdr (1- start) list))
  17000. (while (and list (<= c end))
  17001. (push (pop list) rtn)
  17002. (setq c (1+ c)))
  17003. (nreverse rtn)))
  17004. (defun org-find-base-buffer-visiting (file)
  17005. "Like `find-buffer-visiting' but always return the base buffer and
  17006. not an indirect buffer."
  17007. (let ((buf (or (get-file-buffer file)
  17008. (find-buffer-visiting file))))
  17009. (if buf
  17010. (or (buffer-base-buffer buf) buf)
  17011. nil)))
  17012. (defun org-image-file-name-regexp (&optional extensions)
  17013. "Return regexp matching the file names of images.
  17014. If EXTENSIONS is given, only match these."
  17015. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  17016. (image-file-name-regexp)
  17017. (let ((image-file-name-extensions
  17018. (or extensions
  17019. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17020. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  17021. (concat "\\."
  17022. (regexp-opt (nconc (mapcar 'upcase
  17023. image-file-name-extensions)
  17024. image-file-name-extensions)
  17025. t)
  17026. "\\'"))))
  17027. (defun org-file-image-p (file &optional extensions)
  17028. "Return non-nil if FILE is an image."
  17029. (save-match-data
  17030. (string-match (org-image-file-name-regexp extensions) file)))
  17031. (defun org-get-cursor-date ()
  17032. "Return the date at cursor in as a time.
  17033. This works in the calendar and in the agenda, anywhere else it just
  17034. returns the current time."
  17035. (let (date day defd)
  17036. (cond
  17037. ((eq major-mode 'calendar-mode)
  17038. (setq date (calendar-cursor-to-date)
  17039. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17040. ((eq major-mode 'org-agenda-mode)
  17041. (setq day (get-text-property (point) 'day))
  17042. (if day
  17043. (setq date (calendar-gregorian-from-absolute day)
  17044. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17045. (nth 2 date))))))
  17046. (or defd (current-time))))
  17047. (defvar org-agenda-action-marker (make-marker)
  17048. "Marker pointing to the entry for the next agenda action.")
  17049. (defun org-mark-entry-for-agenda-action ()
  17050. "Mark the current entry as target of an agenda action.
  17051. Agenda actions are actions executed from the agenda with the key `k',
  17052. which make use of the date at the cursor."
  17053. (interactive)
  17054. (move-marker org-agenda-action-marker
  17055. (save-excursion (org-back-to-heading t) (point))
  17056. (current-buffer))
  17057. (message
  17058. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17059. (defun org-mark-subtree ()
  17060. "Mark the current subtree.
  17061. This puts point at the start of the current subtree, and mark at the end.
  17062. If point is in an inline task, mark that task instead."
  17063. (interactive)
  17064. (let ((inline-task-p
  17065. (and (featurep 'org-inlinetask)
  17066. (org-inlinetask-in-task-p)))
  17067. (beg))
  17068. ;; Get beginning of subtree
  17069. (cond
  17070. (inline-task-p (org-inlinetask-goto-beginning))
  17071. ((org-at-heading-p) (beginning-of-line))
  17072. (t (let ((outline-regexp (org-get-limited-outline-regexp)))
  17073. (outline-previous-visible-heading 1))))
  17074. (setq beg (point))
  17075. ;; Get end of it
  17076. (if inline-task-p
  17077. (org-inlinetask-goto-end)
  17078. (org-end-of-subtree))
  17079. ;; Mark zone
  17080. (push-mark (point) nil t)
  17081. (goto-char beg)))
  17082. ;;; Paragraph filling stuff.
  17083. ;; We want this to be just right, so use the full arsenal.
  17084. (defun org-indent-line-function ()
  17085. "Indent line depending on context."
  17086. (interactive)
  17087. (let* ((pos (point))
  17088. (itemp (org-at-item-p))
  17089. (case-fold-search t)
  17090. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17091. (inline-task-p (and (featurep 'org-inlinetask)
  17092. (org-inlinetask-in-task-p)))
  17093. column bpos bcol tpos tcol)
  17094. (beginning-of-line 1)
  17095. (cond
  17096. ;; Comments
  17097. ((looking-at "# ") (setq column 0))
  17098. ;; Headings
  17099. ((looking-at "\\*+ ") (setq column 0))
  17100. ;; Literal examples
  17101. ((looking-at "[ \t]*:[ \t]")
  17102. (setq column (org-get-indentation))) ; do nothing
  17103. ;; Drawers
  17104. ((and (looking-at "[ \t]*:END:")
  17105. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17106. (save-excursion
  17107. (goto-char (1- (match-beginning 1)))
  17108. (setq column (current-column))))
  17109. ;; Special blocks
  17110. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17111. (save-excursion
  17112. (re-search-backward
  17113. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17114. (setq column (org-get-indentation (match-string 0))))
  17115. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17116. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17117. (save-excursion
  17118. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17119. (setq column
  17120. (if (equal (downcase (match-string 1)) "src")
  17121. ;; src blocks: let `org-edit-src-exit' handle them
  17122. (org-get-indentation)
  17123. (org-get-indentation (match-string 0)))))
  17124. ;; Lists
  17125. ((org-in-item-p)
  17126. (org-beginning-of-item)
  17127. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\(:?\\[@\\(:?start:\\)?[0-9]+\\][ \t]*\\)?\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  17128. (setq bpos (match-beginning 1) tpos (match-end 0)
  17129. bcol (progn (goto-char bpos) (current-column))
  17130. tcol (progn (goto-char tpos) (current-column)))
  17131. (if (> tcol (+ bcol org-description-max-indent))
  17132. (setq tcol (+ bcol 5)))
  17133. (goto-char pos)
  17134. (setq column (if itemp (org-get-indentation) tcol)))
  17135. ;; This line has nothing special, look at the previous relevant
  17136. ;; line to compute indentation
  17137. (t
  17138. (beginning-of-line 0)
  17139. (while (and (not (bobp))
  17140. (not (looking-at org-drawer-regexp))
  17141. ;; skip comments, verbatim, empty lines, tables,
  17142. ;; inline tasks, lists, drawers and blocks
  17143. (or (and (looking-at "[ \t]*:END:")
  17144. (re-search-backward org-drawer-regexp nil t))
  17145. (and (looking-at "[ \t]*#\\+end_")
  17146. (re-search-backward "[ \t]*#\\+begin_"nil t))
  17147. (looking-at "[ \t]*[\n:#|]")
  17148. (and (org-in-item-p) (goto-char (org-list-top-point)))
  17149. (and (not inline-task-p)
  17150. (featurep 'org-inlinetask)
  17151. (org-inlinetask-in-task-p)
  17152. (or (org-inlinetask-goto-beginning) t))))
  17153. (beginning-of-line 0))
  17154. (cond
  17155. ;; There was an heading above.
  17156. ((looking-at "\\*+[ \t]+")
  17157. (if (not org-adapt-indentation)
  17158. (setq column 0)
  17159. (goto-char (match-end 0))
  17160. (setq column (current-column))))
  17161. ;; A drawer had started and is unfinished
  17162. ((looking-at org-drawer-regexp)
  17163. (goto-char (1- (match-beginning 1)))
  17164. (setq column (current-column)))
  17165. ;; Else, nothing noticeable found: get indentation and go on.
  17166. (t (setq column (org-get-indentation))))))
  17167. ;; Now apply indentation and move cursor accordingly
  17168. (goto-char pos)
  17169. (if (<= (current-column) (current-indentation))
  17170. (org-indent-line-to column)
  17171. (save-excursion (org-indent-line-to column)))
  17172. ;; Special polishing for properties, see `org-property-format'
  17173. (setq column (current-column))
  17174. (beginning-of-line 1)
  17175. (if (looking-at
  17176. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17177. (replace-match (concat (match-string 1)
  17178. (format org-property-format
  17179. (match-string 2) (match-string 3)))
  17180. t t))
  17181. (org-move-to-column column)))
  17182. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17183. "Variable to store copy of `adaptive-fill-regexp'.
  17184. Since `adaptive-fill-regexp' is set to never match, we need to
  17185. store a backup of its value before entering `org-mode' so that
  17186. the functionality can be provided as a fall-back.")
  17187. (defun org-set-autofill-regexps ()
  17188. (interactive)
  17189. ;; In the paragraph separator we include headlines, because filling
  17190. ;; text in a line directly attached to a headline would otherwise
  17191. ;; fill the headline as well.
  17192. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17193. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17194. ;; The paragraph starter includes hand-formatted lists.
  17195. (org-set-local
  17196. 'paragraph-start
  17197. (concat
  17198. "\f" "\\|"
  17199. "[ ]*$" "\\|"
  17200. "\\*+ " "\\|"
  17201. "[ \t]*#" "\\|"
  17202. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  17203. "[ \t]*[:|]" "\\|"
  17204. "\\$\\$" "\\|"
  17205. "\\\\\\(begin\\|end\\|[][]\\)"))
  17206. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17207. ;; But only if the user has not turned off tables or fixed-width regions
  17208. (org-set-local
  17209. 'auto-fill-inhibit-regexp
  17210. (concat "\\*+ \\|#\\+"
  17211. "\\|[ \t]*" org-keyword-time-regexp
  17212. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17213. (concat
  17214. "\\|[ \t]*["
  17215. (if org-enable-table-editor "|" "")
  17216. (if org-enable-fixed-width-editor ":" "")
  17217. "]"))))
  17218. ;; We use our own fill-paragraph function, to make sure that tables
  17219. ;; and fixed-width regions are not wrapped. That function will pass
  17220. ;; through to `fill-paragraph' when appropriate.
  17221. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17222. ;; Adaptive filling: To get full control, first make sure that
  17223. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17224. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17225. (org-set-local 'org-adaptive-fill-regexp-backup
  17226. adaptive-fill-regexp))
  17227. (org-set-local 'adaptive-fill-regexp "\000")
  17228. (org-set-local 'adaptive-fill-function
  17229. 'org-adaptive-fill-function)
  17230. (org-set-local
  17231. 'align-mode-rules-list
  17232. '((org-in-buffer-settings
  17233. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17234. (modes . '(org-mode))))))
  17235. (defun org-fill-paragraph (&optional justify)
  17236. "Re-align a table, pass through to fill-paragraph if no table."
  17237. (let ((table-p (org-at-table-p))
  17238. (table.el-p (org-at-table.el-p)))
  17239. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17240. (save-excursion (goto-char (point-at-bol))
  17241. (looking-at outline-regexp)))
  17242. t) ; skip headlines
  17243. (table.el-p t) ; skip table.el tables
  17244. (table-p (org-table-align) t) ; align org-mode tables
  17245. (t nil)))) ; call paragraph-fill
  17246. ;; For reference, this is the default value of adaptive-fill-regexp
  17247. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17248. (defun org-adaptive-fill-function ()
  17249. "Return a fill prefix for org-mode files.
  17250. In particular, this makes sure hanging paragraphs for hand-formatted lists
  17251. work correctly."
  17252. (cond
  17253. ;; Comment line
  17254. ((looking-at "#[ \t]+")
  17255. (match-string-no-properties 0))
  17256. ;; Description list
  17257. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  17258. (save-excursion
  17259. (if (> (match-end 1) (+ (match-beginning 1)
  17260. org-description-max-indent))
  17261. (goto-char (+ (match-beginning 1) 5))
  17262. (goto-char (match-end 0)))
  17263. (make-string (current-column) ?\ )))
  17264. ;; Ordered or unordered list
  17265. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  17266. (save-excursion
  17267. (goto-char (match-end 0))
  17268. (make-string (current-column) ?\ )))
  17269. ;; Other text
  17270. ((looking-at org-adaptive-fill-regexp-backup)
  17271. (match-string-no-properties 0))))
  17272. ;;; Other stuff.
  17273. (defun org-toggle-fixed-width-section (arg)
  17274. "Toggle the fixed-width export.
  17275. If there is no active region, the QUOTE keyword at the current headline is
  17276. inserted or removed. When present, it causes the text between this headline
  17277. and the next to be exported as fixed-width text, and unmodified.
  17278. If there is an active region, this command adds or removes a colon as the
  17279. first character of this line. If the first character of a line is a colon,
  17280. this line is also exported in fixed-width font."
  17281. (interactive "P")
  17282. (let* ((cc 0)
  17283. (regionp (org-region-active-p))
  17284. (beg (if regionp (region-beginning) (point)))
  17285. (end (if regionp (region-end)))
  17286. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17287. (case-fold-search nil)
  17288. (re "[ \t]*\\(: \\)")
  17289. off)
  17290. (if regionp
  17291. (save-excursion
  17292. (goto-char beg)
  17293. (setq cc (current-column))
  17294. (beginning-of-line 1)
  17295. (setq off (looking-at re))
  17296. (while (> nlines 0)
  17297. (setq nlines (1- nlines))
  17298. (beginning-of-line 1)
  17299. (cond
  17300. (arg
  17301. (org-move-to-column cc t)
  17302. (insert ": \n")
  17303. (forward-line -1))
  17304. ((and off (looking-at re))
  17305. (replace-match "" t t nil 1))
  17306. ((not off) (org-move-to-column cc t) (insert ": ")))
  17307. (forward-line 1)))
  17308. (save-excursion
  17309. (org-back-to-heading)
  17310. (if (looking-at (concat outline-regexp
  17311. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17312. (replace-match "" t t nil 1)
  17313. (if (looking-at outline-regexp)
  17314. (progn
  17315. (goto-char (match-end 0))
  17316. (insert org-quote-string " "))))))))
  17317. (defun org-reftex-citation ()
  17318. "Use reftex-citation to insert a citation into the buffer.
  17319. This looks for a line like
  17320. #+BIBLIOGRAPHY: foo plain option:-d
  17321. and derives from it that foo.bib is the bibliography file relevant
  17322. for this document. It then installs the necessary environment for RefTeX
  17323. to work in this buffer and calls `reftex-citation' to insert a citation
  17324. into the buffer.
  17325. Export of such citations to both LaTeX and HTML is handled by the contributed
  17326. package org-exp-bibtex by Taru Karttunen."
  17327. (interactive)
  17328. (let ((reftex-docstruct-symbol 'rds)
  17329. (reftex-cite-format "\\cite{%l}")
  17330. rds bib)
  17331. (save-excursion
  17332. (save-restriction
  17333. (widen)
  17334. (let ((case-fold-search t)
  17335. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17336. (if (not (save-excursion
  17337. (or (re-search-forward re nil t)
  17338. (re-search-backward re nil t))))
  17339. (error "No bibliography defined in file")
  17340. (setq bib (concat (match-string 1) ".bib")
  17341. rds (list (list 'bib bib)))))))
  17342. (call-interactively 'reftex-citation)))
  17343. ;;;; Functions extending outline functionality
  17344. (defun org-beginning-of-line (&optional arg)
  17345. "Go to the beginning of the current line. If that is invisible, continue
  17346. to a visible line beginning. This makes the function of C-a more intuitive.
  17347. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17348. first attempt, and only move to after the tags when the cursor is already
  17349. beyond the end of the headline."
  17350. (interactive "P")
  17351. (let ((pos (point))
  17352. (special (if (consp org-special-ctrl-a/e)
  17353. (car org-special-ctrl-a/e)
  17354. org-special-ctrl-a/e))
  17355. refpos)
  17356. (if (org-bound-and-true-p line-move-visual)
  17357. (beginning-of-visual-line 1)
  17358. (beginning-of-line 1))
  17359. (if (and arg (fboundp 'move-beginning-of-line))
  17360. (call-interactively 'move-beginning-of-line)
  17361. (if (bobp)
  17362. nil
  17363. (backward-char 1)
  17364. (if (org-truely-invisible-p)
  17365. (while (and (not (bobp)) (org-truely-invisible-p))
  17366. (backward-char 1)
  17367. (beginning-of-line 1))
  17368. (forward-char 1))))
  17369. (when special
  17370. (cond
  17371. ((and (looking-at org-complex-heading-regexp)
  17372. (= (char-after (match-end 1)) ?\ ))
  17373. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17374. (point-at-eol)))
  17375. (goto-char
  17376. (if (eq special t)
  17377. (cond ((> pos refpos) refpos)
  17378. ((= pos (point)) refpos)
  17379. (t (point)))
  17380. (cond ((> pos (point)) (point))
  17381. ((not (eq last-command this-command)) (point))
  17382. (t refpos)))))
  17383. ((org-at-item-p)
  17384. (goto-char
  17385. (if (eq special t)
  17386. (cond ((> pos (match-end 4)) (match-end 4))
  17387. ((= pos (point)) (match-end 4))
  17388. (t (point)))
  17389. (cond ((> pos (point)) (point))
  17390. ((not (eq last-command this-command)) (point))
  17391. (t (match-end 4))))))))
  17392. (org-no-warnings
  17393. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17394. (defun org-end-of-line (&optional arg)
  17395. "Go to the end of the line.
  17396. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17397. first attempt, and only move to after the tags when the cursor is already
  17398. beyond the end of the headline."
  17399. (interactive "P")
  17400. (let ((special (if (consp org-special-ctrl-a/e)
  17401. (cdr org-special-ctrl-a/e)
  17402. org-special-ctrl-a/e)))
  17403. (if (or (not special)
  17404. (not (org-on-heading-p))
  17405. arg)
  17406. (call-interactively
  17407. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17408. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17409. (t 'end-of-line)))
  17410. (let ((pos (point)))
  17411. (beginning-of-line 1)
  17412. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  17413. (if (eq special t)
  17414. (if (or (< pos (match-beginning 1))
  17415. (= pos (match-end 0)))
  17416. (goto-char (match-beginning 1))
  17417. (goto-char (match-end 0)))
  17418. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17419. (goto-char (match-end 0))
  17420. (goto-char (match-beginning 1))))
  17421. (call-interactively (if (fboundp 'move-end-of-line)
  17422. 'move-end-of-line
  17423. 'end-of-line)))))
  17424. (org-no-warnings
  17425. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17426. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17427. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17428. (defun org-backward-sentence (&optional arg)
  17429. "Go to beginning of sentence, or beginning of table field.
  17430. This will call `backward-sentence' or `org-table-beginning-of-field',
  17431. depending on context."
  17432. (interactive "P")
  17433. (cond
  17434. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17435. (t (call-interactively 'backward-sentence))))
  17436. (defun org-forward-sentence (&optional arg)
  17437. "Go to end of sentence, or end of table field.
  17438. This will call `forward-sentence' or `org-table-end-of-field',
  17439. depending on context."
  17440. (interactive "P")
  17441. (cond
  17442. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17443. (t (call-interactively 'forward-sentence))))
  17444. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17445. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17446. (defun org-kill-line (&optional arg)
  17447. "Kill line, to tags or end of line."
  17448. (interactive "P")
  17449. (cond
  17450. ((or (not org-special-ctrl-k)
  17451. (bolp)
  17452. (not (org-on-heading-p)))
  17453. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17454. org-ctrl-k-protect-subtree)
  17455. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17456. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17457. (error "C-k aborted - would kill hidden subtree")))
  17458. (call-interactively 'kill-line))
  17459. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  17460. (kill-region (point) (match-beginning 1))
  17461. (org-set-tags nil t))
  17462. (t (kill-region (point) (point-at-eol)))))
  17463. (define-key org-mode-map "\C-k" 'org-kill-line)
  17464. (defun org-yank (&optional arg)
  17465. "Yank. If the kill is a subtree, treat it specially.
  17466. This command will look at the current kill and check if is a single
  17467. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17468. cursor is at the beginning of a line or after the stars of a currently
  17469. empty headline, then the yank is handled specially. How exactly depends
  17470. on the value of the following variables, both set by default.
  17471. org-yank-folded-subtrees
  17472. When set, the subtree(s) will be folded after insertion, but only
  17473. if doing so would now swallow text after the yanked text.
  17474. org-yank-adjusted-subtrees
  17475. When set, the subtree will be promoted or demoted in order to
  17476. fit into the local outline tree structure, which means that the level
  17477. will be adjusted so that it becomes the smaller one of the two
  17478. *visible* surrounding headings.
  17479. Any prefix to this command will cause `yank' to be called directly with
  17480. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17481. will just
  17482. plainly yank the text as it is.
  17483. \[1] The test checks if the first non-white line is a heading
  17484. and if there are no other headings with fewer stars."
  17485. (interactive "P")
  17486. (org-yank-generic 'yank arg))
  17487. (defun org-yank-generic (command arg)
  17488. "Perform some yank-like command.
  17489. This function implements the behavior described in the `org-yank'
  17490. documentation. However, it has been generalized to work for any
  17491. interactive command with similar behavior."
  17492. ;; pretend to be command COMMAND
  17493. (setq this-command command)
  17494. (if arg
  17495. (call-interactively command)
  17496. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17497. (and (org-kill-is-subtree-p)
  17498. (or (bolp)
  17499. (and (looking-at "[ \t]*$")
  17500. (string-match
  17501. "\\`\\*+\\'"
  17502. (buffer-substring (point-at-bol) (point)))))))
  17503. swallowp)
  17504. (cond
  17505. ((and subtreep org-yank-folded-subtrees)
  17506. (let ((beg (point))
  17507. end)
  17508. (if (and subtreep org-yank-adjusted-subtrees)
  17509. (org-paste-subtree nil nil 'for-yank)
  17510. (call-interactively command))
  17511. (setq end (point))
  17512. (goto-char beg)
  17513. (when (and (bolp) subtreep
  17514. (not (setq swallowp
  17515. (org-yank-folding-would-swallow-text beg end))))
  17516. (or (looking-at outline-regexp)
  17517. (re-search-forward (concat "^" outline-regexp) end t))
  17518. (while (and (< (point) end) (looking-at outline-regexp))
  17519. (hide-subtree)
  17520. (org-cycle-show-empty-lines 'folded)
  17521. (condition-case nil
  17522. (outline-forward-same-level 1)
  17523. (error (goto-char end)))))
  17524. (when swallowp
  17525. (message
  17526. "Inserted text not folded because that would swallow text"))
  17527. (goto-char end)
  17528. (skip-chars-forward " \t\n\r")
  17529. (beginning-of-line 1)
  17530. (push-mark beg 'nomsg)))
  17531. ((and subtreep org-yank-adjusted-subtrees)
  17532. (let ((beg (point-at-bol)))
  17533. (org-paste-subtree nil nil 'for-yank)
  17534. (push-mark beg 'nomsg)))
  17535. (t
  17536. (call-interactively command))))))
  17537. (defun org-yank-folding-would-swallow-text (beg end)
  17538. "Would hide-subtree at BEG swallow any text after END?"
  17539. (let (level)
  17540. (save-excursion
  17541. (goto-char beg)
  17542. (when (or (looking-at outline-regexp)
  17543. (re-search-forward (concat "^" outline-regexp) end t))
  17544. (setq level (org-outline-level)))
  17545. (goto-char end)
  17546. (skip-chars-forward " \t\r\n\v\f")
  17547. (if (or (eobp)
  17548. (and (bolp) (looking-at org-outline-regexp)
  17549. (<= (org-outline-level) level)))
  17550. nil ; Nothing would be swallowed
  17551. t)))) ; something would swallow
  17552. (define-key org-mode-map "\C-y" 'org-yank)
  17553. (defun org-invisible-p ()
  17554. "Check if point is at a character currently not visible."
  17555. ;; Early versions of noutline don't have `outline-invisible-p'.
  17556. (if (fboundp 'outline-invisible-p)
  17557. (outline-invisible-p)
  17558. (get-char-property (point) 'invisible)))
  17559. (defun org-truely-invisible-p ()
  17560. "Check if point is at a character currently not visible.
  17561. This version does not only check the character property, but also
  17562. `visible-mode'."
  17563. ;; Early versions of noutline don't have `outline-invisible-p'.
  17564. (if (org-bound-and-true-p visible-mode)
  17565. nil
  17566. (if (fboundp 'outline-invisible-p)
  17567. (outline-invisible-p)
  17568. (get-char-property (point) 'invisible))))
  17569. (defun org-invisible-p2 ()
  17570. "Check if point is at a character currently not visible."
  17571. (save-excursion
  17572. (if (and (eolp) (not (bobp))) (backward-char 1))
  17573. ;; Early versions of noutline don't have `outline-invisible-p'.
  17574. (if (fboundp 'outline-invisible-p)
  17575. (outline-invisible-p)
  17576. (get-char-property (point) 'invisible))))
  17577. (defun org-back-to-heading (&optional invisible-ok)
  17578. "Call `outline-back-to-heading', but provide a better error message."
  17579. (condition-case nil
  17580. (outline-back-to-heading invisible-ok)
  17581. (error (error "Before first headline at position %d in buffer %s"
  17582. (point) (current-buffer)))))
  17583. (defun org-beginning-of-defun ()
  17584. "Go to the beginning of the subtree, i.e. back to the heading."
  17585. (org-back-to-heading))
  17586. (defun org-end-of-defun ()
  17587. "Go to the end of the subtree."
  17588. (org-end-of-subtree nil t))
  17589. (defun org-before-first-heading-p ()
  17590. "Before first heading?"
  17591. (save-excursion
  17592. (end-of-line)
  17593. (null (re-search-backward "^\\*+ " nil t))))
  17594. (defun org-on-heading-p (&optional ignored)
  17595. (outline-on-heading-p t))
  17596. (defun org-at-heading-p (&optional ignored)
  17597. (outline-on-heading-p t))
  17598. (defun org-point-at-end-of-empty-headline ()
  17599. "If point is at the end of an empty headline, return t, else nil.
  17600. If the heading only contains a TODO keyword, it is still still considered
  17601. empty."
  17602. (and (looking-at "[ \t]*$")
  17603. (save-excursion
  17604. (beginning-of-line 1)
  17605. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17606. "\\)?[ \t]*$")))))
  17607. (defun org-at-heading-or-item-p ()
  17608. (or (org-on-heading-p) (org-at-item-p)))
  17609. (defun org-on-target-p ()
  17610. (or (org-in-regexp org-radio-target-regexp)
  17611. (org-in-regexp org-target-regexp)))
  17612. (defun org-up-heading-all (arg)
  17613. "Move to the heading line of which the present line is a subheading.
  17614. This function considers both visible and invisible heading lines.
  17615. With argument, move up ARG levels."
  17616. (if (fboundp 'outline-up-heading-all)
  17617. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17618. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17619. (defun org-up-heading-safe ()
  17620. "Move to the heading line of which the present line is a subheading.
  17621. This version will not throw an error. It will return the level of the
  17622. headline found, or nil if no higher level is found.
  17623. Also, this function will be a lot faster than `outline-up-heading',
  17624. because it relies on stars being the outline starters. This can really
  17625. make a significant difference in outlines with very many siblings."
  17626. (let (start-level re)
  17627. (org-back-to-heading t)
  17628. (setq start-level (funcall outline-level))
  17629. (if (equal start-level 1)
  17630. nil
  17631. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17632. (if (re-search-backward re nil t)
  17633. (funcall outline-level)))))
  17634. (defun org-first-sibling-p ()
  17635. "Is this heading the first child of its parents?"
  17636. (interactive)
  17637. (let ((re (concat "^" outline-regexp))
  17638. level l)
  17639. (unless (org-at-heading-p t)
  17640. (error "Not at a heading"))
  17641. (setq level (funcall outline-level))
  17642. (save-excursion
  17643. (if (not (re-search-backward re nil t))
  17644. t
  17645. (setq l (funcall outline-level))
  17646. (< l level)))))
  17647. (defun org-goto-sibling (&optional previous)
  17648. "Goto the next sibling, even if it is invisible.
  17649. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17650. when a sibling was found. When none is found, return nil and don't
  17651. move point."
  17652. (let ((fun (if previous 're-search-backward 're-search-forward))
  17653. (pos (point))
  17654. (re (concat "^" outline-regexp))
  17655. level l)
  17656. (when (condition-case nil (org-back-to-heading t) (error nil))
  17657. (setq level (funcall outline-level))
  17658. (catch 'exit
  17659. (or previous (forward-char 1))
  17660. (while (funcall fun re nil t)
  17661. (setq l (funcall outline-level))
  17662. (when (< l level) (goto-char pos) (throw 'exit nil))
  17663. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17664. (goto-char pos)
  17665. nil))))
  17666. (defun org-show-siblings ()
  17667. "Show all siblings of the current headline."
  17668. (save-excursion
  17669. (while (org-goto-sibling) (org-flag-heading nil)))
  17670. (save-excursion
  17671. (while (org-goto-sibling 'previous)
  17672. (org-flag-heading nil))))
  17673. (defun org-goto-first-child ()
  17674. "Goto the first child, even if it is invisible.
  17675. Return t when a child was found. Otherwise don't move point and
  17676. return nil."
  17677. (let (level (pos (point)) (re (concat "^" outline-regexp)))
  17678. (when (condition-case nil (org-back-to-heading t) (error nil))
  17679. (setq level (outline-level))
  17680. (forward-char 1)
  17681. (if (and (re-search-forward re nil t) (> (outline-level) level))
  17682. (progn (goto-char (match-beginning 0)) t)
  17683. (goto-char pos) nil))))
  17684. (defun org-show-hidden-entry ()
  17685. "Show an entry where even the heading is hidden."
  17686. (save-excursion
  17687. (org-show-entry)))
  17688. (defun org-flag-heading (flag &optional entry)
  17689. "Flag the current heading. FLAG non-nil means make invisible.
  17690. When ENTRY is non-nil, show the entire entry."
  17691. (save-excursion
  17692. (org-back-to-heading t)
  17693. ;; Check if we should show the entire entry
  17694. (if entry
  17695. (progn
  17696. (org-show-entry)
  17697. (save-excursion
  17698. (and (outline-next-heading)
  17699. (org-flag-heading nil))))
  17700. (outline-flag-region (max (point-min) (1- (point)))
  17701. (save-excursion (outline-end-of-heading) (point))
  17702. flag))))
  17703. (defun org-get-next-sibling ()
  17704. "Move to next heading of the same level, and return point.
  17705. If there is no such heading, return nil.
  17706. This is like outline-next-sibling, but invisible headings are ok."
  17707. (let ((level (funcall outline-level)))
  17708. (outline-next-heading)
  17709. (while (and (not (eobp)) (> (funcall outline-level) level))
  17710. (outline-next-heading))
  17711. (if (or (eobp) (< (funcall outline-level) level))
  17712. nil
  17713. (point))))
  17714. (defun org-get-last-sibling ()
  17715. "Move to previous heading of the same level, and return point.
  17716. If there is no such heading, return nil."
  17717. (let ((opoint (point))
  17718. (level (funcall outline-level)))
  17719. (outline-previous-heading)
  17720. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17721. (while (and (> (funcall outline-level) level)
  17722. (not (bobp)))
  17723. (outline-previous-heading))
  17724. (if (< (funcall outline-level) level)
  17725. nil
  17726. (point)))))
  17727. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17728. ;; This contains an exact copy of the original function, but it uses
  17729. ;; `org-back-to-heading', to make it work also in invisible
  17730. ;; trees. And is uses an invisible-OK argument.
  17731. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17732. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17733. ;; with many children and grandchildren etc, this can be much faster
  17734. ;; than the outline version.
  17735. (org-back-to-heading invisible-OK)
  17736. (let ((first t)
  17737. (level (funcall outline-level)))
  17738. (if (and (org-mode-p) (< level 1000))
  17739. ;; A true heading (not a plain list item), in Org-mode
  17740. ;; This means we can easily find the end by looking
  17741. ;; only for the right number of stars. Using a regexp to do
  17742. ;; this is so much faster than using a Lisp loop.
  17743. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17744. (forward-char 1)
  17745. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17746. ;; something else, do it the slow way
  17747. (while (and (not (eobp))
  17748. (or first (> (funcall outline-level) level)))
  17749. (setq first nil)
  17750. (outline-next-heading)))
  17751. (unless to-heading
  17752. (if (memq (preceding-char) '(?\n ?\^M))
  17753. (progn
  17754. ;; Go to end of line before heading
  17755. (forward-char -1)
  17756. (if (memq (preceding-char) '(?\n ?\^M))
  17757. ;; leave blank line before heading
  17758. (forward-char -1))))))
  17759. (point))
  17760. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17761. "Use Org version in org-mode, for dramatic speed-up."
  17762. (if (eq major-mode 'org-mode)
  17763. (progn
  17764. (org-end-of-subtree nil t)
  17765. (unless (eobp) (backward-char 1)))
  17766. ad-do-it))
  17767. (defun org-forward-same-level (arg &optional invisible-ok)
  17768. "Move forward to the arg'th subheading at same level as this one.
  17769. Stop at the first and last subheadings of a superior heading.
  17770. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  17771. it wil also look at invisible ones."
  17772. (interactive "p")
  17773. (org-back-to-heading invisible-ok)
  17774. (org-on-heading-p)
  17775. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17776. (re (format "^\\*\\{1,%d\\} " level))
  17777. l)
  17778. (forward-char 1)
  17779. (while (> arg 0)
  17780. (while (and (re-search-forward re nil 'move)
  17781. (setq l (- (match-end 0) (match-beginning 0) 1))
  17782. (= l level)
  17783. (not invisible-ok)
  17784. (progn (backward-char 1) (org-invisible-p)))
  17785. (if (< l level) (setq arg 1)))
  17786. (setq arg (1- arg)))
  17787. (beginning-of-line 1)))
  17788. (defun org-backward-same-level (arg &optional invisible-ok)
  17789. "Move backward to the arg'th subheading at same level as this one.
  17790. Stop at the first and last subheadings of a superior heading."
  17791. (interactive "p")
  17792. (org-back-to-heading)
  17793. (org-on-heading-p)
  17794. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17795. (re (format "^\\*\\{1,%d\\} " level))
  17796. l)
  17797. (while (> arg 0)
  17798. (while (and (re-search-backward re nil 'move)
  17799. (setq l (- (match-end 0) (match-beginning 0) 1))
  17800. (= l level)
  17801. (not invisible-ok)
  17802. (org-invisible-p))
  17803. (if (< l level) (setq arg 1)))
  17804. (setq arg (1- arg)))))
  17805. (defun org-show-subtree ()
  17806. "Show everything after this heading at deeper levels."
  17807. (outline-flag-region
  17808. (point)
  17809. (save-excursion
  17810. (org-end-of-subtree t t))
  17811. nil))
  17812. (defun org-show-entry ()
  17813. "Show the body directly following this heading.
  17814. Show the heading too, if it is currently invisible."
  17815. (interactive)
  17816. (save-excursion
  17817. (condition-case nil
  17818. (progn
  17819. (org-back-to-heading t)
  17820. (outline-flag-region
  17821. (max (point-min) (1- (point)))
  17822. (save-excursion
  17823. (if (re-search-forward
  17824. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17825. (match-beginning 1)
  17826. (point-max)))
  17827. nil)
  17828. (org-cycle-hide-drawers 'children))
  17829. (error nil))))
  17830. (defun org-make-options-regexp (kwds &optional extra)
  17831. "Make a regular expression for keyword lines."
  17832. (concat
  17833. "^"
  17834. "#?[ \t]*\\+\\("
  17835. (mapconcat 'regexp-quote kwds "\\|")
  17836. (if extra (concat "\\|" extra))
  17837. "\\):[ \t]*"
  17838. "\\(.*\\)"))
  17839. ;; Make isearch reveal the necessary context
  17840. (defun org-isearch-end ()
  17841. "Reveal context after isearch exits."
  17842. (when isearch-success ; only if search was successful
  17843. (if (featurep 'xemacs)
  17844. ;; Under XEmacs, the hook is run in the correct place,
  17845. ;; we directly show the context.
  17846. (org-show-context 'isearch)
  17847. ;; In Emacs the hook runs *before* restoring the overlays.
  17848. ;; So we have to use a one-time post-command-hook to do this.
  17849. ;; (Emacs 22 has a special variable, see function `org-mode')
  17850. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17851. isearch-mode-end-hook-quit)
  17852. ;; Only when the isearch was not quitted.
  17853. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17854. 'append 'local)))))
  17855. (defun org-isearch-post-command ()
  17856. "Remove self from hook, and show context."
  17857. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17858. (org-show-context 'isearch))
  17859. ;;;; Integration with and fixes for other packages
  17860. ;;; Imenu support
  17861. (defvar org-imenu-markers nil
  17862. "All markers currently used by Imenu.")
  17863. (make-variable-buffer-local 'org-imenu-markers)
  17864. (defun org-imenu-new-marker (&optional pos)
  17865. "Return a new marker for use by Imenu, and remember the marker."
  17866. (let ((m (make-marker)))
  17867. (move-marker m (or pos (point)))
  17868. (push m org-imenu-markers)
  17869. m))
  17870. (defun org-imenu-get-tree ()
  17871. "Produce the index for Imenu."
  17872. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17873. (setq org-imenu-markers nil)
  17874. (let* ((n org-imenu-depth)
  17875. (re (concat "^" outline-regexp))
  17876. (subs (make-vector (1+ n) nil))
  17877. (last-level 0)
  17878. m level head)
  17879. (save-excursion
  17880. (save-restriction
  17881. (widen)
  17882. (goto-char (point-max))
  17883. (while (re-search-backward re nil t)
  17884. (setq level (org-reduced-level (funcall outline-level)))
  17885. (when (<= level n)
  17886. (looking-at org-complex-heading-regexp)
  17887. (setq head (org-link-display-format
  17888. (org-match-string-no-properties 4))
  17889. m (org-imenu-new-marker))
  17890. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17891. (if (>= level last-level)
  17892. (push (cons head m) (aref subs level))
  17893. (push (cons head (aref subs (1+ level))) (aref subs level))
  17894. (loop for i from (1+ level) to n do (aset subs i nil)))
  17895. (setq last-level level)))))
  17896. (aref subs 1)))
  17897. (eval-after-load "imenu"
  17898. '(progn
  17899. (add-hook 'imenu-after-jump-hook
  17900. (lambda ()
  17901. (if (eq major-mode 'org-mode)
  17902. (org-show-context 'org-goto))))))
  17903. (defun org-link-display-format (link)
  17904. "Replace a link with either the description, or the link target
  17905. if no description is present"
  17906. (save-match-data
  17907. (if (string-match org-bracket-link-analytic-regexp link)
  17908. (replace-match (if (match-end 5)
  17909. (match-string 5 link)
  17910. (concat (match-string 1 link)
  17911. (match-string 3 link)))
  17912. nil t link)
  17913. link)))
  17914. ;; Speedbar support
  17915. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17916. "Overlay marking the agenda restriction line in speedbar.")
  17917. (overlay-put org-speedbar-restriction-lock-overlay
  17918. 'face 'org-agenda-restriction-lock)
  17919. (overlay-put org-speedbar-restriction-lock-overlay
  17920. 'help-echo "Agendas are currently limited to this item.")
  17921. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17922. (defun org-speedbar-set-agenda-restriction ()
  17923. "Restrict future agenda commands to the location at point in speedbar.
  17924. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17925. (interactive)
  17926. (require 'org-agenda)
  17927. (let (p m tp np dir txt)
  17928. (cond
  17929. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17930. 'org-imenu t))
  17931. (setq m (get-text-property p 'org-imenu-marker))
  17932. (with-current-buffer (marker-buffer m)
  17933. (goto-char m)
  17934. (org-agenda-set-restriction-lock 'subtree)))
  17935. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17936. 'speedbar-function 'speedbar-find-file))
  17937. (setq tp (previous-single-property-change
  17938. (1+ p) 'speedbar-function)
  17939. np (next-single-property-change
  17940. tp 'speedbar-function)
  17941. dir (speedbar-line-directory)
  17942. txt (buffer-substring-no-properties (or tp (point-min))
  17943. (or np (point-max))))
  17944. (with-current-buffer (find-file-noselect
  17945. (let ((default-directory dir))
  17946. (expand-file-name txt)))
  17947. (unless (org-mode-p)
  17948. (error "Cannot restrict to non-Org-mode file"))
  17949. (org-agenda-set-restriction-lock 'file)))
  17950. (t (error "Don't know how to restrict Org-mode's agenda")))
  17951. (move-overlay org-speedbar-restriction-lock-overlay
  17952. (point-at-bol) (point-at-eol))
  17953. (setq current-prefix-arg nil)
  17954. (org-agenda-maybe-redo)))
  17955. (eval-after-load "speedbar"
  17956. '(progn
  17957. (speedbar-add-supported-extension ".org")
  17958. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17959. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17960. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17961. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17962. (add-hook 'speedbar-visiting-tag-hook
  17963. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17964. ;;; Fixes and Hacks for problems with other packages
  17965. ;; Make flyspell not check words in links, to not mess up our keymap
  17966. (defun org-mode-flyspell-verify ()
  17967. "Don't let flyspell put overlays at active buttons."
  17968. (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
  17969. (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
  17970. (defun org-remove-flyspell-overlays-in (beg end)
  17971. "Remove flyspell overlays in region."
  17972. (and (org-bound-and-true-p flyspell-mode)
  17973. (fboundp 'flyspell-delete-region-overlays)
  17974. (flyspell-delete-region-overlays beg end))
  17975. (add-text-properties beg end '(org-no-flyspell t)))
  17976. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17977. (eval-after-load "bookmark"
  17978. '(if (boundp 'bookmark-after-jump-hook)
  17979. ;; We can use the hook
  17980. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17981. ;; Hook not available, use advice
  17982. (defadvice bookmark-jump (after org-make-visible activate)
  17983. "Make the position visible."
  17984. (org-bookmark-jump-unhide))))
  17985. ;; Make sure saveplace shows the location if it was hidden
  17986. (eval-after-load "saveplace"
  17987. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17988. "Make the position visible."
  17989. (org-bookmark-jump-unhide)))
  17990. ;; Make sure ecb shows the location if it was hidden
  17991. (eval-after-load "ecb"
  17992. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17993. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17994. (if (eq major-mode 'org-mode)
  17995. (org-show-context))))
  17996. (defun org-bookmark-jump-unhide ()
  17997. "Unhide the current position, to show the bookmark location."
  17998. (and (org-mode-p)
  17999. (or (org-invisible-p)
  18000. (save-excursion (goto-char (max (point-min) (1- (point))))
  18001. (org-invisible-p)))
  18002. (org-show-context 'bookmark-jump)))
  18003. ;; Make session.el ignore our circular variable
  18004. (eval-after-load "session"
  18005. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  18006. ;;;; Experimental code
  18007. (defun org-closed-in-range ()
  18008. "Sparse tree of items closed in a certain time range.
  18009. Still experimental, may disappear in the future."
  18010. (interactive)
  18011. ;; Get the time interval from the user.
  18012. (let* ((time1 (org-float-time
  18013. (org-read-date nil 'to-time nil "Starting date: ")))
  18014. (time2 (org-float-time
  18015. (org-read-date nil 'to-time nil "End date:")))
  18016. ;; callback function
  18017. (callback (lambda ()
  18018. (let ((time
  18019. (org-float-time
  18020. (apply 'encode-time
  18021. (org-parse-time-string
  18022. (match-string 1))))))
  18023. ;; check if time in interval
  18024. (and (>= time time1) (<= time time2))))))
  18025. ;; make tree, check each match with the callback
  18026. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  18027. ;;;; Finish up
  18028. (provide 'org)
  18029. (run-hooks 'org-load-hook)
  18030. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  18031. ;;; org.el ends here