org.el 734 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.3
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; 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.3"
  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 " vm: Links to VM folders/messages" org-vm)
  271. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  272. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  273. (const :tag " mouse: Additional mouse support" org-mouse)
  274. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  275. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  276. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  277. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  278. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  279. (const :tag "C collector: Collect properties into tables" org-collector)
  280. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  281. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  282. (const :tag "C eval: Include command output as text" org-eval)
  283. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  284. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  285. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  286. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  287. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  288. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  289. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  290. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  291. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  292. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  293. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  294. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  295. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  296. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  297. (const :tag "C registry: A registry for Org-mode links" org-registry)
  298. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  299. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  300. (const :tag "C secretary: Team management with org-mode" org-secretary)
  301. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  302. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  303. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  304. (const :tag "C track: Keep up with Org-mode development" org-track)
  305. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  306. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  307. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  308. (defcustom org-support-shift-select nil
  309. "Non-nil means make shift-cursor commands select text when possible.
  310. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  311. selecting a region, or enlarge regions started in this way.
  312. In Org-mode, in special contexts, these same keys are used for other
  313. purposes, important enough to compete with shift selection. Org tries
  314. to balance these needs by supporting `shift-select-mode' outside these
  315. special contexts, under control of this variable.
  316. The default of this variable is nil, to avoid confusing behavior. Shifted
  317. cursor keys will then execute Org commands in the following contexts:
  318. - on a headline, changing TODO state (left/right) and priority (up/down)
  319. - on a time stamp, changing the time
  320. - in a plain list item, changing the bullet type
  321. - in a property definition line, switching between allowed values
  322. - in the BEGIN line of a clock table (changing the time block).
  323. Outside these contexts, the commands will throw an error.
  324. When this variable is t and the cursor is not in a special context,
  325. Org-mode will support shift-selection for making and enlarging regions.
  326. To make this more effective, the bullet cycling will no longer happen
  327. anywhere in an item line, but only if the cursor is exactly on the bullet.
  328. If you set this variable to the symbol `always', then the keys
  329. will not be special in headlines, property lines, and item lines, to make
  330. shift selection work there as well. If this is what you want, you can
  331. use the following alternative commands: `C-c C-t' and `C-c ,' to
  332. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  333. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  334. edited by hand or in column view.
  335. However, when the cursor is on a timestamp, shift-cursor commands
  336. will still edit the time stamp - this is just too good to give up.
  337. XEmacs user should have this variable set to nil, because shift-select-mode
  338. is Emacs 23 only."
  339. :group 'org
  340. :type '(choice
  341. (const :tag "Never" nil)
  342. (const :tag "When outside special context" t)
  343. (const :tag "Everywhere except timestamps" always)))
  344. (defgroup org-startup nil
  345. "Options concerning startup of Org-mode."
  346. :tag "Org Startup"
  347. :group 'org)
  348. (defcustom org-startup-folded t
  349. "Non-nil means entering Org-mode will switch to OVERVIEW.
  350. This can also be configured on a per-file basis by adding one of
  351. the following lines anywhere in the buffer:
  352. #+STARTUP: fold (or `overview', this is equivalent)
  353. #+STARTUP: nofold (or `showall', this is equivalent)
  354. #+STARTUP: content
  355. #+STARTUP: showeverything"
  356. :group 'org-startup
  357. :type '(choice
  358. (const :tag "nofold: show all" nil)
  359. (const :tag "fold: overview" t)
  360. (const :tag "content: all headlines" content)
  361. (const :tag "show everything, even drawers" showeverything)))
  362. (defcustom org-startup-truncated t
  363. "Non-nil means entering Org-mode will set `truncate-lines'.
  364. This is useful since some lines containing links can be very long and
  365. uninteresting. Also tables look terrible when wrapped."
  366. :group 'org-startup
  367. :type 'boolean)
  368. (defcustom org-startup-indented nil
  369. "Non-nil means turn on `org-indent-mode' on startup.
  370. This can also be configured on a per-file basis by adding one of
  371. the following lines anywhere in the buffer:
  372. #+STARTUP: indent
  373. #+STARTUP: noindent"
  374. :group 'org-structure
  375. :type '(choice
  376. (const :tag "Not" nil)
  377. (const :tag "Globally (slow on startup in large files)" t)))
  378. (defcustom org-use-sub-superscripts t
  379. "Non-nil means interpret \"_\" and \"^\" for export.
  380. When this option is turned on, you can use TeX-like syntax for sub- and
  381. superscripts. Several characters after \"_\" or \"^\" will be
  382. considered as a single item - so grouping with {} is normally not
  383. needed. For example, the following things will be parsed as single
  384. sub- or superscripts.
  385. 10^24 or 10^tau several digits will be considered 1 item.
  386. 10^-12 or 10^-tau a leading sign with digits or a word
  387. x^2-y^3 will be read as x^2 - y^3, because items are
  388. terminated by almost any nonword/nondigit char.
  389. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  390. Still, ambiguity is possible - so when in doubt use {} to enclose the
  391. sub/superscript. If you set this variable to the symbol `{}',
  392. the braces are *required* in order to trigger interpretations as
  393. sub/superscript. This can be helpful in documents that need \"_\"
  394. frequently in plain text.
  395. Not all export backends support this, but HTML does.
  396. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  397. :group 'org-startup
  398. :group 'org-export-translation
  399. :type '(choice
  400. (const :tag "Always interpret" t)
  401. (const :tag "Only with braces" {})
  402. (const :tag "Never interpret" nil)))
  403. (if (fboundp 'defvaralias)
  404. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  405. (defcustom org-startup-with-beamer-mode nil
  406. "Non-nil means turn on `org-beamer-mode' on startup.
  407. This can also be configured on a per-file basis by adding one of
  408. the following lines anywhere in the buffer:
  409. #+STARTUP: beamer"
  410. :group 'org-startup
  411. :type 'boolean)
  412. (defcustom org-startup-align-all-tables nil
  413. "Non-nil means align all tables when visiting a file.
  414. This is useful when the column width in tables is forced with <N> cookies
  415. in table fields. Such tables will look correct only after the first re-align.
  416. This can also be configured on a per-file basis by adding one of
  417. the following lines anywhere in the buffer:
  418. #+STARTUP: align
  419. #+STARTUP: noalign"
  420. :group 'org-startup
  421. :type 'boolean)
  422. (defcustom org-startup-with-inline-images nil
  423. "Non-nil means show inline images when loading a new Org file.
  424. This can also be configured on a per-file basis by adding one of
  425. the following lines anywhere in the buffer:
  426. #+STARTUP: inlineimages
  427. #+STARTUP: noinlineimages"
  428. :group 'org-startup
  429. :type 'boolean)
  430. (defcustom org-insert-mode-line-in-empty-file nil
  431. "Non-nil means insert the first line setting Org-mode in empty files.
  432. When the function `org-mode' is called interactively in an empty file, this
  433. normally means that the file name does not automatically trigger Org-mode.
  434. To ensure that the file will always be in Org-mode in the future, a
  435. line enforcing Org-mode will be inserted into the buffer, if this option
  436. has been set."
  437. :group 'org-startup
  438. :type 'boolean)
  439. (defcustom org-replace-disputed-keys nil
  440. "Non-nil means use alternative key bindings for some keys.
  441. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  442. These keys are also used by other packages like shift-selection-mode'
  443. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  444. If you want to use Org-mode together with one of these other modes,
  445. or more generally if you would like to move some Org-mode commands to
  446. other keys, set this variable and configure the keys with the variable
  447. `org-disputed-keys'.
  448. This option is only relevant at load-time of Org-mode, and must be set
  449. *before* org.el is loaded. Changing it requires a restart of Emacs to
  450. become effective."
  451. :group 'org-startup
  452. :type 'boolean)
  453. (defcustom org-use-extra-keys nil
  454. "Non-nil means use extra key sequence definitions for certain commands.
  455. This happens automatically if you run XEmacs or if `window-system'
  456. is nil. This variable lets you do the same manually. You must
  457. set it before loading org.
  458. Example: on Carbon Emacs 22 running graphically, with an external
  459. keyboard on a Powerbook, the default way of setting M-left might
  460. not work for either Alt or ESC. Setting this variable will make
  461. it work for ESC."
  462. :group 'org-startup
  463. :type 'boolean)
  464. (if (fboundp 'defvaralias)
  465. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  466. (defcustom org-disputed-keys
  467. '(([(shift up)] . [(meta p)])
  468. ([(shift down)] . [(meta n)])
  469. ([(shift left)] . [(meta -)])
  470. ([(shift right)] . [(meta +)])
  471. ([(control shift right)] . [(meta shift +)])
  472. ([(control shift left)] . [(meta shift -)]))
  473. "Keys for which Org-mode and other modes compete.
  474. This is an alist, cars are the default keys, second element specifies
  475. the alternative to use when `org-replace-disputed-keys' is t.
  476. Keys can be specified in any syntax supported by `define-key'.
  477. The value of this option takes effect only at Org-mode's startup,
  478. therefore you'll have to restart Emacs to apply it after changing."
  479. :group 'org-startup
  480. :type 'alist)
  481. (defun org-key (key)
  482. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  483. Or return the original if not disputed.
  484. Also apply the translations defined in `org-xemacs-key-equivalents'."
  485. (when org-replace-disputed-keys
  486. (let* ((nkey (key-description key))
  487. (x (org-find-if (lambda (x)
  488. (equal (key-description (car x)) nkey))
  489. org-disputed-keys)))
  490. (setq key (if x (cdr x) key))))
  491. (when (featurep 'xemacs)
  492. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  493. key)
  494. (defun org-find-if (predicate seq)
  495. (catch 'exit
  496. (while seq
  497. (if (funcall predicate (car seq))
  498. (throw 'exit (car seq))
  499. (pop seq)))))
  500. (defun org-defkey (keymap key def)
  501. "Define a key, possibly translated, as returned by `org-key'."
  502. (define-key keymap (org-key key) def))
  503. (defcustom org-ellipsis nil
  504. "The ellipsis to use in the Org-mode outline.
  505. When nil, just use the standard three dots. When a string, use that instead,
  506. When a face, use the standard 3 dots, but with the specified face.
  507. The change affects only Org-mode (which will then use its own display table).
  508. Changing this requires executing `M-x org-mode' in a buffer to become
  509. effective."
  510. :group 'org-startup
  511. :type '(choice (const :tag "Default" nil)
  512. (face :tag "Face" :value org-warning)
  513. (string :tag "String" :value "...#")))
  514. (defvar org-display-table nil
  515. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  516. (defgroup org-keywords nil
  517. "Keywords in Org-mode."
  518. :tag "Org Keywords"
  519. :group 'org)
  520. (defcustom org-deadline-string "DEADLINE:"
  521. "String to mark deadline entries.
  522. A deadline is this string, followed by a time stamp. Should be a word,
  523. terminated by a colon. You can insert a schedule keyword and
  524. a timestamp with \\[org-deadline].
  525. Changes become only effective after restarting Emacs."
  526. :group 'org-keywords
  527. :type 'string)
  528. (defcustom org-scheduled-string "SCHEDULED:"
  529. "String to mark scheduled TODO entries.
  530. A schedule is this string, followed by a time stamp. Should be a word,
  531. terminated by a colon. You can insert a schedule keyword and
  532. a timestamp with \\[org-schedule].
  533. Changes become only effective after restarting Emacs."
  534. :group 'org-keywords
  535. :type 'string)
  536. (defcustom org-closed-string "CLOSED:"
  537. "String used as the prefix for timestamps logging closing a TODO entry."
  538. :group 'org-keywords
  539. :type 'string)
  540. (defcustom org-clock-string "CLOCK:"
  541. "String used as prefix for timestamps clocking work hours on an item."
  542. :group 'org-keywords
  543. :type 'string)
  544. (defcustom org-comment-string "COMMENT"
  545. "Entries starting with this keyword will never be exported.
  546. An entry can be toggled between COMMENT and normal with
  547. \\[org-toggle-comment].
  548. Changes become only effective after restarting Emacs."
  549. :group 'org-keywords
  550. :type 'string)
  551. (defcustom org-quote-string "QUOTE"
  552. "Entries starting with this keyword will be exported in fixed-width font.
  553. Quoting applies only to the text in the entry following the headline, and does
  554. not extend beyond the next headline, even if that is lower level.
  555. An entry can be toggled between QUOTE and normal with
  556. \\[org-toggle-fixed-width-section]."
  557. :group 'org-keywords
  558. :type 'string)
  559. (defconst org-repeat-re
  560. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  561. "Regular expression for specifying repeated events.
  562. After a match, group 1 contains the repeat expression.")
  563. (defgroup org-structure nil
  564. "Options concerning the general structure of Org-mode files."
  565. :tag "Org Structure"
  566. :group 'org)
  567. (defgroup org-reveal-location nil
  568. "Options about how to make context of a location visible."
  569. :tag "Org Reveal Location"
  570. :group 'org-structure)
  571. (defconst org-context-choice
  572. '(choice
  573. (const :tag "Always" t)
  574. (const :tag "Never" nil)
  575. (repeat :greedy t :tag "Individual contexts"
  576. (cons
  577. (choice :tag "Context"
  578. (const agenda)
  579. (const org-goto)
  580. (const occur-tree)
  581. (const tags-tree)
  582. (const link-search)
  583. (const mark-goto)
  584. (const bookmark-jump)
  585. (const isearch)
  586. (const default))
  587. (boolean))))
  588. "Contexts for the reveal options.")
  589. (defcustom org-show-hierarchy-above '((default . t))
  590. "Non-nil means show full hierarchy when revealing a location.
  591. Org-mode often shows locations in an org-mode file which might have
  592. been invisible before. When this is set, the hierarchy of headings
  593. above the exposed location is shown.
  594. Turning this off for example for sparse trees makes them very compact.
  595. Instead of t, this can also be an alist specifying this option for different
  596. contexts. Valid contexts are
  597. agenda when exposing an entry from the agenda
  598. org-goto when using the command `org-goto' on key C-c C-j
  599. occur-tree when using the command `org-occur' on key C-c /
  600. tags-tree when constructing a sparse tree based on tags matches
  601. link-search when exposing search matches associated with a link
  602. mark-goto when exposing the jump goal of a mark
  603. bookmark-jump when exposing a bookmark location
  604. isearch when exiting from an incremental search
  605. default default for all contexts not set explicitly"
  606. :group 'org-reveal-location
  607. :type org-context-choice)
  608. (defcustom org-show-following-heading '((default . nil))
  609. "Non-nil means show following heading when revealing a location.
  610. Org-mode often shows locations in an org-mode file which might have
  611. been invisible before. When this is set, the heading following the
  612. match is shown.
  613. Turning this off for example for sparse trees makes them very compact,
  614. but makes it harder to edit the location of the match. In such a case,
  615. use the command \\[org-reveal] to show more context.
  616. Instead of t, this can also be an alist specifying this option for different
  617. contexts. See `org-show-hierarchy-above' for valid contexts."
  618. :group 'org-reveal-location
  619. :type org-context-choice)
  620. (defcustom org-show-siblings '((default . nil) (isearch t))
  621. "Non-nil means show all sibling heading when revealing a location.
  622. Org-mode often shows locations in an org-mode file which might have
  623. been invisible before. When this is set, the sibling of the current entry
  624. heading are all made visible. If `org-show-hierarchy-above' is t,
  625. the same happens on each level of the hierarchy above the current entry.
  626. By default this is on for the isearch context, off for all other contexts.
  627. Turning this off for example for sparse trees makes them very compact,
  628. but makes it harder to edit the location of the match. In such a case,
  629. use the command \\[org-reveal] to show more context.
  630. Instead of t, this can also be an alist specifying this option for different
  631. contexts. See `org-show-hierarchy-above' for valid contexts."
  632. :group 'org-reveal-location
  633. :type org-context-choice)
  634. (defcustom org-show-entry-below '((default . nil))
  635. "Non-nil means show the entry below a headline when revealing a location.
  636. Org-mode often shows locations in an org-mode file which might have
  637. been invisible before. When this is set, the text below the headline that is
  638. exposed is also shown.
  639. By default this is off for all contexts.
  640. Instead of t, this can also be an alist specifying this option for different
  641. contexts. See `org-show-hierarchy-above' for valid contexts."
  642. :group 'org-reveal-location
  643. :type org-context-choice)
  644. (defcustom org-indirect-buffer-display 'other-window
  645. "How should indirect tree buffers be displayed?
  646. This applies to indirect buffers created with the commands
  647. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  648. Valid values are:
  649. current-window Display in the current window
  650. other-window Just display in another window.
  651. dedicated-frame Create one new frame, and re-use it each time.
  652. new-frame Make a new frame each time. Note that in this case
  653. previously-made indirect buffers are kept, and you need to
  654. kill these buffers yourself."
  655. :group 'org-structure
  656. :group 'org-agenda-windows
  657. :type '(choice
  658. (const :tag "In current window" current-window)
  659. (const :tag "In current frame, other window" other-window)
  660. (const :tag "Each time a new frame" new-frame)
  661. (const :tag "One dedicated frame" dedicated-frame)))
  662. (defcustom org-use-speed-commands nil
  663. "Non-nil means activate single letter commands at beginning of a headline.
  664. This may also be a function to test for appropriate locations where speed
  665. commands should be active."
  666. :group 'org-structure
  667. :type '(choice
  668. (const :tag "Never" nil)
  669. (const :tag "At beginning of headline stars" t)
  670. (function)))
  671. (defcustom org-speed-commands-user nil
  672. "Alist of additional speed commands.
  673. This list will be checked before `org-speed-commands-default'
  674. when the variable `org-use-speed-commands' is non-nil
  675. and when the cursor is at the beginning of a headline.
  676. The car if each entry is a string with a single letter, which must
  677. be assigned to `self-insert-command' in the global map.
  678. The cdr is either a command to be called interactively, a function
  679. to be called, or a form to be evaluated.
  680. An entry that is just a list with a single string will be interpreted
  681. as a descriptive headline that will be added when listing the speed
  682. commands in the Help buffer using the `?' speed command."
  683. :group 'org-structure
  684. :type '(repeat :value ("k" . ignore)
  685. (choice :value ("k" . ignore)
  686. (list :tag "Descriptive Headline" (string :tag "Headline"))
  687. (cons :tag "Letter and Command"
  688. (string :tag "Command letter")
  689. (choice
  690. (function)
  691. (sexp))))))
  692. (defgroup org-cycle nil
  693. "Options concerning visibility cycling in Org-mode."
  694. :tag "Org Cycle"
  695. :group 'org-structure)
  696. (defcustom org-cycle-skip-children-state-if-no-children t
  697. "Non-nil means skip CHILDREN state in entries that don't have any."
  698. :group 'org-cycle
  699. :type 'boolean)
  700. (defcustom org-cycle-max-level nil
  701. "Maximum level which should still be subject to visibility cycling.
  702. Levels higher than this will, for cycling, be treated as text, not a headline.
  703. When `org-odd-levels-only' is set, a value of N in this variable actually
  704. means 2N-1 stars as the limiting headline.
  705. When nil, cycle all levels.
  706. Note that the limiting level of cycling is also influenced by
  707. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  708. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  709. than its value."
  710. :group 'org-cycle
  711. :type '(choice
  712. (const :tag "No limit" nil)
  713. (integer :tag "Maximum level")))
  714. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  715. "Names of drawers. Drawers are not opened by cycling on the headline above.
  716. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  717. this:
  718. :DRAWERNAME:
  719. .....
  720. :END:
  721. The drawer \"PROPERTIES\" is special for capturing properties through
  722. the property API.
  723. Drawers can be defined on the per-file basis with a line like:
  724. #+DRAWERS: HIDDEN STATE PROPERTIES"
  725. :group 'org-structure
  726. :group 'org-cycle
  727. :type '(repeat (string :tag "Drawer Name")))
  728. (defcustom org-hide-block-startup nil
  729. "Non-nil means entering Org-mode will fold all blocks.
  730. This can also be set in on a per-file basis with
  731. #+STARTUP: hideblocks
  732. #+STARTUP: showblocks"
  733. :group 'org-startup
  734. :group 'org-cycle
  735. :type 'boolean)
  736. (defcustom org-cycle-global-at-bob nil
  737. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  738. This makes it possible to do global cycling without having to use S-TAB or
  739. \\[universal-argument] TAB. For this special case to work, the first line \
  740. of the buffer
  741. must not be a headline - it may be empty or some other text. When used in
  742. this way, `org-cycle-hook' is disables temporarily, to make sure the
  743. cursor stays at the beginning of the buffer.
  744. When this option is nil, don't do anything special at the beginning
  745. of the buffer."
  746. :group 'org-cycle
  747. :type 'boolean)
  748. (defcustom org-cycle-level-after-item/entry-creation t
  749. "Non-nil means cycle entry level or item indentation in new empty entries.
  750. When the cursor is at the end of an empty headline, i.e with only stars
  751. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  752. and then all possible ancestor states, before returning to the original state.
  753. This makes data entry extremely fast: M-RET to create a new headline,
  754. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  755. When the cursor is at the end of an empty plain list item, one TAB will
  756. make it a subitem, two or more tabs will back up to make this an item
  757. higher up in the item hierarchy."
  758. :group 'org-cycle
  759. :type 'boolean)
  760. (defcustom org-cycle-emulate-tab t
  761. "Where should `org-cycle' emulate TAB.
  762. nil Never
  763. white Only in completely white lines
  764. whitestart Only at the beginning of lines, before the first non-white char
  765. t Everywhere except in headlines
  766. exc-hl-bol Everywhere except at the start of a headline
  767. If TAB is used in a place where it does not emulate TAB, the current subtree
  768. visibility is cycled."
  769. :group 'org-cycle
  770. :type '(choice (const :tag "Never" nil)
  771. (const :tag "Only in completely white lines" white)
  772. (const :tag "Before first char in a line" whitestart)
  773. (const :tag "Everywhere except in headlines" t)
  774. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  775. ))
  776. (defcustom org-cycle-separator-lines 2
  777. "Number of empty lines needed to keep an empty line between collapsed trees.
  778. If you leave an empty line between the end of a subtree and the following
  779. headline, this empty line is hidden when the subtree is folded.
  780. Org-mode will leave (exactly) one empty line visible if the number of
  781. empty lines is equal or larger to the number given in this variable.
  782. So the default 2 means at least 2 empty lines after the end of a subtree
  783. are needed to produce free space between a collapsed subtree and the
  784. following headline.
  785. If the number is negative, and the number of empty lines is at least -N,
  786. all empty lines are shown.
  787. Special case: when 0, never leave empty lines in collapsed view."
  788. :group 'org-cycle
  789. :type 'integer)
  790. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  791. (defcustom org-pre-cycle-hook nil
  792. "Hook that is run before visibility cycling is happening.
  793. The function(s) in this hook must accept a single argument which indicates
  794. the new state that will be set right after running this hook. The
  795. argument is a symbol. Before a global state change, it can have the values
  796. `overview', `content', or `all'. Before a local state change, it can have
  797. the values `folded', `children', or `subtree'."
  798. :group 'org-cycle
  799. :type 'hook)
  800. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  801. org-cycle-hide-drawers
  802. org-cycle-show-empty-lines
  803. org-optimize-window-after-visibility-change)
  804. "Hook that is run after `org-cycle' has changed the buffer visibility.
  805. The function(s) in this hook must accept a single argument which indicates
  806. the new state that was set by the most recent `org-cycle' command. The
  807. argument is a symbol. After a global state change, it can have the values
  808. `overview', `content', or `all'. After a local state change, it can have
  809. the values `folded', `children', or `subtree'."
  810. :group 'org-cycle
  811. :type 'hook)
  812. (defgroup org-edit-structure nil
  813. "Options concerning structure editing in Org-mode."
  814. :tag "Org Edit Structure"
  815. :group 'org-structure)
  816. (defcustom org-odd-levels-only nil
  817. "Non-nil means skip even levels and only use odd levels for the outline.
  818. This has the effect that two stars are being added/taken away in
  819. promotion/demotion commands. It also influences how levels are
  820. handled by the exporters.
  821. Changing it requires restart of `font-lock-mode' to become effective
  822. for fontification also in regions already fontified.
  823. You may also set this on a per-file basis by adding one of the following
  824. lines to the buffer:
  825. #+STARTUP: odd
  826. #+STARTUP: oddeven"
  827. :group 'org-edit-structure
  828. :group 'org-appearance
  829. :type 'boolean)
  830. (defcustom org-adapt-indentation t
  831. "Non-nil means adapt indentation to outline node level.
  832. When this variable is set, Org assumes that you write outlines by
  833. indenting text in each node to align with the headline (after the stars).
  834. The following issues are influenced by this variable:
  835. - When this is set and the *entire* text in an entry is indented, the
  836. indentation is increased by one space in a demotion command, and
  837. decreased by one in a promotion command. If any line in the entry
  838. body starts with text at column 0, indentation is not changed at all.
  839. - Property drawers and planning information is inserted indented when
  840. this variable s set. When nil, they will not be indented.
  841. - TAB indents a line relative to context. The lines below a headline
  842. will be indented when this variable is set.
  843. Note that this is all about true indentation, by adding and removing
  844. space characters. See also `org-indent.el' which does level-dependent
  845. indentation in a virtual way, i.e. at display time in Emacs."
  846. :group 'org-edit-structure
  847. :type 'boolean)
  848. (defcustom org-special-ctrl-a/e nil
  849. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  850. When t, `C-a' will bring back the cursor to the beginning of the
  851. headline text, i.e. after the stars and after a possible TODO keyword.
  852. In an item, this will be the position after the bullet.
  853. When the cursor is already at that position, another `C-a' will bring
  854. it to the beginning of the line.
  855. `C-e' will jump to the end of the headline, ignoring the presence of tags
  856. in the headline. A second `C-e' will then jump to the true end of the
  857. line, after any tags. This also means that, when this variable is
  858. non-nil, `C-e' also will never jump beyond the end of the heading of a
  859. folded section, i.e. not after the ellipses.
  860. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  861. going to the true line boundary first. Only a directly following, identical
  862. keypress will bring the cursor to the special positions.
  863. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  864. set separately."
  865. :group 'org-edit-structure
  866. :type '(choice
  867. (const :tag "off" nil)
  868. (const :tag "on: after stars/bullet and before tags first" t)
  869. (const :tag "reversed: true line boundary first" reversed)
  870. (cons :tag "Set C-a and C-e separately"
  871. (choice :tag "Special C-a"
  872. (const :tag "off" nil)
  873. (const :tag "on: after stars/bullet first" t)
  874. (const :tag "reversed: before stars/bullet first" reversed))
  875. (choice :tag "Special C-e"
  876. (const :tag "off" nil)
  877. (const :tag "on: before tags first" t)
  878. (const :tag "reversed: after tags first" reversed)))))
  879. (if (fboundp 'defvaralias)
  880. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  881. (defcustom org-special-ctrl-k nil
  882. "Non-nil means `C-k' will behave specially in headlines.
  883. When nil, `C-k' will call the default `kill-line' command.
  884. When t, the following will happen while the cursor is in the headline:
  885. - When the cursor is at the beginning of a headline, kill the entire
  886. line and possible the folded subtree below the line.
  887. - When in the middle of the headline text, kill the headline up to the tags.
  888. - When after the headline text, kill the tags."
  889. :group 'org-edit-structure
  890. :type 'boolean)
  891. (defcustom org-ctrl-k-protect-subtree nil
  892. "Non-nil means, do not delete a hidden subtree with C-k.
  893. When set to the symbol `error', simply throw an error when C-k is
  894. used to kill (part-of) a headline that has hidden text behind it.
  895. Any other non-nil value will result in a query to the user, if it is
  896. OK to kill that hidden subtree. When nil, kill without remorse."
  897. :group 'org-edit-structure
  898. :type '(choice
  899. (const :tag "Do not protect hidden subtrees" nil)
  900. (const :tag "Protect hidden subtrees with a security query" t)
  901. (const :tag "Never kill a hidden subtree with C-k" error)))
  902. (defcustom org-yank-folded-subtrees t
  903. "Non-nil means when yanking subtrees, fold them.
  904. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  905. it starts with a heading and all other headings in it are either children
  906. or siblings, then fold all the subtrees. However, do this only if no
  907. text after the yank would be swallowed into a folded tree by this action."
  908. :group 'org-edit-structure
  909. :type 'boolean)
  910. (defcustom org-yank-adjusted-subtrees nil
  911. "Non-nil means when yanking subtrees, adjust the level.
  912. With this setting, `org-paste-subtree' is used to insert the subtree, see
  913. this function for details."
  914. :group 'org-edit-structure
  915. :type 'boolean)
  916. (defcustom org-M-RET-may-split-line '((default . t))
  917. "Non-nil means M-RET will split the line at the cursor position.
  918. When nil, it will go to the end of the line before making a
  919. new line.
  920. You may also set this option in a different way for different
  921. contexts. Valid contexts are:
  922. headline when creating a new headline
  923. item when creating a new item
  924. table in a table field
  925. default the value to be used for all contexts not explicitly
  926. customized"
  927. :group 'org-structure
  928. :group 'org-table
  929. :type '(choice
  930. (const :tag "Always" t)
  931. (const :tag "Never" nil)
  932. (repeat :greedy t :tag "Individual contexts"
  933. (cons
  934. (choice :tag "Context"
  935. (const headline)
  936. (const item)
  937. (const table)
  938. (const default))
  939. (boolean)))))
  940. (defcustom org-insert-heading-respect-content nil
  941. "Non-nil means insert new headings after the current subtree.
  942. When nil, the new heading is created directly after the current line.
  943. The commands \\[org-insert-heading-respect-content] and
  944. \\[org-insert-todo-heading-respect-content] turn this variable on
  945. for the duration of the command."
  946. :group 'org-structure
  947. :type 'boolean)
  948. (defcustom org-blank-before-new-entry '((heading . auto)
  949. (plain-list-item . auto))
  950. "Should `org-insert-heading' leave a blank line before new heading/item?
  951. The value is an alist, with `heading' and `plain-list-item' as car,
  952. and a boolean flag as cdr. The cdr may lso be the symbol `auto', and then
  953. Org will look at the surrounding headings/items and try to make an
  954. intelligent decision wether to insert a blank line or not.
  955. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  956. set, the setting here is ignored and no empty line is inserted, to avoid
  957. breaking the list structure."
  958. :group 'org-edit-structure
  959. :type '(list
  960. (cons (const heading)
  961. (choice (const :tag "Never" nil)
  962. (const :tag "Always" t)
  963. (const :tag "Auto" auto)))
  964. (cons (const plain-list-item)
  965. (choice (const :tag "Never" nil)
  966. (const :tag "Always" t)
  967. (const :tag "Auto" auto)))))
  968. (defcustom org-insert-heading-hook nil
  969. "Hook being run after inserting a new heading."
  970. :group 'org-edit-structure
  971. :type 'hook)
  972. (defcustom org-enable-fixed-width-editor t
  973. "Non-nil means lines starting with \":\" are treated as fixed-width.
  974. This currently only means they are never auto-wrapped.
  975. When nil, such lines will be treated like ordinary lines.
  976. See also the QUOTE keyword."
  977. :group 'org-edit-structure
  978. :type 'boolean)
  979. (defcustom org-goto-auto-isearch t
  980. "Non-nil means typing characters in `org-goto' starts incremental search."
  981. :group 'org-edit-structure
  982. :type 'boolean)
  983. (defgroup org-sparse-trees nil
  984. "Options concerning sparse trees in Org-mode."
  985. :tag "Org Sparse Trees"
  986. :group 'org-structure)
  987. (defcustom org-highlight-sparse-tree-matches t
  988. "Non-nil means highlight all matches that define a sparse tree.
  989. The highlights will automatically disappear the next time the buffer is
  990. changed by an edit command."
  991. :group 'org-sparse-trees
  992. :type 'boolean)
  993. (defcustom org-remove-highlights-with-change t
  994. "Non-nil means any change to the buffer will remove temporary highlights.
  995. Such highlights are created by `org-occur' and `org-clock-display'.
  996. When nil, `C-c C-c needs to be used to get rid of the highlights.
  997. The highlights created by `org-preview-latex-fragment' always need
  998. `C-c C-c' to be removed."
  999. :group 'org-sparse-trees
  1000. :group 'org-time
  1001. :type 'boolean)
  1002. (defcustom org-occur-hook '(org-first-headline-recenter)
  1003. "Hook that is run after `org-occur' has constructed a sparse tree.
  1004. This can be used to recenter the window to show as much of the structure
  1005. as possible."
  1006. :group 'org-sparse-trees
  1007. :type 'hook)
  1008. (defgroup org-imenu-and-speedbar nil
  1009. "Options concerning imenu and speedbar in Org-mode."
  1010. :tag "Org Imenu and Speedbar"
  1011. :group 'org-structure)
  1012. (defcustom org-imenu-depth 2
  1013. "The maximum level for Imenu access to Org-mode headlines.
  1014. This also applied for speedbar access."
  1015. :group 'org-imenu-and-speedbar
  1016. :type 'integer)
  1017. (defgroup org-table nil
  1018. "Options concerning tables in Org-mode."
  1019. :tag "Org Table"
  1020. :group 'org)
  1021. (defcustom org-enable-table-editor 'optimized
  1022. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1023. When nil, such lines will be treated like ordinary lines.
  1024. When equal to the symbol `optimized', the table editor will be optimized to
  1025. do the following:
  1026. - Automatic overwrite mode in front of whitespace in table fields.
  1027. This makes the structure of the table stay in tact as long as the edited
  1028. field does not exceed the column width.
  1029. - Minimize the number of realigns. Normally, the table is aligned each time
  1030. TAB or RET are pressed to move to another field. With optimization this
  1031. happens only if changes to a field might have changed the column width.
  1032. Optimization requires replacing the functions `self-insert-command',
  1033. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1034. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1035. very good at guessing when a re-align will be necessary, but you can always
  1036. force one with \\[org-ctrl-c-ctrl-c].
  1037. If you would like to use the optimized version in Org-mode, but the
  1038. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1039. This variable can be used to turn on and off the table editor during a session,
  1040. but in order to toggle optimization, a restart is required.
  1041. See also the variable `org-table-auto-blank-field'."
  1042. :group 'org-table
  1043. :type '(choice
  1044. (const :tag "off" nil)
  1045. (const :tag "on" t)
  1046. (const :tag "on, optimized" optimized)))
  1047. (defcustom org-self-insert-cluster-for-undo t
  1048. "Non-nil means cluster self-insert commands for undo when possible.
  1049. If this is set, then, like in the Emacs command loop, 20 consecutive
  1050. characters will be undone together.
  1051. This is configurable, because there is some impact on typing performance."
  1052. :group 'org-table
  1053. :type 'boolean)
  1054. (defcustom org-table-tab-recognizes-table.el t
  1055. "Non-nil means TAB will automatically notice a table.el table.
  1056. When it sees such a table, it moves point into it and - if necessary -
  1057. calls `table-recognize-table'."
  1058. :group 'org-table-editing
  1059. :type 'boolean)
  1060. (defgroup org-link nil
  1061. "Options concerning links in Org-mode."
  1062. :tag "Org Link"
  1063. :group 'org)
  1064. (defvar org-link-abbrev-alist-local nil
  1065. "Buffer-local version of `org-link-abbrev-alist', which see.
  1066. The value of this is taken from the #+LINK lines.")
  1067. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1068. (defcustom org-link-abbrev-alist nil
  1069. "Alist of link abbreviations.
  1070. The car of each element is a string, to be replaced at the start of a link.
  1071. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1072. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1073. [[linkkey:tag][description]]
  1074. The 'linkkey' must be a word word, starting with a letter, followed
  1075. by letters, numbers, '-' or '_'.
  1076. If REPLACE is a string, the tag will simply be appended to create the link.
  1077. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1078. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1079. be inserted at that point (see the function `url-hexify-string').
  1080. REPLACE may also be a function that will be called with the tag as the
  1081. only argument to create the link, which should be returned as a string.
  1082. See the manual for examples."
  1083. :group 'org-link
  1084. :type '(repeat
  1085. (cons
  1086. (string :tag "Protocol")
  1087. (choice
  1088. (string :tag "Format")
  1089. (function)))))
  1090. (defcustom org-descriptive-links t
  1091. "Non-nil means hide link part and only show description of bracket links.
  1092. Bracket links are like [[link][description]]. This variable sets the initial
  1093. state in new org-mode buffers. The setting can then be toggled on a
  1094. per-buffer basis from the Org->Hyperlinks menu."
  1095. :group 'org-link
  1096. :type 'boolean)
  1097. (defcustom org-link-file-path-type 'adaptive
  1098. "How the path name in file links should be stored.
  1099. Valid values are:
  1100. relative Relative to the current directory, i.e. the directory of the file
  1101. into which the link is being inserted.
  1102. absolute Absolute path, if possible with ~ for home directory.
  1103. noabbrev Absolute path, no abbreviation of home directory.
  1104. adaptive Use relative path for files in the current directory and sub-
  1105. directories of it. For other files, use an absolute path."
  1106. :group 'org-link
  1107. :type '(choice
  1108. (const relative)
  1109. (const absolute)
  1110. (const noabbrev)
  1111. (const adaptive)))
  1112. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1113. "Types of links that should be activated in Org-mode files.
  1114. This is a list of symbols, each leading to the activation of a certain link
  1115. type. In principle, it does not hurt to turn on most link types - there may
  1116. be a small gain when turning off unused link types. The types are:
  1117. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1118. angle Links in angular brackets that may contain whitespace like
  1119. <bbdb:Carsten Dominik>.
  1120. plain Plain links in normal text, no whitespace, like http://google.com.
  1121. radio Text that is matched by a radio target, see manual for details.
  1122. tag Tag settings in a headline (link to tag search).
  1123. date Time stamps (link to calendar).
  1124. footnote Footnote labels.
  1125. Changing this variable requires a restart of Emacs to become effective."
  1126. :group 'org-link
  1127. :type '(set :greedy t
  1128. (const :tag "Double bracket links" bracket)
  1129. (const :tag "Angular bracket links" angle)
  1130. (const :tag "Plain text links" plain)
  1131. (const :tag "Radio target matches" radio)
  1132. (const :tag "Tags" tag)
  1133. (const :tag "Timestamps" date)
  1134. (const :tag "Footnotes" footnote)))
  1135. (defcustom org-make-link-description-function nil
  1136. "Function to use to generate link descriptions from links.
  1137. If nil the link location will be used. This function must take
  1138. two parameters; the first is the link and the second the
  1139. description `org-insert-link' has generated, and should return the
  1140. description to use."
  1141. :group 'org-link
  1142. :type 'function)
  1143. (defgroup org-link-store nil
  1144. "Options concerning storing links in Org-mode."
  1145. :tag "Org Store Link"
  1146. :group 'org-link)
  1147. (defcustom org-email-link-description-format "Email %c: %.30s"
  1148. "Format of the description part of a link to an email or usenet message.
  1149. The following %-escapes will be replaced by corresponding information:
  1150. %F full \"From\" field
  1151. %f name, taken from \"From\" field, address if no name
  1152. %T full \"To\" field
  1153. %t first name in \"To\" field, address if no name
  1154. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1155. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1156. %s subject
  1157. %m message-id.
  1158. You may use normal field width specification between the % and the letter.
  1159. This is for example useful to limit the length of the subject.
  1160. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1161. :group 'org-link-store
  1162. :type 'string)
  1163. (defcustom org-from-is-user-regexp
  1164. (let (r1 r2)
  1165. (when (and user-mail-address (not (string= user-mail-address "")))
  1166. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1167. (when (and user-full-name (not (string= user-full-name "")))
  1168. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1169. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1170. "Regexp matched against the \"From:\" header of an email or usenet message.
  1171. It should match if the message is from the user him/herself."
  1172. :group 'org-link-store
  1173. :type 'regexp)
  1174. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1175. "Non-nil means storing a link to an Org file will use entry IDs.
  1176. Note that before this variable is even considered, org-id must be loaded,
  1177. so please customize `org-modules' and turn it on.
  1178. The variable can have the following values:
  1179. t Create an ID if needed to make a link to the current entry.
  1180. create-if-interactive
  1181. If `org-store-link' is called directly (interactively, as a user
  1182. command), do create an ID to support the link. But when doing the
  1183. job for remember, only use the ID if it already exists. The
  1184. purpose of this setting is to avoid proliferation of unwanted
  1185. IDs, just because you happen to be in an Org file when you
  1186. call `org-remember' that automatically and preemptively
  1187. creates a link. If you do want to get an ID link in a remember
  1188. template to an entry not having an ID, create it first by
  1189. explicitly creating a link to it, using `C-c C-l' first.
  1190. create-if-interactive-and-no-custom-id
  1191. Like create-if-interactive, but do not create an ID if there is
  1192. a CUSTOM_ID property defined in the entry. This is the default.
  1193. use-existing
  1194. Use existing ID, do not create one.
  1195. nil Never use an ID to make a link, instead link using a text search for
  1196. the headline text."
  1197. :group 'org-link-store
  1198. :type '(choice
  1199. (const :tag "Create ID to make link" t)
  1200. (const :tag "Create if storing link interactively"
  1201. create-if-interactive)
  1202. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1203. create-if-interactive-and-no-custom-id)
  1204. (const :tag "Only use existing" use-existing)
  1205. (const :tag "Do not use ID to create link" nil)))
  1206. (defcustom org-context-in-file-links t
  1207. "Non-nil means file links from `org-store-link' contain context.
  1208. A search string will be added to the file name with :: as separator and
  1209. used to find the context when the link is activated by the command
  1210. `org-open-at-point'. When this option is t, the entire active region
  1211. will be placed in the search string of the file link. If set to a
  1212. positive integer, only the first n lines of context will be stored.
  1213. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1214. negates this setting for the duration of the command."
  1215. :group 'org-link-store
  1216. :type '(choice boolean integer))
  1217. (defcustom org-keep-stored-link-after-insertion nil
  1218. "Non-nil means keep link in list for entire session.
  1219. The command `org-store-link' adds a link pointing to the current
  1220. location to an internal list. These links accumulate during a session.
  1221. The command `org-insert-link' can be used to insert links into any
  1222. Org-mode file (offering completion for all stored links). When this
  1223. option is nil, every link which has been inserted once using \\[org-insert-link]
  1224. will be removed from the list, to make completing the unused links
  1225. more efficient."
  1226. :group 'org-link-store
  1227. :type 'boolean)
  1228. (defgroup org-link-follow nil
  1229. "Options concerning following links in Org-mode."
  1230. :tag "Org Follow Link"
  1231. :group 'org-link)
  1232. (defcustom org-link-translation-function nil
  1233. "Function to translate links with different syntax to Org syntax.
  1234. This can be used to translate links created for example by the Planner
  1235. or emacs-wiki packages to Org syntax.
  1236. The function must accept two parameters, a TYPE containing the link
  1237. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1238. which is everything after the link protocol. It should return a cons
  1239. with possibly modified values of type and path.
  1240. Org contains a function for this, so if you set this variable to
  1241. `org-translate-link-from-planner', you should be able follow many
  1242. links created by planner."
  1243. :group 'org-link-follow
  1244. :type 'function)
  1245. (defcustom org-follow-link-hook nil
  1246. "Hook that is run after a link has been followed."
  1247. :group 'org-link-follow
  1248. :type 'hook)
  1249. (defcustom org-tab-follows-link nil
  1250. "Non-nil means on links TAB will follow the link.
  1251. Needs to be set before org.el is loaded.
  1252. This really should not be used, it does not make sense, and the
  1253. implementation is bad."
  1254. :group 'org-link-follow
  1255. :type 'boolean)
  1256. (defcustom org-return-follows-link nil
  1257. "Non-nil means on links RET will follow the link."
  1258. :group 'org-link-follow
  1259. :type 'boolean)
  1260. (defcustom org-mouse-1-follows-link
  1261. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1262. "Non-nil means mouse-1 on a link will follow the link.
  1263. A longer mouse click will still set point. Does not work on XEmacs.
  1264. Needs to be set before org.el is loaded."
  1265. :group 'org-link-follow
  1266. :type 'boolean)
  1267. (defcustom org-mark-ring-length 4
  1268. "Number of different positions to be recorded in the ring.
  1269. Changing this requires a restart of Emacs to work correctly."
  1270. :group 'org-link-follow
  1271. :type 'integer)
  1272. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1273. "Non-nil means internal links in Org files must exactly match a headline.
  1274. When nil, the link search tries to match a phrase will all words
  1275. in the search text."
  1276. :group 'org-link-follow
  1277. :type '(choice
  1278. (const :tag "Use fuzy text search" nil)
  1279. (const :tag "Match only exact headline" t)
  1280. (const :tag "Match extact headline or query to create it"
  1281. query-to-create)))
  1282. (defcustom org-link-frame-setup
  1283. '((vm . vm-visit-folder-other-frame)
  1284. (gnus . org-gnus-no-new-news)
  1285. (file . find-file-other-window)
  1286. (wl . wl-other-frame))
  1287. "Setup the frame configuration for following links.
  1288. When following a link with Emacs, it may often be useful to display
  1289. this link in another window or frame. This variable can be used to
  1290. set this up for the different types of links.
  1291. For VM, use any of
  1292. `vm-visit-folder'
  1293. `vm-visit-folder-other-frame'
  1294. For Gnus, use any of
  1295. `gnus'
  1296. `gnus-other-frame'
  1297. `org-gnus-no-new-news'
  1298. For FILE, use any of
  1299. `find-file'
  1300. `find-file-other-window'
  1301. `find-file-other-frame'
  1302. For Wanderlust use any of
  1303. `wl'
  1304. `wl-other-frame'
  1305. For the calendar, use the variable `calendar-setup'.
  1306. For BBDB, it is currently only possible to display the matches in
  1307. another window."
  1308. :group 'org-link-follow
  1309. :type '(list
  1310. (cons (const vm)
  1311. (choice
  1312. (const vm-visit-folder)
  1313. (const vm-visit-folder-other-window)
  1314. (const vm-visit-folder-other-frame)))
  1315. (cons (const gnus)
  1316. (choice
  1317. (const gnus)
  1318. (const gnus-other-frame)
  1319. (const org-gnus-no-new-news)))
  1320. (cons (const file)
  1321. (choice
  1322. (const find-file)
  1323. (const find-file-other-window)
  1324. (const find-file-other-frame)))
  1325. (cons (const wl)
  1326. (choice
  1327. (const wl)
  1328. (const wl-other-frame)))))
  1329. (defcustom org-display-internal-link-with-indirect-buffer nil
  1330. "Non-nil means use indirect buffer to display infile links.
  1331. Activating internal links (from one location in a file to another location
  1332. in the same file) normally just jumps to the location. When the link is
  1333. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1334. is displayed in
  1335. another window. When this option is set, the other window actually displays
  1336. an indirect buffer clone of the current buffer, to avoid any visibility
  1337. changes to the current buffer."
  1338. :group 'org-link-follow
  1339. :type 'boolean)
  1340. (defcustom org-open-non-existing-files nil
  1341. "Non-nil means `org-open-file' will open non-existing files.
  1342. When nil, an error will be generated.
  1343. This variable applies only to external applications because they
  1344. might choke on non-existing files. If the link is to a file that
  1345. will be opened in Emacs, the variable is ignored."
  1346. :group 'org-link-follow
  1347. :type 'boolean)
  1348. (defcustom org-open-directory-means-index-dot-org nil
  1349. "Non-nil means a link to a directory really means to index.org.
  1350. When nil, following a directory link will run dired or open a finder/explorer
  1351. window on that directory."
  1352. :group 'org-link-follow
  1353. :type 'boolean)
  1354. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1355. "Function and arguments to call for following mailto links.
  1356. This is a list with the first element being a Lisp function, and the
  1357. remaining elements being arguments to the function. In string arguments,
  1358. %a will be replaced by the address, and %s will be replaced by the subject
  1359. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1360. :group 'org-link-follow
  1361. :type '(choice
  1362. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1363. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1364. (const :tag "message-mail" (message-mail "%a" "%s"))
  1365. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1366. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1367. "Non-nil means ask for confirmation before executing shell links.
  1368. Shell links can be dangerous: just think about a link
  1369. [[shell:rm -rf ~/*][Google Search]]
  1370. This link would show up in your Org-mode document as \"Google Search\",
  1371. but really it would remove your entire home directory.
  1372. Therefore we advise against setting this variable to nil.
  1373. Just change it to `y-or-n-p' if you want to confirm with a
  1374. single keystroke rather than having to type \"yes\"."
  1375. :group 'org-link-follow
  1376. :type '(choice
  1377. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1378. (const :tag "with y-or-n (faster)" y-or-n-p)
  1379. (const :tag "no confirmation (dangerous)" nil)))
  1380. (put 'org-confirm-shell-link-function
  1381. 'safe-local-variable
  1382. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1383. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1384. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1385. Elisp links can be dangerous: just think about a link
  1386. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1387. This link would show up in your Org-mode document as \"Google Search\",
  1388. but really it would remove your entire home directory.
  1389. Therefore we advise against setting this variable to nil.
  1390. Just change it to `y-or-n-p' if you want to confirm with a
  1391. single keystroke rather than having to type \"yes\"."
  1392. :group 'org-link-follow
  1393. :type '(choice
  1394. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1395. (const :tag "with y-or-n (faster)" y-or-n-p)
  1396. (const :tag "no confirmation (dangerous)" nil)))
  1397. (put 'org-confirm-shell-link-function
  1398. 'safe-local-variable
  1399. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1400. (defconst org-file-apps-defaults-gnu
  1401. '((remote . emacs)
  1402. (system . mailcap)
  1403. (t . mailcap))
  1404. "Default file applications on a UNIX or GNU/Linux system.
  1405. See `org-file-apps'.")
  1406. (defconst org-file-apps-defaults-macosx
  1407. '((remote . emacs)
  1408. (t . "open %s")
  1409. (system . "open %s")
  1410. ("ps.gz" . "gv %s")
  1411. ("eps.gz" . "gv %s")
  1412. ("dvi" . "xdvi %s")
  1413. ("fig" . "xfig %s"))
  1414. "Default file applications on a MacOS X system.
  1415. The system \"open\" is known as a default, but we use X11 applications
  1416. for some files for which the OS does not have a good default.
  1417. See `org-file-apps'.")
  1418. (defconst org-file-apps-defaults-windowsnt
  1419. (list
  1420. '(remote . emacs)
  1421. (cons t
  1422. (list (if (featurep 'xemacs)
  1423. 'mswindows-shell-execute
  1424. 'w32-shell-execute)
  1425. "open" 'file))
  1426. (cons 'system
  1427. (list (if (featurep 'xemacs)
  1428. 'mswindows-shell-execute
  1429. 'w32-shell-execute)
  1430. "open" 'file)))
  1431. "Default file applications on a Windows NT system.
  1432. The system \"open\" is used for most files.
  1433. See `org-file-apps'.")
  1434. (defcustom org-file-apps
  1435. '(
  1436. (auto-mode . emacs)
  1437. ("\\.mm\\'" . default)
  1438. ("\\.x?html?\\'" . default)
  1439. ("\\.pdf\\'" . default)
  1440. )
  1441. "External applications for opening `file:path' items in a document.
  1442. Org-mode uses system defaults for different file types, but
  1443. you can use this variable to set the application for a given file
  1444. extension. The entries in this list are cons cells where the car identifies
  1445. files and the cdr the corresponding command. Possible values for the
  1446. file identifier are
  1447. \"string\" A string as a file identifier can be interpreted in different
  1448. ways, depending on its contents:
  1449. - Alphanumeric characters only:
  1450. Match links with this file extension.
  1451. Example: (\"pdf\" . \"evince %s\")
  1452. to open PDFs with evince.
  1453. - Regular expression: Match links where the
  1454. filename matches the regexp. If you want to
  1455. use groups here, use shy groups.
  1456. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1457. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1458. to open *.html and *.xhtml with firefox.
  1459. - Regular expression which contains (non-shy) groups:
  1460. Match links where the whole link, including \"::\", and
  1461. anything after that, matches the regexp.
  1462. In a custom command string, %1, %2, etc. are replaced with
  1463. the parts of the link that were matched by the groups.
  1464. For backwards compatibility, if a command string is given
  1465. that does not use any of the group matches, this case is
  1466. handled identically to the second one (i.e. match against
  1467. file name only).
  1468. In a custom lisp form, you can access the group matches with
  1469. (match-string n link).
  1470. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1471. to open [[file:document.pdf::5]] with evince at page 5.
  1472. `directory' Matches a directory
  1473. `remote' Matches a remote file, accessible through tramp or efs.
  1474. Remote files most likely should be visited through Emacs
  1475. because external applications cannot handle such paths.
  1476. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1477. so all files Emacs knows how to handle. Using this with
  1478. command `emacs' will open most files in Emacs. Beware that this
  1479. will also open html files inside Emacs, unless you add
  1480. (\"html\" . default) to the list as well.
  1481. t Default for files not matched by any of the other options.
  1482. `system' The system command to open files, like `open' on Windows
  1483. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1484. that will be selected if you call `C-c C-o' with a double
  1485. \\[universal-argument] \\[universal-argument] prefix.
  1486. Possible values for the command are:
  1487. `emacs' The file will be visited by the current Emacs process.
  1488. `default' Use the default application for this file type, which is the
  1489. association for t in the list, most likely in the system-specific
  1490. part.
  1491. This can be used to overrule an unwanted setting in the
  1492. system-specific variable.
  1493. `system' Use the system command for opening files, like \"open\".
  1494. This command is specified by the entry whose car is `system'.
  1495. Most likely, the system-specific version of this variable
  1496. does define this command, but you can overrule/replace it
  1497. here.
  1498. string A command to be executed by a shell; %s will be replaced
  1499. by the path to the file.
  1500. sexp A Lisp form which will be evaluated. The file path will
  1501. be available in the Lisp variable `file'.
  1502. For more examples, see the system specific constants
  1503. `org-file-apps-defaults-macosx'
  1504. `org-file-apps-defaults-windowsnt'
  1505. `org-file-apps-defaults-gnu'."
  1506. :group 'org-link-follow
  1507. :type '(repeat
  1508. (cons (choice :value ""
  1509. (string :tag "Extension")
  1510. (const :tag "System command to open files" system)
  1511. (const :tag "Default for unrecognized files" t)
  1512. (const :tag "Remote file" remote)
  1513. (const :tag "Links to a directory" directory)
  1514. (const :tag "Any files that have Emacs modes"
  1515. auto-mode))
  1516. (choice :value ""
  1517. (const :tag "Visit with Emacs" emacs)
  1518. (const :tag "Use default" default)
  1519. (const :tag "Use the system command" system)
  1520. (string :tag "Command")
  1521. (sexp :tag "Lisp form")))))
  1522. (defgroup org-refile nil
  1523. "Options concerning refiling entries in Org-mode."
  1524. :tag "Org Refile"
  1525. :group 'org)
  1526. (defcustom org-directory "~/org"
  1527. "Directory with org files.
  1528. This is just a default location to look for Org files. There is no need
  1529. at all to put your files into this directory. It is only used in the
  1530. following situations:
  1531. 1. When a remember template specifies a target file that is not an
  1532. absolute path. The path will then be interpreted relative to
  1533. `org-directory'
  1534. 2. When a remember note is filed away in an interactive way (when exiting the
  1535. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1536. with `org-directory' as the default path."
  1537. :group 'org-refile
  1538. :group 'org-remember
  1539. :type 'directory)
  1540. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1541. "Default target for storing notes.
  1542. Used as a fall back file for org-remember.el and org-capture.el, for
  1543. templates that do not specify a target file."
  1544. :group 'org-refile
  1545. :group 'org-remember
  1546. :type '(choice
  1547. (const :tag "Default from remember-data-file" nil)
  1548. file))
  1549. (defcustom org-goto-interface 'outline
  1550. "The default interface to be used for `org-goto'.
  1551. Allowed values are:
  1552. outline The interface shows an outline of the relevant file
  1553. and the correct heading is found by moving through
  1554. the outline or by searching with incremental search.
  1555. outline-path-completion Headlines in the current buffer are offered via
  1556. completion. This is the interface also used by
  1557. the refile command."
  1558. :group 'org-refile
  1559. :type '(choice
  1560. (const :tag "Outline" outline)
  1561. (const :tag "Outline-path-completion" outline-path-completion)))
  1562. (defcustom org-goto-max-level 5
  1563. "Maximum target level when running `org-goto' with refile interface."
  1564. :group 'org-refile
  1565. :type 'integer)
  1566. (defcustom org-reverse-note-order nil
  1567. "Non-nil means store new notes at the beginning of a file or entry.
  1568. When nil, new notes will be filed to the end of a file or entry.
  1569. This can also be a list with cons cells of regular expressions that
  1570. are matched against file names, and values."
  1571. :group 'org-remember
  1572. :group 'org-refile
  1573. :type '(choice
  1574. (const :tag "Reverse always" t)
  1575. (const :tag "Reverse never" nil)
  1576. (repeat :tag "By file name regexp"
  1577. (cons regexp boolean))))
  1578. (defcustom org-log-refile nil
  1579. "Information to record when a task is refiled.
  1580. Possible values are:
  1581. nil Don't add anything
  1582. time Add a time stamp to the task
  1583. note Prompt for a note and add it with template `org-log-note-headings'
  1584. This option can also be set with on a per-file-basis with
  1585. #+STARTUP: nologrefile
  1586. #+STARTUP: logrefile
  1587. #+STARTUP: lognoterefile
  1588. You can have local logging settings for a subtree by setting the LOGGING
  1589. property to one or more of these keywords.
  1590. When bulk-refiling from the agenda, the value `note' is forbidden and
  1591. will temporarily be changed to `time'."
  1592. :group 'org-refile
  1593. :group 'org-progress
  1594. :type '(choice
  1595. (const :tag "No logging" nil)
  1596. (const :tag "Record timestamp" time)
  1597. (const :tag "Record timestamp with note." note)))
  1598. (defcustom org-refile-targets nil
  1599. "Targets for refiling entries with \\[org-refile].
  1600. This is list of cons cells. Each cell contains:
  1601. - a specification of the files to be considered, either a list of files,
  1602. or a symbol whose function or variable value will be used to retrieve
  1603. a file name or a list of file names. If you use `org-agenda-files' for
  1604. that, all agenda files will be scanned for targets. Nil means consider
  1605. headings in the current buffer.
  1606. - A specification of how to find candidate refile targets. This may be
  1607. any of:
  1608. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1609. This tag has to be present in all target headlines, inheritance will
  1610. not be considered.
  1611. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1612. todo keyword.
  1613. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1614. headlines that are refiling targets.
  1615. - a cons cell (:level . N). Any headline of level N is considered a target.
  1616. Note that, when `org-odd-levels-only' is set, level corresponds to
  1617. order in hierarchy, not to the number of stars.
  1618. - a cons cell (:maxlevel . N). Any headline with level <= N is 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. You can set the variable `org-refile-target-verify-function' to a function
  1622. to verify each headline found by the simple criteria above.
  1623. When this variable is nil, all top-level headlines in the current buffer
  1624. are used, equivalent to the value `((nil . (:level . 1))'."
  1625. :group 'org-refile
  1626. :type '(repeat
  1627. (cons
  1628. (choice :value org-agenda-files
  1629. (const :tag "All agenda files" org-agenda-files)
  1630. (const :tag "Current buffer" nil)
  1631. (function) (variable) (file))
  1632. (choice :tag "Identify target headline by"
  1633. (cons :tag "Specific tag" (const :value :tag) (string))
  1634. (cons :tag "TODO keyword" (const :value :todo) (string))
  1635. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1636. (cons :tag "Level number" (const :value :level) (integer))
  1637. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1638. (defcustom org-refile-target-verify-function nil
  1639. "Function to verify if the headline at point should be a refile target.
  1640. The function will be called without arguments, with point at the
  1641. beginning of the headline. It should return t and leave point
  1642. where it is if the headline is a valid target for refiling.
  1643. If the target should not be selected, the function must return nil.
  1644. In addition to this, it may move point to a place from where the search
  1645. should be continued. For example, the function may decide that the entire
  1646. subtree of the current entry should be excluded and move point to the end
  1647. of the subtree."
  1648. :group 'org-refile
  1649. :type 'function)
  1650. (defcustom org-refile-use-cache nil
  1651. "Non-nil means cache refile targets to speed up the process.
  1652. The cache for a particular file will be updated automatically when
  1653. the buffer has been killed, or when any of the marker used for flagging
  1654. refile targets no longer points at a live buffer.
  1655. If you have added new entries to a buffer that might themselves be targets,
  1656. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1657. find that easier, `C-u C-u C-u C-c C-w'."
  1658. :group 'org-refile
  1659. :type 'boolean)
  1660. (defcustom org-refile-use-outline-path nil
  1661. "Non-nil means provide refile targets as paths.
  1662. So a level 3 headline will be available as level1/level2/level3.
  1663. When the value is `file', also include the file name (without directory)
  1664. into the path. In this case, you can also stop the completion after
  1665. the file name, to get entries inserted as top level in the file.
  1666. When `full-file-path', include the full file path."
  1667. :group 'org-refile
  1668. :type '(choice
  1669. (const :tag "Not" nil)
  1670. (const :tag "Yes" t)
  1671. (const :tag "Start with file name" file)
  1672. (const :tag "Start with full file path" full-file-path)))
  1673. (defcustom org-outline-path-complete-in-steps t
  1674. "Non-nil means complete the outline path in hierarchical steps.
  1675. When Org-mode uses the refile interface to select an outline path
  1676. \(see variable `org-refile-use-outline-path'), the completion of
  1677. the path can be done is a single go, or if can be done in steps down
  1678. the headline hierarchy. Going in steps is probably the best if you
  1679. do not use a special completion package like `ido' or `icicles'.
  1680. However, when using these packages, going in one step can be very
  1681. fast, while still showing the whole path to the entry."
  1682. :group 'org-refile
  1683. :type 'boolean)
  1684. (defcustom org-refile-allow-creating-parent-nodes nil
  1685. "Non-nil means allow to create new nodes as refile targets.
  1686. New nodes are then created by adding \"/new node name\" to the completion
  1687. of an existing node. When the value of this variable is `confirm',
  1688. new node creation must be confirmed by the user (recommended)
  1689. When nil, the completion must match an existing entry.
  1690. Note that, if the new heading is not seen by the criteria
  1691. listed in `org-refile-targets', multiple instances of the same
  1692. heading would be created by trying again to file under the new
  1693. heading."
  1694. :group 'org-refile
  1695. :type '(choice
  1696. (const :tag "Never" nil)
  1697. (const :tag "Always" t)
  1698. (const :tag "Prompt for confirmation" confirm)))
  1699. (defgroup org-todo nil
  1700. "Options concerning TODO items in Org-mode."
  1701. :tag "Org TODO"
  1702. :group 'org)
  1703. (defgroup org-progress nil
  1704. "Options concerning Progress logging in Org-mode."
  1705. :tag "Org Progress"
  1706. :group 'org-time)
  1707. (defvar org-todo-interpretation-widgets
  1708. '(
  1709. (:tag "Sequence (cycling hits every state)" sequence)
  1710. (:tag "Type (cycling directly to DONE)" type))
  1711. "The available interpretation symbols for customizing `org-todo-keywords'.
  1712. Interested libraries should add to this list.")
  1713. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1714. "List of TODO entry keyword sequences and their interpretation.
  1715. \\<org-mode-map>This is a list of sequences.
  1716. Each sequence starts with a symbol, either `sequence' or `type',
  1717. indicating if the keywords should be interpreted as a sequence of
  1718. action steps, or as different types of TODO items. The first
  1719. keywords are states requiring action - these states will select a headline
  1720. for inclusion into the global TODO list Org-mode produces. If one of
  1721. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1722. signify that no further action is necessary. If \"|\" is not found,
  1723. the last keyword is treated as the only DONE state of the sequence.
  1724. The command \\[org-todo] cycles an entry through these states, and one
  1725. additional state where no keyword is present. For details about this
  1726. cycling, see the manual.
  1727. TODO keywords and interpretation can also be set on a per-file basis with
  1728. the special #+SEQ_TODO and #+TYP_TODO lines.
  1729. Each keyword can optionally specify a character for fast state selection
  1730. \(in combination with the variable `org-use-fast-todo-selection')
  1731. and specifiers for state change logging, using the same syntax
  1732. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1733. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1734. indicates to record a time stamp each time this state is selected.
  1735. Each keyword may also specify if a timestamp or a note should be
  1736. recorded when entering or leaving the state, by adding additional
  1737. characters in the parenthesis after the keyword. This looks like this:
  1738. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1739. record only the time of the state change. With X and Y being either
  1740. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1741. Y when leaving the state if and only if the *target* state does not
  1742. define X. You may omit any of the fast-selection key or X or /Y,
  1743. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1744. For backward compatibility, this variable may also be just a list
  1745. of keywords - in this case the interpretation (sequence or type) will be
  1746. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1747. :group 'org-todo
  1748. :group 'org-keywords
  1749. :type '(choice
  1750. (repeat :tag "Old syntax, just keywords"
  1751. (string :tag "Keyword"))
  1752. (repeat :tag "New syntax"
  1753. (cons
  1754. (choice
  1755. :tag "Interpretation"
  1756. ;;Quick and dirty way to see
  1757. ;;`org-todo-interpretations'. This takes the
  1758. ;;place of item arguments
  1759. :convert-widget
  1760. (lambda (widget)
  1761. (widget-put widget
  1762. :args (mapcar
  1763. #'(lambda (x)
  1764. (widget-convert
  1765. (cons 'const x)))
  1766. org-todo-interpretation-widgets))
  1767. widget))
  1768. (repeat
  1769. (string :tag "Keyword"))))))
  1770. (defvar org-todo-keywords-1 nil
  1771. "All TODO and DONE keywords active in a buffer.")
  1772. (make-variable-buffer-local 'org-todo-keywords-1)
  1773. (defvar org-todo-keywords-for-agenda nil)
  1774. (defvar org-done-keywords-for-agenda nil)
  1775. (defvar org-drawers-for-agenda nil)
  1776. (defvar org-todo-keyword-alist-for-agenda nil)
  1777. (defvar org-tag-alist-for-agenda nil)
  1778. (defvar org-agenda-contributing-files nil)
  1779. (defvar org-not-done-keywords nil)
  1780. (make-variable-buffer-local 'org-not-done-keywords)
  1781. (defvar org-done-keywords nil)
  1782. (make-variable-buffer-local 'org-done-keywords)
  1783. (defvar org-todo-heads nil)
  1784. (make-variable-buffer-local 'org-todo-heads)
  1785. (defvar org-todo-sets nil)
  1786. (make-variable-buffer-local 'org-todo-sets)
  1787. (defvar org-todo-log-states nil)
  1788. (make-variable-buffer-local 'org-todo-log-states)
  1789. (defvar org-todo-kwd-alist nil)
  1790. (make-variable-buffer-local 'org-todo-kwd-alist)
  1791. (defvar org-todo-key-alist nil)
  1792. (make-variable-buffer-local 'org-todo-key-alist)
  1793. (defvar org-todo-key-trigger nil)
  1794. (make-variable-buffer-local 'org-todo-key-trigger)
  1795. (defcustom org-todo-interpretation 'sequence
  1796. "Controls how TODO keywords are interpreted.
  1797. This variable is in principle obsolete and is only used for
  1798. backward compatibility, if the interpretation of todo keywords is
  1799. not given already in `org-todo-keywords'. See that variable for
  1800. more information."
  1801. :group 'org-todo
  1802. :group 'org-keywords
  1803. :type '(choice (const sequence)
  1804. (const type)))
  1805. (defcustom org-use-fast-todo-selection t
  1806. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1807. This variable describes if and under what circumstances the cycling
  1808. mechanism for TODO keywords will be replaced by a single-key, direct
  1809. selection scheme.
  1810. When nil, fast selection is never used.
  1811. When the symbol `prefix', it will be used when `org-todo' is called with
  1812. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1813. in an agenda buffer.
  1814. When t, fast selection is used by default. In this case, the prefix
  1815. argument forces cycling instead.
  1816. In all cases, the special interface is only used if access keys have actually
  1817. been assigned by the user, i.e. if keywords in the configuration are followed
  1818. by a letter in parenthesis, like TODO(t)."
  1819. :group 'org-todo
  1820. :type '(choice
  1821. (const :tag "Never" nil)
  1822. (const :tag "By default" t)
  1823. (const :tag "Only with C-u C-c C-t" prefix)))
  1824. (defcustom org-provide-todo-statistics t
  1825. "Non-nil means update todo statistics after insert and toggle.
  1826. ALL-HEADLINES means update todo statistics by including headlines
  1827. with no TODO keyword as well, counting them as not done.
  1828. A list of TODO keywords means the same, but skip keywords that are
  1829. not in this list.
  1830. When this is set, todo statistics is updated in the parent of the
  1831. current entry each time a todo state is changed."
  1832. :group 'org-todo
  1833. :type '(choice
  1834. (const :tag "Yes, only for TODO entries" t)
  1835. (const :tag "Yes, including all entries" 'all-headlines)
  1836. (repeat :tag "Yes, for TODOs in this list"
  1837. (string :tag "TODO keyword"))
  1838. (other :tag "No TODO statistics" nil)))
  1839. (defcustom org-hierarchical-todo-statistics t
  1840. "Non-nil means TODO statistics covers just direct children.
  1841. When nil, all entries in the subtree are considered.
  1842. This has only an effect if `org-provide-todo-statistics' is set.
  1843. To set this to nil for only a single subtree, use a COOKIE_DATA
  1844. property and include the word \"recursive\" into the value."
  1845. :group 'org-todo
  1846. :type 'boolean)
  1847. (defcustom org-after-todo-state-change-hook nil
  1848. "Hook which is run after the state of a TODO item was changed.
  1849. The new state (a string with a TODO keyword, or nil) is available in the
  1850. Lisp variable `state'."
  1851. :group 'org-todo
  1852. :type 'hook)
  1853. (defvar org-blocker-hook nil
  1854. "Hook for functions that are allowed to block a state change.
  1855. Each function gets as its single argument a property list, see
  1856. `org-trigger-hook' for more information about this list.
  1857. If any of the functions in this hook returns nil, the state change
  1858. is blocked.")
  1859. (defvar org-trigger-hook nil
  1860. "Hook for functions that are triggered by a state change.
  1861. Each function gets as its single argument a property list with at least
  1862. the following elements:
  1863. (:type type-of-change :position pos-at-entry-start
  1864. :from old-state :to new-state)
  1865. Depending on the type, more properties may be present.
  1866. This mechanism is currently implemented for:
  1867. TODO state changes
  1868. ------------------
  1869. :type todo-state-change
  1870. :from previous state (keyword as a string), or nil, or a symbol
  1871. 'todo' or 'done', to indicate the general type of state.
  1872. :to new state, like in :from")
  1873. (defcustom org-enforce-todo-dependencies nil
  1874. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1875. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1876. be blocked if any prior sibling is not yet done.
  1877. Finally, if the parent is blocked because of ordered siblings of its own,
  1878. the child will also be blocked.
  1879. This variable needs to be set before org.el is loaded, and you need to
  1880. restart Emacs after a change to make the change effective. The only way
  1881. to change is while Emacs is running is through the customize interface."
  1882. :set (lambda (var val)
  1883. (set var val)
  1884. (if val
  1885. (add-hook 'org-blocker-hook
  1886. 'org-block-todo-from-children-or-siblings-or-parent)
  1887. (remove-hook 'org-blocker-hook
  1888. 'org-block-todo-from-children-or-siblings-or-parent)))
  1889. :group 'org-todo
  1890. :type 'boolean)
  1891. (defcustom org-enforce-todo-checkbox-dependencies nil
  1892. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1893. When this is nil, checkboxes have no influence on switching TODO states.
  1894. When non-nil, you first need to check off all check boxes before the TODO
  1895. entry can be switched to DONE.
  1896. This variable needs to be set before org.el is loaded, and you need to
  1897. restart Emacs after a change to make the change effective. The only way
  1898. to change is while Emacs is running is through the customize interface."
  1899. :set (lambda (var val)
  1900. (set var val)
  1901. (if val
  1902. (add-hook 'org-blocker-hook
  1903. 'org-block-todo-from-checkboxes)
  1904. (remove-hook 'org-blocker-hook
  1905. 'org-block-todo-from-checkboxes)))
  1906. :group 'org-todo
  1907. :type 'boolean)
  1908. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1909. "Non-nil means inserting a TODO heading is treated as state change.
  1910. So when the command \\[org-insert-todo-heading] is used, state change
  1911. logging will apply if appropriate. When nil, the new TODO item will
  1912. be inserted directly, and no logging will take place."
  1913. :group 'org-todo
  1914. :type 'boolean)
  1915. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1916. "Non-nil means switching TODO states with S-cursor counts as state change.
  1917. This is the default behavior. However, setting this to nil allows a
  1918. convenient way to select a TODO state and bypass any logging associated
  1919. with that."
  1920. :group 'org-todo
  1921. :type 'boolean)
  1922. (defcustom org-todo-state-tags-triggers nil
  1923. "Tag changes that should be triggered by TODO state changes.
  1924. This is a list. Each entry is
  1925. (state-change (tag . flag) .......)
  1926. State-change can be a string with a state, and empty string to indicate the
  1927. state that has no TODO keyword, or it can be one of the symbols `todo'
  1928. or `done', meaning any not-done or done state, respectively."
  1929. :group 'org-todo
  1930. :group 'org-tags
  1931. :type '(repeat
  1932. (cons (choice :tag "When changing to"
  1933. (const :tag "Not-done state" todo)
  1934. (const :tag "Done state" done)
  1935. (string :tag "State"))
  1936. (repeat
  1937. (cons :tag "Tag action"
  1938. (string :tag "Tag")
  1939. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1940. (defcustom org-log-done nil
  1941. "Information to record when a task moves to the DONE state.
  1942. Possible values are:
  1943. nil Don't add anything, just change the keyword
  1944. time Add a time stamp to the task
  1945. note Prompt for a note and add it with template `org-log-note-headings'
  1946. This option can also be set with on a per-file-basis with
  1947. #+STARTUP: nologdone
  1948. #+STARTUP: logdone
  1949. #+STARTUP: lognotedone
  1950. You can have local logging settings for a subtree by setting the LOGGING
  1951. property to one or more of these keywords."
  1952. :group 'org-todo
  1953. :group 'org-progress
  1954. :type '(choice
  1955. (const :tag "No logging" nil)
  1956. (const :tag "Record CLOSED timestamp" time)
  1957. (const :tag "Record CLOSED timestamp with note." note)))
  1958. ;; Normalize old uses of org-log-done.
  1959. (cond
  1960. ((eq org-log-done t) (setq org-log-done 'time))
  1961. ((and (listp org-log-done) (memq 'done org-log-done))
  1962. (setq org-log-done 'note)))
  1963. (defcustom org-log-reschedule nil
  1964. "Information to record when the scheduling date of a tasks is modified.
  1965. Possible values are:
  1966. nil Don't add anything, just change the date
  1967. time Add a time stamp to the task
  1968. note Prompt for a note and add it with template `org-log-note-headings'
  1969. This option can also be set with on a per-file-basis with
  1970. #+STARTUP: nologreschedule
  1971. #+STARTUP: logreschedule
  1972. #+STARTUP: lognotereschedule"
  1973. :group 'org-todo
  1974. :group 'org-progress
  1975. :type '(choice
  1976. (const :tag "No logging" nil)
  1977. (const :tag "Record timestamp" time)
  1978. (const :tag "Record timestamp with note." note)))
  1979. (defcustom org-log-redeadline nil
  1980. "Information to record when the deadline date of a tasks is modified.
  1981. Possible values are:
  1982. nil Don't add anything, just change the date
  1983. time Add a time stamp to the task
  1984. note Prompt for a note and add it with template `org-log-note-headings'
  1985. This option can also be set with on a per-file-basis with
  1986. #+STARTUP: nologredeadline
  1987. #+STARTUP: logredeadline
  1988. #+STARTUP: lognoteredeadline
  1989. You can have local logging settings for a subtree by setting the LOGGING
  1990. property to one or more of these keywords."
  1991. :group 'org-todo
  1992. :group 'org-progress
  1993. :type '(choice
  1994. (const :tag "No logging" nil)
  1995. (const :tag "Record timestamp" time)
  1996. (const :tag "Record timestamp with note." note)))
  1997. (defcustom org-log-note-clock-out nil
  1998. "Non-nil means record a note when clocking out of an item.
  1999. This can also be configured on a per-file basis by adding one of
  2000. the following lines anywhere in the buffer:
  2001. #+STARTUP: lognoteclock-out
  2002. #+STARTUP: nolognoteclock-out"
  2003. :group 'org-todo
  2004. :group 'org-progress
  2005. :type 'boolean)
  2006. (defcustom org-log-done-with-time t
  2007. "Non-nil means the CLOSED time stamp will contain date and time.
  2008. When nil, only the date will be recorded."
  2009. :group 'org-progress
  2010. :type 'boolean)
  2011. (defcustom org-log-note-headings
  2012. '((done . "CLOSING NOTE %t")
  2013. (state . "State %-12s from %-12S %t")
  2014. (note . "Note taken on %t")
  2015. (reschedule . "Rescheduled from %S on %t")
  2016. (delschedule . "Not scheduled, was %S on %t")
  2017. (redeadline . "New deadline from %S on %t")
  2018. (deldeadline . "Removed deadline, was %S on %t")
  2019. (refile . "Refiled on %t")
  2020. (clock-out . ""))
  2021. "Headings for notes added to entries.
  2022. The value is an alist, with the car being a symbol indicating the note
  2023. context, and the cdr is the heading to be used. The heading may also be the
  2024. empty string.
  2025. %t in the heading will be replaced by a time stamp.
  2026. %T will be an active time stamp instead the default inactive one
  2027. %s will be replaced by the new TODO state, in double quotes.
  2028. %S will be replaced by the old TODO state, in double quotes.
  2029. %u will be replaced by the user name.
  2030. %U will be replaced by the full user name.
  2031. In fact, it is not a good idea to change the `state' entry, because
  2032. agenda log mode depends on the format of these entries."
  2033. :group 'org-todo
  2034. :group 'org-progress
  2035. :type '(list :greedy t
  2036. (cons (const :tag "Heading when closing an item" done) string)
  2037. (cons (const :tag
  2038. "Heading when changing todo state (todo sequence only)"
  2039. state) string)
  2040. (cons (const :tag "Heading when just taking a note" note) string)
  2041. (cons (const :tag "Heading when clocking out" clock-out) string)
  2042. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2043. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2044. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2045. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2046. (cons (const :tag "Heading when refiling" refile) string)))
  2047. (unless (assq 'note org-log-note-headings)
  2048. (push '(note . "%t") org-log-note-headings))
  2049. (defcustom org-log-into-drawer nil
  2050. "Non-nil means insert state change notes and time stamps into a drawer.
  2051. When nil, state changes notes will be inserted after the headline and
  2052. any scheduling and clock lines, but not inside a drawer.
  2053. The value of this variable should be the name of the drawer to use.
  2054. LOGBOOK is proposed at the default drawer for this purpose, you can
  2055. also set this to a string to define the drawer of your choice.
  2056. A value of t is also allowed, representing \"LOGBOOK\".
  2057. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2058. will be ignored.
  2059. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2060. a subtree."
  2061. :group 'org-todo
  2062. :group 'org-progress
  2063. :type '(choice
  2064. (const :tag "Not into a drawer" nil)
  2065. (const :tag "LOGBOOK" t)
  2066. (string :tag "Other")))
  2067. (if (fboundp 'defvaralias)
  2068. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2069. (defun org-log-into-drawer ()
  2070. "Return the value of `org-log-into-drawer', but let properties overrule.
  2071. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2072. used instead of the default value."
  2073. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  2074. (cond
  2075. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2076. ((equal p "t") "LOGBOOK")
  2077. (t p))))
  2078. (defcustom org-log-state-notes-insert-after-drawers nil
  2079. "Non-nil means insert state change notes after any drawers in entry.
  2080. Only the drawers that *immediately* follow the headline and the
  2081. deadline/scheduled line are skipped.
  2082. When nil, insert notes right after the heading and perhaps the line
  2083. with deadline/scheduling if present.
  2084. This variable will have no effect if `org-log-into-drawer' is
  2085. set."
  2086. :group 'org-todo
  2087. :group 'org-progress
  2088. :type 'boolean)
  2089. (defcustom org-log-states-order-reversed t
  2090. "Non-nil means the latest state note will be directly after heading.
  2091. When nil, the state change notes will be ordered according to time."
  2092. :group 'org-todo
  2093. :group 'org-progress
  2094. :type 'boolean)
  2095. (defcustom org-todo-repeat-to-state nil
  2096. "The TODO state to which a repeater should return the repeating task.
  2097. By default this is the first task in a TODO sequence, or the previous state
  2098. in a TODO_TYP set. But you can specify another task here.
  2099. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2100. :group 'org-todo
  2101. :type '(choice (const :tag "Head of sequence" nil)
  2102. (string :tag "Specific state")))
  2103. (defcustom org-log-repeat 'time
  2104. "Non-nil means record moving through the DONE state when triggering repeat.
  2105. An auto-repeating task is immediately switched back to TODO when
  2106. marked DONE. If you are not logging state changes (by adding \"@\"
  2107. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2108. record a closing note, there will be no record of the task moving
  2109. through DONE. This variable forces taking a note anyway.
  2110. nil Don't force a record
  2111. time Record a time stamp
  2112. note Record a note
  2113. This option can also be set with on a per-file-basis with
  2114. #+STARTUP: logrepeat
  2115. #+STARTUP: lognoterepeat
  2116. #+STARTUP: nologrepeat
  2117. You can have local logging settings for a subtree by setting the LOGGING
  2118. property to one or more of these keywords."
  2119. :group 'org-todo
  2120. :group 'org-progress
  2121. :type '(choice
  2122. (const :tag "Don't force a record" nil)
  2123. (const :tag "Force recording the DONE state" time)
  2124. (const :tag "Force recording a note with the DONE state" note)))
  2125. (defgroup org-priorities nil
  2126. "Priorities in Org-mode."
  2127. :tag "Org Priorities"
  2128. :group 'org-todo)
  2129. (defcustom org-enable-priority-commands t
  2130. "Non-nil means priority commands are active.
  2131. When nil, these commands will be disabled, so that you never accidentally
  2132. set a priority."
  2133. :group 'org-priorities
  2134. :type 'boolean)
  2135. (defcustom org-highest-priority ?A
  2136. "The highest priority of TODO items. A character like ?A, ?B etc.
  2137. Must have a smaller ASCII number than `org-lowest-priority'."
  2138. :group 'org-priorities
  2139. :type 'character)
  2140. (defcustom org-lowest-priority ?C
  2141. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2142. Must have a larger ASCII number than `org-highest-priority'."
  2143. :group 'org-priorities
  2144. :type 'character)
  2145. (defcustom org-default-priority ?B
  2146. "The default priority of TODO items.
  2147. This is the priority an item get if no explicit priority is given."
  2148. :group 'org-priorities
  2149. :type 'character)
  2150. (defcustom org-priority-start-cycle-with-default t
  2151. "Non-nil means start with default priority when starting to cycle.
  2152. When this is nil, the first step in the cycle will be (depending on the
  2153. command used) one higher or lower that the default priority."
  2154. :group 'org-priorities
  2155. :type 'boolean)
  2156. (defcustom org-get-priority-function nil
  2157. "Function to extract the priority from a string.
  2158. The string is normally the headline. If this is nil Org computes the
  2159. priority from the priority cookie like [#A] in the headline. It returns
  2160. an integer, increasing by 1000 for each priority level.
  2161. The user can set a different function here, which should take a string
  2162. as an argument and return the numeric priority."
  2163. :group 'org-priorities
  2164. :type 'function)
  2165. (defgroup org-time nil
  2166. "Options concerning time stamps and deadlines in Org-mode."
  2167. :tag "Org Time"
  2168. :group 'org)
  2169. (defcustom org-insert-labeled-timestamps-at-point nil
  2170. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2171. When nil, these labeled time stamps are forces into the second line of an
  2172. entry, just after the headline. When scheduling from the global TODO list,
  2173. the time stamp will always be forced into the second line."
  2174. :group 'org-time
  2175. :type 'boolean)
  2176. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2177. "Formats for `format-time-string' which are used for time stamps.
  2178. It is not recommended to change this constant.")
  2179. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2180. "Number of minutes to round time stamps to.
  2181. These are two values, the first applies when first creating a time stamp.
  2182. The second applies when changing it with the commands `S-up' and `S-down'.
  2183. When changing the time stamp, this means that it will change in steps
  2184. of N minutes, as given by the second value.
  2185. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2186. numbers should be factors of 60, so for example 5, 10, 15.
  2187. When this is larger than 1, you can still force an exact time stamp by using
  2188. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2189. and by using a prefix arg to `S-up/down' to specify the exact number
  2190. of minutes to shift."
  2191. :group 'org-time
  2192. :get '(lambda (var) ; Make sure both elements are there
  2193. (if (integerp (default-value var))
  2194. (list (default-value var) 5)
  2195. (default-value var)))
  2196. :type '(list
  2197. (integer :tag "when inserting times")
  2198. (integer :tag "when modifying times")))
  2199. ;; Normalize old customizations of this variable.
  2200. (when (integerp org-time-stamp-rounding-minutes)
  2201. (setq org-time-stamp-rounding-minutes
  2202. (list org-time-stamp-rounding-minutes
  2203. org-time-stamp-rounding-minutes)))
  2204. (defcustom org-display-custom-times nil
  2205. "Non-nil means overlay custom formats over all time stamps.
  2206. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2207. To turn this on on a per-file basis, insert anywhere in the file:
  2208. #+STARTUP: customtime"
  2209. :group 'org-time
  2210. :set 'set-default
  2211. :type 'sexp)
  2212. (make-variable-buffer-local 'org-display-custom-times)
  2213. (defcustom org-time-stamp-custom-formats
  2214. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2215. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2216. These are overlayed over the default ISO format if the variable
  2217. `org-display-custom-times' is set. Time like %H:%M should be at the
  2218. end of the second format. The custom formats are also honored by export
  2219. commands, if custom time display is turned on at the time of export."
  2220. :group 'org-time
  2221. :type 'sexp)
  2222. (defun org-time-stamp-format (&optional long inactive)
  2223. "Get the right format for a time string."
  2224. (let ((f (if long (cdr org-time-stamp-formats)
  2225. (car org-time-stamp-formats))))
  2226. (if inactive
  2227. (concat "[" (substring f 1 -1) "]")
  2228. f)))
  2229. (defcustom org-time-clocksum-format "%d:%02d"
  2230. "The format string used when creating CLOCKSUM lines.
  2231. This is also used when org-mode generates a time duration."
  2232. :group 'org-time
  2233. :type 'string)
  2234. (defcustom org-time-clocksum-use-fractional nil
  2235. "If non-nil, \\[org-clock-display] uses fractional times.
  2236. org-mode generates a time duration."
  2237. :group 'org-time
  2238. :type 'boolean)
  2239. (defcustom org-time-clocksum-fractional-format "%.2f"
  2240. "The format string used when creating CLOCKSUM lines, or when
  2241. org-mode generates a time duration."
  2242. :group 'org-time
  2243. :type 'string)
  2244. (defcustom org-deadline-warning-days 14
  2245. "No. of days before expiration during which a deadline becomes active.
  2246. This variable governs the display in sparse trees and in the agenda.
  2247. When 0 or negative, it means use this number (the absolute value of it)
  2248. even if a deadline has a different individual lead time specified.
  2249. Custom commands can set this variable in the options section."
  2250. :group 'org-time
  2251. :group 'org-agenda-daily/weekly
  2252. :type 'integer)
  2253. (defcustom org-read-date-prefer-future t
  2254. "Non-nil means assume future for incomplete date input from user.
  2255. This affects the following situations:
  2256. 1. The user gives a month but not a year.
  2257. For example, if it is April and you enter \"feb 2\", this will be read
  2258. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2259. 2. The user gives a day, but no month.
  2260. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2261. read this as the third of *next* month. However, if you enter \"17\",
  2262. it will be considered as *this* month.
  2263. If you set this variable to the symbol `time', then also the following
  2264. will work:
  2265. 3. If the user gives a time, but no day. If the time is before now,
  2266. to will be interpreted as tomorrow.
  2267. Currently none of this works for ISO week specifications.
  2268. When this option is nil, the current day, month and year will always be
  2269. used as defaults.
  2270. See also `org-agenda-jump-prefer-future'."
  2271. :group 'org-time
  2272. :type '(choice
  2273. (const :tag "Never" nil)
  2274. (const :tag "Check month and day" t)
  2275. (const :tag "Check month, day, and time" time)))
  2276. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2277. "Should the agenda jump command prefer the future for incomplete dates?
  2278. The default is to do the same as configured in `org-read-date-prefer-future'.
  2279. But you can alse set a deviating value here.
  2280. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2281. :group 'org-agenda
  2282. :group 'org-time
  2283. :type '(choice
  2284. (const :tag "Use org-aread-date-prefer-future"
  2285. org-read-date-prefer-future)
  2286. (const :tag "Never" nil)
  2287. (const :tag "Always" t)))
  2288. (defcustom org-read-date-display-live t
  2289. "Non-nil means display current interpretation of date prompt live.
  2290. This display will be in an overlay, in the minibuffer."
  2291. :group 'org-time
  2292. :type 'boolean)
  2293. (defcustom org-read-date-popup-calendar t
  2294. "Non-nil means pop up a calendar when prompting for a date.
  2295. In the calendar, the date can be selected with mouse-1. However, the
  2296. minibuffer will also be active, and you can simply enter the date as well.
  2297. When nil, only the minibuffer will be available."
  2298. :group 'org-time
  2299. :type 'boolean)
  2300. (if (fboundp 'defvaralias)
  2301. (defvaralias 'org-popup-calendar-for-date-prompt
  2302. 'org-read-date-popup-calendar))
  2303. (defcustom org-read-date-minibuffer-setup-hook nil
  2304. "Hook to be used to set up keys for the date/time interface.
  2305. Add key definitions to `minibuffer-local-map', which will be a temporary
  2306. copy."
  2307. :group 'org-time
  2308. :type 'hook)
  2309. (defcustom org-extend-today-until 0
  2310. "The hour when your day really ends. Must be an integer.
  2311. This has influence for the following applications:
  2312. - When switching the agenda to \"today\". It it is still earlier than
  2313. the time given here, the day recognized as TODAY is actually yesterday.
  2314. - When a date is read from the user and it is still before the time given
  2315. here, the current date and time will be assumed to be yesterday, 23:59.
  2316. Also, timestamps inserted in remember templates follow this rule.
  2317. IMPORTANT: This is a feature whose implementation is and likely will
  2318. remain incomplete. Really, it is only here because past midnight seems to
  2319. be the favorite working time of John Wiegley :-)"
  2320. :group 'org-time
  2321. :type 'integer)
  2322. (defcustom org-edit-timestamp-down-means-later nil
  2323. "Non-nil means S-down will increase the time in a time stamp.
  2324. When nil, S-up will increase."
  2325. :group 'org-time
  2326. :type 'boolean)
  2327. (defcustom org-calendar-follow-timestamp-change t
  2328. "Non-nil means make the calendar window follow timestamp changes.
  2329. When a timestamp is modified and the calendar window is visible, it will be
  2330. moved to the new date."
  2331. :group 'org-time
  2332. :type 'boolean)
  2333. (defgroup org-tags nil
  2334. "Options concerning tags in Org-mode."
  2335. :tag "Org Tags"
  2336. :group 'org)
  2337. (defcustom org-tag-alist nil
  2338. "List of tags allowed in Org-mode files.
  2339. When this list is nil, Org-mode will base TAG input on what is already in the
  2340. buffer.
  2341. The value of this variable is an alist, the car of each entry must be a
  2342. keyword as a string, the cdr may be a character that is used to select
  2343. that tag through the fast-tag-selection interface.
  2344. See the manual for details."
  2345. :group 'org-tags
  2346. :type '(repeat
  2347. (choice
  2348. (cons (string :tag "Tag name")
  2349. (character :tag "Access char"))
  2350. (list :tag "Start radio group"
  2351. (const :startgroup)
  2352. (option (string :tag "Group description")))
  2353. (list :tag "End radio group"
  2354. (const :endgroup)
  2355. (option (string :tag "Group description")))
  2356. (const :tag "New line" (:newline)))))
  2357. (defcustom org-tag-persistent-alist nil
  2358. "List of tags that will always appear in all Org-mode files.
  2359. This is in addition to any in buffer settings or customizations
  2360. of `org-tag-alist'.
  2361. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2362. The value of this variable is an alist, the car of each entry must be a
  2363. keyword as a string, the cdr may be a character that is used to select
  2364. that tag through the fast-tag-selection interface.
  2365. See the manual for details.
  2366. To disable these tags on a per-file basis, insert anywhere in the file:
  2367. #+STARTUP: noptag"
  2368. :group 'org-tags
  2369. :type '(repeat
  2370. (choice
  2371. (cons (string :tag "Tag name")
  2372. (character :tag "Access char"))
  2373. (const :tag "Start radio group" (:startgroup))
  2374. (const :tag "End radio group" (:endgroup))
  2375. (const :tag "New line" (:newline)))))
  2376. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2377. "If non-nil, always offer completion for all tags of all agenda files.
  2378. Instead of customizing this variable directly, you might want to
  2379. set it locally for capture buffers, because there no list of
  2380. tags in that file can be created dynamically (there are none).
  2381. (add-hook 'org-capture-mode-hook
  2382. (lambda ()
  2383. (set (make-local-variable
  2384. 'org-complete-tags-always-offer-all-agenda-tags)
  2385. t)))"
  2386. :group 'org-tags
  2387. :type 'boolean)
  2388. (defvar org-file-tags nil
  2389. "List of tags that can be inherited by all entries in the file.
  2390. The tags will be inherited if the variable `org-use-tag-inheritance'
  2391. says they should be.
  2392. This variable is populated from #+FILETAGS lines.")
  2393. (defcustom org-use-fast-tag-selection 'auto
  2394. "Non-nil means use fast tag selection scheme.
  2395. This is a special interface to select and deselect tags with single keys.
  2396. When nil, fast selection is never used.
  2397. When the symbol `auto', fast selection is used if and only if selection
  2398. characters for tags have been configured, either through the variable
  2399. `org-tag-alist' or through a #+TAGS line in the buffer.
  2400. When t, fast selection is always used and selection keys are assigned
  2401. automatically if necessary."
  2402. :group 'org-tags
  2403. :type '(choice
  2404. (const :tag "Always" t)
  2405. (const :tag "Never" nil)
  2406. (const :tag "When selection characters are configured" 'auto)))
  2407. (defcustom org-fast-tag-selection-single-key nil
  2408. "Non-nil means fast tag selection exits after first change.
  2409. When nil, you have to press RET to exit it.
  2410. During fast tag selection, you can toggle this flag with `C-c'.
  2411. This variable can also have the value `expert'. In this case, the window
  2412. displaying the tags menu is not even shown, until you press C-c again."
  2413. :group 'org-tags
  2414. :type '(choice
  2415. (const :tag "No" nil)
  2416. (const :tag "Yes" t)
  2417. (const :tag "Expert" expert)))
  2418. (defvar org-fast-tag-selection-include-todo nil
  2419. "Non-nil means fast tags selection interface will also offer TODO states.
  2420. This is an undocumented feature, you should not rely on it.")
  2421. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2422. "The column to which tags should be indented in a headline.
  2423. If this number is positive, it specifies the column. If it is negative,
  2424. it means that the tags should be flushright to that column. For example,
  2425. -80 works well for a normal 80 character screen."
  2426. :group 'org-tags
  2427. :type 'integer)
  2428. (defcustom org-auto-align-tags t
  2429. "Non-nil means realign tags after pro/demotion of TODO state change.
  2430. These operations change the length of a headline and therefore shift
  2431. the tags around. With this options turned on, after each such operation
  2432. the tags are again aligned to `org-tags-column'."
  2433. :group 'org-tags
  2434. :type 'boolean)
  2435. (defcustom org-use-tag-inheritance t
  2436. "Non-nil means tags in levels apply also for sublevels.
  2437. When nil, only the tags directly given in a specific line apply there.
  2438. This may also be a list of tags that should be inherited, or a regexp that
  2439. matches tags that should be inherited. Additional control is possible
  2440. with the variable `org-tags-exclude-from-inheritance' which gives an
  2441. explicit list of tags to be excluded from inheritance., even if the value of
  2442. `org-use-tag-inheritance' would select it for inheritance.
  2443. If this option is t, a match early-on in a tree can lead to a large
  2444. number of matches in the subtree when constructing the agenda or creating
  2445. a sparse tree. If you only want to see the first match in a tree during
  2446. a search, check out the variable `org-tags-match-list-sublevels'."
  2447. :group 'org-tags
  2448. :type '(choice
  2449. (const :tag "Not" nil)
  2450. (const :tag "Always" t)
  2451. (repeat :tag "Specific tags" (string :tag "Tag"))
  2452. (regexp :tag "Tags matched by regexp")))
  2453. (defcustom org-tags-exclude-from-inheritance nil
  2454. "List of tags that should never be inherited.
  2455. This is a way to exclude a few tags from inheritance. For way to do
  2456. the opposite, to actively allow inheritance for selected tags,
  2457. see the variable `org-use-tag-inheritance'."
  2458. :group 'org-tags
  2459. :type '(repeat (string :tag "Tag")))
  2460. (defun org-tag-inherit-p (tag)
  2461. "Check if TAG is one that should be inherited."
  2462. (cond
  2463. ((member tag org-tags-exclude-from-inheritance) nil)
  2464. ((eq org-use-tag-inheritance t) t)
  2465. ((not org-use-tag-inheritance) nil)
  2466. ((stringp org-use-tag-inheritance)
  2467. (string-match org-use-tag-inheritance tag))
  2468. ((listp org-use-tag-inheritance)
  2469. (member tag org-use-tag-inheritance))
  2470. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2471. (defcustom org-tags-match-list-sublevels t
  2472. "Non-nil means list also sublevels of headlines matching a search.
  2473. This variable applies to tags/property searches, and also to stuck
  2474. projects because this search is based on a tags match as well.
  2475. When set to the symbol `indented', sublevels are indented with
  2476. leading dots.
  2477. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2478. the sublevels of a headline matching a tag search often also match
  2479. the same search. Listing all of them can create very long lists.
  2480. Setting this variable to nil causes subtrees of a match to be skipped.
  2481. This variable is semi-obsolete and probably should always be true. It
  2482. is better to limit inheritance to certain tags using the variables
  2483. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2484. :group 'org-tags
  2485. :type '(choice
  2486. (const :tag "No, don't list them" nil)
  2487. (const :tag "Yes, do list them" t)
  2488. (const :tag "List them, indented with leading dots" indented)))
  2489. (defcustom org-tags-sort-function nil
  2490. "When set, tags are sorted using this function as a comparator."
  2491. :group 'org-tags
  2492. :type '(choice
  2493. (const :tag "No sorting" nil)
  2494. (const :tag "Alphabetical" string<)
  2495. (const :tag "Reverse alphabetical" string>)
  2496. (function :tag "Custom function" nil)))
  2497. (defvar org-tags-history nil
  2498. "History of minibuffer reads for tags.")
  2499. (defvar org-last-tags-completion-table nil
  2500. "The last used completion table for tags.")
  2501. (defvar org-after-tags-change-hook nil
  2502. "Hook that is run after the tags in a line have changed.")
  2503. (defgroup org-properties nil
  2504. "Options concerning properties in Org-mode."
  2505. :tag "Org Properties"
  2506. :group 'org)
  2507. (defcustom org-property-format "%-10s %s"
  2508. "How property key/value pairs should be formatted by `indent-line'.
  2509. When `indent-line' hits a property definition, it will format the line
  2510. according to this format, mainly to make sure that the values are
  2511. lined-up with respect to each other."
  2512. :group 'org-properties
  2513. :type 'string)
  2514. (defcustom org-use-property-inheritance nil
  2515. "Non-nil means properties apply also for sublevels.
  2516. This setting is chiefly used during property searches. Turning it on can
  2517. cause significant overhead when doing a search, which is why it is not
  2518. on by default.
  2519. When nil, only the properties directly given in the current entry count.
  2520. When t, every property is inherited. The value may also be a list of
  2521. properties that should have inheritance, or a regular expression matching
  2522. properties that should be inherited.
  2523. However, note that some special properties use inheritance under special
  2524. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2525. and the properties ending in \"_ALL\" when they are used as descriptor
  2526. for valid values of a property.
  2527. Note for programmers:
  2528. When querying an entry with `org-entry-get', you can control if inheritance
  2529. should be used. By default, `org-entry-get' looks only at the local
  2530. properties. You can request inheritance by setting the inherit argument
  2531. to t (to force inheritance) or to `selective' (to respect the setting
  2532. in this variable)."
  2533. :group 'org-properties
  2534. :type '(choice
  2535. (const :tag "Not" nil)
  2536. (const :tag "Always" t)
  2537. (repeat :tag "Specific properties" (string :tag "Property"))
  2538. (regexp :tag "Properties matched by regexp")))
  2539. (defun org-property-inherit-p (property)
  2540. "Check if PROPERTY is one that should be inherited."
  2541. (cond
  2542. ((eq org-use-property-inheritance t) t)
  2543. ((not org-use-property-inheritance) nil)
  2544. ((stringp org-use-property-inheritance)
  2545. (string-match org-use-property-inheritance property))
  2546. ((listp org-use-property-inheritance)
  2547. (member property org-use-property-inheritance))
  2548. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2549. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2550. "The default column format, if no other format has been defined.
  2551. This variable can be set on the per-file basis by inserting a line
  2552. #+COLUMNS: %25ITEM ....."
  2553. :group 'org-properties
  2554. :type 'string)
  2555. (defcustom org-columns-ellipses ".."
  2556. "The ellipses to be used when a field in column view is truncated.
  2557. When this is the empty string, as many characters as possible are shown,
  2558. but then there will be no visual indication that the field has been truncated.
  2559. When this is a string of length N, the last N characters of a truncated
  2560. field are replaced by this string. If the column is narrower than the
  2561. ellipses string, only part of the ellipses string will be shown."
  2562. :group 'org-properties
  2563. :type 'string)
  2564. (defcustom org-columns-modify-value-for-display-function nil
  2565. "Function that modifies values for display in column view.
  2566. For example, it can be used to cut out a certain part from a time stamp.
  2567. The function must take 2 arguments:
  2568. column-title The title of the column (*not* the property name)
  2569. value The value that should be modified.
  2570. The function should return the value that should be displayed,
  2571. or nil if the normal value should be used."
  2572. :group 'org-properties
  2573. :type 'function)
  2574. (defcustom org-effort-property "Effort"
  2575. "The property that is being used to keep track of effort estimates.
  2576. Effort estimates given in this property need to have the format H:MM."
  2577. :group 'org-properties
  2578. :group 'org-progress
  2579. :type '(string :tag "Property"))
  2580. (defconst org-global-properties-fixed
  2581. '(("VISIBILITY_ALL" . "folded children content all")
  2582. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2583. "List of property/value pairs that can be inherited by any entry.
  2584. These are fixed values, for the preset properties. The user variable
  2585. that can be used to add to this list is `org-global-properties'.
  2586. The entries in this list are cons cells where the car is a property
  2587. name and cdr is a string with the value. If the value represents
  2588. multiple items like an \"_ALL\" property, separate the items by
  2589. spaces.")
  2590. (defcustom org-global-properties nil
  2591. "List of property/value pairs that can be inherited by any entry.
  2592. This list will be combined with the constant `org-global-properties-fixed'.
  2593. The entries in this list are cons cells where the car is a property
  2594. name and cdr is a string with the value.
  2595. You can set buffer-local values for the same purpose in the variable
  2596. `org-file-properties' this by adding lines like
  2597. #+PROPERTY: NAME VALUE"
  2598. :group 'org-properties
  2599. :type '(repeat
  2600. (cons (string :tag "Property")
  2601. (string :tag "Value"))))
  2602. (defvar org-file-properties nil
  2603. "List of property/value pairs that can be inherited by any entry.
  2604. Valid for the current buffer.
  2605. This variable is populated from #+PROPERTY lines.")
  2606. (make-variable-buffer-local 'org-file-properties)
  2607. (defgroup org-agenda nil
  2608. "Options concerning agenda views in Org-mode."
  2609. :tag "Org Agenda"
  2610. :group 'org)
  2611. (defvar org-category nil
  2612. "Variable used by org files to set a category for agenda display.
  2613. Such files should use a file variable to set it, for example
  2614. # -*- mode: org; org-category: \"ELisp\"
  2615. or contain a special line
  2616. #+CATEGORY: ELisp
  2617. If the file does not specify a category, then file's base name
  2618. is used instead.")
  2619. (make-variable-buffer-local 'org-category)
  2620. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2621. (defcustom org-agenda-files nil
  2622. "The files to be used for agenda display.
  2623. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2624. \\[org-remove-file]. You can also use customize to edit the list.
  2625. If an entry is a directory, all files in that directory that are matched by
  2626. `org-agenda-file-regexp' will be part of the file list.
  2627. If the value of the variable is not a list but a single file name, then
  2628. the list of agenda files is actually stored and maintained in that file, one
  2629. agenda file per line. In this file paths can be given relative to
  2630. `org-directory'. Tilde expansion and environment variable substitution
  2631. are also made."
  2632. :group 'org-agenda
  2633. :type '(choice
  2634. (repeat :tag "List of files and directories" file)
  2635. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2636. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2637. "Regular expression to match files for `org-agenda-files'.
  2638. If any element in the list in that variable contains a directory instead
  2639. of a normal file, all files in that directory that are matched by this
  2640. regular expression will be included."
  2641. :group 'org-agenda
  2642. :type 'regexp)
  2643. (defcustom org-agenda-text-search-extra-files nil
  2644. "List of extra files to be searched by text search commands.
  2645. These files will be search in addition to the agenda files by the
  2646. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2647. Note that these files will only be searched for text search commands,
  2648. not for the other agenda views like todo lists, tag searches or the weekly
  2649. agenda. This variable is intended to list notes and possibly archive files
  2650. that should also be searched by these two commands.
  2651. In fact, if the first element in the list is the symbol `agenda-archives',
  2652. than all archive files of all agenda files will be added to the search
  2653. scope."
  2654. :group 'org-agenda
  2655. :type '(set :greedy t
  2656. (const :tag "Agenda Archives" agenda-archives)
  2657. (repeat :inline t (file))))
  2658. (if (fboundp 'defvaralias)
  2659. (defvaralias 'org-agenda-multi-occur-extra-files
  2660. 'org-agenda-text-search-extra-files))
  2661. (defcustom org-agenda-skip-unavailable-files nil
  2662. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2663. A nil value means to remove them, after a query, from the list."
  2664. :group 'org-agenda
  2665. :type 'boolean)
  2666. (defcustom org-calendar-to-agenda-key [?c]
  2667. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2668. The command `org-calendar-goto-agenda' will be bound to this key. The
  2669. default is the character `c' because then `c' can be used to switch back and
  2670. forth between agenda and calendar."
  2671. :group 'org-agenda
  2672. :type 'sexp)
  2673. (defcustom org-calendar-agenda-action-key [?k]
  2674. "The key to be installed in `calendar-mode-map' for agenda-action.
  2675. The command `org-agenda-action' will be bound to this key. The
  2676. default is the character `k' because we use the same key in the agenda."
  2677. :group 'org-agenda
  2678. :type 'sexp)
  2679. (defcustom org-calendar-insert-diary-entry-key [?i]
  2680. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2681. This option is irrelevant until `org-agenda-diary-file' has been configured
  2682. to point to an Org-mode file. When that is the case, the command
  2683. `org-agenda-diary-entry' will be bound to the key given here, by default
  2684. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2685. if you want to continue doing this, you need to change this to a different
  2686. key."
  2687. :group 'org-agenda
  2688. :type 'sexp)
  2689. (defcustom org-agenda-diary-file 'diary-file
  2690. "File to which to add new entries with the `i' key in agenda and calendar.
  2691. When this is the symbol `diary-file', the functionality in the Emacs
  2692. calendar will be used to add entries to the `diary-file'. But when this
  2693. points to a file, `org-agenda-diary-entry' will be used instead."
  2694. :group 'org-agenda
  2695. :type '(choice
  2696. (const :tag "The standard Emacs diary file" diary-file)
  2697. (file :tag "Special Org file diary entries")))
  2698. (eval-after-load "calendar"
  2699. '(progn
  2700. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2701. 'org-calendar-goto-agenda)
  2702. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2703. 'org-agenda-action)
  2704. (add-hook 'calendar-mode-hook
  2705. (lambda ()
  2706. (unless (eq org-agenda-diary-file 'diary-file)
  2707. (define-key calendar-mode-map
  2708. org-calendar-insert-diary-entry-key
  2709. 'org-agenda-diary-entry))))))
  2710. (defgroup org-latex nil
  2711. "Options for embedding LaTeX code into Org-mode."
  2712. :tag "Org LaTeX"
  2713. :group 'org)
  2714. (defcustom org-format-latex-options
  2715. '(:foreground default :background default :scale 1.0
  2716. :html-foreground "Black" :html-background "Transparent"
  2717. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2718. "Options for creating images from LaTeX fragments.
  2719. This is a property list with the following properties:
  2720. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2721. `default' means use the foreground of the default face.
  2722. :background the background color, or \"Transparent\".
  2723. `default' means use the background of the default face.
  2724. :scale a scaling factor for the size of the images, to get more pixels
  2725. :html-foreground, :html-background, :html-scale
  2726. the same numbers for HTML export.
  2727. :matchers a list indicating which matchers should be used to
  2728. find LaTeX fragments. Valid members of this list are:
  2729. \"begin\" find environments
  2730. \"$1\" find single characters surrounded by $.$
  2731. \"$\" find math expressions surrounded by $...$
  2732. \"$$\" find math expressions surrounded by $$....$$
  2733. \"\\(\" find math expressions surrounded by \\(...\\)
  2734. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2735. :group 'org-latex
  2736. :type 'plist)
  2737. (defcustom org-format-latex-signal-error t
  2738. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2739. When nil, just push out a message."
  2740. :group 'org-latex
  2741. :type 'boolean)
  2742. (defcustom org-format-latex-header "\\documentclass{article}
  2743. \\usepackage[usenames]{color}
  2744. \\usepackage{amsmath}
  2745. \\usepackage[mathscr]{eucal}
  2746. \\pagestyle{empty} % do not remove
  2747. \[PACKAGES]
  2748. \[DEFAULT-PACKAGES]
  2749. % The settings below are copied from fullpage.sty
  2750. \\setlength{\\textwidth}{\\paperwidth}
  2751. \\addtolength{\\textwidth}{-3cm}
  2752. \\setlength{\\oddsidemargin}{1.5cm}
  2753. \\addtolength{\\oddsidemargin}{-2.54cm}
  2754. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2755. \\setlength{\\textheight}{\\paperheight}
  2756. \\addtolength{\\textheight}{-\\headheight}
  2757. \\addtolength{\\textheight}{-\\headsep}
  2758. \\addtolength{\\textheight}{-\\footskip}
  2759. \\addtolength{\\textheight}{-3cm}
  2760. \\setlength{\\topmargin}{1.5cm}
  2761. \\addtolength{\\topmargin}{-2.54cm}"
  2762. "The document header used for processing LaTeX fragments.
  2763. It is imperative that this header make sure that no page number
  2764. appears on the page. The package defined in the variables
  2765. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2766. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2767. will be appended."
  2768. :group 'org-latex
  2769. :type 'string)
  2770. (defvar org-format-latex-header-extra nil)
  2771. (defun org-set-packages-alist (var val)
  2772. "Set the packages alist and make sure it has 3 elements per entry."
  2773. (set var (mapcar (lambda (x)
  2774. (if (and (consp x) (= (length x) 2))
  2775. (list (car x) (nth 1 x) t)
  2776. x))
  2777. val)))
  2778. (defun org-get-packages-alist (var)
  2779. "Get the packages alist and make sure it has 3 elements per entry."
  2780. (mapcar (lambda (x)
  2781. (if (and (consp x) (= (length x) 2))
  2782. (list (car x) (nth 1 x) t)
  2783. x))
  2784. (default-value var)))
  2785. ;; The following variables are defined here because is it also used
  2786. ;; when formatting latex fragments. Originally it was part of the
  2787. ;; LaTeX exporter, which is why the name includes "export".
  2788. (defcustom org-export-latex-default-packages-alist
  2789. '(("AUTO" "inputenc" t)
  2790. ("T1" "fontenc" t)
  2791. ("" "fixltx2e" nil)
  2792. ("" "graphicx" t)
  2793. ("" "longtable" nil)
  2794. ("" "float" nil)
  2795. ("" "wrapfig" nil)
  2796. ("" "soul" t)
  2797. ("" "textcomp" t)
  2798. ("" "marvosym" t)
  2799. ("" "wasysym" t)
  2800. ("" "latexsym" t)
  2801. ("" "amssymb" t)
  2802. ("" "hyperref" nil)
  2803. "\\tolerance=1000"
  2804. )
  2805. "Alist of default packages to be inserted in the header.
  2806. Change this only if one of the packages here causes an incompatibility
  2807. with another package you are using.
  2808. The packages in this list are needed by one part or another of Org-mode
  2809. to function properly.
  2810. - inputenc, fontenc: for basic font and character selection
  2811. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2812. for interpreting the entities in `org-entities'. You can skip some of these
  2813. packages if you don't use any of the symbols in it.
  2814. - graphicx: for including images
  2815. - float, wrapfig: for figure placement
  2816. - longtable: for long tables
  2817. - hyperref: for cross references
  2818. Therefore you should not modify this variable unless you know what you
  2819. are doing. The one reason to change it anyway is that you might be loading
  2820. some other package that conflicts with one of the default packages.
  2821. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2822. If SNIPPET-FLAG is t, the package also needs to be included when
  2823. compiling LaTeX snippets into images for inclusion into HTML."
  2824. :group 'org-export-latex
  2825. :set 'org-set-packages-alist
  2826. :get 'org-get-packages-alist
  2827. :type '(repeat
  2828. (choice
  2829. (list :tag "options/package pair"
  2830. (string :tag "options")
  2831. (string :tag "package")
  2832. (boolean :tag "Snippet"))
  2833. (string :tag "A line of LaTeX"))))
  2834. (defcustom org-export-latex-packages-alist nil
  2835. "Alist of packages to be inserted in every LaTeX header.
  2836. These will be inserted after `org-export-latex-default-packages-alist'.
  2837. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2838. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2839. turning LaTeX snippets into images for inclusion into HTML.
  2840. Make sure that you only list packages here which:
  2841. - you want in every file
  2842. - do not conflict with the default packages in
  2843. `org-export-latex-default-packages-alist'
  2844. - do not conflict with the setup in `org-format-latex-header'."
  2845. :group 'org-export-latex
  2846. :set 'org-set-packages-alist
  2847. :get 'org-get-packages-alist
  2848. :type '(repeat
  2849. (choice
  2850. (list :tag "options/package pair"
  2851. (string :tag "options")
  2852. (string :tag "package")
  2853. (boolean :tag "Snippet"))
  2854. (string :tag "A line of LaTeX"))))
  2855. (defgroup org-appearance nil
  2856. "Settings for Org-mode appearance."
  2857. :tag "Org Appearance"
  2858. :group 'org)
  2859. (defcustom org-level-color-stars-only nil
  2860. "Non-nil means fontify only the stars in each headline.
  2861. When nil, the entire headline is fontified.
  2862. Changing it requires restart of `font-lock-mode' to become effective
  2863. also in regions already fontified."
  2864. :group 'org-appearance
  2865. :type 'boolean)
  2866. (defcustom org-hide-leading-stars nil
  2867. "Non-nil means hide the first N-1 stars in a headline.
  2868. This works by using the face `org-hide' for these stars. This
  2869. face is white for a light background, and black for a dark
  2870. background. You may have to customize the face `org-hide' to
  2871. make this work.
  2872. Changing it requires restart of `font-lock-mode' to become effective
  2873. also in regions already fontified.
  2874. You may also set this on a per-file basis by adding one of the following
  2875. lines to the buffer:
  2876. #+STARTUP: hidestars
  2877. #+STARTUP: showstars"
  2878. :group 'org-appearance
  2879. :type 'boolean)
  2880. (defcustom org-hidden-keywords nil
  2881. "List of keywords that should be hidden when typed in the org buffer.
  2882. For example, add #+TITLE to this list in order to make the
  2883. document title appear in the buffer without the initial #+TITLE:
  2884. keyword."
  2885. :group 'org-appearance
  2886. :type '(set (const :tag "#+AUTHOR" author)
  2887. (const :tag "#+DATE" date)
  2888. (const :tag "#+EMAIL" email)
  2889. (const :tag "#+TITLE" title)))
  2890. (defcustom org-fontify-done-headline nil
  2891. "Non-nil means change the face of a headline if it is marked DONE.
  2892. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2893. When this is non-nil, the headline after the keyword is set to the
  2894. `org-headline-done' as an additional indication."
  2895. :group 'org-appearance
  2896. :type 'boolean)
  2897. (defcustom org-fontify-emphasized-text t
  2898. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2899. Changing this variable requires a restart of Emacs to take effect."
  2900. :group 'org-appearance
  2901. :type 'boolean)
  2902. (defcustom org-fontify-whole-heading-line nil
  2903. "Non-nil means fontify the whole line for headings.
  2904. This is useful when setting a background color for the
  2905. org-level-* faces."
  2906. :group 'org-appearance
  2907. :type 'boolean)
  2908. (defcustom org-highlight-latex-fragments-and-specials nil
  2909. "Non-nil means fontify what is treated specially by the exporters."
  2910. :group 'org-appearance
  2911. :type 'boolean)
  2912. (defcustom org-hide-emphasis-markers nil
  2913. "Non-nil mean font-lock should hide the emphasis marker characters."
  2914. :group 'org-appearance
  2915. :type 'boolean)
  2916. (defcustom org-pretty-entities nil
  2917. "Non-nil means show entities as UTF8 characters.
  2918. When nil, the \\name form remains in the buffer."
  2919. :group 'org-appearance
  2920. :type 'boolean)
  2921. (defcustom org-pretty-entities-include-sub-superscripts t
  2922. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2923. :group 'org-appearance
  2924. :type 'boolean)
  2925. (defvar org-emph-re nil
  2926. "Regular expression for matching emphasis.
  2927. After a match, the match groups contain these elements:
  2928. 0 The match of the full regular expression, including the characters
  2929. before and after the proper match
  2930. 1 The character before the proper match, or empty at beginning of line
  2931. 2 The proper match, including the leading and trailing markers
  2932. 3 The leading marker like * or /, indicating the type of highlighting
  2933. 4 The text between the emphasis markers, not including the markers
  2934. 5 The character after the match, empty at the end of a line")
  2935. (defvar org-verbatim-re nil
  2936. "Regular expression for matching verbatim text.")
  2937. (defvar org-emphasis-regexp-components) ; defined just below
  2938. (defvar org-emphasis-alist) ; defined just below
  2939. (defun org-set-emph-re (var val)
  2940. "Set variable and compute the emphasis regular expression."
  2941. (set var val)
  2942. (when (and (boundp 'org-emphasis-alist)
  2943. (boundp 'org-emphasis-regexp-components)
  2944. org-emphasis-alist org-emphasis-regexp-components)
  2945. (let* ((e org-emphasis-regexp-components)
  2946. (pre (car e))
  2947. (post (nth 1 e))
  2948. (border (nth 2 e))
  2949. (body (nth 3 e))
  2950. (nl (nth 4 e))
  2951. (body1 (concat body "*?"))
  2952. (markers (mapconcat 'car org-emphasis-alist ""))
  2953. (vmarkers (mapconcat
  2954. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2955. org-emphasis-alist "")))
  2956. ;; make sure special characters appear at the right position in the class
  2957. (if (string-match "\\^" markers)
  2958. (setq markers (concat (replace-match "" t t markers) "^")))
  2959. (if (string-match "-" markers)
  2960. (setq markers (concat (replace-match "" t t markers) "-")))
  2961. (if (string-match "\\^" vmarkers)
  2962. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2963. (if (string-match "-" vmarkers)
  2964. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2965. (if (> nl 0)
  2966. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2967. (int-to-string nl) "\\}")))
  2968. ;; Make the regexp
  2969. (setq org-emph-re
  2970. (concat "\\([" pre "]\\|^\\)"
  2971. "\\("
  2972. "\\([" markers "]\\)"
  2973. "\\("
  2974. "[^" border "]\\|"
  2975. "[^" border "]"
  2976. body1
  2977. "[^" border "]"
  2978. "\\)"
  2979. "\\3\\)"
  2980. "\\([" post "]\\|$\\)"))
  2981. (setq org-verbatim-re
  2982. (concat "\\([" pre "]\\|^\\)"
  2983. "\\("
  2984. "\\([" vmarkers "]\\)"
  2985. "\\("
  2986. "[^" border "]\\|"
  2987. "[^" border "]"
  2988. body1
  2989. "[^" border "]"
  2990. "\\)"
  2991. "\\3\\)"
  2992. "\\([" post "]\\|$\\)")))))
  2993. (defcustom org-emphasis-regexp-components
  2994. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2995. "Components used to build the regular expression for emphasis.
  2996. This is a list with five entries. Terminology: In an emphasis string
  2997. like \" *strong word* \", we call the initial space PREMATCH, the final
  2998. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2999. and \"trong wor\" is the body. The different components in this variable
  3000. specify what is allowed/forbidden in each part:
  3001. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3002. post Chars allowed as postmatch. End of line will be allowed too.
  3003. border The chars *forbidden* as border characters.
  3004. body-regexp A regexp like \".\" to match a body character. Don't use
  3005. non-shy groups here, and don't allow newline here.
  3006. newline The maximum number of newlines allowed in an emphasis exp.
  3007. Use customize to modify this, or restart Emacs after changing it."
  3008. :group 'org-appearance
  3009. :set 'org-set-emph-re
  3010. :type '(list
  3011. (sexp :tag "Allowed chars in pre ")
  3012. (sexp :tag "Allowed chars in post ")
  3013. (sexp :tag "Forbidden chars in border ")
  3014. (sexp :tag "Regexp for body ")
  3015. (integer :tag "number of newlines allowed")
  3016. (option (boolean :tag "Please ignore this button"))))
  3017. (defcustom org-emphasis-alist
  3018. `(("*" bold "<b>" "</b>")
  3019. ("/" italic "<i>" "</i>")
  3020. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3021. ("=" org-code "<code>" "</code>" verbatim)
  3022. ("~" org-verbatim "<code>" "</code>" verbatim)
  3023. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3024. "<del>" "</del>")
  3025. )
  3026. "Special syntax for emphasized text.
  3027. Text starting and ending with a special character will be emphasized, for
  3028. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3029. characters, the face to be used by font-lock for highlighting in Org-mode
  3030. Emacs buffers, and the HTML tags to be used for this.
  3031. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3032. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3033. Use customize to modify this, or restart Emacs after changing it."
  3034. :group 'org-appearance
  3035. :set 'org-set-emph-re
  3036. :type '(repeat
  3037. (list
  3038. (string :tag "Marker character")
  3039. (choice
  3040. (face :tag "Font-lock-face")
  3041. (plist :tag "Face property list"))
  3042. (string :tag "HTML start tag")
  3043. (string :tag "HTML end tag")
  3044. (option (const verbatim)))))
  3045. (defvar org-protecting-blocks
  3046. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3047. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3048. This is needed for font-lock setup.")
  3049. ;;; Miscellaneous options
  3050. (defgroup org-completion nil
  3051. "Completion in Org-mode."
  3052. :tag "Org Completion"
  3053. :group 'org)
  3054. (defcustom org-completion-use-ido nil
  3055. "Non-nil means use ido completion wherever possible.
  3056. Note that `ido-mode' must be active for this variable to be relevant.
  3057. If you decide to turn this variable on, you might well want to turn off
  3058. `org-outline-path-complete-in-steps'.
  3059. See also `org-completion-use-iswitchb'."
  3060. :group 'org-completion
  3061. :type 'boolean)
  3062. (defcustom org-completion-use-iswitchb nil
  3063. "Non-nil means use iswitchb completion wherever possible.
  3064. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3065. If you decide to turn this variable on, you might well want to turn off
  3066. `org-outline-path-complete-in-steps'.
  3067. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3068. :group 'org-completion
  3069. :type 'boolean)
  3070. (defcustom org-completion-fallback-command 'hippie-expand
  3071. "The expansion command called by \\[pcomplete] in normal context.
  3072. Normal means, no org-mode-specific context."
  3073. :group 'org-completion
  3074. :type 'function)
  3075. ;;; Functions and variables from their packages
  3076. ;; Declared here to avoid compiler warnings
  3077. ;; XEmacs only
  3078. (defvar outline-mode-menu-heading)
  3079. (defvar outline-mode-menu-show)
  3080. (defvar outline-mode-menu-hide)
  3081. (defvar zmacs-regions) ; XEmacs regions
  3082. ;; Emacs only
  3083. (defvar mark-active)
  3084. ;; Various packages
  3085. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3086. (declare-function calendar-forward-day "cal-move" (arg))
  3087. (declare-function calendar-goto-date "cal-move" (date))
  3088. (declare-function calendar-goto-today "cal-move" ())
  3089. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3090. (defvar calc-embedded-close-formula)
  3091. (defvar calc-embedded-open-formula)
  3092. (declare-function cdlatex-tab "ext:cdlatex" ())
  3093. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3094. (defvar font-lock-unfontify-region-function)
  3095. (declare-function iswitchb-read-buffer "iswitchb"
  3096. (prompt &optional default require-match start matches-set))
  3097. (defvar iswitchb-temp-buflist)
  3098. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3099. (defvar org-agenda-tags-todo-honor-ignore-options)
  3100. (declare-function org-agenda-skip "org-agenda" ())
  3101. (declare-function
  3102. org-format-agenda-item "org-agenda"
  3103. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3104. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3105. (declare-function org-agenda-change-all-lines "org-agenda"
  3106. (newhead hdmarker &optional fixface just-this))
  3107. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3108. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3109. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3110. (beg end))
  3111. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3112. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3113. "org-agenda" (&optional end))
  3114. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3115. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3116. (declare-function org-indent-mode "org-indent" (&optional arg))
  3117. (declare-function parse-time-string "parse-time" (string))
  3118. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3119. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3120. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3121. (defvar remember-data-file)
  3122. (defvar texmathp-why)
  3123. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3124. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3125. (defvar w3m-current-url)
  3126. (defvar w3m-current-title)
  3127. (defvar org-latex-regexps)
  3128. ;;; Autoload and prepare some org modules
  3129. ;; Some table stuff that needs to be defined here, because it is used
  3130. ;; by the functions setting up org-mode or checking for table context.
  3131. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3132. "Detect an org-type or table-type table.")
  3133. (defconst org-table-line-regexp "^[ \t]*|"
  3134. "Detect an org-type table line.")
  3135. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3136. "Detect an org-type table line.")
  3137. (defconst org-table-hline-regexp "^[ \t]*|-"
  3138. "Detect an org-type table hline.")
  3139. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3140. "Detect a table-type table hline.")
  3141. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3142. "Detect the first line outside a table when searching from within it.
  3143. This works for both table types.")
  3144. ;; Autoload the functions in org-table.el that are needed by functions here.
  3145. (eval-and-compile
  3146. (org-autoload "org-table"
  3147. '(org-table-align org-table-begin org-table-blank-field
  3148. org-table-convert org-table-convert-region org-table-copy-down
  3149. org-table-copy-region org-table-create
  3150. org-table-create-or-convert-from-region
  3151. org-table-create-with-table.el org-table-current-dline
  3152. org-table-cut-region org-table-delete-column org-table-edit-field
  3153. org-table-edit-formulas org-table-end org-table-eval-formula
  3154. org-table-export org-table-field-info
  3155. org-table-get-stored-formulas org-table-goto-column
  3156. org-table-hline-and-move org-table-import org-table-insert-column
  3157. org-table-insert-hline org-table-insert-row org-table-iterate
  3158. org-table-justify-field-maybe org-table-kill-row
  3159. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3160. org-table-move-column org-table-move-column-left
  3161. org-table-move-column-right org-table-move-row
  3162. org-table-move-row-down org-table-move-row-up
  3163. org-table-next-field org-table-next-row org-table-paste-rectangle
  3164. org-table-previous-field org-table-recalculate
  3165. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3166. org-table-toggle-coordinate-overlays
  3167. org-table-toggle-formula-debugger org-table-wrap-region
  3168. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3169. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3170. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3171. (defun org-at-table-p (&optional table-type)
  3172. "Return t if the cursor is inside an org-type table.
  3173. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3174. (if org-enable-table-editor
  3175. (save-excursion
  3176. (beginning-of-line 1)
  3177. (looking-at (if table-type org-table-any-line-regexp
  3178. org-table-line-regexp)))
  3179. nil))
  3180. (defsubst org-table-p () (org-at-table-p))
  3181. (defun org-at-table.el-p ()
  3182. "Return t if and only if we are at a table.el table."
  3183. (and (org-at-table-p 'any)
  3184. (save-excursion
  3185. (goto-char (org-table-begin 'any))
  3186. (looking-at org-table1-hline-regexp))))
  3187. (defun org-table-recognize-table.el ()
  3188. "If there is a table.el table nearby, recognize it and move into it."
  3189. (if org-table-tab-recognizes-table.el
  3190. (if (org-at-table.el-p)
  3191. (progn
  3192. (beginning-of-line 1)
  3193. (if (looking-at org-table-dataline-regexp)
  3194. nil
  3195. (if (looking-at org-table1-hline-regexp)
  3196. (progn
  3197. (beginning-of-line 2)
  3198. (if (looking-at org-table-any-border-regexp)
  3199. (beginning-of-line -1)))))
  3200. (if (re-search-forward "|" (org-table-end t) t)
  3201. (progn
  3202. (require 'table)
  3203. (if (table--at-cell-p (point))
  3204. t
  3205. (message "recognizing table.el table...")
  3206. (table-recognize-table)
  3207. (message "recognizing table.el table...done")))
  3208. (error "This should not happen"))
  3209. t)
  3210. nil)
  3211. nil))
  3212. (defun org-at-table-hline-p ()
  3213. "Return t if the cursor is inside a hline in a table."
  3214. (if org-enable-table-editor
  3215. (save-excursion
  3216. (beginning-of-line 1)
  3217. (looking-at org-table-hline-regexp))
  3218. nil))
  3219. (defvar org-table-clean-did-remove-column nil)
  3220. (defun org-table-map-tables (function &optional quietly)
  3221. "Apply FUNCTION to the start of all tables in the buffer."
  3222. (save-excursion
  3223. (save-restriction
  3224. (widen)
  3225. (goto-char (point-min))
  3226. (while (re-search-forward org-table-any-line-regexp nil t)
  3227. (unless quietly
  3228. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3229. (beginning-of-line 1)
  3230. (when (looking-at org-table-line-regexp)
  3231. (save-excursion (funcall function))
  3232. (or (looking-at org-table-line-regexp)
  3233. (forward-char 1)))
  3234. (re-search-forward org-table-any-border-regexp nil 1))))
  3235. (unless quietly (message "Mapping tables: done")))
  3236. ;; Declare and autoload functions from org-exp.el & Co
  3237. (declare-function org-default-export-plist "org-exp")
  3238. (declare-function org-infile-export-plist "org-exp")
  3239. (declare-function org-get-current-options "org-exp")
  3240. (eval-and-compile
  3241. (org-autoload "org-exp"
  3242. '(org-export org-export-visible
  3243. org-insert-export-options-template
  3244. org-table-clean-before-export))
  3245. (org-autoload "org-ascii"
  3246. '(org-export-as-ascii org-export-ascii-preprocess
  3247. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3248. org-export-region-as-ascii))
  3249. (org-autoload "org-latex"
  3250. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3251. org-replace-region-by-latex org-export-region-as-latex
  3252. org-export-as-latex org-export-as-pdf
  3253. org-export-as-pdf-and-open))
  3254. (org-autoload "org-html"
  3255. '(org-export-as-html-and-open
  3256. org-export-as-html-batch org-export-as-html-to-buffer
  3257. org-replace-region-by-html org-export-region-as-html
  3258. org-export-as-html))
  3259. (org-autoload "org-docbook"
  3260. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3261. org-replace-region-by-docbook org-export-region-as-docbook
  3262. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3263. org-export-as-docbook))
  3264. (org-autoload "org-icalendar"
  3265. '(org-export-icalendar-this-file
  3266. org-export-icalendar-all-agenda-files
  3267. org-export-icalendar-combine-agenda-files))
  3268. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3269. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3270. ;; Declare and autoload functions from org-agenda.el
  3271. (eval-and-compile
  3272. (org-autoload "org-agenda"
  3273. '(org-agenda org-agenda-list org-search-view
  3274. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3275. org-diary org-agenda-to-appt
  3276. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3277. ;; Autoload org-remember
  3278. (eval-and-compile
  3279. (org-autoload "org-remember"
  3280. '(org-remember-insinuate org-remember-annotation
  3281. org-remember-apply-template org-remember org-remember-handler)))
  3282. (eval-and-compile
  3283. (org-autoload "org-capture"
  3284. '(org-capture org-capture-insert-template-here
  3285. org-capture-import-remember-templates)))
  3286. ;; Autoload org-clock.el
  3287. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3288. (beg end))
  3289. (declare-function org-clock-update-mode-line "org-clock" ())
  3290. (declare-function org-resolve-clocks "org-clock"
  3291. (&optional also-non-dangling-p prompt last-valid))
  3292. (defvar org-clock-start-time)
  3293. (defvar org-clock-marker (make-marker)
  3294. "Marker recording the last clock-in.")
  3295. (defvar org-clock-hd-marker (make-marker)
  3296. "Marker recording the last clock-in, but the headline position.")
  3297. (defvar org-clock-heading ""
  3298. "The heading of the current clock entry.")
  3299. (defun org-clock-is-active ()
  3300. "Return non-nil if clock is currently running.
  3301. The return value is actually the clock marker."
  3302. (marker-buffer org-clock-marker))
  3303. (eval-and-compile
  3304. (org-autoload
  3305. "org-clock"
  3306. '(org-clock-in org-clock-out org-clock-cancel
  3307. org-clock-goto org-clock-sum org-clock-display
  3308. org-clock-remove-overlays org-clock-report
  3309. org-clocktable-shift org-dblock-write:clocktable
  3310. org-get-clocktable org-resolve-clocks)))
  3311. (defun org-clock-update-time-maybe ()
  3312. "If this is a CLOCK line, update it and return t.
  3313. Otherwise, return nil."
  3314. (interactive)
  3315. (save-excursion
  3316. (beginning-of-line 1)
  3317. (skip-chars-forward " \t")
  3318. (when (looking-at org-clock-string)
  3319. (let ((re (concat "[ \t]*" org-clock-string
  3320. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3321. "\\([ \t]*=>.*\\)?\\)?"))
  3322. ts te h m s neg)
  3323. (cond
  3324. ((not (looking-at re))
  3325. nil)
  3326. ((not (match-end 2))
  3327. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3328. (> org-clock-marker (point))
  3329. (<= org-clock-marker (point-at-eol)))
  3330. ;; The clock is running here
  3331. (setq org-clock-start-time
  3332. (apply 'encode-time
  3333. (org-parse-time-string (match-string 1))))
  3334. (org-clock-update-mode-line)))
  3335. (t
  3336. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3337. (end-of-line 1)
  3338. (setq ts (match-string 1)
  3339. te (match-string 3))
  3340. (setq s (- (org-float-time
  3341. (apply 'encode-time (org-parse-time-string te)))
  3342. (org-float-time
  3343. (apply 'encode-time (org-parse-time-string ts))))
  3344. neg (< s 0)
  3345. s (abs s)
  3346. h (floor (/ s 3600))
  3347. s (- s (* 3600 h))
  3348. m (floor (/ s 60))
  3349. s (- s (* 60 s)))
  3350. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3351. t))))))
  3352. (defun org-check-running-clock ()
  3353. "Check if the current buffer contains the running clock.
  3354. If yes, offer to stop it and to save the buffer with the changes."
  3355. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3356. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3357. (buffer-name))))
  3358. (org-clock-out)
  3359. (when (y-or-n-p "Save changed buffer?")
  3360. (save-buffer))))
  3361. (defun org-clocktable-try-shift (dir n)
  3362. "Check if this line starts a clock table, if yes, shift the time block."
  3363. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3364. (org-clocktable-shift dir n)))
  3365. ;; Autoload org-timer.el
  3366. (eval-and-compile
  3367. (org-autoload
  3368. "org-timer"
  3369. '(org-timer-start org-timer org-timer-item
  3370. org-timer-change-times-in-region
  3371. org-timer-set-timer
  3372. org-timer-reset-timers
  3373. org-timer-show-remaining-time)))
  3374. ;; Autoload org-feed.el
  3375. (eval-and-compile
  3376. (org-autoload
  3377. "org-feed"
  3378. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3379. ;; Autoload org-indent.el
  3380. ;; Define the variable already here, to make sure we have it.
  3381. (defvar org-indent-mode nil
  3382. "Non-nil if Org-Indent mode is enabled.
  3383. Use the command `org-indent-mode' to change this variable.")
  3384. (eval-and-compile
  3385. (org-autoload
  3386. "org-indent"
  3387. '(org-indent-mode)))
  3388. ;; Autoload org-mobile.el
  3389. (eval-and-compile
  3390. (org-autoload
  3391. "org-mobile"
  3392. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3393. ;; Autoload archiving code
  3394. ;; The stuff that is needed for cycling and tags has to be defined here.
  3395. (defgroup org-archive nil
  3396. "Options concerning archiving in Org-mode."
  3397. :tag "Org Archive"
  3398. :group 'org-structure)
  3399. (defcustom org-archive-location "%s_archive::"
  3400. "The location where subtrees should be archived.
  3401. The value of this variable is a string, consisting of two parts,
  3402. separated by a double-colon. The first part is a filename and
  3403. the second part is a headline.
  3404. When the filename is omitted, archiving happens in the same file.
  3405. %s in the filename will be replaced by the current file
  3406. name (without the directory part). Archiving to a different file
  3407. is useful to keep archived entries from contributing to the
  3408. Org-mode Agenda.
  3409. The archived entries will be filed as subtrees of the specified
  3410. headline. When the headline is omitted, the subtrees are simply
  3411. filed away at the end of the file, as top-level entries. Also in
  3412. the heading you can use %s to represent the file name, this can be
  3413. useful when using the same archive for a number of different files.
  3414. Here are a few examples:
  3415. \"%s_archive::\"
  3416. If the current file is Projects.org, archive in file
  3417. Projects.org_archive, as top-level trees. This is the default.
  3418. \"::* Archived Tasks\"
  3419. Archive in the current file, under the top-level headline
  3420. \"* Archived Tasks\".
  3421. \"~/org/archive.org::\"
  3422. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3423. \"~/org/archive.org::From %s\"
  3424. Archive in file ~/org/archive.org (absolute path), under headlines
  3425. \"From FILENAME\" where file name is the current file name.
  3426. \"basement::** Finished Tasks\"
  3427. Archive in file ./basement (relative path), as level 3 trees
  3428. below the level 2 heading \"** Finished Tasks\".
  3429. You may set this option on a per-file basis by adding to the buffer a
  3430. line like
  3431. #+ARCHIVE: basement::** Finished Tasks
  3432. You may also define it locally for a subtree by setting an ARCHIVE property
  3433. in the entry. If such a property is found in an entry, or anywhere up
  3434. the hierarchy, it will be used."
  3435. :group 'org-archive
  3436. :type 'string)
  3437. (defcustom org-archive-tag "ARCHIVE"
  3438. "The tag that marks a subtree as archived.
  3439. An archived subtree does not open during visibility cycling, and does
  3440. not contribute to the agenda listings.
  3441. After changing this, font-lock must be restarted in the relevant buffers to
  3442. get the proper fontification."
  3443. :group 'org-archive
  3444. :group 'org-keywords
  3445. :type 'string)
  3446. (defcustom org-agenda-skip-archived-trees t
  3447. "Non-nil means the agenda will skip any items located in archived trees.
  3448. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3449. variable is no longer recommended, you should leave it at the value t.
  3450. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3451. :group 'org-archive
  3452. :group 'org-agenda-skip
  3453. :type 'boolean)
  3454. (defcustom org-columns-skip-archived-trees t
  3455. "Non-nil means ignore archived trees when creating column view."
  3456. :group 'org-archive
  3457. :group 'org-properties
  3458. :type 'boolean)
  3459. (defcustom org-cycle-open-archived-trees nil
  3460. "Non-nil means `org-cycle' will open archived trees.
  3461. An archived tree is a tree marked with the tag ARCHIVE.
  3462. When nil, archived trees will stay folded. You can still open them with
  3463. normal outline commands like `show-all', but not with the cycling commands."
  3464. :group 'org-archive
  3465. :group 'org-cycle
  3466. :type 'boolean)
  3467. (defcustom org-sparse-tree-open-archived-trees nil
  3468. "Non-nil means sparse tree construction shows matches in archived trees.
  3469. When nil, matches in these trees are highlighted, but the trees are kept in
  3470. collapsed state."
  3471. :group 'org-archive
  3472. :group 'org-sparse-trees
  3473. :type 'boolean)
  3474. (defun org-cycle-hide-archived-subtrees (state)
  3475. "Re-hide all archived subtrees after a visibility state change."
  3476. (when (and (not org-cycle-open-archived-trees)
  3477. (not (memq state '(overview folded))))
  3478. (save-excursion
  3479. (let* ((globalp (memq state '(contents all)))
  3480. (beg (if globalp (point-min) (point)))
  3481. (end (if globalp (point-max) (org-end-of-subtree t))))
  3482. (org-hide-archived-subtrees beg end)
  3483. (goto-char beg)
  3484. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3485. (message "%s" (substitute-command-keys
  3486. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3487. (defun org-force-cycle-archived ()
  3488. "Cycle subtree even if it is archived."
  3489. (interactive)
  3490. (setq this-command 'org-cycle)
  3491. (let ((org-cycle-open-archived-trees t))
  3492. (call-interactively 'org-cycle)))
  3493. (defun org-hide-archived-subtrees (beg end)
  3494. "Re-hide all archived subtrees after a visibility state change."
  3495. (save-excursion
  3496. (let* ((re (concat ":" org-archive-tag ":")))
  3497. (goto-char beg)
  3498. (while (re-search-forward re end t)
  3499. (when (org-on-heading-p)
  3500. (org-flag-subtree t)
  3501. (org-end-of-subtree t))))))
  3502. (defun org-flag-subtree (flag)
  3503. (save-excursion
  3504. (org-back-to-heading t)
  3505. (outline-end-of-heading)
  3506. (outline-flag-region (point)
  3507. (progn (org-end-of-subtree t) (point))
  3508. flag)))
  3509. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3510. (eval-and-compile
  3511. (org-autoload "org-archive"
  3512. '(org-add-archive-files org-archive-subtree
  3513. org-archive-to-archive-sibling org-toggle-archive-tag
  3514. org-archive-subtree-default
  3515. org-archive-subtree-default-with-confirmation)))
  3516. ;; Autoload Column View Code
  3517. (declare-function org-columns-number-to-string "org-colview")
  3518. (declare-function org-columns-get-format-and-top-level "org-colview")
  3519. (declare-function org-columns-compute "org-colview")
  3520. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3521. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3522. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3523. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3524. ;; Autoload ID code
  3525. (declare-function org-id-store-link "org-id")
  3526. (declare-function org-id-locations-load "org-id")
  3527. (declare-function org-id-locations-save "org-id")
  3528. (defvar org-id-track-globally)
  3529. (org-autoload "org-id"
  3530. '(org-id-get-create org-id-new org-id-copy org-id-get
  3531. org-id-get-with-outline-path-completion
  3532. org-id-get-with-outline-drilling org-id-store-link
  3533. org-id-goto org-id-find org-id-store-link))
  3534. ;; Autoload Plotting Code
  3535. (org-autoload "org-plot"
  3536. '(org-plot/gnuplot))
  3537. ;;; Variables for pre-computed regular expressions, all buffer local
  3538. (defvar org-drawer-regexp nil
  3539. "Matches first line of a hidden block.")
  3540. (make-variable-buffer-local 'org-drawer-regexp)
  3541. (defvar org-todo-regexp nil
  3542. "Matches any of the TODO state keywords.")
  3543. (make-variable-buffer-local 'org-todo-regexp)
  3544. (defvar org-not-done-regexp nil
  3545. "Matches any of the TODO state keywords except the last one.")
  3546. (make-variable-buffer-local 'org-not-done-regexp)
  3547. (defvar org-not-done-heading-regexp nil
  3548. "Matches a TODO headline that is not done.")
  3549. (make-variable-buffer-local 'org-not-done-regexp)
  3550. (defvar org-todo-line-regexp nil
  3551. "Matches a headline and puts TODO state into group 2 if present.")
  3552. (make-variable-buffer-local 'org-todo-line-regexp)
  3553. (defvar org-complex-heading-regexp nil
  3554. "Matches a headline and puts everything into groups:
  3555. group 1: the stars
  3556. group 2: The todo keyword, maybe
  3557. group 3: Priority cookie
  3558. group 4: True headline
  3559. group 5: Tags")
  3560. (make-variable-buffer-local 'org-complex-heading-regexp)
  3561. (defvar org-complex-heading-regexp-format nil
  3562. "Printf format to make regexp to match an exact headline.
  3563. This regexp will match the headline of any node which hase the exact
  3564. headline text that is put into the format, but may have any TODO state,
  3565. priority and tags.")
  3566. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3567. (defvar org-todo-line-tags-regexp nil
  3568. "Matches a headline and puts TODO state into group 2 if present.
  3569. Also put tags into group 4 if tags are present.")
  3570. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3571. (defvar org-nl-done-regexp nil
  3572. "Matches newline followed by a headline with the DONE keyword.")
  3573. (make-variable-buffer-local 'org-nl-done-regexp)
  3574. (defvar org-looking-at-done-regexp nil
  3575. "Matches the DONE keyword a point.")
  3576. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3577. (defvar org-ds-keyword-length 12
  3578. "Maximum length of the Deadline and SCHEDULED keywords.")
  3579. (make-variable-buffer-local 'org-ds-keyword-length)
  3580. (defvar org-deadline-regexp nil
  3581. "Matches the DEADLINE keyword.")
  3582. (make-variable-buffer-local 'org-deadline-regexp)
  3583. (defvar org-deadline-time-regexp nil
  3584. "Matches the DEADLINE keyword together with a time stamp.")
  3585. (make-variable-buffer-local 'org-deadline-time-regexp)
  3586. (defvar org-deadline-line-regexp nil
  3587. "Matches the DEADLINE keyword and the rest of the line.")
  3588. (make-variable-buffer-local 'org-deadline-line-regexp)
  3589. (defvar org-scheduled-regexp nil
  3590. "Matches the SCHEDULED keyword.")
  3591. (make-variable-buffer-local 'org-scheduled-regexp)
  3592. (defvar org-scheduled-time-regexp nil
  3593. "Matches the SCHEDULED keyword together with a time stamp.")
  3594. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3595. (defvar org-closed-time-regexp nil
  3596. "Matches the CLOSED keyword together with a time stamp.")
  3597. (make-variable-buffer-local 'org-closed-time-regexp)
  3598. (defvar org-keyword-time-regexp nil
  3599. "Matches any of the 4 keywords, together with the time stamp.")
  3600. (make-variable-buffer-local 'org-keyword-time-regexp)
  3601. (defvar org-keyword-time-not-clock-regexp nil
  3602. "Matches any of the 3 keywords, together with the time stamp.")
  3603. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3604. (defvar org-maybe-keyword-time-regexp nil
  3605. "Matches a timestamp, possibly preceded by a keyword.")
  3606. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3607. (defvar org-planning-or-clock-line-re nil
  3608. "Matches a line with planning or clock info.")
  3609. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3610. (defvar org-all-time-keywords nil
  3611. "List of time keywords.")
  3612. (make-variable-buffer-local 'org-all-time-keywords)
  3613. (defconst org-plain-time-of-day-regexp
  3614. (concat
  3615. "\\(\\<[012]?[0-9]"
  3616. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3617. "\\(--?"
  3618. "\\(\\<[012]?[0-9]"
  3619. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3620. "\\)?")
  3621. "Regular expression to match a plain time or time range.
  3622. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3623. groups carry important information:
  3624. 0 the full match
  3625. 1 the first time, range or not
  3626. 8 the second time, if it is a range.")
  3627. (defconst org-plain-time-extension-regexp
  3628. (concat
  3629. "\\(\\<[012]?[0-9]"
  3630. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3631. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3632. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3633. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3634. groups carry important information:
  3635. 0 the full match
  3636. 7 hours of duration
  3637. 9 minutes of duration")
  3638. (defconst org-stamp-time-of-day-regexp
  3639. (concat
  3640. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3641. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3642. "\\(--?"
  3643. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3644. "Regular expression to match a timestamp time or time range.
  3645. After a match, the following groups carry important information:
  3646. 0 the full match
  3647. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3648. 2 the first time, range or not
  3649. 4 the second time, if it is a range.")
  3650. (defconst org-startup-options
  3651. '(("fold" org-startup-folded t)
  3652. ("overview" org-startup-folded t)
  3653. ("nofold" org-startup-folded nil)
  3654. ("showall" org-startup-folded nil)
  3655. ("showeverything" org-startup-folded showeverything)
  3656. ("content" org-startup-folded content)
  3657. ("indent" org-startup-indented t)
  3658. ("noindent" org-startup-indented nil)
  3659. ("hidestars" org-hide-leading-stars t)
  3660. ("showstars" org-hide-leading-stars nil)
  3661. ("odd" org-odd-levels-only t)
  3662. ("oddeven" org-odd-levels-only nil)
  3663. ("align" org-startup-align-all-tables t)
  3664. ("noalign" org-startup-align-all-tables nil)
  3665. ("inlineimages" org-startup-with-inline-images t)
  3666. ("noinlineimages" org-startup-with-inline-images nil)
  3667. ("customtime" org-display-custom-times t)
  3668. ("logdone" org-log-done time)
  3669. ("lognotedone" org-log-done note)
  3670. ("nologdone" org-log-done nil)
  3671. ("lognoteclock-out" org-log-note-clock-out t)
  3672. ("nolognoteclock-out" org-log-note-clock-out nil)
  3673. ("logrepeat" org-log-repeat state)
  3674. ("lognoterepeat" org-log-repeat note)
  3675. ("nologrepeat" org-log-repeat nil)
  3676. ("logreschedule" org-log-reschedule time)
  3677. ("lognotereschedule" org-log-reschedule note)
  3678. ("nologreschedule" org-log-reschedule nil)
  3679. ("logredeadline" org-log-redeadline time)
  3680. ("lognoteredeadline" org-log-redeadline note)
  3681. ("nologredeadline" org-log-redeadline nil)
  3682. ("logrefile" org-log-refile time)
  3683. ("lognoterefile" org-log-refile note)
  3684. ("nologrefile" org-log-refile nil)
  3685. ("fninline" org-footnote-define-inline t)
  3686. ("nofninline" org-footnote-define-inline nil)
  3687. ("fnlocal" org-footnote-section nil)
  3688. ("fnauto" org-footnote-auto-label t)
  3689. ("fnprompt" org-footnote-auto-label nil)
  3690. ("fnconfirm" org-footnote-auto-label confirm)
  3691. ("fnplain" org-footnote-auto-label plain)
  3692. ("fnadjust" org-footnote-auto-adjust t)
  3693. ("nofnadjust" org-footnote-auto-adjust nil)
  3694. ("constcgs" constants-unit-system cgs)
  3695. ("constSI" constants-unit-system SI)
  3696. ("noptag" org-tag-persistent-alist nil)
  3697. ("hideblocks" org-hide-block-startup t)
  3698. ("nohideblocks" org-hide-block-startup nil)
  3699. ("beamer" org-startup-with-beamer-mode t)
  3700. ("entitiespretty" org-pretty-entities t)
  3701. ("entitiesplain" org-pretty-entities nil))
  3702. "Variable associated with STARTUP options for org-mode.
  3703. Each element is a list of three items: The startup options as written
  3704. in the #+STARTUP line, the corresponding variable, and the value to
  3705. set this variable to if the option is found. An optional forth element PUSH
  3706. means to push this value onto the list in the variable.")
  3707. (defun org-set-regexps-and-options ()
  3708. "Precompute regular expressions for current buffer."
  3709. (when (org-mode-p)
  3710. (org-set-local 'org-todo-kwd-alist nil)
  3711. (org-set-local 'org-todo-key-alist nil)
  3712. (org-set-local 'org-todo-key-trigger nil)
  3713. (org-set-local 'org-todo-keywords-1 nil)
  3714. (org-set-local 'org-done-keywords nil)
  3715. (org-set-local 'org-todo-heads nil)
  3716. (org-set-local 'org-todo-sets nil)
  3717. (org-set-local 'org-todo-log-states nil)
  3718. (org-set-local 'org-file-properties nil)
  3719. (org-set-local 'org-file-tags nil)
  3720. (let ((re (org-make-options-regexp
  3721. '("CATEGORY" "TODO" "COLUMNS"
  3722. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3723. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3724. "OPTIONS")
  3725. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3726. (splitre "[ \t]+")
  3727. (scripts org-use-sub-superscripts)
  3728. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3729. tail sep kws1 prio props ftags drawers beamer-p
  3730. ext-setup-or-nil setup-contents (start 0))
  3731. (save-excursion
  3732. (save-restriction
  3733. (widen)
  3734. (goto-char (point-min))
  3735. (while (or (and ext-setup-or-nil
  3736. (string-match re ext-setup-or-nil start)
  3737. (setq start (match-end 0)))
  3738. (and (setq ext-setup-or-nil nil start 0)
  3739. (re-search-forward re nil t)))
  3740. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3741. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3742. (if (stringp value) (setq value (org-trim value)))
  3743. (cond
  3744. ((equal key "CATEGORY")
  3745. (setq cat value))
  3746. ((member key '("SEQ_TODO" "TODO"))
  3747. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3748. ((equal key "TYP_TODO")
  3749. (push (cons 'type (org-split-string value splitre)) kwds))
  3750. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3751. ;; general TODO-like setup
  3752. (push (cons (intern (downcase (match-string 1 key)))
  3753. (org-split-string value splitre)) kwds))
  3754. ((equal key "TAGS")
  3755. (setq tags (append tags (if tags '("\\n") nil)
  3756. (org-split-string value splitre))))
  3757. ((equal key "COLUMNS")
  3758. (org-set-local 'org-columns-default-format value))
  3759. ((equal key "LINK")
  3760. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3761. (push (cons (match-string 1 value)
  3762. (org-trim (match-string 2 value)))
  3763. links)))
  3764. ((equal key "PRIORITIES")
  3765. (setq prio (org-split-string value " +")))
  3766. ((equal key "PROPERTY")
  3767. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3768. (push (cons (match-string 1 value) (match-string 2 value))
  3769. props)))
  3770. ((equal key "FILETAGS")
  3771. (when (string-match "\\S-" value)
  3772. (setq ftags
  3773. (append
  3774. ftags
  3775. (apply 'append
  3776. (mapcar (lambda (x) (org-split-string x ":"))
  3777. (org-split-string value)))))))
  3778. ((equal key "DRAWERS")
  3779. (setq drawers (org-split-string value splitre)))
  3780. ((equal key "CONSTANTS")
  3781. (setq const (append const (org-split-string value splitre))))
  3782. ((equal key "STARTUP")
  3783. (let ((opts (org-split-string value splitre))
  3784. l var val)
  3785. (while (setq l (pop opts))
  3786. (when (setq l (assoc l org-startup-options))
  3787. (setq var (nth 1 l) val (nth 2 l))
  3788. (if (not (nth 3 l))
  3789. (set (make-local-variable var) val)
  3790. (if (not (listp (symbol-value var)))
  3791. (set (make-local-variable var) nil))
  3792. (set (make-local-variable var) (symbol-value var))
  3793. (add-to-list var val))))))
  3794. ((equal key "ARCHIVE")
  3795. (setq arch value)
  3796. (remove-text-properties 0 (length arch)
  3797. '(face t fontified t) arch))
  3798. ((equal key "LATEX_CLASS")
  3799. (setq beamer-p (equal value "beamer")))
  3800. ((equal key "OPTIONS")
  3801. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3802. (setq scripts (read (match-string 2 value)))))
  3803. ((equal key "SETUPFILE")
  3804. (setq setup-contents (org-file-contents
  3805. (expand-file-name
  3806. (org-remove-double-quotes value))
  3807. 'noerror))
  3808. (if (not ext-setup-or-nil)
  3809. (setq ext-setup-or-nil setup-contents start 0)
  3810. (setq ext-setup-or-nil
  3811. (concat (substring ext-setup-or-nil 0 start)
  3812. "\n" setup-contents "\n"
  3813. (substring ext-setup-or-nil start)))))
  3814. ))))
  3815. (org-set-local 'org-use-sub-superscripts scripts)
  3816. (when cat
  3817. (org-set-local 'org-category (intern cat))
  3818. (push (cons "CATEGORY" cat) props))
  3819. (when prio
  3820. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3821. (setq prio (mapcar 'string-to-char prio))
  3822. (org-set-local 'org-highest-priority (nth 0 prio))
  3823. (org-set-local 'org-lowest-priority (nth 1 prio))
  3824. (org-set-local 'org-default-priority (nth 2 prio)))
  3825. (and props (org-set-local 'org-file-properties (nreverse props)))
  3826. (and ftags (org-set-local 'org-file-tags
  3827. (mapcar 'org-add-prop-inherited ftags)))
  3828. (and drawers (org-set-local 'org-drawers drawers))
  3829. (and arch (org-set-local 'org-archive-location arch))
  3830. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3831. ;; Process the TODO keywords
  3832. (unless kwds
  3833. ;; Use the global values as if they had been given locally.
  3834. (setq kwds (default-value 'org-todo-keywords))
  3835. (if (stringp (car kwds))
  3836. (setq kwds (list (cons org-todo-interpretation
  3837. (default-value 'org-todo-keywords)))))
  3838. (setq kwds (reverse kwds)))
  3839. (setq kwds (nreverse kwds))
  3840. (let (inter kws kw)
  3841. (while (setq kws (pop kwds))
  3842. (let ((kws (or
  3843. (run-hook-with-args-until-success
  3844. 'org-todo-setup-filter-hook kws)
  3845. kws)))
  3846. (setq inter (pop kws) sep (member "|" kws)
  3847. kws0 (delete "|" (copy-sequence kws))
  3848. kwsa nil
  3849. kws1 (mapcar
  3850. (lambda (x)
  3851. ;; 1 2
  3852. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3853. (progn
  3854. (setq kw (match-string 1 x)
  3855. key (and (match-end 2) (match-string 2 x))
  3856. log (org-extract-log-state-settings x))
  3857. (push (cons kw (and key (string-to-char key))) kwsa)
  3858. (and log (push log org-todo-log-states))
  3859. kw)
  3860. (error "Invalid TODO keyword %s" x)))
  3861. kws0)
  3862. kwsa (if kwsa (append '((:startgroup))
  3863. (nreverse kwsa)
  3864. '((:endgroup))))
  3865. hw (car kws1)
  3866. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3867. tail (list inter hw (car dws) (org-last dws))))
  3868. (add-to-list 'org-todo-heads hw 'append)
  3869. (push kws1 org-todo-sets)
  3870. (setq org-done-keywords (append org-done-keywords dws nil))
  3871. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3872. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3873. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3874. (setq org-todo-sets (nreverse org-todo-sets)
  3875. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3876. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3877. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3878. ;; Process the constants
  3879. (when const
  3880. (let (e cst)
  3881. (while (setq e (pop const))
  3882. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3883. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3884. (setq org-table-formula-constants-local cst)))
  3885. ;; Process the tags.
  3886. (when tags
  3887. (let (e tgs)
  3888. (while (setq e (pop tags))
  3889. (cond
  3890. ((equal e "{") (push '(:startgroup) tgs))
  3891. ((equal e "}") (push '(:endgroup) tgs))
  3892. ((equal e "\\n") (push '(:newline) tgs))
  3893. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3894. (push (cons (match-string 1 e)
  3895. (string-to-char (match-string 2 e)))
  3896. tgs))
  3897. (t (push (list e) tgs))))
  3898. (org-set-local 'org-tag-alist nil)
  3899. (while (setq e (pop tgs))
  3900. (or (and (stringp (car e))
  3901. (assoc (car e) org-tag-alist))
  3902. (push e org-tag-alist)))))
  3903. ;; Compute the regular expressions and other local variables
  3904. (if (not org-done-keywords)
  3905. (setq org-done-keywords (and org-todo-keywords-1
  3906. (list (org-last org-todo-keywords-1)))))
  3907. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3908. (length org-scheduled-string)
  3909. (length org-clock-string)
  3910. (length org-closed-string)))
  3911. org-drawer-regexp
  3912. (concat "^[ \t]*:\\("
  3913. (mapconcat 'regexp-quote org-drawers "\\|")
  3914. "\\):[ \t]*$")
  3915. org-not-done-keywords
  3916. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3917. org-todo-regexp
  3918. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3919. "\\|") "\\)\\>")
  3920. org-not-done-regexp
  3921. (concat "\\<\\("
  3922. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3923. "\\)\\>")
  3924. org-not-done-heading-regexp
  3925. (concat "^\\(\\*+\\)[ \t]+\\("
  3926. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3927. "\\)\\>")
  3928. org-todo-line-regexp
  3929. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3930. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3931. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3932. org-complex-heading-regexp
  3933. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3934. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3935. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3936. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  3937. org-complex-heading-regexp-format
  3938. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3939. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3940. "\\)\\>\\)?"
  3941. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3942. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3943. "[ \t]*\\(%s\\)"
  3944. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3945. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  3946. org-nl-done-regexp
  3947. (concat "\n\\*+[ \t]+"
  3948. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3949. "\\)" "\\>")
  3950. org-todo-line-tags-regexp
  3951. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3952. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3953. (org-re
  3954. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  3955. org-looking-at-done-regexp
  3956. (concat "^" "\\(?:"
  3957. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3958. "\\>")
  3959. org-deadline-regexp (concat "\\<" org-deadline-string)
  3960. org-deadline-time-regexp
  3961. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3962. org-deadline-line-regexp
  3963. (concat "\\<\\(" org-deadline-string "\\).*")
  3964. org-scheduled-regexp
  3965. (concat "\\<" org-scheduled-string)
  3966. org-scheduled-time-regexp
  3967. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3968. org-closed-time-regexp
  3969. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3970. org-keyword-time-regexp
  3971. (concat "\\<\\(" org-scheduled-string
  3972. "\\|" org-deadline-string
  3973. "\\|" org-closed-string
  3974. "\\|" org-clock-string "\\)"
  3975. " *[[<]\\([^]>]+\\)[]>]")
  3976. org-keyword-time-not-clock-regexp
  3977. (concat "\\<\\(" org-scheduled-string
  3978. "\\|" org-deadline-string
  3979. "\\|" org-closed-string
  3980. "\\)"
  3981. " *[[<]\\([^]>]+\\)[]>]")
  3982. org-maybe-keyword-time-regexp
  3983. (concat "\\(\\<\\(" org-scheduled-string
  3984. "\\|" org-deadline-string
  3985. "\\|" org-closed-string
  3986. "\\|" org-clock-string "\\)\\)?"
  3987. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3988. org-planning-or-clock-line-re
  3989. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3990. "\\|" org-deadline-string
  3991. "\\|" org-closed-string "\\|" org-clock-string
  3992. "\\)\\>\\)")
  3993. org-all-time-keywords
  3994. (mapcar (lambda (w) (substring w 0 -1))
  3995. (list org-scheduled-string org-deadline-string
  3996. org-clock-string org-closed-string))
  3997. )
  3998. (org-compute-latex-and-specials-regexp)
  3999. (org-set-font-lock-defaults))))
  4000. (defun org-file-contents (file &optional noerror)
  4001. "Return the contents of FILE, as a string."
  4002. (if (or (not file)
  4003. (not (file-readable-p file)))
  4004. (if noerror
  4005. (progn
  4006. (message "Cannot read file \"%s\"" file)
  4007. (ding) (sit-for 2)
  4008. "")
  4009. (error "Cannot read file \"%s\"" file))
  4010. (with-temp-buffer
  4011. (insert-file-contents file)
  4012. (buffer-string))))
  4013. (defun org-extract-log-state-settings (x)
  4014. "Extract the log state setting from a TODO keyword string.
  4015. This will extract info from a string like \"WAIT(w@/!)\"."
  4016. (let (kw key log1 log2)
  4017. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4018. (setq kw (match-string 1 x)
  4019. key (and (match-end 2) (match-string 2 x))
  4020. log1 (and (match-end 3) (match-string 3 x))
  4021. log2 (and (match-end 4) (match-string 4 x)))
  4022. (and (or log1 log2)
  4023. (list kw
  4024. (and log1 (if (equal log1 "!") 'time 'note))
  4025. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4026. (defun org-remove-keyword-keys (list)
  4027. "Remove a pair of parenthesis at the end of each string in LIST."
  4028. (mapcar (lambda (x)
  4029. (if (string-match "(.*)$" x)
  4030. (substring x 0 (match-beginning 0))
  4031. x))
  4032. list))
  4033. (defun org-assign-fast-keys (alist)
  4034. "Assign fast keys to a keyword-key alist.
  4035. Respect keys that are already there."
  4036. (let (new e (alt ?0))
  4037. (while (setq e (pop alist))
  4038. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4039. (cdr e)) ;; Key already assigned.
  4040. (push e new)
  4041. (let ((clist (string-to-list (downcase (car e))))
  4042. (used (append new alist)))
  4043. (when (= (car clist) ?@)
  4044. (pop clist))
  4045. (while (and clist (rassoc (car clist) used))
  4046. (pop clist))
  4047. (unless clist
  4048. (while (rassoc alt used)
  4049. (incf alt)))
  4050. (push (cons (car e) (or (car clist) alt)) new))))
  4051. (nreverse new)))
  4052. ;;; Some variables used in various places
  4053. (defvar org-window-configuration nil
  4054. "Used in various places to store a window configuration.")
  4055. (defvar org-selected-window nil
  4056. "Used in various places to store a window configuration.")
  4057. (defvar org-finish-function nil
  4058. "Function to be called when `C-c C-c' is used.
  4059. This is for getting out of special buffers like remember.")
  4060. ;; FIXME: Occasionally check by commenting these, to make sure
  4061. ;; no other functions uses these, forgetting to let-bind them.
  4062. (defvar entry)
  4063. (defvar last-state)
  4064. (defvar date)
  4065. ;; Defined somewhere in this file, but used before definition.
  4066. (defvar org-entities) ;; defined in org-entities.el
  4067. (defvar org-struct-menu)
  4068. (defvar org-org-menu)
  4069. (defvar org-tbl-menu)
  4070. ;;;; Define the Org-mode
  4071. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4072. (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"))
  4073. ;; We use a before-change function to check if a table might need
  4074. ;; an update.
  4075. (defvar org-table-may-need-update t
  4076. "Indicates that a table might need an update.
  4077. This variable is set by `org-before-change-function'.
  4078. `org-table-align' sets it back to nil.")
  4079. (defun org-before-change-function (beg end)
  4080. "Every change indicates that a table might need an update."
  4081. (setq org-table-may-need-update t))
  4082. (defvar org-mode-map)
  4083. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4084. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4085. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4086. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4087. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4088. (defvar org-table-buffer-is-an nil)
  4089. (defconst org-outline-regexp "\\*+ ")
  4090. ;;;###autoload
  4091. (define-derived-mode org-mode outline-mode "Org"
  4092. "Outline-based notes management and organizer, alias
  4093. \"Carsten's outline-mode for keeping track of everything.\"
  4094. Org-mode develops organizational tasks around a NOTES file which
  4095. contains information about projects as plain text. Org-mode is
  4096. implemented on top of outline-mode, which is ideal to keep the content
  4097. of large files well structured. It supports ToDo items, deadlines and
  4098. time stamps, which magically appear in the diary listing of the Emacs
  4099. calendar. Tables are easily created with a built-in table editor.
  4100. Plain text URL-like links connect to websites, emails (VM), Usenet
  4101. messages (Gnus), BBDB entries, and any files related to the project.
  4102. For printing and sharing of notes, an Org-mode file (or a part of it)
  4103. can be exported as a structured ASCII or HTML file.
  4104. The following commands are available:
  4105. \\{org-mode-map}"
  4106. ;; Get rid of Outline menus, they are not needed
  4107. ;; Need to do this here because define-derived-mode sets up
  4108. ;; the keymap so late. Still, it is a waste to call this each time
  4109. ;; we switch another buffer into org-mode.
  4110. (if (featurep 'xemacs)
  4111. (when (boundp 'outline-mode-menu-heading)
  4112. ;; Assume this is Greg's port, it uses easymenu
  4113. (easy-menu-remove outline-mode-menu-heading)
  4114. (easy-menu-remove outline-mode-menu-show)
  4115. (easy-menu-remove outline-mode-menu-hide))
  4116. (define-key org-mode-map [menu-bar headings] 'undefined)
  4117. (define-key org-mode-map [menu-bar hide] 'undefined)
  4118. (define-key org-mode-map [menu-bar show] 'undefined))
  4119. (org-load-modules-maybe)
  4120. (easy-menu-add org-org-menu)
  4121. (easy-menu-add org-tbl-menu)
  4122. (org-install-agenda-files-menu)
  4123. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4124. (add-to-invisibility-spec '(org-cwidth))
  4125. (add-to-invisibility-spec '(org-hide-block . t))
  4126. (when (featurep 'xemacs)
  4127. (org-set-local 'line-move-ignore-invisible t))
  4128. (org-set-local 'outline-regexp org-outline-regexp)
  4129. (org-set-local 'outline-level 'org-outline-level)
  4130. (when (and org-ellipsis
  4131. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4132. (fboundp 'make-glyph-code))
  4133. (unless org-display-table
  4134. (setq org-display-table (make-display-table)))
  4135. (set-display-table-slot
  4136. org-display-table 4
  4137. (vconcat (mapcar
  4138. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4139. org-ellipsis)))
  4140. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4141. (setq buffer-display-table org-display-table))
  4142. (org-set-regexps-and-options)
  4143. (when (and org-tag-faces (not org-tags-special-faces-re))
  4144. ;; tag faces set outside customize.... force initialization.
  4145. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4146. ;; Calc embedded
  4147. (org-set-local 'calc-embedded-open-mode "# ")
  4148. (modify-syntax-entry ?@ "w")
  4149. (if org-startup-truncated (setq truncate-lines t))
  4150. (org-set-local 'font-lock-unfontify-region-function
  4151. 'org-unfontify-region)
  4152. ;; Activate before-change-function
  4153. (org-set-local 'org-table-may-need-update t)
  4154. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4155. 'local)
  4156. ;; Check for running clock before killing a buffer
  4157. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4158. ;; Paragraphs and auto-filling
  4159. (org-set-autofill-regexps)
  4160. (setq indent-line-function 'org-indent-line-function)
  4161. (org-update-radio-target-regexp)
  4162. ;; Beginning/end of defun
  4163. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4164. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4165. ;; Make sure dependence stuff works reliably, even for users who set it
  4166. ;; too late :-(
  4167. (if org-enforce-todo-dependencies
  4168. (add-hook 'org-blocker-hook
  4169. 'org-block-todo-from-children-or-siblings-or-parent)
  4170. (remove-hook 'org-blocker-hook
  4171. 'org-block-todo-from-children-or-siblings-or-parent))
  4172. (if org-enforce-todo-checkbox-dependencies
  4173. (add-hook 'org-blocker-hook
  4174. 'org-block-todo-from-checkboxes)
  4175. (remove-hook 'org-blocker-hook
  4176. 'org-block-todo-from-checkboxes))
  4177. ;; Comment characters
  4178. (org-set-local 'comment-start "#")
  4179. (org-set-local 'comment-padding " ")
  4180. ;; Align options lines
  4181. (org-set-local
  4182. 'align-mode-rules-list
  4183. '((org-in-buffer-settings
  4184. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4185. (modes . '(org-mode)))))
  4186. ;; Imenu
  4187. (org-set-local 'imenu-create-index-function
  4188. 'org-imenu-get-tree)
  4189. ;; Make isearch reveal context
  4190. (if (or (featurep 'xemacs)
  4191. (not (boundp 'outline-isearch-open-invisible-function)))
  4192. ;; Emacs 21 and XEmacs make use of the hook
  4193. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4194. ;; Emacs 22 deals with this through a special variable
  4195. (org-set-local 'outline-isearch-open-invisible-function
  4196. (lambda (&rest ignore) (org-show-context 'isearch))))
  4197. ;; Turn on org-beamer-mode?
  4198. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4199. ;; Setup the pcomplete hooks
  4200. (set (make-local-variable 'pcomplete-command-completion-function)
  4201. 'org-complete-initial)
  4202. (set (make-local-variable 'pcomplete-command-name-function)
  4203. 'org-command-at-point)
  4204. (set (make-local-variable 'pcomplete-default-completion-function)
  4205. 'ignore)
  4206. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4207. 'org-parse-arguments)
  4208. (set (make-local-variable 'pcomplete-termination-string) "")
  4209. ;; If empty file that did not turn on org-mode automatically, make it to.
  4210. (if (and org-insert-mode-line-in-empty-file
  4211. (interactive-p)
  4212. (= (point-min) (point-max)))
  4213. (insert "# -*- mode: org -*-\n\n"))
  4214. (unless org-inhibit-startup
  4215. (when org-startup-align-all-tables
  4216. (let ((bmp (buffer-modified-p)))
  4217. (org-table-map-tables 'org-table-align 'quietly)
  4218. (set-buffer-modified-p bmp)))
  4219. (when org-startup-with-inline-images
  4220. (org-display-inline-images))
  4221. (when org-startup-indented
  4222. (require 'org-indent)
  4223. (org-indent-mode 1))
  4224. (unless org-inhibit-startup-visibility-stuff
  4225. (org-set-startup-visibility))))
  4226. (when (fboundp 'abbrev-table-put)
  4227. (abbrev-table-put org-mode-abbrev-table
  4228. :parents (list text-mode-abbrev-table)))
  4229. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4230. (defun org-current-time ()
  4231. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4232. (if (> (car org-time-stamp-rounding-minutes) 1)
  4233. (let ((r (car org-time-stamp-rounding-minutes))
  4234. (time (decode-time)))
  4235. (apply 'encode-time
  4236. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4237. (nthcdr 2 time))))
  4238. (current-time)))
  4239. (defun org-today ()
  4240. "Return today date, considering `org-extend-today-until'."
  4241. (time-to-days
  4242. (time-subtract (current-time)
  4243. (list 0 (* 3600 org-extend-today-until) 0))))
  4244. ;;;; Font-Lock stuff, including the activators
  4245. (defvar org-mouse-map (make-sparse-keymap))
  4246. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4247. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4248. (when org-mouse-1-follows-link
  4249. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4250. (when org-tab-follows-link
  4251. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4252. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4253. (require 'font-lock)
  4254. (defconst org-non-link-chars "]\t\n\r<>")
  4255. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4256. "shell" "elisp" "doi" "message"))
  4257. (defvar org-link-types-re nil
  4258. "Matches a link that has a url-like prefix like \"http:\"")
  4259. (defvar org-link-re-with-space nil
  4260. "Matches a link with spaces, optional angular brackets around it.")
  4261. (defvar org-link-re-with-space2 nil
  4262. "Matches a link with spaces, optional angular brackets around it.")
  4263. (defvar org-link-re-with-space3 nil
  4264. "Matches a link with spaces, only for internal part in bracket links.")
  4265. (defvar org-angle-link-re nil
  4266. "Matches link with angular brackets, spaces are allowed.")
  4267. (defvar org-plain-link-re nil
  4268. "Matches plain link, without spaces.")
  4269. (defvar org-bracket-link-regexp nil
  4270. "Matches a link in double brackets.")
  4271. (defvar org-bracket-link-analytic-regexp nil
  4272. "Regular expression used to analyze links.
  4273. Here is what the match groups contain after a match:
  4274. 1: http:
  4275. 2: http
  4276. 3: path
  4277. 4: [desc]
  4278. 5: desc")
  4279. (defvar org-bracket-link-analytic-regexp++ nil
  4280. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4281. (defvar org-any-link-re nil
  4282. "Regular expression matching any link.")
  4283. (defcustom org-match-sexp-depth 3
  4284. "Number of stacked braces for sub/superscript matching.
  4285. This has to be set before loading org.el to be effective."
  4286. :group 'org-export-translation ; ??????????????????????????/
  4287. :type 'integer)
  4288. (defun org-create-multibrace-regexp (left right n)
  4289. "Create a regular expression which will match a balanced sexp.
  4290. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4291. as single character strings.
  4292. The regexp returned will match the entire expression including the
  4293. delimiters. It will also define a single group which contains the
  4294. match except for the outermost delimiters. The maximum depth of
  4295. stacked delimiters is N. Escaping delimiters is not possible."
  4296. (let* ((nothing (concat "[^" left right "]*?"))
  4297. (or "\\|")
  4298. (re nothing)
  4299. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4300. (while (> n 1)
  4301. (setq n (1- n)
  4302. re (concat re or next)
  4303. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4304. (concat left "\\(" re "\\)" right)))
  4305. (defvar org-match-substring-regexp
  4306. (concat
  4307. "\\([^\\]\\)\\([_^]\\)\\("
  4308. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4309. "\\|"
  4310. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4311. "\\|"
  4312. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4313. "The regular expression matching a sub- or superscript.")
  4314. (defvar org-match-substring-with-braces-regexp
  4315. (concat
  4316. "\\([^\\]\\)\\([_^]\\)\\("
  4317. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4318. "\\)")
  4319. "The regular expression matching a sub- or superscript, forcing braces.")
  4320. (defun org-make-link-regexps ()
  4321. "Update the link regular expressions.
  4322. This should be called after the variable `org-link-types' has changed."
  4323. (setq org-link-types-re
  4324. (concat
  4325. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4326. org-link-re-with-space
  4327. (concat
  4328. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4329. "\\([^" org-non-link-chars " ]"
  4330. "[^" org-non-link-chars "]*"
  4331. "[^" org-non-link-chars " ]\\)>?")
  4332. org-link-re-with-space2
  4333. (concat
  4334. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4335. "\\([^" org-non-link-chars " ]"
  4336. "[^\t\n\r]*"
  4337. "[^" org-non-link-chars " ]\\)>?")
  4338. org-link-re-with-space3
  4339. (concat
  4340. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4341. "\\([^" org-non-link-chars " ]"
  4342. "[^\t\n\r]*\\)")
  4343. org-angle-link-re
  4344. (concat
  4345. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4346. "\\([^" org-non-link-chars " ]"
  4347. "[^" org-non-link-chars "]*"
  4348. "\\)>")
  4349. org-plain-link-re
  4350. (concat
  4351. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4352. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4353. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4354. org-bracket-link-regexp
  4355. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4356. org-bracket-link-analytic-regexp
  4357. (concat
  4358. "\\[\\["
  4359. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4360. "\\([^]]+\\)"
  4361. "\\]"
  4362. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4363. "\\]")
  4364. org-bracket-link-analytic-regexp++
  4365. (concat
  4366. "\\[\\["
  4367. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4368. "\\([^]]+\\)"
  4369. "\\]"
  4370. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4371. "\\]")
  4372. org-any-link-re
  4373. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4374. org-angle-link-re "\\)\\|\\("
  4375. org-plain-link-re "\\)")))
  4376. (org-make-link-regexps)
  4377. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4378. "Regular expression for fast time stamp matching.")
  4379. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4380. "Regular expression for fast time stamp matching.")
  4381. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4382. "Regular expression matching time strings for analysis.
  4383. This one does not require the space after the date, so it can be used
  4384. on a string that terminates immediately after the date.")
  4385. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4386. "Regular expression matching time strings for analysis.")
  4387. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4388. "Regular expression matching time stamps, with groups.")
  4389. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4390. "Regular expression matching time stamps (also [..]), with groups.")
  4391. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4392. "Regular expression matching a time stamp range.")
  4393. (defconst org-tr-regexp-both
  4394. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4395. "Regular expression matching a time stamp range.")
  4396. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4397. org-ts-regexp "\\)?")
  4398. "Regular expression matching a time stamp or time stamp range.")
  4399. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4400. org-ts-regexp-both "\\)?")
  4401. "Regular expression matching a time stamp or time stamp range.
  4402. The time stamps may be either active or inactive.")
  4403. (defvar org-emph-face nil)
  4404. (defun org-do-emphasis-faces (limit)
  4405. "Run through the buffer and add overlays to links."
  4406. (let (rtn a)
  4407. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4408. (if (not (= (char-after (match-beginning 3))
  4409. (char-after (match-beginning 4))))
  4410. (progn
  4411. (setq rtn t)
  4412. (setq a (assoc (match-string 3) org-emphasis-alist))
  4413. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4414. 'face
  4415. (nth 1 a))
  4416. (and (nth 4 a)
  4417. (org-remove-flyspell-overlays-in
  4418. (match-beginning 0) (match-end 0)))
  4419. (add-text-properties (match-beginning 2) (match-end 2)
  4420. '(font-lock-multiline t org-emphasis t))
  4421. (when org-hide-emphasis-markers
  4422. (add-text-properties (match-end 4) (match-beginning 5)
  4423. '(invisible org-link))
  4424. (add-text-properties (match-beginning 3) (match-end 3)
  4425. '(invisible org-link)))))
  4426. (backward-char 1))
  4427. rtn))
  4428. (defun org-emphasize (&optional char)
  4429. "Insert or change an emphasis, i.e. a font like bold or italic.
  4430. If there is an active region, change that region to a new emphasis.
  4431. If there is no region, just insert the marker characters and position
  4432. the cursor between them.
  4433. CHAR should be either the marker character, or the first character of the
  4434. HTML tag associated with that emphasis. If CHAR is a space, the means
  4435. to remove the emphasis of the selected region.
  4436. If char is not given (for example in an interactive call) it
  4437. will be prompted for."
  4438. (interactive)
  4439. (let ((eal org-emphasis-alist) e det
  4440. (erc org-emphasis-regexp-components)
  4441. (prompt "")
  4442. (string "") beg end move tag c s)
  4443. (if (org-region-active-p)
  4444. (setq beg (region-beginning) end (region-end)
  4445. string (buffer-substring beg end))
  4446. (setq move t))
  4447. (while (setq e (pop eal))
  4448. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4449. c (aref tag 0))
  4450. (push (cons c (string-to-char (car e))) det)
  4451. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4452. (substring tag 1)))))
  4453. (setq det (nreverse det))
  4454. (unless char
  4455. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4456. (setq char (read-char-exclusive)))
  4457. (setq char (or (cdr (assoc char det)) char))
  4458. (if (equal char ?\ )
  4459. (setq s "" move nil)
  4460. (unless (assoc (char-to-string char) org-emphasis-alist)
  4461. (error "No such emphasis marker: \"%c\"" char))
  4462. (setq s (char-to-string char)))
  4463. (while (and (> (length string) 1)
  4464. (equal (substring string 0 1) (substring string -1))
  4465. (assoc (substring string 0 1) org-emphasis-alist))
  4466. (setq string (substring string 1 -1)))
  4467. (setq string (concat s string s))
  4468. (if beg (delete-region beg end))
  4469. (unless (or (bolp)
  4470. (string-match (concat "[" (nth 0 erc) "\n]")
  4471. (char-to-string (char-before (point)))))
  4472. (insert " "))
  4473. (unless (or (eobp)
  4474. (string-match (concat "[" (nth 1 erc) "\n]")
  4475. (char-to-string (char-after (point)))))
  4476. (insert " ") (backward-char 1))
  4477. (insert string)
  4478. (and move (backward-char 1))))
  4479. (defconst org-nonsticky-props
  4480. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4481. (defsubst org-rear-nonsticky-at (pos)
  4482. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4483. (defun org-activate-plain-links (limit)
  4484. "Run through the buffer and add overlays to links."
  4485. (catch 'exit
  4486. (let (f)
  4487. (if (re-search-forward org-plain-link-re limit t)
  4488. (progn
  4489. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4490. (setq f (get-text-property (match-beginning 0) 'face))
  4491. (if (or (eq f 'org-tag)
  4492. (and (listp f) (memq 'org-tag f)))
  4493. nil
  4494. (add-text-properties (match-beginning 0) (match-end 0)
  4495. (list 'mouse-face 'highlight
  4496. 'face 'org-link
  4497. 'keymap org-mouse-map))
  4498. (org-rear-nonsticky-at (match-end 0)))
  4499. t)))))
  4500. (defun org-activate-code (limit)
  4501. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4502. (progn
  4503. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4504. (remove-text-properties (match-beginning 0) (match-end 0)
  4505. '(display t invisible t intangible t))
  4506. t)))
  4507. (defcustom org-src-fontify-natively nil
  4508. "When non-nil, fontify code in code blocks."
  4509. :type 'boolean
  4510. :group 'org-appearance
  4511. :group 'org-babel)
  4512. (defun org-fontify-meta-lines-and-blocks (limit)
  4513. "Fontify #+ lines and blocks, in the correct ways."
  4514. (let ((case-fold-search t))
  4515. (if (re-search-forward
  4516. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4517. limit t)
  4518. (let ((beg (match-beginning 0))
  4519. (block-start (match-end 0))
  4520. (block-end nil)
  4521. (lang (match-string 7))
  4522. (beg1 (line-beginning-position 2))
  4523. (dc1 (downcase (match-string 2)))
  4524. (dc3 (downcase (match-string 3)))
  4525. end end1 quoting block-type)
  4526. (cond
  4527. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4528. ;; a single line of backend-specific content
  4529. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4530. (remove-text-properties (match-beginning 0) (match-end 0)
  4531. '(display t invisible t intangible t))
  4532. (add-text-properties (match-beginning 1) (match-end 3)
  4533. '(font-lock-fontified t face org-meta-line))
  4534. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4535. '(font-lock-fontified t face org-block))
  4536. ; for backend-specific code
  4537. t)
  4538. ((and (match-end 4) (equal dc3 "begin"))
  4539. ;; Truly a block
  4540. (setq block-type (downcase (match-string 5))
  4541. quoting (member block-type org-protecting-blocks))
  4542. (when (re-search-forward
  4543. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4544. nil t) ;; on purpose, we look further than LIMIT
  4545. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4546. (setq block-end (match-beginning 0))
  4547. (when quoting
  4548. (remove-text-properties beg end
  4549. '(display t invisible t intangible t)))
  4550. (add-text-properties
  4551. beg end
  4552. '(font-lock-fontified t font-lock-multiline t))
  4553. (add-text-properties beg beg1 '(face org-meta-line))
  4554. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4555. ; for end_src
  4556. (cond
  4557. ((and lang org-src-fontify-natively)
  4558. (org-src-font-lock-fontify-block lang block-start block-end))
  4559. (quoting
  4560. (add-text-properties beg1 (+ end1 1) '(face
  4561. org-block)))
  4562. ; end of source block
  4563. ((not org-fontify-quote-and-verse-blocks))
  4564. ((string= block-type "quote")
  4565. (add-text-properties beg1 end1 '(face org-quote)))
  4566. ((string= block-type "verse")
  4567. (add-text-properties beg1 end1 '(face org-verse))))
  4568. t))
  4569. ((member dc1 '("title:" "author:" "email:" "date:"))
  4570. (add-text-properties
  4571. beg (match-end 3)
  4572. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4573. '(font-lock-fontified t invisible t)
  4574. '(font-lock-fontified t face org-document-info-keyword)))
  4575. (add-text-properties
  4576. (match-beginning 6) (match-end 6)
  4577. (if (string-equal dc1 "title:")
  4578. '(font-lock-fontified t face org-document-title)
  4579. '(font-lock-fontified t face org-document-info))))
  4580. ((not (member (char-after beg) '(?\ ?\t)))
  4581. ;; just any other in-buffer setting, but not indented
  4582. (add-text-properties
  4583. beg (match-end 0)
  4584. '(font-lock-fontified t face org-meta-line))
  4585. t)
  4586. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4587. "orgtbl:" "tblfm:" "tblname:" "result:"
  4588. "results:" "source:" "srcname:" "call:"))
  4589. (and (match-end 4) (equal dc3 "attr")))
  4590. (add-text-properties
  4591. beg (match-end 0)
  4592. '(font-lock-fontified t face org-meta-line))
  4593. t)
  4594. ((member dc3 '(" " ""))
  4595. (add-text-properties
  4596. beg (match-end 0)
  4597. '(font-lock-fontified t face font-lock-comment-face)))
  4598. (t nil))))))
  4599. (defun org-activate-angle-links (limit)
  4600. "Run through the buffer and add overlays to links."
  4601. (if (re-search-forward org-angle-link-re limit t)
  4602. (progn
  4603. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4604. (add-text-properties (match-beginning 0) (match-end 0)
  4605. (list 'mouse-face 'highlight
  4606. 'keymap org-mouse-map))
  4607. (org-rear-nonsticky-at (match-end 0))
  4608. t)))
  4609. (defun org-activate-footnote-links (limit)
  4610. "Run through the buffer and add overlays to links."
  4611. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4612. limit t)
  4613. (progn
  4614. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4615. (add-text-properties (match-beginning 2) (match-end 2)
  4616. (list 'mouse-face 'highlight
  4617. 'keymap org-mouse-map
  4618. 'help-echo
  4619. (if (= (point-at-bol) (match-beginning 2))
  4620. "Footnote definition"
  4621. "Footnote reference")
  4622. ))
  4623. (org-rear-nonsticky-at (match-end 2))
  4624. t)))
  4625. (defun org-activate-bracket-links (limit)
  4626. "Run through the buffer and add overlays to bracketed links."
  4627. (if (re-search-forward org-bracket-link-regexp limit t)
  4628. (let* ((help (concat "LINK: "
  4629. (org-match-string-no-properties 1)))
  4630. ;; FIXME: above we should remove the escapes.
  4631. ;; but that requires another match, protecting match data,
  4632. ;; a lot of overhead for font-lock.
  4633. (ip (org-maybe-intangible
  4634. (list 'invisible 'org-link
  4635. 'keymap org-mouse-map 'mouse-face 'highlight
  4636. 'font-lock-multiline t 'help-echo help)))
  4637. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4638. 'font-lock-multiline t 'help-echo help)))
  4639. ;; We need to remove the invisible property here. Table narrowing
  4640. ;; may have made some of this invisible.
  4641. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4642. (remove-text-properties (match-beginning 0) (match-end 0)
  4643. '(invisible nil))
  4644. (if (match-end 3)
  4645. (progn
  4646. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4647. (org-rear-nonsticky-at (match-beginning 3))
  4648. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4649. (org-rear-nonsticky-at (match-end 3))
  4650. (add-text-properties (match-end 3) (match-end 0) ip)
  4651. (org-rear-nonsticky-at (match-end 0)))
  4652. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4653. (org-rear-nonsticky-at (match-beginning 1))
  4654. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4655. (org-rear-nonsticky-at (match-end 1))
  4656. (add-text-properties (match-end 1) (match-end 0) ip)
  4657. (org-rear-nonsticky-at (match-end 0)))
  4658. t)))
  4659. (defun org-activate-dates (limit)
  4660. "Run through the buffer and add overlays to dates."
  4661. (if (re-search-forward org-tsr-regexp-both limit t)
  4662. (progn
  4663. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4664. (add-text-properties (match-beginning 0) (match-end 0)
  4665. (list 'mouse-face 'highlight
  4666. 'keymap org-mouse-map))
  4667. (org-rear-nonsticky-at (match-end 0))
  4668. (when org-display-custom-times
  4669. (if (match-end 3)
  4670. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4671. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4672. t)))
  4673. (defvar org-target-link-regexp nil
  4674. "Regular expression matching radio targets in plain text.")
  4675. (make-variable-buffer-local 'org-target-link-regexp)
  4676. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4677. "Regular expression matching a link target.")
  4678. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4679. "Regular expression matching a radio target.")
  4680. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4681. "Regular expression matching any target.")
  4682. (defun org-activate-target-links (limit)
  4683. "Run through the buffer and add overlays to target matches."
  4684. (when org-target-link-regexp
  4685. (let ((case-fold-search t))
  4686. (if (re-search-forward org-target-link-regexp limit t)
  4687. (progn
  4688. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4689. (add-text-properties (match-beginning 0) (match-end 0)
  4690. (list 'mouse-face 'highlight
  4691. 'keymap org-mouse-map
  4692. 'help-echo "Radio target link"
  4693. 'org-linked-text t))
  4694. (org-rear-nonsticky-at (match-end 0))
  4695. t)))))
  4696. (defun org-update-radio-target-regexp ()
  4697. "Find all radio targets in this file and update the regular expression."
  4698. (interactive)
  4699. (when (memq 'radio org-activate-links)
  4700. (setq org-target-link-regexp
  4701. (org-make-target-link-regexp (org-all-targets 'radio)))
  4702. (org-restart-font-lock)))
  4703. (defun org-hide-wide-columns (limit)
  4704. (let (s e)
  4705. (setq s (text-property-any (point) (or limit (point-max))
  4706. 'org-cwidth t))
  4707. (when s
  4708. (setq e (next-single-property-change s 'org-cwidth))
  4709. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4710. (goto-char e)
  4711. t)))
  4712. (defvar org-latex-and-specials-regexp nil
  4713. "Regular expression for highlighting export special stuff.")
  4714. (defvar org-match-substring-regexp)
  4715. (defvar org-match-substring-with-braces-regexp)
  4716. ;; This should be with the exporter code, but we also use if for font-locking
  4717. (defconst org-export-html-special-string-regexps
  4718. '(("\\\\-" . "&shy;")
  4719. ("---\\([^-]\\)" . "&mdash;\\1")
  4720. ("--\\([^-]\\)" . "&ndash;\\1")
  4721. ("\\.\\.\\." . "&hellip;"))
  4722. "Regular expressions for special string conversion.")
  4723. (defun org-compute-latex-and-specials-regexp ()
  4724. "Compute regular expression for stuff treated specially by exporters."
  4725. (if (not org-highlight-latex-fragments-and-specials)
  4726. (org-set-local 'org-latex-and-specials-regexp nil)
  4727. (require 'org-exp)
  4728. (let*
  4729. ((matchers (plist-get org-format-latex-options :matchers))
  4730. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4731. org-latex-regexps)))
  4732. (org-export-allow-BIND nil)
  4733. (options (org-combine-plists (org-default-export-plist)
  4734. (org-infile-export-plist)))
  4735. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4736. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4737. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4738. (org-export-html-expand (plist-get options :expand-quoted-html))
  4739. (org-export-with-special-strings (plist-get options :special-strings))
  4740. (re-sub
  4741. (cond
  4742. ((equal org-export-with-sub-superscripts '{})
  4743. (list org-match-substring-with-braces-regexp))
  4744. (org-export-with-sub-superscripts
  4745. (list org-match-substring-regexp))
  4746. (t nil)))
  4747. (re-latex
  4748. (if org-export-with-LaTeX-fragments
  4749. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4750. (re-macros
  4751. (if org-export-with-TeX-macros
  4752. (list (concat "\\\\"
  4753. (regexp-opt
  4754. (append
  4755. (delq nil
  4756. (mapcar 'car-safe
  4757. (append org-entities-user
  4758. org-entities)))
  4759. (if (boundp 'org-latex-entities)
  4760. (mapcar (lambda (x)
  4761. (or (car-safe x) x))
  4762. org-latex-entities)
  4763. nil))
  4764. 'words))) ; FIXME
  4765. ))
  4766. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4767. (re-special (if org-export-with-special-strings
  4768. (mapcar (lambda (x) (car x))
  4769. org-export-html-special-string-regexps)))
  4770. (re-rest
  4771. (delq nil
  4772. (list
  4773. (if org-export-html-expand "@<[^>\n]+>")
  4774. ))))
  4775. (org-set-local
  4776. 'org-latex-and-specials-regexp
  4777. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4778. re-rest) "\\|")))))
  4779. (defun org-do-latex-and-special-faces (limit)
  4780. "Run through the buffer and add overlays to links."
  4781. (when org-latex-and-specials-regexp
  4782. (let (rtn d)
  4783. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4784. limit t))
  4785. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4786. 'face))
  4787. '(org-code org-verbatim underline)))
  4788. (progn
  4789. (setq rtn t
  4790. d (cond ((member (char-after (1+ (match-beginning 0)))
  4791. '(?_ ?^)) 1)
  4792. (t 0)))
  4793. (font-lock-prepend-text-property
  4794. (+ d (match-beginning 0)) (match-end 0)
  4795. 'face 'org-latex-and-export-specials)
  4796. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4797. '(font-lock-multiline t)))))
  4798. rtn)))
  4799. (defun org-restart-font-lock ()
  4800. "Restart `font-lock-mode', to force refontification."
  4801. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4802. (font-lock-mode -1)
  4803. (font-lock-mode 1)))
  4804. (defun org-all-targets (&optional radio)
  4805. "Return a list of all targets in this file.
  4806. With optional argument RADIO, only find radio targets."
  4807. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4808. rtn)
  4809. (save-excursion
  4810. (goto-char (point-min))
  4811. (while (re-search-forward re nil t)
  4812. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4813. rtn)))
  4814. (defun org-make-target-link-regexp (targets)
  4815. "Make regular expression matching all strings in TARGETS.
  4816. The regular expression finds the targets also if there is a line break
  4817. between words."
  4818. (and targets
  4819. (concat
  4820. "\\<\\("
  4821. (mapconcat
  4822. (lambda (x)
  4823. (while (string-match " +" x)
  4824. (setq x (replace-match "\\s-+" t t x)))
  4825. x)
  4826. targets
  4827. "\\|")
  4828. "\\)\\>")))
  4829. (defun org-activate-tags (limit)
  4830. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4831. (progn
  4832. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4833. (add-text-properties (match-beginning 1) (match-end 1)
  4834. (list 'mouse-face 'highlight
  4835. 'keymap org-mouse-map))
  4836. (org-rear-nonsticky-at (match-end 1))
  4837. t)))
  4838. (defun org-outline-level ()
  4839. "Compute the outline level of the heading at point.
  4840. This function assumes that the cursor is at the beginning of a line matched
  4841. by `outline-regexp'. Otherwise it returns garbage.
  4842. If this is called at a normal headline, the level is the number of stars.
  4843. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4844. For plain list items, if they are matched by `outline-regexp', this returns
  4845. 1000 plus the line indentation."
  4846. (save-excursion
  4847. (looking-at outline-regexp)
  4848. (if (match-beginning 1)
  4849. (+ (org-get-string-indentation (match-string 1)) 1000)
  4850. (1- (- (match-end 0) (match-beginning 0))))))
  4851. (defvar org-font-lock-keywords nil)
  4852. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4853. "Regular expression matching a property line.")
  4854. (defvar org-font-lock-hook nil
  4855. "Functions to be called for special font lock stuff.")
  4856. (defvar org-font-lock-set-keywords-hook nil
  4857. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4858. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4859. it is installed to be used by font lock. This can be useful if something
  4860. needs to be inserted at a specific position in the font-lock sequence.")
  4861. (defun org-font-lock-hook (limit)
  4862. (run-hook-with-args 'org-font-lock-hook limit))
  4863. (defun org-set-font-lock-defaults ()
  4864. (let* ((em org-fontify-emphasized-text)
  4865. (lk org-activate-links)
  4866. (org-font-lock-extra-keywords
  4867. (list
  4868. ;; Call the hook
  4869. '(org-font-lock-hook)
  4870. ;; Headlines
  4871. `(,(if org-fontify-whole-heading-line
  4872. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4873. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4874. (1 (org-get-level-face 1))
  4875. (2 (org-get-level-face 2))
  4876. (3 (org-get-level-face 3)))
  4877. ;; Table lines
  4878. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4879. (1 'org-table t))
  4880. ;; Table internals
  4881. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4882. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4883. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4884. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  4885. ;; Drawers
  4886. (list org-drawer-regexp '(0 'org-special-keyword t))
  4887. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4888. ;; Properties
  4889. (list org-property-re
  4890. '(1 'org-special-keyword t)
  4891. '(3 'org-property-value t))
  4892. ;; Links
  4893. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4894. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4895. (if (memq 'plain lk) '(org-activate-plain-links))
  4896. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4897. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4898. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4899. (if (memq 'footnote lk) '(org-activate-footnote-links
  4900. (2 'org-footnote t)))
  4901. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4902. '(org-hide-wide-columns (0 nil append))
  4903. ;; TODO lines
  4904. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4905. '(1 (org-get-todo-face 1) t))
  4906. ;; DONE
  4907. (if org-fontify-done-headline
  4908. (list (concat "^[*]+ +\\<\\("
  4909. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4910. "\\)\\(.*\\)")
  4911. '(2 'org-headline-done t))
  4912. nil)
  4913. ;; Priorities
  4914. '(org-font-lock-add-priority-faces)
  4915. ;; Tags
  4916. '(org-font-lock-add-tag-faces)
  4917. ;; Special keywords
  4918. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4919. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4920. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4921. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4922. ;; Emphasis
  4923. (if em
  4924. (if (featurep 'xemacs)
  4925. '(org-do-emphasis-faces (0 nil append))
  4926. '(org-do-emphasis-faces)))
  4927. ;; Checkboxes
  4928. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4929. 1 'org-checkbox prepend)
  4930. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4931. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4932. (0 (org-get-checkbox-statistics-face) t)))
  4933. ;; Description list items
  4934. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(.*? ::\\)"
  4935. 2 'bold prepend)
  4936. ;; ARCHIVEd headings
  4937. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4938. '(1 'org-archived prepend))
  4939. ;; Specials
  4940. '(org-do-latex-and-special-faces)
  4941. '(org-fontify-entities)
  4942. '(org-raise-scripts)
  4943. ;; Code
  4944. '(org-activate-code (1 'org-code t))
  4945. ;; COMMENT
  4946. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4947. "\\|" org-quote-string "\\)\\>")
  4948. '(1 'org-special-keyword t))
  4949. '("^#.*" (0 'font-lock-comment-face t))
  4950. ;; Blocks and meta lines
  4951. '(org-fontify-meta-lines-and-blocks)
  4952. )))
  4953. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4954. (run-hooks 'org-font-lock-set-keywords-hook)
  4955. ;; Now set the full font-lock-keywords
  4956. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4957. (org-set-local 'font-lock-defaults
  4958. '(org-font-lock-keywords t nil nil backward-paragraph))
  4959. (kill-local-variable 'font-lock-keywords) nil))
  4960. (defun org-toggle-pretty-entities ()
  4961. "Toggle the composition display of entities as UTF8 characters."
  4962. (interactive)
  4963. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4964. (org-restart-font-lock)
  4965. (if org-pretty-entities
  4966. (message "Entities are displayed as UTF8 characers")
  4967. (save-restriction
  4968. (widen)
  4969. (org-decompose-region (point-min) (point-max))
  4970. (message "Entities are displayed plain"))))
  4971. (defun org-fontify-entities (limit)
  4972. "Find an entity to fontify."
  4973. (let (ee)
  4974. (when org-pretty-entities
  4975. (catch 'match
  4976. (while (re-search-forward
  4977. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  4978. limit t)
  4979. (if (and (not (org-in-indented-comment-line))
  4980. (setq ee (org-entity-get (match-string 1)))
  4981. (= (length (nth 6 ee)) 1))
  4982. (progn
  4983. (add-text-properties
  4984. (match-beginning 0) (match-end 1)
  4985. (list 'font-lock-fontified t))
  4986. (compose-region (match-beginning 0) (match-end 1)
  4987. (nth 6 ee) nil)
  4988. (backward-char 1)
  4989. (throw 'match t))))
  4990. nil))))
  4991. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4992. "Fontify string S like in Org-mode."
  4993. (with-temp-buffer
  4994. (insert s)
  4995. (let ((org-odd-levels-only odd-levels))
  4996. (org-mode)
  4997. (font-lock-fontify-buffer)
  4998. (buffer-string))))
  4999. (defvar org-m nil)
  5000. (defvar org-l nil)
  5001. (defvar org-f nil)
  5002. (defun org-get-level-face (n)
  5003. "Get the right face for match N in font-lock matching of headlines."
  5004. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5005. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5006. (if org-cycle-level-faces
  5007. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5008. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5009. (cond
  5010. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5011. ((eq n 2) org-f)
  5012. (t (if org-level-color-stars-only nil org-f))))
  5013. (defun org-get-todo-face (kwd)
  5014. "Get the right face for a TODO keyword KWD.
  5015. If KWD is a number, get the corresponding match group."
  5016. (if (numberp kwd) (setq kwd (match-string kwd)))
  5017. (or (org-face-from-face-or-color
  5018. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5019. (and (member kwd org-done-keywords) 'org-done)
  5020. 'org-todo))
  5021. (defun org-face-from-face-or-color (context inherit face-or-color)
  5022. "Create a face list that inherits INHERIT, but sets the foreground color.
  5023. When FACE-OR-COLOR is not a string, just return it."
  5024. (if (stringp face-or-color)
  5025. (list :inherit inherit
  5026. (cdr (assoc context org-faces-easy-properties))
  5027. face-or-color)
  5028. face-or-color))
  5029. (defun org-font-lock-add-tag-faces (limit)
  5030. "Add the special tag faces."
  5031. (when (and org-tag-faces org-tags-special-faces-re)
  5032. (while (re-search-forward org-tags-special-faces-re limit t)
  5033. (add-text-properties (match-beginning 1) (match-end 1)
  5034. (list 'face (org-get-tag-face 1)
  5035. 'font-lock-fontified t))
  5036. (backward-char 1))))
  5037. (defun org-font-lock-add-priority-faces (limit)
  5038. "Add the special priority faces."
  5039. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5040. (add-text-properties
  5041. (match-beginning 0) (match-end 0)
  5042. (list 'face (or (org-face-from-face-or-color
  5043. 'priority 'org-special-keyword
  5044. (cdr (assoc (char-after (match-beginning 1))
  5045. org-priority-faces)))
  5046. 'org-special-keyword)
  5047. 'font-lock-fontified t))))
  5048. (defun org-get-tag-face (kwd)
  5049. "Get the right face for a TODO keyword KWD.
  5050. If KWD is a number, get the corresponding match group."
  5051. (if (numberp kwd) (setq kwd (match-string kwd)))
  5052. (or (org-face-from-face-or-color
  5053. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5054. 'org-tag))
  5055. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5056. "Remove fontification and activation overlays from links."
  5057. (font-lock-default-unfontify-region beg end)
  5058. (let* ((buffer-undo-list t)
  5059. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5060. (inhibit-modification-hooks t)
  5061. deactivate-mark buffer-file-name buffer-file-truename)
  5062. (org-decompose-region beg end)
  5063. (remove-text-properties
  5064. beg end
  5065. (if org-indent-mode
  5066. ;; also remove line-prefix and wrap-prefix properties
  5067. '(mouse-face t keymap t org-linked-text t
  5068. invisible t intangible t
  5069. line-prefix t wrap-prefix t
  5070. org-no-flyspell t org-emphasis t)
  5071. '(mouse-face t keymap t org-linked-text t
  5072. invisible t intangible t
  5073. org-no-flyspell t org-emphasis t)))
  5074. (org-remove-font-lock-display-properties beg end)))
  5075. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5076. ((raise 0.3) (height 0.7))
  5077. ((raise -0.5))
  5078. ((raise 0.5)))
  5079. "Display properties for showing superscripts and subscripts.")
  5080. (defun org-remove-font-lock-display-properties (beg end)
  5081. "Remove specific display properties that have been added by font lock.
  5082. The will remove the raise properties that are used to show superscripts
  5083. and subscripts."
  5084. (let (next prop)
  5085. (while (< beg end)
  5086. (setq next (next-single-property-change beg 'display nil end)
  5087. prop (get-text-property beg 'display))
  5088. (if (member prop org-script-display)
  5089. (put-text-property beg next 'display nil))
  5090. (setq beg next))))
  5091. (defun org-raise-scripts (limit)
  5092. "Add raise properties to sub/superscripts."
  5093. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5094. (if (re-search-forward
  5095. (if (eq org-use-sub-superscripts t)
  5096. org-match-substring-regexp
  5097. org-match-substring-with-braces-regexp)
  5098. limit t)
  5099. (let* ((pos (point)) table-p comment-p
  5100. (mpos (match-beginning 3))
  5101. (emph-p (get-text-property mpos 'org-emphasis))
  5102. (link-p (get-text-property mpos 'mouse-face))
  5103. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5104. (goto-char (point-at-bol))
  5105. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5106. comment-p (org-looking-at-p "[ \t]*#"))
  5107. (goto-char pos)
  5108. ;; FIXME: Should we go back one character here, for a_b^c
  5109. ;; (goto-char (1- pos)) ;????????????????????
  5110. (if (or comment-p emph-p link-p keyw-p)
  5111. t
  5112. (put-text-property (match-beginning 3) (match-end 0)
  5113. 'display
  5114. (if (equal (char-after (match-beginning 2)) ?^)
  5115. (nth (if table-p 3 1) org-script-display)
  5116. (nth (if table-p 2 0) org-script-display)))
  5117. (add-text-properties (match-beginning 2) (match-end 2)
  5118. (list 'invisible t
  5119. 'org-dwidth t 'org-dwidth-n 1))
  5120. (if (and (eq (char-after (match-beginning 3)) ?{)
  5121. (eq (char-before (match-end 3)) ?}))
  5122. (progn
  5123. (add-text-properties
  5124. (match-beginning 3) (1+ (match-beginning 3))
  5125. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5126. (add-text-properties
  5127. (1- (match-end 3)) (match-end 3)
  5128. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5129. t)))))
  5130. ;;;; Visibility cycling, including org-goto and indirect buffer
  5131. ;;; Cycling
  5132. (defvar org-cycle-global-status nil)
  5133. (make-variable-buffer-local 'org-cycle-global-status)
  5134. (defvar org-cycle-subtree-status nil)
  5135. (make-variable-buffer-local 'org-cycle-subtree-status)
  5136. ;;;###autoload
  5137. (defvar org-inlinetask-min-level)
  5138. (defun org-cycle (&optional arg)
  5139. "TAB-action and visibility cycling for Org-mode.
  5140. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5141. is outline visibility cycling, but it also invokes other actions
  5142. in special contexts.
  5143. - When this function is called with a prefix argument, rotate the entire
  5144. buffer through 3 states (global cycling)
  5145. 1. OVERVIEW: Show only top-level headlines.
  5146. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5147. 3. SHOW ALL: Show everything.
  5148. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5149. determined by the variable `org-startup-folded', and by any VISIBILITY
  5150. properties in the buffer.
  5151. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5152. including any drawers.
  5153. - When inside a table, re-align the table and move to the next field.
  5154. - When point is at the beginning of a headline, rotate the subtree started
  5155. by this line through 3 different states (local cycling)
  5156. 1. FOLDED: Only the main headline is shown.
  5157. 2. CHILDREN: The main headline and the direct children are shown.
  5158. From this state, you can move to one of the children
  5159. and zoom in further.
  5160. 3. SUBTREE: Show the entire subtree, including body text.
  5161. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5162. - When point is at the beginning of an empty headline and the variable
  5163. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5164. of the headline by demoting and promoting it to likely levels. This
  5165. speeds up creation document structure by pressing TAB once or several
  5166. times right after creating a new headline.
  5167. - When there is a numeric prefix, go up to a heading with level ARG, do
  5168. a `show-subtree' and return to the previous cursor position. If ARG
  5169. is negative, go up that many levels.
  5170. - When point is not at the beginning of a headline, execute the global
  5171. binding for TAB, which is re-indenting the line. See the option
  5172. `org-cycle-emulate-tab' for details.
  5173. - Special case: if point is at the beginning of the buffer and there is
  5174. no headline in line 1, this function will act as if called with prefix arg
  5175. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5176. But only if also the variable `org-cycle-global-at-bob' is t."
  5177. (interactive "P")
  5178. (org-load-modules-maybe)
  5179. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5180. (and org-cycle-level-after-item/entry-creation
  5181. (or (org-cycle-level)
  5182. (org-cycle-item-indentation))))
  5183. (let* ((limit-level
  5184. (or org-cycle-max-level
  5185. (and (boundp 'org-inlinetask-min-level)
  5186. org-inlinetask-min-level
  5187. (1- org-inlinetask-min-level))))
  5188. (nstars (and limit-level
  5189. (if org-odd-levels-only
  5190. (and limit-level (1- (* limit-level 2)))
  5191. limit-level)))
  5192. (outline-regexp
  5193. (cond
  5194. ((not (org-mode-p)) outline-regexp)
  5195. ((or (eq org-cycle-include-plain-lists 'integrate)
  5196. (and org-cycle-include-plain-lists (org-at-item-p)))
  5197. (concat "\\(?:\\*"
  5198. (if nstars (format "\\{1,%d\\}" nstars) "+")
  5199. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  5200. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  5201. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5202. (not (looking-at outline-regexp))))
  5203. (org-cycle-hook
  5204. (if bob-special
  5205. (delq 'org-optimize-window-after-visibility-change
  5206. (copy-sequence org-cycle-hook))
  5207. org-cycle-hook))
  5208. (pos (point)))
  5209. (if (or bob-special (equal arg '(4)))
  5210. ;; special case: use global cycling
  5211. (setq arg t))
  5212. (cond
  5213. ((equal arg '(16))
  5214. (setq last-command 'dummy)
  5215. (org-set-startup-visibility)
  5216. (message "Startup visibility, plus VISIBILITY properties"))
  5217. ((equal arg '(64))
  5218. (show-all)
  5219. (message "Entire buffer visible, including drawers"))
  5220. ((org-at-table-p 'any)
  5221. ;; Enter the table or move to the next field in the table
  5222. (if (org-at-table.el-p)
  5223. (message "Use C-c ' to edit table.el tables")
  5224. (if arg (org-table-edit-field t)
  5225. (org-table-justify-field-maybe)
  5226. (call-interactively 'org-table-next-field))))
  5227. ((run-hook-with-args-until-success
  5228. 'org-tab-after-check-for-table-hook))
  5229. ((eq arg t) ;; Global cycling
  5230. (org-cycle-internal-global))
  5231. ((and org-drawers org-drawer-regexp
  5232. (save-excursion
  5233. (beginning-of-line 1)
  5234. (looking-at org-drawer-regexp)))
  5235. ;; Toggle block visibility
  5236. (org-flag-drawer
  5237. (not (get-char-property (match-end 0) 'invisible))))
  5238. ((integerp arg)
  5239. ;; Show-subtree, ARG levels up from here.
  5240. (save-excursion
  5241. (org-back-to-heading)
  5242. (outline-up-heading (if (< arg 0) (- arg)
  5243. (- (funcall outline-level) arg)))
  5244. (org-show-subtree)))
  5245. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5246. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5247. (org-cycle-internal-local))
  5248. ;; TAB emulation and template completion
  5249. (buffer-read-only (org-back-to-heading))
  5250. ((run-hook-with-args-until-success
  5251. 'org-tab-after-check-for-cycling-hook))
  5252. ((org-try-structure-completion))
  5253. ((org-try-cdlatex-tab))
  5254. ((run-hook-with-args-until-success
  5255. 'org-tab-before-tab-emulation-hook))
  5256. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5257. (or (not (bolp))
  5258. (not (looking-at outline-regexp))))
  5259. (call-interactively (global-key-binding "\t")))
  5260. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5261. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5262. (or (and (eq org-cycle-emulate-tab 'white)
  5263. (= (match-end 0) (point-at-eol)))
  5264. (and (eq org-cycle-emulate-tab 'whitestart)
  5265. (>= (match-end 0) pos))))
  5266. t
  5267. (eq org-cycle-emulate-tab t))
  5268. (call-interactively (global-key-binding "\t")))
  5269. (t (save-excursion
  5270. (org-back-to-heading)
  5271. (org-cycle)))))))
  5272. (defun org-cycle-internal-global ()
  5273. "Do the global cycling action."
  5274. (cond
  5275. ((and (eq last-command this-command)
  5276. (eq org-cycle-global-status 'overview))
  5277. ;; We just created the overview - now do table of contents
  5278. ;; This can be slow in very large buffers, so indicate action
  5279. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5280. (message "CONTENTS...")
  5281. (org-content)
  5282. (message "CONTENTS...done")
  5283. (setq org-cycle-global-status 'contents)
  5284. (run-hook-with-args 'org-cycle-hook 'contents))
  5285. ((and (eq last-command this-command)
  5286. (eq org-cycle-global-status 'contents))
  5287. ;; We just showed the table of contents - now show everything
  5288. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5289. (show-all)
  5290. (message "SHOW ALL")
  5291. (setq org-cycle-global-status 'all)
  5292. (run-hook-with-args 'org-cycle-hook 'all))
  5293. (t
  5294. ;; Default action: go to overview
  5295. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5296. (org-overview)
  5297. (message "OVERVIEW")
  5298. (setq org-cycle-global-status 'overview)
  5299. (run-hook-with-args 'org-cycle-hook 'overview))))
  5300. (defun org-cycle-internal-local ()
  5301. "Do the local cycling action."
  5302. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  5303. ;; First, some boundaries
  5304. (save-excursion
  5305. (org-back-to-heading)
  5306. (setq level (funcall outline-level))
  5307. (save-excursion
  5308. (beginning-of-line 2)
  5309. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  5310. ; XEmacs does not have `next-single-char-property-change'
  5311. ; I'm not sure about Emacs 21.
  5312. (while (and (not (eobp)) ;; this is like `next-line'
  5313. (get-char-property (1- (point)) 'invisible))
  5314. (beginning-of-line 2))
  5315. (while (and (not (eobp)) ;; this is like `next-line'
  5316. (get-char-property (1- (point)) 'invisible))
  5317. (goto-char (next-single-char-property-change (point) 'invisible))
  5318. (and (eolp) (beginning-of-line 2))))
  5319. (setq eol (point)))
  5320. (outline-end-of-heading) (setq eoh (point))
  5321. (save-excursion
  5322. (outline-next-heading)
  5323. (setq has-children (and (org-at-heading-p t)
  5324. (> (funcall outline-level) level))))
  5325. ;; if we're in a list, org-end-of-subtree is in fact org-end-of-item.
  5326. (if (org-at-item-p)
  5327. (setq eos (if (and (org-end-of-item) (bolp))
  5328. (1- (point))
  5329. (point)))
  5330. (org-end-of-subtree t)
  5331. (unless (eobp)
  5332. (skip-chars-forward " \t\n"))
  5333. (setq eos (if (eobp) (point) (1- (point))))))
  5334. ;; Find out what to do next and set `this-command'
  5335. (cond
  5336. ((= eos eoh)
  5337. ;; Nothing is hidden behind this heading
  5338. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5339. (message "EMPTY ENTRY")
  5340. (setq org-cycle-subtree-status nil)
  5341. (save-excursion
  5342. (goto-char eos)
  5343. (outline-next-heading)
  5344. (if (org-invisible-p) (org-flag-heading nil))))
  5345. ((and (or (>= eol eos)
  5346. (not (string-match "\\S-" (buffer-substring eol eos))))
  5347. (or has-children
  5348. (not (setq children-skipped
  5349. org-cycle-skip-children-state-if-no-children))))
  5350. ;; Entire subtree is hidden in one line: children view
  5351. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5352. (org-show-entry)
  5353. (show-children)
  5354. (message "CHILDREN")
  5355. (save-excursion
  5356. (goto-char eos)
  5357. (outline-next-heading)
  5358. (if (org-invisible-p) (org-flag-heading nil)))
  5359. (setq org-cycle-subtree-status 'children)
  5360. (run-hook-with-args 'org-cycle-hook 'children))
  5361. ((or children-skipped
  5362. (and (eq last-command this-command)
  5363. (eq org-cycle-subtree-status 'children)))
  5364. ;; We just showed the children, or no children are there,
  5365. ;; now show everything.
  5366. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5367. (outline-flag-region eoh eos nil)
  5368. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5369. (setq org-cycle-subtree-status 'subtree)
  5370. (run-hook-with-args 'org-cycle-hook 'subtree))
  5371. (t
  5372. ;; Default action: hide the subtree.
  5373. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5374. (outline-flag-region eoh eos t)
  5375. (message "FOLDED")
  5376. (setq org-cycle-subtree-status 'folded)
  5377. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5378. ;;;###autoload
  5379. (defun org-global-cycle (&optional arg)
  5380. "Cycle the global visibility. For details see `org-cycle'.
  5381. With \\[universal-argument] prefix arg, switch to startup visibility.
  5382. With a numeric prefix, show all headlines up to that level."
  5383. (interactive "P")
  5384. (let ((org-cycle-include-plain-lists
  5385. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5386. (cond
  5387. ((integerp arg)
  5388. (show-all)
  5389. (hide-sublevels arg)
  5390. (setq org-cycle-global-status 'contents))
  5391. ((equal arg '(4))
  5392. (org-set-startup-visibility)
  5393. (message "Startup visibility, plus VISIBILITY properties."))
  5394. (t
  5395. (org-cycle '(4))))))
  5396. (defun org-set-startup-visibility ()
  5397. "Set the visibility required by startup options and properties."
  5398. (cond
  5399. ((eq org-startup-folded t)
  5400. (org-cycle '(4)))
  5401. ((eq org-startup-folded 'content)
  5402. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5403. (org-cycle '(4)) (org-cycle '(4)))))
  5404. (unless (eq org-startup-folded 'showeverything)
  5405. (if org-hide-block-startup (org-hide-block-all))
  5406. (org-set-visibility-according-to-property 'no-cleanup)
  5407. (org-cycle-hide-archived-subtrees 'all)
  5408. (org-cycle-hide-drawers 'all)
  5409. (org-cycle-show-empty-lines t)))
  5410. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5411. "Switch subtree visibilities according to :VISIBILITY: property."
  5412. (interactive)
  5413. (let (org-show-entry-below state)
  5414. (save-excursion
  5415. (goto-char (point-min))
  5416. (while (re-search-forward
  5417. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5418. nil t)
  5419. (setq state (match-string 1))
  5420. (save-excursion
  5421. (org-back-to-heading t)
  5422. (hide-subtree)
  5423. (org-reveal)
  5424. (cond
  5425. ((equal state '("fold" "folded"))
  5426. (hide-subtree))
  5427. ((equal state "children")
  5428. (org-show-hidden-entry)
  5429. (show-children))
  5430. ((equal state "content")
  5431. (save-excursion
  5432. (save-restriction
  5433. (org-narrow-to-subtree)
  5434. (org-content))))
  5435. ((member state '("all" "showall"))
  5436. (show-subtree)))))
  5437. (unless no-cleanup
  5438. (org-cycle-hide-archived-subtrees 'all)
  5439. (org-cycle-hide-drawers 'all)
  5440. (org-cycle-show-empty-lines 'all)))))
  5441. (defun org-overview ()
  5442. "Switch to overview mode, showing only top-level headlines.
  5443. Really, this shows all headlines with level equal or greater than the level
  5444. of the first headline in the buffer. This is important, because if the
  5445. first headline is not level one, then (hide-sublevels 1) gives confusing
  5446. results."
  5447. (interactive)
  5448. (let ((level (save-excursion
  5449. (goto-char (point-min))
  5450. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5451. (progn
  5452. (goto-char (match-beginning 0))
  5453. (funcall outline-level))))))
  5454. (and level (hide-sublevels level))))
  5455. (defun org-content (&optional arg)
  5456. "Show all headlines in the buffer, like a table of contents.
  5457. With numerical argument N, show content up to level N."
  5458. (interactive "P")
  5459. (save-excursion
  5460. ;; Visit all headings and show their offspring
  5461. (and (integerp arg) (org-overview))
  5462. (goto-char (point-max))
  5463. (catch 'exit
  5464. (while (and (progn (condition-case nil
  5465. (outline-previous-visible-heading 1)
  5466. (error (goto-char (point-min))))
  5467. t)
  5468. (looking-at outline-regexp))
  5469. (if (integerp arg)
  5470. (show-children (1- arg))
  5471. (show-branches))
  5472. (if (bobp) (throw 'exit nil))))))
  5473. (defun org-optimize-window-after-visibility-change (state)
  5474. "Adjust the window after a change in outline visibility.
  5475. This function is the default value of the hook `org-cycle-hook'."
  5476. (when (get-buffer-window (current-buffer))
  5477. (cond
  5478. ((eq state 'content) nil)
  5479. ((eq state 'all) nil)
  5480. ((eq state 'folded) nil)
  5481. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5482. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5483. (defun org-remove-empty-overlays-at (pos)
  5484. "Remove outline overlays that do not contain non-white stuff."
  5485. (mapc
  5486. (lambda (o)
  5487. (and (eq 'outline (overlay-get o 'invisible))
  5488. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5489. (overlay-end o))))
  5490. (delete-overlay o)))
  5491. (overlays-at pos)))
  5492. (defun org-clean-visibility-after-subtree-move ()
  5493. "Fix visibility issues after moving a subtree."
  5494. ;; First, find a reasonable region to look at:
  5495. ;; Start two siblings above, end three below
  5496. (let* ((beg (save-excursion
  5497. (and (org-get-last-sibling)
  5498. (org-get-last-sibling))
  5499. (point)))
  5500. (end (save-excursion
  5501. (and (org-get-next-sibling)
  5502. (org-get-next-sibling)
  5503. (org-get-next-sibling))
  5504. (if (org-at-heading-p)
  5505. (point-at-eol)
  5506. (point))))
  5507. (level (looking-at "\\*+"))
  5508. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5509. (save-excursion
  5510. (save-restriction
  5511. (narrow-to-region beg end)
  5512. (when re
  5513. ;; Properly fold already folded siblings
  5514. (goto-char (point-min))
  5515. (while (re-search-forward re nil t)
  5516. (if (and (not (org-invisible-p))
  5517. (save-excursion
  5518. (goto-char (point-at-eol)) (org-invisible-p)))
  5519. (hide-entry))))
  5520. (org-cycle-show-empty-lines 'overview)
  5521. (org-cycle-hide-drawers 'overview)))))
  5522. (defun org-cycle-show-empty-lines (state)
  5523. "Show empty lines above all visible headlines.
  5524. The region to be covered depends on STATE when called through
  5525. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5526. entire buffer covered. Note that an empty line is only shown if there
  5527. are at least `org-cycle-separator-lines' empty lines before the headline."
  5528. (when (not (= org-cycle-separator-lines 0))
  5529. (save-excursion
  5530. (let* ((n (abs org-cycle-separator-lines))
  5531. (re (cond
  5532. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5533. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5534. (t (let ((ns (number-to-string (- n 2))))
  5535. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5536. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5537. beg end b e)
  5538. (cond
  5539. ((memq state '(overview contents t))
  5540. (setq beg (point-min) end (point-max)))
  5541. ((memq state '(children folded))
  5542. (setq beg (point) end (progn (org-end-of-subtree t t)
  5543. (beginning-of-line 2)
  5544. (point)))))
  5545. (when beg
  5546. (goto-char beg)
  5547. (while (re-search-forward re end t)
  5548. (unless (get-char-property (match-end 1) 'invisible)
  5549. (setq e (match-end 1))
  5550. (if (< org-cycle-separator-lines 0)
  5551. (setq b (save-excursion
  5552. (goto-char (match-beginning 0))
  5553. (org-back-over-empty-lines)
  5554. (if (save-excursion
  5555. (goto-char (max (point-min) (1- (point))))
  5556. (org-on-heading-p))
  5557. (1- (point))
  5558. (point))))
  5559. (setq b (match-beginning 1)))
  5560. (outline-flag-region b e nil)))))))
  5561. ;; Never hide empty lines at the end of the file.
  5562. (save-excursion
  5563. (goto-char (point-max))
  5564. (outline-previous-heading)
  5565. (outline-end-of-heading)
  5566. (if (and (looking-at "[ \t\n]+")
  5567. (= (match-end 0) (point-max)))
  5568. (outline-flag-region (point) (match-end 0) nil))))
  5569. (defun org-show-empty-lines-in-parent ()
  5570. "Move to the parent and re-show empty lines before visible headlines."
  5571. (save-excursion
  5572. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5573. (org-cycle-show-empty-lines context))))
  5574. (defun org-files-list ()
  5575. "Return `org-agenda-files' list, plus all open org-mode files.
  5576. This is useful for operations that need to scan all of a user's
  5577. open and agenda-wise Org files."
  5578. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5579. (dolist (buf (buffer-list))
  5580. (with-current-buffer buf
  5581. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5582. (let ((file (expand-file-name (buffer-file-name))))
  5583. (unless (member file files)
  5584. (push file files))))))
  5585. files))
  5586. (defsubst org-entry-beginning-position ()
  5587. "Return the beginning position of the current entry."
  5588. (save-excursion (outline-back-to-heading t) (point)))
  5589. (defsubst org-entry-end-position ()
  5590. "Return the end position of the current entry."
  5591. (save-excursion (outline-next-heading) (point)))
  5592. (defun org-cycle-hide-drawers (state)
  5593. "Re-hide all drawers after a visibility state change."
  5594. (when (and (org-mode-p)
  5595. (not (memq state '(overview folded contents))))
  5596. (save-excursion
  5597. (let* ((globalp (memq state '(contents all)))
  5598. (beg (if globalp (point-min) (point)))
  5599. (end (if globalp (point-max)
  5600. (if (eq state 'children)
  5601. (save-excursion (outline-next-heading) (point))
  5602. (org-end-of-subtree t)))))
  5603. (goto-char beg)
  5604. (while (re-search-forward org-drawer-regexp end t)
  5605. (org-flag-drawer t))))))
  5606. (defun org-flag-drawer (flag)
  5607. (save-excursion
  5608. (beginning-of-line 1)
  5609. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5610. (let ((b (match-end 0))
  5611. (outline-regexp org-outline-regexp))
  5612. (if (re-search-forward
  5613. "^[ \t]*:END:"
  5614. (save-excursion (outline-next-heading) (point)) t)
  5615. (outline-flag-region b (point-at-eol) flag)
  5616. (error ":END: line missing at position %s" b))))))
  5617. (defun org-subtree-end-visible-p ()
  5618. "Is the end of the current subtree visible?"
  5619. (pos-visible-in-window-p
  5620. (save-excursion (org-end-of-subtree t) (point))))
  5621. (defun org-first-headline-recenter (&optional N)
  5622. "Move cursor to the first headline and recenter the headline.
  5623. Optional argument N means put the headline into the Nth line of the window."
  5624. (goto-char (point-min))
  5625. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5626. (beginning-of-line)
  5627. (recenter (prefix-numeric-value N))))
  5628. ;;; Saving and restoring visibility
  5629. (defun org-outline-overlay-data (&optional use-markers)
  5630. "Return a list of the locations of all outline overlays.
  5631. These are overlays with the `invisible' property value `outline'.
  5632. The return value is a list of cons cells, with start and stop
  5633. positions for each overlay.
  5634. If USE-MARKERS is set, return the positions as markers."
  5635. (let (beg end)
  5636. (save-excursion
  5637. (save-restriction
  5638. (widen)
  5639. (delq nil
  5640. (mapcar (lambda (o)
  5641. (when (eq (overlay-get o 'invisible) 'outline)
  5642. (setq beg (overlay-start o)
  5643. end (overlay-end o))
  5644. (and beg end (> end beg)
  5645. (if use-markers
  5646. (cons (move-marker (make-marker) beg)
  5647. (move-marker (make-marker) end))
  5648. (cons beg end)))))
  5649. (overlays-in (point-min) (point-max))))))))
  5650. (defun org-set-outline-overlay-data (data)
  5651. "Create visibility overlays for all positions in DATA.
  5652. DATA should have been made by `org-outline-overlay-data'."
  5653. (let (o)
  5654. (save-excursion
  5655. (save-restriction
  5656. (widen)
  5657. (show-all)
  5658. (mapc (lambda (c)
  5659. (setq o (make-overlay (car c) (cdr c)))
  5660. (overlay-put o 'invisible 'outline))
  5661. data)))))
  5662. ;;; Folding of blocks
  5663. (defconst org-block-regexp
  5664. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5665. "Regular expression for hiding blocks.")
  5666. (defvar org-hide-block-overlays nil
  5667. "Overlays hiding blocks.")
  5668. (make-variable-buffer-local 'org-hide-block-overlays)
  5669. (defun org-block-map (function &optional start end)
  5670. "Call FUNCTION at the head of all source blocks in the current buffer.
  5671. Optional arguments START and END can be used to limit the range."
  5672. (let ((start (or start (point-min)))
  5673. (end (or end (point-max))))
  5674. (save-excursion
  5675. (goto-char start)
  5676. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5677. (save-excursion
  5678. (save-match-data
  5679. (goto-char (match-beginning 0))
  5680. (funcall function)))))))
  5681. (defun org-hide-block-toggle-all ()
  5682. "Toggle the visibility of all blocks in the current buffer."
  5683. (org-block-map #'org-hide-block-toggle))
  5684. (defun org-hide-block-all ()
  5685. "Fold all blocks in the current buffer."
  5686. (interactive)
  5687. (org-show-block-all)
  5688. (org-block-map #'org-hide-block-toggle-maybe))
  5689. (defun org-show-block-all ()
  5690. "Unfold all blocks in the current buffer."
  5691. (interactive)
  5692. (mapc 'delete-overlay org-hide-block-overlays)
  5693. (setq org-hide-block-overlays nil))
  5694. (defun org-hide-block-toggle-maybe ()
  5695. "Toggle visibility of block at point."
  5696. (interactive)
  5697. (let ((case-fold-search t))
  5698. (if (save-excursion
  5699. (beginning-of-line 1)
  5700. (looking-at org-block-regexp))
  5701. (progn (org-hide-block-toggle)
  5702. t) ;; to signal that we took action
  5703. nil))) ;; to signal that we did not
  5704. (defun org-hide-block-toggle (&optional force)
  5705. "Toggle the visibility of the current block."
  5706. (interactive)
  5707. (save-excursion
  5708. (beginning-of-line)
  5709. (if (re-search-forward org-block-regexp nil t)
  5710. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5711. (end (match-end 0)) ;; end of entire body
  5712. ov)
  5713. (if (memq t (mapcar (lambda (overlay)
  5714. (eq (overlay-get overlay 'invisible)
  5715. 'org-hide-block))
  5716. (overlays-at start)))
  5717. (if (or (not force) (eq force 'off))
  5718. (mapc (lambda (ov)
  5719. (when (member ov org-hide-block-overlays)
  5720. (setq org-hide-block-overlays
  5721. (delq ov org-hide-block-overlays)))
  5722. (when (eq (overlay-get ov 'invisible)
  5723. 'org-hide-block)
  5724. (delete-overlay ov)))
  5725. (overlays-at start)))
  5726. (setq ov (make-overlay start end))
  5727. (overlay-put ov 'invisible 'org-hide-block)
  5728. ;; make the block accessible to isearch
  5729. (overlay-put
  5730. ov 'isearch-open-invisible
  5731. (lambda (ov)
  5732. (when (member ov org-hide-block-overlays)
  5733. (setq org-hide-block-overlays
  5734. (delq ov org-hide-block-overlays)))
  5735. (when (eq (overlay-get ov 'invisible)
  5736. 'org-hide-block)
  5737. (delete-overlay ov))))
  5738. (push ov org-hide-block-overlays)))
  5739. (error "Not looking at a source block"))))
  5740. ;; org-tab-after-check-for-cycling-hook
  5741. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5742. ;; Remove overlays when changing major mode
  5743. (add-hook 'org-mode-hook
  5744. (lambda () (org-add-hook 'change-major-mode-hook
  5745. 'org-show-block-all 'append 'local)))
  5746. ;;; Org-goto
  5747. (defvar org-goto-window-configuration nil)
  5748. (defvar org-goto-marker nil)
  5749. (defvar org-goto-map
  5750. (let ((map (make-sparse-keymap)))
  5751. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5752. (while (setq cmd (pop cmds))
  5753. (substitute-key-definition cmd cmd map global-map)))
  5754. (suppress-keymap map)
  5755. (org-defkey map "\C-m" 'org-goto-ret)
  5756. (org-defkey map [(return)] 'org-goto-ret)
  5757. (org-defkey map [(left)] 'org-goto-left)
  5758. (org-defkey map [(right)] 'org-goto-right)
  5759. (org-defkey map [(control ?g)] 'org-goto-quit)
  5760. (org-defkey map "\C-i" 'org-cycle)
  5761. (org-defkey map [(tab)] 'org-cycle)
  5762. (org-defkey map [(down)] 'outline-next-visible-heading)
  5763. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5764. (if org-goto-auto-isearch
  5765. (if (fboundp 'define-key-after)
  5766. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5767. nil)
  5768. (org-defkey map "q" 'org-goto-quit)
  5769. (org-defkey map "n" 'outline-next-visible-heading)
  5770. (org-defkey map "p" 'outline-previous-visible-heading)
  5771. (org-defkey map "f" 'outline-forward-same-level)
  5772. (org-defkey map "b" 'outline-backward-same-level)
  5773. (org-defkey map "u" 'outline-up-heading))
  5774. (org-defkey map "/" 'org-occur)
  5775. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5776. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5777. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5778. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5779. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5780. map))
  5781. (defconst org-goto-help
  5782. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5783. RET=jump to location [Q]uit and return to previous location
  5784. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5785. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5786. ;; FIXME: Docstring does not mention both interfaces
  5787. (defun org-goto (&optional alternative-interface)
  5788. "Look up a different location in the current file, keeping current visibility.
  5789. When you want look-up or go to a different location in a document, the
  5790. fastest way is often to fold the entire buffer and then dive into the tree.
  5791. This method has the disadvantage, that the previous location will be folded,
  5792. which may not be what you want.
  5793. This command works around this by showing a copy of the current buffer
  5794. in an indirect buffer, in overview mode. You can dive into the tree in
  5795. that copy, use org-occur and incremental search to find a location.
  5796. When pressing RET or `Q', the command returns to the original buffer in
  5797. which the visibility is still unchanged. After RET is will also jump to
  5798. the location selected in the indirect buffer and expose the
  5799. the headline hierarchy above."
  5800. (interactive "P")
  5801. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5802. (org-refile-use-outline-path t)
  5803. (org-refile-target-verify-function nil)
  5804. (interface
  5805. (if (not alternative-interface)
  5806. org-goto-interface
  5807. (if (eq org-goto-interface 'outline)
  5808. 'outline-path-completion
  5809. 'outline)))
  5810. (org-goto-start-pos (point))
  5811. (selected-point
  5812. (if (eq interface 'outline)
  5813. (car (org-get-location (current-buffer) org-goto-help))
  5814. (let ((pa (org-refile-get-location "Goto: ")))
  5815. (org-refile-check-position pa)
  5816. (nth 3 pa)))))
  5817. (if selected-point
  5818. (progn
  5819. (org-mark-ring-push org-goto-start-pos)
  5820. (goto-char selected-point)
  5821. (if (or (org-invisible-p) (org-invisible-p2))
  5822. (org-show-context 'org-goto)))
  5823. (message "Quit"))))
  5824. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5825. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5826. (defvar org-goto-local-auto-isearch-map) ; defined below
  5827. (defun org-get-location (buf help)
  5828. "Let the user select a location in the Org-mode buffer BUF.
  5829. This function uses a recursive edit. It returns the selected position
  5830. or nil."
  5831. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5832. (isearch-hide-immediately nil)
  5833. (isearch-search-fun-function
  5834. (lambda () 'org-goto-local-search-headings))
  5835. (org-goto-selected-point org-goto-exit-command)
  5836. (pop-up-frames nil)
  5837. (special-display-buffer-names nil)
  5838. (special-display-regexps nil)
  5839. (special-display-function nil))
  5840. (save-excursion
  5841. (save-window-excursion
  5842. (delete-other-windows)
  5843. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5844. (switch-to-buffer
  5845. (condition-case nil
  5846. (make-indirect-buffer (current-buffer) "*org-goto*")
  5847. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5848. (with-output-to-temp-buffer "*Help*"
  5849. (princ help))
  5850. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5851. (setq buffer-read-only nil)
  5852. (let ((org-startup-truncated t)
  5853. (org-startup-folded nil)
  5854. (org-startup-align-all-tables nil))
  5855. (org-mode)
  5856. (org-overview))
  5857. (setq buffer-read-only t)
  5858. (if (and (boundp 'org-goto-start-pos)
  5859. (integer-or-marker-p org-goto-start-pos))
  5860. (let ((org-show-hierarchy-above t)
  5861. (org-show-siblings t)
  5862. (org-show-following-heading t))
  5863. (goto-char org-goto-start-pos)
  5864. (and (org-invisible-p) (org-show-context)))
  5865. (goto-char (point-min)))
  5866. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5867. (message "Select location and press RET")
  5868. (use-local-map org-goto-map)
  5869. (recursive-edit)
  5870. ))
  5871. (kill-buffer "*org-goto*")
  5872. (cons org-goto-selected-point org-goto-exit-command)))
  5873. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5874. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5875. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5876. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5877. (defun org-goto-local-search-headings (string bound noerror)
  5878. "Search and make sure that any matches are in headlines."
  5879. (catch 'return
  5880. (while (if isearch-forward
  5881. (search-forward string bound noerror)
  5882. (search-backward string bound noerror))
  5883. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5884. (and (member :headline context)
  5885. (not (member :tags context))))
  5886. (throw 'return (point))))))
  5887. (defun org-goto-local-auto-isearch ()
  5888. "Start isearch."
  5889. (interactive)
  5890. (goto-char (point-min))
  5891. (let ((keys (this-command-keys)))
  5892. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5893. (isearch-mode t)
  5894. (isearch-process-search-char (string-to-char keys)))))
  5895. (defun org-goto-ret (&optional arg)
  5896. "Finish `org-goto' by going to the new location."
  5897. (interactive "P")
  5898. (setq org-goto-selected-point (point)
  5899. org-goto-exit-command 'return)
  5900. (throw 'exit nil))
  5901. (defun org-goto-left ()
  5902. "Finish `org-goto' by going to the new location."
  5903. (interactive)
  5904. (if (org-on-heading-p)
  5905. (progn
  5906. (beginning-of-line 1)
  5907. (setq org-goto-selected-point (point)
  5908. org-goto-exit-command 'left)
  5909. (throw 'exit nil))
  5910. (error "Not on a heading")))
  5911. (defun org-goto-right ()
  5912. "Finish `org-goto' by going to the new location."
  5913. (interactive)
  5914. (if (org-on-heading-p)
  5915. (progn
  5916. (setq org-goto-selected-point (point)
  5917. org-goto-exit-command 'right)
  5918. (throw 'exit nil))
  5919. (error "Not on a heading")))
  5920. (defun org-goto-quit ()
  5921. "Finish `org-goto' without cursor motion."
  5922. (interactive)
  5923. (setq org-goto-selected-point nil)
  5924. (setq org-goto-exit-command 'quit)
  5925. (throw 'exit nil))
  5926. ;;; Indirect buffer display of subtrees
  5927. (defvar org-indirect-dedicated-frame nil
  5928. "This is the frame being used for indirect tree display.")
  5929. (defvar org-last-indirect-buffer nil)
  5930. (defun org-tree-to-indirect-buffer (&optional arg)
  5931. "Create indirect buffer and narrow it to current subtree.
  5932. With numerical prefix ARG, go up to this level and then take that tree.
  5933. If ARG is negative, go up that many levels.
  5934. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5935. indirect buffer previously made with this command, to avoid proliferation of
  5936. indirect buffers. However, when you call the command with a \
  5937. \\[universal-argument] prefix, or
  5938. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5939. is kept so that you can work with several indirect buffers at the same time.
  5940. If `org-indirect-buffer-display' is `dedicated-frame', the \
  5941. \\[universal-argument] prefix also
  5942. requests that a new frame be made for the new buffer, so that the dedicated
  5943. frame is not changed."
  5944. (interactive "P")
  5945. (let ((cbuf (current-buffer))
  5946. (cwin (selected-window))
  5947. (pos (point))
  5948. beg end level heading ibuf)
  5949. (save-excursion
  5950. (org-back-to-heading t)
  5951. (when (numberp arg)
  5952. (setq level (org-outline-level))
  5953. (if (< arg 0) (setq arg (+ level arg)))
  5954. (while (> (setq level (org-outline-level)) arg)
  5955. (outline-up-heading 1 t)))
  5956. (setq beg (point)
  5957. heading (org-get-heading))
  5958. (org-end-of-subtree t t)
  5959. (if (org-on-heading-p) (backward-char 1))
  5960. (setq end (point)))
  5961. (if (and (buffer-live-p org-last-indirect-buffer)
  5962. (not (eq org-indirect-buffer-display 'new-frame))
  5963. (not arg))
  5964. (kill-buffer org-last-indirect-buffer))
  5965. (setq ibuf (org-get-indirect-buffer cbuf)
  5966. org-last-indirect-buffer ibuf)
  5967. (cond
  5968. ((or (eq org-indirect-buffer-display 'new-frame)
  5969. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5970. (select-frame (make-frame))
  5971. (delete-other-windows)
  5972. (switch-to-buffer ibuf)
  5973. (org-set-frame-title heading))
  5974. ((eq org-indirect-buffer-display 'dedicated-frame)
  5975. (raise-frame
  5976. (select-frame (or (and org-indirect-dedicated-frame
  5977. (frame-live-p org-indirect-dedicated-frame)
  5978. org-indirect-dedicated-frame)
  5979. (setq org-indirect-dedicated-frame (make-frame)))))
  5980. (delete-other-windows)
  5981. (switch-to-buffer ibuf)
  5982. (org-set-frame-title (concat "Indirect: " heading)))
  5983. ((eq org-indirect-buffer-display 'current-window)
  5984. (switch-to-buffer ibuf))
  5985. ((eq org-indirect-buffer-display 'other-window)
  5986. (pop-to-buffer ibuf))
  5987. (t (error "Invalid value")))
  5988. (if (featurep 'xemacs)
  5989. (save-excursion (org-mode) (turn-on-font-lock)))
  5990. (narrow-to-region beg end)
  5991. (show-all)
  5992. (goto-char pos)
  5993. (and (window-live-p cwin) (select-window cwin))))
  5994. (defun org-get-indirect-buffer (&optional buffer)
  5995. (setq buffer (or buffer (current-buffer)))
  5996. (let ((n 1) (base (buffer-name buffer)) bname)
  5997. (while (buffer-live-p
  5998. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5999. (setq n (1+ n)))
  6000. (condition-case nil
  6001. (make-indirect-buffer buffer bname 'clone)
  6002. (error (make-indirect-buffer buffer bname)))))
  6003. (defun org-set-frame-title (title)
  6004. "Set the title of the current frame to the string TITLE."
  6005. ;; FIXME: how to name a single frame in XEmacs???
  6006. (unless (featurep 'xemacs)
  6007. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6008. ;;;; Structure editing
  6009. ;;; Inserting headlines
  6010. (defun org-previous-line-empty-p ()
  6011. (save-excursion
  6012. (and (not (bobp))
  6013. (or (beginning-of-line 0) t)
  6014. (save-match-data
  6015. (looking-at "[ \t]*$")))))
  6016. (defun org-insert-heading (&optional force-heading invisible-ok)
  6017. "Insert a new heading or item with same depth at point.
  6018. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6019. If point is at the beginning of a headline, insert a sibling before the
  6020. current headline. If point is not at the beginning, split the line,
  6021. create the new headline with the text in the current line after point
  6022. \(but see also the variable `org-M-RET-may-split-line').
  6023. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6024. This is important for non-interactive uses of the command."
  6025. (interactive "P")
  6026. (if (or (= (buffer-size) 0)
  6027. (and (not (save-excursion
  6028. (and (ignore-errors (org-back-to-heading invisible-ok))
  6029. (org-on-heading-p))))
  6030. (not (org-in-item-p))))
  6031. (progn
  6032. (insert "\n* ")
  6033. (run-hooks 'org-insert-heading-hook))
  6034. (when (or force-heading (not (org-insert-item)))
  6035. (let* ((empty-line-p nil)
  6036. (level nil)
  6037. (on-heading (org-on-heading-p))
  6038. (head (save-excursion
  6039. (condition-case nil
  6040. (progn
  6041. (org-back-to-heading invisible-ok)
  6042. (when (and (not on-heading)
  6043. (featurep 'org-inlinetask)
  6044. (integerp org-inlinetask-min-level)
  6045. (>= (length (match-string 0))
  6046. org-inlinetask-min-level))
  6047. ;; Find a heading level before the inline task
  6048. (while (and (setq level (org-up-heading-safe))
  6049. (>= level org-inlinetask-min-level)))
  6050. (if (org-on-heading-p)
  6051. (org-back-to-heading invisible-ok)
  6052. (error "This should not happen")))
  6053. (setq empty-line-p (org-previous-line-empty-p))
  6054. (match-string 0))
  6055. (error "*"))))
  6056. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6057. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6058. pos hide-previous previous-pos)
  6059. (cond
  6060. ((and (org-on-heading-p) (bolp)
  6061. (or (bobp)
  6062. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  6063. ;; insert before the current line
  6064. (open-line (if blank 2 1)))
  6065. ((and (bolp)
  6066. (not org-insert-heading-respect-content)
  6067. (or (bobp)
  6068. (save-excursion
  6069. (backward-char 1) (not (org-invisible-p)))))
  6070. ;; insert right here
  6071. nil)
  6072. (t
  6073. ;; somewhere in the line
  6074. (save-excursion
  6075. (setq previous-pos (point-at-bol))
  6076. (end-of-line)
  6077. (setq hide-previous (org-invisible-p)))
  6078. (and org-insert-heading-respect-content (org-show-subtree))
  6079. (let ((split
  6080. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6081. (save-excursion
  6082. (let ((p (point)))
  6083. (goto-char (point-at-bol))
  6084. (and (looking-at org-complex-heading-regexp)
  6085. (> p (match-beginning 4)))))))
  6086. tags pos)
  6087. (cond
  6088. (org-insert-heading-respect-content
  6089. (org-end-of-subtree nil t)
  6090. (when (featurep 'org-inlinetask)
  6091. (while (and (not (eobp))
  6092. (looking-at "\\(\\*+\\)[ \t]+")
  6093. (>= (length (match-string 1))
  6094. org-inlinetask-min-level))
  6095. (org-end-of-subtree nil t)))
  6096. (or (bolp) (newline))
  6097. (or (org-previous-line-empty-p)
  6098. (and blank (newline)))
  6099. (open-line 1))
  6100. ((org-on-heading-p)
  6101. (when hide-previous
  6102. (show-children)
  6103. (org-show-entry))
  6104. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6105. (setq tags (and (match-end 2) (match-string 2)))
  6106. (and (match-end 1)
  6107. (delete-region (match-beginning 1) (match-end 1)))
  6108. (setq pos (point-at-bol))
  6109. (or split (end-of-line 1))
  6110. (delete-horizontal-space)
  6111. (if (string-match "\\`\\*+\\'"
  6112. (buffer-substring (point-at-bol) (point)))
  6113. (insert " "))
  6114. (newline (if blank 2 1))
  6115. (when tags
  6116. (save-excursion
  6117. (goto-char pos)
  6118. (end-of-line 1)
  6119. (insert " " tags)
  6120. (org-set-tags nil 'align))))
  6121. (t
  6122. (or split (end-of-line 1))
  6123. (newline (if blank 2 1)))))))
  6124. (insert head) (just-one-space)
  6125. (setq pos (point))
  6126. (end-of-line 1)
  6127. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6128. (when (and org-insert-heading-respect-content hide-previous)
  6129. (save-excursion
  6130. (goto-char previous-pos)
  6131. (hide-subtree)))
  6132. (run-hooks 'org-insert-heading-hook)))))
  6133. (defun org-get-heading (&optional no-tags)
  6134. "Return the heading of the current entry, without the stars."
  6135. (save-excursion
  6136. (org-back-to-heading t)
  6137. (if (looking-at
  6138. (if no-tags
  6139. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6140. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6141. (match-string 1) "")))
  6142. (defun org-heading-components ()
  6143. "Return the components of the current heading.
  6144. This is a list with the following elements:
  6145. - the level as an integer
  6146. - the reduced level, different if `org-odd-levels-only' is set.
  6147. - the TODO keyword, or nil
  6148. - the priority character, like ?A, or nil if no priority is given
  6149. - the headline text itself, or the tags string if no headline text
  6150. - the tags string, or nil."
  6151. (save-excursion
  6152. (org-back-to-heading t)
  6153. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6154. (list (length (match-string 1))
  6155. (org-reduced-level (length (match-string 1)))
  6156. (org-match-string-no-properties 2)
  6157. (and (match-end 3) (aref (match-string 3) 2))
  6158. (org-match-string-no-properties 4)
  6159. (org-match-string-no-properties 5)))))
  6160. (defun org-get-entry ()
  6161. "Get the entry text, after heading, entire subtree."
  6162. (save-excursion
  6163. (org-back-to-heading t)
  6164. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6165. (defun org-insert-heading-after-current ()
  6166. "Insert a new heading with same level as current, after current subtree."
  6167. (interactive)
  6168. (org-back-to-heading)
  6169. (org-insert-heading)
  6170. (org-move-subtree-down)
  6171. (end-of-line 1))
  6172. (defun org-insert-heading-respect-content ()
  6173. (interactive)
  6174. (let ((org-insert-heading-respect-content t))
  6175. (org-insert-heading t)))
  6176. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6177. (interactive "P")
  6178. (let ((org-insert-heading-respect-content t))
  6179. (org-insert-todo-heading force-state t)))
  6180. (defun org-insert-todo-heading (arg &optional force-heading)
  6181. "Insert a new heading with the same level and TODO state as current heading.
  6182. If the heading has no TODO state, or if the state is DONE, use the first
  6183. state (TODO by default). Also with prefix arg, force first state."
  6184. (interactive "P")
  6185. (when (or force-heading (not (org-insert-item 'checkbox)))
  6186. (org-insert-heading force-heading)
  6187. (save-excursion
  6188. (org-back-to-heading)
  6189. (outline-previous-heading)
  6190. (looking-at org-todo-line-regexp))
  6191. (let*
  6192. ((new-mark-x
  6193. (if (or arg
  6194. (not (match-beginning 2))
  6195. (member (match-string 2) org-done-keywords))
  6196. (car org-todo-keywords-1)
  6197. (match-string 2)))
  6198. (new-mark
  6199. (or
  6200. (run-hook-with-args-until-success
  6201. 'org-todo-get-default-hook new-mark-x nil)
  6202. new-mark-x)))
  6203. (beginning-of-line 1)
  6204. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6205. (if org-treat-insert-todo-heading-as-state-change
  6206. (org-todo new-mark)
  6207. (insert new-mark " "))))
  6208. (when org-provide-todo-statistics
  6209. (org-update-parent-todo-statistics))))
  6210. (defun org-insert-subheading (arg)
  6211. "Insert a new subheading and demote it.
  6212. Works for outline headings and for plain lists alike."
  6213. (interactive "P")
  6214. (org-insert-heading arg)
  6215. (cond
  6216. ((org-on-heading-p) (org-do-demote))
  6217. ((org-at-item-p) (org-indent-item))))
  6218. (defun org-insert-todo-subheading (arg)
  6219. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6220. Works for outline headings and for plain lists alike."
  6221. (interactive "P")
  6222. (org-insert-todo-heading arg)
  6223. (cond
  6224. ((org-on-heading-p) (org-do-demote))
  6225. ((org-at-item-p) (org-indent-item))))
  6226. ;;; Promotion and Demotion
  6227. (defvar org-after-demote-entry-hook nil
  6228. "Hook run after an entry has been demoted.
  6229. The cursor will be at the beginning of the entry.
  6230. When a subtree is being demoted, the hook will be called for each node.")
  6231. (defvar org-after-promote-entry-hook nil
  6232. "Hook run after an entry has been promoted.
  6233. The cursor will be at the beginning of the entry.
  6234. When a subtree is being promoted, the hook will be called for each node.")
  6235. (defun org-promote-subtree ()
  6236. "Promote the entire subtree.
  6237. See also `org-promote'."
  6238. (interactive)
  6239. (save-excursion
  6240. (org-map-tree 'org-promote))
  6241. (org-fix-position-after-promote))
  6242. (defun org-demote-subtree ()
  6243. "Demote the entire subtree. See `org-demote'.
  6244. See also `org-promote'."
  6245. (interactive)
  6246. (save-excursion
  6247. (org-map-tree 'org-demote))
  6248. (org-fix-position-after-promote))
  6249. (defun org-do-promote ()
  6250. "Promote the current heading higher up the tree.
  6251. If the region is active in `transient-mark-mode', promote all headings
  6252. in the region."
  6253. (interactive)
  6254. (save-excursion
  6255. (if (org-region-active-p)
  6256. (org-map-region 'org-promote (region-beginning) (region-end))
  6257. (org-promote)))
  6258. (org-fix-position-after-promote))
  6259. (defun org-do-demote ()
  6260. "Demote the current heading lower down the tree.
  6261. If the region is active in `transient-mark-mode', demote all headings
  6262. in the region."
  6263. (interactive)
  6264. (save-excursion
  6265. (if (org-region-active-p)
  6266. (org-map-region 'org-demote (region-beginning) (region-end))
  6267. (org-demote)))
  6268. (org-fix-position-after-promote))
  6269. (defun org-fix-position-after-promote ()
  6270. "Make sure that after pro/demotion cursor position is right."
  6271. (let ((pos (point)))
  6272. (when (save-excursion
  6273. (beginning-of-line 1)
  6274. (looking-at org-todo-line-regexp)
  6275. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6276. (cond ((eobp) (insert " "))
  6277. ((eolp) (insert " "))
  6278. ((equal (char-after) ?\ ) (forward-char 1))))))
  6279. (defun org-current-level ()
  6280. "Return the level of the current entry, or nil if before the first headline.
  6281. The level is the number of stars at the beginning of the headline."
  6282. (save-excursion
  6283. (condition-case nil
  6284. (progn
  6285. (org-back-to-heading t)
  6286. (funcall outline-level))
  6287. (error nil))))
  6288. (defun org-get-previous-line-level ()
  6289. "Return the outline depth of the last headline before the current line.
  6290. Returns 0 for the first headline in the buffer, and nil if before the
  6291. first headline."
  6292. (let ((current-level (org-current-level))
  6293. (prev-level (when (> (line-number-at-pos) 1)
  6294. (save-excursion
  6295. (beginning-of-line 0)
  6296. (org-current-level)))))
  6297. (cond ((null current-level) nil) ; Before first headline
  6298. ((null prev-level) 0) ; At first headline
  6299. (prev-level))))
  6300. (defun org-reduced-level (l)
  6301. "Compute the effective level of a heading.
  6302. This takes into account the setting of `org-odd-levels-only'."
  6303. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6304. (defun org-level-increment ()
  6305. "Return the number of stars that will be added or removed at a
  6306. time to headlines when structure editing, based on the value of
  6307. `org-odd-levels-only'."
  6308. (if org-odd-levels-only 2 1))
  6309. (defun org-get-valid-level (level &optional change)
  6310. "Rectify a level change under the influence of `org-odd-levels-only'
  6311. LEVEL is a current level, CHANGE is by how much the level should be
  6312. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6313. even level numbers will become the next higher odd number."
  6314. (if org-odd-levels-only
  6315. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6316. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6317. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6318. (max 1 (+ level (or change 0)))))
  6319. (if (boundp 'define-obsolete-function-alias)
  6320. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6321. (define-obsolete-function-alias 'org-get-legal-level
  6322. 'org-get-valid-level)
  6323. (define-obsolete-function-alias 'org-get-legal-level
  6324. 'org-get-valid-level "23.1")))
  6325. (defun org-promote ()
  6326. "Promote the current heading higher up the tree.
  6327. If the region is active in `transient-mark-mode', promote all headings
  6328. in the region."
  6329. (org-back-to-heading t)
  6330. (let* ((level (save-match-data (funcall outline-level)))
  6331. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6332. (diff (abs (- level (length up-head) -1))))
  6333. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6334. (replace-match up-head nil t)
  6335. ;; Fixup tag positioning
  6336. (and org-auto-align-tags (org-set-tags nil t))
  6337. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6338. (run-hooks 'org-after-promote-entry-hook)))
  6339. (defun org-demote ()
  6340. "Demote the current heading lower down the tree.
  6341. If the region is active in `transient-mark-mode', demote all headings
  6342. in the region."
  6343. (org-back-to-heading t)
  6344. (let* ((level (save-match-data (funcall outline-level)))
  6345. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6346. (diff (abs (- level (length down-head) -1))))
  6347. (replace-match down-head nil t)
  6348. ;; Fixup tag positioning
  6349. (and org-auto-align-tags (org-set-tags nil t))
  6350. (if org-adapt-indentation (org-fixup-indentation diff))
  6351. (run-hooks 'org-after-demote-entry-hook)))
  6352. (defun org-cycle-level ()
  6353. "Cycle the level of an empty headline through possible states.
  6354. This goes first to child, then to parent, level, then up the hierarchy.
  6355. After top level, it switches back to sibling level."
  6356. (interactive)
  6357. (let ((org-adapt-indentation nil))
  6358. (when (org-point-at-end-of-empty-headline)
  6359. (setq this-command 'org-cycle-level) ; Only needed for caching
  6360. (let ((cur-level (org-current-level))
  6361. (prev-level (org-get-previous-line-level)))
  6362. (cond
  6363. ;; If first headline in file, promote to top-level.
  6364. ((= prev-level 0)
  6365. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6366. do (org-do-promote)))
  6367. ;; If same level as prev, demote one.
  6368. ((= prev-level cur-level)
  6369. (org-do-demote))
  6370. ;; If parent is top-level, promote to top level if not already.
  6371. ((= prev-level 1)
  6372. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6373. do (org-do-promote)))
  6374. ;; If top-level, return to prev-level.
  6375. ((= cur-level 1)
  6376. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6377. do (org-do-demote)))
  6378. ;; If less than prev-level, promote one.
  6379. ((< cur-level prev-level)
  6380. (org-do-promote))
  6381. ;; If deeper than prev-level, promote until higher than
  6382. ;; prev-level.
  6383. ((> cur-level prev-level)
  6384. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6385. do (org-do-promote))))
  6386. t))))
  6387. (defun org-map-tree (fun)
  6388. "Call FUN for every heading underneath the current one."
  6389. (org-back-to-heading)
  6390. (let ((level (funcall outline-level)))
  6391. (save-excursion
  6392. (funcall fun)
  6393. (while (and (progn
  6394. (outline-next-heading)
  6395. (> (funcall outline-level) level))
  6396. (not (eobp)))
  6397. (funcall fun)))))
  6398. (defun org-map-region (fun beg end)
  6399. "Call FUN for every heading between BEG and END."
  6400. (let ((org-ignore-region t))
  6401. (save-excursion
  6402. (setq end (copy-marker end))
  6403. (goto-char beg)
  6404. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6405. (< (point) end))
  6406. (funcall fun))
  6407. (while (and (progn
  6408. (outline-next-heading)
  6409. (< (point) end))
  6410. (not (eobp)))
  6411. (funcall fun)))))
  6412. (defun org-fixup-indentation (diff)
  6413. "Change the indentation in the current entry by DIFF.
  6414. However, if any line in the current entry has no indentation, or if it
  6415. would end up with no indentation after the change, nothing at all is done."
  6416. (save-excursion
  6417. (let ((end (save-excursion (outline-next-heading)
  6418. (point-marker)))
  6419. (prohibit (if (> diff 0)
  6420. "^\\S-"
  6421. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6422. col)
  6423. (unless (save-excursion (end-of-line 1)
  6424. (re-search-forward prohibit end t))
  6425. (while (and (< (point) end)
  6426. (re-search-forward "^[ \t]+" end t))
  6427. (goto-char (match-end 0))
  6428. (setq col (current-column))
  6429. (if (< diff 0) (replace-match ""))
  6430. (org-indent-to-column (+ diff col))))
  6431. (move-marker end nil))))
  6432. (defun org-convert-to-odd-levels ()
  6433. "Convert an org-mode file with all levels allowed to one with odd levels.
  6434. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6435. level 5 etc."
  6436. (interactive)
  6437. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6438. (let ((outline-regexp org-outline-regexp)
  6439. (outline-level 'org-outline-level)
  6440. (org-odd-levels-only nil) n)
  6441. (save-excursion
  6442. (goto-char (point-min))
  6443. (while (re-search-forward "^\\*\\*+ " nil t)
  6444. (setq n (- (length (match-string 0)) 2))
  6445. (while (>= (setq n (1- n)) 0)
  6446. (org-demote))
  6447. (end-of-line 1))))))
  6448. (defun org-convert-to-oddeven-levels ()
  6449. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6450. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6451. file contains a section with an even level, conversion would
  6452. destroy the structure of the file. An error is signaled in this
  6453. case."
  6454. (interactive)
  6455. (goto-char (point-min))
  6456. ;; First check if there are no even levels
  6457. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6458. (org-show-context t)
  6459. (error "Not all levels are odd in this file. Conversion not possible"))
  6460. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6461. (let ((outline-regexp org-outline-regexp)
  6462. (outline-level 'org-outline-level)
  6463. (org-odd-levels-only nil) n)
  6464. (save-excursion
  6465. (goto-char (point-min))
  6466. (while (re-search-forward "^\\*\\*+ " nil t)
  6467. (setq n (/ (1- (length (match-string 0))) 2))
  6468. (while (>= (setq n (1- n)) 0)
  6469. (org-promote))
  6470. (end-of-line 1))))))
  6471. (defun org-tr-level (n)
  6472. "Make N odd if required."
  6473. (if org-odd-levels-only (1+ (/ n 2)) n))
  6474. ;;; Vertical tree motion, cutting and pasting of subtrees
  6475. (defun org-move-subtree-up (&optional arg)
  6476. "Move the current subtree up past ARG headlines of the same level."
  6477. (interactive "p")
  6478. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6479. (defun org-move-subtree-down (&optional arg)
  6480. "Move the current subtree down past ARG headlines of the same level."
  6481. (interactive "p")
  6482. (setq arg (prefix-numeric-value arg))
  6483. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6484. 'org-get-last-sibling))
  6485. (ins-point (make-marker))
  6486. (cnt (abs arg))
  6487. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6488. ;; Select the tree
  6489. (org-back-to-heading)
  6490. (setq beg0 (point))
  6491. (save-excursion
  6492. (setq ne-beg (org-back-over-empty-lines))
  6493. (setq beg (point)))
  6494. (save-match-data
  6495. (save-excursion (outline-end-of-heading)
  6496. (setq folded (org-invisible-p)))
  6497. (outline-end-of-subtree))
  6498. (outline-next-heading)
  6499. (setq ne-end (org-back-over-empty-lines))
  6500. (setq end (point))
  6501. (goto-char beg0)
  6502. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6503. ;; include less whitespace
  6504. (save-excursion
  6505. (goto-char beg)
  6506. (forward-line (- ne-beg ne-end))
  6507. (setq beg (point))))
  6508. ;; Find insertion point, with error handling
  6509. (while (> cnt 0)
  6510. (or (and (funcall movfunc) (looking-at outline-regexp))
  6511. (progn (goto-char beg0)
  6512. (error "Cannot move past superior level or buffer limit")))
  6513. (setq cnt (1- cnt)))
  6514. (if (> arg 0)
  6515. ;; Moving forward - still need to move over subtree
  6516. (progn (org-end-of-subtree t t)
  6517. (save-excursion
  6518. (org-back-over-empty-lines)
  6519. (or (bolp) (newline)))))
  6520. (setq ne-ins (org-back-over-empty-lines))
  6521. (move-marker ins-point (point))
  6522. (setq txt (buffer-substring beg end))
  6523. (org-save-markers-in-region beg end)
  6524. (delete-region beg end)
  6525. (org-remove-empty-overlays-at beg)
  6526. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6527. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6528. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6529. (let ((bbb (point)))
  6530. (insert-before-markers txt)
  6531. (org-reinstall-markers-in-region bbb)
  6532. (move-marker ins-point bbb))
  6533. (or (bolp) (insert "\n"))
  6534. (setq ins-end (point))
  6535. (goto-char ins-point)
  6536. (org-skip-whitespace)
  6537. (when (and (< arg 0)
  6538. (org-first-sibling-p)
  6539. (> ne-ins ne-beg))
  6540. ;; Move whitespace back to beginning
  6541. (save-excursion
  6542. (goto-char ins-end)
  6543. (let ((kill-whole-line t))
  6544. (kill-line (- ne-ins ne-beg)) (point)))
  6545. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6546. (move-marker ins-point nil)
  6547. (if folded
  6548. (hide-subtree)
  6549. (org-show-entry)
  6550. (show-children)
  6551. (org-cycle-hide-drawers 'children))
  6552. (org-clean-visibility-after-subtree-move)))
  6553. (defvar org-subtree-clip ""
  6554. "Clipboard for cut and paste of subtrees.
  6555. This is actually only a copy of the kill, because we use the normal kill
  6556. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6557. (defvar org-subtree-clip-folded nil
  6558. "Was the last copied subtree folded?
  6559. This is used to fold the tree back after pasting.")
  6560. (defun org-cut-subtree (&optional n)
  6561. "Cut the current subtree into the clipboard.
  6562. With prefix arg N, cut this many sequential subtrees.
  6563. This is a short-hand for marking the subtree and then cutting it."
  6564. (interactive "p")
  6565. (org-copy-subtree n 'cut))
  6566. (defun org-copy-subtree (&optional n cut force-store-markers)
  6567. "Cut the current subtree into the clipboard.
  6568. With prefix arg N, cut this many sequential subtrees.
  6569. This is a short-hand for marking the subtree and then copying it.
  6570. If CUT is non-nil, actually cut the subtree.
  6571. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6572. of some markers in the region, even if CUT is non-nil. This is
  6573. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6574. (interactive "p")
  6575. (let (beg end folded (beg0 (point)))
  6576. (if (interactive-p)
  6577. (org-back-to-heading nil) ; take what looks like a subtree
  6578. (org-back-to-heading t)) ; take what is really there
  6579. (org-back-over-empty-lines)
  6580. (setq beg (point))
  6581. (skip-chars-forward " \t\r\n")
  6582. (save-match-data
  6583. (save-excursion (outline-end-of-heading)
  6584. (setq folded (org-invisible-p)))
  6585. (condition-case nil
  6586. (org-forward-same-level (1- n) t)
  6587. (error nil))
  6588. (org-end-of-subtree t t))
  6589. (org-back-over-empty-lines)
  6590. (setq end (point))
  6591. (goto-char beg0)
  6592. (when (> end beg)
  6593. (setq org-subtree-clip-folded folded)
  6594. (when (or cut force-store-markers)
  6595. (org-save-markers-in-region beg end))
  6596. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6597. (setq org-subtree-clip (current-kill 0))
  6598. (message "%s: Subtree(s) with %d characters"
  6599. (if cut "Cut" "Copied")
  6600. (length org-subtree-clip)))))
  6601. (defun org-paste-subtree (&optional level tree for-yank)
  6602. "Paste the clipboard as a subtree, with modification of headline level.
  6603. The entire subtree is promoted or demoted in order to match a new headline
  6604. level.
  6605. If the cursor is at the beginning of a headline, the same level as
  6606. that headline is used to paste the tree
  6607. If not, the new level is derived from the *visible* headings
  6608. before and after the insertion point, and taken to be the inferior headline
  6609. level of the two. So if the previous visible heading is level 3 and the
  6610. next is level 4 (or vice versa), level 4 will be used for insertion.
  6611. This makes sure that the subtree remains an independent subtree and does
  6612. not swallow low level entries.
  6613. You can also force a different level, either by using a numeric prefix
  6614. argument, or by inserting the heading marker by hand. For example, if the
  6615. cursor is after \"*****\", then the tree will be shifted to level 5.
  6616. If optional TREE is given, use this text instead of the kill ring.
  6617. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6618. move back over whitespace before inserting, and move point to the end of
  6619. the inserted text when done."
  6620. (interactive "P")
  6621. (setq tree (or tree (and kill-ring (current-kill 0))))
  6622. (unless (org-kill-is-subtree-p tree)
  6623. (error "%s"
  6624. (substitute-command-keys
  6625. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6626. (let* ((visp (not (org-invisible-p)))
  6627. (txt tree)
  6628. (^re (concat "^\\(" outline-regexp "\\)"))
  6629. (re (concat "\\(" outline-regexp "\\)"))
  6630. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6631. (old-level (if (string-match ^re txt)
  6632. (- (match-end 0) (match-beginning 0) 1)
  6633. -1))
  6634. (force-level (cond (level (prefix-numeric-value level))
  6635. ((and (looking-at "[ \t]*$")
  6636. (string-match
  6637. ^re_ (buffer-substring
  6638. (point-at-bol) (point))))
  6639. (- (match-end 1) (match-beginning 1)))
  6640. ((and (bolp)
  6641. (looking-at org-outline-regexp))
  6642. (- (match-end 0) (point) 1))
  6643. (t nil)))
  6644. (previous-level (save-excursion
  6645. (condition-case nil
  6646. (progn
  6647. (outline-previous-visible-heading 1)
  6648. (if (looking-at re)
  6649. (- (match-end 0) (match-beginning 0) 1)
  6650. 1))
  6651. (error 1))))
  6652. (next-level (save-excursion
  6653. (condition-case nil
  6654. (progn
  6655. (or (looking-at outline-regexp)
  6656. (outline-next-visible-heading 1))
  6657. (if (looking-at re)
  6658. (- (match-end 0) (match-beginning 0) 1)
  6659. 1))
  6660. (error 1))))
  6661. (new-level (or force-level (max previous-level next-level)))
  6662. (shift (if (or (= old-level -1)
  6663. (= new-level -1)
  6664. (= old-level new-level))
  6665. 0
  6666. (- new-level old-level)))
  6667. (delta (if (> shift 0) -1 1))
  6668. (func (if (> shift 0) 'org-demote 'org-promote))
  6669. (org-odd-levels-only nil)
  6670. beg end newend)
  6671. ;; Remove the forced level indicator
  6672. (if force-level
  6673. (delete-region (point-at-bol) (point)))
  6674. ;; Paste
  6675. (beginning-of-line 1)
  6676. (unless for-yank (org-back-over-empty-lines))
  6677. (setq beg (point))
  6678. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6679. (insert-before-markers txt)
  6680. (unless (string-match "\n\\'" txt) (insert "\n"))
  6681. (setq newend (point))
  6682. (org-reinstall-markers-in-region beg)
  6683. (setq end (point))
  6684. (goto-char beg)
  6685. (skip-chars-forward " \t\n\r")
  6686. (setq beg (point))
  6687. (if (and (org-invisible-p) visp)
  6688. (save-excursion (outline-show-heading)))
  6689. ;; Shift if necessary
  6690. (unless (= shift 0)
  6691. (save-restriction
  6692. (narrow-to-region beg end)
  6693. (while (not (= shift 0))
  6694. (org-map-region func (point-min) (point-max))
  6695. (setq shift (+ delta shift)))
  6696. (goto-char (point-min))
  6697. (setq newend (point-max))))
  6698. (when (or (interactive-p) for-yank)
  6699. (message "Clipboard pasted as level %d subtree" new-level))
  6700. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6701. kill-ring
  6702. (eq org-subtree-clip (current-kill 0))
  6703. org-subtree-clip-folded)
  6704. ;; The tree was folded before it was killed/copied
  6705. (hide-subtree))
  6706. (and for-yank (goto-char newend))))
  6707. (defun org-kill-is-subtree-p (&optional txt)
  6708. "Check if the current kill is an outline subtree, or a set of trees.
  6709. Returns nil if kill does not start with a headline, or if the first
  6710. headline level is not the largest headline level in the tree.
  6711. So this will actually accept several entries of equal levels as well,
  6712. which is OK for `org-paste-subtree'.
  6713. If optional TXT is given, check this string instead of the current kill."
  6714. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6715. (start-level (and kill
  6716. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6717. org-outline-regexp "\\)")
  6718. kill)
  6719. (- (match-end 2) (match-beginning 2) 1)))
  6720. (re (concat "^" org-outline-regexp))
  6721. (start (1+ (or (match-beginning 2) -1))))
  6722. (if (not start-level)
  6723. (progn
  6724. nil) ;; does not even start with a heading
  6725. (catch 'exit
  6726. (while (setq start (string-match re kill (1+ start)))
  6727. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6728. (throw 'exit nil)))
  6729. t))))
  6730. (defvar org-markers-to-move nil
  6731. "Markers that should be moved with a cut-and-paste operation.
  6732. Those markers are stored together with their positions relative to
  6733. the start of the region.")
  6734. (defun org-save-markers-in-region (beg end)
  6735. "Check markers in region.
  6736. If these markers are between BEG and END, record their position relative
  6737. to BEG, so that after moving the block of text, we can put the markers back
  6738. into place.
  6739. This function gets called just before an entry or tree gets cut from the
  6740. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6741. called immediately, to move the markers with the entries."
  6742. (setq org-markers-to-move nil)
  6743. (when (featurep 'org-clock)
  6744. (org-clock-save-markers-for-cut-and-paste beg end))
  6745. (when (featurep 'org-agenda)
  6746. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6747. (defun org-check-and-save-marker (marker beg end)
  6748. "Check if MARKER is between BEG and END.
  6749. If yes, remember the marker and the distance to BEG."
  6750. (when (and (marker-buffer marker)
  6751. (equal (marker-buffer marker) (current-buffer)))
  6752. (if (and (>= marker beg) (< marker end))
  6753. (push (cons marker (- marker beg)) org-markers-to-move))))
  6754. (defun org-reinstall-markers-in-region (beg)
  6755. "Move all remembered markers to their position relative to BEG."
  6756. (mapc (lambda (x)
  6757. (move-marker (car x) (+ beg (cdr x))))
  6758. org-markers-to-move)
  6759. (setq org-markers-to-move nil))
  6760. (defun org-narrow-to-subtree ()
  6761. "Narrow buffer to the current subtree."
  6762. (interactive)
  6763. (save-excursion
  6764. (save-match-data
  6765. (narrow-to-region
  6766. (progn (org-back-to-heading t) (point))
  6767. (progn (org-end-of-subtree t t)
  6768. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6769. (point))))))
  6770. (eval-when-compile
  6771. (defvar org-property-drawer-re))
  6772. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6773. "Clone the task (subtree) at point N times.
  6774. The clones will be inserted as siblings.
  6775. In interactive use, the user will be prompted for the number of
  6776. clones to be produced, and for a time SHIFT, which may be a
  6777. repeater as used in time stamps, for example `+3d'.
  6778. When a valid repeater is given and the entry contains any time
  6779. stamps, the clones will become a sequence in time, with time
  6780. stamps in the subtree shifted for each clone produced. If SHIFT
  6781. is nil or the empty string, time stamps will be left alone. The
  6782. ID property of the original subtree is removed.
  6783. If the original subtree did contain time stamps with a repeater,
  6784. the following will happen:
  6785. - the repeater will be removed in each clone
  6786. - an additional clone will be produced, with the current, unshifted
  6787. date(s) in the entry.
  6788. - the original entry will be placed *after* all the clones, with
  6789. repeater intact.
  6790. - the start days in the repeater in the original entry will be shifted
  6791. to past the last clone.
  6792. I this way you can spell out a number of instances of a repeating task,
  6793. and still retain the repeater to cover future instances of the task."
  6794. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6795. (let (beg end template task idprop
  6796. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6797. (if (not (and (integerp n) (> n 0)))
  6798. (error "Invalid number of replications %s" n))
  6799. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6800. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6801. shift)))
  6802. (error "Invalid shift specification %s" shift))
  6803. (when doshift
  6804. (setq shift-n (string-to-number (match-string 1 shift))
  6805. shift-what (cdr (assoc (match-string 2 shift)
  6806. '(("d" . day) ("w" . week)
  6807. ("m" . month) ("y" . year))))))
  6808. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6809. (setq nmin 1 nmax n)
  6810. (org-back-to-heading t)
  6811. (setq beg (point))
  6812. (setq idprop (org-entry-get nil "ID"))
  6813. (org-end-of-subtree t t)
  6814. (or (bolp) (insert "\n"))
  6815. (setq end (point))
  6816. (setq template (buffer-substring beg end))
  6817. (when (and doshift
  6818. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6819. (delete-region beg end)
  6820. (setq end beg)
  6821. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6822. (goto-char end)
  6823. (loop for n from nmin to nmax do
  6824. ;; prepare clone
  6825. (with-temp-buffer
  6826. (insert template)
  6827. (org-mode)
  6828. (goto-char (point-min))
  6829. (and idprop (if org-clone-delete-id
  6830. (org-entry-delete nil "ID")
  6831. (org-id-get-create t)))
  6832. (while (re-search-forward org-property-start-re nil t)
  6833. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6834. (goto-char (point-min))
  6835. (when doshift
  6836. (while (re-search-forward org-ts-regexp-both nil t)
  6837. (org-timestamp-change (* n shift-n) shift-what))
  6838. (unless (= n n-no-remove)
  6839. (goto-char (point-min))
  6840. (while (re-search-forward org-ts-regexp nil t)
  6841. (save-excursion
  6842. (goto-char (match-beginning 0))
  6843. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6844. (delete-region (match-beginning 1) (match-end 1)))))))
  6845. (setq task (buffer-string)))
  6846. (insert task))
  6847. (goto-char beg)))
  6848. ;;; Outline Sorting
  6849. (defun org-sort (with-case)
  6850. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6851. Optional argument WITH-CASE means sort case-sensitively.
  6852. With a double prefix argument, also remove duplicate entries."
  6853. (interactive "P")
  6854. (cond
  6855. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6856. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6857. (t
  6858. (org-call-with-arg 'org-sort-entries with-case))))
  6859. (defun org-sort-remove-invisible (s)
  6860. (remove-text-properties 0 (length s) org-rm-props s)
  6861. (while (string-match org-bracket-link-regexp s)
  6862. (setq s (replace-match (if (match-end 2)
  6863. (match-string 3 s)
  6864. (match-string 1 s)) t t s)))
  6865. s)
  6866. (defvar org-priority-regexp) ; defined later in the file
  6867. (defvar org-after-sorting-entries-or-items-hook nil
  6868. "Hook that is run after a bunch of entries or items have been sorted.
  6869. When children are sorted, the cursor is in the parent line when this
  6870. hook gets called. When a region or a plain list is sorted, the cursor
  6871. will be in the first entry of the sorted region/list.")
  6872. (defun org-sort-entries
  6873. (&optional with-case sorting-type getkey-func compare-func property)
  6874. "Sort entries on a certain level of an outline tree.
  6875. If there is an active region, the entries in the region are sorted.
  6876. Else, if the cursor is before the first entry, sort the top-level items.
  6877. Else, the children of the entry at point are sorted.
  6878. Sorting can be alphabetically, numerically, by date/time as given by
  6879. a time stamp, by a property or by priority.
  6880. The command prompts for the sorting type unless it has been given to the
  6881. function through the SORTING-TYPE argument, which needs to be a character,
  6882. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6883. precise meaning of each character:
  6884. n Numerically, by converting the beginning of the entry/item to a number.
  6885. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6886. t By date/time, either the first active time stamp in the entry, or, if
  6887. none exist, by the first inactive one.
  6888. s By the scheduled date/time.
  6889. d By deadline date/time.
  6890. c By creation time, which is assumed to be the first inactive time stamp
  6891. at the beginning of a line.
  6892. p By priority according to the cookie.
  6893. r By the value of a property.
  6894. Capital letters will reverse the sort order.
  6895. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6896. called with point at the beginning of the record. It must return either
  6897. a string or a number that should serve as the sorting key for that record.
  6898. Comparing entries ignores case by default. However, with an optional argument
  6899. WITH-CASE, the sorting considers case as well."
  6900. (interactive "P")
  6901. (let ((case-func (if with-case 'identity 'downcase))
  6902. start beg end stars re re2
  6903. txt what tmp)
  6904. ;; Find beginning and end of region to sort
  6905. (cond
  6906. ((org-region-active-p)
  6907. ;; we will sort the region
  6908. (setq end (region-end)
  6909. what "region")
  6910. (goto-char (region-beginning))
  6911. (if (not (org-on-heading-p)) (outline-next-heading))
  6912. (setq start (point)))
  6913. ((or (org-on-heading-p)
  6914. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6915. ;; we will sort the children of the current headline
  6916. (org-back-to-heading)
  6917. (setq start (point)
  6918. end (progn (org-end-of-subtree t t)
  6919. (or (bolp) (insert "\n"))
  6920. (org-back-over-empty-lines)
  6921. (point))
  6922. what "children")
  6923. (goto-char start)
  6924. (show-subtree)
  6925. (outline-next-heading))
  6926. (t
  6927. ;; we will sort the top-level entries in this file
  6928. (goto-char (point-min))
  6929. (or (org-on-heading-p) (outline-next-heading))
  6930. (setq start (point))
  6931. (goto-char (point-max))
  6932. (beginning-of-line 1)
  6933. (when (looking-at ".*?\\S-")
  6934. ;; File ends in a non-white line
  6935. (end-of-line 1)
  6936. (insert "\n"))
  6937. (setq end (point-max))
  6938. (setq what "top-level")
  6939. (goto-char start)
  6940. (show-all)))
  6941. (setq beg (point))
  6942. (if (>= beg end) (error "Nothing to sort"))
  6943. (looking-at "\\(\\*+\\)")
  6944. (setq stars (match-string 1)
  6945. re (concat "^" (regexp-quote stars) " +")
  6946. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6947. txt (buffer-substring beg end))
  6948. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6949. (if (and (not (equal stars "*")) (string-match re2 txt))
  6950. (error "Region to sort contains a level above the first entry"))
  6951. (unless sorting-type
  6952. (message
  6953. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6954. [t]ime [s]cheduled [d]eadline [c]reated
  6955. A/N/T/S/D/C/P/O/F means reversed:"
  6956. what)
  6957. (setq sorting-type (read-char-exclusive))
  6958. (and (= (downcase sorting-type) ?f)
  6959. (setq getkey-func
  6960. (org-icompleting-read "Sort using function: "
  6961. obarray 'fboundp t nil nil))
  6962. (setq getkey-func (intern getkey-func)))
  6963. (and (= (downcase sorting-type) ?r)
  6964. (setq property
  6965. (org-icompleting-read "Property: "
  6966. (mapcar 'list (org-buffer-property-keys t))
  6967. nil t))))
  6968. (message "Sorting entries...")
  6969. (save-restriction
  6970. (narrow-to-region start end)
  6971. (let ((dcst (downcase sorting-type))
  6972. (case-fold-search nil)
  6973. (now (current-time)))
  6974. (sort-subr
  6975. (/= dcst sorting-type)
  6976. ;; This function moves to the beginning character of the "record" to
  6977. ;; be sorted.
  6978. (lambda nil
  6979. (if (re-search-forward re nil t)
  6980. (goto-char (match-beginning 0))
  6981. (goto-char (point-max))))
  6982. ;; This function moves to the last character of the "record" being
  6983. ;; sorted.
  6984. (lambda nil
  6985. (save-match-data
  6986. (condition-case nil
  6987. (outline-forward-same-level 1)
  6988. (error
  6989. (goto-char (point-max))))))
  6990. ;; This function returns the value that gets sorted against.
  6991. (lambda nil
  6992. (cond
  6993. ((= dcst ?n)
  6994. (if (looking-at org-complex-heading-regexp)
  6995. (string-to-number (match-string 4))
  6996. nil))
  6997. ((= dcst ?a)
  6998. (if (looking-at org-complex-heading-regexp)
  6999. (funcall case-func (match-string 4))
  7000. nil))
  7001. ((= dcst ?t)
  7002. (let ((end (save-excursion (outline-next-heading) (point))))
  7003. (if (or (re-search-forward org-ts-regexp end t)
  7004. (re-search-forward org-ts-regexp-both end t))
  7005. (org-time-string-to-seconds (match-string 0))
  7006. (org-float-time now))))
  7007. ((= dcst ?c)
  7008. (let ((end (save-excursion (outline-next-heading) (point))))
  7009. (if (re-search-forward
  7010. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7011. end t)
  7012. (org-time-string-to-seconds (match-string 0))
  7013. (org-float-time now))))
  7014. ((= dcst ?s)
  7015. (let ((end (save-excursion (outline-next-heading) (point))))
  7016. (if (re-search-forward org-scheduled-time-regexp end t)
  7017. (org-time-string-to-seconds (match-string 1))
  7018. (org-float-time now))))
  7019. ((= dcst ?d)
  7020. (let ((end (save-excursion (outline-next-heading) (point))))
  7021. (if (re-search-forward org-deadline-time-regexp end t)
  7022. (org-time-string-to-seconds (match-string 1))
  7023. (org-float-time now))))
  7024. ((= dcst ?p)
  7025. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7026. (string-to-char (match-string 2))
  7027. org-default-priority))
  7028. ((= dcst ?r)
  7029. (or (org-entry-get nil property) ""))
  7030. ((= dcst ?o)
  7031. (if (looking-at org-complex-heading-regexp)
  7032. (- 9999 (length (member (match-string 2)
  7033. org-todo-keywords-1)))))
  7034. ((= dcst ?f)
  7035. (if getkey-func
  7036. (progn
  7037. (setq tmp (funcall getkey-func))
  7038. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7039. tmp)
  7040. (error "Invalid key function `%s'" getkey-func)))
  7041. (t (error "Invalid sorting type `%c'" sorting-type))))
  7042. nil
  7043. (cond
  7044. ((= dcst ?a) 'string<)
  7045. ((= dcst ?f) compare-func)
  7046. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7047. (t nil)))))
  7048. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7049. (message "Sorting entries...done")))
  7050. (defun org-do-sort (table what &optional with-case sorting-type)
  7051. "Sort TABLE of WHAT according to SORTING-TYPE.
  7052. The user will be prompted for the SORTING-TYPE if the call to this
  7053. function does not specify it. WHAT is only for the prompt, to indicate
  7054. what is being sorted. The sorting key will be extracted from
  7055. the car of the elements of the table.
  7056. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7057. (unless sorting-type
  7058. (message
  7059. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7060. what)
  7061. (setq sorting-type (read-char-exclusive)))
  7062. (let ((dcst (downcase sorting-type))
  7063. extractfun comparefun)
  7064. ;; Define the appropriate functions
  7065. (cond
  7066. ((= dcst ?n)
  7067. (setq extractfun 'string-to-number
  7068. comparefun (if (= dcst sorting-type) '< '>)))
  7069. ((= dcst ?a)
  7070. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7071. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7072. comparefun (if (= dcst sorting-type)
  7073. 'string<
  7074. (lambda (a b) (and (not (string< a b))
  7075. (not (string= a b)))))))
  7076. ((= dcst ?t)
  7077. (setq extractfun
  7078. (lambda (x)
  7079. (if (or (string-match org-ts-regexp x)
  7080. (string-match org-ts-regexp-both x))
  7081. (org-float-time
  7082. (org-time-string-to-time (match-string 0 x)))
  7083. 0))
  7084. comparefun (if (= dcst sorting-type) '< '>)))
  7085. (t (error "Invalid sorting type `%c'" sorting-type)))
  7086. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7087. table)
  7088. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7089. ;;; The orgstruct minor mode
  7090. ;; Define a minor mode which can be used in other modes in order to
  7091. ;; integrate the org-mode structure editing commands.
  7092. ;; This is really a hack, because the org-mode structure commands use
  7093. ;; keys which normally belong to the major mode. Here is how it
  7094. ;; works: The minor mode defines all the keys necessary to operate the
  7095. ;; structure commands, but wraps the commands into a function which
  7096. ;; tests if the cursor is currently at a headline or a plain list
  7097. ;; item. If that is the case, the structure command is used,
  7098. ;; temporarily setting many Org-mode variables like regular
  7099. ;; expressions for filling etc. However, when any of those keys is
  7100. ;; used at a different location, function uses `key-binding' to look
  7101. ;; up if the key has an associated command in another currently active
  7102. ;; keymap (minor modes, major mode, global), and executes that
  7103. ;; command. There might be problems if any of the keys is otherwise
  7104. ;; used as a prefix key.
  7105. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7106. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7107. ;; addresses this by checking explicitly for both bindings.
  7108. (defvar orgstruct-mode-map (make-sparse-keymap)
  7109. "Keymap for the minor `orgstruct-mode'.")
  7110. (defvar org-local-vars nil
  7111. "List of local variables, for use by `orgstruct-mode'.")
  7112. ;;;###autoload
  7113. (define-minor-mode orgstruct-mode
  7114. "Toggle the minor mode `orgstruct-mode'.
  7115. This mode is for using Org-mode structure commands in other
  7116. modes. The following keys behave as if Org-mode were active, if
  7117. the cursor is on a headline, or on a plain list item (both as
  7118. defined by Org-mode).
  7119. M-up Move entry/item up
  7120. M-down Move entry/item down
  7121. M-left Promote
  7122. M-right Demote
  7123. M-S-up Move entry/item up
  7124. M-S-down Move entry/item down
  7125. M-S-left Promote subtree
  7126. M-S-right Demote subtree
  7127. M-q Fill paragraph and items like in Org-mode
  7128. C-c ^ Sort entries
  7129. C-c - Cycle list bullet
  7130. TAB Cycle item visibility
  7131. M-RET Insert new heading/item
  7132. S-M-RET Insert new TODO heading / Checkbox item
  7133. C-c C-c Set tags / toggle checkbox"
  7134. nil " OrgStruct" nil
  7135. (org-load-modules-maybe)
  7136. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7137. ;;;###autoload
  7138. (defun turn-on-orgstruct ()
  7139. "Unconditionally turn on `orgstruct-mode'."
  7140. (orgstruct-mode 1))
  7141. (defun orgstruct++-mode (&optional arg)
  7142. "Toggle `orgstruct-mode', the enhanced version of it.
  7143. In addition to setting orgstruct-mode, this also exports all indentation
  7144. and autofilling variables from org-mode into the buffer. It will also
  7145. recognize item context in multiline items.
  7146. Note that turning off orgstruct-mode will *not* remove the
  7147. indentation/paragraph settings. This can only be done by refreshing the
  7148. major mode, for example with \\[normal-mode]."
  7149. (interactive "P")
  7150. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7151. (if (< arg 1)
  7152. (orgstruct-mode -1)
  7153. (orgstruct-mode 1)
  7154. (let (var val)
  7155. (mapc
  7156. (lambda (x)
  7157. (when (string-match
  7158. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7159. (symbol-name (car x)))
  7160. (setq var (car x) val (nth 1 x))
  7161. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7162. org-local-vars)
  7163. (org-set-local 'orgstruct-is-++ t))))
  7164. (defvar orgstruct-is-++ nil
  7165. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7166. (make-variable-buffer-local 'orgstruct-is-++)
  7167. ;;;###autoload
  7168. (defun turn-on-orgstruct++ ()
  7169. "Unconditionally turn on `orgstruct++-mode'."
  7170. (orgstruct++-mode 1))
  7171. (defun orgstruct-error ()
  7172. "Error when there is no default binding for a structure key."
  7173. (interactive)
  7174. (error "This key has no function outside structure elements"))
  7175. (defun orgstruct-setup ()
  7176. "Setup orgstruct keymaps."
  7177. (let ((nfunc 0)
  7178. (bindings
  7179. (list
  7180. '([(meta up)] org-metaup)
  7181. '([(meta down)] org-metadown)
  7182. '([(meta left)] org-metaleft)
  7183. '([(meta right)] org-metaright)
  7184. '([(meta shift up)] org-shiftmetaup)
  7185. '([(meta shift down)] org-shiftmetadown)
  7186. '([(meta shift left)] org-shiftmetaleft)
  7187. '([(meta shift right)] org-shiftmetaright)
  7188. '([?\e (up)] org-metaup)
  7189. '([?\e (down)] org-metadown)
  7190. '([?\e (left)] org-metaleft)
  7191. '([?\e (right)] org-metaright)
  7192. '([?\e (shift up)] org-shiftmetaup)
  7193. '([?\e (shift down)] org-shiftmetadown)
  7194. '([?\e (shift left)] org-shiftmetaleft)
  7195. '([?\e (shift right)] org-shiftmetaright)
  7196. '([(shift up)] org-shiftup)
  7197. '([(shift down)] org-shiftdown)
  7198. '([(shift left)] org-shiftleft)
  7199. '([(shift right)] org-shiftright)
  7200. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7201. '("\M-q" fill-paragraph)
  7202. '("\C-c^" org-sort)
  7203. '("\C-c-" org-cycle-list-bullet)))
  7204. elt key fun cmd)
  7205. (while (setq elt (pop bindings))
  7206. (setq nfunc (1+ nfunc))
  7207. (setq key (org-key (car elt))
  7208. fun (nth 1 elt)
  7209. cmd (orgstruct-make-binding fun nfunc key))
  7210. (org-defkey orgstruct-mode-map key cmd))
  7211. ;; Special treatment needed for TAB and RET
  7212. (org-defkey orgstruct-mode-map [(tab)]
  7213. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7214. (org-defkey orgstruct-mode-map "\C-i"
  7215. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7216. (org-defkey orgstruct-mode-map "\M-\C-m"
  7217. (orgstruct-make-binding 'org-insert-heading 105
  7218. "\M-\C-m" [(meta return)]))
  7219. (org-defkey orgstruct-mode-map [(meta return)]
  7220. (orgstruct-make-binding 'org-insert-heading 106
  7221. [(meta return)] "\M-\C-m"))
  7222. (org-defkey orgstruct-mode-map [(shift meta return)]
  7223. (orgstruct-make-binding 'org-insert-todo-heading 107
  7224. [(meta return)] "\M-\C-m"))
  7225. (org-defkey orgstruct-mode-map "\e\C-m"
  7226. (orgstruct-make-binding 'org-insert-heading 108
  7227. "\e\C-m" [?\e (return)]))
  7228. (org-defkey orgstruct-mode-map [?\e (return)]
  7229. (orgstruct-make-binding 'org-insert-heading 109
  7230. [?\e (return)] "\e\C-m"))
  7231. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7232. (orgstruct-make-binding 'org-insert-todo-heading 110
  7233. [?\e (return)] "\e\C-m"))
  7234. (unless org-local-vars
  7235. (setq org-local-vars (org-get-local-variables)))
  7236. t))
  7237. (defun orgstruct-make-binding (fun n &rest keys)
  7238. "Create a function for binding in the structure minor mode.
  7239. FUN is the command to call inside a table. N is used to create a unique
  7240. command name. KEYS are keys that should be checked in for a command
  7241. to execute outside of tables."
  7242. (eval
  7243. (list 'defun
  7244. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7245. '(arg)
  7246. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7247. "Outside of structure, run the binding of `"
  7248. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7249. "'.")
  7250. '(interactive "p")
  7251. (list 'if
  7252. `(org-context-p 'headline 'item
  7253. (and orgstruct-is-++
  7254. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7255. 'item-body))
  7256. (list 'org-run-like-in-org-mode (list 'quote fun))
  7257. (list 'let '(orgstruct-mode)
  7258. (list 'call-interactively
  7259. (append '(or)
  7260. (mapcar (lambda (k)
  7261. (list 'key-binding k))
  7262. keys)
  7263. '('orgstruct-error))))))))
  7264. (defun org-context-p (&rest contexts)
  7265. "Check if local context is any of CONTEXTS.
  7266. Possible values in the list of contexts are `table', `headline', and `item'."
  7267. (let ((pos (point)))
  7268. (goto-char (point-at-bol))
  7269. (prog1 (or (and (memq 'table contexts)
  7270. (looking-at "[ \t]*|"))
  7271. (and (memq 'headline contexts)
  7272. ;;????????? (looking-at "\\*+"))
  7273. (looking-at outline-regexp))
  7274. (and (memq 'item contexts)
  7275. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7276. (and (memq 'item-body contexts)
  7277. (org-in-item-p)))
  7278. (goto-char pos))))
  7279. (defun org-get-local-variables ()
  7280. "Return a list of all local variables in an org-mode buffer."
  7281. (let (varlist)
  7282. (with-current-buffer (get-buffer-create "*Org tmp*")
  7283. (erase-buffer)
  7284. (org-mode)
  7285. (setq varlist (buffer-local-variables)))
  7286. (kill-buffer "*Org tmp*")
  7287. (delq nil
  7288. (mapcar
  7289. (lambda (x)
  7290. (setq x
  7291. (if (symbolp x)
  7292. (list x)
  7293. (list (car x) (list 'quote (cdr x)))))
  7294. (if (string-match
  7295. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7296. (symbol-name (car x)))
  7297. x nil))
  7298. varlist))))
  7299. ;;;###autoload
  7300. (defun org-run-like-in-org-mode (cmd)
  7301. "Run a command, pretending that the current buffer is in Org-mode.
  7302. This will temporarily bind local variables that are typically bound in
  7303. Org-mode to the values they have in Org-mode, and then interactively
  7304. call CMD."
  7305. (org-load-modules-maybe)
  7306. (unless org-local-vars
  7307. (setq org-local-vars (org-get-local-variables)))
  7308. (eval (list 'let org-local-vars
  7309. (list 'call-interactively (list 'quote cmd)))))
  7310. ;;;; Archiving
  7311. (defun org-get-category (&optional pos)
  7312. "Get the category applying to position POS."
  7313. (get-text-property (or pos (point)) 'org-category))
  7314. (defun org-refresh-category-properties ()
  7315. "Refresh category text properties in the buffer."
  7316. (let ((def-cat (cond
  7317. ((null org-category)
  7318. (if buffer-file-name
  7319. (file-name-sans-extension
  7320. (file-name-nondirectory buffer-file-name))
  7321. "???"))
  7322. ((symbolp org-category) (symbol-name org-category))
  7323. (t org-category)))
  7324. beg end cat pos optionp)
  7325. (org-unmodified
  7326. (save-excursion
  7327. (save-restriction
  7328. (widen)
  7329. (goto-char (point-min))
  7330. (put-text-property (point) (point-max) 'org-category def-cat)
  7331. (while (re-search-forward
  7332. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7333. (setq pos (match-end 0)
  7334. optionp (equal (char-after (match-beginning 0)) ?#)
  7335. cat (org-trim (match-string 2)))
  7336. (if optionp
  7337. (setq beg (point-at-bol) end (point-max))
  7338. (org-back-to-heading t)
  7339. (setq beg (point) end (org-end-of-subtree t t)))
  7340. (put-text-property beg end 'org-category cat)
  7341. (goto-char pos)))))))
  7342. ;;;; Link Stuff
  7343. ;;; Link abbreviations
  7344. (defun org-link-expand-abbrev (link)
  7345. "Apply replacements as defined in `org-link-abbrev-alist."
  7346. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7347. (let* ((key (match-string 1 link))
  7348. (as (or (assoc key org-link-abbrev-alist-local)
  7349. (assoc key org-link-abbrev-alist)))
  7350. (tag (and (match-end 2) (match-string 3 link)))
  7351. rpl)
  7352. (if (not as)
  7353. link
  7354. (setq rpl (cdr as))
  7355. (cond
  7356. ((symbolp rpl) (funcall rpl tag))
  7357. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7358. ((string-match "%h" rpl)
  7359. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7360. (t (concat rpl tag)))))
  7361. link))
  7362. ;;; Storing and inserting links
  7363. (defvar org-insert-link-history nil
  7364. "Minibuffer history for links inserted with `org-insert-link'.")
  7365. (defvar org-stored-links nil
  7366. "Contains the links stored with `org-store-link'.")
  7367. (defvar org-store-link-plist nil
  7368. "Plist with info about the most recently link created with `org-store-link'.")
  7369. (defvar org-link-protocols nil
  7370. "Link protocols added to Org-mode using `org-add-link-type'.")
  7371. (defvar org-store-link-functions nil
  7372. "List of functions that are called to create and store a link.
  7373. Each function will be called in turn until one returns a non-nil
  7374. value. Each function should check if it is responsible for creating
  7375. this link (for example by looking at the major mode).
  7376. If not, it must exit and return nil.
  7377. If yes, it should return a non-nil value after a calling
  7378. `org-store-link-props' with a list of properties and values.
  7379. Special properties are:
  7380. :type The link prefix, like \"http\". This must be given.
  7381. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7382. This is obligatory as well.
  7383. :description Optional default description for the second pair
  7384. of brackets in an Org-mode link. The user can still change
  7385. this when inserting this link into an Org-mode buffer.
  7386. In addition to these, any additional properties can be specified
  7387. and then used in remember templates.")
  7388. (defun org-add-link-type (type &optional follow export)
  7389. "Add TYPE to the list of `org-link-types'.
  7390. Re-compute all regular expressions depending on `org-link-types'
  7391. FOLLOW and EXPORT are two functions.
  7392. FOLLOW should take the link path as the single argument and do whatever
  7393. is necessary to follow the link, for example find a file or display
  7394. a mail message.
  7395. EXPORT should format the link path for export to one of the export formats.
  7396. It should be a function accepting three arguments:
  7397. path the path of the link, the text after the prefix (like \"http:\")
  7398. desc the description of the link, if any, nil if there was no description
  7399. format the export format, a symbol like `html' or `latex' or `ascii'..
  7400. The function may use the FORMAT information to return different values
  7401. depending on the format. The return value will be put literally into
  7402. the exported file. If the return value is nil, this means Org should
  7403. do what it normally does with links which do not have EXPORT defined.
  7404. Org-mode has a built-in default for exporting links. If you are happy with
  7405. this default, there is no need to define an export function for the link
  7406. type. For a simple example of an export function, see `org-bbdb.el'."
  7407. (add-to-list 'org-link-types type t)
  7408. (org-make-link-regexps)
  7409. (if (assoc type org-link-protocols)
  7410. (setcdr (assoc type org-link-protocols) (list follow export))
  7411. (push (list type follow export) org-link-protocols)))
  7412. (defvar org-agenda-buffer-name)
  7413. ;;;###autoload
  7414. (defun org-store-link (arg)
  7415. "\\<org-mode-map>Store an org-link to the current location.
  7416. This link is added to `org-stored-links' and can later be inserted
  7417. into an org-buffer with \\[org-insert-link].
  7418. For some link types, a prefix arg is interpreted:
  7419. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7420. For file links, arg negates `org-context-in-file-links'."
  7421. (interactive "P")
  7422. (org-load-modules-maybe)
  7423. (setq org-store-link-plist nil) ; reset
  7424. (let ((outline-regexp (org-get-limited-outline-regexp))
  7425. link cpltxt desc description search txt custom-id agenda-link)
  7426. (cond
  7427. ((run-hook-with-args-until-success 'org-store-link-functions)
  7428. (setq link (plist-get org-store-link-plist :link)
  7429. desc (or (plist-get org-store-link-plist :description) link)))
  7430. ((equal (buffer-name) "*Org Edit Src Example*")
  7431. (let (label gc)
  7432. (while (or (not label)
  7433. (save-excursion
  7434. (save-restriction
  7435. (widen)
  7436. (goto-char (point-min))
  7437. (re-search-forward
  7438. (regexp-quote (format org-coderef-label-format label))
  7439. nil t))))
  7440. (when label (message "Label exists already") (sit-for 2))
  7441. (setq label (read-string "Code line label: " label)))
  7442. (end-of-line 1)
  7443. (setq link (format org-coderef-label-format label))
  7444. (setq gc (- 79 (length link)))
  7445. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7446. (insert link)
  7447. (setq link (concat "(" label ")") desc nil)))
  7448. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7449. ;; We are in the agenda, link to referenced location
  7450. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7451. (get-text-property (point) 'org-marker))))
  7452. (when m
  7453. (org-with-point-at m
  7454. (setq agenda-link
  7455. (if (interactive-p)
  7456. (call-interactively 'org-store-link)
  7457. (org-store-link nil)))))))
  7458. ((eq major-mode 'calendar-mode)
  7459. (let ((cd (calendar-cursor-to-date)))
  7460. (setq link
  7461. (format-time-string
  7462. (car org-time-stamp-formats)
  7463. (apply 'encode-time
  7464. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7465. nil nil nil))))
  7466. (org-store-link-props :type "calendar" :date cd)))
  7467. ((eq major-mode 'w3-mode)
  7468. (setq cpltxt (if (and (buffer-name)
  7469. (not (string-match "Untitled" (buffer-name))))
  7470. (buffer-name)
  7471. (url-view-url t))
  7472. link (org-make-link (url-view-url t)))
  7473. (org-store-link-props :type "w3" :url (url-view-url t)))
  7474. ((eq major-mode 'w3m-mode)
  7475. (setq cpltxt (or w3m-current-title w3m-current-url)
  7476. link (org-make-link w3m-current-url))
  7477. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7478. ((setq search (run-hook-with-args-until-success
  7479. 'org-create-file-search-functions))
  7480. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7481. "::" search))
  7482. (setq cpltxt (or description link)))
  7483. ((eq major-mode 'image-mode)
  7484. (setq cpltxt (concat "file:"
  7485. (abbreviate-file-name buffer-file-name))
  7486. link (org-make-link cpltxt))
  7487. (org-store-link-props :type "image" :file buffer-file-name))
  7488. ((eq major-mode 'dired-mode)
  7489. ;; link to the file in the current line
  7490. (let ((file (dired-get-filename nil t)))
  7491. (setq file (if file
  7492. (abbreviate-file-name
  7493. (expand-file-name (dired-get-filename nil t)))
  7494. ;; otherwise, no file so use current directory.
  7495. default-directory))
  7496. (setq cpltxt (concat "file:" file)
  7497. link (org-make-link cpltxt))))
  7498. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7499. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7500. (cond
  7501. ((org-in-regexp "<<\\(.*?\\)>>")
  7502. (setq cpltxt
  7503. (concat "file:"
  7504. (abbreviate-file-name
  7505. (buffer-file-name (buffer-base-buffer)))
  7506. "::" (match-string 1))
  7507. link (org-make-link cpltxt)))
  7508. ((and (featurep 'org-id)
  7509. (or (eq org-link-to-org-use-id t)
  7510. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7511. (interactive-p))
  7512. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7513. (interactive-p)
  7514. (not custom-id))
  7515. (and org-link-to-org-use-id
  7516. (condition-case nil
  7517. (org-entry-get nil "ID")
  7518. (error nil)))))
  7519. ;; We can make a link using the ID.
  7520. (setq link (condition-case nil
  7521. (prog1 (org-id-store-link)
  7522. (setq desc (plist-get org-store-link-plist
  7523. :description)))
  7524. (error
  7525. ;; probably before first headline, link to file only
  7526. (concat "file:"
  7527. (abbreviate-file-name
  7528. (buffer-file-name (buffer-base-buffer))))))))
  7529. (t
  7530. ;; Just link to current headline
  7531. (setq cpltxt (concat "file:"
  7532. (abbreviate-file-name
  7533. (buffer-file-name (buffer-base-buffer)))))
  7534. ;; Add a context search string
  7535. (when (org-xor org-context-in-file-links arg)
  7536. (setq txt (cond
  7537. ((org-on-heading-p) nil)
  7538. ((org-region-active-p)
  7539. (buffer-substring (region-beginning) (region-end)))
  7540. (t nil)))
  7541. (when (or (null txt) (string-match "\\S-" txt))
  7542. (setq cpltxt
  7543. (concat cpltxt "::"
  7544. (condition-case nil
  7545. (org-make-org-heading-search-string txt)
  7546. (error "")))
  7547. desc (or (nth 4 (ignore-errors
  7548. (org-heading-components))) "NONE"))))
  7549. (if (string-match "::\\'" cpltxt)
  7550. (setq cpltxt (substring cpltxt 0 -2)))
  7551. (setq link (org-make-link cpltxt)))))
  7552. ((buffer-file-name (buffer-base-buffer))
  7553. ;; Just link to this file here.
  7554. (setq cpltxt (concat "file:"
  7555. (abbreviate-file-name
  7556. (buffer-file-name (buffer-base-buffer)))))
  7557. ;; Add a context string
  7558. (when (org-xor org-context-in-file-links arg)
  7559. (setq txt (if (org-region-active-p)
  7560. (buffer-substring (region-beginning) (region-end))
  7561. (buffer-substring (point-at-bol) (point-at-eol))))
  7562. ;; Only use search option if there is some text.
  7563. (when (string-match "\\S-" txt)
  7564. (setq cpltxt
  7565. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7566. desc "NONE")))
  7567. (setq link (org-make-link cpltxt)))
  7568. ((interactive-p)
  7569. (error "Cannot link to a buffer which is not visiting a file"))
  7570. (t (setq link nil)))
  7571. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7572. (setq link (or link cpltxt)
  7573. desc (or desc cpltxt))
  7574. (if (equal desc "NONE") (setq desc nil))
  7575. (if (and (or (interactive-p) executing-kbd-macro) link)
  7576. (progn
  7577. (setq org-stored-links
  7578. (cons (list link desc) org-stored-links))
  7579. (message "Stored: %s" (or desc link))
  7580. (when custom-id
  7581. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7582. "::#" custom-id))
  7583. (setq org-stored-links
  7584. (cons (list link desc) org-stored-links))))
  7585. (or agenda-link (and link (org-make-link-string link desc))))))
  7586. (defun org-store-link-props (&rest plist)
  7587. "Store link properties, extract names and addresses."
  7588. (let (x adr)
  7589. (when (setq x (plist-get plist :from))
  7590. (setq adr (mail-extract-address-components x))
  7591. (setq plist (plist-put plist :fromname (car adr)))
  7592. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7593. (when (setq x (plist-get plist :to))
  7594. (setq adr (mail-extract-address-components x))
  7595. (setq plist (plist-put plist :toname (car adr)))
  7596. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7597. (let ((from (plist-get plist :from))
  7598. (to (plist-get plist :to)))
  7599. (when (and from to org-from-is-user-regexp)
  7600. (setq plist
  7601. (plist-put plist :fromto
  7602. (if (string-match org-from-is-user-regexp from)
  7603. (concat "to %t")
  7604. (concat "from %f"))))))
  7605. (setq org-store-link-plist plist))
  7606. (defun org-add-link-props (&rest plist)
  7607. "Add these properties to the link property list."
  7608. (let (key value)
  7609. (while plist
  7610. (setq key (pop plist) value (pop plist))
  7611. (setq org-store-link-plist
  7612. (plist-put org-store-link-plist key value)))))
  7613. (defun org-email-link-description (&optional fmt)
  7614. "Return the description part of an email link.
  7615. This takes information from `org-store-link-plist' and formats it
  7616. according to FMT (default from `org-email-link-description-format')."
  7617. (setq fmt (or fmt org-email-link-description-format))
  7618. (let* ((p org-store-link-plist)
  7619. (to (plist-get p :toaddress))
  7620. (from (plist-get p :fromaddress))
  7621. (table
  7622. (list
  7623. (cons "%c" (plist-get p :fromto))
  7624. (cons "%F" (plist-get p :from))
  7625. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7626. (cons "%T" (plist-get p :to))
  7627. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7628. (cons "%s" (plist-get p :subject))
  7629. (cons "%m" (plist-get p :message-id)))))
  7630. (when (string-match "%c" fmt)
  7631. ;; Check if the user wrote this message
  7632. (if (and org-from-is-user-regexp from to
  7633. (save-match-data (string-match org-from-is-user-regexp from)))
  7634. (setq fmt (replace-match "to %t" t t fmt))
  7635. (setq fmt (replace-match "from %f" t t fmt))))
  7636. (org-replace-escapes fmt table)))
  7637. (defun org-make-org-heading-search-string (&optional string heading)
  7638. "Make search string for STRING or current headline."
  7639. (interactive)
  7640. (let ((s (or string (org-get-heading)))
  7641. (lines org-context-in-file-links))
  7642. (unless (and string (not heading))
  7643. ;; We are using a headline, clean up garbage in there.
  7644. (if (string-match org-todo-regexp s)
  7645. (setq s (replace-match "" t t s)))
  7646. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7647. (setq s (replace-match "" t t s)))
  7648. (setq s (org-trim s))
  7649. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7650. org-comment-string "\\)") s)
  7651. (setq s (replace-match "" t t s)))
  7652. (while (string-match org-ts-regexp s)
  7653. (setq s (replace-match "" t t s))))
  7654. (or string (setq s (concat "*" s))) ; Add * for headlines
  7655. (when (and string (integerp lines) (> lines 0))
  7656. (let ((slines (org-split-string s "\n")))
  7657. (when (< lines (length slines))
  7658. (setq s (mapconcat
  7659. 'identity
  7660. (reverse (nthcdr (- (length slines) lines)
  7661. (reverse slines))) "\n")))))
  7662. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7663. (defun org-make-link (&rest strings)
  7664. "Concatenate STRINGS."
  7665. (apply 'concat strings))
  7666. (defun org-make-link-string (link &optional description)
  7667. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7668. (unless (string-match "\\S-" link)
  7669. (error "Empty link"))
  7670. (when (and description
  7671. (stringp description)
  7672. (not (string-match "\\S-" description)))
  7673. (setq description nil))
  7674. (when (stringp description)
  7675. ;; Remove brackets from the description, they are fatal.
  7676. (while (string-match "\\[" description)
  7677. (setq description (replace-match "{" t t description)))
  7678. (while (string-match "\\]" description)
  7679. (setq description (replace-match "}" t t description))))
  7680. (when (equal (org-link-escape link) description)
  7681. ;; No description needed, it is identical
  7682. (setq description nil))
  7683. (when (and (not description)
  7684. (not (equal link (org-link-escape link))))
  7685. (setq description (org-extract-attributes link)))
  7686. (setq link (if (string-match org-link-types-re link)
  7687. (concat (match-string 1 link)
  7688. (org-link-escape (substring link (match-end 1))))
  7689. (org-link-escape link)))
  7690. (concat "[[" link "]"
  7691. (if description (concat "[" description "]") "")
  7692. "]"))
  7693. (defconst org-link-escape-chars
  7694. '((?\ . "%20")
  7695. (?\[ . "%5B")
  7696. (?\] . "%5D")
  7697. (?\340 . "%E0") ; `a
  7698. (?\342 . "%E2") ; ^a
  7699. (?\347 . "%E7") ; ,c
  7700. (?\350 . "%E8") ; `e
  7701. (?\351 . "%E9") ; 'e
  7702. (?\352 . "%EA") ; ^e
  7703. (?\356 . "%EE") ; ^i
  7704. (?\364 . "%F4") ; ^o
  7705. (?\371 . "%F9") ; `u
  7706. (?\373 . "%FB") ; ^u
  7707. (?\; . "%3B")
  7708. ;; (?? . "%3F")
  7709. (?= . "%3D")
  7710. (?+ . "%2B")
  7711. )
  7712. "Association list of escapes for some characters problematic in links.
  7713. This is the list that is used for internal purposes.")
  7714. (defvar org-url-encoding-use-url-hexify nil)
  7715. (defconst org-link-escape-chars-browser
  7716. '((?\ . "%20")) ; 32 for the SPC char
  7717. "Association list of escapes for some characters problematic in links.
  7718. This is the list that is used before handing over to the browser.")
  7719. (defun org-link-escape (text &optional table)
  7720. "Escape characters in TEXT that are problematic for links."
  7721. (if (and org-url-encoding-use-url-hexify (not table))
  7722. (url-hexify-string text)
  7723. (setq table (or table org-link-escape-chars))
  7724. (when text
  7725. (let ((re (mapconcat (lambda (x) (regexp-quote
  7726. (char-to-string (car x))))
  7727. table "\\|")))
  7728. (while (string-match re text)
  7729. (setq text
  7730. (replace-match
  7731. (cdr (assoc (string-to-char (match-string 0 text))
  7732. table))
  7733. t t text)))
  7734. text))))
  7735. (defun org-link-unescape (text &optional table)
  7736. "Reverse the action of `org-link-escape'."
  7737. (if (and org-url-encoding-use-url-hexify (not table))
  7738. (url-unhex-string text)
  7739. (setq table (or table org-link-escape-chars))
  7740. (when text
  7741. (let ((case-fold-search t)
  7742. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7743. table "\\|")))
  7744. (while (string-match re text)
  7745. (setq text
  7746. (replace-match
  7747. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7748. table)))
  7749. t t text)))
  7750. text))))
  7751. (defun org-xor (a b)
  7752. "Exclusive or."
  7753. (if a (not b) b))
  7754. (defun org-fixup-message-id-for-http (s)
  7755. "Replace special characters in a message id, so it can be used in an http query."
  7756. (when (string-match "%" s)
  7757. (setq s (mapconcat (lambda (c)
  7758. (if (eq c ?%)
  7759. "%25"
  7760. (char-to-string c)))
  7761. s "")))
  7762. (while (string-match "<" s)
  7763. (setq s (replace-match "%3C" t t s)))
  7764. (while (string-match ">" s)
  7765. (setq s (replace-match "%3E" t t s)))
  7766. (while (string-match "@" s)
  7767. (setq s (replace-match "%40" t t s)))
  7768. s)
  7769. ;;;###autoload
  7770. (defun org-insert-link-global ()
  7771. "Insert a link like Org-mode does.
  7772. This command can be called in any mode to insert a link in Org-mode syntax."
  7773. (interactive)
  7774. (org-load-modules-maybe)
  7775. (org-run-like-in-org-mode 'org-insert-link))
  7776. (defun org-insert-link (&optional complete-file link-location)
  7777. "Insert a link. At the prompt, enter the link.
  7778. Completion can be used to insert any of the link protocol prefixes like
  7779. http or ftp in use.
  7780. The history can be used to select a link previously stored with
  7781. `org-store-link'. When the empty string is entered (i.e. if you just
  7782. press RET at the prompt), the link defaults to the most recently
  7783. stored link. As SPC triggers completion in the minibuffer, you need to
  7784. use M-SPC or C-q SPC to force the insertion of a space character.
  7785. You will also be prompted for a description, and if one is given, it will
  7786. be displayed in the buffer instead of the link.
  7787. If there is already a link at point, this command will allow you to edit link
  7788. and description parts.
  7789. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7790. be selected using completion. The path to the file will be relative to the
  7791. current directory if the file is in the current directory or a subdirectory.
  7792. Otherwise, the link will be the absolute path as completed in the minibuffer
  7793. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7794. option `org-link-file-path-type'.
  7795. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7796. the current directory or below.
  7797. With three \\[universal-argument] prefixes, negate the meaning of
  7798. `org-keep-stored-link-after-insertion'.
  7799. If `org-make-link-description-function' is non-nil, this function will be
  7800. called with the link target, and the result will be the default
  7801. link description.
  7802. If the LINK-LOCATION parameter is non-nil, this value will be
  7803. used as the link location instead of reading one interactively."
  7804. (interactive "P")
  7805. (let* ((wcf (current-window-configuration))
  7806. (region (if (org-region-active-p)
  7807. (buffer-substring (region-beginning) (region-end))))
  7808. (remove (and region (list (region-beginning) (region-end))))
  7809. (desc region)
  7810. tmphist ; byte-compile incorrectly complains about this
  7811. (link link-location)
  7812. entry file all-prefixes)
  7813. (cond
  7814. (link-location) ; specified by arg, just use it.
  7815. ((org-in-regexp org-bracket-link-regexp 1)
  7816. ;; We do have a link at point, and we are going to edit it.
  7817. (setq remove (list (match-beginning 0) (match-end 0)))
  7818. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7819. (setq link (read-string "Link: "
  7820. (org-link-unescape
  7821. (org-match-string-no-properties 1)))))
  7822. ((or (org-in-regexp org-angle-link-re)
  7823. (org-in-regexp org-plain-link-re))
  7824. ;; Convert to bracket link
  7825. (setq remove (list (match-beginning 0) (match-end 0))
  7826. link (read-string "Link: "
  7827. (org-remove-angle-brackets (match-string 0)))))
  7828. ((member complete-file '((4) (16)))
  7829. ;; Completing read for file names.
  7830. (setq link (org-file-complete-link complete-file)))
  7831. (t
  7832. ;; Read link, with completion for stored links.
  7833. (with-output-to-temp-buffer "*Org Links*"
  7834. (princ "Insert a link.
  7835. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7836. (when org-stored-links
  7837. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7838. (princ (mapconcat
  7839. (lambda (x)
  7840. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7841. (reverse org-stored-links) "\n"))))
  7842. (let ((cw (selected-window)))
  7843. (select-window (get-buffer-window "*Org Links*" 'visible))
  7844. (setq truncate-lines t)
  7845. (unless (pos-visible-in-window-p (point-max))
  7846. (org-fit-window-to-buffer))
  7847. (and (window-live-p cw) (select-window cw)))
  7848. ;; Fake a link history, containing the stored links.
  7849. (setq tmphist (append (mapcar 'car org-stored-links)
  7850. org-insert-link-history))
  7851. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7852. (mapcar 'car org-link-abbrev-alist)
  7853. org-link-types))
  7854. (unwind-protect
  7855. (progn
  7856. (setq link
  7857. (let ((org-completion-use-ido nil)
  7858. (org-completion-use-iswitchb nil))
  7859. (org-completing-read
  7860. "Link: "
  7861. (append
  7862. (mapcar (lambda (x) (list (concat x ":")))
  7863. all-prefixes)
  7864. (mapcar 'car org-stored-links))
  7865. nil nil nil
  7866. 'tmphist
  7867. (car (car org-stored-links)))))
  7868. (if (not (string-match "\\S-" link))
  7869. (error "No link selected"))
  7870. (if (or (member link all-prefixes)
  7871. (and (equal ":" (substring link -1))
  7872. (member (substring link 0 -1) all-prefixes)
  7873. (setq link (substring link 0 -1))))
  7874. (setq link (org-link-try-special-completion link))))
  7875. (set-window-configuration wcf)
  7876. (kill-buffer "*Org Links*"))
  7877. (setq entry (assoc link org-stored-links))
  7878. (or entry (push link org-insert-link-history))
  7879. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7880. (not org-keep-stored-link-after-insertion))
  7881. (setq org-stored-links (delq (assoc link org-stored-links)
  7882. org-stored-links)))
  7883. (setq desc (or desc (nth 1 entry)))))
  7884. (if (string-match org-plain-link-re link)
  7885. ;; URL-like link, normalize the use of angular brackets.
  7886. (setq link (org-make-link (org-remove-angle-brackets link))))
  7887. ;; Check if we are linking to the current file with a search option
  7888. ;; If yes, simplify the link by using only the search option.
  7889. (when (and buffer-file-name
  7890. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7891. (let* ((path (match-string 1 link))
  7892. (case-fold-search nil)
  7893. (search (match-string 2 link)))
  7894. (save-match-data
  7895. (if (equal (file-truename buffer-file-name) (file-truename path))
  7896. ;; We are linking to this same file, with a search option
  7897. (setq link search)))))
  7898. ;; Check if we can/should use a relative path. If yes, simplify the link
  7899. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7900. (let* ((type (match-string 1 link))
  7901. (path (match-string 2 link))
  7902. (origpath path)
  7903. (case-fold-search nil))
  7904. (cond
  7905. ((or (eq org-link-file-path-type 'absolute)
  7906. (equal complete-file '(16)))
  7907. (setq path (abbreviate-file-name (expand-file-name path))))
  7908. ((eq org-link-file-path-type 'noabbrev)
  7909. (setq path (expand-file-name path)))
  7910. ((eq org-link-file-path-type 'relative)
  7911. (setq path (file-relative-name path)))
  7912. (t
  7913. (save-match-data
  7914. (if (string-match (concat "^" (regexp-quote
  7915. (expand-file-name
  7916. (file-name-as-directory
  7917. default-directory))))
  7918. (expand-file-name path))
  7919. ;; We are linking a file with relative path name.
  7920. (setq path (substring (expand-file-name path)
  7921. (match-end 0)))
  7922. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7923. (setq link (concat type path))
  7924. (if (equal desc origpath)
  7925. (setq desc path))))
  7926. (if org-make-link-description-function
  7927. (setq desc (funcall org-make-link-description-function link desc)))
  7928. (setq desc (read-string "Description: " desc))
  7929. (unless (string-match "\\S-" desc) (setq desc nil))
  7930. (if remove (apply 'delete-region remove))
  7931. (insert (org-make-link-string link desc))))
  7932. (defun org-link-try-special-completion (type)
  7933. "If there is completion support for link type TYPE, offer it."
  7934. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7935. (if (functionp fun)
  7936. (funcall fun)
  7937. (read-string "Link (no completion support): " (concat type ":")))))
  7938. (defun org-file-complete-link (&optional arg)
  7939. "Create a file link using completion."
  7940. (let (file link)
  7941. (setq file (read-file-name "File: "))
  7942. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7943. (pwd1 (file-name-as-directory (abbreviate-file-name
  7944. (expand-file-name ".")))))
  7945. (cond
  7946. ((equal arg '(16))
  7947. (setq link (org-make-link
  7948. "file:"
  7949. (abbreviate-file-name (expand-file-name file)))))
  7950. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7951. (setq link (org-make-link "file:" (match-string 1 file))))
  7952. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7953. (expand-file-name file))
  7954. (setq link (org-make-link
  7955. "file:" (match-string 1 (expand-file-name file)))))
  7956. (t (setq link (org-make-link "file:" file)))))
  7957. link))
  7958. (defun org-completing-read (&rest args)
  7959. "Completing-read with SPACE being a normal character."
  7960. (let ((minibuffer-local-completion-map
  7961. (copy-keymap minibuffer-local-completion-map)))
  7962. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7963. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7964. (apply 'org-icompleting-read args)))
  7965. (defun org-completing-read-no-i (&rest args)
  7966. (let (org-completion-use-ido org-completion-use-iswitchb)
  7967. (apply 'org-completing-read args)))
  7968. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7969. "Use iswitch as a completing-read replacement to choose from choices.
  7970. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7971. from."
  7972. (let* ((iswitchb-use-virtual-buffers nil)
  7973. (iswitchb-make-buflist-hook
  7974. (lambda ()
  7975. (setq iswitchb-temp-buflist choices))))
  7976. (iswitchb-read-buffer prompt)))
  7977. (defun org-icompleting-read (&rest args)
  7978. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7979. (org-without-partial-completion
  7980. (if (and org-completion-use-ido
  7981. (fboundp 'ido-completing-read)
  7982. (boundp 'ido-mode) ido-mode
  7983. (listp (second args)))
  7984. (let ((ido-enter-matching-directory nil))
  7985. (apply 'ido-completing-read (concat (car args))
  7986. (if (consp (car (nth 1 args)))
  7987. (mapcar (lambda (x) (car x)) (nth 1 args))
  7988. (nth 1 args))
  7989. (cddr args)))
  7990. (if (and org-completion-use-iswitchb
  7991. (boundp 'iswitchb-mode) iswitchb-mode
  7992. (listp (second args)))
  7993. (apply 'org-iswitchb-completing-read (concat (car args))
  7994. (if (consp (car (nth 1 args)))
  7995. (mapcar (lambda (x) (car x)) (nth 1 args))
  7996. (nth 1 args))
  7997. (cddr args))
  7998. (apply 'completing-read args)))))
  7999. (defun org-extract-attributes (s)
  8000. "Extract the attributes cookie from a string and set as text property."
  8001. (let (a attr (start 0) key value)
  8002. (save-match-data
  8003. (when (string-match "{{\\([^}]+\\)}}$" s)
  8004. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8005. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8006. (setq key (match-string 1 a) value (match-string 2 a)
  8007. start (match-end 0)
  8008. attr (plist-put attr (intern key) value))))
  8009. (org-add-props s nil 'org-attr attr))
  8010. s))
  8011. (defun org-extract-attributes-from-string (tag)
  8012. (let (key value attr)
  8013. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8014. (setq key (match-string 1 tag) value (match-string 2 tag)
  8015. tag (replace-match "" t t tag)
  8016. attr (plist-put attr (intern key) value)))
  8017. (cons tag attr)))
  8018. (defun org-attributes-to-string (plist)
  8019. "Format a property list into an HTML attribute list."
  8020. (let ((s "") key value)
  8021. (while plist
  8022. (setq key (pop plist) value (pop plist))
  8023. (and value
  8024. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8025. s))
  8026. ;;; Opening/following a link
  8027. (defvar org-link-search-failed nil)
  8028. (defvar org-open-link-functions nil
  8029. "Hook for functions finding a plain text link.
  8030. These functions must take a single argument, the link content.
  8031. They will be called for links that look like [[link text][description]]
  8032. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8033. like a filename (e.g. \"./blue.png\").
  8034. These functions will be called *before* Org attempts to resolve the
  8035. link by doing text searches in the current buffer - so if you want a
  8036. link \"[[target]]\" to still find \"<<target>>\", your function should
  8037. handle this as a special case.
  8038. When the function does handle the link, it must return a non-nil value.
  8039. If it decides that it is not responsible for this link, it must return
  8040. nil to indicate that that Org-mode can continue with other options
  8041. like exact and fuzzy text search.")
  8042. (defun org-next-link ()
  8043. "Move forward to the next link.
  8044. If the link is in hidden text, expose it."
  8045. (interactive)
  8046. (when (and org-link-search-failed (eq this-command last-command))
  8047. (goto-char (point-min))
  8048. (message "Link search wrapped back to beginning of buffer"))
  8049. (setq org-link-search-failed nil)
  8050. (let* ((pos (point))
  8051. (ct (org-context))
  8052. (a (assoc :link ct)))
  8053. (if a (goto-char (nth 2 a)))
  8054. (if (re-search-forward org-any-link-re nil t)
  8055. (progn
  8056. (goto-char (match-beginning 0))
  8057. (if (org-invisible-p) (org-show-context)))
  8058. (goto-char pos)
  8059. (setq org-link-search-failed t)
  8060. (error "No further link found"))))
  8061. (defun org-previous-link ()
  8062. "Move backward to the previous link.
  8063. If the link is in hidden text, expose it."
  8064. (interactive)
  8065. (when (and org-link-search-failed (eq this-command last-command))
  8066. (goto-char (point-max))
  8067. (message "Link search wrapped back to end of buffer"))
  8068. (setq org-link-search-failed nil)
  8069. (let* ((pos (point))
  8070. (ct (org-context))
  8071. (a (assoc :link ct)))
  8072. (if a (goto-char (nth 1 a)))
  8073. (if (re-search-backward org-any-link-re nil t)
  8074. (progn
  8075. (goto-char (match-beginning 0))
  8076. (if (org-invisible-p) (org-show-context)))
  8077. (goto-char pos)
  8078. (setq org-link-search-failed t)
  8079. (error "No further link found"))))
  8080. (defun org-translate-link (s)
  8081. "Translate a link string if a translation function has been defined."
  8082. (if (and org-link-translation-function
  8083. (fboundp org-link-translation-function)
  8084. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8085. (progn
  8086. (setq s (funcall org-link-translation-function
  8087. (match-string 1) (match-string 2)))
  8088. (concat (car s) ":" (cdr s)))
  8089. s))
  8090. (defun org-translate-link-from-planner (type path)
  8091. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8092. This is still an experimental function, your mileage may vary."
  8093. (cond
  8094. ((member type '("http" "https" "news" "ftp"))
  8095. ;; standard Internet links are the same.
  8096. nil)
  8097. ((and (equal type "irc") (string-match "^//" path))
  8098. ;; Planner has two / at the beginning of an irc link, we have 1.
  8099. ;; We should have zero, actually....
  8100. (setq path (substring path 1)))
  8101. ((and (equal type "lisp") (string-match "^/" path))
  8102. ;; Planner has a slash, we do not.
  8103. (setq type "elisp" path (substring path 1)))
  8104. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8105. ;; A typical message link. Planner has the id after the final slash,
  8106. ;; we separate it with a hash mark
  8107. (setq path (concat (match-string 1 path) "#"
  8108. (org-remove-angle-brackets (match-string 2 path)))))
  8109. )
  8110. (cons type path))
  8111. (defun org-find-file-at-mouse (ev)
  8112. "Open file link or URL at mouse."
  8113. (interactive "e")
  8114. (mouse-set-point ev)
  8115. (org-open-at-point 'in-emacs))
  8116. (defun org-open-at-mouse (ev)
  8117. "Open file link or URL at mouse."
  8118. (interactive "e")
  8119. (mouse-set-point ev)
  8120. (if (eq major-mode 'org-agenda-mode)
  8121. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8122. (org-open-at-point))
  8123. (defvar org-window-config-before-follow-link nil
  8124. "The window configuration before following a link.
  8125. This is saved in case the need arises to restore it.")
  8126. (defvar org-open-link-marker (make-marker)
  8127. "Marker pointing to the location where `org-open-at-point; was called.")
  8128. ;;;###autoload
  8129. (defun org-open-at-point-global ()
  8130. "Follow a link like Org-mode does.
  8131. This command can be called in any mode to follow a link that has
  8132. Org-mode syntax."
  8133. (interactive)
  8134. (org-run-like-in-org-mode 'org-open-at-point))
  8135. ;;;###autoload
  8136. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8137. "Open a link in the string S, as if it was in Org-mode."
  8138. (interactive "sLink: \nP")
  8139. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8140. (with-temp-buffer
  8141. (let ((org-inhibit-startup t))
  8142. (org-mode)
  8143. (insert s)
  8144. (goto-char (point-min))
  8145. (when reference-buffer
  8146. (setq org-link-abbrev-alist-local
  8147. (with-current-buffer reference-buffer
  8148. org-link-abbrev-alist-local)))
  8149. (org-open-at-point arg reference-buffer)))))
  8150. (defvar org-open-at-point-functions nil
  8151. "Hook that is run when following a link at point.
  8152. Functions in this hook must return t if they identify and follow
  8153. a link at point. If they don't find anything interesting at point,
  8154. they must return nil.")
  8155. (defun org-open-at-point (&optional in-emacs reference-buffer)
  8156. "Open link at or after point.
  8157. If there is no link at point, this function will search forward up to
  8158. the end of the current line.
  8159. Normally, files will be opened by an appropriate application. If the
  8160. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  8161. With a double prefix argument, try to open outside of Emacs, in the
  8162. application the system uses for this file type."
  8163. (interactive "P")
  8164. ;; if in a code block, then open the block's results
  8165. (unless (call-interactively #'org-babel-open-src-block-result)
  8166. (org-load-modules-maybe)
  8167. (move-marker org-open-link-marker (point))
  8168. (setq org-window-config-before-follow-link (current-window-configuration))
  8169. (org-remove-occur-highlights nil nil t)
  8170. (cond
  8171. ((and (org-on-heading-p)
  8172. (not (org-in-regexp
  8173. (concat org-plain-link-re "\\|"
  8174. org-bracket-link-regexp "\\|"
  8175. org-angle-link-re "\\|"
  8176. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8177. (not (get-text-property (point) 'org-linked-text)))
  8178. (or (org-offer-links-in-entry in-emacs)
  8179. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8180. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8181. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8182. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8183. (not (org-in-regexp org-bracket-link-regexp)))
  8184. (org-footnote-action))
  8185. (t
  8186. (let (type path link line search (pos (point)))
  8187. (catch 'match
  8188. (save-excursion
  8189. (skip-chars-forward "^]\n\r")
  8190. (when (org-in-regexp org-bracket-link-regexp 1)
  8191. (setq link (org-extract-attributes
  8192. (org-link-unescape (org-match-string-no-properties 1))))
  8193. (while (string-match " *\n *" link)
  8194. (setq link (replace-match " " t t link)))
  8195. (setq link (org-link-expand-abbrev link))
  8196. (cond
  8197. ((or (file-name-absolute-p link)
  8198. (string-match "^\\.\\.?/" link))
  8199. (setq type "file" path link))
  8200. ((string-match org-link-re-with-space3 link)
  8201. (setq type (match-string 1 link) path (match-string 2 link)))
  8202. (t (setq type "thisfile" path link)))
  8203. (throw 'match t)))
  8204. (when (get-text-property (point) 'org-linked-text)
  8205. (setq type "thisfile"
  8206. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8207. (1+ (point)) (point))
  8208. path (buffer-substring
  8209. (previous-single-property-change pos 'org-linked-text)
  8210. (next-single-property-change pos 'org-linked-text)))
  8211. (throw 'match t))
  8212. (save-excursion
  8213. (when (or (org-in-regexp org-angle-link-re)
  8214. (org-in-regexp org-plain-link-re))
  8215. (setq type (match-string 1) path (match-string 2))
  8216. (throw 'match t)))
  8217. (save-excursion
  8218. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8219. (setq type "tags"
  8220. path (match-string 1))
  8221. (while (string-match ":" path)
  8222. (setq path (replace-match "+" t t path)))
  8223. (throw 'match t)))
  8224. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8225. (setq type "tree-match"
  8226. path (match-string 1))
  8227. (throw 'match t)))
  8228. (unless path
  8229. (error "No link found"))
  8230. ;; switch back to reference buffer
  8231. ;; needed when if called in a temporary buffer through
  8232. ;; org-open-link-from-string
  8233. (with-current-buffer (or reference-buffer (current-buffer))
  8234. ;; Remove any trailing spaces in path
  8235. (if (string-match " +\\'" path)
  8236. (setq path (replace-match "" t t path)))
  8237. (if (and org-link-translation-function
  8238. (fboundp org-link-translation-function))
  8239. ;; Check if we need to translate the link
  8240. (let ((tmp (funcall org-link-translation-function type path)))
  8241. (setq type (car tmp) path (cdr tmp))))
  8242. (cond
  8243. ((assoc type org-link-protocols)
  8244. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8245. ((equal type "mailto")
  8246. (let ((cmd (car org-link-mailto-program))
  8247. (args (cdr org-link-mailto-program)) args1
  8248. (address path) (subject "") a)
  8249. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8250. (setq address (match-string 1 path)
  8251. subject (org-link-escape (match-string 2 path))))
  8252. (while args
  8253. (cond
  8254. ((not (stringp (car args))) (push (pop args) args1))
  8255. (t (setq a (pop args))
  8256. (if (string-match "%a" a)
  8257. (setq a (replace-match address t t a)))
  8258. (if (string-match "%s" a)
  8259. (setq a (replace-match subject t t a)))
  8260. (push a args1))))
  8261. (apply cmd (nreverse args1))))
  8262. ((member type '("http" "https" "ftp" "news"))
  8263. (browse-url (concat type ":" (org-link-escape
  8264. path org-link-escape-chars-browser))))
  8265. ((string= type "doi")
  8266. (browse-url (concat "http://dx.doi.org/"
  8267. (org-link-escape
  8268. path org-link-escape-chars-browser))))
  8269. ((member type '("message"))
  8270. (browse-url (concat type ":" path)))
  8271. ((string= type "tags")
  8272. (org-tags-view in-emacs path))
  8273. ((string= type "tree-match")
  8274. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8275. ((string= type "file")
  8276. (if (string-match "::\\([0-9]+\\)\\'" path)
  8277. (setq line (string-to-number (match-string 1 path))
  8278. path (substring path 0 (match-beginning 0)))
  8279. (if (string-match "::\\(.+\\)\\'" path)
  8280. (setq search (match-string 1 path)
  8281. path (substring path 0 (match-beginning 0)))))
  8282. (if (string-match "[*?{]" (file-name-nondirectory path))
  8283. (dired path)
  8284. (org-open-file path in-emacs line search)))
  8285. ((string= type "shell")
  8286. (let ((cmd path))
  8287. (if (or (not org-confirm-shell-link-function)
  8288. (funcall org-confirm-shell-link-function
  8289. (format "Execute \"%s\" in shell? "
  8290. (org-add-props cmd nil
  8291. 'face 'org-warning))))
  8292. (progn
  8293. (message "Executing %s" cmd)
  8294. (shell-command cmd))
  8295. (error "Abort"))))
  8296. ((string= type "elisp")
  8297. (let ((cmd path))
  8298. (if (or (not org-confirm-elisp-link-function)
  8299. (funcall org-confirm-elisp-link-function
  8300. (format "Execute \"%s\" as elisp? "
  8301. (org-add-props cmd nil
  8302. 'face 'org-warning))))
  8303. (message "%s => %s" cmd
  8304. (if (equal (string-to-char cmd) ?\()
  8305. (eval (read cmd))
  8306. (call-interactively (read cmd))))
  8307. (error "Abort"))))
  8308. ((and (string= type "thisfile")
  8309. (run-hook-with-args-until-success
  8310. 'org-open-link-functions path)))
  8311. ((string= type "thisfile")
  8312. (if in-emacs
  8313. (switch-to-buffer-other-window
  8314. (org-get-buffer-for-internal-link (current-buffer)))
  8315. (org-mark-ring-push))
  8316. (let ((cmd `(org-link-search
  8317. ,path
  8318. ,(cond ((equal in-emacs '(4)) 'occur)
  8319. ((equal in-emacs '(16)) 'org-occur)
  8320. (t nil))
  8321. ,pos)))
  8322. (condition-case nil (eval cmd)
  8323. (error (progn (widen) (eval cmd))))))
  8324. (t
  8325. (browse-url-at-point)))))))
  8326. (move-marker org-open-link-marker nil)
  8327. (run-hook-with-args 'org-follow-link-hook)))
  8328. (defun org-offer-links-in-entry (&optional nth zero)
  8329. "Offer links in the current entry and follow the selected link.
  8330. If there is only one link, follow it immediately as well.
  8331. If NTH is an integer, immediately pick the NTH link found.
  8332. If ZERO is a string, check also this string for a link, and if
  8333. there is one, offer it as link number zero."
  8334. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8335. "\\(" org-angle-link-re "\\)\\|"
  8336. "\\(" org-plain-link-re "\\)"))
  8337. (cnt ?0)
  8338. (in-emacs (if (integerp nth) nil nth))
  8339. have-zero end links link c)
  8340. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8341. (push (match-string 0 zero) links)
  8342. (setq cnt (1- cnt) have-zero t))
  8343. (save-excursion
  8344. (org-back-to-heading t)
  8345. (setq end (save-excursion (outline-next-heading) (point)))
  8346. (while (re-search-forward re end t)
  8347. (push (match-string 0) links))
  8348. (setq links (org-uniquify (reverse links))))
  8349. (cond
  8350. ((null links)
  8351. (message "No links"))
  8352. ((equal (length links) 1)
  8353. (setq link (list (car links))))
  8354. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8355. (setq link (nth (if have-zero nth (1- nth)) links)))
  8356. (t ; we have to select a link
  8357. (save-excursion
  8358. (save-window-excursion
  8359. (delete-other-windows)
  8360. (with-output-to-temp-buffer "*Select Link*"
  8361. (mapc (lambda (l)
  8362. (if (not (string-match org-bracket-link-regexp l))
  8363. (princ (format "[%c] %s\n" (incf cnt)
  8364. (org-remove-angle-brackets l)))
  8365. (if (match-end 3)
  8366. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8367. (match-string 3 l) (match-string 1 l)))
  8368. (princ (format "[%c] %s\n" (incf cnt)
  8369. (match-string 1 l))))))
  8370. links))
  8371. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8372. (message "Select link to open, RET to open all:")
  8373. (setq c (read-char-exclusive))
  8374. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8375. (when (equal c ?q) (error "Abort"))
  8376. (if (equal c ?\C-m)
  8377. (setq link links)
  8378. (setq nth (- c ?0))
  8379. (if have-zero (setq nth (1+ nth)))
  8380. (unless (and (integerp nth) (>= (length links) nth))
  8381. (error "Invalid link selection"))
  8382. (setq link (list (nth (1- nth) links))))))
  8383. (if link
  8384. (let ((buf (current-buffer)))
  8385. (dolist (l link)
  8386. (org-open-link-from-string l in-emacs buf))
  8387. t)
  8388. nil)))
  8389. ;; Add special file links that specify the way of opening
  8390. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8391. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8392. (defun org-open-file-with-system (path)
  8393. "Open file at PATH using the system way of opening it."
  8394. (org-open-file path 'system))
  8395. (defun org-open-file-with-emacs (path)
  8396. "Open file at PATH in Emacs."
  8397. (org-open-file path 'emacs))
  8398. (defun org-remove-file-link-modifiers ()
  8399. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8400. (goto-char (point-min))
  8401. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8402. (org-if-unprotected
  8403. (replace-match "file:" t t))))
  8404. (eval-after-load "org-exp"
  8405. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8406. 'org-remove-file-link-modifiers))
  8407. ;;;; Time estimates
  8408. (defun org-get-effort (&optional pom)
  8409. "Get the effort estimate for the current entry."
  8410. (org-entry-get pom org-effort-property))
  8411. ;;; File search
  8412. (defvar org-create-file-search-functions nil
  8413. "List of functions to construct the right search string for a file link.
  8414. These functions are called in turn with point at the location to
  8415. which the link should point.
  8416. A function in the hook should first test if it would like to
  8417. handle this file type, for example by checking the `major-mode'
  8418. or the file extension. If it decides not to handle this file, it
  8419. should just return nil to give other functions a chance. If it
  8420. does handle the file, it must return the search string to be used
  8421. when following the link. The search string will be part of the
  8422. file link, given after a double colon, and `org-open-at-point'
  8423. will automatically search for it. If special measures must be
  8424. taken to make the search successful, another function should be
  8425. added to the companion hook `org-execute-file-search-functions',
  8426. which see.
  8427. A function in this hook may also use `setq' to set the variable
  8428. `description' to provide a suggestion for the descriptive text to
  8429. be used for this link when it gets inserted into an Org-mode
  8430. buffer with \\[org-insert-link].")
  8431. (defvar org-execute-file-search-functions nil
  8432. "List of functions to execute a file search triggered by a link.
  8433. Functions added to this hook must accept a single argument, the
  8434. search string that was part of the file link, the part after the
  8435. double colon. The function must first check if it would like to
  8436. handle this search, for example by checking the `major-mode' or
  8437. the file extension. If it decides not to handle this search, it
  8438. should just return nil to give other functions a chance. If it
  8439. does handle the search, it must return a non-nil value to keep
  8440. other functions from trying.
  8441. Each function can access the current prefix argument through the
  8442. variable `current-prefix-argument'. Note that a single prefix is
  8443. used to force opening a link in Emacs, so it may be good to only
  8444. use a numeric or double prefix to guide the search function.
  8445. In case this is needed, a function in this hook can also restore
  8446. the window configuration before `org-open-at-point' was called using:
  8447. (set-window-configuration org-window-config-before-follow-link)")
  8448. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8449. (defun org-link-search (s &optional type avoid-pos)
  8450. "Search for a link search option.
  8451. If S is surrounded by forward slashes, it is interpreted as a
  8452. regular expression. In org-mode files, this will create an `org-occur'
  8453. sparse tree. In ordinary files, `occur' will be used to list matches.
  8454. If the current buffer is in `dired-mode', grep will be used to search
  8455. in all files. If AVOID-POS is given, ignore matches near that position."
  8456. (let ((case-fold-search t)
  8457. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8458. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8459. (append '(("") (" ") ("\t") ("\n"))
  8460. org-emphasis-alist)
  8461. "\\|") "\\)"))
  8462. (pos (point))
  8463. (pre nil) (post nil)
  8464. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8465. (cond
  8466. ;; First check if there are any special search functions
  8467. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8468. ;; Now try the builtin stuff
  8469. ((and (equal (string-to-char s0) ?#)
  8470. (> (length s0) 1)
  8471. (save-excursion
  8472. (goto-char (point-min))
  8473. (and
  8474. (re-search-forward
  8475. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8476. (setq type 'dedicated
  8477. pos (match-beginning 0))))
  8478. ;; There is an exact target for this
  8479. (goto-char pos)
  8480. (org-back-to-heading t)))
  8481. ((save-excursion
  8482. (goto-char (point-min))
  8483. (and
  8484. (re-search-forward
  8485. (concat "<<" (regexp-quote s0) ">>") nil t)
  8486. (setq type 'dedicated
  8487. pos (match-beginning 0))))
  8488. ;; There is an exact target for this
  8489. (goto-char pos))
  8490. ((and (string-match "^(\\(.*\\))$" s0)
  8491. (save-excursion
  8492. (goto-char (point-min))
  8493. (and
  8494. (re-search-forward
  8495. (concat "[^[]" (regexp-quote
  8496. (format org-coderef-label-format
  8497. (match-string 1 s0))))
  8498. nil t)
  8499. (setq type 'dedicated
  8500. pos (1+ (match-beginning 0))))))
  8501. ;; There is a coderef target for this
  8502. (goto-char pos))
  8503. ((string-match "^/\\(.*\\)/$" s)
  8504. ;; A regular expression
  8505. (cond
  8506. ((org-mode-p)
  8507. (org-occur (match-string 1 s)))
  8508. ;;((eq major-mode 'dired-mode)
  8509. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8510. (t (org-do-occur (match-string 1 s)))))
  8511. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8512. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8513. (goto-char (point-min))
  8514. (cond
  8515. ((let (case-fold-search)
  8516. (re-search-forward (format org-complex-heading-regexp-format
  8517. (regexp-quote s))
  8518. nil t))
  8519. ;; OK, found a match
  8520. (setq type 'dedicated)
  8521. (goto-char (match-beginning 0)))
  8522. ((and (not org-link-search-inhibit-query)
  8523. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8524. (y-or-n-p "No match - create this as a new heading? "))
  8525. (goto-char (point-max))
  8526. (or (bolp) (newline))
  8527. (insert "* " s "\n")
  8528. (beginning-of-line 0))
  8529. (t
  8530. (goto-char pos)
  8531. (error "No match"))))
  8532. (t
  8533. ;; A normal search string
  8534. (when (equal (string-to-char s) ?*)
  8535. ;; Anchor on headlines, post may include tags.
  8536. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8537. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8538. s (substring s 1)))
  8539. (remove-text-properties
  8540. 0 (length s)
  8541. '(face nil mouse-face nil keymap nil fontified nil) s)
  8542. ;; Make a series of regular expressions to find a match
  8543. (setq words (org-split-string s "[ \n\r\t]+")
  8544. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8545. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8546. "\\)" markers)
  8547. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8548. re2a (concat "[ \t\r\n]" re2a_)
  8549. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8550. re4 (concat "[^a-zA-Z_]" re4_)
  8551. re1 (concat pre re2 post)
  8552. re3 (concat pre (if pre re4_ re4) post)
  8553. re5 (concat pre ".*" re4)
  8554. re2 (concat pre re2)
  8555. re2a (concat pre (if pre re2a_ re2a))
  8556. re4 (concat pre (if pre re4_ re4))
  8557. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8558. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8559. re5 "\\)"
  8560. ))
  8561. (cond
  8562. ((eq type 'org-occur) (org-occur reall))
  8563. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8564. (t (goto-char (point-min))
  8565. (setq type 'fuzzy)
  8566. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8567. (org-search-not-self 1 re1 nil t)
  8568. (org-search-not-self 1 re2 nil t)
  8569. (org-search-not-self 1 re2a nil t)
  8570. (org-search-not-self 1 re3 nil t)
  8571. (org-search-not-self 1 re4 nil t)
  8572. (org-search-not-self 1 re5 nil t)
  8573. )
  8574. (goto-char (match-beginning 1))
  8575. (goto-char pos)
  8576. (error "No match"))))))
  8577. (and (org-mode-p) (org-show-context 'link-search))
  8578. type))
  8579. (defun org-search-not-self (group &rest args)
  8580. "Execute `re-search-forward', but only accept matches that do not
  8581. enclose the position of `org-open-link-marker'."
  8582. (let ((m org-open-link-marker))
  8583. (catch 'exit
  8584. (while (apply 're-search-forward args)
  8585. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8586. (goto-char (match-end group))
  8587. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8588. (> (match-beginning 0) (marker-position m))
  8589. (< (match-end 0) (marker-position m)))
  8590. (save-match-data
  8591. (or (not (org-in-regexp
  8592. org-bracket-link-analytic-regexp 1))
  8593. (not (match-end 4)) ; no description
  8594. (and (<= (match-beginning 4) (point))
  8595. (>= (match-end 4) (point))))))
  8596. (throw 'exit (point))))))))
  8597. (defun org-get-buffer-for-internal-link (buffer)
  8598. "Return a buffer to be used for displaying the link target of internal links."
  8599. (cond
  8600. ((not org-display-internal-link-with-indirect-buffer)
  8601. buffer)
  8602. ((string-match "(Clone)$" (buffer-name buffer))
  8603. (message "Buffer is already a clone, not making another one")
  8604. ;; we also do not modify visibility in this case
  8605. buffer)
  8606. (t ; make a new indirect buffer for displaying the link
  8607. (let* ((bn (buffer-name buffer))
  8608. (ibn (concat bn "(Clone)"))
  8609. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8610. (with-current-buffer ib (org-overview))
  8611. ib))))
  8612. (defun org-do-occur (regexp &optional cleanup)
  8613. "Call the Emacs command `occur'.
  8614. If CLEANUP is non-nil, remove the printout of the regular expression
  8615. in the *Occur* buffer. This is useful if the regex is long and not useful
  8616. to read."
  8617. (occur regexp)
  8618. (when cleanup
  8619. (let ((cwin (selected-window)) win beg end)
  8620. (when (setq win (get-buffer-window "*Occur*"))
  8621. (select-window win))
  8622. (goto-char (point-min))
  8623. (when (re-search-forward "match[a-z]+" nil t)
  8624. (setq beg (match-end 0))
  8625. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8626. (setq end (1- (match-beginning 0)))))
  8627. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8628. (goto-char (point-min))
  8629. (select-window cwin))))
  8630. ;;; The mark ring for links jumps
  8631. (defvar org-mark-ring nil
  8632. "Mark ring for positions before jumps in Org-mode.")
  8633. (defvar org-mark-ring-last-goto nil
  8634. "Last position in the mark ring used to go back.")
  8635. ;; Fill and close the ring
  8636. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8637. (loop for i from 1 to org-mark-ring-length do
  8638. (push (make-marker) org-mark-ring))
  8639. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8640. org-mark-ring)
  8641. (defun org-mark-ring-push (&optional pos buffer)
  8642. "Put the current position or POS into the mark ring and rotate it."
  8643. (interactive)
  8644. (setq pos (or pos (point)))
  8645. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8646. (move-marker (car org-mark-ring)
  8647. (or pos (point))
  8648. (or buffer (current-buffer)))
  8649. (message "%s"
  8650. (substitute-command-keys
  8651. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8652. (defun org-mark-ring-goto (&optional n)
  8653. "Jump to the previous position in the mark ring.
  8654. With prefix arg N, jump back that many stored positions. When
  8655. called several times in succession, walk through the entire ring.
  8656. Org-mode commands jumping to a different position in the current file,
  8657. or to another Org-mode file, automatically push the old position
  8658. onto the ring."
  8659. (interactive "p")
  8660. (let (p m)
  8661. (if (eq last-command this-command)
  8662. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8663. (setq p org-mark-ring))
  8664. (setq org-mark-ring-last-goto p)
  8665. (setq m (car p))
  8666. (switch-to-buffer (marker-buffer m))
  8667. (goto-char m)
  8668. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8669. (defun org-remove-angle-brackets (s)
  8670. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8671. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8672. s)
  8673. (defun org-add-angle-brackets (s)
  8674. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8675. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8676. s)
  8677. (defun org-remove-double-quotes (s)
  8678. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8679. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8680. s)
  8681. ;;; Following specific links
  8682. (defun org-follow-timestamp-link ()
  8683. (cond
  8684. ((org-at-date-range-p t)
  8685. (let ((org-agenda-start-on-weekday)
  8686. (t1 (match-string 1))
  8687. (t2 (match-string 2)))
  8688. (setq t1 (time-to-days (org-time-string-to-time t1))
  8689. t2 (time-to-days (org-time-string-to-time t2)))
  8690. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8691. ((org-at-timestamp-p t)
  8692. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8693. (substring (match-string 1) 0 10)))
  8694. 1))
  8695. (t (error "This should not happen"))))
  8696. ;;; Following file links
  8697. (defvar org-wait nil)
  8698. (defun org-open-file (path &optional in-emacs line search)
  8699. "Open the file at PATH.
  8700. First, this expands any special file name abbreviations. Then the
  8701. configuration variable `org-file-apps' is checked if it contains an
  8702. entry for this file type, and if yes, the corresponding command is launched.
  8703. If no application is found, Emacs simply visits the file.
  8704. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8705. With a double \\[universal-argument] \\[universal-argument] \
  8706. prefix arg, Org tries to avoid opening in Emacs
  8707. and to use an external application to visit the file.
  8708. Optional LINE specifies a line to go to, optional SEARCH a string
  8709. to search for. If LINE or SEARCH is given, the file will be
  8710. opened in Emacs, unless an entry from org-file-apps that makes
  8711. use of groups in a regexp matches.
  8712. If the file does not exist, an error is thrown."
  8713. (let* ((file (if (equal path "")
  8714. buffer-file-name
  8715. (substitute-in-file-name (expand-file-name path))))
  8716. (file-apps (append org-file-apps (org-default-apps)))
  8717. (apps (org-remove-if
  8718. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8719. (apps-dlink (org-remove-if-not
  8720. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8721. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8722. (dirp (if remp nil (file-directory-p file)))
  8723. (file (if (and dirp org-open-directory-means-index-dot-org)
  8724. (concat (file-name-as-directory file) "index.org")
  8725. file))
  8726. (a-m-a-p (assq 'auto-mode apps))
  8727. (dfile (downcase file))
  8728. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8729. (link (cond ((and (eq line nil)
  8730. (eq search nil))
  8731. file)
  8732. (line
  8733. (concat file "::" (number-to-string line)))
  8734. (search
  8735. (concat file "::" search))))
  8736. (dlink (downcase link))
  8737. (old-buffer (current-buffer))
  8738. (old-pos (point))
  8739. (old-mode major-mode)
  8740. ext cmd link-match-data)
  8741. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8742. (setq ext (match-string 1 dfile))
  8743. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8744. (setq ext (match-string 1 dfile))))
  8745. (cond
  8746. ((member in-emacs '((16) system))
  8747. (setq cmd (cdr (assoc 'system apps))))
  8748. (in-emacs (setq cmd 'emacs))
  8749. (t
  8750. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8751. (and dirp (cdr (assoc 'directory apps)))
  8752. ; first, try matching against apps-dlink
  8753. ; if we get a match here, store the match data for later
  8754. (let ((match (assoc-default dlink apps-dlink
  8755. 'string-match)))
  8756. (if match
  8757. (progn (setq link-match-data (match-data))
  8758. match)
  8759. (progn (setq in-emacs (or in-emacs line search))
  8760. nil))) ; if we have no match in apps-dlink,
  8761. ; always open the file in emacs if line or search
  8762. ; is given (for backwards compatibility)
  8763. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8764. 'string-match)
  8765. (cdr (assoc ext apps))
  8766. (cdr (assoc t apps))))))
  8767. (when (eq cmd 'system)
  8768. (setq cmd (cdr (assoc 'system apps))))
  8769. (when (eq cmd 'default)
  8770. (setq cmd (cdr (assoc t apps))))
  8771. (when (eq cmd 'mailcap)
  8772. (require 'mailcap)
  8773. (mailcap-parse-mailcaps)
  8774. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8775. (command (mailcap-mime-info mime-type)))
  8776. (if (stringp command)
  8777. (setq cmd command)
  8778. (setq cmd 'emacs))))
  8779. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8780. (not (file-exists-p file))
  8781. (not org-open-non-existing-files))
  8782. (error "No such file: %s" file))
  8783. (cond
  8784. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8785. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8786. (while (string-match "['\"]%s['\"]" cmd)
  8787. (setq cmd (replace-match "%s" t t cmd)))
  8788. (while (string-match "%s" cmd)
  8789. (setq cmd (replace-match
  8790. (save-match-data
  8791. (shell-quote-argument
  8792. (convert-standard-filename file)))
  8793. t t cmd)))
  8794. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8795. (save-match-data
  8796. (let ((match-index 1)
  8797. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8798. (set-match-data link-match-data)
  8799. (while (<= match-index number-of-groups)
  8800. (let ((regex (concat "%" (number-to-string match-index)))
  8801. (replace-with (match-string match-index dlink)))
  8802. (while (string-match regex cmd)
  8803. (setq cmd (replace-match replace-with t t cmd))))
  8804. (setq match-index (+ match-index 1)))))
  8805. (save-window-excursion
  8806. (start-process-shell-command cmd nil cmd)
  8807. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8808. ))
  8809. ((or (stringp cmd)
  8810. (eq cmd 'emacs))
  8811. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8812. (widen)
  8813. (if line (org-goto-line line)
  8814. (if search (org-link-search search))))
  8815. ((consp cmd)
  8816. (let ((file (convert-standard-filename file)))
  8817. (save-match-data
  8818. (set-match-data link-match-data)
  8819. (eval cmd))))
  8820. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8821. (and (org-mode-p) (eq old-mode 'org-mode)
  8822. (or (not (equal old-buffer (current-buffer)))
  8823. (not (equal old-pos (point))))
  8824. (org-mark-ring-push old-pos old-buffer))))
  8825. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8826. "This function returns non-nil if `entry' uses a regular
  8827. expression which should be matched against the whole link by
  8828. org-open-file.
  8829. It assumes that is the case when the entry uses a regular
  8830. expression which has at least one grouping construct and the
  8831. action is either a lisp form or a command string containing
  8832. '%1', i.e. using at least one subexpression match as a
  8833. parameter."
  8834. (let ((selector (car entry))
  8835. (action (cdr entry)))
  8836. (if (stringp selector)
  8837. (and (> (regexp-opt-depth selector) 0)
  8838. (or (and (stringp action)
  8839. (string-match "%[0-9]" action))
  8840. (consp action)))
  8841. nil)))
  8842. (defun org-default-apps ()
  8843. "Return the default applications for this operating system."
  8844. (cond
  8845. ((eq system-type 'darwin)
  8846. org-file-apps-defaults-macosx)
  8847. ((eq system-type 'windows-nt)
  8848. org-file-apps-defaults-windowsnt)
  8849. (t org-file-apps-defaults-gnu)))
  8850. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8851. "Convert extensions to regular expressions in the cars of LIST.
  8852. Also, weed out any non-string entries, because the return value is used
  8853. only for regexp matching.
  8854. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8855. point to the symbol `emacs', indicating that the file should
  8856. be opened in Emacs."
  8857. (append
  8858. (delq nil
  8859. (mapcar (lambda (x)
  8860. (if (not (stringp (car x)))
  8861. nil
  8862. (if (string-match "\\W" (car x))
  8863. x
  8864. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8865. list))
  8866. (if add-auto-mode
  8867. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8868. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8869. (defun org-file-remote-p (file)
  8870. "Test whether FILE specifies a location on a remote system.
  8871. Return non-nil if the location is indeed remote.
  8872. For example, the filename \"/user@host:/foo\" specifies a location
  8873. on the system \"/user@host:\"."
  8874. (cond ((fboundp 'file-remote-p)
  8875. (file-remote-p file))
  8876. ((fboundp 'tramp-handle-file-remote-p)
  8877. (tramp-handle-file-remote-p file))
  8878. ((and (boundp 'ange-ftp-name-format)
  8879. (string-match (car ange-ftp-name-format) file))
  8880. t)
  8881. (t nil)))
  8882. ;;;; Refiling
  8883. (defun org-get-org-file ()
  8884. "Read a filename, with default directory `org-directory'."
  8885. (let ((default (or org-default-notes-file remember-data-file)))
  8886. (read-file-name (format "File name [%s]: " default)
  8887. (file-name-as-directory org-directory)
  8888. default)))
  8889. (defun org-notes-order-reversed-p ()
  8890. "Check if the current file should receive notes in reversed order."
  8891. (cond
  8892. ((not org-reverse-note-order) nil)
  8893. ((eq t org-reverse-note-order) t)
  8894. ((not (listp org-reverse-note-order)) nil)
  8895. (t (catch 'exit
  8896. (let ((all org-reverse-note-order)
  8897. entry)
  8898. (while (setq entry (pop all))
  8899. (if (string-match (car entry) buffer-file-name)
  8900. (throw 'exit (cdr entry))))
  8901. nil)))))
  8902. (defvar org-refile-target-table nil
  8903. "The list of refile targets, created by `org-refile'.")
  8904. (defvar org-agenda-new-buffers nil
  8905. "Buffers created to visit agenda files.")
  8906. (defvar org-refile-cache nil
  8907. "Cache for refile targets.")
  8908. (defvar org-refile-markers nil
  8909. "All the markers used for caching refile locations.")
  8910. (defun org-refile-marker (pos)
  8911. "Get a new refile marker, but only if caching is in use."
  8912. (if (not org-refile-use-cache)
  8913. pos
  8914. (let ((m (make-marker)))
  8915. (move-marker m pos)
  8916. (push m org-refile-markers)
  8917. m)))
  8918. (defun org-refile-cache-clear ()
  8919. "Clear the refile cache and disable all the markers."
  8920. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8921. (setq org-refile-markers nil)
  8922. (setq org-refile-cache nil)
  8923. (message "Refile cache has been cleared"))
  8924. (defun org-refile-cache-check-set (set)
  8925. "Check if all the markers in the cache still have live buffers."
  8926. (let (marker)
  8927. (catch 'exit
  8928. (while (and set (setq marker (nth 3 (pop set))))
  8929. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8930. (when (and marker (null (marker-buffer marker)))
  8931. (message "not found") (sit-for 3)
  8932. (throw 'exit nil)))
  8933. t)))
  8934. (defun org-refile-cache-put (set &rest identifiers)
  8935. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8936. (let* ((key (sha1 (prin1-to-string identifiers)))
  8937. (entry (assoc key org-refile-cache)))
  8938. (if entry
  8939. (setcdr entry set)
  8940. (push (cons key set) org-refile-cache))))
  8941. (defun org-refile-cache-get (&rest identifiers)
  8942. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8943. (cond
  8944. ((not org-refile-cache) nil)
  8945. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8946. (t
  8947. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8948. org-refile-cache))))
  8949. (and set (org-refile-cache-check-set set) set)))))
  8950. (defun org-get-refile-targets (&optional default-buffer)
  8951. "Produce a table with refile targets."
  8952. (let ((case-fold-search nil)
  8953. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8954. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8955. targets tgs txt re files f desc descre fast-path-p level pos0)
  8956. (message "Getting targets...")
  8957. (with-current-buffer (or default-buffer (current-buffer))
  8958. (while (setq entry (pop entries))
  8959. (setq files (car entry) desc (cdr entry))
  8960. (setq fast-path-p nil)
  8961. (cond
  8962. ((null files) (setq files (list (current-buffer))))
  8963. ((eq files 'org-agenda-files)
  8964. (setq files (org-agenda-files 'unrestricted)))
  8965. ((and (symbolp files) (fboundp files))
  8966. (setq files (funcall files)))
  8967. ((and (symbolp files) (boundp files))
  8968. (setq files (symbol-value files))))
  8969. (if (stringp files) (setq files (list files)))
  8970. (cond
  8971. ((eq (car desc) :tag)
  8972. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8973. ((eq (car desc) :todo)
  8974. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8975. ((eq (car desc) :regexp)
  8976. (setq descre (cdr desc)))
  8977. ((eq (car desc) :level)
  8978. (setq descre (concat "^\\*\\{" (number-to-string
  8979. (if org-odd-levels-only
  8980. (1- (* 2 (cdr desc)))
  8981. (cdr desc)))
  8982. "\\}[ \t]")))
  8983. ((eq (car desc) :maxlevel)
  8984. (setq fast-path-p t)
  8985. (setq descre (concat "^\\*\\{1," (number-to-string
  8986. (if org-odd-levels-only
  8987. (1- (* 2 (cdr desc)))
  8988. (cdr desc)))
  8989. "\\}[ \t]")))
  8990. (t (error "Bad refiling target description %s" desc)))
  8991. (while (setq f (pop files))
  8992. (with-current-buffer
  8993. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8994. (or
  8995. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8996. (progn
  8997. (if (bufferp f) (setq f (buffer-file-name
  8998. (buffer-base-buffer f))))
  8999. (setq f (and f (expand-file-name f)))
  9000. (if (eq org-refile-use-outline-path 'file)
  9001. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9002. (save-excursion
  9003. (save-restriction
  9004. (widen)
  9005. (goto-char (point-min))
  9006. (while (re-search-forward descre nil t)
  9007. (goto-char (setq pos0 (point-at-bol)))
  9008. (catch 'next
  9009. (when org-refile-target-verify-function
  9010. (save-match-data
  9011. (or (funcall org-refile-target-verify-function)
  9012. (throw 'next t))))
  9013. (when (looking-at org-complex-heading-regexp)
  9014. (setq level (org-reduced-level
  9015. (- (match-end 1) (match-beginning 1)))
  9016. txt (org-link-display-format (match-string 4))
  9017. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9018. re (format org-complex-heading-regexp-format
  9019. (regexp-quote (match-string 4))))
  9020. (when org-refile-use-outline-path
  9021. (setq txt (mapconcat
  9022. 'org-protect-slash
  9023. (append
  9024. (if (eq org-refile-use-outline-path
  9025. 'file)
  9026. (list (file-name-nondirectory
  9027. (buffer-file-name
  9028. (buffer-base-buffer))))
  9029. (if (eq org-refile-use-outline-path
  9030. 'full-file-path)
  9031. (list (buffer-file-name
  9032. (buffer-base-buffer)))))
  9033. (org-get-outline-path fast-path-p
  9034. level txt)
  9035. (list txt))
  9036. "/")))
  9037. (push (list txt f re (org-refile-marker (point)))
  9038. tgs)))
  9039. (when (= (point) pos0)
  9040. ;; verification function has not moved point
  9041. (goto-char (point-at-eol))))))))
  9042. (when org-refile-use-cache
  9043. (org-refile-cache-put tgs (buffer-file-name) descre))
  9044. (setq targets (append tgs targets))
  9045. ))))
  9046. (message "Getting targets...done")
  9047. (nreverse targets)))
  9048. (defun org-protect-slash (s)
  9049. (while (string-match "/" s)
  9050. (setq s (replace-match "\\" t t s)))
  9051. s)
  9052. (defvar org-olpa (make-vector 20 nil))
  9053. (defun org-get-outline-path (&optional fastp level heading)
  9054. "Return the outline path to the current entry, as a list.
  9055. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9056. routine which makes outline path derivations for an entire file,
  9057. avoiding backtracing. Refile target collection makes use of that."
  9058. (if fastp
  9059. (progn
  9060. (if (> level 19)
  9061. (error "Outline path failure, more than 19 levels"))
  9062. (loop for i from level upto 19 do
  9063. (aset org-olpa i nil))
  9064. (prog1
  9065. (delq nil (append org-olpa nil))
  9066. (aset org-olpa level heading)))
  9067. (let (rtn case-fold-search)
  9068. (save-excursion
  9069. (save-restriction
  9070. (widen)
  9071. (while (org-up-heading-safe)
  9072. (when (looking-at org-complex-heading-regexp)
  9073. (push (org-match-string-no-properties 4) rtn)))
  9074. rtn)))))
  9075. (defun org-format-outline-path (path &optional width prefix)
  9076. "Format the outline path PATH for display.
  9077. Width is the maximum number of characters that is available.
  9078. Prefix is a prefix to be included in the returned string,
  9079. such as the file name."
  9080. (setq width (or width 79))
  9081. (if prefix (setq width (- width (length prefix))))
  9082. (if (not path)
  9083. (or prefix "")
  9084. (let* ((nsteps (length path))
  9085. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9086. (maxwidth (if (<= total-width width)
  9087. 10000 ;; everything fits
  9088. ;; we need to shorten the level headings
  9089. (/ (- width nsteps) nsteps)))
  9090. (org-odd-levels-only nil)
  9091. (n 0)
  9092. (total (1+ (length prefix))))
  9093. (setq maxwidth (max maxwidth 10))
  9094. (concat prefix
  9095. (mapconcat
  9096. (lambda (h)
  9097. (setq n (1+ n))
  9098. (if (and (= n nsteps) (< maxwidth 10000))
  9099. (setq maxwidth (- total-width total)))
  9100. (if (< (length h) maxwidth)
  9101. (progn (setq total (+ total (length h) 1)) h)
  9102. (setq h (substring h 0 (- maxwidth 2))
  9103. total (+ total maxwidth 1))
  9104. (if (string-match "[ \t]+\\'" h)
  9105. (setq h (substring h 0 (match-beginning 0))))
  9106. (setq h (concat h "..")))
  9107. (org-add-props h nil 'face
  9108. (nth (% (1- n) org-n-level-faces)
  9109. org-level-faces))
  9110. h)
  9111. path "/")))))
  9112. (defun org-display-outline-path (&optional file current)
  9113. "Display the current outline path in the echo area."
  9114. (interactive "P")
  9115. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9116. (case-fold-search nil)
  9117. (path (and (org-mode-p) (org-get-outline-path))))
  9118. (if current (setq path (append path
  9119. (save-excursion
  9120. (org-back-to-heading t)
  9121. (if (looking-at org-complex-heading-regexp)
  9122. (list (match-string 4)))))))
  9123. (message "%s"
  9124. (org-format-outline-path
  9125. path
  9126. (1- (frame-width))
  9127. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9128. (defvar org-refile-history nil
  9129. "History for refiling operations.")
  9130. (defvar org-after-refile-insert-hook nil
  9131. "Hook run after `org-refile' has inserted its stuff at the new location.
  9132. Note that this is still *before* the stuff will be removed from
  9133. the *old* location.")
  9134. (defvar org-capture-last-stored-marker)
  9135. (defun org-refile (&optional goto default-buffer rfloc)
  9136. "Move the entry at point to another heading.
  9137. The list of target headings is compiled using the information in
  9138. `org-refile-targets', which see. This list is created before each use
  9139. and will therefore always be up-to-date.
  9140. At the target location, the entry is filed as a subitem of the target heading.
  9141. Depending on `org-reverse-note-order', the new subitem will either be the
  9142. first or the last subitem.
  9143. If there is an active region, all entries in that region will be moved.
  9144. However, the region must fulfill the requirement that the first heading
  9145. is the first one sets the top-level of the moved text - at most siblings
  9146. below it are allowed.
  9147. With prefix arg GOTO, the command will only visit the target location,
  9148. not actually move anything.
  9149. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9150. go to the location where the last refiling
  9151. operation has put the subtree.
  9152. With a prefix argument of `2', refile to the running clock.
  9153. RFLOC can be a refile location obtained in a different way.
  9154. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9155. If you are using target caching (see `org-refile-use-cache'),
  9156. You have to clear the target cache in order to find new targets.
  9157. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9158. (interactive "P")
  9159. (if (member goto '(0 (64)))
  9160. (org-refile-cache-clear)
  9161. (let* ((cbuf (current-buffer))
  9162. (regionp (org-region-active-p))
  9163. (region-start (and regionp (region-beginning)))
  9164. (region-end (and regionp (region-end)))
  9165. (region-length (and regionp (- region-end region-start)))
  9166. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9167. pos it nbuf file re level reversed)
  9168. (setq last-command nil)
  9169. (when regionp
  9170. (goto-char region-start)
  9171. (or (bolp) (goto-char (point-at-bol)))
  9172. (setq region-start (point))
  9173. (unless (org-kill-is-subtree-p
  9174. (buffer-substring region-start region-end))
  9175. (error "The region is not a (sequence of) subtree(s)")))
  9176. (if (equal goto '(16))
  9177. (org-refile-goto-last-stored)
  9178. (when (or
  9179. (and (equal goto 2)
  9180. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9181. (prog1
  9182. (setq it (list (or org-clock-heading "running clock")
  9183. (buffer-file-name
  9184. (marker-buffer org-clock-hd-marker))
  9185. ""
  9186. (marker-position org-clock-hd-marker)))
  9187. (setq goto nil)))
  9188. (setq it (or rfloc
  9189. (save-excursion
  9190. (org-refile-get-location
  9191. (if goto "Goto: " "Refile to: ") default-buffer
  9192. org-refile-allow-creating-parent-nodes)))))
  9193. (setq file (nth 1 it)
  9194. re (nth 2 it)
  9195. pos (nth 3 it))
  9196. (if (and (not goto)
  9197. pos
  9198. (equal (buffer-file-name) file)
  9199. (if regionp
  9200. (and (>= pos region-start)
  9201. (<= pos region-end))
  9202. (and (>= pos (point))
  9203. (< pos (save-excursion
  9204. (org-end-of-subtree t t))))))
  9205. (error "Cannot refile to position inside the tree or region"))
  9206. (setq nbuf (or (find-buffer-visiting file)
  9207. (find-file-noselect file)))
  9208. (if goto
  9209. (progn
  9210. (switch-to-buffer nbuf)
  9211. (goto-char pos)
  9212. (org-show-context 'org-goto))
  9213. (if regionp
  9214. (progn
  9215. (org-kill-new (buffer-substring region-start region-end))
  9216. (org-save-markers-in-region region-start region-end))
  9217. (org-copy-subtree 1 nil t))
  9218. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9219. (find-file-noselect file)))
  9220. (setq reversed (org-notes-order-reversed-p))
  9221. (save-excursion
  9222. (save-restriction
  9223. (widen)
  9224. (if pos
  9225. (progn
  9226. (goto-char pos)
  9227. (looking-at outline-regexp)
  9228. (setq level (org-get-valid-level (funcall outline-level) 1))
  9229. (goto-char
  9230. (if reversed
  9231. (or (outline-next-heading) (point-max))
  9232. (or (save-excursion (org-get-next-sibling))
  9233. (org-end-of-subtree t t)
  9234. (point-max)))))
  9235. (setq level 1)
  9236. (if (not reversed)
  9237. (goto-char (point-max))
  9238. (goto-char (point-min))
  9239. (or (outline-next-heading) (goto-char (point-max)))))
  9240. (if (not (bolp)) (newline))
  9241. (org-paste-subtree level)
  9242. (when org-log-refile
  9243. (org-add-log-setup 'refile nil nil 'findpos
  9244. org-log-refile)
  9245. (unless (eq org-log-refile 'note)
  9246. (save-excursion (org-add-log-note))))
  9247. (and org-auto-align-tags (org-set-tags nil t))
  9248. (bookmark-set "org-refile-last-stored")
  9249. ;; If we are refiling for capture, make sure that the
  9250. ;; last-capture pointers point here
  9251. (when (org-bound-and-true-p org-refile-for-capture)
  9252. (bookmark-set "org-capture-last-stored-marker")
  9253. (move-marker org-capture-last-stored-marker (point)))
  9254. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9255. (run-hooks 'org-after-refile-insert-hook))))
  9256. (if regionp
  9257. (delete-region (point) (+ (point) region-length))
  9258. (org-cut-subtree))
  9259. (when (featurep 'org-inlinetask)
  9260. (org-inlinetask-remove-END-maybe))
  9261. (setq org-markers-to-move nil)
  9262. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9263. (defun org-refile-goto-last-stored ()
  9264. "Go to the location where the last refile was stored."
  9265. (interactive)
  9266. (bookmark-jump "org-refile-last-stored")
  9267. (message "This is the location of the last refile"))
  9268. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9269. "Prompt the user for a refile location, using PROMPT."
  9270. (let ((org-refile-targets org-refile-targets)
  9271. (org-refile-use-outline-path org-refile-use-outline-path))
  9272. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  9273. (unless org-refile-target-table
  9274. (error "No refile targets"))
  9275. (let* ((cbuf (current-buffer))
  9276. (partial-completion-mode nil)
  9277. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9278. (cfunc (if (and org-refile-use-outline-path
  9279. org-outline-path-complete-in-steps)
  9280. 'org-olpath-completing-read
  9281. 'org-icompleting-read))
  9282. (extra (if org-refile-use-outline-path "/" ""))
  9283. (filename (and cfn (expand-file-name cfn)))
  9284. (tbl (mapcar
  9285. (lambda (x)
  9286. (if (and (not (member org-refile-use-outline-path
  9287. '(file full-file-path)))
  9288. (not (equal filename (nth 1 x))))
  9289. (cons (concat (car x) extra " ("
  9290. (file-name-nondirectory (nth 1 x)) ")")
  9291. (cdr x))
  9292. (cons (concat (car x) extra) (cdr x))))
  9293. org-refile-target-table))
  9294. (completion-ignore-case t)
  9295. pa answ parent-target child parent old-hist)
  9296. (setq old-hist org-refile-history)
  9297. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9298. nil 'org-refile-history))
  9299. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9300. (org-refile-check-position pa)
  9301. (if pa
  9302. (progn
  9303. (when (or (not org-refile-history)
  9304. (not (eq old-hist org-refile-history))
  9305. (not (equal (car pa) (car org-refile-history))))
  9306. (setq org-refile-history
  9307. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9308. org-refile-history
  9309. (cdr org-refile-history))))
  9310. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9311. (pop org-refile-history)))
  9312. pa)
  9313. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9314. (progn
  9315. (setq parent (match-string 1 answ)
  9316. child (match-string 2 answ))
  9317. (setq parent-target (or (assoc parent tbl)
  9318. (assoc (concat parent "/") tbl)))
  9319. (when (and parent-target
  9320. (or (eq new-nodes t)
  9321. (and (eq new-nodes 'confirm)
  9322. (y-or-n-p (format "Create new node \"%s\"? "
  9323. child)))))
  9324. (org-refile-new-child parent-target child)))
  9325. (error "Invalid target location")))))
  9326. (defun org-refile-check-position (refile-pointer)
  9327. "Check if the refile pointer matches the readline to which it points."
  9328. (let* ((file (nth 1 refile-pointer))
  9329. (re (nth 2 refile-pointer))
  9330. (pos (nth 3 refile-pointer))
  9331. buffer)
  9332. (when (org-string-nw-p re)
  9333. (setq buffer (if (markerp pos)
  9334. (marker-buffer pos)
  9335. (or (find-buffer-visiting file)
  9336. (find-file-noselect file))))
  9337. (with-current-buffer buffer
  9338. (save-excursion
  9339. (save-restriction
  9340. (widen)
  9341. (goto-char pos)
  9342. (beginning-of-line 1)
  9343. (unless (org-looking-at-p re)
  9344. (error "Invalid refile position, please rebuild the cache"))))))))
  9345. (defun org-refile-new-child (parent-target child)
  9346. "Use refile target PARENT-TARGET to add new CHILD below it."
  9347. (unless parent-target
  9348. (error "Cannot find parent for new node"))
  9349. (let ((file (nth 1 parent-target))
  9350. (pos (nth 3 parent-target))
  9351. level)
  9352. (with-current-buffer (or (find-buffer-visiting file)
  9353. (find-file-noselect file))
  9354. (save-excursion
  9355. (save-restriction
  9356. (widen)
  9357. (if pos
  9358. (goto-char pos)
  9359. (goto-char (point-max))
  9360. (if (not (bolp)) (newline)))
  9361. (when (looking-at outline-regexp)
  9362. (setq level (funcall outline-level))
  9363. (org-end-of-subtree t t))
  9364. (org-back-over-empty-lines)
  9365. (insert "\n" (make-string
  9366. (if pos (org-get-valid-level level 1) 1) ?*)
  9367. " " child "\n")
  9368. (beginning-of-line 0)
  9369. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9370. (defun org-olpath-completing-read (prompt collection &rest args)
  9371. "Read an outline path like a file name."
  9372. (let ((thetable collection)
  9373. (org-completion-use-ido nil) ; does not work with ido.
  9374. (org-completion-use-iswitchb nil)) ; or iswitchb
  9375. (apply
  9376. 'org-icompleting-read prompt
  9377. (lambda (string predicate &optional flag)
  9378. (let (rtn r f (l (length string)))
  9379. (cond
  9380. ((eq flag nil)
  9381. ;; try completion
  9382. (try-completion string thetable))
  9383. ((eq flag t)
  9384. ;; all-completions
  9385. (setq rtn (all-completions string thetable predicate))
  9386. (mapcar
  9387. (lambda (x)
  9388. (setq r (substring x l))
  9389. (if (string-match " ([^)]*)$" x)
  9390. (setq f (match-string 0 x))
  9391. (setq f ""))
  9392. (if (string-match "/" r)
  9393. (concat string (substring r 0 (match-end 0)) f)
  9394. x))
  9395. rtn))
  9396. ((eq flag 'lambda)
  9397. ;; exact match?
  9398. (assoc string thetable)))
  9399. ))
  9400. args)))
  9401. ;;;; Dynamic blocks
  9402. (defun org-find-dblock (name)
  9403. "Find the first dynamic block with name NAME in the buffer.
  9404. If not found, stay at current position and return nil."
  9405. (let (pos)
  9406. (save-excursion
  9407. (goto-char (point-min))
  9408. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9409. nil t)
  9410. (match-beginning 0))))
  9411. (if pos (goto-char pos))
  9412. pos))
  9413. (defconst org-dblock-start-re
  9414. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9415. "Matches the start line of a dynamic block, with parameters.")
  9416. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9417. "Matches the end of a dynamic block.")
  9418. (defun org-create-dblock (plist)
  9419. "Create a dynamic block section, with parameters taken from PLIST.
  9420. PLIST must contain a :name entry which is used as name of the block."
  9421. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9422. (end-of-line 1)
  9423. (newline))
  9424. (let ((col (current-column))
  9425. (name (plist-get plist :name)))
  9426. (insert "#+BEGIN: " name)
  9427. (while plist
  9428. (if (eq (car plist) :name)
  9429. (setq plist (cddr plist))
  9430. (insert " " (prin1-to-string (pop plist)))))
  9431. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9432. (beginning-of-line -2)))
  9433. (defun org-prepare-dblock ()
  9434. "Prepare dynamic block for refresh.
  9435. This empties the block, puts the cursor at the insert position and returns
  9436. the property list including an extra property :name with the block name."
  9437. (unless (looking-at org-dblock-start-re)
  9438. (error "Not at a dynamic block"))
  9439. (let* ((begdel (1+ (match-end 0)))
  9440. (name (org-no-properties (match-string 1)))
  9441. (params (append (list :name name)
  9442. (read (concat "(" (match-string 3) ")")))))
  9443. (save-excursion
  9444. (beginning-of-line 1)
  9445. (skip-chars-forward " \t")
  9446. (setq params (plist-put params :indentation-column (current-column))))
  9447. (unless (re-search-forward org-dblock-end-re nil t)
  9448. (error "Dynamic block not terminated"))
  9449. (setq params
  9450. (append params
  9451. (list :content (buffer-substring
  9452. begdel (match-beginning 0)))))
  9453. (delete-region begdel (match-beginning 0))
  9454. (goto-char begdel)
  9455. (open-line 1)
  9456. params))
  9457. (defun org-map-dblocks (&optional command)
  9458. "Apply COMMAND to all dynamic blocks in the current buffer.
  9459. If COMMAND is not given, use `org-update-dblock'."
  9460. (let ((cmd (or command 'org-update-dblock)))
  9461. (save-excursion
  9462. (goto-char (point-min))
  9463. (while (re-search-forward org-dblock-start-re nil t)
  9464. (goto-char (match-beginning 0))
  9465. (save-excursion
  9466. (condition-case nil
  9467. (funcall cmd)
  9468. (error (message "Error during update of dynamic block"))))
  9469. (unless (re-search-forward org-dblock-end-re nil t)
  9470. (error "Dynamic block not terminated"))))))
  9471. (defun org-dblock-update (&optional arg)
  9472. "User command for updating dynamic blocks.
  9473. Update the dynamic block at point. With prefix ARG, update all dynamic
  9474. blocks in the buffer."
  9475. (interactive "P")
  9476. (if arg
  9477. (org-update-all-dblocks)
  9478. (or (looking-at org-dblock-start-re)
  9479. (org-beginning-of-dblock))
  9480. (org-update-dblock)))
  9481. (defun org-update-dblock ()
  9482. "Update the dynamic block at point.
  9483. This means to empty the block, parse for parameters and then call
  9484. the correct writing function."
  9485. (interactive)
  9486. (save-window-excursion
  9487. (let* ((pos (point))
  9488. (line (org-current-line))
  9489. (params (org-prepare-dblock))
  9490. (name (plist-get params :name))
  9491. (indent (plist-get params :indentation-column))
  9492. (cmd (intern (concat "org-dblock-write:" name))))
  9493. (message "Updating dynamic block `%s' at line %d..." name line)
  9494. (funcall cmd params)
  9495. (message "Updating dynamic block `%s' at line %d...done" name line)
  9496. (goto-char pos)
  9497. (when (and indent (> indent 0))
  9498. (setq indent (make-string indent ?\ ))
  9499. (save-excursion
  9500. (org-beginning-of-dblock)
  9501. (forward-line 1)
  9502. (while (not (looking-at org-dblock-end-re))
  9503. (insert indent)
  9504. (beginning-of-line 2))
  9505. (when (looking-at org-dblock-end-re)
  9506. (and (looking-at "[ \t]+")
  9507. (replace-match ""))
  9508. (insert indent)))))))
  9509. (defun org-beginning-of-dblock ()
  9510. "Find the beginning of the dynamic block at point.
  9511. Error if there is no such block at point."
  9512. (let ((pos (point))
  9513. beg)
  9514. (end-of-line 1)
  9515. (if (and (re-search-backward org-dblock-start-re nil t)
  9516. (setq beg (match-beginning 0))
  9517. (re-search-forward org-dblock-end-re nil t)
  9518. (> (match-end 0) pos))
  9519. (goto-char beg)
  9520. (goto-char pos)
  9521. (error "Not in a dynamic block"))))
  9522. (defun org-update-all-dblocks ()
  9523. "Update all dynamic blocks in the buffer.
  9524. This function can be used in a hook."
  9525. (interactive)
  9526. (when (org-mode-p)
  9527. (org-map-dblocks 'org-update-dblock)))
  9528. ;;;; Completion
  9529. (defconst org-additional-option-like-keywords
  9530. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9531. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9532. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9533. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9534. "BEGIN:" "END:"
  9535. "ORGTBL" "TBLFM:" "TBLNAME:"
  9536. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9537. "BEGIN_QUOTE" "END_QUOTE"
  9538. "BEGIN_VERSE" "END_VERSE"
  9539. "BEGIN_CENTER" "END_CENTER"
  9540. "BEGIN_SRC" "END_SRC"
  9541. "BEGIN_RESULT" "END_RESULT"
  9542. "SOURCE:" "SRCNAME:" "FUNCTION:"
  9543. "RESULTS:"
  9544. "HEADER:" "HEADERS:"
  9545. "BABEL:"
  9546. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9547. "CAPTION:" "LABEL:"
  9548. "SETUPFILE:"
  9549. "INCLUDE:"
  9550. "BIND:"
  9551. "MACRO:"))
  9552. (defcustom org-structure-template-alist
  9553. '(
  9554. ("s" "#+begin_src ?\n\n#+end_src"
  9555. "<src lang=\"?\">\n\n</src>")
  9556. ("e" "#+begin_example\n?\n#+end_example"
  9557. "<example>\n?\n</example>")
  9558. ("q" "#+begin_quote\n?\n#+end_quote"
  9559. "<quote>\n?\n</quote>")
  9560. ("v" "#+begin_verse\n?\n#+end_verse"
  9561. "<verse>\n?\n/verse>")
  9562. ("c" "#+begin_center\n?\n#+end_center"
  9563. "<center>\n?\n/center>")
  9564. ("l" "#+begin_latex\n?\n#+end_latex"
  9565. "<literal style=\"latex\">\n?\n</literal>")
  9566. ("L" "#+latex: "
  9567. "<literal style=\"latex\">?</literal>")
  9568. ("h" "#+begin_html\n?\n#+end_html"
  9569. "<literal style=\"html\">\n?\n</literal>")
  9570. ("H" "#+html: "
  9571. "<literal style=\"html\">?</literal>")
  9572. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9573. ("A" "#+ascii: ")
  9574. ("i" "#+include %file ?"
  9575. "<include file=%file markup=\"?\">")
  9576. )
  9577. "Structure completion elements.
  9578. This is a list of abbreviation keys and values. The value gets inserted
  9579. if you type `<' followed by the key and then press the completion key,
  9580. usually `M-TAB'. %file will be replaced by a file name after prompting
  9581. for the file using completion.
  9582. There are two templates for each key, the first uses the original Org syntax,
  9583. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9584. the default when the /org-mtags.el/ module has been loaded. See also the
  9585. variable `org-mtags-prefer-muse-templates'.
  9586. This is an experimental feature, it is undecided if it is going to stay in."
  9587. :group 'org-completion
  9588. :type '(repeat
  9589. (string :tag "Key")
  9590. (string :tag "Template")
  9591. (string :tag "Muse Template")))
  9592. (defun org-try-structure-completion ()
  9593. "Try to complete a structure template before point.
  9594. This looks for strings like \"<e\" on an otherwise empty line and
  9595. expands them."
  9596. (let ((l (buffer-substring (point-at-bol) (point)))
  9597. a)
  9598. (when (and (looking-at "[ \t]*$")
  9599. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9600. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9601. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9602. (match-beginning 1)) a)
  9603. t)))
  9604. (defun org-complete-expand-structure-template (start cell)
  9605. "Expand a structure template."
  9606. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9607. (rpl (nth (if musep 2 1) cell))
  9608. (ind ""))
  9609. (delete-region start (point))
  9610. (when (string-match "\\`#\\+" rpl)
  9611. (cond
  9612. ((bolp))
  9613. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9614. (setq ind (buffer-substring (point-at-bol) (point))))
  9615. (t (newline))))
  9616. (setq start (point))
  9617. (if (string-match "%file" rpl)
  9618. (setq rpl (replace-match
  9619. (concat
  9620. "\""
  9621. (save-match-data
  9622. (abbreviate-file-name (read-file-name "Include file: ")))
  9623. "\"")
  9624. t t rpl)))
  9625. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9626. (concat "\n" ind)))
  9627. (insert rpl)
  9628. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9629. ;;;; TODO, DEADLINE, Comments
  9630. (defun org-toggle-comment ()
  9631. "Change the COMMENT state of an entry."
  9632. (interactive)
  9633. (save-excursion
  9634. (org-back-to-heading)
  9635. (let (case-fold-search)
  9636. (if (looking-at (concat outline-regexp
  9637. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9638. (replace-match "" t t nil 1)
  9639. (if (looking-at outline-regexp)
  9640. (progn
  9641. (goto-char (match-end 0))
  9642. (insert org-comment-string " ")))))))
  9643. (defvar org-last-todo-state-is-todo nil
  9644. "This is non-nil when the last TODO state change led to a TODO state.
  9645. If the last change removed the TODO tag or switched to DONE, then
  9646. this is nil.")
  9647. (defvar org-setting-tags nil) ; dynamically skipped
  9648. (defvar org-todo-setup-filter-hook nil
  9649. "Hook for functions that pre-filter todo specs.
  9650. Each function takes a todo spec and returns either nil or the spec
  9651. transformed into canonical form." )
  9652. (defvar org-todo-get-default-hook nil
  9653. "Hook for functions that get a default item for todo.
  9654. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9655. nil or a string to be used for the todo mark." )
  9656. (defvar org-agenda-headline-snapshot-before-repeat)
  9657. (defun org-todo (&optional arg)
  9658. "Change the TODO state of an item.
  9659. The state of an item is given by a keyword at the start of the heading,
  9660. like
  9661. *** TODO Write paper
  9662. *** DONE Call mom
  9663. The different keywords are specified in the variable `org-todo-keywords'.
  9664. By default the available states are \"TODO\" and \"DONE\".
  9665. So for this example: when the item starts with TODO, it is changed to DONE.
  9666. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9667. DONE are present, add TODO at the beginning of the heading.
  9668. With \\[universal-argument] prefix arg, use completion to determine the new \
  9669. state.
  9670. With numeric prefix arg, switch to that state.
  9671. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9672. keywords (nextset).
  9673. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9674. For calling through lisp, arg is also interpreted in the following way:
  9675. 'none -> empty state
  9676. \"\"(empty string) -> switch to empty state
  9677. 'done -> switch to DONE
  9678. 'nextset -> switch to the next set of keywords
  9679. 'previousset -> switch to the previous set of keywords
  9680. \"WAITING\" -> switch to the specified keyword, but only if it
  9681. really is a member of `org-todo-keywords'."
  9682. (interactive "P")
  9683. (if (equal arg '(16)) (setq arg 'nextset))
  9684. (let ((org-blocker-hook org-blocker-hook)
  9685. (case-fold-search nil))
  9686. (when (equal arg '(64))
  9687. (setq arg nil org-blocker-hook nil))
  9688. (when (and org-blocker-hook
  9689. (or org-inhibit-blocking
  9690. (org-entry-get nil "NOBLOCKING")))
  9691. (setq org-blocker-hook nil))
  9692. (save-excursion
  9693. (catch 'exit
  9694. (org-back-to-heading t)
  9695. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9696. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9697. (looking-at " *"))
  9698. (let* ((match-data (match-data))
  9699. (startpos (point-at-bol))
  9700. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9701. (org-log-done org-log-done)
  9702. (org-log-repeat org-log-repeat)
  9703. (org-todo-log-states org-todo-log-states)
  9704. (this (match-string 1))
  9705. (hl-pos (match-beginning 0))
  9706. (head (org-get-todo-sequence-head this))
  9707. (ass (assoc head org-todo-kwd-alist))
  9708. (interpret (nth 1 ass))
  9709. (done-word (nth 3 ass))
  9710. (final-done-word (nth 4 ass))
  9711. (last-state (or this ""))
  9712. (completion-ignore-case t)
  9713. (member (member this org-todo-keywords-1))
  9714. (tail (cdr member))
  9715. (state (cond
  9716. ((and org-todo-key-trigger
  9717. (or (and (equal arg '(4))
  9718. (eq org-use-fast-todo-selection 'prefix))
  9719. (and (not arg) org-use-fast-todo-selection
  9720. (not (eq org-use-fast-todo-selection
  9721. 'prefix)))))
  9722. ;; Use fast selection
  9723. (org-fast-todo-selection))
  9724. ((and (equal arg '(4))
  9725. (or (not org-use-fast-todo-selection)
  9726. (not org-todo-key-trigger)))
  9727. ;; Read a state with completion
  9728. (org-icompleting-read
  9729. "State: " (mapcar (lambda(x) (list x))
  9730. org-todo-keywords-1)
  9731. nil t))
  9732. ((eq arg 'right)
  9733. (if this
  9734. (if tail (car tail) nil)
  9735. (car org-todo-keywords-1)))
  9736. ((eq arg 'left)
  9737. (if (equal member org-todo-keywords-1)
  9738. nil
  9739. (if this
  9740. (nth (- (length org-todo-keywords-1)
  9741. (length tail) 2)
  9742. org-todo-keywords-1)
  9743. (org-last org-todo-keywords-1))))
  9744. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9745. (setq arg nil))) ; hack to fall back to cycling
  9746. (arg
  9747. ;; user or caller requests a specific state
  9748. (cond
  9749. ((equal arg "") nil)
  9750. ((eq arg 'none) nil)
  9751. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9752. ((eq arg 'nextset)
  9753. (or (car (cdr (member head org-todo-heads)))
  9754. (car org-todo-heads)))
  9755. ((eq arg 'previousset)
  9756. (let ((org-todo-heads (reverse org-todo-heads)))
  9757. (or (car (cdr (member head org-todo-heads)))
  9758. (car org-todo-heads))))
  9759. ((car (member arg org-todo-keywords-1)))
  9760. ((stringp arg)
  9761. (error "State `%s' not valid in this file" arg))
  9762. ((nth (1- (prefix-numeric-value arg))
  9763. org-todo-keywords-1))))
  9764. ((null member) (or head (car org-todo-keywords-1)))
  9765. ((equal this final-done-word) nil) ;; -> make empty
  9766. ((null tail) nil) ;; -> first entry
  9767. ((memq interpret '(type priority))
  9768. (if (eq this-command last-command)
  9769. (car tail)
  9770. (if (> (length tail) 0)
  9771. (or done-word (car org-done-keywords))
  9772. nil)))
  9773. (t
  9774. (car tail))))
  9775. (state (or
  9776. (run-hook-with-args-until-success
  9777. 'org-todo-get-default-hook state last-state)
  9778. state))
  9779. (next (if state (concat " " state " ") " "))
  9780. (change-plist (list :type 'todo-state-change :from this :to state
  9781. :position startpos))
  9782. dolog now-done-p)
  9783. (when org-blocker-hook
  9784. (setq org-last-todo-state-is-todo
  9785. (not (member this org-done-keywords)))
  9786. (unless (save-excursion
  9787. (save-match-data
  9788. (run-hook-with-args-until-failure
  9789. 'org-blocker-hook change-plist)))
  9790. (if (interactive-p)
  9791. (error "TODO state change from %s to %s blocked" this state)
  9792. ;; fail silently
  9793. (message "TODO state change from %s to %s blocked" this state)
  9794. (throw 'exit nil))))
  9795. (store-match-data match-data)
  9796. (replace-match next t t)
  9797. (unless (pos-visible-in-window-p hl-pos)
  9798. (message "TODO state changed to %s" (org-trim next)))
  9799. (unless head
  9800. (setq head (org-get-todo-sequence-head state)
  9801. ass (assoc head org-todo-kwd-alist)
  9802. interpret (nth 1 ass)
  9803. done-word (nth 3 ass)
  9804. final-done-word (nth 4 ass)))
  9805. (when (memq arg '(nextset previousset))
  9806. (message "Keyword-Set %d/%d: %s"
  9807. (- (length org-todo-sets) -1
  9808. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9809. (length org-todo-sets)
  9810. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9811. (setq org-last-todo-state-is-todo
  9812. (not (member state org-done-keywords)))
  9813. (setq now-done-p (and (member state org-done-keywords)
  9814. (not (member this org-done-keywords))))
  9815. (and logging (org-local-logging logging))
  9816. (when (and (or org-todo-log-states org-log-done)
  9817. (not (eq org-inhibit-logging t))
  9818. (not (memq arg '(nextset previousset))))
  9819. ;; we need to look at recording a time and note
  9820. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9821. (nth 2 (assoc this org-todo-log-states))))
  9822. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9823. (setq dolog 'time))
  9824. (when (and state
  9825. (member state org-not-done-keywords)
  9826. (not (member this org-not-done-keywords)))
  9827. ;; This is now a todo state and was not one before
  9828. ;; If there was a CLOSED time stamp, get rid of it.
  9829. (org-add-planning-info nil nil 'closed))
  9830. (when (and now-done-p org-log-done)
  9831. ;; It is now done, and it was not done before
  9832. (org-add-planning-info 'closed (org-current-time))
  9833. (if (and (not dolog) (eq 'note org-log-done))
  9834. (org-add-log-setup 'done state this 'findpos 'note)))
  9835. (when (and state dolog)
  9836. ;; This is a non-nil state, and we need to log it
  9837. (org-add-log-setup 'state state this 'findpos dolog)))
  9838. ;; Fixup tag positioning
  9839. (org-todo-trigger-tag-changes state)
  9840. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9841. (when org-provide-todo-statistics
  9842. (org-update-parent-todo-statistics))
  9843. (run-hooks 'org-after-todo-state-change-hook)
  9844. (if (and arg (not (member state org-done-keywords)))
  9845. (setq head (org-get-todo-sequence-head state)))
  9846. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9847. ;; Do we need to trigger a repeat?
  9848. (when now-done-p
  9849. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9850. ;; This is for the agenda, take a snapshot of the headline.
  9851. (save-match-data
  9852. (setq org-agenda-headline-snapshot-before-repeat
  9853. (org-get-heading))))
  9854. (org-auto-repeat-maybe state))
  9855. ;; Fixup cursor location if close to the keyword
  9856. (if (and (outline-on-heading-p)
  9857. (not (bolp))
  9858. (save-excursion (beginning-of-line 1)
  9859. (looking-at org-todo-line-regexp))
  9860. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9861. (progn
  9862. (goto-char (or (match-end 2) (match-end 1)))
  9863. (and (looking-at " ") (just-one-space))))
  9864. (when org-trigger-hook
  9865. (save-excursion
  9866. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9867. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9868. "Block turning an entry into a TODO, using the hierarchy.
  9869. This checks whether the current task should be blocked from state
  9870. changes. Such blocking occurs when:
  9871. 1. The task has children which are not all in a completed state.
  9872. 2. A task has a parent with the property :ORDERED:, and there
  9873. are siblings prior to the current task with incomplete
  9874. status.
  9875. 3. The parent of the task is blocked because it has siblings that should
  9876. be done first, or is child of a block grandparent TODO entry."
  9877. (if (not org-enforce-todo-dependencies)
  9878. t ; if locally turned off don't block
  9879. (catch 'dont-block
  9880. ;; If this is not a todo state change, or if this entry is already DONE,
  9881. ;; do not block
  9882. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9883. (member (plist-get change-plist :from)
  9884. (cons 'done org-done-keywords))
  9885. (member (plist-get change-plist :to)
  9886. (cons 'todo org-not-done-keywords))
  9887. (not (plist-get change-plist :to)))
  9888. (throw 'dont-block t))
  9889. ;; If this task has children, and any are undone, it's blocked
  9890. (save-excursion
  9891. (org-back-to-heading t)
  9892. (let ((this-level (funcall outline-level)))
  9893. (outline-next-heading)
  9894. (let ((child-level (funcall outline-level)))
  9895. (while (and (not (eobp))
  9896. (> child-level this-level))
  9897. ;; this todo has children, check whether they are all
  9898. ;; completed
  9899. (if (and (not (org-entry-is-done-p))
  9900. (org-entry-is-todo-p))
  9901. (throw 'dont-block nil))
  9902. (outline-next-heading)
  9903. (setq child-level (funcall outline-level))))))
  9904. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9905. ;; any previous siblings are undone, it's blocked
  9906. (save-excursion
  9907. (org-back-to-heading t)
  9908. (let* ((pos (point))
  9909. (parent-pos (and (org-up-heading-safe) (point))))
  9910. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9911. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9912. (forward-line 1)
  9913. (re-search-forward org-not-done-heading-regexp pos t))
  9914. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9915. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9916. (while t
  9917. (goto-char parent-pos)
  9918. (if (not (looking-at org-not-done-heading-regexp))
  9919. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9920. (setq pos (point))
  9921. (setq parent-pos (and (org-up-heading-safe) (point)))
  9922. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9923. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9924. (forward-line 1)
  9925. (re-search-forward org-not-done-heading-regexp pos t))
  9926. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9927. (defcustom org-track-ordered-property-with-tag nil
  9928. "Should the ORDERED property also be shown as a tag?
  9929. The ORDERED property decides if an entry should require subtasks to be
  9930. completed in sequence. Since a property is not very visible, setting
  9931. this option means that toggling the ORDERED property with the command
  9932. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9933. not relevant for the behavior, but it makes things more visible.
  9934. Note that toggling the tag with tags commands will not change the property
  9935. and therefore not influence behavior!
  9936. This can be t, meaning the tag ORDERED should be used, It can also be a
  9937. string to select a different tag for this task."
  9938. :group 'org-todo
  9939. :type '(choice
  9940. (const :tag "No tracking" nil)
  9941. (const :tag "Track with ORDERED tag" t)
  9942. (string :tag "Use other tag")))
  9943. (defun org-toggle-ordered-property ()
  9944. "Toggle the ORDERED property of the current entry.
  9945. For better visibility, you can track the value of this property with a tag.
  9946. See variable `org-track-ordered-property-with-tag'."
  9947. (interactive)
  9948. (let* ((t1 org-track-ordered-property-with-tag)
  9949. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9950. (save-excursion
  9951. (org-back-to-heading)
  9952. (if (org-entry-get nil "ORDERED")
  9953. (progn
  9954. (org-delete-property "ORDERED")
  9955. (and tag (org-toggle-tag tag 'off))
  9956. (message "Subtasks can be completed in arbitrary order"))
  9957. (org-entry-put nil "ORDERED" "t")
  9958. (and tag (org-toggle-tag tag 'on))
  9959. (message "Subtasks must be completed in sequence")))))
  9960. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9961. (defun org-block-todo-from-checkboxes (change-plist)
  9962. "Block turning an entry into a TODO, using checkboxes.
  9963. This checks whether the current task should be blocked from state
  9964. changes because there are unchecked boxes in this entry."
  9965. (if (not org-enforce-todo-checkbox-dependencies)
  9966. t ; if locally turned off don't block
  9967. (catch 'dont-block
  9968. ;; If this is not a todo state change, or if this entry is already DONE,
  9969. ;; do not block
  9970. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9971. (member (plist-get change-plist :from)
  9972. (cons 'done org-done-keywords))
  9973. (member (plist-get change-plist :to)
  9974. (cons 'todo org-not-done-keywords))
  9975. (not (plist-get change-plist :to)))
  9976. (throw 'dont-block t))
  9977. ;; If this task has checkboxes that are not checked, it's blocked
  9978. (save-excursion
  9979. (org-back-to-heading t)
  9980. (let ((beg (point)) end)
  9981. (outline-next-heading)
  9982. (setq end (point))
  9983. (goto-char beg)
  9984. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9985. end t)
  9986. (progn
  9987. (if (boundp 'org-blocked-by-checkboxes)
  9988. (setq org-blocked-by-checkboxes t))
  9989. (throw 'dont-block nil)))))
  9990. t))) ; do not block
  9991. (defun org-entry-blocked-p ()
  9992. "Is the current entry blocked?"
  9993. (if (org-entry-get nil "NOBLOCKING")
  9994. nil ;; Never block this entry
  9995. (not
  9996. (run-hook-with-args-until-failure
  9997. 'org-blocker-hook
  9998. (list :type 'todo-state-change
  9999. :position (point)
  10000. :from 'todo
  10001. :to 'done)))))
  10002. (defun org-update-statistics-cookies (all)
  10003. "Update the statistics cookie, either from TODO or from checkboxes.
  10004. This should be called with the cursor in a line with a statistics cookie."
  10005. (interactive "P")
  10006. (if all
  10007. (progn
  10008. (org-update-checkbox-count 'all)
  10009. (org-map-entries 'org-update-parent-todo-statistics))
  10010. (if (not (org-on-heading-p))
  10011. (org-update-checkbox-count)
  10012. (let ((pos (move-marker (make-marker) (point)))
  10013. end l1 l2)
  10014. (ignore-errors (org-back-to-heading t))
  10015. (if (not (org-on-heading-p))
  10016. (org-update-checkbox-count)
  10017. (setq l1 (org-outline-level))
  10018. (setq end (save-excursion
  10019. (outline-next-heading)
  10020. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10021. (point)))
  10022. (if (and (save-excursion
  10023. (re-search-forward
  10024. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10025. (not (save-excursion (re-search-forward
  10026. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10027. (org-update-checkbox-count)
  10028. (if (and l2 (> l2 l1))
  10029. (progn
  10030. (goto-char end)
  10031. (org-update-parent-todo-statistics))
  10032. (goto-char pos)
  10033. (beginning-of-line 1)
  10034. (while (re-search-forward
  10035. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10036. (point-at-eol) t)
  10037. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10038. (goto-char pos)
  10039. (move-marker pos nil)))))
  10040. (defvar org-entry-property-inherited-from) ;; defined below
  10041. (defun org-update-parent-todo-statistics ()
  10042. "Update any statistics cookie in the parent of the current headline.
  10043. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10044. the entire subtree and this will travel up the hierarchy and update
  10045. statistics everywhere."
  10046. (interactive)
  10047. (let* ((lim 0) prop
  10048. (recursive (or (not org-hierarchical-todo-statistics)
  10049. (string-match
  10050. "\\<recursive\\>"
  10051. (or (setq prop (org-entry-get
  10052. nil "COOKIE_DATA" 'inherit)) ""))))
  10053. (lim (or (and prop (marker-position
  10054. org-entry-property-inherited-from))
  10055. lim))
  10056. (first t)
  10057. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10058. level ltoggle l1 new ndel
  10059. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  10060. (catch 'exit
  10061. (save-excursion
  10062. (beginning-of-line 1)
  10063. (if (org-at-heading-p)
  10064. (setq ltoggle (funcall outline-level))
  10065. (error "This should not happen"))
  10066. (while (and (setq level (org-up-heading-safe))
  10067. (or recursive first)
  10068. (>= (point) lim))
  10069. (setq first nil cookie-present nil)
  10070. (unless (and level
  10071. (not (string-match
  10072. "\\<checkbox\\>"
  10073. (downcase
  10074. (or (org-entry-get
  10075. nil "COOKIE_DATA")
  10076. "")))))
  10077. (throw 'exit nil))
  10078. (while (re-search-forward box-re (point-at-eol) t)
  10079. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10080. (setq is-percent (match-end 2))
  10081. (save-match-data
  10082. (unless (outline-next-heading) (throw 'exit nil))
  10083. (while (and (looking-at org-complex-heading-regexp)
  10084. (> (setq l1 (length (match-string 1))) level))
  10085. (setq kwd (and (or recursive (= l1 ltoggle))
  10086. (match-string 2)))
  10087. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10088. (and (listp org-provide-todo-statistics)
  10089. (or (member kwd org-provide-todo-statistics)
  10090. (member kwd org-done-keywords))))
  10091. (setq cnt-all (1+ cnt-all))
  10092. (if (eq org-provide-todo-statistics t)
  10093. (and kwd (setq cnt-all (1+ cnt-all)))))
  10094. (and (member kwd org-done-keywords)
  10095. (setq cnt-done (1+ cnt-done)))
  10096. (outline-next-heading)))
  10097. (setq new
  10098. (if is-percent
  10099. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10100. (format "[%d/%d]" cnt-done cnt-all))
  10101. ndel (- (match-end 0) (match-beginning 0)))
  10102. (goto-char (match-beginning 0))
  10103. (insert new)
  10104. (delete-region (point) (+ (point) ndel)))
  10105. (when cookie-present
  10106. (run-hook-with-args 'org-after-todo-statistics-hook
  10107. cnt-done (- cnt-all cnt-done))))))
  10108. (run-hooks 'org-todo-statistics-hook)))
  10109. (defvar org-after-todo-statistics-hook nil
  10110. "Hook that is called after a TODO statistics cookie has been updated.
  10111. Each function is called with two arguments: the number of not-done entries
  10112. and the number of done entries.
  10113. For example, the following function, when added to this hook, will switch
  10114. an entry to DONE when all children are done, and back to TODO when new
  10115. entries are set to a TODO status. Note that this hook is only called
  10116. when there is a statistics cookie in the headline!
  10117. (defun org-summary-todo (n-done n-not-done)
  10118. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10119. (let (org-log-done org-log-states) ; turn off logging
  10120. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10121. ")
  10122. (defvar org-todo-statistics-hook nil
  10123. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10124. This hook runs even if there is no statistics cookie present, in which case
  10125. `org-after-todo-statistics-hook' would not run.")
  10126. (defun org-todo-trigger-tag-changes (state)
  10127. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10128. (let ((l org-todo-state-tags-triggers)
  10129. changes)
  10130. (when (or (not state) (equal state ""))
  10131. (setq changes (append changes (cdr (assoc "" l)))))
  10132. (when (and (stringp state) (> (length state) 0))
  10133. (setq changes (append changes (cdr (assoc state l)))))
  10134. (when (member state org-not-done-keywords)
  10135. (setq changes (append changes (cdr (assoc 'todo l)))))
  10136. (when (member state org-done-keywords)
  10137. (setq changes (append changes (cdr (assoc 'done l)))))
  10138. (dolist (c changes)
  10139. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10140. (defun org-local-logging (value)
  10141. "Get logging settings from a property VALUE."
  10142. (let* (words w a)
  10143. ;; directly set the variables, they are already local.
  10144. (setq org-log-done nil
  10145. org-log-repeat nil
  10146. org-todo-log-states nil)
  10147. (setq words (org-split-string value))
  10148. (while (setq w (pop words))
  10149. (cond
  10150. ((setq a (assoc w org-startup-options))
  10151. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10152. (set (nth 1 a) (nth 2 a))))
  10153. ((setq a (org-extract-log-state-settings w))
  10154. (and (member (car a) org-todo-keywords-1)
  10155. (push a org-todo-log-states)))))))
  10156. (defun org-get-todo-sequence-head (kwd)
  10157. "Return the head of the TODO sequence to which KWD belongs.
  10158. If KWD is not set, check if there is a text property remembering the
  10159. right sequence."
  10160. (let (p)
  10161. (cond
  10162. ((not kwd)
  10163. (or (get-text-property (point-at-bol) 'org-todo-head)
  10164. (progn
  10165. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10166. nil (point-at-eol)))
  10167. (get-text-property p 'org-todo-head))))
  10168. ((not (member kwd org-todo-keywords-1))
  10169. (car org-todo-keywords-1))
  10170. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10171. (defun org-fast-todo-selection ()
  10172. "Fast TODO keyword selection with single keys.
  10173. Returns the new TODO keyword, or nil if no state change should occur."
  10174. (let* ((fulltable org-todo-key-alist)
  10175. (done-keywords org-done-keywords) ;; needed for the faces.
  10176. (maxlen (apply 'max (mapcar
  10177. (lambda (x)
  10178. (if (stringp (car x)) (string-width (car x)) 0))
  10179. fulltable)))
  10180. (expert nil)
  10181. (fwidth (+ maxlen 3 1 3))
  10182. (ncol (/ (- (window-width) 4) fwidth))
  10183. tg cnt e c tbl
  10184. groups ingroup)
  10185. (save-excursion
  10186. (save-window-excursion
  10187. (if expert
  10188. (set-buffer (get-buffer-create " *Org todo*"))
  10189. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10190. (erase-buffer)
  10191. (org-set-local 'org-done-keywords done-keywords)
  10192. (setq tbl fulltable cnt 0)
  10193. (while (setq e (pop tbl))
  10194. (cond
  10195. ((equal e '(:startgroup))
  10196. (push '() groups) (setq ingroup t)
  10197. (when (not (= cnt 0))
  10198. (setq cnt 0)
  10199. (insert "\n"))
  10200. (insert "{ "))
  10201. ((equal e '(:endgroup))
  10202. (setq ingroup nil cnt 0)
  10203. (insert "}\n"))
  10204. ((equal e '(:newline))
  10205. (when (not (= cnt 0))
  10206. (setq cnt 0)
  10207. (insert "\n")
  10208. (setq e (car tbl))
  10209. (while (equal (car tbl) '(:newline))
  10210. (insert "\n")
  10211. (setq tbl (cdr tbl)))))
  10212. (t
  10213. (setq tg (car e) c (cdr e))
  10214. (if ingroup (push tg (car groups)))
  10215. (setq tg (org-add-props tg nil 'face
  10216. (org-get-todo-face tg)))
  10217. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10218. (insert "[" c "] " tg (make-string
  10219. (- fwidth 4 (length tg)) ?\ ))
  10220. (when (= (setq cnt (1+ cnt)) ncol)
  10221. (insert "\n")
  10222. (if ingroup (insert " "))
  10223. (setq cnt 0)))))
  10224. (insert "\n")
  10225. (goto-char (point-min))
  10226. (if (not expert) (org-fit-window-to-buffer))
  10227. (message "[a-z..]:Set [SPC]:clear")
  10228. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10229. (cond
  10230. ((or (= c ?\C-g)
  10231. (and (= c ?q) (not (rassoc c fulltable))))
  10232. (setq quit-flag t))
  10233. ((= c ?\ ) nil)
  10234. ((setq e (rassoc c fulltable) tg (car e))
  10235. tg)
  10236. (t (setq quit-flag t)))))))
  10237. (defun org-entry-is-todo-p ()
  10238. (member (org-get-todo-state) org-not-done-keywords))
  10239. (defun org-entry-is-done-p ()
  10240. (member (org-get-todo-state) org-done-keywords))
  10241. (defun org-get-todo-state ()
  10242. (save-excursion
  10243. (org-back-to-heading t)
  10244. (and (looking-at org-todo-line-regexp)
  10245. (match-end 2)
  10246. (match-string 2))))
  10247. (defun org-at-date-range-p (&optional inactive-ok)
  10248. "Is the cursor inside a date range?"
  10249. (interactive)
  10250. (save-excursion
  10251. (catch 'exit
  10252. (let ((pos (point)))
  10253. (skip-chars-backward "^[<\r\n")
  10254. (skip-chars-backward "<[")
  10255. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10256. (>= (match-end 0) pos)
  10257. (throw 'exit t))
  10258. (skip-chars-backward "^<[\r\n")
  10259. (skip-chars-backward "<[")
  10260. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10261. (>= (match-end 0) pos)
  10262. (throw 'exit t)))
  10263. nil)))
  10264. (defun org-get-repeat (&optional tagline)
  10265. "Check if there is a deadline/schedule with repeater in this entry."
  10266. (save-match-data
  10267. (save-excursion
  10268. (org-back-to-heading t)
  10269. (and (re-search-forward (if tagline
  10270. (concat tagline "\\s-*" org-repeat-re)
  10271. org-repeat-re)
  10272. (org-entry-end-position) t)
  10273. (match-string-no-properties 1)))))
  10274. (defvar org-last-changed-timestamp)
  10275. (defvar org-last-inserted-timestamp)
  10276. (defvar org-log-post-message)
  10277. (defvar org-log-note-purpose)
  10278. (defvar org-log-note-how)
  10279. (defvar org-log-note-extra)
  10280. (defun org-auto-repeat-maybe (done-word)
  10281. "Check if the current headline contains a repeated deadline/schedule.
  10282. If yes, set TODO state back to what it was and change the base date
  10283. of repeating deadline/scheduled time stamps to new date.
  10284. This function is run automatically after each state change to a DONE state."
  10285. ;; last-state is dynamically scoped into this function
  10286. (let* ((repeat (org-get-repeat))
  10287. (aa (assoc last-state org-todo-kwd-alist))
  10288. (interpret (nth 1 aa))
  10289. (head (nth 2 aa))
  10290. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10291. (msg "Entry repeats: ")
  10292. (org-log-done nil)
  10293. (org-todo-log-states nil)
  10294. re type n what ts time to-state)
  10295. (when repeat
  10296. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10297. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10298. org-todo-repeat-to-state))
  10299. (unless (and to-state (member to-state org-todo-keywords-1))
  10300. (setq to-state (if (eq interpret 'type) last-state head)))
  10301. (org-todo to-state)
  10302. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10303. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10304. (org-time-stamp-format t t))))
  10305. (when org-log-repeat
  10306. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10307. (memq 'org-add-log-note post-command-hook))
  10308. ;; OK, we are already setup for some record
  10309. (if (eq org-log-repeat 'note)
  10310. ;; make sure we take a note, not only a time stamp
  10311. (setq org-log-note-how 'note))
  10312. ;; Set up for taking a record
  10313. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10314. last-state
  10315. 'findpos org-log-repeat)))
  10316. (org-back-to-heading t)
  10317. (org-add-planning-info nil nil 'closed)
  10318. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10319. org-deadline-time-regexp "\\)\\|\\("
  10320. org-ts-regexp "\\)"))
  10321. (while (re-search-forward
  10322. re (save-excursion (outline-next-heading) (point)) t)
  10323. (setq type (if (match-end 1) org-scheduled-string
  10324. (if (match-end 3) org-deadline-string "Plain:"))
  10325. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10326. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10327. (setq n (string-to-number (match-string 2 ts))
  10328. what (match-string 3 ts))
  10329. (if (equal what "w") (setq n (* n 7) what "d"))
  10330. ;; Preparation, see if we need to modify the start date for the change
  10331. (when (match-end 1)
  10332. (setq time (save-match-data (org-time-string-to-time ts)))
  10333. (cond
  10334. ((equal (match-string 1 ts) ".")
  10335. ;; Shift starting date to today
  10336. (org-timestamp-change
  10337. (- (time-to-days (current-time)) (time-to-days time))
  10338. 'day))
  10339. ((equal (match-string 1 ts) "+")
  10340. (let ((nshiftmax 10) (nshift 0))
  10341. (while (or (= nshift 0)
  10342. (<= (time-to-days time)
  10343. (time-to-days (current-time))))
  10344. (when (= (incf nshift) nshiftmax)
  10345. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10346. (error "Abort")))
  10347. (org-timestamp-change n (cdr (assoc what whata)))
  10348. (org-at-timestamp-p t)
  10349. (setq ts (match-string 1))
  10350. (setq time (save-match-data (org-time-string-to-time ts)))))
  10351. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10352. ;; rematch, so that we have everything in place for the real shift
  10353. (org-at-timestamp-p t)
  10354. (setq ts (match-string 1))
  10355. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10356. (org-timestamp-change n (cdr (assoc what whata)))
  10357. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10358. (setq org-log-post-message msg)
  10359. (message "%s" msg))))
  10360. (defun org-show-todo-tree (arg)
  10361. "Make a compact tree which shows all headlines marked with TODO.
  10362. The tree will show the lines where the regexp matches, and all higher
  10363. headlines above the match.
  10364. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10365. With a numeric prefix N, construct a sparse tree for the Nth element
  10366. of `org-todo-keywords-1'."
  10367. (interactive "P")
  10368. (let ((case-fold-search nil)
  10369. (kwd-re
  10370. (cond ((null arg) org-not-done-regexp)
  10371. ((equal arg '(4))
  10372. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10373. (mapcar 'list org-todo-keywords-1))))
  10374. (concat "\\("
  10375. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10376. "\\)\\>")))
  10377. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10378. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10379. org-todo-keywords-1)))
  10380. (t (error "Invalid prefix argument: %s" arg)))))
  10381. (message "%d TODO entries found"
  10382. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10383. (defun org-deadline (&optional remove time)
  10384. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10385. With argument REMOVE, remove any deadline from the item.
  10386. When TIME is set, it should be an internal time specification, and the
  10387. scheduling will use the corresponding date."
  10388. (interactive "P")
  10389. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10390. (repeater (and old-date
  10391. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10392. (match-string 1 old-date))))
  10393. (if remove
  10394. (progn
  10395. (when (and old-date org-log-redeadline)
  10396. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10397. org-log-redeadline))
  10398. (org-remove-timestamp-with-keyword org-deadline-string)
  10399. (message "Item no longer has a deadline."))
  10400. (org-add-planning-info 'deadline time 'closed)
  10401. (when (and old-date org-log-redeadline
  10402. (not (equal old-date
  10403. (substring org-last-inserted-timestamp 1 -1))))
  10404. (org-add-log-setup 'redeadline nil old-date 'findpos
  10405. org-log-redeadline))
  10406. (when repeater
  10407. (save-excursion
  10408. (org-back-to-heading t)
  10409. (when (re-search-forward (concat org-deadline-string " "
  10410. org-last-inserted-timestamp)
  10411. (save-excursion
  10412. (outline-next-heading) (point)) t)
  10413. (goto-char (1- (match-end 0)))
  10414. (insert " " repeater)
  10415. (setq org-last-inserted-timestamp
  10416. (concat (substring org-last-inserted-timestamp 0 -1)
  10417. " " repeater
  10418. (substring org-last-inserted-timestamp -1))))))
  10419. (message "Deadline on %s" org-last-inserted-timestamp))))
  10420. (defun org-schedule (&optional remove time)
  10421. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10422. With argument REMOVE, remove any scheduling date from the item.
  10423. When TIME is set, it should be an internal time specification, and the
  10424. scheduling will use the corresponding date."
  10425. (interactive "P")
  10426. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10427. (repeater (and old-date
  10428. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10429. (match-string 1 old-date))))
  10430. (if remove
  10431. (progn
  10432. (when (and old-date org-log-reschedule)
  10433. (org-add-log-setup 'delschedule nil old-date 'findpos
  10434. org-log-reschedule))
  10435. (org-remove-timestamp-with-keyword org-scheduled-string)
  10436. (message "Item is no longer scheduled."))
  10437. (org-add-planning-info 'scheduled time 'closed)
  10438. (when (and old-date org-log-reschedule
  10439. (not (equal old-date
  10440. (substring org-last-inserted-timestamp 1 -1))))
  10441. (org-add-log-setup 'reschedule nil old-date 'findpos
  10442. org-log-reschedule))
  10443. (when repeater
  10444. (save-excursion
  10445. (org-back-to-heading t)
  10446. (when (re-search-forward (concat org-scheduled-string " "
  10447. org-last-inserted-timestamp)
  10448. (save-excursion
  10449. (outline-next-heading) (point)) t)
  10450. (goto-char (1- (match-end 0)))
  10451. (insert " " repeater)
  10452. (setq org-last-inserted-timestamp
  10453. (concat (substring org-last-inserted-timestamp 0 -1)
  10454. " " repeater
  10455. (substring org-last-inserted-timestamp -1))))))
  10456. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10457. (defun org-get-scheduled-time (pom &optional inherit)
  10458. "Get the scheduled time as a time tuple, of a format suitable
  10459. for calling org-schedule with, or if there is no scheduling,
  10460. returns nil."
  10461. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10462. (when time
  10463. (apply 'encode-time (org-parse-time-string time)))))
  10464. (defun org-get-deadline-time (pom &optional inherit)
  10465. "Get the deadline as a time tuple, of a format suitable for
  10466. calling org-deadline with, or if there is no scheduling, returns
  10467. nil."
  10468. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10469. (when time
  10470. (apply 'encode-time (org-parse-time-string time)))))
  10471. (defun org-remove-timestamp-with-keyword (keyword)
  10472. "Remove all time stamps with KEYWORD in the current entry."
  10473. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10474. beg)
  10475. (save-excursion
  10476. (org-back-to-heading t)
  10477. (setq beg (point))
  10478. (outline-next-heading)
  10479. (while (re-search-backward re beg t)
  10480. (replace-match "")
  10481. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10482. (equal (char-before) ?\ ))
  10483. (backward-delete-char 1)
  10484. (if (string-match "^[ \t]*$" (buffer-substring
  10485. (point-at-bol) (point-at-eol)))
  10486. (delete-region (point-at-bol)
  10487. (min (point-max) (1+ (point-at-eol))))))))))
  10488. (defun org-add-planning-info (what &optional time &rest remove)
  10489. "Insert new timestamp with keyword in the line directly after the headline.
  10490. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10491. If non is given, the user is prompted for a date.
  10492. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10493. be removed."
  10494. (interactive)
  10495. (let (org-time-was-given org-end-time-was-given ts
  10496. end default-time default-input)
  10497. (catch 'exit
  10498. (when (and (not time) (memq what '(scheduled deadline)))
  10499. ;; Try to get a default date/time from existing timestamp
  10500. (save-excursion
  10501. (org-back-to-heading t)
  10502. (setq end (save-excursion (outline-next-heading) (point)))
  10503. (when (re-search-forward (if (eq what 'scheduled)
  10504. org-scheduled-time-regexp
  10505. org-deadline-time-regexp)
  10506. end t)
  10507. (setq ts (match-string 1)
  10508. default-time
  10509. (apply 'encode-time (org-parse-time-string ts))
  10510. default-input (and ts (org-get-compact-tod ts))))))
  10511. (when what
  10512. ;; If necessary, get the time from the user
  10513. (setq time (or time (org-read-date nil 'to-time nil nil
  10514. default-time default-input))))
  10515. (when (and org-insert-labeled-timestamps-at-point
  10516. (member what '(scheduled deadline)))
  10517. (insert
  10518. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10519. (org-insert-time-stamp time org-time-was-given
  10520. nil nil nil (list org-end-time-was-given))
  10521. (setq what nil))
  10522. (save-excursion
  10523. (save-restriction
  10524. (let (col list elt ts buffer-invisibility-spec)
  10525. (org-back-to-heading t)
  10526. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10527. (goto-char (match-end 1))
  10528. (setq col (current-column))
  10529. (goto-char (match-end 0))
  10530. (if (eobp) (insert "\n") (forward-char 1))
  10531. (when (and (not what)
  10532. (not (looking-at
  10533. (concat "[ \t]*"
  10534. org-keyword-time-not-clock-regexp))))
  10535. ;; Nothing to add, nothing to remove...... :-)
  10536. (throw 'exit nil))
  10537. (if (and (not (looking-at outline-regexp))
  10538. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10539. "[^\r\n]*"))
  10540. (not (equal (match-string 1) org-clock-string)))
  10541. (narrow-to-region (match-beginning 0) (match-end 0))
  10542. (insert-before-markers "\n")
  10543. (backward-char 1)
  10544. (narrow-to-region (point) (point))
  10545. (and org-adapt-indentation (org-indent-to-column col)))
  10546. ;; Check if we have to remove something.
  10547. (setq list (cons what remove))
  10548. (while list
  10549. (setq elt (pop list))
  10550. (goto-char (point-min))
  10551. (when (or (and (eq elt 'scheduled)
  10552. (re-search-forward org-scheduled-time-regexp nil t))
  10553. (and (eq elt 'deadline)
  10554. (re-search-forward org-deadline-time-regexp nil t))
  10555. (and (eq elt 'closed)
  10556. (re-search-forward org-closed-time-regexp nil t)))
  10557. (replace-match "")
  10558. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10559. (skip-chars-backward " ")
  10560. (if (looking-at " +") (replace-match ""))))
  10561. (goto-char (point-max))
  10562. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10563. (when what
  10564. (insert
  10565. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10566. (cond ((eq what 'scheduled) org-scheduled-string)
  10567. ((eq what 'deadline) org-deadline-string)
  10568. ((eq what 'closed) org-closed-string))
  10569. " ")
  10570. (setq ts (org-insert-time-stamp
  10571. time
  10572. (or org-time-was-given
  10573. (and (eq what 'closed) org-log-done-with-time))
  10574. (eq what 'closed)
  10575. nil nil (list org-end-time-was-given)))
  10576. (end-of-line 1))
  10577. (goto-char (point-min))
  10578. (widen)
  10579. (if (and (looking-at "[ \t]*\n")
  10580. (equal (char-before) ?\n))
  10581. (delete-region (1- (point)) (point-at-eol)))
  10582. ts))))))
  10583. (defvar org-log-note-marker (make-marker))
  10584. (defvar org-log-note-purpose nil)
  10585. (defvar org-log-note-state nil)
  10586. (defvar org-log-note-previous-state nil)
  10587. (defvar org-log-note-how nil)
  10588. (defvar org-log-note-extra nil)
  10589. (defvar org-log-note-window-configuration nil)
  10590. (defvar org-log-note-return-to (make-marker))
  10591. (defvar org-log-post-message nil
  10592. "Message to be displayed after a log note has been stored.
  10593. The auto-repeater uses this.")
  10594. (defun org-add-note ()
  10595. "Add a note to the current entry.
  10596. This is done in the same way as adding a state change note."
  10597. (interactive)
  10598. (org-add-log-setup 'note nil nil 'findpos nil))
  10599. (defvar org-property-end-re)
  10600. (defun org-add-log-setup (&optional purpose state prev-state
  10601. findpos how extra)
  10602. "Set up the post command hook to take a note.
  10603. If this is about to TODO state change, the new state is expected in STATE.
  10604. When FINDPOS is non-nil, find the correct position for the note in
  10605. the current entry. If not, assume that it can be inserted at point.
  10606. HOW is an indicator what kind of note should be created.
  10607. EXTRA is additional text that will be inserted into the notes buffer."
  10608. (let* ((org-log-into-drawer (org-log-into-drawer))
  10609. (drawer (cond ((stringp org-log-into-drawer)
  10610. org-log-into-drawer)
  10611. (org-log-into-drawer "LOGBOOK")
  10612. (t nil))))
  10613. (save-restriction
  10614. (save-excursion
  10615. (when findpos
  10616. (org-back-to-heading t)
  10617. (narrow-to-region (point) (save-excursion
  10618. (outline-next-heading) (point)))
  10619. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10620. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10621. "[^\r\n]*\\)?"))
  10622. (goto-char (match-end 0))
  10623. (cond
  10624. (drawer
  10625. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10626. nil t)
  10627. (progn
  10628. (goto-char (match-end 0))
  10629. (or org-log-states-order-reversed
  10630. (and (re-search-forward org-property-end-re nil t)
  10631. (goto-char (1- (match-beginning 0))))))
  10632. (insert "\n:" drawer ":\n:END:")
  10633. (beginning-of-line 0)
  10634. (org-indent-line-function)
  10635. (beginning-of-line 2)
  10636. (org-indent-line-function)
  10637. (end-of-line 0)))
  10638. ((and org-log-state-notes-insert-after-drawers
  10639. (save-excursion
  10640. (forward-line) (looking-at org-drawer-regexp)))
  10641. (forward-line)
  10642. (while (looking-at org-drawer-regexp)
  10643. (goto-char (match-end 0))
  10644. (re-search-forward org-property-end-re (point-max) t)
  10645. (forward-line))
  10646. (forward-line -1)))
  10647. (unless org-log-states-order-reversed
  10648. (and (= (char-after) ?\n) (forward-char 1))
  10649. (org-skip-over-state-notes)
  10650. (skip-chars-backward " \t\n\r")))
  10651. (move-marker org-log-note-marker (point))
  10652. (setq org-log-note-purpose purpose
  10653. org-log-note-state state
  10654. org-log-note-previous-state prev-state
  10655. org-log-note-how how
  10656. org-log-note-extra extra)
  10657. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10658. (defun org-skip-over-state-notes ()
  10659. "Skip past the list of State notes in an entry."
  10660. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10661. (when (org-in-item-p)
  10662. (let ((limit (org-list-bottom-point)))
  10663. (while (looking-at "[ \t]*- State")
  10664. (goto-char (or (org-get-next-item (point) limit)
  10665. (org-get-end-of-item limit)))))))
  10666. (defun org-add-log-note (&optional purpose)
  10667. "Pop up a window for taking a note, and add this note later at point."
  10668. (remove-hook 'post-command-hook 'org-add-log-note)
  10669. (setq org-log-note-window-configuration (current-window-configuration))
  10670. (delete-other-windows)
  10671. (move-marker org-log-note-return-to (point))
  10672. (switch-to-buffer (marker-buffer org-log-note-marker))
  10673. (goto-char org-log-note-marker)
  10674. (org-switch-to-buffer-other-window "*Org Note*")
  10675. (erase-buffer)
  10676. (if (memq org-log-note-how '(time state))
  10677. (let (current-prefix-arg) (org-store-log-note))
  10678. (let ((org-inhibit-startup t)) (org-mode))
  10679. (insert (format "# Insert note for %s.
  10680. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10681. (cond
  10682. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10683. ((eq org-log-note-purpose 'done) "closed todo item")
  10684. ((eq org-log-note-purpose 'state)
  10685. (format "state change from \"%s\" to \"%s\""
  10686. (or org-log-note-previous-state "")
  10687. (or org-log-note-state "")))
  10688. ((eq org-log-note-purpose 'reschedule)
  10689. "rescheduling")
  10690. ((eq org-log-note-purpose 'delschedule)
  10691. "no longer scheduled")
  10692. ((eq org-log-note-purpose 'redeadline)
  10693. "changing deadline")
  10694. ((eq org-log-note-purpose 'deldeadline)
  10695. "removing deadline")
  10696. ((eq org-log-note-purpose 'refile)
  10697. "refiling")
  10698. ((eq org-log-note-purpose 'note)
  10699. "this entry")
  10700. (t (error "This should not happen")))))
  10701. (if org-log-note-extra (insert org-log-note-extra))
  10702. (org-set-local 'org-finish-function 'org-store-log-note)))
  10703. (defvar org-note-abort nil) ; dynamically scoped
  10704. (defun org-store-log-note ()
  10705. "Finish taking a log note, and insert it to where it belongs."
  10706. (let ((txt (buffer-string))
  10707. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10708. lines ind bul)
  10709. (kill-buffer (current-buffer))
  10710. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10711. (setq txt (replace-match "" t t txt)))
  10712. (if (string-match "\\s-+\\'" txt)
  10713. (setq txt (replace-match "" t t txt)))
  10714. (setq lines (org-split-string txt "\n"))
  10715. (when (and note (string-match "\\S-" note))
  10716. (setq note
  10717. (org-replace-escapes
  10718. note
  10719. (list (cons "%u" (user-login-name))
  10720. (cons "%U" user-full-name)
  10721. (cons "%t" (format-time-string
  10722. (org-time-stamp-format 'long 'inactive)
  10723. (current-time)))
  10724. (cons "%T" (format-time-string
  10725. (org-time-stamp-format 'long nil)
  10726. (current-time)))
  10727. (cons "%s" (if org-log-note-state
  10728. (concat "\"" org-log-note-state "\"")
  10729. ""))
  10730. (cons "%S" (if org-log-note-previous-state
  10731. (concat "\"" org-log-note-previous-state "\"")
  10732. "\"\"")))))
  10733. (if lines (setq note (concat note " \\\\")))
  10734. (push note lines))
  10735. (when (or current-prefix-arg org-note-abort)
  10736. (when org-log-into-drawer
  10737. (org-remove-empty-drawer-at
  10738. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10739. org-log-note-marker))
  10740. (setq lines nil))
  10741. (when lines
  10742. (with-current-buffer (marker-buffer org-log-note-marker)
  10743. (save-excursion
  10744. (goto-char org-log-note-marker)
  10745. (move-marker org-log-note-marker nil)
  10746. (end-of-line 1)
  10747. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10748. (setq ind (save-excursion
  10749. (if (org-in-item-p)
  10750. (progn
  10751. (goto-char (org-list-top-point))
  10752. (org-get-indentation))
  10753. (skip-chars-backward " \r\t\n")
  10754. (cond
  10755. ((and (org-at-heading-p)
  10756. org-adapt-indentation)
  10757. (1+ (org-current-level)))
  10758. ((org-at-heading-p) 0)
  10759. (t (org-get-indentation))))))
  10760. (setq bul (org-list-bullet-string "-"))
  10761. (org-indent-line-to ind)
  10762. (insert bul (pop lines))
  10763. (let ((ind-body (+ (length bul) ind)))
  10764. (while lines
  10765. (insert "\n")
  10766. (org-indent-line-to ind-body)
  10767. (insert (pop lines))))
  10768. (message "Note stored")
  10769. (org-back-to-heading t)
  10770. (org-cycle-hide-drawers 'children)))))
  10771. (set-window-configuration org-log-note-window-configuration)
  10772. (with-current-buffer (marker-buffer org-log-note-return-to)
  10773. (goto-char org-log-note-return-to))
  10774. (move-marker org-log-note-return-to nil)
  10775. (and org-log-post-message (message "%s" org-log-post-message)))
  10776. (defun org-remove-empty-drawer-at (drawer pos)
  10777. "Remove an empty drawer DRAWER at position POS.
  10778. POS may also be a marker."
  10779. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10780. (save-excursion
  10781. (save-restriction
  10782. (widen)
  10783. (goto-char pos)
  10784. (if (org-in-regexp
  10785. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10786. (replace-match ""))))))
  10787. (defun org-sparse-tree (&optional arg)
  10788. "Create a sparse tree, prompt for the details.
  10789. This command can create sparse trees. You first need to select the type
  10790. of match used to create the tree:
  10791. t Show all TODO entries.
  10792. T Show entries with a specific TODO keyword.
  10793. m Show entries selected by a tags/property match.
  10794. p Enter a property name and its value (both with completion on existing
  10795. names/values) and show entries with that property.
  10796. r Show entries matching a regular expression (`/' can be used as well)
  10797. d Show deadlines due within `org-deadline-warning-days'.
  10798. b Show deadlines and scheduled items before a date.
  10799. a Show deadlines and scheduled items after a date."
  10800. (interactive "P")
  10801. (let (ans kwd value)
  10802. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  10803. (setq ans (read-char-exclusive))
  10804. (cond
  10805. ((equal ans ?d)
  10806. (call-interactively 'org-check-deadlines))
  10807. ((equal ans ?b)
  10808. (call-interactively 'org-check-before-date))
  10809. ((equal ans ?a)
  10810. (call-interactively 'org-check-after-date))
  10811. ((equal ans ?t)
  10812. (org-show-todo-tree nil))
  10813. ((equal ans ?T)
  10814. (org-show-todo-tree '(4)))
  10815. ((member ans '(?T ?m))
  10816. (call-interactively 'org-match-sparse-tree))
  10817. ((member ans '(?p ?P))
  10818. (setq kwd (org-icompleting-read "Property: "
  10819. (mapcar 'list (org-buffer-property-keys))))
  10820. (setq value (org-icompleting-read "Value: "
  10821. (mapcar 'list (org-property-values kwd))))
  10822. (unless (string-match "\\`{.*}\\'" value)
  10823. (setq value (concat "\"" value "\"")))
  10824. (org-match-sparse-tree arg (concat kwd "=" value)))
  10825. ((member ans '(?r ?R ?/))
  10826. (call-interactively 'org-occur))
  10827. (t (error "No such sparse tree command \"%c\"" ans)))))
  10828. (defvar org-occur-highlights nil
  10829. "List of overlays used for occur matches.")
  10830. (make-variable-buffer-local 'org-occur-highlights)
  10831. (defvar org-occur-parameters nil
  10832. "Parameters of the active org-occur calls.
  10833. This is a list, each call to org-occur pushes as cons cell,
  10834. containing the regular expression and the callback, onto the list.
  10835. The list can contain several entries if `org-occur' has been called
  10836. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10837. will only contain one set of parameters. When the highlights are
  10838. removed (for example with `C-c C-c', or with the next edit (depending
  10839. on `org-remove-highlights-with-change'), this variable is emptied
  10840. as well.")
  10841. (make-variable-buffer-local 'org-occur-parameters)
  10842. (defun org-occur (regexp &optional keep-previous callback)
  10843. "Make a compact tree which shows all matches of REGEXP.
  10844. The tree will show the lines where the regexp matches, and all higher
  10845. headlines above the match. It will also show the heading after the match,
  10846. to make sure editing the matching entry is easy.
  10847. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10848. call to `org-occur' will be kept, to allow stacking of calls to this
  10849. command.
  10850. If CALLBACK is non-nil, it is a function which is called to confirm
  10851. that the match should indeed be shown."
  10852. (interactive "sRegexp: \nP")
  10853. (when (equal regexp "")
  10854. (error "Regexp cannot be empty"))
  10855. (unless keep-previous
  10856. (org-remove-occur-highlights nil nil t))
  10857. (push (cons regexp callback) org-occur-parameters)
  10858. (let ((cnt 0))
  10859. (save-excursion
  10860. (goto-char (point-min))
  10861. (if (or (not keep-previous) ; do not want to keep
  10862. (not org-occur-highlights)) ; no previous matches
  10863. ;; hide everything
  10864. (org-overview))
  10865. (while (re-search-forward regexp nil t)
  10866. (when (or (not callback)
  10867. (save-match-data (funcall callback)))
  10868. (setq cnt (1+ cnt))
  10869. (when org-highlight-sparse-tree-matches
  10870. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10871. (org-show-context 'occur-tree))))
  10872. (when org-remove-highlights-with-change
  10873. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10874. nil 'local))
  10875. (unless org-sparse-tree-open-archived-trees
  10876. (org-hide-archived-subtrees (point-min) (point-max)))
  10877. (run-hooks 'org-occur-hook)
  10878. (if (interactive-p)
  10879. (message "%d match(es) for regexp %s" cnt regexp))
  10880. cnt))
  10881. (defun org-show-context (&optional key)
  10882. "Make sure point and context are visible.
  10883. How much context is shown depends upon the variables
  10884. `org-show-hierarchy-above', `org-show-following-heading'. and
  10885. `org-show-siblings'."
  10886. (let ((heading-p (org-on-heading-p t))
  10887. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10888. (following-p (org-get-alist-option org-show-following-heading key))
  10889. (entry-p (org-get-alist-option org-show-entry-below key))
  10890. (siblings-p (org-get-alist-option org-show-siblings key)))
  10891. (catch 'exit
  10892. ;; Show heading or entry text
  10893. (if (and heading-p (not entry-p))
  10894. (org-flag-heading nil) ; only show the heading
  10895. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10896. (org-show-hidden-entry))) ; show entire entry
  10897. (when following-p
  10898. ;; Show next sibling, or heading below text
  10899. (save-excursion
  10900. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10901. (org-flag-heading nil))))
  10902. (when siblings-p (org-show-siblings))
  10903. (when hierarchy-p
  10904. ;; show all higher headings, possibly with siblings
  10905. (save-excursion
  10906. (while (and (condition-case nil
  10907. (progn (org-up-heading-all 1) t)
  10908. (error nil))
  10909. (not (bobp)))
  10910. (org-flag-heading nil)
  10911. (when siblings-p (org-show-siblings))))))))
  10912. (defvar org-reveal-start-hook nil
  10913. "Hook run before revealing a location.")
  10914. (defun org-reveal (&optional siblings)
  10915. "Show current entry, hierarchy above it, and the following headline.
  10916. This can be used to show a consistent set of context around locations
  10917. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10918. not t for the search context.
  10919. With optional argument SIBLINGS, on each level of the hierarchy all
  10920. siblings are shown. This repairs the tree structure to what it would
  10921. look like when opened with hierarchical calls to `org-cycle'.
  10922. With double optional argument \\[universal-argument] \\[universal-argument], \
  10923. go to the parent and show the
  10924. entire tree."
  10925. (interactive "P")
  10926. (run-hooks 'org-reveal-start-hook)
  10927. (let ((org-show-hierarchy-above t)
  10928. (org-show-following-heading t)
  10929. (org-show-siblings (if siblings t org-show-siblings)))
  10930. (org-show-context nil))
  10931. (when (equal siblings '(16))
  10932. (save-excursion
  10933. (when (org-up-heading-safe)
  10934. (org-show-subtree)
  10935. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10936. (defun org-highlight-new-match (beg end)
  10937. "Highlight from BEG to END and mark the highlight is an occur headline."
  10938. (let ((ov (make-overlay beg end)))
  10939. (overlay-put ov 'face 'secondary-selection)
  10940. (push ov org-occur-highlights)))
  10941. (defun org-remove-occur-highlights (&optional beg end noremove)
  10942. "Remove the occur highlights from the buffer.
  10943. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10944. from the `before-change-functions' in the current buffer."
  10945. (interactive)
  10946. (unless org-inhibit-highlight-removal
  10947. (mapc 'delete-overlay org-occur-highlights)
  10948. (setq org-occur-highlights nil)
  10949. (setq org-occur-parameters nil)
  10950. (unless noremove
  10951. (remove-hook 'before-change-functions
  10952. 'org-remove-occur-highlights 'local))))
  10953. ;;;; Priorities
  10954. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10955. "Regular expression matching the priority indicator.")
  10956. (defvar org-remove-priority-next-time nil)
  10957. (defun org-priority-up ()
  10958. "Increase the priority of the current item."
  10959. (interactive)
  10960. (org-priority 'up))
  10961. (defun org-priority-down ()
  10962. "Decrease the priority of the current item."
  10963. (interactive)
  10964. (org-priority 'down))
  10965. (defun org-priority (&optional action)
  10966. "Change the priority of an item by ARG.
  10967. ACTION can be `set', `up', `down', or a character."
  10968. (interactive)
  10969. (unless org-enable-priority-commands
  10970. (error "Priority commands are disabled"))
  10971. (setq action (or action 'set))
  10972. (let (current new news have remove)
  10973. (save-excursion
  10974. (org-back-to-heading t)
  10975. (if (looking-at org-priority-regexp)
  10976. (setq current (string-to-char (match-string 2))
  10977. have t)
  10978. (setq current org-default-priority))
  10979. (cond
  10980. ((eq action 'remove)
  10981. (setq remove t new ?\ ))
  10982. ((or (eq action 'set)
  10983. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10984. (if (not (eq action 'set))
  10985. (setq new action)
  10986. (message "Priority %c-%c, SPC to remove: "
  10987. org-highest-priority org-lowest-priority)
  10988. (save-match-data
  10989. (setq new (read-char-exclusive))))
  10990. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10991. (= (upcase org-lowest-priority) org-lowest-priority))
  10992. (setq new (upcase new)))
  10993. (cond ((equal new ?\ ) (setq remove t))
  10994. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10995. (error "Priority must be between `%c' and `%c'"
  10996. org-highest-priority org-lowest-priority))))
  10997. ((eq action 'up)
  10998. (if (and (not have) (eq last-command this-command))
  10999. (setq new org-lowest-priority)
  11000. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11001. org-default-priority (1- current)))))
  11002. ((eq action 'down)
  11003. (if (and (not have) (eq last-command this-command))
  11004. (setq new org-highest-priority)
  11005. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11006. org-default-priority (1+ current)))))
  11007. (t (error "Invalid action")))
  11008. (if (or (< (upcase new) org-highest-priority)
  11009. (> (upcase new) org-lowest-priority))
  11010. (setq remove t))
  11011. (setq news (format "%c" new))
  11012. (if have
  11013. (if remove
  11014. (replace-match "" t t nil 1)
  11015. (replace-match news t t nil 2))
  11016. (if remove
  11017. (error "No priority cookie found in line")
  11018. (let ((case-fold-search nil))
  11019. (looking-at org-todo-line-regexp))
  11020. (if (match-end 2)
  11021. (progn
  11022. (goto-char (match-end 2))
  11023. (insert " [#" news "]"))
  11024. (goto-char (match-beginning 3))
  11025. (insert "[#" news "] "))))
  11026. (org-preserve-lc (org-set-tags nil 'align)))
  11027. (if remove
  11028. (message "Priority removed")
  11029. (message "Priority of current item set to %s" news))))
  11030. (defun org-get-priority (s)
  11031. "Find priority cookie and return priority."
  11032. (if (functionp org-get-priority-function)
  11033. (funcall org-get-priority-function)
  11034. (save-match-data
  11035. (if (not (string-match org-priority-regexp s))
  11036. (* 1000 (- org-lowest-priority org-default-priority))
  11037. (* 1000 (- org-lowest-priority
  11038. (string-to-char (match-string 2 s))))))))
  11039. ;;;; Tags
  11040. (defvar org-agenda-archives-mode)
  11041. (defvar org-map-continue-from nil
  11042. "Position from where mapping should continue.
  11043. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11044. (defvar org-scanner-tags nil
  11045. "The current tag list while the tags scanner is running.")
  11046. (defvar org-trust-scanner-tags nil
  11047. "Should `org-get-tags-at' use the tags fro the scanner.
  11048. This is for internal dynamical scoping only.
  11049. When this is non-nil, the function `org-get-tags-at' will return the value
  11050. of `org-scanner-tags' instead of building the list by itself. This
  11051. can lead to large speed-ups when the tags scanner is used in a file with
  11052. many entries, and when the list of tags is retrieved, for example to
  11053. obtain a list of properties. Building the tags list for each entry in such
  11054. a file becomes an N^2 operation - but with this variable set, it scales
  11055. as N.")
  11056. (defun org-scan-tags (action matcher &optional todo-only)
  11057. "Scan headline tags with inheritance and produce output ACTION.
  11058. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11059. or `agenda' to produce an entry list for an agenda view. It can also be
  11060. a Lisp form or a function that should be called at each matched headline, in
  11061. this case the return value is a list of all return values from these calls.
  11062. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11063. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11064. only lines with a TODO keyword are included in the output."
  11065. (require 'org-agenda)
  11066. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11067. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11068. (org-re
  11069. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11070. (props (list 'face 'default
  11071. 'done-face 'org-agenda-done
  11072. 'undone-face 'default
  11073. 'mouse-face 'highlight
  11074. 'org-not-done-regexp org-not-done-regexp
  11075. 'org-todo-regexp org-todo-regexp
  11076. 'help-echo
  11077. (format "mouse-2 or RET jump to org file %s"
  11078. (abbreviate-file-name
  11079. (or (buffer-file-name (buffer-base-buffer))
  11080. (buffer-name (buffer-base-buffer)))))))
  11081. (case-fold-search nil)
  11082. (org-map-continue-from nil)
  11083. lspos tags tags-list
  11084. (tags-alist (list (cons 0 org-file-tags)))
  11085. (llast 0) rtn rtn1 level category i txt
  11086. todo marker entry priority)
  11087. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11088. (setq action (list 'lambda nil action)))
  11089. (save-excursion
  11090. (goto-char (point-min))
  11091. (when (eq action 'sparse-tree)
  11092. (org-overview)
  11093. (org-remove-occur-highlights))
  11094. (while (re-search-forward re nil t)
  11095. (catch :skip
  11096. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11097. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11098. (goto-char (setq lspos (match-beginning 0)))
  11099. (setq level (org-reduced-level (funcall outline-level))
  11100. category (org-get-category))
  11101. (setq i llast llast level)
  11102. ;; remove tag lists from same and sublevels
  11103. (while (>= i level)
  11104. (when (setq entry (assoc i tags-alist))
  11105. (setq tags-alist (delete entry tags-alist)))
  11106. (setq i (1- i)))
  11107. ;; add the next tags
  11108. (when tags
  11109. (setq tags (org-split-string tags ":")
  11110. tags-alist
  11111. (cons (cons level tags) tags-alist)))
  11112. ;; compile tags for current headline
  11113. (setq tags-list
  11114. (if org-use-tag-inheritance
  11115. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11116. tags)
  11117. org-scanner-tags tags-list)
  11118. (when org-use-tag-inheritance
  11119. (setcdr (car tags-alist)
  11120. (mapcar (lambda (x)
  11121. (setq x (copy-sequence x))
  11122. (org-add-prop-inherited x))
  11123. (cdar tags-alist))))
  11124. (when (and tags org-use-tag-inheritance
  11125. (or (not (eq t org-use-tag-inheritance))
  11126. org-tags-exclude-from-inheritance))
  11127. ;; selective inheritance, remove uninherited ones
  11128. (setcdr (car tags-alist)
  11129. (org-remove-uniherited-tags (cdar tags-alist))))
  11130. (when (and (or (not todo-only)
  11131. (and (member todo org-not-done-keywords)
  11132. (or (not org-agenda-tags-todo-honor-ignore-options)
  11133. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11134. (let ((case-fold-search t)) (eval matcher))
  11135. (or
  11136. (not (member org-archive-tag tags-list))
  11137. ;; we have an archive tag, should we use this anyway?
  11138. (or (not org-agenda-skip-archived-trees)
  11139. (and (eq action 'agenda) org-agenda-archives-mode))))
  11140. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11141. ;; select this headline
  11142. (cond
  11143. ((eq action 'sparse-tree)
  11144. (and org-highlight-sparse-tree-matches
  11145. (org-get-heading) (match-end 0)
  11146. (org-highlight-new-match
  11147. (match-beginning 0) (match-beginning 1)))
  11148. (org-show-context 'tags-tree))
  11149. ((eq action 'agenda)
  11150. (setq txt (org-format-agenda-item
  11151. ""
  11152. (concat
  11153. (if (eq org-tags-match-list-sublevels 'indented)
  11154. (make-string (1- level) ?.) "")
  11155. (org-get-heading))
  11156. category
  11157. tags-list
  11158. )
  11159. priority (org-get-priority txt))
  11160. (goto-char lspos)
  11161. (setq marker (org-agenda-new-marker))
  11162. (org-add-props txt props
  11163. 'org-marker marker 'org-hd-marker marker 'org-category category
  11164. 'todo-state todo
  11165. 'priority priority 'type "tagsmatch")
  11166. (push txt rtn))
  11167. ((functionp action)
  11168. (setq org-map-continue-from nil)
  11169. (save-excursion
  11170. (setq rtn1 (funcall action))
  11171. (push rtn1 rtn)))
  11172. (t (error "Invalid action")))
  11173. ;; if we are to skip sublevels, jump to end of subtree
  11174. (unless org-tags-match-list-sublevels
  11175. (org-end-of-subtree t)
  11176. (backward-char 1))))
  11177. ;; Get the correct position from where to continue
  11178. (if org-map-continue-from
  11179. (goto-char org-map-continue-from)
  11180. (and (= (point) lspos) (end-of-line 1)))))
  11181. (when (and (eq action 'sparse-tree)
  11182. (not org-sparse-tree-open-archived-trees))
  11183. (org-hide-archived-subtrees (point-min) (point-max)))
  11184. (nreverse rtn)))
  11185. (defun org-remove-uniherited-tags (tags)
  11186. "Remove all tags that are not inherited from the list TAGS."
  11187. (cond
  11188. ((eq org-use-tag-inheritance t)
  11189. (if org-tags-exclude-from-inheritance
  11190. (org-delete-all org-tags-exclude-from-inheritance tags)
  11191. tags))
  11192. ((not org-use-tag-inheritance) nil)
  11193. ((stringp org-use-tag-inheritance)
  11194. (delq nil (mapcar
  11195. (lambda (x)
  11196. (if (and (string-match org-use-tag-inheritance x)
  11197. (not (member x org-tags-exclude-from-inheritance)))
  11198. x nil))
  11199. tags)))
  11200. ((listp org-use-tag-inheritance)
  11201. (delq nil (mapcar
  11202. (lambda (x)
  11203. (if (member x org-use-tag-inheritance) x nil))
  11204. tags)))))
  11205. (defvar todo-only) ;; dynamically scoped
  11206. (defun org-match-sparse-tree (&optional todo-only match)
  11207. "Create a sparse tree according to tags string MATCH.
  11208. MATCH can contain positive and negative selection of tags, like
  11209. \"+WORK+URGENT-WITHBOSS\".
  11210. If optional argument TODO-ONLY is non-nil, only select lines that are
  11211. also TODO lines."
  11212. (interactive "P")
  11213. (org-prepare-agenda-buffers (list (current-buffer)))
  11214. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11215. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11216. (defvar org-cached-props nil)
  11217. (defun org-cached-entry-get (pom property)
  11218. (if (or (eq t org-use-property-inheritance)
  11219. (and (stringp org-use-property-inheritance)
  11220. (string-match org-use-property-inheritance property))
  11221. (and (listp org-use-property-inheritance)
  11222. (member property org-use-property-inheritance)))
  11223. ;; Caching is not possible, check it directly
  11224. (org-entry-get pom property 'inherit)
  11225. ;; Get all properties, so that we can do complicated checks easily
  11226. (cdr (assoc property (or org-cached-props
  11227. (setq org-cached-props
  11228. (org-entry-properties pom)))))))
  11229. (defun org-global-tags-completion-table (&optional files)
  11230. "Return the list of all tags in all agenda buffer/files."
  11231. (save-excursion
  11232. (org-uniquify
  11233. (delq nil
  11234. (apply 'append
  11235. (mapcar
  11236. (lambda (file)
  11237. (set-buffer (find-file-noselect file))
  11238. (append (org-get-buffer-tags)
  11239. (mapcar (lambda (x) (if (stringp (car-safe x))
  11240. (list (car-safe x)) nil))
  11241. org-tag-alist)))
  11242. (if (and files (car files))
  11243. files
  11244. (org-agenda-files))))))))
  11245. (defun org-make-tags-matcher (match)
  11246. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11247. ;; todo-only is scoped dynamically into this function, and the function
  11248. ;; may change it if the matcher asks for it.
  11249. (unless match
  11250. ;; Get a new match request, with completion
  11251. (let ((org-last-tags-completion-table
  11252. (org-global-tags-completion-table)))
  11253. (setq match (org-completing-read-no-i
  11254. "Match: " 'org-tags-completion-function nil nil nil
  11255. 'org-tags-history))))
  11256. ;; Parse the string and create a lisp form
  11257. (let ((match0 match)
  11258. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11259. minus tag mm
  11260. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11261. orterms term orlist re-p str-p level-p level-op time-p
  11262. prop-p pn pv po cat-p gv rest)
  11263. (if (string-match "/+" match)
  11264. ;; match contains also a todo-matching request
  11265. (progn
  11266. (setq tagsmatch (substring match 0 (match-beginning 0))
  11267. todomatch (substring match (match-end 0)))
  11268. (if (string-match "^!" todomatch)
  11269. (setq todo-only t todomatch (substring todomatch 1)))
  11270. (if (string-match "^\\s-*$" todomatch)
  11271. (setq todomatch nil)))
  11272. ;; only matching tags
  11273. (setq tagsmatch match todomatch nil))
  11274. ;; Make the tags matcher
  11275. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11276. (setq tagsmatcher t)
  11277. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11278. (while (setq term (pop orterms))
  11279. (while (and (equal (substring term -1) "\\") orterms)
  11280. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11281. (while (string-match re term)
  11282. (setq rest (substring term (match-end 0))
  11283. minus (and (match-end 1)
  11284. (equal (match-string 1 term) "-"))
  11285. tag (save-match-data (replace-regexp-in-string
  11286. "\\\\-" "-"
  11287. (match-string 2 term)))
  11288. re-p (equal (string-to-char tag) ?{)
  11289. level-p (match-end 4)
  11290. prop-p (match-end 5)
  11291. mm (cond
  11292. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11293. (level-p
  11294. (setq level-op (org-op-to-function (match-string 3 term)))
  11295. `(,level-op level ,(string-to-number
  11296. (match-string 4 term))))
  11297. (prop-p
  11298. (setq pn (match-string 5 term)
  11299. po (match-string 6 term)
  11300. pv (match-string 7 term)
  11301. cat-p (equal pn "CATEGORY")
  11302. re-p (equal (string-to-char pv) ?{)
  11303. str-p (equal (string-to-char pv) ?\")
  11304. time-p (save-match-data
  11305. (string-match "^\"[[<].*[]>]\"$" pv))
  11306. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11307. (if time-p (setq pv (org-matcher-time pv)))
  11308. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11309. (cond
  11310. ((equal pn "CATEGORY")
  11311. (setq gv '(get-text-property (point) 'org-category)))
  11312. ((equal pn "TODO")
  11313. (setq gv 'todo))
  11314. (t
  11315. (setq gv `(org-cached-entry-get nil ,pn))))
  11316. (if re-p
  11317. (if (eq po 'org<>)
  11318. `(not (string-match ,pv (or ,gv "")))
  11319. `(string-match ,pv (or ,gv "")))
  11320. (if str-p
  11321. `(,po (or ,gv "") ,pv)
  11322. `(,po (string-to-number (or ,gv ""))
  11323. ,(string-to-number pv) ))))
  11324. (t `(member ,tag tags-list)))
  11325. mm (if minus (list 'not mm) mm)
  11326. term rest)
  11327. (push mm tagsmatcher))
  11328. (push (if (> (length tagsmatcher) 1)
  11329. (cons 'and tagsmatcher)
  11330. (car tagsmatcher))
  11331. orlist)
  11332. (setq tagsmatcher nil))
  11333. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11334. (setq tagsmatcher
  11335. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11336. ;; Make the todo matcher
  11337. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11338. (setq todomatcher t)
  11339. (setq orterms (org-split-string todomatch "|") orlist nil)
  11340. (while (setq term (pop orterms))
  11341. (while (string-match re term)
  11342. (setq minus (and (match-end 1)
  11343. (equal (match-string 1 term) "-"))
  11344. kwd (match-string 2 term)
  11345. re-p (equal (string-to-char kwd) ?{)
  11346. term (substring term (match-end 0))
  11347. mm (if re-p
  11348. `(string-match ,(substring kwd 1 -1) todo)
  11349. (list 'equal 'todo kwd))
  11350. mm (if minus (list 'not mm) mm))
  11351. (push mm todomatcher))
  11352. (push (if (> (length todomatcher) 1)
  11353. (cons 'and todomatcher)
  11354. (car todomatcher))
  11355. orlist)
  11356. (setq todomatcher nil))
  11357. (setq todomatcher (if (> (length orlist) 1)
  11358. (cons 'or orlist) (car orlist))))
  11359. ;; Return the string and lisp forms of the matcher
  11360. (setq matcher (if todomatcher
  11361. (list 'and tagsmatcher todomatcher)
  11362. tagsmatcher))
  11363. (cons match0 matcher)))
  11364. (defun org-op-to-function (op &optional stringp)
  11365. "Turn an operator into the appropriate function."
  11366. (setq op
  11367. (cond
  11368. ((equal op "<" ) '(< string< org-time<))
  11369. ((equal op ">" ) '(> org-string> org-time>))
  11370. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11371. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11372. ((member op '("=" "==")) '(= string= org-time=))
  11373. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11374. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11375. (defun org<> (a b) (not (= a b)))
  11376. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11377. (defun org-string>= (a b) (not (string< a b)))
  11378. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11379. (defun org-string<> (a b) (not (string= a b)))
  11380. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11381. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11382. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11383. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11384. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11385. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11386. (defun org-2ft (s)
  11387. "Convert S to a floating point time.
  11388. If S is already a number, just return it. If it is a string, parse
  11389. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11390. (cond
  11391. ((numberp s) s)
  11392. ((stringp s)
  11393. (condition-case nil
  11394. (float-time (apply 'encode-time (org-parse-time-string s)))
  11395. (error 0.)))
  11396. (t 0.)))
  11397. (defun org-time-today ()
  11398. "Time in seconds today at 0:00.
  11399. Returns the float number of seconds since the beginning of the
  11400. epoch to the beginning of today (00:00)."
  11401. (float-time (apply 'encode-time
  11402. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11403. (defun org-matcher-time (s)
  11404. "Interpret a time comparison value."
  11405. (save-match-data
  11406. (cond
  11407. ((string= s "<now>") (float-time))
  11408. ((string= s "<today>") (org-time-today))
  11409. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11410. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11411. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11412. (+ (org-time-today)
  11413. (* (string-to-number (match-string 1 s))
  11414. (cdr (assoc (match-string 2 s)
  11415. '(("d" . 86400.0) ("w" . 604800.0)
  11416. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11417. (t (org-2ft s)))))
  11418. (defun org-match-any-p (re list)
  11419. "Does re match any element of list?"
  11420. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11421. (delq nil list))
  11422. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11423. (defvar org-tags-overlay (make-overlay 1 1))
  11424. (org-detach-overlay org-tags-overlay)
  11425. (defun org-get-local-tags-at (&optional pos)
  11426. "Get a list of tags defined in the current headline."
  11427. (org-get-tags-at pos 'local))
  11428. (defun org-get-local-tags ()
  11429. "Get a list of tags defined in the current headline."
  11430. (org-get-tags-at nil 'local))
  11431. (defun org-get-tags-at (&optional pos local)
  11432. "Get a list of all headline tags applicable at POS.
  11433. POS defaults to point. If tags are inherited, the list contains
  11434. the targets in the same sequence as the headlines appear, i.e.
  11435. the tags of the current headline come last.
  11436. When LOCAL is non-nil, only return tags from the current headline,
  11437. ignore inherited ones."
  11438. (interactive)
  11439. (if (and org-trust-scanner-tags
  11440. (or (not pos) (equal pos (point)))
  11441. (not local))
  11442. org-scanner-tags
  11443. (let (tags ltags lastpos parent)
  11444. (save-excursion
  11445. (save-restriction
  11446. (widen)
  11447. (goto-char (or pos (point)))
  11448. (save-match-data
  11449. (catch 'done
  11450. (condition-case nil
  11451. (progn
  11452. (org-back-to-heading t)
  11453. (while (not (equal lastpos (point)))
  11454. (setq lastpos (point))
  11455. (when (looking-at
  11456. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11457. (setq ltags (org-split-string
  11458. (org-match-string-no-properties 1) ":"))
  11459. (when parent
  11460. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11461. (setq tags (append
  11462. (if parent
  11463. (org-remove-uniherited-tags ltags)
  11464. ltags)
  11465. tags)))
  11466. (or org-use-tag-inheritance (throw 'done t))
  11467. (if local (throw 'done t))
  11468. (or (org-up-heading-safe) (error nil))
  11469. (setq parent t)))
  11470. (error nil)))))
  11471. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11472. (defun org-add-prop-inherited (s)
  11473. (add-text-properties 0 (length s) '(inherited t) s)
  11474. s)
  11475. (defun org-toggle-tag (tag &optional onoff)
  11476. "Toggle the tag TAG for the current line.
  11477. If ONOFF is `on' or `off', don't toggle but set to this state."
  11478. (let (res current)
  11479. (save-excursion
  11480. (org-back-to-heading t)
  11481. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11482. (point-at-eol) t)
  11483. (progn
  11484. (setq current (match-string 1))
  11485. (replace-match ""))
  11486. (setq current ""))
  11487. (setq current (nreverse (org-split-string current ":")))
  11488. (cond
  11489. ((eq onoff 'on)
  11490. (setq res t)
  11491. (or (member tag current) (push tag current)))
  11492. ((eq onoff 'off)
  11493. (or (not (member tag current)) (setq current (delete tag current))))
  11494. (t (if (member tag current)
  11495. (setq current (delete tag current))
  11496. (setq res t)
  11497. (push tag current))))
  11498. (end-of-line 1)
  11499. (if current
  11500. (progn
  11501. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11502. (org-set-tags nil t))
  11503. (delete-horizontal-space))
  11504. (run-hooks 'org-after-tags-change-hook))
  11505. res))
  11506. (defun org-align-tags-here (to-col)
  11507. ;; Assumes that this is a headline
  11508. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11509. (beginning-of-line 1)
  11510. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11511. (< pos (match-beginning 2)))
  11512. (progn
  11513. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11514. (goto-char (match-beginning 1))
  11515. (insert " ")
  11516. (delete-region (point) (1+ (match-beginning 2)))
  11517. (setq ncol (max (1+ (current-column))
  11518. (1+ col)
  11519. (if (> to-col 0)
  11520. to-col
  11521. (- (abs to-col) tags-l))))
  11522. (setq p (point))
  11523. (insert (make-string (- ncol (current-column)) ?\ ))
  11524. (setq ncol (current-column))
  11525. (when indent-tabs-mode (tabify p (point-at-eol)))
  11526. (org-move-to-column (min ncol col) t))
  11527. (goto-char pos))))
  11528. (defun org-set-tags-command (&optional arg just-align)
  11529. "Call the set-tags command for the current entry."
  11530. (interactive "P")
  11531. (if (org-on-heading-p)
  11532. (org-set-tags arg just-align)
  11533. (save-excursion
  11534. (org-back-to-heading t)
  11535. (org-set-tags arg just-align))))
  11536. (defun org-set-tags-to (data)
  11537. "Set the tags of the current entry to DATA, replacing the current tags.
  11538. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11539. If DATA is nil or the empty string, any tags will be removed."
  11540. (interactive "sTags: ")
  11541. (setq data
  11542. (cond
  11543. ((eq data nil) "")
  11544. ((equal data "") "")
  11545. ((stringp data)
  11546. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11547. ":"))
  11548. ((listp data)
  11549. (concat ":" (mapconcat 'identity data ":") ":"))
  11550. (t nil)))
  11551. (when data
  11552. (save-excursion
  11553. (org-back-to-heading t)
  11554. (when (looking-at org-complex-heading-regexp)
  11555. (if (match-end 5)
  11556. (progn
  11557. (goto-char (match-beginning 5))
  11558. (insert data)
  11559. (delete-region (point) (point-at-eol))
  11560. (org-set-tags nil 'align))
  11561. (goto-char (point-at-eol))
  11562. (insert " " data)
  11563. (org-set-tags nil 'align)))
  11564. (beginning-of-line 1)
  11565. (if (looking-at ".*?\\([ \t]+\\)$")
  11566. (delete-region (match-beginning 1) (match-end 1))))))
  11567. (defun org-align-all-tags ()
  11568. "Align the tags i all headings."
  11569. (interactive)
  11570. (save-excursion
  11571. (or (ignore-errors (org-back-to-heading t))
  11572. (outline-next-heading))
  11573. (if (org-on-heading-p)
  11574. (org-set-tags t)
  11575. (message "No headings"))))
  11576. (defvar org-indent-indentation-per-level)
  11577. (defun org-set-tags (&optional arg just-align)
  11578. "Set the tags for the current headline.
  11579. With prefix ARG, realign all tags in headings in the current buffer."
  11580. (interactive "P")
  11581. (let* ((re (concat "^" outline-regexp))
  11582. (current (org-get-tags-string))
  11583. (col (current-column))
  11584. (org-setting-tags t)
  11585. table current-tags inherited-tags ; computed below when needed
  11586. tags p0 c0 c1 rpl di tc level)
  11587. (if arg
  11588. (save-excursion
  11589. (goto-char (point-min))
  11590. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11591. (while (re-search-forward re nil t)
  11592. (org-set-tags nil t)
  11593. (end-of-line 1)))
  11594. (message "All tags realigned to column %d" org-tags-column))
  11595. (if just-align
  11596. (setq tags current)
  11597. ;; Get a new set of tags from the user
  11598. (save-excursion
  11599. (setq table (append org-tag-persistent-alist
  11600. (or org-tag-alist (org-get-buffer-tags))
  11601. (and
  11602. org-complete-tags-always-offer-all-agenda-tags
  11603. (org-global-tags-completion-table
  11604. (org-agenda-files))))
  11605. org-last-tags-completion-table table
  11606. current-tags (org-split-string current ":")
  11607. inherited-tags (nreverse
  11608. (nthcdr (length current-tags)
  11609. (nreverse (org-get-tags-at))))
  11610. tags
  11611. (if (or (eq t org-use-fast-tag-selection)
  11612. (and org-use-fast-tag-selection
  11613. (delq nil (mapcar 'cdr table))))
  11614. (org-fast-tag-selection
  11615. current-tags inherited-tags table
  11616. (if org-fast-tag-selection-include-todo
  11617. org-todo-key-alist))
  11618. (let ((org-add-colon-after-tag-completion t))
  11619. (org-trim
  11620. (org-without-partial-completion
  11621. (org-icompleting-read "Tags: "
  11622. 'org-tags-completion-function
  11623. nil nil current 'org-tags-history)))))))
  11624. (while (string-match "[-+&]+" tags)
  11625. ;; No boolean logic, just a list
  11626. (setq tags (replace-match ":" t t tags))))
  11627. (setq tags (replace-regexp-in-string "[ ,]" ":" tags))
  11628. (if org-tags-sort-function
  11629. (setq tags (mapconcat 'identity
  11630. (sort (org-split-string
  11631. tags (org-re "[^[:alnum:]_@#%]+"))
  11632. org-tags-sort-function) ":")))
  11633. (if (string-match "\\`[\t ]*\\'" tags)
  11634. (setq tags "")
  11635. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11636. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11637. ;; Insert new tags at the correct column
  11638. (beginning-of-line 1)
  11639. (setq level (or (and (looking-at org-outline-regexp)
  11640. (- (match-end 0) (point) 1))
  11641. 1))
  11642. (cond
  11643. ((and (equal current "") (equal tags "")))
  11644. ((re-search-forward
  11645. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11646. (point-at-eol) t)
  11647. (if (equal tags "")
  11648. (setq rpl "")
  11649. (goto-char (match-beginning 0))
  11650. (setq c0 (current-column)
  11651. ;; compute offset for the case of org-indent-mode active
  11652. di (if org-indent-mode
  11653. (* (1- org-indent-indentation-per-level) (1- level))
  11654. 0)
  11655. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11656. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11657. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11658. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11659. (replace-match rpl t t)
  11660. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11661. tags)
  11662. (t (error "Tags alignment failed")))
  11663. (org-move-to-column col)
  11664. (unless just-align
  11665. (run-hooks 'org-after-tags-change-hook)))))
  11666. (defun org-change-tag-in-region (beg end tag off)
  11667. "Add or remove TAG for each entry in the region.
  11668. This works in the agenda, and also in an org-mode buffer."
  11669. (interactive
  11670. (list (region-beginning) (region-end)
  11671. (let ((org-last-tags-completion-table
  11672. (if (org-mode-p)
  11673. (org-get-buffer-tags)
  11674. (org-global-tags-completion-table))))
  11675. (org-icompleting-read
  11676. "Tag: " 'org-tags-completion-function nil nil nil
  11677. 'org-tags-history))
  11678. (progn
  11679. (message "[s]et or [r]emove? ")
  11680. (equal (read-char-exclusive) ?r))))
  11681. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11682. (let ((agendap (equal major-mode 'org-agenda-mode))
  11683. l1 l2 m buf pos newhead (cnt 0))
  11684. (goto-char end)
  11685. (setq l2 (1- (org-current-line)))
  11686. (goto-char beg)
  11687. (setq l1 (org-current-line))
  11688. (loop for l from l1 to l2 do
  11689. (org-goto-line l)
  11690. (setq m (get-text-property (point) 'org-hd-marker))
  11691. (when (or (and (org-mode-p) (org-on-heading-p))
  11692. (and agendap m))
  11693. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11694. pos (if agendap m (point)))
  11695. (with-current-buffer buf
  11696. (save-excursion
  11697. (save-restriction
  11698. (goto-char pos)
  11699. (setq cnt (1+ cnt))
  11700. (org-toggle-tag tag (if off 'off 'on))
  11701. (setq newhead (org-get-heading)))))
  11702. (and agendap (org-agenda-change-all-lines newhead m))))
  11703. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11704. (defun org-tags-completion-function (string predicate &optional flag)
  11705. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11706. (confirm (lambda (x) (stringp (car x)))))
  11707. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11708. (setq s1 (match-string 1 string)
  11709. s2 (match-string 2 string))
  11710. (setq s1 "" s2 string))
  11711. (cond
  11712. ((eq flag nil)
  11713. ;; try completion
  11714. (setq rtn (try-completion s2 ctable confirm))
  11715. (if (stringp rtn)
  11716. (setq rtn
  11717. (concat s1 s2 (substring rtn (length s2))
  11718. (if (and org-add-colon-after-tag-completion
  11719. (assoc rtn ctable))
  11720. ":" ""))))
  11721. rtn)
  11722. ((eq flag t)
  11723. ;; all-completions
  11724. (all-completions s2 ctable confirm)
  11725. )
  11726. ((eq flag 'lambda)
  11727. ;; exact match?
  11728. (assoc s2 ctable)))
  11729. ))
  11730. (defun org-fast-tag-insert (kwd tags face &optional end)
  11731. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11732. (insert (format "%-12s" (concat kwd ":"))
  11733. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11734. (or end "")))
  11735. (defun org-fast-tag-show-exit (flag)
  11736. (save-excursion
  11737. (org-goto-line 3)
  11738. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11739. (replace-match ""))
  11740. (when flag
  11741. (end-of-line 1)
  11742. (org-move-to-column (- (window-width) 19) t)
  11743. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11744. (defun org-set-current-tags-overlay (current prefix)
  11745. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11746. (if (featurep 'xemacs)
  11747. (org-overlay-display org-tags-overlay (concat prefix s)
  11748. 'secondary-selection)
  11749. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11750. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11751. (defvar org-last-tag-selection-key nil)
  11752. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11753. "Fast tag selection with single keys.
  11754. CURRENT is the current list of tags in the headline, INHERITED is the
  11755. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11756. possibly with grouping information. TODO-TABLE is a similar table with
  11757. TODO keywords, should these have keys assigned to them.
  11758. If the keys are nil, a-z are automatically assigned.
  11759. Returns the new tags string, or nil to not change the current settings."
  11760. (let* ((fulltable (append table todo-table))
  11761. (maxlen (apply 'max (mapcar
  11762. (lambda (x)
  11763. (if (stringp (car x)) (string-width (car x)) 0))
  11764. fulltable)))
  11765. (buf (current-buffer))
  11766. (expert (eq org-fast-tag-selection-single-key 'expert))
  11767. (buffer-tags nil)
  11768. (fwidth (+ maxlen 3 1 3))
  11769. (ncol (/ (- (window-width) 4) fwidth))
  11770. (i-face 'org-done)
  11771. (c-face 'org-todo)
  11772. tg cnt e c char c1 c2 ntable tbl rtn
  11773. ov-start ov-end ov-prefix
  11774. (exit-after-next org-fast-tag-selection-single-key)
  11775. (done-keywords org-done-keywords)
  11776. groups ingroup)
  11777. (save-excursion
  11778. (beginning-of-line 1)
  11779. (if (looking-at
  11780. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11781. (setq ov-start (match-beginning 1)
  11782. ov-end (match-end 1)
  11783. ov-prefix "")
  11784. (setq ov-start (1- (point-at-eol))
  11785. ov-end (1+ ov-start))
  11786. (skip-chars-forward "^\n\r")
  11787. (setq ov-prefix
  11788. (concat
  11789. (buffer-substring (1- (point)) (point))
  11790. (if (> (current-column) org-tags-column)
  11791. " "
  11792. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11793. (move-overlay org-tags-overlay ov-start ov-end)
  11794. (save-window-excursion
  11795. (if expert
  11796. (set-buffer (get-buffer-create " *Org tags*"))
  11797. (delete-other-windows)
  11798. (split-window-vertically)
  11799. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11800. (erase-buffer)
  11801. (org-set-local 'org-done-keywords done-keywords)
  11802. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11803. (org-fast-tag-insert "Current" current c-face "\n\n")
  11804. (org-fast-tag-show-exit exit-after-next)
  11805. (org-set-current-tags-overlay current ov-prefix)
  11806. (setq tbl fulltable char ?a cnt 0)
  11807. (while (setq e (pop tbl))
  11808. (cond
  11809. ((equal (car e) :startgroup)
  11810. (push '() groups) (setq ingroup t)
  11811. (when (not (= cnt 0))
  11812. (setq cnt 0)
  11813. (insert "\n"))
  11814. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11815. ((equal (car e) :endgroup)
  11816. (setq ingroup nil cnt 0)
  11817. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11818. ((equal e '(:newline))
  11819. (when (not (= cnt 0))
  11820. (setq cnt 0)
  11821. (insert "\n")
  11822. (setq e (car tbl))
  11823. (while (equal (car tbl) '(:newline))
  11824. (insert "\n")
  11825. (setq tbl (cdr tbl)))))
  11826. (t
  11827. (setq tg (copy-sequence (car e)) c2 nil)
  11828. (if (cdr e)
  11829. (setq c (cdr e))
  11830. ;; automatically assign a character.
  11831. (setq c1 (string-to-char
  11832. (downcase (substring
  11833. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11834. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11835. (while (or (rassoc char ntable) (rassoc char table))
  11836. (setq char (1+ char)))
  11837. (setq c2 c1))
  11838. (setq c (or c2 char)))
  11839. (if ingroup (push tg (car groups)))
  11840. (setq tg (org-add-props tg nil 'face
  11841. (cond
  11842. ((not (assoc tg table))
  11843. (org-get-todo-face tg))
  11844. ((member tg current) c-face)
  11845. ((member tg inherited) i-face)
  11846. (t nil))))
  11847. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11848. (insert "[" c "] " tg (make-string
  11849. (- fwidth 4 (length tg)) ?\ ))
  11850. (push (cons tg c) ntable)
  11851. (when (= (setq cnt (1+ cnt)) ncol)
  11852. (insert "\n")
  11853. (if ingroup (insert " "))
  11854. (setq cnt 0)))))
  11855. (setq ntable (nreverse ntable))
  11856. (insert "\n")
  11857. (goto-char (point-min))
  11858. (if (not expert) (org-fit-window-to-buffer))
  11859. (setq rtn
  11860. (catch 'exit
  11861. (while t
  11862. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11863. (if (not groups) "no " "")
  11864. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11865. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11866. (setq org-last-tag-selection-key c)
  11867. (cond
  11868. ((= c ?\r) (throw 'exit t))
  11869. ((= c ?!)
  11870. (setq groups (not groups))
  11871. (goto-char (point-min))
  11872. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11873. ((= c ?\C-c)
  11874. (if (not expert)
  11875. (org-fast-tag-show-exit
  11876. (setq exit-after-next (not exit-after-next)))
  11877. (setq expert nil)
  11878. (delete-other-windows)
  11879. (split-window-vertically)
  11880. (org-switch-to-buffer-other-window " *Org tags*")
  11881. (org-fit-window-to-buffer)))
  11882. ((or (= c ?\C-g)
  11883. (and (= c ?q) (not (rassoc c ntable))))
  11884. (org-detach-overlay org-tags-overlay)
  11885. (setq quit-flag t))
  11886. ((= c ?\ )
  11887. (setq current nil)
  11888. (if exit-after-next (setq exit-after-next 'now)))
  11889. ((= c ?\t)
  11890. (condition-case nil
  11891. (setq tg (org-icompleting-read
  11892. "Tag: "
  11893. (or buffer-tags
  11894. (with-current-buffer buf
  11895. (org-get-buffer-tags)))))
  11896. (quit (setq tg "")))
  11897. (when (string-match "\\S-" tg)
  11898. (add-to-list 'buffer-tags (list tg))
  11899. (if (member tg current)
  11900. (setq current (delete tg current))
  11901. (push tg current)))
  11902. (if exit-after-next (setq exit-after-next 'now)))
  11903. ((setq e (rassoc c todo-table) tg (car e))
  11904. (with-current-buffer buf
  11905. (save-excursion (org-todo tg)))
  11906. (if exit-after-next (setq exit-after-next 'now)))
  11907. ((setq e (rassoc c ntable) tg (car e))
  11908. (if (member tg current)
  11909. (setq current (delete tg current))
  11910. (loop for g in groups do
  11911. (if (member tg g)
  11912. (mapc (lambda (x)
  11913. (setq current (delete x current)))
  11914. g)))
  11915. (push tg current))
  11916. (if exit-after-next (setq exit-after-next 'now))))
  11917. ;; Create a sorted list
  11918. (setq current
  11919. (sort current
  11920. (lambda (a b)
  11921. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11922. (if (eq exit-after-next 'now) (throw 'exit t))
  11923. (goto-char (point-min))
  11924. (beginning-of-line 2)
  11925. (delete-region (point) (point-at-eol))
  11926. (org-fast-tag-insert "Current" current c-face)
  11927. (org-set-current-tags-overlay current ov-prefix)
  11928. (while (re-search-forward
  11929. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  11930. (setq tg (match-string 1))
  11931. (add-text-properties
  11932. (match-beginning 1) (match-end 1)
  11933. (list 'face
  11934. (cond
  11935. ((member tg current) c-face)
  11936. ((member tg inherited) i-face)
  11937. (t (get-text-property (match-beginning 1) 'face))))))
  11938. (goto-char (point-min)))))
  11939. (org-detach-overlay org-tags-overlay)
  11940. (if rtn
  11941. (mapconcat 'identity current ":")
  11942. nil))))
  11943. (defun org-get-tags-string ()
  11944. "Get the TAGS string in the current headline."
  11945. (unless (org-on-heading-p t)
  11946. (error "Not on a heading"))
  11947. (save-excursion
  11948. (beginning-of-line 1)
  11949. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11950. (org-match-string-no-properties 1)
  11951. "")))
  11952. (defun org-get-tags ()
  11953. "Get the list of tags specified in the current headline."
  11954. (org-split-string (org-get-tags-string) ":"))
  11955. (defun org-get-buffer-tags ()
  11956. "Get a table of all tags used in the buffer, for completion."
  11957. (let (tags)
  11958. (save-excursion
  11959. (goto-char (point-min))
  11960. (while (re-search-forward
  11961. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  11962. (when (equal (char-after (point-at-bol 0)) ?*)
  11963. (mapc (lambda (x) (add-to-list 'tags x))
  11964. (org-split-string (org-match-string-no-properties 1) ":")))))
  11965. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11966. (mapcar 'list tags)))
  11967. ;;;; The mapping API
  11968. ;;;###autoload
  11969. (defun org-map-entries (func &optional match scope &rest skip)
  11970. "Call FUNC at each headline selected by MATCH in SCOPE.
  11971. FUNC is a function or a lisp form. The function will be called without
  11972. arguments, with the cursor positioned at the beginning of the headline.
  11973. The return values of all calls to the function will be collected and
  11974. returned as a list.
  11975. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11976. does not need to preserve point. After evaluation, the cursor will be
  11977. moved to the end of the line (presumably of the headline of the
  11978. processed entry) and search continues from there. Under some
  11979. circumstances, this may not produce the wanted results. For example,
  11980. if you have removed (e.g. archived) the current (sub)tree it could
  11981. mean that the next entry will be skipped entirely. In such cases, you
  11982. can specify the position from where search should continue by making
  11983. FUNC set the variable `org-map-continue-from' to the desired buffer
  11984. position.
  11985. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11986. Only headlines that are matched by this query will be considered during
  11987. the iteration. When MATCH is nil or t, all headlines will be
  11988. visited by the iteration.
  11989. SCOPE determines the scope of this command. It can be any of:
  11990. nil The current buffer, respecting the restriction if any
  11991. tree The subtree started with the entry at point
  11992. file The current buffer, without restriction
  11993. file-with-archives
  11994. The current buffer, and any archives associated with it
  11995. agenda All agenda files
  11996. agenda-with-archives
  11997. All agenda files with any archive files associated with them
  11998. \(file1 file2 ...)
  11999. If this is a list, all files in the list will be scanned
  12000. The remaining args are treated as settings for the skipping facilities of
  12001. the scanner. The following items can be given here:
  12002. archive skip trees with the archive tag.
  12003. comment skip trees with the COMMENT keyword
  12004. function or Emacs Lisp form:
  12005. will be used as value for `org-agenda-skip-function', so whenever
  12006. the function returns t, FUNC will not be called for that
  12007. entry and search will continue from the point where the
  12008. function leaves it.
  12009. If your function needs to retrieve the tags including inherited tags
  12010. at the *current* entry, you can use the value of the variable
  12011. `org-scanner-tags' which will be much faster than getting the value
  12012. with `org-get-tags-at'. If your function gets properties with
  12013. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12014. to t around the call to `org-entry-properties' to get the same speedup.
  12015. Note that if your function moves around to retrieve tags and properties at
  12016. a *different* entry, you cannot use these techniques."
  12017. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12018. (org-agenda-skip-archived-trees (memq 'archive skip))
  12019. (org-agenda-skip-comment-trees (memq 'comment skip))
  12020. (org-agenda-skip-function
  12021. (car (org-delete-all '(comment archive) skip)))
  12022. (org-tags-match-list-sublevels t)
  12023. matcher file res
  12024. org-todo-keywords-for-agenda
  12025. org-done-keywords-for-agenda
  12026. org-todo-keyword-alist-for-agenda
  12027. org-drawers-for-agenda
  12028. org-tag-alist-for-agenda)
  12029. (cond
  12030. ((eq match t) (setq matcher t))
  12031. ((eq match nil) (setq matcher t))
  12032. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12033. (save-excursion
  12034. (save-restriction
  12035. (when (eq scope 'tree)
  12036. (org-back-to-heading t)
  12037. (org-narrow-to-subtree)
  12038. (setq scope nil))
  12039. (if (not scope)
  12040. (progn
  12041. (org-prepare-agenda-buffers
  12042. (list (buffer-file-name (current-buffer))))
  12043. (setq res (org-scan-tags func matcher)))
  12044. ;; Get the right scope
  12045. (cond
  12046. ((and scope (listp scope) (symbolp (car scope)))
  12047. (setq scope (eval scope)))
  12048. ((eq scope 'agenda)
  12049. (setq scope (org-agenda-files t)))
  12050. ((eq scope 'agenda-with-archives)
  12051. (setq scope (org-agenda-files t))
  12052. (setq scope (org-add-archive-files scope)))
  12053. ((eq scope 'file)
  12054. (setq scope (list (buffer-file-name))))
  12055. ((eq scope 'file-with-archives)
  12056. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12057. (org-prepare-agenda-buffers scope)
  12058. (while (setq file (pop scope))
  12059. (with-current-buffer (org-find-base-buffer-visiting file)
  12060. (save-excursion
  12061. (save-restriction
  12062. (widen)
  12063. (goto-char (point-min))
  12064. (setq res (append res (org-scan-tags func matcher))))))))))
  12065. res))
  12066. ;;;; Properties
  12067. ;;; Setting and retrieving properties
  12068. (defconst org-special-properties
  12069. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12070. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  12071. "The special properties valid in Org-mode.
  12072. These are properties that are not defined in the property drawer,
  12073. but in some other way.")
  12074. (defconst org-default-properties
  12075. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12076. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12077. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12078. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12079. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12080. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12081. "Some properties that are used by Org-mode for various purposes.
  12082. Being in this list makes sure that they are offered for completion.")
  12083. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12084. "Regular expression matching the first line of a property drawer.")
  12085. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12086. "Regular expression matching the last line of a property drawer.")
  12087. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12088. "Regular expression matching the first line of a property drawer.")
  12089. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12090. "Regular expression matching the first line of a property drawer.")
  12091. (defconst org-property-drawer-re
  12092. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12093. org-property-end-re "\\)\n?")
  12094. "Matches an entire property drawer.")
  12095. (defconst org-clock-drawer-re
  12096. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12097. org-property-end-re "\\)\n?")
  12098. "Matches an entire clock drawer.")
  12099. (defun org-property-action ()
  12100. "Do an action on properties."
  12101. (interactive)
  12102. (let (c)
  12103. (org-at-property-p)
  12104. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12105. (setq c (read-char-exclusive))
  12106. (cond
  12107. ((equal c ?s)
  12108. (call-interactively 'org-set-property))
  12109. ((equal c ?d)
  12110. (call-interactively 'org-delete-property))
  12111. ((equal c ?D)
  12112. (call-interactively 'org-delete-property-globally))
  12113. ((equal c ?c)
  12114. (call-interactively 'org-compute-property-at-point))
  12115. (t (error "No such property action %c" c)))))
  12116. (defun org-set-effort (&optional value)
  12117. "Set the effort property of the current entry.
  12118. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12119. allowed value."
  12120. (interactive "P")
  12121. (if (equal value 0) (setq value 10))
  12122. (let* ((completion-ignore-case t)
  12123. (prop org-effort-property)
  12124. (cur (org-entry-get nil prop))
  12125. (allowed (org-property-get-allowed-values nil prop 'table))
  12126. (existing (mapcar 'list (org-property-values prop)))
  12127. rpl
  12128. (val (cond
  12129. ((stringp value) value)
  12130. ((and allowed (integerp value))
  12131. (or (car (nth (1- value) allowed))
  12132. (car (org-last allowed))))
  12133. (allowed
  12134. (message "Select 1-9,0, [RET%s]: %s"
  12135. (if cur (concat "=" cur) "")
  12136. (mapconcat 'car allowed " "))
  12137. (setq rpl (read-char-exclusive))
  12138. (if (equal rpl ?\r)
  12139. cur
  12140. (setq rpl (- rpl ?0))
  12141. (if (equal rpl 0) (setq rpl 10))
  12142. (if (and (> rpl 0) (<= rpl (length allowed)))
  12143. (car (nth (1- rpl) allowed))
  12144. (org-completing-read "Effort: " allowed nil))))
  12145. (t
  12146. (let (org-completion-use-ido org-completion-use-iswitchb)
  12147. (org-completing-read
  12148. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12149. (concat "[" cur "]") "")
  12150. ": ")
  12151. existing nil nil "" nil cur))))))
  12152. (unless (equal (org-entry-get nil prop) val)
  12153. (org-entry-put nil prop val))
  12154. (message "%s is now %s" prop val)))
  12155. (defun org-at-property-p ()
  12156. "Is cursor inside a property drawer?"
  12157. (save-excursion
  12158. (beginning-of-line 1)
  12159. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12160. (save-match-data ;; Used by calling procedures
  12161. (let ((p (point))
  12162. (range (unless (org-before-first-heading-p)
  12163. (org-get-property-block))))
  12164. (and range (<= (car range) p) (< p (cdr range))))))))
  12165. (defun org-get-property-block (&optional beg end force)
  12166. "Return the (beg . end) range of the body of the property drawer.
  12167. BEG and END can be beginning and end of subtree, if not given
  12168. they will be found.
  12169. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12170. (catch 'exit
  12171. (save-excursion
  12172. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12173. (end (or end (progn (outline-next-heading) (point)))))
  12174. (goto-char beg)
  12175. (if (re-search-forward org-property-start-re end t)
  12176. (setq beg (1+ (match-end 0)))
  12177. (if force
  12178. (save-excursion
  12179. (org-insert-property-drawer)
  12180. (setq end (progn (outline-next-heading) (point))))
  12181. (throw 'exit nil))
  12182. (goto-char beg)
  12183. (if (re-search-forward org-property-start-re end t)
  12184. (setq beg (1+ (match-end 0)))))
  12185. (if (re-search-forward org-property-end-re end t)
  12186. (setq end (match-beginning 0))
  12187. (or force (throw 'exit nil))
  12188. (goto-char beg)
  12189. (setq end beg)
  12190. (org-indent-line-function)
  12191. (insert ":END:\n"))
  12192. (cons beg end)))))
  12193. (defun org-entry-properties (&optional pom which specific)
  12194. "Get all properties of the entry at point-or-marker POM.
  12195. This includes the TODO keyword, the tags, time strings for deadline,
  12196. scheduled, and clocking, and any additional properties defined in the
  12197. entry. The return value is an alist, keys may occur multiple times
  12198. if the property key was used several times.
  12199. POM may also be nil, in which case the current entry is used.
  12200. If WHICH is nil or `all', get all properties. If WHICH is
  12201. `special' or `standard', only get that subclass. If WHICH
  12202. is a string only get exactly this property. Specific can be a string, the
  12203. specific property we are interested in. Specifying it can speed
  12204. things up because then unnecessary parsing is avoided."
  12205. (setq which (or which 'all))
  12206. (org-with-point-at pom
  12207. (let ((clockstr (substring org-clock-string 0 -1))
  12208. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12209. (case-fold-search nil)
  12210. beg end range props sum-props key key1 value string clocksum)
  12211. (save-excursion
  12212. (when (condition-case nil
  12213. (and (org-mode-p) (org-back-to-heading t))
  12214. (error nil))
  12215. (setq beg (point))
  12216. (setq sum-props (get-text-property (point) 'org-summaries))
  12217. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12218. (outline-next-heading)
  12219. (setq end (point))
  12220. (when (memq which '(all special))
  12221. ;; Get the special properties, like TODO and tags
  12222. (goto-char beg)
  12223. (when (and (or (not specific) (string= specific "TODO"))
  12224. (looking-at org-todo-line-regexp) (match-end 2))
  12225. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12226. (when (and (or (not specific) (string= specific "PRIORITY"))
  12227. (looking-at org-priority-regexp))
  12228. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12229. (when (and (or (not specific) (string= specific "TAGS"))
  12230. (setq value (org-get-tags-string))
  12231. (string-match "\\S-" value))
  12232. (push (cons "TAGS" value) props))
  12233. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12234. (setq value (org-get-tags-at)))
  12235. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12236. ":"))
  12237. props))
  12238. (when (or (not specific) (string= specific "BLOCKED"))
  12239. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12240. (when (or (not specific)
  12241. (member specific
  12242. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12243. "TIMESTAMP" "TIMESTAMP_IA")))
  12244. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12245. (setq key (if (match-end 1)
  12246. (substring (org-match-string-no-properties 1)
  12247. 0 -1))
  12248. string (if (equal key clockstr)
  12249. (org-no-properties
  12250. (org-trim
  12251. (buffer-substring
  12252. (match-beginning 3) (goto-char
  12253. (point-at-eol)))))
  12254. (substring (org-match-string-no-properties 3)
  12255. 1 -1)))
  12256. ;; Get the correct property name from the key. This is
  12257. ;; necessary if the user has configured time keywords.
  12258. (setq key1 (concat key ":"))
  12259. (cond
  12260. ((not key)
  12261. (setq key
  12262. (if (= (char-after (match-beginning 3)) ?\[)
  12263. "TIMESTAMP_IA" "TIMESTAMP")))
  12264. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12265. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12266. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12267. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12268. (when (or (equal key "CLOCK") (not (assoc key props)))
  12269. (push (cons key string) props))))
  12270. )
  12271. (when (memq which '(all standard))
  12272. ;; Get the standard properties, like :PROP: ...
  12273. (setq range (org-get-property-block beg end))
  12274. (when range
  12275. (goto-char (car range))
  12276. (while (re-search-forward
  12277. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12278. (cdr range) t)
  12279. (setq key (org-match-string-no-properties 1)
  12280. value (org-trim (or (org-match-string-no-properties 2) "")))
  12281. (unless (member key excluded)
  12282. (push (cons key (or value "")) props)))))
  12283. (if clocksum
  12284. (push (cons "CLOCKSUM"
  12285. (org-columns-number-to-string (/ (float clocksum) 60.)
  12286. 'add_times))
  12287. props))
  12288. (unless (assoc "CATEGORY" props)
  12289. (setq value (or (org-get-category)
  12290. (progn (org-refresh-category-properties)
  12291. (org-get-category))))
  12292. (push (cons "CATEGORY" value) props))
  12293. (append sum-props (nreverse props)))))))
  12294. (defun org-entry-get (pom property &optional inherit literal-nil)
  12295. "Get value of PROPERTY for entry at point-or-marker POM.
  12296. If INHERIT is non-nil and the entry does not have the property,
  12297. then also check higher levels of the hierarchy.
  12298. If INHERIT is the symbol `selective', use inheritance only if the setting
  12299. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12300. If the property is present but empty, the return value is the empty string.
  12301. If the property is not present at all, nil is returned.
  12302. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12303. do not interpret it as the list atom nil. This is used for inheritance
  12304. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12305. (org-with-point-at pom
  12306. (if (and inherit (if (eq inherit 'selective)
  12307. (org-property-inherit-p property)
  12308. t))
  12309. (org-entry-get-with-inheritance property literal-nil)
  12310. (if (member property org-special-properties)
  12311. ;; We need a special property. Use `org-entry-properties' to
  12312. ;; retrieve it, but specify the wanted property
  12313. (cdr (assoc property (org-entry-properties nil 'special property)))
  12314. (let ((range (org-get-property-block)))
  12315. (if (and range
  12316. (goto-char (car range))
  12317. (re-search-forward
  12318. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12319. (cdr range) t))
  12320. ;; Found the property, return it.
  12321. (if (match-end 1)
  12322. (if literal-nil
  12323. (org-match-string-no-properties 1)
  12324. (org-not-nil (org-match-string-no-properties 1)))
  12325. "")))))))
  12326. (defun org-property-or-variable-value (var &optional inherit)
  12327. "Check if there is a property fixing the value of VAR.
  12328. If yes, return this value. If not, return the current value of the variable."
  12329. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12330. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12331. (read prop)
  12332. (symbol-value var))))
  12333. (defun org-entry-delete (pom property)
  12334. "Delete the property PROPERTY from entry at point-or-marker POM."
  12335. (org-with-point-at pom
  12336. (if (member property org-special-properties)
  12337. nil ; cannot delete these properties.
  12338. (let ((range (org-get-property-block)))
  12339. (if (and range
  12340. (goto-char (car range))
  12341. (re-search-forward
  12342. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12343. (cdr range) t))
  12344. (progn
  12345. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12346. t)
  12347. nil)))))
  12348. ;; Multi-values properties are properties that contain multiple values
  12349. ;; These values are assumed to be single words, separated by whitespace.
  12350. (defun org-entry-add-to-multivalued-property (pom property value)
  12351. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12352. (let* ((old (org-entry-get pom property))
  12353. (values (and old (org-split-string old "[ \t]"))))
  12354. (setq value (org-entry-protect-space value))
  12355. (unless (member value values)
  12356. (setq values (cons value values))
  12357. (org-entry-put pom property
  12358. (mapconcat 'identity values " ")))))
  12359. (defun org-entry-remove-from-multivalued-property (pom property value)
  12360. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12361. (let* ((old (org-entry-get pom property))
  12362. (values (and old (org-split-string old "[ \t]"))))
  12363. (setq value (org-entry-protect-space value))
  12364. (when (member value values)
  12365. (setq values (delete value values))
  12366. (org-entry-put pom property
  12367. (mapconcat 'identity values " ")))))
  12368. (defun org-entry-member-in-multivalued-property (pom property value)
  12369. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12370. (let* ((old (org-entry-get pom property))
  12371. (values (and old (org-split-string old "[ \t]"))))
  12372. (setq value (org-entry-protect-space value))
  12373. (member value values)))
  12374. (defun org-entry-get-multivalued-property (pom property)
  12375. "Return a list of values in a multivalued property."
  12376. (let* ((value (org-entry-get pom property))
  12377. (values (and value (org-split-string value "[ \t]"))))
  12378. (mapcar 'org-entry-restore-space values)))
  12379. (defun org-entry-put-multivalued-property (pom property &rest values)
  12380. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12381. VALUES should be a list of strings. Spaces will be protected."
  12382. (org-entry-put pom property
  12383. (mapconcat 'org-entry-protect-space values " "))
  12384. (let* ((value (org-entry-get pom property))
  12385. (values (and value (org-split-string value "[ \t]"))))
  12386. (mapcar 'org-entry-restore-space values)))
  12387. (defun org-entry-protect-space (s)
  12388. "Protect spaces and newline in string S."
  12389. (while (string-match " " s)
  12390. (setq s (replace-match "%20" t t s)))
  12391. (while (string-match "\n" s)
  12392. (setq s (replace-match "%0A" t t s)))
  12393. s)
  12394. (defun org-entry-restore-space (s)
  12395. "Restore spaces and newline in string S."
  12396. (while (string-match "%20" s)
  12397. (setq s (replace-match " " t t s)))
  12398. (while (string-match "%0A" s)
  12399. (setq s (replace-match "\n" t t s)))
  12400. s)
  12401. (defvar org-entry-property-inherited-from (make-marker)
  12402. "Marker pointing to the entry from where a property was inherited.
  12403. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12404. location of the entry where the inheritance search matched. If there was
  12405. no match, the marker will point nowhere.
  12406. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12407. is set.")
  12408. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12409. "Get entry property, and search higher levels if not present.
  12410. The search will stop at the first ancestor which has the property defined.
  12411. If the value found is \"nil\", return nil to show that the property
  12412. should be considered as undefined (this is the meaning of nil here).
  12413. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12414. (move-marker org-entry-property-inherited-from nil)
  12415. (let (tmp)
  12416. (save-excursion
  12417. (save-restriction
  12418. (widen)
  12419. (catch 'ex
  12420. (while t
  12421. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12422. (org-back-to-heading t)
  12423. (move-marker org-entry-property-inherited-from (point))
  12424. (throw 'ex tmp))
  12425. (or (org-up-heading-safe) (throw 'ex nil)))))
  12426. (setq tmp (or tmp
  12427. (cdr (assoc property org-file-properties))
  12428. (cdr (assoc property org-global-properties))
  12429. (cdr (assoc property org-global-properties-fixed))))
  12430. (if literal-nil tmp (org-not-nil tmp)))))
  12431. (defvar org-property-changed-functions nil
  12432. "Hook called when the value of a property has changed.
  12433. Each hook function should accept two arguments, the name of the property
  12434. and the new value.")
  12435. (defun org-entry-put (pom property value)
  12436. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12437. (org-with-point-at pom
  12438. (org-back-to-heading t)
  12439. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12440. range)
  12441. (cond
  12442. ((equal property "TODO")
  12443. (when (and (stringp value) (string-match "\\S-" value)
  12444. (not (member value org-todo-keywords-1)))
  12445. (error "\"%s\" is not a valid TODO state" value))
  12446. (if (or (not value)
  12447. (not (string-match "\\S-" value)))
  12448. (setq value 'none))
  12449. (org-todo value)
  12450. (org-set-tags nil 'align))
  12451. ((equal property "PRIORITY")
  12452. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12453. (string-to-char value) ?\ ))
  12454. (org-set-tags nil 'align))
  12455. ((equal property "SCHEDULED")
  12456. (if (re-search-forward org-scheduled-time-regexp end t)
  12457. (cond
  12458. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12459. ((eq value 'later) (org-timestamp-change 1 'day))
  12460. (t (call-interactively 'org-schedule)))
  12461. (call-interactively 'org-schedule)))
  12462. ((equal property "DEADLINE")
  12463. (if (re-search-forward org-deadline-time-regexp end t)
  12464. (cond
  12465. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12466. ((eq value 'later) (org-timestamp-change 1 'day))
  12467. (t (call-interactively 'org-deadline)))
  12468. (call-interactively 'org-deadline)))
  12469. ((member property org-special-properties)
  12470. (error "The %s property can not yet be set with `org-entry-put'"
  12471. property))
  12472. (t ; a non-special property
  12473. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12474. (setq range (org-get-property-block beg end 'force))
  12475. (goto-char (car range))
  12476. (if (re-search-forward
  12477. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12478. (progn
  12479. (delete-region (match-beginning 1) (match-end 1))
  12480. (goto-char (match-beginning 1)))
  12481. (goto-char (cdr range))
  12482. (insert "\n")
  12483. (backward-char 1)
  12484. (org-indent-line-function)
  12485. (insert ":" property ":"))
  12486. (and value (insert " " value))
  12487. (org-indent-line-function)))))
  12488. (run-hook-with-args 'org-property-changed-functions property value)))
  12489. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12490. "Get all property keys in the current buffer.
  12491. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12492. like tags and TODO state.
  12493. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12494. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12495. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12496. formats in the current buffer."
  12497. (let (rtn range cfmt s p)
  12498. (save-excursion
  12499. (save-restriction
  12500. (widen)
  12501. (goto-char (point-min))
  12502. (while (re-search-forward org-property-start-re nil t)
  12503. (setq range (org-get-property-block))
  12504. (goto-char (car range))
  12505. (while (re-search-forward
  12506. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12507. (cdr range) t)
  12508. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12509. (outline-next-heading))))
  12510. (when include-specials
  12511. (setq rtn (append org-special-properties rtn)))
  12512. (when include-defaults
  12513. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12514. (add-to-list 'rtn org-effort-property))
  12515. (when include-columns
  12516. (save-excursion
  12517. (save-restriction
  12518. (widen)
  12519. (goto-char (point-min))
  12520. (while (re-search-forward
  12521. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12522. nil t)
  12523. (setq cfmt (match-string 2) s 0)
  12524. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12525. cfmt s)
  12526. (setq s (match-end 0)
  12527. p (match-string 1 cfmt))
  12528. (unless (or (equal p "ITEM")
  12529. (member p org-special-properties))
  12530. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12531. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12532. (defun org-property-values (key)
  12533. "Return a list of all values of property KEY."
  12534. (save-excursion
  12535. (save-restriction
  12536. (widen)
  12537. (goto-char (point-min))
  12538. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12539. values)
  12540. (while (re-search-forward re nil t)
  12541. (add-to-list 'values (org-trim (match-string 1))))
  12542. (delete "" values)))))
  12543. (defun org-insert-property-drawer ()
  12544. "Insert a property drawer into the current entry."
  12545. (interactive)
  12546. (org-back-to-heading t)
  12547. (looking-at outline-regexp)
  12548. (let ((indent (if org-adapt-indentation
  12549. (- (match-end 0)(match-beginning 0))
  12550. 0))
  12551. (beg (point))
  12552. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12553. end hiddenp)
  12554. (outline-next-heading)
  12555. (setq end (point))
  12556. (goto-char beg)
  12557. (while (re-search-forward re end t))
  12558. (setq hiddenp (org-invisible-p))
  12559. (end-of-line 1)
  12560. (and (equal (char-after) ?\n) (forward-char 1))
  12561. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12562. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12563. ;; just skip this line
  12564. (beginning-of-line 2)
  12565. ;; Drawer start, find the end
  12566. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12567. (beginning-of-line 1)))
  12568. (org-skip-over-state-notes)
  12569. (skip-chars-backward " \t\n\r")
  12570. (if (eq (char-before) ?*) (forward-char 1))
  12571. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12572. (beginning-of-line 0)
  12573. (org-indent-to-column indent)
  12574. (beginning-of-line 2)
  12575. (org-indent-to-column indent)
  12576. (beginning-of-line 0)
  12577. (if hiddenp
  12578. (save-excursion
  12579. (org-back-to-heading t)
  12580. (hide-entry))
  12581. (org-flag-drawer t))))
  12582. (defun org-set-property (property value)
  12583. "In the current entry, set PROPERTY to VALUE.
  12584. When called interactively, this will prompt for a property name, offering
  12585. completion on existing and default properties. And then it will prompt
  12586. for a value, offering completion either on allowed values (via an inherited
  12587. xxx_ALL property) or on existing values in other instances of this property
  12588. in the current file."
  12589. (interactive
  12590. (let* ((completion-ignore-case t)
  12591. (keys (org-buffer-property-keys nil t t))
  12592. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12593. (prop (if (member prop0 keys)
  12594. prop0
  12595. (or (cdr (assoc (downcase prop0)
  12596. (mapcar (lambda (x) (cons (downcase x) x))
  12597. keys)))
  12598. prop0)))
  12599. (cur (org-entry-get nil prop))
  12600. (prompt (concat prop " value"
  12601. (if (and cur (string-match "\\S-" cur))
  12602. (concat " [" cur "]") "") ": "))
  12603. (allowed (org-property-get-allowed-values nil prop 'table))
  12604. (existing (mapcar 'list (org-property-values prop)))
  12605. (val (if allowed
  12606. (org-completing-read prompt allowed nil
  12607. (not (get-text-property 0 'org-unrestricted
  12608. (caar allowed))))
  12609. (let (org-completion-use-ido org-completion-use-iswitchb)
  12610. (org-completing-read prompt existing nil nil "" nil cur)))))
  12611. (list prop (if (equal val "") cur val))))
  12612. (unless (equal (org-entry-get nil property) value)
  12613. (org-entry-put nil property value)))
  12614. (defun org-delete-property (property)
  12615. "In the current entry, delete PROPERTY."
  12616. (interactive
  12617. (let* ((completion-ignore-case t)
  12618. (prop (org-icompleting-read "Property: "
  12619. (org-entry-properties nil 'standard))))
  12620. (list prop)))
  12621. (message "Property %s %s" property
  12622. (if (org-entry-delete nil property)
  12623. "deleted"
  12624. "was not present in the entry")))
  12625. (defun org-delete-property-globally (property)
  12626. "Remove PROPERTY globally, from all entries."
  12627. (interactive
  12628. (let* ((completion-ignore-case t)
  12629. (prop (org-icompleting-read
  12630. "Globally remove property: "
  12631. (mapcar 'list (org-buffer-property-keys)))))
  12632. (list prop)))
  12633. (save-excursion
  12634. (save-restriction
  12635. (widen)
  12636. (goto-char (point-min))
  12637. (let ((cnt 0))
  12638. (while (re-search-forward
  12639. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12640. nil t)
  12641. (setq cnt (1+ cnt))
  12642. (replace-match ""))
  12643. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12644. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12645. (defun org-compute-property-at-point ()
  12646. "Compute the property at point.
  12647. This looks for an enclosing column format, extracts the operator and
  12648. then applies it to the property in the column format's scope."
  12649. (interactive)
  12650. (unless (org-at-property-p)
  12651. (error "Not at a property"))
  12652. (let ((prop (org-match-string-no-properties 2)))
  12653. (org-columns-get-format-and-top-level)
  12654. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12655. (error "No operator defined for property %s" prop))
  12656. (org-columns-compute prop)))
  12657. (defvar org-property-allowed-value-functions nil
  12658. "Hook for functions supplying allowed values for a specific property.
  12659. The functions must take a single argument, the name of the property, and
  12660. return a flat list of allowed values. If \":ETC\" is one of
  12661. the values, this means that these values are intended as defaults for
  12662. completion, but that other values should be allowed too.
  12663. The functions must return nil if they are not responsible for this
  12664. property.")
  12665. (defun org-property-get-allowed-values (pom property &optional table)
  12666. "Get allowed values for the property PROPERTY.
  12667. When TABLE is non-nil, return an alist that can directly be used for
  12668. completion."
  12669. (let (vals)
  12670. (cond
  12671. ((equal property "TODO")
  12672. (setq vals (org-with-point-at pom
  12673. (append org-todo-keywords-1 '("")))))
  12674. ((equal property "PRIORITY")
  12675. (let ((n org-lowest-priority))
  12676. (while (>= n org-highest-priority)
  12677. (push (char-to-string n) vals)
  12678. (setq n (1- n)))))
  12679. ((member property org-special-properties))
  12680. ((setq vals (run-hook-with-args-until-success
  12681. 'org-property-allowed-value-functions property)))
  12682. (t
  12683. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12684. (when (and vals (string-match "\\S-" vals))
  12685. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12686. (setq vals (mapcar (lambda (x)
  12687. (cond ((stringp x) x)
  12688. ((numberp x) (number-to-string x))
  12689. ((symbolp x) (symbol-name x))
  12690. (t "???")))
  12691. vals)))))
  12692. (when (member ":ETC" vals)
  12693. (setq vals (remove ":ETC" vals))
  12694. (org-add-props (car vals) '(org-unrestricted t)))
  12695. (if table (mapcar 'list vals) vals)))
  12696. (defun org-property-previous-allowed-value (&optional previous)
  12697. "Switch to the next allowed value for this property."
  12698. (interactive)
  12699. (org-property-next-allowed-value t))
  12700. (defun org-property-next-allowed-value (&optional previous)
  12701. "Switch to the next allowed value for this property."
  12702. (interactive)
  12703. (unless (org-at-property-p)
  12704. (error "Not at a property"))
  12705. (let* ((key (match-string 2))
  12706. (value (match-string 3))
  12707. (allowed (or (org-property-get-allowed-values (point) key)
  12708. (and (member value '("[ ]" "[-]" "[X]"))
  12709. '("[ ]" "[X]"))))
  12710. nval)
  12711. (unless allowed
  12712. (error "Allowed values for this property have not been defined"))
  12713. (if previous (setq allowed (reverse allowed)))
  12714. (if (member value allowed)
  12715. (setq nval (car (cdr (member value allowed)))))
  12716. (setq nval (or nval (car allowed)))
  12717. (if (equal nval value)
  12718. (error "Only one allowed value for this property"))
  12719. (org-at-property-p)
  12720. (replace-match (concat " :" key ": " nval) t t)
  12721. (org-indent-line-function)
  12722. (beginning-of-line 1)
  12723. (skip-chars-forward " \t")
  12724. (run-hook-with-args 'org-property-changed-functions key nval)))
  12725. (defun org-find-olp (path &optional this-buffer)
  12726. "Return a marker pointing to the entry at outline path OLP.
  12727. If anything goes wrong, throw an error.
  12728. You can wrap this call to catch the error like this:
  12729. (condition-case msg
  12730. (org-mobile-locate-entry (match-string 4))
  12731. (error (nth 1 msg)))
  12732. The return value will then be either a string with the error message,
  12733. or a marker if everything is OK.
  12734. If THIS-BUFFER is set, the outline path does not contain a file,
  12735. only headings."
  12736. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12737. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12738. (level 1)
  12739. (lmin 1)
  12740. (lmax 1)
  12741. limit re end found pos heading cnt)
  12742. (unless buffer (error "File not found :%s" file))
  12743. (with-current-buffer buffer
  12744. (save-excursion
  12745. (save-restriction
  12746. (widen)
  12747. (setq limit (point-max))
  12748. (goto-char (point-min))
  12749. (while (setq heading (pop path))
  12750. (setq re (format org-complex-heading-regexp-format
  12751. (regexp-quote heading)))
  12752. (setq cnt 0 pos (point))
  12753. (while (re-search-forward re end t)
  12754. (setq level (- (match-end 1) (match-beginning 1)))
  12755. (if (and (>= level lmin) (<= level lmax))
  12756. (setq found (match-beginning 0) cnt (1+ cnt))))
  12757. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12758. lmax heading))
  12759. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12760. lmax heading))
  12761. (goto-char found)
  12762. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12763. (setq end (save-excursion (org-end-of-subtree t t))))
  12764. (when (org-on-heading-p)
  12765. (move-marker (make-marker) (point))))))))
  12766. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  12767. "Find node HEADING in BUFFER.
  12768. Return a marker to the heading if it was found, or nil if not.
  12769. If POS-ONLY is set, return just the position instead of a marker.
  12770. The heading text must match exact, but it may have a TODO keyword,
  12771. a priority cookie and tags in the standard locations."
  12772. (with-current-buffer (or buffer (current-buffer))
  12773. (save-excursion
  12774. (save-restriction
  12775. (widen)
  12776. (goto-char (point-min))
  12777. (let (case-fold-search)
  12778. (if (re-search-forward
  12779. (format org-complex-heading-regexp-format
  12780. (regexp-quote heading)) nil t)
  12781. (if pos-only
  12782. (match-beginning 0)
  12783. (move-marker (make-marker) (match-beginning 0)))))))))
  12784. (defun org-find-exact-heading-in-directory (heading &optional dir)
  12785. "Find Org node headline HEADING in all .org files in directory DIR.
  12786. When the target headline is found, return a marker to this location."
  12787. (let ((files (directory-files (or dir default-directory)
  12788. nil "\\`[^.#].*\\.org\\'"))
  12789. file visiting m buffer)
  12790. (catch 'found
  12791. (while (setq file (pop files))
  12792. (message "trying %s" file)
  12793. (setq visiting (org-find-base-buffer-visiting file))
  12794. (setq buffer (or visiting (find-file-noselect file)))
  12795. (setq m (org-find-exact-headline-in-buffer
  12796. heading buffer))
  12797. (when (and (not m) (not visiting)) (kill-buffer buffer))
  12798. (and m (throw 'found m))))))
  12799. (defun org-find-entry-with-id (ident)
  12800. "Locate the entry that contains the ID property with exact value IDENT.
  12801. IDENT can be a string, a symbol or a number, this function will search for
  12802. the string representation of it.
  12803. Return the position where this entry starts, or nil if there is no such entry."
  12804. (interactive "sID: ")
  12805. (let ((id (cond
  12806. ((stringp ident) ident)
  12807. ((symbol-name ident) (symbol-name ident))
  12808. ((numberp ident) (number-to-string ident))
  12809. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12810. (case-fold-search nil))
  12811. (save-excursion
  12812. (save-restriction
  12813. (widen)
  12814. (goto-char (point-min))
  12815. (when (re-search-forward
  12816. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12817. nil t)
  12818. (org-back-to-heading t)
  12819. (point))))))
  12820. ;;;; Timestamps
  12821. (defvar org-last-changed-timestamp nil)
  12822. (defvar org-last-inserted-timestamp nil
  12823. "The last time stamp inserted with `org-insert-time-stamp'.")
  12824. (defvar org-time-was-given) ; dynamically scoped parameter
  12825. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12826. (defvar org-ts-what) ; dynamically scoped parameter
  12827. (defun org-time-stamp (arg &optional inactive)
  12828. "Prompt for a date/time and insert a time stamp.
  12829. If the user specifies a time like HH:MM, or if this command is called
  12830. with a prefix argument, the time stamp will contain date and time.
  12831. Otherwise, only the date will be included. All parts of a date not
  12832. specified by the user will be filled in from the current date/time.
  12833. So if you press just return without typing anything, the time stamp
  12834. will represent the current date/time. If there is already a timestamp
  12835. at the cursor, it will be modified."
  12836. (interactive "P")
  12837. (let* ((ts nil)
  12838. (default-time
  12839. ;; Default time is either today, or, when entering a range,
  12840. ;; the range start.
  12841. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12842. (save-excursion
  12843. (re-search-backward
  12844. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12845. (- (point) 20) t)))
  12846. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12847. (current-time)))
  12848. (default-input (and ts (org-get-compact-tod ts)))
  12849. org-time-was-given org-end-time-was-given time)
  12850. (cond
  12851. ((and (org-at-timestamp-p t)
  12852. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12853. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12854. (insert "--")
  12855. (setq time (let ((this-command this-command))
  12856. (org-read-date arg 'totime nil nil
  12857. default-time default-input)))
  12858. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12859. ((org-at-timestamp-p t)
  12860. (setq time (let ((this-command this-command))
  12861. (org-read-date arg 'totime nil nil default-time default-input)))
  12862. (when (org-at-timestamp-p t) ; just to get the match data
  12863. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12864. (replace-match "")
  12865. (setq org-last-changed-timestamp
  12866. (org-insert-time-stamp
  12867. time (or org-time-was-given arg)
  12868. inactive nil nil (list org-end-time-was-given))))
  12869. (message "Timestamp updated"))
  12870. (t
  12871. (setq time (let ((this-command this-command))
  12872. (org-read-date arg 'totime nil nil default-time default-input)))
  12873. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12874. nil nil (list org-end-time-was-given))))))
  12875. ;; FIXME: can we use this for something else, like computing time differences?
  12876. (defun org-get-compact-tod (s)
  12877. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12878. (let* ((t1 (match-string 1 s))
  12879. (h1 (string-to-number (match-string 2 s)))
  12880. (m1 (string-to-number (match-string 3 s)))
  12881. (t2 (and (match-end 4) (match-string 5 s)))
  12882. (h2 (and t2 (string-to-number (match-string 6 s))))
  12883. (m2 (and t2 (string-to-number (match-string 7 s))))
  12884. dh dm)
  12885. (if (not t2)
  12886. t1
  12887. (setq dh (- h2 h1) dm (- m2 m1))
  12888. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12889. (concat t1 "+" (number-to-string dh)
  12890. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12891. (defun org-time-stamp-inactive (&optional arg)
  12892. "Insert an inactive time stamp.
  12893. An inactive time stamp is enclosed in square brackets instead of angle
  12894. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12895. does not link to the calendar and cannot be changed with the S-cursor keys.
  12896. So these are more for recording a certain time/date."
  12897. (interactive "P")
  12898. (org-time-stamp arg 'inactive))
  12899. (defvar org-date-ovl (make-overlay 1 1))
  12900. (overlay-put org-date-ovl 'face 'org-warning)
  12901. (org-detach-overlay org-date-ovl)
  12902. (defvar org-ans1) ; dynamically scoped parameter
  12903. (defvar org-ans2) ; dynamically scoped parameter
  12904. (defvar org-plain-time-of-day-regexp) ; defined below
  12905. (defvar org-overriding-default-time nil) ; dynamically scoped
  12906. (defvar org-read-date-overlay nil)
  12907. (defvar org-dcst nil) ; dynamically scoped
  12908. (defvar org-read-date-history nil)
  12909. (defvar org-read-date-final-answer nil)
  12910. (defun org-read-date (&optional with-time to-time from-string prompt
  12911. default-time default-input)
  12912. "Read a date, possibly a time, and make things smooth for the user.
  12913. The prompt will suggest to enter an ISO date, but you can also enter anything
  12914. which will at least partially be understood by `parse-time-string'.
  12915. Unrecognized parts of the date will default to the current day, month, year,
  12916. hour and minute. If this command is called to replace a timestamp at point,
  12917. of to enter the second timestamp of a range, the default time is taken
  12918. from the existing stamp. Furthermore, the command prefers the future,
  12919. so if you are giving a date where the year is not given, and the day-month
  12920. combination is already past in the current year, it will assume you
  12921. mean next year. For details, see the manual. A few examples:
  12922. 3-2-5 --> 2003-02-05
  12923. feb 15 --> currentyear-02-15
  12924. 2/15 --> currentyear-02-15
  12925. sep 12 9 --> 2009-09-12
  12926. 12:45 --> today 12:45
  12927. 22 sept 0:34 --> currentyear-09-22 0:34
  12928. 12 --> currentyear-currentmonth-12
  12929. Fri --> nearest Friday (today or later)
  12930. etc.
  12931. Furthermore you can specify a relative date by giving, as the *first* thing
  12932. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12933. change in days weeks, months, years.
  12934. With a single plus or minus, the date is relative to today. With a double
  12935. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12936. +4d --> four days from today
  12937. +4 --> same as above
  12938. +2w --> two weeks from today
  12939. ++5 --> five days from default date
  12940. The function understands only English month and weekday abbreviations,
  12941. but this can be configured with the variables `parse-time-months' and
  12942. `parse-time-weekdays'.
  12943. While prompting, a calendar is popped up - you can also select the
  12944. date with the mouse (button 1). The calendar shows a period of three
  12945. months. To scroll it to other months, use the keys `>' and `<'.
  12946. If you don't like the calendar, turn it off with
  12947. \(setq org-read-date-popup-calendar nil)
  12948. With optional argument TO-TIME, the date will immediately be converted
  12949. to an internal time.
  12950. With an optional argument WITH-TIME, the prompt will suggest to also
  12951. insert a time. Note that when WITH-TIME is not set, you can still
  12952. enter a time, and this function will inform the calling routine about
  12953. this change. The calling routine may then choose to change the format
  12954. used to insert the time stamp into the buffer to include the time.
  12955. With optional argument FROM-STRING, read from this string instead from
  12956. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12957. the time/date that is used for everything that is not specified by the
  12958. user."
  12959. (require 'parse-time)
  12960. (let* ((org-time-stamp-rounding-minutes
  12961. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12962. (org-dcst org-display-custom-times)
  12963. (ct (org-current-time))
  12964. (def (or org-overriding-default-time default-time ct))
  12965. (defdecode (decode-time def))
  12966. (dummy (progn
  12967. (when (< (nth 2 defdecode) org-extend-today-until)
  12968. (setcar (nthcdr 2 defdecode) -1)
  12969. (setcar (nthcdr 1 defdecode) 59)
  12970. (setq def (apply 'encode-time defdecode)
  12971. defdecode (decode-time def)))))
  12972. (calendar-frame-setup nil)
  12973. (calendar-setup nil)
  12974. (calendar-move-hook nil)
  12975. (calendar-view-diary-initially-flag nil)
  12976. (calendar-view-holidays-initially-flag nil)
  12977. (timestr (format-time-string
  12978. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12979. (prompt (concat (if prompt (concat prompt " ") "")
  12980. (format "Date+time [%s]: " timestr)))
  12981. ans (org-ans0 "") org-ans1 org-ans2 final)
  12982. (cond
  12983. (from-string (setq ans from-string))
  12984. (org-read-date-popup-calendar
  12985. (save-excursion
  12986. (save-window-excursion
  12987. (calendar)
  12988. (calendar-forward-day (- (time-to-days def)
  12989. (calendar-absolute-from-gregorian
  12990. (calendar-current-date))))
  12991. (org-eval-in-calendar nil t)
  12992. (let* ((old-map (current-local-map))
  12993. (map (copy-keymap calendar-mode-map))
  12994. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12995. (org-defkey map (kbd "RET") 'org-calendar-select)
  12996. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  12997. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  12998. (org-defkey minibuffer-local-map [(meta shift left)]
  12999. (lambda () (interactive)
  13000. (org-eval-in-calendar '(calendar-backward-month 1))))
  13001. (org-defkey minibuffer-local-map [(meta shift right)]
  13002. (lambda () (interactive)
  13003. (org-eval-in-calendar '(calendar-forward-month 1))))
  13004. (org-defkey minibuffer-local-map [(meta shift up)]
  13005. (lambda () (interactive)
  13006. (org-eval-in-calendar '(calendar-backward-year 1))))
  13007. (org-defkey minibuffer-local-map [(meta shift down)]
  13008. (lambda () (interactive)
  13009. (org-eval-in-calendar '(calendar-forward-year 1))))
  13010. (org-defkey minibuffer-local-map [?\e (shift left)]
  13011. (lambda () (interactive)
  13012. (org-eval-in-calendar '(calendar-backward-month 1))))
  13013. (org-defkey minibuffer-local-map [?\e (shift right)]
  13014. (lambda () (interactive)
  13015. (org-eval-in-calendar '(calendar-forward-month 1))))
  13016. (org-defkey minibuffer-local-map [?\e (shift up)]
  13017. (lambda () (interactive)
  13018. (org-eval-in-calendar '(calendar-backward-year 1))))
  13019. (org-defkey minibuffer-local-map [?\e (shift down)]
  13020. (lambda () (interactive)
  13021. (org-eval-in-calendar '(calendar-forward-year 1))))
  13022. (org-defkey minibuffer-local-map [(shift up)]
  13023. (lambda () (interactive)
  13024. (org-eval-in-calendar '(calendar-backward-week 1))))
  13025. (org-defkey minibuffer-local-map [(shift down)]
  13026. (lambda () (interactive)
  13027. (org-eval-in-calendar '(calendar-forward-week 1))))
  13028. (org-defkey minibuffer-local-map [(shift left)]
  13029. (lambda () (interactive)
  13030. (org-eval-in-calendar '(calendar-backward-day 1))))
  13031. (org-defkey minibuffer-local-map [(shift right)]
  13032. (lambda () (interactive)
  13033. (org-eval-in-calendar '(calendar-forward-day 1))))
  13034. (org-defkey minibuffer-local-map ">"
  13035. (lambda () (interactive)
  13036. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13037. (org-defkey minibuffer-local-map "<"
  13038. (lambda () (interactive)
  13039. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13040. (org-defkey minibuffer-local-map "\C-v"
  13041. (lambda () (interactive)
  13042. (org-eval-in-calendar
  13043. '(calendar-scroll-left-three-months 1))))
  13044. (org-defkey minibuffer-local-map "\M-v"
  13045. (lambda () (interactive)
  13046. (org-eval-in-calendar
  13047. '(calendar-scroll-right-three-months 1))))
  13048. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13049. (unwind-protect
  13050. (progn
  13051. (use-local-map map)
  13052. (add-hook 'post-command-hook 'org-read-date-display)
  13053. (setq org-ans0 (read-string prompt default-input
  13054. 'org-read-date-history nil))
  13055. ;; org-ans0: from prompt
  13056. ;; org-ans1: from mouse click
  13057. ;; org-ans2: from calendar motion
  13058. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13059. (remove-hook 'post-command-hook 'org-read-date-display)
  13060. (use-local-map old-map)
  13061. (when org-read-date-overlay
  13062. (delete-overlay org-read-date-overlay)
  13063. (setq org-read-date-overlay nil)))))))
  13064. (t ; Naked prompt only
  13065. (unwind-protect
  13066. (setq ans (read-string prompt default-input
  13067. 'org-read-date-history timestr))
  13068. (when org-read-date-overlay
  13069. (delete-overlay org-read-date-overlay)
  13070. (setq org-read-date-overlay nil)))))
  13071. (setq final (org-read-date-analyze ans def defdecode))
  13072. ;; One round trip to get rid of 34th of August and stuff like that....
  13073. (setq final (decode-time (apply 'encode-time final)))
  13074. (setq org-read-date-final-answer ans)
  13075. (if to-time
  13076. (apply 'encode-time final)
  13077. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13078. (format "%04d-%02d-%02d %02d:%02d"
  13079. (nth 5 final) (nth 4 final) (nth 3 final)
  13080. (nth 2 final) (nth 1 final))
  13081. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13082. (defvar def)
  13083. (defvar defdecode)
  13084. (defvar with-time)
  13085. (defvar org-read-date-analyze-futurep nil)
  13086. (defun org-read-date-display ()
  13087. "Display the current date prompt interpretation in the minibuffer."
  13088. (when org-read-date-display-live
  13089. (when org-read-date-overlay
  13090. (delete-overlay org-read-date-overlay))
  13091. (let ((p (point)))
  13092. (end-of-line 1)
  13093. (while (not (equal (buffer-substring
  13094. (max (point-min) (- (point) 4)) (point))
  13095. " "))
  13096. (insert " "))
  13097. (goto-char p))
  13098. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13099. " " (or org-ans1 org-ans2)))
  13100. (org-end-time-was-given nil)
  13101. (f (org-read-date-analyze ans def defdecode))
  13102. (fmts (if org-dcst
  13103. org-time-stamp-custom-formats
  13104. org-time-stamp-formats))
  13105. (fmt (if (or with-time
  13106. (and (boundp 'org-time-was-given) org-time-was-given))
  13107. (cdr fmts)
  13108. (car fmts)))
  13109. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13110. (when (and org-end-time-was-given
  13111. (string-match org-plain-time-of-day-regexp txt))
  13112. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13113. org-end-time-was-given
  13114. (substring txt (match-end 0)))))
  13115. (when org-read-date-analyze-futurep
  13116. (setq txt (concat txt " (=>F)")))
  13117. (setq org-read-date-overlay
  13118. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13119. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13120. (defun org-read-date-analyze (ans def defdecode)
  13121. "Analyze the combined answer of the date prompt."
  13122. ;; FIXME: cleanup and comment
  13123. (let ((nowdecode (decode-time (current-time)))
  13124. delta deltan deltaw deltadef year month day
  13125. hour minute second wday pm h2 m2 tl wday1
  13126. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13127. (setq org-read-date-analyze-futurep nil)
  13128. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13129. (setq ans "+0"))
  13130. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13131. (setq ans (replace-match "" t t ans)
  13132. deltan (car delta)
  13133. deltaw (nth 1 delta)
  13134. deltadef (nth 2 delta)))
  13135. ;; Check if there is an iso week date in there
  13136. ;; If yes, store the info and postpone interpreting it until the rest
  13137. ;; of the parsing is done
  13138. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13139. (setq iso-year (if (match-end 1)
  13140. (org-small-year-to-year
  13141. (string-to-number (match-string 1 ans))))
  13142. iso-weekday (if (match-end 3)
  13143. (string-to-number (match-string 3 ans)))
  13144. iso-week (string-to-number (match-string 2 ans)))
  13145. (setq ans (replace-match "" t t ans)))
  13146. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13147. (when (string-match
  13148. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13149. (setq year (if (match-end 2)
  13150. (string-to-number (match-string 2 ans))
  13151. (progn (setq kill-year t)
  13152. (string-to-number (format-time-string "%Y"))))
  13153. month (string-to-number (match-string 3 ans))
  13154. day (string-to-number (match-string 4 ans)))
  13155. (if (< year 100) (setq year (+ 2000 year)))
  13156. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13157. t nil ans)))
  13158. ;; Help matching american dates, like 5/30 or 5/30/7
  13159. (when (string-match
  13160. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13161. (setq year (if (match-end 4)
  13162. (string-to-number (match-string 4 ans))
  13163. (progn (setq kill-year t)
  13164. (string-to-number (format-time-string "%Y"))))
  13165. month (string-to-number (match-string 1 ans))
  13166. day (string-to-number (match-string 2 ans)))
  13167. (if (< year 100) (setq year (+ 2000 year)))
  13168. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13169. t nil ans)))
  13170. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13171. ;; If there is a time with am/pm, and *no* time without it, we convert
  13172. ;; so that matching will be successful.
  13173. (loop for i from 1 to 2 do ; twice, for end time as well
  13174. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13175. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13176. (setq hour (string-to-number (match-string 1 ans))
  13177. minute (if (match-end 3)
  13178. (string-to-number (match-string 3 ans))
  13179. 0)
  13180. pm (equal ?p
  13181. (string-to-char (downcase (match-string 4 ans)))))
  13182. (if (and (= hour 12) (not pm))
  13183. (setq hour 0)
  13184. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13185. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13186. t t ans))))
  13187. ;; Check if a time range is given as a duration
  13188. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13189. (setq hour (string-to-number (match-string 1 ans))
  13190. h2 (+ hour (string-to-number (match-string 3 ans)))
  13191. minute (string-to-number (match-string 2 ans))
  13192. m2 (+ minute (if (match-end 5) (string-to-number
  13193. (match-string 5 ans))0)))
  13194. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13195. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13196. t t ans)))
  13197. ;; Check if there is a time range
  13198. (when (boundp 'org-end-time-was-given)
  13199. (setq org-time-was-given nil)
  13200. (when (and (string-match org-plain-time-of-day-regexp ans)
  13201. (match-end 8))
  13202. (setq org-end-time-was-given (match-string 8 ans))
  13203. (setq ans (concat (substring ans 0 (match-beginning 7))
  13204. (substring ans (match-end 7))))))
  13205. (setq tl (parse-time-string ans)
  13206. day (or (nth 3 tl) (nth 3 defdecode))
  13207. month (or (nth 4 tl)
  13208. (if (and org-read-date-prefer-future
  13209. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13210. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13211. (nth 4 defdecode)))
  13212. year (or (and (not kill-year) (nth 5 tl))
  13213. (if (and org-read-date-prefer-future
  13214. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13215. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13216. (nth 5 defdecode)))
  13217. hour (or (nth 2 tl) (nth 2 defdecode))
  13218. minute (or (nth 1 tl) (nth 1 defdecode))
  13219. second (or (nth 0 tl) 0)
  13220. wday (nth 6 tl))
  13221. (when (and (eq org-read-date-prefer-future 'time)
  13222. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13223. (equal day (nth 3 nowdecode))
  13224. (equal month (nth 4 nowdecode))
  13225. (equal year (nth 5 nowdecode))
  13226. (nth 2 tl)
  13227. (or (< (nth 2 tl) (nth 2 nowdecode))
  13228. (and (= (nth 2 tl) (nth 2 nowdecode))
  13229. (nth 1 tl)
  13230. (< (nth 1 tl) (nth 1 nowdecode)))))
  13231. (setq day (1+ day)
  13232. futurep t))
  13233. ;; Special date definitions below
  13234. (cond
  13235. (iso-week
  13236. ;; There was an iso week
  13237. (require 'cal-iso)
  13238. (setq futurep nil)
  13239. (setq year (or iso-year year)
  13240. day (or iso-weekday wday 1)
  13241. wday nil ; to make sure that the trigger below does not match
  13242. iso-date (calendar-gregorian-from-absolute
  13243. (calendar-absolute-from-iso
  13244. (list iso-week day year))))
  13245. ; FIXME: Should we also push ISO weeks into the future?
  13246. ; (when (and org-read-date-prefer-future
  13247. ; (not iso-year)
  13248. ; (< (calendar-absolute-from-gregorian iso-date)
  13249. ; (time-to-days (current-time))))
  13250. ; (setq year (1+ year)
  13251. ; iso-date (calendar-gregorian-from-absolute
  13252. ; (calendar-absolute-from-iso
  13253. ; (list iso-week day year)))))
  13254. (setq month (car iso-date)
  13255. year (nth 2 iso-date)
  13256. day (nth 1 iso-date)))
  13257. (deltan
  13258. (setq futurep nil)
  13259. (unless deltadef
  13260. (let ((now (decode-time (current-time))))
  13261. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13262. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13263. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13264. ((equal deltaw "m") (setq month (+ month deltan)))
  13265. ((equal deltaw "y") (setq year (+ year deltan)))))
  13266. ((and wday (not (nth 3 tl)))
  13267. (setq futurep nil)
  13268. ;; Weekday was given, but no day, so pick that day in the week
  13269. ;; on or after the derived date.
  13270. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13271. (unless (equal wday wday1)
  13272. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13273. (if (and (boundp 'org-time-was-given)
  13274. (nth 2 tl))
  13275. (setq org-time-was-given t))
  13276. (if (< year 100) (setq year (+ 2000 year)))
  13277. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  13278. (setq org-read-date-analyze-futurep futurep)
  13279. (list second minute hour day month year)))
  13280. (defvar parse-time-weekdays)
  13281. (defun org-read-date-get-relative (s today default)
  13282. "Check string S for special relative date string.
  13283. TODAY and DEFAULT are internal times, for today and for a default.
  13284. Return shift list (N what def-flag)
  13285. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13286. N is the number of WHATs to shift.
  13287. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13288. the DEFAULT date rather than TODAY."
  13289. (when (and
  13290. (string-match
  13291. (concat
  13292. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13293. "\\([0-9]+\\)?"
  13294. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13295. "\\([ \t]\\|$\\)") s)
  13296. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13297. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13298. (string-to-char (substring (match-string 1 s) -1))
  13299. ?+))
  13300. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13301. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13302. (what (if (match-end 3) (match-string 3 s) "d"))
  13303. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13304. (date (if rel default today))
  13305. (wday (nth 6 (decode-time date)))
  13306. delta)
  13307. (if wday1
  13308. (progn
  13309. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13310. (if (= dir ?-) (setq delta (- delta 7)))
  13311. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13312. (list delta "d" rel))
  13313. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13314. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13315. "Turn a user-specified date into the internal representation.
  13316. The internal representation needed by the calendar is (month day year).
  13317. This is a wrapper to handle the brain-dead convention in calendar that
  13318. user function argument order change dependent on argument order."
  13319. (if (boundp 'calendar-date-style)
  13320. (cond
  13321. ((eq calendar-date-style 'american)
  13322. (list arg1 arg2 arg3))
  13323. ((eq calendar-date-style 'european)
  13324. (list arg2 arg1 arg3))
  13325. ((eq calendar-date-style 'iso)
  13326. (list arg2 arg3 arg1)))
  13327. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13328. (if (org-bound-and-true-p european-calendar-style)
  13329. (list arg2 arg1 arg3)
  13330. (list arg1 arg2 arg3)))))
  13331. (defun org-eval-in-calendar (form &optional keepdate)
  13332. "Eval FORM in the calendar window and return to current window.
  13333. Also, store the cursor date in variable org-ans2."
  13334. (let ((sf (selected-frame))
  13335. (sw (selected-window)))
  13336. (select-window (get-buffer-window "*Calendar*" t))
  13337. (eval form)
  13338. (when (and (not keepdate) (calendar-cursor-to-date))
  13339. (let* ((date (calendar-cursor-to-date))
  13340. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13341. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13342. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13343. (select-window sw)
  13344. (org-select-frame-set-input-focus sf)))
  13345. (defun org-calendar-select ()
  13346. "Return to `org-read-date' with the date currently selected.
  13347. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13348. (interactive)
  13349. (when (calendar-cursor-to-date)
  13350. (let* ((date (calendar-cursor-to-date))
  13351. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13352. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13353. (if (active-minibuffer-window) (exit-minibuffer))))
  13354. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13355. "Insert a date stamp for the date given by the internal TIME.
  13356. WITH-HM means use the stamp format that includes the time of the day.
  13357. INACTIVE means use square brackets instead of angular ones, so that the
  13358. stamp will not contribute to the agenda.
  13359. PRE and POST are optional strings to be inserted before and after the
  13360. stamp.
  13361. The command returns the inserted time stamp."
  13362. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13363. stamp)
  13364. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13365. (insert-before-markers (or pre ""))
  13366. (when (listp extra)
  13367. (setq extra (car extra))
  13368. (if (and (stringp extra)
  13369. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13370. (setq extra (format "-%02d:%02d"
  13371. (string-to-number (match-string 1 extra))
  13372. (string-to-number (match-string 2 extra))))
  13373. (setq extra nil)))
  13374. (when extra
  13375. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13376. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13377. (insert-before-markers (or post ""))
  13378. (setq org-last-inserted-timestamp stamp)))
  13379. (defun org-toggle-time-stamp-overlays ()
  13380. "Toggle the use of custom time stamp formats."
  13381. (interactive)
  13382. (setq org-display-custom-times (not org-display-custom-times))
  13383. (unless org-display-custom-times
  13384. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13385. (while (setq p (next-single-property-change p 'display))
  13386. (if (and (get-text-property p 'display)
  13387. (eq (get-text-property p 'face) 'org-date))
  13388. (remove-text-properties
  13389. p (setq p (next-single-property-change p 'display))
  13390. '(display t))))
  13391. (set-buffer-modified-p bmp)))
  13392. (if (featurep 'xemacs)
  13393. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13394. (org-restart-font-lock)
  13395. (setq org-table-may-need-update t)
  13396. (if org-display-custom-times
  13397. (message "Time stamps are overlayed with custom format")
  13398. (message "Time stamp overlays removed")))
  13399. (defun org-display-custom-time (beg end)
  13400. "Overlay modified time stamp format over timestamp between BEG and END."
  13401. (let* ((ts (buffer-substring beg end))
  13402. t1 w1 with-hm tf time str w2 (off 0))
  13403. (save-match-data
  13404. (setq t1 (org-parse-time-string ts t))
  13405. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13406. (setq off (- (match-end 0) (match-beginning 0)))))
  13407. (setq end (- end off))
  13408. (setq w1 (- end beg)
  13409. with-hm (and (nth 1 t1) (nth 2 t1))
  13410. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13411. time (org-fix-decoded-time t1)
  13412. str (org-add-props
  13413. (format-time-string
  13414. (substring tf 1 -1) (apply 'encode-time time))
  13415. nil 'mouse-face 'highlight)
  13416. w2 (length str))
  13417. (if (not (= w2 w1))
  13418. (add-text-properties (1+ beg) (+ 2 beg)
  13419. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13420. (if (featurep 'xemacs)
  13421. (progn
  13422. (put-text-property beg end 'invisible t)
  13423. (put-text-property beg end 'end-glyph (make-glyph str)))
  13424. (put-text-property beg end 'display str))))
  13425. (defun org-translate-time (string)
  13426. "Translate all timestamps in STRING to custom format.
  13427. But do this only if the variable `org-display-custom-times' is set."
  13428. (when org-display-custom-times
  13429. (save-match-data
  13430. (let* ((start 0)
  13431. (re org-ts-regexp-both)
  13432. t1 with-hm inactive tf time str beg end)
  13433. (while (setq start (string-match re string start))
  13434. (setq beg (match-beginning 0)
  13435. end (match-end 0)
  13436. t1 (save-match-data
  13437. (org-parse-time-string (substring string beg end) t))
  13438. with-hm (and (nth 1 t1) (nth 2 t1))
  13439. inactive (equal (substring string beg (1+ beg)) "[")
  13440. tf (funcall (if with-hm 'cdr 'car)
  13441. org-time-stamp-custom-formats)
  13442. time (org-fix-decoded-time t1)
  13443. str (format-time-string
  13444. (concat
  13445. (if inactive "[" "<") (substring tf 1 -1)
  13446. (if inactive "]" ">"))
  13447. (apply 'encode-time time))
  13448. string (replace-match str t t string)
  13449. start (+ start (length str)))))))
  13450. string)
  13451. (defun org-fix-decoded-time (time)
  13452. "Set 0 instead of nil for the first 6 elements of time.
  13453. Don't touch the rest."
  13454. (let ((n 0))
  13455. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13456. (defun org-days-to-time (timestamp-string)
  13457. "Difference between TIMESTAMP-STRING and now in days."
  13458. (- (time-to-days (org-time-string-to-time timestamp-string))
  13459. (time-to-days (current-time))))
  13460. (defun org-deadline-close (timestamp-string &optional ndays)
  13461. "Is the time in TIMESTAMP-STRING close to the current date?"
  13462. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13463. (and (< (org-days-to-time timestamp-string) ndays)
  13464. (not (org-entry-is-done-p))))
  13465. (defun org-get-wdays (ts)
  13466. "Get the deadline lead time appropriate for timestring TS."
  13467. (cond
  13468. ((<= org-deadline-warning-days 0)
  13469. ;; 0 or negative, enforce this value no matter what
  13470. (- org-deadline-warning-days))
  13471. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13472. ;; lead time is specified.
  13473. (floor (* (string-to-number (match-string 1 ts))
  13474. (cdr (assoc (match-string 2 ts)
  13475. '(("d" . 1) ("w" . 7)
  13476. ("m" . 30.4) ("y" . 365.25)))))))
  13477. ;; go for the default.
  13478. (t org-deadline-warning-days)))
  13479. (defun org-calendar-select-mouse (ev)
  13480. "Return to `org-read-date' with the date currently selected.
  13481. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13482. (interactive "e")
  13483. (mouse-set-point ev)
  13484. (when (calendar-cursor-to-date)
  13485. (let* ((date (calendar-cursor-to-date))
  13486. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13487. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13488. (if (active-minibuffer-window) (exit-minibuffer))))
  13489. (defun org-check-deadlines (ndays)
  13490. "Check if there are any deadlines due or past due.
  13491. A deadline is considered due if it happens within `org-deadline-warning-days'
  13492. days from today's date. If the deadline appears in an entry marked DONE,
  13493. it is not shown. The prefix arg NDAYS can be used to test that many
  13494. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13495. (interactive "P")
  13496. (let* ((org-warn-days
  13497. (cond
  13498. ((equal ndays '(4)) 100000)
  13499. (ndays (prefix-numeric-value ndays))
  13500. (t (abs org-deadline-warning-days))))
  13501. (case-fold-search nil)
  13502. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13503. (callback
  13504. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13505. (message "%d deadlines past-due or due within %d days"
  13506. (org-occur regexp nil callback)
  13507. org-warn-days)))
  13508. (defun org-check-before-date (date)
  13509. "Check if there are deadlines or scheduled entries before DATE."
  13510. (interactive (list (org-read-date)))
  13511. (let ((case-fold-search nil)
  13512. (regexp (concat "\\<\\(" org-deadline-string
  13513. "\\|" org-scheduled-string
  13514. "\\) *<\\([^>]+\\)>"))
  13515. (callback
  13516. (lambda () (time-less-p
  13517. (org-time-string-to-time (match-string 2))
  13518. (org-time-string-to-time date)))))
  13519. (message "%d entries before %s"
  13520. (org-occur regexp nil callback) date)))
  13521. (defun org-check-after-date (date)
  13522. "Check if there are deadlines or scheduled entries after DATE."
  13523. (interactive (list (org-read-date)))
  13524. (let ((case-fold-search nil)
  13525. (regexp (concat "\\<\\(" org-deadline-string
  13526. "\\|" org-scheduled-string
  13527. "\\) *<\\([^>]+\\)>"))
  13528. (callback
  13529. (lambda () (not
  13530. (time-less-p
  13531. (org-time-string-to-time (match-string 2))
  13532. (org-time-string-to-time date))))))
  13533. (message "%d entries after %s"
  13534. (org-occur regexp nil callback) date)))
  13535. (defun org-evaluate-time-range (&optional to-buffer)
  13536. "Evaluate a time range by computing the difference between start and end.
  13537. Normally the result is just printed in the echo area, but with prefix arg
  13538. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13539. If the time range is actually in a table, the result is inserted into the
  13540. next column.
  13541. For time difference computation, a year is assumed to be exactly 365
  13542. days in order to avoid rounding problems."
  13543. (interactive "P")
  13544. (or
  13545. (org-clock-update-time-maybe)
  13546. (save-excursion
  13547. (unless (org-at-date-range-p t)
  13548. (goto-char (point-at-bol))
  13549. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13550. (if (not (org-at-date-range-p t))
  13551. (error "Not at a time-stamp range, and none found in current line")))
  13552. (let* ((ts1 (match-string 1))
  13553. (ts2 (match-string 2))
  13554. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13555. (match-end (match-end 0))
  13556. (time1 (org-time-string-to-time ts1))
  13557. (time2 (org-time-string-to-time ts2))
  13558. (t1 (org-float-time time1))
  13559. (t2 (org-float-time time2))
  13560. (diff (abs (- t2 t1)))
  13561. (negative (< (- t2 t1) 0))
  13562. ;; (ys (floor (* 365 24 60 60)))
  13563. (ds (* 24 60 60))
  13564. (hs (* 60 60))
  13565. (fy "%dy %dd %02d:%02d")
  13566. (fy1 "%dy %dd")
  13567. (fd "%dd %02d:%02d")
  13568. (fd1 "%dd")
  13569. (fh "%02d:%02d")
  13570. y d h m align)
  13571. (if havetime
  13572. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13573. y 0
  13574. d (floor (/ diff ds)) diff (mod diff ds)
  13575. h (floor (/ diff hs)) diff (mod diff hs)
  13576. m (floor (/ diff 60)))
  13577. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13578. y 0
  13579. d (floor (+ (/ diff ds) 0.5))
  13580. h 0 m 0))
  13581. (if (not to-buffer)
  13582. (message "%s" (org-make-tdiff-string y d h m))
  13583. (if (org-at-table-p)
  13584. (progn
  13585. (goto-char match-end)
  13586. (setq align t)
  13587. (and (looking-at " *|") (goto-char (match-end 0))))
  13588. (goto-char match-end))
  13589. (if (looking-at
  13590. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13591. (replace-match ""))
  13592. (if negative (insert " -"))
  13593. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13594. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13595. (insert " " (format fh h m))))
  13596. (if align (org-table-align))
  13597. (message "Time difference inserted")))))
  13598. (defun org-make-tdiff-string (y d h m)
  13599. (let ((fmt "")
  13600. (l nil))
  13601. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13602. l (push y l)))
  13603. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13604. l (push d l)))
  13605. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13606. l (push h l)))
  13607. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13608. l (push m l)))
  13609. (apply 'format fmt (nreverse l))))
  13610. (defun org-time-string-to-time (s)
  13611. (apply 'encode-time (org-parse-time-string s)))
  13612. (defun org-time-string-to-seconds (s)
  13613. (org-float-time (org-time-string-to-time s)))
  13614. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13615. "Convert a time stamp to an absolute day number.
  13616. If there is a specifier for a cyclic time stamp, get the closest date to
  13617. DAYNR.
  13618. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13619. the variable date is bound by the calendar when this is called."
  13620. (cond
  13621. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13622. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13623. daynr
  13624. (+ daynr 1000)))
  13625. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13626. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13627. (time-to-days (current-time))) (match-string 0 s)
  13628. prefer show-all))
  13629. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13630. (defun org-days-to-iso-week (days)
  13631. "Return the iso week number."
  13632. (require 'cal-iso)
  13633. (car (calendar-iso-from-absolute days)))
  13634. (defun org-small-year-to-year (year)
  13635. "Convert 2-digit years into 4-digit years.
  13636. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13637. The year 2000 cannot be abbreviated. Any year larger than 99
  13638. is returned unchanged."
  13639. (if (< year 38)
  13640. (setq year (+ 2000 year))
  13641. (if (< year 100)
  13642. (setq year (+ 1900 year))))
  13643. year)
  13644. (defun org-time-from-absolute (d)
  13645. "Return the time corresponding to date D.
  13646. D may be an absolute day number, or a calendar-type list (month day year)."
  13647. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13648. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13649. (defun org-calendar-holiday ()
  13650. "List of holidays, for Diary display in Org-mode."
  13651. (require 'holidays)
  13652. (let ((hl (funcall
  13653. (if (fboundp 'calendar-check-holidays)
  13654. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13655. (if hl (mapconcat 'identity hl "; "))))
  13656. (defun org-diary-sexp-entry (sexp entry date)
  13657. "Process a SEXP diary ENTRY for DATE."
  13658. (require 'diary-lib)
  13659. (let ((result (if calendar-debug-sexp
  13660. (let ((stack-trace-on-error t))
  13661. (eval (car (read-from-string sexp))))
  13662. (condition-case nil
  13663. (eval (car (read-from-string sexp)))
  13664. (error
  13665. (beep)
  13666. (message "Bad sexp at line %d in %s: %s"
  13667. (org-current-line)
  13668. (buffer-file-name) sexp)
  13669. (sleep-for 2))))))
  13670. (cond ((stringp result) (split-string result "; "))
  13671. ((and (consp result)
  13672. (not (consp (cdr result)))
  13673. (stringp (cdr result))) (cdr result))
  13674. ((and (consp result)
  13675. (stringp (car result))) result)
  13676. (result entry)
  13677. (t nil))))
  13678. (defun org-diary-to-ical-string (frombuf)
  13679. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13680. This uses the icalendar.el library."
  13681. (let* ((tmpdir (if (featurep 'xemacs)
  13682. (temp-directory)
  13683. temporary-file-directory))
  13684. (tmpfile (make-temp-name
  13685. (expand-file-name "orgics" tmpdir)))
  13686. buf rtn b e)
  13687. (with-current-buffer frombuf
  13688. (icalendar-export-region (point-min) (point-max) tmpfile)
  13689. (setq buf (find-buffer-visiting tmpfile))
  13690. (set-buffer buf)
  13691. (goto-char (point-min))
  13692. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13693. (setq b (match-beginning 0)))
  13694. (goto-char (point-max))
  13695. (if (re-search-backward "^END:VEVENT" nil t)
  13696. (setq e (match-end 0)))
  13697. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13698. (kill-buffer buf)
  13699. (delete-file tmpfile)
  13700. rtn))
  13701. (defun org-closest-date (start current change prefer show-all)
  13702. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13703. When PREFER is `past' return a date that is either CURRENT or past.
  13704. When PREFER is `future', return a date that is either CURRENT or future.
  13705. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13706. ;; Make the proper lists from the dates
  13707. (catch 'exit
  13708. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13709. dn dw sday cday n1 n2 n0
  13710. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13711. (setq start (org-date-to-gregorian start)
  13712. current (org-date-to-gregorian
  13713. (if show-all
  13714. current
  13715. (time-to-days (current-time))))
  13716. sday (calendar-absolute-from-gregorian start)
  13717. cday (calendar-absolute-from-gregorian current))
  13718. (if (<= cday sday) (throw 'exit sday))
  13719. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13720. (setq dn (string-to-number (match-string 1 change))
  13721. dw (cdr (assoc (match-string 2 change) a1)))
  13722. (error "Invalid change specifier: %s" change))
  13723. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13724. (cond
  13725. ((eq dw 'day)
  13726. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13727. n2 (+ n1 dn)))
  13728. ((eq dw 'year)
  13729. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13730. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13731. (setq date1 (list m d y1)
  13732. n1 (calendar-absolute-from-gregorian date1)
  13733. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13734. n2 (calendar-absolute-from-gregorian date2)))
  13735. ((eq dw 'month)
  13736. ;; approx number of month between the two dates
  13737. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13738. ;; How often does dn fit in there?
  13739. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13740. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13741. m (+ m nm)
  13742. ny (floor (/ m 12))
  13743. y (+ y ny)
  13744. m (- m (* ny 12)))
  13745. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13746. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13747. (setq m2 (+ m dn) y2 y)
  13748. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13749. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13750. (while (<= n2 cday)
  13751. (setq n1 n2 m m2 y y2)
  13752. (setq m2 (+ m dn) y2 y)
  13753. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13754. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13755. ;; Make sure n1 is the earlier date
  13756. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13757. (if show-all
  13758. (cond
  13759. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13760. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13761. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13762. (cond
  13763. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13764. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13765. (t (if (= cday n1) n1 n2)))))))
  13766. (defun org-date-to-gregorian (date)
  13767. "Turn any specification of DATE into a Gregorian date for the calendar."
  13768. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13769. ((and (listp date) (= (length date) 3)) date)
  13770. ((stringp date)
  13771. (setq date (org-parse-time-string date))
  13772. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13773. ((listp date)
  13774. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13775. (defun org-parse-time-string (s &optional nodefault)
  13776. "Parse the standard Org-mode time string.
  13777. This should be a lot faster than the normal `parse-time-string'.
  13778. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13779. hour and minute fields will be nil if not given."
  13780. (if (string-match org-ts-regexp0 s)
  13781. (list 0
  13782. (if (or (match-beginning 8) (not nodefault))
  13783. (string-to-number (or (match-string 8 s) "0")))
  13784. (if (or (match-beginning 7) (not nodefault))
  13785. (string-to-number (or (match-string 7 s) "0")))
  13786. (string-to-number (match-string 4 s))
  13787. (string-to-number (match-string 3 s))
  13788. (string-to-number (match-string 2 s))
  13789. nil nil nil)
  13790. (error "Not a standard Org-mode time string: %s" s)))
  13791. (defun org-timestamp-up (&optional arg)
  13792. "Increase the date item at the cursor by one.
  13793. If the cursor is on the year, change the year. If it is on the month or
  13794. the day, change that.
  13795. With prefix ARG, change by that many units."
  13796. (interactive "p")
  13797. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  13798. (defun org-timestamp-down (&optional arg)
  13799. "Decrease the date item at the cursor by one.
  13800. If the cursor is on the year, change the year. If it is on the month or
  13801. the day, change that.
  13802. With prefix ARG, change by that many units."
  13803. (interactive "p")
  13804. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  13805. (defun org-timestamp-up-day (&optional arg)
  13806. "Increase the date in the time stamp by one day.
  13807. With prefix ARG, change that many days."
  13808. (interactive "p")
  13809. (if (and (not (org-at-timestamp-p t))
  13810. (org-on-heading-p))
  13811. (org-todo 'up)
  13812. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  13813. (defun org-timestamp-down-day (&optional arg)
  13814. "Decrease the date in the time stamp by one day.
  13815. With prefix ARG, change that many days."
  13816. (interactive "p")
  13817. (if (and (not (org-at-timestamp-p t))
  13818. (org-on-heading-p))
  13819. (org-todo 'down)
  13820. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  13821. (defun org-at-timestamp-p (&optional inactive-ok)
  13822. "Determine if the cursor is in or at a timestamp."
  13823. (interactive)
  13824. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13825. (pos (point))
  13826. (ans (or (looking-at tsr)
  13827. (save-excursion
  13828. (skip-chars-backward "^[<\n\r\t")
  13829. (if (> (point) (point-min)) (backward-char 1))
  13830. (and (looking-at tsr)
  13831. (> (- (match-end 0) pos) -1))))))
  13832. (and ans
  13833. (boundp 'org-ts-what)
  13834. (setq org-ts-what
  13835. (cond
  13836. ((= pos (match-beginning 0)) 'bracket)
  13837. ((= pos (1- (match-end 0))) 'bracket)
  13838. ((org-pos-in-match-range pos 2) 'year)
  13839. ((org-pos-in-match-range pos 3) 'month)
  13840. ((org-pos-in-match-range pos 7) 'hour)
  13841. ((org-pos-in-match-range pos 8) 'minute)
  13842. ((or (org-pos-in-match-range pos 4)
  13843. (org-pos-in-match-range pos 5)) 'day)
  13844. ((and (> pos (or (match-end 8) (match-end 5)))
  13845. (< pos (match-end 0)))
  13846. (- pos (or (match-end 8) (match-end 5))))
  13847. (t 'day))))
  13848. ans))
  13849. (defun org-toggle-timestamp-type ()
  13850. "Toggle the type (<active> or [inactive]) of a time stamp."
  13851. (interactive)
  13852. (when (org-at-timestamp-p t)
  13853. (let ((beg (match-beginning 0)) (end (match-end 0))
  13854. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13855. (save-excursion
  13856. (goto-char beg)
  13857. (while (re-search-forward "[][<>]" end t)
  13858. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13859. t t)))
  13860. (message "Timestamp is now %sactive"
  13861. (if (equal (char-after beg) ?<) "" "in")))))
  13862. (defun org-timestamp-change (n &optional what updown)
  13863. "Change the date in the time stamp at point.
  13864. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13865. `year', `minute', `second'. If WHAT is not given, the cursor position
  13866. in the timestamp determines what will be changed."
  13867. (let ((pos (point))
  13868. with-hm inactive
  13869. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13870. org-ts-what
  13871. extra rem
  13872. ts time time0)
  13873. (if (not (org-at-timestamp-p t))
  13874. (error "Not at a timestamp"))
  13875. (if (and (not what) (eq org-ts-what 'bracket))
  13876. (org-toggle-timestamp-type)
  13877. (if (and (not what) (not (eq org-ts-what 'day))
  13878. org-display-custom-times
  13879. (get-text-property (point) 'display)
  13880. (not (get-text-property (1- (point)) 'display)))
  13881. (setq org-ts-what 'day))
  13882. (setq org-ts-what (or what org-ts-what)
  13883. inactive (= (char-after (match-beginning 0)) ?\[)
  13884. ts (match-string 0))
  13885. (replace-match "")
  13886. (if (string-match
  13887. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13888. ts)
  13889. (setq extra (match-string 1 ts)))
  13890. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13891. (setq with-hm t))
  13892. (setq time0 (org-parse-time-string ts))
  13893. (when (and updown
  13894. (eq org-ts-what 'minute)
  13895. (not current-prefix-arg))
  13896. ;; This looks like s-up and s-down. Change by one rounding step.
  13897. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13898. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13899. (setcar (cdr time0) (+ (nth 1 time0)
  13900. (if (> n 0) (- rem) (- dm rem))))))
  13901. (setq time
  13902. (encode-time (or (car time0) 0)
  13903. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13904. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13905. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13906. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13907. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13908. (nthcdr 6 time0)))
  13909. (when (and (member org-ts-what '(hour minute))
  13910. extra
  13911. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13912. (setq extra (org-modify-ts-extra
  13913. extra
  13914. (if (eq org-ts-what 'hour) 2 5)
  13915. n dm)))
  13916. (when (integerp org-ts-what)
  13917. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13918. (if (eq what 'calendar)
  13919. (let ((cal-date (org-get-date-from-calendar)))
  13920. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13921. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13922. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13923. (setcar time0 (or (car time0) 0))
  13924. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13925. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13926. (setq time (apply 'encode-time time0))))
  13927. (setq org-last-changed-timestamp
  13928. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13929. (org-clock-update-time-maybe)
  13930. (goto-char pos)
  13931. ;; Try to recenter the calendar window, if any
  13932. (if (and org-calendar-follow-timestamp-change
  13933. (get-buffer-window "*Calendar*" t)
  13934. (memq org-ts-what '(day month year)))
  13935. (org-recenter-calendar (time-to-days time))))))
  13936. (defun org-modify-ts-extra (s pos n dm)
  13937. "Change the different parts of the lead-time and repeat fields in timestamp."
  13938. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13939. ng h m new rem)
  13940. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13941. (cond
  13942. ((or (org-pos-in-match-range pos 2)
  13943. (org-pos-in-match-range pos 3))
  13944. (setq m (string-to-number (match-string 3 s))
  13945. h (string-to-number (match-string 2 s)))
  13946. (if (org-pos-in-match-range pos 2)
  13947. (setq h (+ h n))
  13948. (setq n (* dm (org-no-warnings (signum n))))
  13949. (when (not (= 0 (setq rem (% m dm))))
  13950. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13951. (setq m (+ m n)))
  13952. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13953. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13954. (setq h (min 24 (max 0 h)))
  13955. (setq ng 1 new (format "-%02d:%02d" h m)))
  13956. ((org-pos-in-match-range pos 6)
  13957. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13958. ((org-pos-in-match-range pos 5)
  13959. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13960. ((org-pos-in-match-range pos 9)
  13961. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13962. ((org-pos-in-match-range pos 8)
  13963. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13964. (when ng
  13965. (setq s (concat
  13966. (substring s 0 (match-beginning ng))
  13967. new
  13968. (substring s (match-end ng))))))
  13969. s))
  13970. (defun org-recenter-calendar (date)
  13971. "If the calendar is visible, recenter it to DATE."
  13972. (let* ((win (selected-window))
  13973. (cwin (get-buffer-window "*Calendar*" t))
  13974. (calendar-move-hook nil))
  13975. (when cwin
  13976. (select-window cwin)
  13977. (calendar-goto-date (if (listp date) date
  13978. (calendar-gregorian-from-absolute date)))
  13979. (select-window win))))
  13980. (defun org-goto-calendar (&optional arg)
  13981. "Go to the Emacs calendar at the current date.
  13982. If there is a time stamp in the current line, go to that date.
  13983. A prefix ARG can be used to force the current date."
  13984. (interactive "P")
  13985. (let ((tsr org-ts-regexp) diff
  13986. (calendar-move-hook nil)
  13987. (calendar-view-holidays-initially-flag nil)
  13988. (calendar-view-diary-initially-flag nil))
  13989. (if (or (org-at-timestamp-p)
  13990. (save-excursion
  13991. (beginning-of-line 1)
  13992. (looking-at (concat ".*" tsr))))
  13993. (let ((d1 (time-to-days (current-time)))
  13994. (d2 (time-to-days
  13995. (org-time-string-to-time (match-string 1)))))
  13996. (setq diff (- d2 d1))))
  13997. (calendar)
  13998. (calendar-goto-today)
  13999. (if (and diff (not arg)) (calendar-forward-day diff))))
  14000. (defun org-get-date-from-calendar ()
  14001. "Return a list (month day year) of date at point in calendar."
  14002. (with-current-buffer "*Calendar*"
  14003. (save-match-data
  14004. (calendar-cursor-to-date))))
  14005. (defun org-date-from-calendar ()
  14006. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14007. If there is already a time stamp at the cursor position, update it."
  14008. (interactive)
  14009. (if (org-at-timestamp-p t)
  14010. (org-timestamp-change 0 'calendar)
  14011. (let ((cal-date (org-get-date-from-calendar)))
  14012. (org-insert-time-stamp
  14013. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14014. (defun org-minutes-to-hh:mm-string (m)
  14015. "Compute H:MM from a number of minutes."
  14016. (let ((h (/ m 60)))
  14017. (setq m (- m (* 60 h)))
  14018. (format org-time-clocksum-format h m)))
  14019. (defun org-hh:mm-string-to-minutes (s)
  14020. "Convert a string H:MM to a number of minutes.
  14021. If the string is just a number, interpret it as minutes.
  14022. In fact, the first hh:mm or number in the string will be taken,
  14023. there can be extra stuff in the string.
  14024. If no number is found, the return value is 0."
  14025. (cond
  14026. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14027. (+ (* (string-to-number (match-string 1 s)) 60)
  14028. (string-to-number (match-string 2 s))))
  14029. ((string-match "\\([0-9]+\\)" s)
  14030. (string-to-number (match-string 1 s)))
  14031. (t 0)))
  14032. ;;;; Files
  14033. (defun org-save-all-org-buffers ()
  14034. "Save all Org-mode buffers without user confirmation."
  14035. (interactive)
  14036. (message "Saving all Org-mode buffers...")
  14037. (save-some-buffers t 'org-mode-p)
  14038. (when (featurep 'org-id) (org-id-locations-save))
  14039. (message "Saving all Org-mode buffers... done"))
  14040. (defun org-revert-all-org-buffers ()
  14041. "Revert all Org-mode buffers.
  14042. Prompt for confirmation when there are unsaved changes.
  14043. Be sure you know what you are doing before letting this function
  14044. overwrite your changes.
  14045. This function is useful in a setup where one tracks org files
  14046. with a version control system, to revert on one machine after pulling
  14047. changes from another. I believe the procedure must be like this:
  14048. 1. M-x org-save-all-org-buffers
  14049. 2. Pull changes from the other machine, resolve conflicts
  14050. 3. M-x org-revert-all-org-buffers"
  14051. (interactive)
  14052. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14053. (error "Abort"))
  14054. (save-excursion
  14055. (save-window-excursion
  14056. (mapc
  14057. (lambda (b)
  14058. (when (and (with-current-buffer b (org-mode-p))
  14059. (with-current-buffer b buffer-file-name))
  14060. (switch-to-buffer b)
  14061. (revert-buffer t 'no-confirm)))
  14062. (buffer-list))
  14063. (when (and (featurep 'org-id) org-id-track-globally)
  14064. (org-id-locations-load)))))
  14065. ;;;; Agenda files
  14066. ;;;###autoload
  14067. (defun org-switchb (&optional arg)
  14068. "Switch between Org buffers.
  14069. With a prefix argument, restrict available to files.
  14070. With two prefix arguments, restrict available buffers to agenda files.
  14071. Defaults to `iswitchb' for buffer name completion.
  14072. Set `org-completion-use-ido' to make it use ido instead."
  14073. (interactive "P")
  14074. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14075. ((equal arg '(16)) (org-buffer-list 'agenda))
  14076. (t (org-buffer-list))))
  14077. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14078. (org-completion-use-ido org-completion-use-ido))
  14079. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14080. (setq org-completion-use-iswitchb t))
  14081. (switch-to-buffer
  14082. (org-icompleting-read "Org buffer: "
  14083. (mapcar 'list (mapcar 'buffer-name blist))
  14084. nil t))))
  14085. ;;; Define some older names previously used for this functionality
  14086. ;;;###autoload
  14087. (defalias 'org-ido-switchb 'org-switchb)
  14088. ;;;###autoload
  14089. (defalias 'org-iswitchb 'org-switchb)
  14090. (defun org-buffer-list (&optional predicate exclude-tmp)
  14091. "Return a list of Org buffers.
  14092. PREDICATE can be `export', `files' or `agenda'.
  14093. export restrict the list to Export buffers.
  14094. files restrict the list to buffers visiting Org files.
  14095. agenda restrict the list to buffers visiting agenda files.
  14096. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14097. (let* ((bfn nil)
  14098. (agenda-files (and (eq predicate 'agenda)
  14099. (mapcar 'file-truename (org-agenda-files t))))
  14100. (filter
  14101. (cond
  14102. ((eq predicate 'files)
  14103. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14104. ((eq predicate 'export)
  14105. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14106. ((eq predicate 'agenda)
  14107. (lambda (b)
  14108. (with-current-buffer b
  14109. (and (eq major-mode 'org-mode)
  14110. (setq bfn (buffer-file-name b))
  14111. (member (file-truename bfn) agenda-files)))))
  14112. (t (lambda (b) (with-current-buffer b
  14113. (or (eq major-mode 'org-mode)
  14114. (string-match "\*Org .*Export"
  14115. (buffer-name b)))))))))
  14116. (delq nil
  14117. (mapcar
  14118. (lambda(b)
  14119. (if (and (funcall filter b)
  14120. (or (not exclude-tmp)
  14121. (not (string-match "tmp" (buffer-name b)))))
  14122. b
  14123. nil))
  14124. (buffer-list)))))
  14125. (defun org-agenda-files (&optional unrestricted archives)
  14126. "Get the list of agenda files.
  14127. Optional UNRESTRICTED means return the full list even if a restriction
  14128. is currently in place.
  14129. When ARCHIVES is t, include all archive files that are really being
  14130. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14131. `org-agenda-archives-mode' is t."
  14132. (let ((files
  14133. (cond
  14134. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14135. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14136. ((listp org-agenda-files) org-agenda-files)
  14137. (t (error "Invalid value of `org-agenda-files'")))))
  14138. (setq files (apply 'append
  14139. (mapcar (lambda (f)
  14140. (if (file-directory-p f)
  14141. (directory-files
  14142. f t org-agenda-file-regexp)
  14143. (list f)))
  14144. files)))
  14145. (when org-agenda-skip-unavailable-files
  14146. (setq files (delq nil
  14147. (mapcar (function
  14148. (lambda (file)
  14149. (and (file-readable-p file) file)))
  14150. files))))
  14151. (when (or (eq archives t)
  14152. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14153. (setq files (org-add-archive-files files)))
  14154. files))
  14155. (defun org-agenda-file-p (&optional file)
  14156. "Return non-nil, if FILE is an agenda file.
  14157. If FILE is omitted, use the file associated with the current
  14158. buffer."
  14159. (member (or file (buffer-file-name))
  14160. (org-agenda-files t)))
  14161. (defun org-edit-agenda-file-list ()
  14162. "Edit the list of agenda files.
  14163. Depending on setup, this either uses customize to edit the variable
  14164. `org-agenda-files', or it visits the file that is holding the list. In the
  14165. latter case, the buffer is set up in a way that saving it automatically kills
  14166. the buffer and restores the previous window configuration."
  14167. (interactive)
  14168. (if (stringp org-agenda-files)
  14169. (let ((cw (current-window-configuration)))
  14170. (find-file org-agenda-files)
  14171. (org-set-local 'org-window-configuration cw)
  14172. (org-add-hook 'after-save-hook
  14173. (lambda ()
  14174. (set-window-configuration
  14175. (prog1 org-window-configuration
  14176. (kill-buffer (current-buffer))))
  14177. (org-install-agenda-files-menu)
  14178. (message "New agenda file list installed"))
  14179. nil 'local)
  14180. (message "%s" (substitute-command-keys
  14181. "Edit list and finish with \\[save-buffer]")))
  14182. (customize-variable 'org-agenda-files)))
  14183. (defun org-store-new-agenda-file-list (list)
  14184. "Set new value for the agenda file list and save it correctly."
  14185. (if (stringp org-agenda-files)
  14186. (let ((fe (org-read-agenda-file-list t)) b u)
  14187. (while (setq b (find-buffer-visiting org-agenda-files))
  14188. (kill-buffer b))
  14189. (with-temp-file org-agenda-files
  14190. (insert
  14191. (mapconcat
  14192. (lambda (f) ;; Keep un-expanded entries.
  14193. (if (setq u (assoc f fe))
  14194. (cdr u)
  14195. f))
  14196. list "\n")
  14197. "\n")))
  14198. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14199. (org-insert-mode-line-in-empty-file nil))
  14200. (setq org-agenda-files list)
  14201. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14202. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14203. "Read the list of agenda files from a file.
  14204. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14205. filenames, used by `org-store-new-agenda-file-list' to write back
  14206. un-expanded file names."
  14207. (when (file-directory-p org-agenda-files)
  14208. (error "`org-agenda-files' cannot be a single directory"))
  14209. (when (stringp org-agenda-files)
  14210. (with-temp-buffer
  14211. (insert-file-contents org-agenda-files)
  14212. (mapcar
  14213. (lambda (f)
  14214. (let ((e (expand-file-name (substitute-in-file-name f)
  14215. org-directory)))
  14216. (if pair-with-expansion
  14217. (cons e f)
  14218. e)))
  14219. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14220. ;;;###autoload
  14221. (defun org-cycle-agenda-files ()
  14222. "Cycle through the files in `org-agenda-files'.
  14223. If the current buffer visits an agenda file, find the next one in the list.
  14224. If the current buffer does not, find the first agenda file."
  14225. (interactive)
  14226. (let* ((fs (org-agenda-files t))
  14227. (files (append fs (list (car fs))))
  14228. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14229. file)
  14230. (unless files (error "No agenda files"))
  14231. (catch 'exit
  14232. (while (setq file (pop files))
  14233. (if (equal (file-truename file) tcf)
  14234. (when (car files)
  14235. (find-file (car files))
  14236. (throw 'exit t))))
  14237. (find-file (car fs)))
  14238. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14239. (defun org-agenda-file-to-front (&optional to-end)
  14240. "Move/add the current file to the top of the agenda file list.
  14241. If the file is not present in the list, it is added to the front. If it is
  14242. present, it is moved there. With optional argument TO-END, add/move to the
  14243. end of the list."
  14244. (interactive "P")
  14245. (let ((org-agenda-skip-unavailable-files nil)
  14246. (file-alist (mapcar (lambda (x)
  14247. (cons (file-truename x) x))
  14248. (org-agenda-files t)))
  14249. (ctf (file-truename buffer-file-name))
  14250. x had)
  14251. (setq x (assoc ctf file-alist) had x)
  14252. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14253. (if to-end
  14254. (setq file-alist (append (delq x file-alist) (list x)))
  14255. (setq file-alist (cons x (delq x file-alist))))
  14256. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14257. (org-install-agenda-files-menu)
  14258. (message "File %s to %s of agenda file list"
  14259. (if had "moved" "added") (if to-end "end" "front"))))
  14260. (defun org-remove-file (&optional file)
  14261. "Remove current file from the list of files in variable `org-agenda-files'.
  14262. These are the files which are being checked for agenda entries.
  14263. Optional argument FILE means use this file instead of the current."
  14264. (interactive)
  14265. (let* ((org-agenda-skip-unavailable-files nil)
  14266. (file (or file buffer-file-name))
  14267. (true-file (file-truename file))
  14268. (afile (abbreviate-file-name file))
  14269. (files (delq nil (mapcar
  14270. (lambda (x)
  14271. (if (equal true-file
  14272. (file-truename x))
  14273. nil x))
  14274. (org-agenda-files t)))))
  14275. (if (not (= (length files) (length (org-agenda-files t))))
  14276. (progn
  14277. (org-store-new-agenda-file-list files)
  14278. (org-install-agenda-files-menu)
  14279. (message "Removed file: %s" afile))
  14280. (message "File was not in list: %s (not removed)" afile))))
  14281. (defun org-file-menu-entry (file)
  14282. (vector file (list 'find-file file) t))
  14283. (defun org-check-agenda-file (file)
  14284. "Make sure FILE exists. If not, ask user what to do."
  14285. (when (not (file-exists-p file))
  14286. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14287. (abbreviate-file-name file))
  14288. (let ((r (downcase (read-char-exclusive))))
  14289. (cond
  14290. ((equal r ?r)
  14291. (org-remove-file file)
  14292. (throw 'nextfile t))
  14293. (t (error "Abort"))))))
  14294. (defun org-get-agenda-file-buffer (file)
  14295. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14296. it to the list of buffers which might be released later."
  14297. (let ((buf (org-find-base-buffer-visiting file)))
  14298. (if buf
  14299. buf ; just return it
  14300. ;; Make a new buffer and remember it
  14301. (setq buf (find-file-noselect file))
  14302. (if buf (push buf org-agenda-new-buffers))
  14303. buf)))
  14304. (defun org-release-buffers (blist)
  14305. "Release all buffers in list, asking the user for confirmation when needed.
  14306. When a buffer is unmodified, it is just killed. When modified, it is saved
  14307. \(if the user agrees) and then killed."
  14308. (let (buf file)
  14309. (while (setq buf (pop blist))
  14310. (setq file (buffer-file-name buf))
  14311. (when (and (buffer-modified-p buf)
  14312. file
  14313. (y-or-n-p (format "Save file %s? " file)))
  14314. (with-current-buffer buf (save-buffer)))
  14315. (kill-buffer buf))))
  14316. (defun org-prepare-agenda-buffers (files)
  14317. "Create buffers for all agenda files, protect archived trees and comments."
  14318. (interactive)
  14319. (let ((pa '(:org-archived t))
  14320. (pc '(:org-comment t))
  14321. (pall '(:org-archived t :org-comment t))
  14322. (inhibit-read-only t)
  14323. (rea (concat ":" org-archive-tag ":"))
  14324. bmp file re)
  14325. (save-excursion
  14326. (save-restriction
  14327. (while (setq file (pop files))
  14328. (catch 'nextfile
  14329. (if (bufferp file)
  14330. (set-buffer file)
  14331. (org-check-agenda-file file)
  14332. (set-buffer (org-get-agenda-file-buffer file)))
  14333. (widen)
  14334. (setq bmp (buffer-modified-p))
  14335. (org-refresh-category-properties)
  14336. (setq org-todo-keywords-for-agenda
  14337. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14338. (setq org-done-keywords-for-agenda
  14339. (append org-done-keywords-for-agenda org-done-keywords))
  14340. (setq org-todo-keyword-alist-for-agenda
  14341. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14342. (setq org-drawers-for-agenda
  14343. (append org-drawers-for-agenda org-drawers))
  14344. (setq org-tag-alist-for-agenda
  14345. (append org-tag-alist-for-agenda org-tag-alist))
  14346. (save-excursion
  14347. (remove-text-properties (point-min) (point-max) pall)
  14348. (when org-agenda-skip-archived-trees
  14349. (goto-char (point-min))
  14350. (while (re-search-forward rea nil t)
  14351. (if (org-on-heading-p t)
  14352. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14353. (goto-char (point-min))
  14354. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14355. (while (re-search-forward re nil t)
  14356. (add-text-properties
  14357. (match-beginning 0) (org-end-of-subtree t) pc)))
  14358. (set-buffer-modified-p bmp)))))
  14359. (setq org-todo-keywords-for-agenda
  14360. (org-uniquify org-todo-keywords-for-agenda))
  14361. (setq org-todo-keyword-alist-for-agenda
  14362. (org-uniquify org-todo-keyword-alist-for-agenda)
  14363. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14364. ;;;; Embedded LaTeX
  14365. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14366. "Keymap for the minor `org-cdlatex-mode'.")
  14367. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14368. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14369. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14370. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14371. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14372. (defvar org-cdlatex-texmathp-advice-is-done nil
  14373. "Flag remembering if we have applied the advice to texmathp already.")
  14374. (define-minor-mode org-cdlatex-mode
  14375. "Toggle the minor `org-cdlatex-mode'.
  14376. This mode supports entering LaTeX environment and math in LaTeX fragments
  14377. in Org-mode.
  14378. \\{org-cdlatex-mode-map}"
  14379. nil " OCDL" nil
  14380. (when org-cdlatex-mode (require 'cdlatex))
  14381. (unless org-cdlatex-texmathp-advice-is-done
  14382. (setq org-cdlatex-texmathp-advice-is-done t)
  14383. (defadvice texmathp (around org-math-always-on activate)
  14384. "Always return t in org-mode buffers.
  14385. This is because we want to insert math symbols without dollars even outside
  14386. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14387. an embedded LaTeX fragment, let texmathp do its job.
  14388. \\[org-cdlatex-mode-map]"
  14389. (interactive)
  14390. (let (p)
  14391. (cond
  14392. ((not (org-mode-p)) ad-do-it)
  14393. ((eq this-command 'cdlatex-math-symbol)
  14394. (setq ad-return-value t
  14395. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14396. (t
  14397. (let ((p (org-inside-LaTeX-fragment-p)))
  14398. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14399. (setq ad-return-value t
  14400. texmathp-why '("Org-mode embedded math" . 0))
  14401. (if p ad-do-it)))))))))
  14402. (defun turn-on-org-cdlatex ()
  14403. "Unconditionally turn on `org-cdlatex-mode'."
  14404. (org-cdlatex-mode 1))
  14405. (defun org-inside-LaTeX-fragment-p ()
  14406. "Test if point is inside a LaTeX fragment.
  14407. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14408. sequence appearing also before point.
  14409. Even though the matchers for math are configurable, this function assumes
  14410. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14411. delimiters are skipped when they have been removed by customization.
  14412. The return value is nil, or a cons cell with the delimiter and
  14413. and the position of this delimiter.
  14414. This function does a reasonably good job, but can locally be fooled by
  14415. for example currency specifications. For example it will assume being in
  14416. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14417. fragments that are properly closed, but during editing, we have to live
  14418. with the uncertainty caused by missing closing delimiters. This function
  14419. looks only before point, not after."
  14420. (catch 'exit
  14421. (let ((pos (point))
  14422. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14423. (lim (progn
  14424. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14425. (point)))
  14426. dd-on str (start 0) m re)
  14427. (goto-char pos)
  14428. (when dodollar
  14429. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14430. re (nth 1 (assoc "$" org-latex-regexps)))
  14431. (while (string-match re str start)
  14432. (cond
  14433. ((= (match-end 0) (length str))
  14434. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14435. ((= (match-end 0) (- (length str) 5))
  14436. (throw 'exit nil))
  14437. (t (setq start (match-end 0))))))
  14438. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14439. (goto-char pos)
  14440. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14441. (and (match-beginning 2) (throw 'exit nil))
  14442. ;; count $$
  14443. (while (re-search-backward "\\$\\$" lim t)
  14444. (setq dd-on (not dd-on)))
  14445. (goto-char pos)
  14446. (if dd-on (cons "$$" m))))))
  14447. (defun org-inside-latex-macro-p ()
  14448. "Is point inside a LaTeX macro or its arguments?"
  14449. (save-match-data
  14450. (org-in-regexp
  14451. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14452. (defun org-try-cdlatex-tab ()
  14453. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14454. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14455. - inside a LaTeX fragment, or
  14456. - after the first word in a line, where an abbreviation expansion could
  14457. insert a LaTeX environment."
  14458. (when org-cdlatex-mode
  14459. (cond
  14460. ((save-excursion
  14461. (skip-chars-backward "a-zA-Z0-9*")
  14462. (skip-chars-backward " \t")
  14463. (bolp))
  14464. (cdlatex-tab) t)
  14465. ((org-inside-LaTeX-fragment-p)
  14466. (cdlatex-tab) t)
  14467. (t nil))))
  14468. (defun org-cdlatex-underscore-caret (&optional arg)
  14469. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14470. Revert to the normal definition outside of these fragments."
  14471. (interactive "P")
  14472. (if (org-inside-LaTeX-fragment-p)
  14473. (call-interactively 'cdlatex-sub-superscript)
  14474. (let (org-cdlatex-mode)
  14475. (call-interactively (key-binding (vector last-input-event))))))
  14476. (defun org-cdlatex-math-modify (&optional arg)
  14477. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14478. Revert to the normal definition outside of these fragments."
  14479. (interactive "P")
  14480. (if (org-inside-LaTeX-fragment-p)
  14481. (call-interactively 'cdlatex-math-modify)
  14482. (let (org-cdlatex-mode)
  14483. (call-interactively (key-binding (vector last-input-event))))))
  14484. (defvar org-latex-fragment-image-overlays nil
  14485. "List of overlays carrying the images of latex fragments.")
  14486. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14487. (defun org-remove-latex-fragment-image-overlays ()
  14488. "Remove all overlays with LaTeX fragment images in current buffer."
  14489. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14490. (setq org-latex-fragment-image-overlays nil))
  14491. (defun org-preview-latex-fragment (&optional subtree)
  14492. "Preview the LaTeX fragment at point, or all locally or globally.
  14493. If the cursor is in a LaTeX fragment, create the image and overlay
  14494. it over the source code. If there is no fragment at point, display
  14495. all fragments in the current text, from one headline to the next. With
  14496. prefix SUBTREE, display all fragments in the current subtree. With a
  14497. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14498. the cursor is before the first headline,
  14499. display all fragments in the buffer.
  14500. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14501. (interactive "P")
  14502. (org-remove-latex-fragment-image-overlays)
  14503. (save-excursion
  14504. (save-restriction
  14505. (let (beg end at msg)
  14506. (cond
  14507. ((or (equal subtree '(16))
  14508. (not (save-excursion
  14509. (re-search-backward (concat "^" outline-regexp) nil t))))
  14510. (setq beg (point-min) end (point-max)
  14511. msg "Creating images for buffer...%s"))
  14512. ((equal subtree '(4))
  14513. (org-back-to-heading)
  14514. (setq beg (point) end (org-end-of-subtree t)
  14515. msg "Creating images for subtree...%s"))
  14516. (t
  14517. (if (setq at (org-inside-LaTeX-fragment-p))
  14518. (goto-char (max (point-min) (- (cdr at) 2)))
  14519. (org-back-to-heading))
  14520. (setq beg (point) end (progn (outline-next-heading) (point))
  14521. msg (if at "Creating image...%s"
  14522. "Creating images for entry...%s"))))
  14523. (message msg "")
  14524. (narrow-to-region beg end)
  14525. (goto-char beg)
  14526. (org-format-latex
  14527. (concat "ltxpng/" (file-name-sans-extension
  14528. (file-name-nondirectory
  14529. buffer-file-name)))
  14530. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14531. (message msg "done. Use `C-c C-c' to remove images.")))))
  14532. (defvar org-latex-regexps
  14533. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14534. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14535. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14536. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14537. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14538. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14539. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14540. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14541. "Regular expressions for matching embedded LaTeX.")
  14542. (defvar org-export-have-math nil) ;; dynamic scoping
  14543. (defun org-format-latex (prefix &optional dir overlays msg at
  14544. forbuffer processing-type)
  14545. "Replace LaTeX fragments with links to an image, and produce images.
  14546. Some of the options can be changed using the variable
  14547. `org-format-latex-options'."
  14548. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14549. (let* ((prefixnodir (file-name-nondirectory prefix))
  14550. (absprefix (expand-file-name prefix dir))
  14551. (todir (file-name-directory absprefix))
  14552. (opt org-format-latex-options)
  14553. (matchers (plist-get opt :matchers))
  14554. (re-list org-latex-regexps)
  14555. (org-format-latex-header-extra
  14556. (plist-get (org-infile-export-plist) :latex-header-extra))
  14557. (cnt 0) txt hash link beg end re e checkdir
  14558. executables-checked string
  14559. m n block linkfile movefile ov)
  14560. ;; Check the different regular expressions
  14561. (while (setq e (pop re-list))
  14562. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14563. block (if (nth 3 e) "\n\n" ""))
  14564. (when (member m matchers)
  14565. (goto-char (point-min))
  14566. (while (re-search-forward re nil t)
  14567. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14568. (not (get-text-property (match-beginning n)
  14569. 'org-protected))
  14570. (or (not overlays)
  14571. (not (eq (get-char-property (match-beginning n)
  14572. 'org-overlay-type)
  14573. 'org-latex-overlay))))
  14574. (setq org-export-have-math t)
  14575. (cond
  14576. ((eq processing-type 'verbatim)
  14577. ;; Leave the text verbatim, just protect it
  14578. (add-text-properties (match-beginning n) (match-end n)
  14579. '(org-protected t)))
  14580. ((eq processing-type 'mathjax)
  14581. ;; Prepare for MathJax processing
  14582. (setq string (match-string n))
  14583. (if (member m '("$" "$1"))
  14584. (save-excursion
  14585. (delete-region (match-beginning n) (match-end n))
  14586. (goto-char (match-beginning n))
  14587. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14588. "\\)")
  14589. '(org-protected t))))
  14590. (add-text-properties (match-beginning n) (match-end n)
  14591. '(org-protected t))))
  14592. ((or (eq processing-type 'dvipng) t)
  14593. ;; Process to an image
  14594. (setq txt (match-string n)
  14595. beg (match-beginning n) end (match-end n)
  14596. cnt (1+ cnt))
  14597. (let (print-length print-level) ; make sure full list is printed
  14598. (setq hash (sha1 (prin1-to-string
  14599. (list org-format-latex-header
  14600. org-format-latex-header-extra
  14601. org-export-latex-default-packages-alist
  14602. org-export-latex-packages-alist
  14603. org-format-latex-options
  14604. forbuffer txt)))
  14605. linkfile (format "%s_%s.png" prefix hash)
  14606. movefile (format "%s_%s.png" absprefix hash)))
  14607. (setq link (concat block "[[file:" linkfile "]]" block))
  14608. (if msg (message msg cnt))
  14609. (goto-char beg)
  14610. (unless checkdir ; make sure the directory exists
  14611. (setq checkdir t)
  14612. (or (file-directory-p todir) (make-directory todir t)))
  14613. (unless executables-checked
  14614. (org-check-external-command
  14615. "latex" "needed to convert LaTeX fragments to images")
  14616. (org-check-external-command
  14617. "dvipng" "needed to convert LaTeX fragments to images")
  14618. (setq executables-checked t))
  14619. (unless (file-exists-p movefile)
  14620. (org-create-formula-image
  14621. txt movefile opt forbuffer))
  14622. (if overlays
  14623. (progn
  14624. (mapc (lambda (o)
  14625. (if (eq (overlay-get o 'org-overlay-type)
  14626. 'org-latex-overlay)
  14627. (delete-overlay o)))
  14628. (overlays-in beg end))
  14629. (setq ov (make-overlay beg end))
  14630. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14631. (if (featurep 'xemacs)
  14632. (progn
  14633. (overlay-put ov 'invisible t)
  14634. (overlay-put
  14635. ov 'end-glyph
  14636. (make-glyph (vector 'png :file movefile))))
  14637. (overlay-put
  14638. ov 'display
  14639. (list 'image :type 'png :file movefile :ascent 'center)))
  14640. (push ov org-latex-fragment-image-overlays)
  14641. (goto-char end))
  14642. (delete-region beg end)
  14643. (insert (org-add-props link
  14644. (list 'org-latex-src
  14645. (replace-regexp-in-string
  14646. "\"" "" txt)))))))))))))
  14647. ;; This function borrows from Ganesh Swami's latex2png.el
  14648. (defun org-create-formula-image (string tofile options buffer)
  14649. "This calls dvipng."
  14650. (require 'org-latex)
  14651. (let* ((tmpdir (if (featurep 'xemacs)
  14652. (temp-directory)
  14653. temporary-file-directory))
  14654. (texfilebase (make-temp-name
  14655. (expand-file-name "orgtex" tmpdir)))
  14656. (texfile (concat texfilebase ".tex"))
  14657. (dvifile (concat texfilebase ".dvi"))
  14658. (pngfile (concat texfilebase ".png"))
  14659. (fnh (if (featurep 'xemacs)
  14660. (font-height (get-face-font 'default))
  14661. (face-attribute 'default :height nil)))
  14662. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14663. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14664. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14665. "Black"))
  14666. (bg (or (plist-get options (if buffer :background :html-background))
  14667. "Transparent")))
  14668. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14669. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14670. (with-temp-file texfile
  14671. (insert (org-splice-latex-header
  14672. org-format-latex-header
  14673. org-export-latex-default-packages-alist
  14674. org-export-latex-packages-alist t
  14675. org-format-latex-header-extra))
  14676. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14677. (require 'org-latex)
  14678. (org-export-latex-fix-inputenc))
  14679. (let ((dir default-directory))
  14680. (condition-case nil
  14681. (progn
  14682. (cd tmpdir)
  14683. (call-process "latex" nil nil nil texfile))
  14684. (error nil))
  14685. (cd dir))
  14686. (if (not (file-exists-p dvifile))
  14687. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14688. (condition-case nil
  14689. (call-process "dvipng" nil nil nil
  14690. "-fg" fg "-bg" bg
  14691. "-D" dpi
  14692. ;;"-x" scale "-y" scale
  14693. "-T" "tight"
  14694. "-o" pngfile
  14695. dvifile)
  14696. (error nil))
  14697. (if (not (file-exists-p pngfile))
  14698. (if org-format-latex-signal-error
  14699. (error "Failed to create png file from %s" texfile)
  14700. (message "Failed to create png file from %s" texfile)
  14701. nil)
  14702. ;; Use the requested file name and clean up
  14703. (copy-file pngfile tofile 'replace)
  14704. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14705. (delete-file (concat texfilebase e)))
  14706. pngfile))))
  14707. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14708. "Fill a LaTeX header template TPL.
  14709. In the template, the following place holders will be recognized:
  14710. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14711. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14712. [PACKAGES] \\usepackage statements for PKG
  14713. [NO-PACKAGES] do not include PKG
  14714. [EXTRA] the string EXTRA
  14715. [NO-EXTRA] do not include EXTRA
  14716. For backward compatibility, if both the positive and the negative place
  14717. holder is missing, the positive one (without the \"NO-\") will be
  14718. assumed to be present at the end of the template.
  14719. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14720. EXTRA is a string.
  14721. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14722. (let (rpl (end ""))
  14723. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14724. (setq rpl (if (or (match-end 1) (not def-pkg))
  14725. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14726. tpl (replace-match rpl t t tpl))
  14727. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14728. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14729. (setq rpl (if (or (match-end 1) (not pkg))
  14730. "" (org-latex-packages-to-string pkg snippets-p t))
  14731. tpl (replace-match rpl t t tpl))
  14732. (if pkg (setq end
  14733. (concat end "\n"
  14734. (org-latex-packages-to-string pkg snippets-p)))))
  14735. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14736. (setq rpl (if (or (match-end 1) (not extra))
  14737. "" (concat extra "\n"))
  14738. tpl (replace-match rpl t t tpl))
  14739. (if (and extra (string-match "\\S-" extra))
  14740. (setq end (concat end "\n" extra))))
  14741. (if (string-match "\\S-" end)
  14742. (concat tpl "\n" end)
  14743. tpl)))
  14744. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14745. "Turn an alist of packages into a string with the \\usepackage macros."
  14746. (setq pkg (mapconcat (lambda(p)
  14747. (cond
  14748. ((stringp p) p)
  14749. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14750. (format "%% Package %s omitted" (cadr p)))
  14751. ((equal "" (car p))
  14752. (format "\\usepackage{%s}" (cadr p)))
  14753. (t
  14754. (format "\\usepackage[%s]{%s}"
  14755. (car p) (cadr p)))))
  14756. pkg
  14757. "\n"))
  14758. (if newline (concat pkg "\n") pkg))
  14759. (defun org-dvipng-color (attr)
  14760. "Return an rgb color specification for dvipng."
  14761. (apply 'format "rgb %s %s %s"
  14762. (mapcar 'org-normalize-color
  14763. (color-values (face-attribute 'default attr nil)))))
  14764. (defun org-normalize-color (value)
  14765. "Return string to be used as color value for an RGB component."
  14766. (format "%g" (/ value 65535.0)))
  14767. ;; Image display
  14768. (defvar org-inline-image-overlays nil)
  14769. (make-variable-buffer-local 'org-inline-image-overlays)
  14770. (defun org-toggle-inline-images (&optional include-linked)
  14771. "Toggle the display of inline images.
  14772. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14773. (interactive "P")
  14774. (if org-inline-image-overlays
  14775. (progn
  14776. (org-remove-inline-images)
  14777. (message "Inline image display turned off"))
  14778. (org-display-inline-images include-linked)
  14779. (if org-inline-image-overlays
  14780. (message "%d images displayed inline"
  14781. (length org-inline-image-overlays))
  14782. (message "No images to display inline"))))
  14783. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14784. "Display inline images.
  14785. Normally only links without a description part are inlined, because this
  14786. is how it will work for export. When INCLUDE-LINKED is set, also links
  14787. with a description part will be inlined. This can be nice for a quick
  14788. look at those images, but it does not reflect what exported files will look
  14789. like.
  14790. When REFRESH is set, refresh existing images between BEG and END.
  14791. This will create new image displays only if necessary.
  14792. BEG and END default to the buffer boundaries."
  14793. (interactive "P")
  14794. (unless refresh
  14795. (org-remove-inline-images)
  14796. (clear-image-cache))
  14797. (save-excursion
  14798. (save-restriction
  14799. (widen)
  14800. (setq beg (or beg (point-min)) end (or end (point-max)))
  14801. (goto-char (point-min))
  14802. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  14803. (substring (org-image-file-name-regexp) 0 -2)
  14804. "\\)\\]" (if include-linked "" "\\]")))
  14805. old file ov img)
  14806. (while (re-search-forward re end t)
  14807. (setq old (get-char-property-and-overlay (match-beginning 1)
  14808. 'org-image-overlay))
  14809. (setq file (expand-file-name
  14810. (concat (or (match-string 3) "") (match-string 4))))
  14811. (when (file-exists-p file)
  14812. (if (and (car-safe old) refresh)
  14813. (image-refresh (overlay-get (cdr old) 'display))
  14814. (setq img (save-match-data (create-image file)))
  14815. (when img
  14816. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14817. (overlay-put ov 'display img)
  14818. (overlay-put ov 'face 'default)
  14819. (overlay-put ov 'org-image-overlay t)
  14820. (overlay-put ov 'modification-hooks
  14821. (list 'org-display-inline-modification-hook))
  14822. (push ov org-inline-image-overlays)))))))))
  14823. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  14824. "Remove inline-display overlay if a corresponding region is modified."
  14825. (let ((inhibit-modification-hooks t))
  14826. (when (and ov after)
  14827. (delete ov org-inline-image-overlays)
  14828. (delete-overlay ov))))
  14829. (defun org-remove-inline-images ()
  14830. "Remove inline display of images."
  14831. (interactive)
  14832. (mapc 'delete-overlay org-inline-image-overlays)
  14833. (setq org-inline-image-overlays nil))
  14834. ;;;; Key bindings
  14835. ;; Make `C-c C-x' a prefix key
  14836. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14837. ;; TAB key with modifiers
  14838. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14839. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14840. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14841. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  14842. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  14843. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  14844. ;; The following line is necessary under Suse GNU/Linux
  14845. (unless (featurep 'xemacs)
  14846. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14847. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14848. (define-key org-mode-map [backtab] 'org-shifttab)
  14849. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14850. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14851. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14852. ;; Cursor keys with modifiers
  14853. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14854. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14855. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14856. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14857. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14858. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14859. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14860. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14861. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14862. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14863. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14864. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14865. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14866. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14867. ;; Babel keys
  14868. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  14869. (mapc (lambda (pair)
  14870. (define-key org-babel-map (car pair) (cdr pair)))
  14871. org-babel-key-bindings)
  14872. ;;; Extra keys for tty access.
  14873. ;; We only set them when really needed because otherwise the
  14874. ;; menus don't show the simple keys
  14875. (when (or org-use-extra-keys
  14876. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14877. (not window-system))
  14878. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14879. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14880. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14881. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14882. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14883. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14884. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14885. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14886. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14887. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14888. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14889. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14890. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14891. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14892. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14893. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14894. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14895. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14896. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14897. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14898. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14899. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14900. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  14901. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14902. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14903. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14904. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14905. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14906. ;; All the other keys
  14907. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14908. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14909. (if (boundp 'narrow-map)
  14910. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14911. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14912. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14913. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14914. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14915. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14916. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14917. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14918. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14919. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14920. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14921. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14922. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14923. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14924. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14925. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14926. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14927. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14928. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14929. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14930. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14931. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14932. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14933. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14934. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14935. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14936. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14937. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14938. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14939. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14940. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14941. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14942. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14943. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14944. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14945. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14946. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14947. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14948. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14949. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14950. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14951. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14952. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14953. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14954. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14955. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14956. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14957. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14958. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14959. (org-defkey org-mode-map "\C-m" 'org-return)
  14960. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14961. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14962. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14963. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14964. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14965. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14966. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14967. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14968. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14969. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14970. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14971. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14972. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14973. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14974. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14975. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14976. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14977. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14978. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14979. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14980. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14981. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14982. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14983. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14984. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14985. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14986. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14987. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14988. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14989. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14990. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14991. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14992. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14993. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14994. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  14995. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  14996. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14997. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14998. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14999. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15000. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15001. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15002. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15003. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15004. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15005. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15006. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15007. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15008. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15009. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15010. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15011. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15012. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15013. (when (featurep 'xemacs)
  15014. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15015. (defconst org-speed-commands-default
  15016. '(
  15017. ("Outline Navigation")
  15018. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15019. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15020. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15021. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15022. ("u" . (org-speed-move-safe 'outline-up-heading))
  15023. ("j" . org-goto)
  15024. ("g" . (org-refile t))
  15025. ("Outline Visibility")
  15026. ("c" . org-cycle)
  15027. ("C" . org-shifttab)
  15028. (" " . org-display-outline-path)
  15029. ("Outline Structure Editing")
  15030. ("U" . org-shiftmetaup)
  15031. ("D" . org-shiftmetadown)
  15032. ("r" . org-metaright)
  15033. ("l" . org-metaleft)
  15034. ("R" . org-shiftmetaright)
  15035. ("L" . org-shiftmetaleft)
  15036. ("i" . (progn (forward-char 1) (call-interactively
  15037. 'org-insert-heading-respect-content)))
  15038. ("^" . org-sort)
  15039. ("w" . org-refile)
  15040. ("a" . org-archive-subtree-default-with-confirmation)
  15041. ("." . outline-mark-subtree)
  15042. ("Clock Commands")
  15043. ("I" . org-clock-in)
  15044. ("O" . org-clock-out)
  15045. ("Meta Data Editing")
  15046. ("t" . org-todo)
  15047. ("0" . (org-priority ?\ ))
  15048. ("1" . (org-priority ?A))
  15049. ("2" . (org-priority ?B))
  15050. ("3" . (org-priority ?C))
  15051. (";" . org-set-tags-command)
  15052. ("e" . org-set-effort)
  15053. ("Agenda Views etc")
  15054. ("v" . org-agenda)
  15055. ("/" . org-sparse-tree)
  15056. ("Misc")
  15057. ("o" . org-open-at-point)
  15058. ("?" . org-speed-command-help)
  15059. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15060. (">" . (org-agenda-remove-restriction-lock))
  15061. )
  15062. "The default speed commands.")
  15063. (defun org-print-speed-command (e)
  15064. (if (> (length (car e)) 1)
  15065. (progn
  15066. (princ "\n")
  15067. (princ (car e))
  15068. (princ "\n")
  15069. (princ (make-string (length (car e)) ?-))
  15070. (princ "\n"))
  15071. (princ (car e))
  15072. (princ " ")
  15073. (if (symbolp (cdr e))
  15074. (princ (symbol-name (cdr e)))
  15075. (prin1 (cdr e)))
  15076. (princ "\n")))
  15077. (defun org-speed-command-help ()
  15078. "Show the available speed commands."
  15079. (interactive)
  15080. (if (not org-use-speed-commands)
  15081. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15082. (with-output-to-temp-buffer "*Help*"
  15083. (princ "User-defined Speed commands\n===========================\n")
  15084. (mapc 'org-print-speed-command org-speed-commands-user)
  15085. (princ "\n")
  15086. (princ "Built-in Speed commands\n=======================\n")
  15087. (mapc 'org-print-speed-command org-speed-commands-default))
  15088. (with-current-buffer "*Help*"
  15089. (setq truncate-lines t))))
  15090. (defun org-speed-move-safe (cmd)
  15091. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15092. If not, return to the original position and throw an error."
  15093. (interactive)
  15094. (let ((pos (point)))
  15095. (call-interactively cmd)
  15096. (unless (and (bolp) (org-on-heading-p))
  15097. (goto-char pos)
  15098. (error "Boundary reached while executing %s" cmd))))
  15099. (defvar org-self-insert-command-undo-counter 0)
  15100. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15101. (defvar org-speed-command nil)
  15102. (defun org-speed-command-default-hook (keys)
  15103. "Hook for activating single-letter speed commands.
  15104. `org-speed-commands-default' specifies a minimal command set. Use
  15105. `org-speed-commands-user' for further customization."
  15106. (when (or (and (bolp) (looking-at outline-regexp))
  15107. (and (functionp org-use-speed-commands)
  15108. (funcall org-use-speed-commands)))
  15109. (cdr (assoc keys (append org-speed-commands-user
  15110. org-speed-commands-default)))))
  15111. (defun org-babel-speed-command-hook (keys)
  15112. "Hook for activating single-letter code block commands."
  15113. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15114. (cdr (assoc keys org-babel-key-bindings))))
  15115. (defcustom org-speed-command-hook
  15116. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15117. "Hook for activating speed commands at strategic locations.
  15118. Hook functions are called in sequence until a valid handler is
  15119. found.
  15120. Each hook takes a single argument, a user-pressed command key
  15121. which is also a `self-insert-command' from the global map.
  15122. Within the hook, examine the cursor position and the command key
  15123. and return nil or a valid handler as appropriate. Handler could
  15124. be one of an interactive command, a function, or a form.
  15125. Set `org-use-speed-commands' to non-nil value to enable this
  15126. hook. The default setting is `org-speed-command-default-hook'."
  15127. :group 'org-structure
  15128. :type 'hook)
  15129. (defun org-self-insert-command (N)
  15130. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15131. If the cursor is in a table looking at whitespace, the whitespace is
  15132. overwritten, and the table is not marked as requiring realignment."
  15133. (interactive "p")
  15134. (cond
  15135. ((and org-use-speed-commands
  15136. (setq org-speed-command
  15137. (run-hook-with-args-until-success
  15138. 'org-speed-command-hook (this-command-keys))))
  15139. (cond
  15140. ((commandp org-speed-command)
  15141. (setq this-command org-speed-command)
  15142. (call-interactively org-speed-command))
  15143. ((functionp org-speed-command)
  15144. (funcall org-speed-command))
  15145. ((and org-speed-command (listp org-speed-command))
  15146. (eval org-speed-command))
  15147. (t (let (org-use-speed-commands)
  15148. (call-interactively 'org-self-insert-command)))))
  15149. ((and
  15150. (org-table-p)
  15151. (progn
  15152. ;; check if we blank the field, and if that triggers align
  15153. (and (featurep 'org-table) org-table-auto-blank-field
  15154. (member last-command
  15155. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15156. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15157. ;; got extra space, this field does not determine column width
  15158. (let (org-table-may-need-update) (org-table-blank-field))
  15159. ;; no extra space, this field may determine column width
  15160. (org-table-blank-field)))
  15161. t)
  15162. (eq N 1)
  15163. (looking-at "[^|\n]* |"))
  15164. (let (org-table-may-need-update)
  15165. (goto-char (1- (match-end 0)))
  15166. (delete-backward-char 1)
  15167. (goto-char (match-beginning 0))
  15168. (self-insert-command N)))
  15169. (t
  15170. (setq org-table-may-need-update t)
  15171. (self-insert-command N)
  15172. (org-fix-tags-on-the-fly)
  15173. (if org-self-insert-cluster-for-undo
  15174. (if (not (eq last-command 'org-self-insert-command))
  15175. (setq org-self-insert-command-undo-counter 1)
  15176. (if (>= org-self-insert-command-undo-counter 20)
  15177. (setq org-self-insert-command-undo-counter 1)
  15178. (and (> org-self-insert-command-undo-counter 0)
  15179. buffer-undo-list
  15180. (not (cadr buffer-undo-list)) ; remove nil entry
  15181. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15182. (setq org-self-insert-command-undo-counter
  15183. (1+ org-self-insert-command-undo-counter))))))))
  15184. (defun org-fix-tags-on-the-fly ()
  15185. (when (and (equal (char-after (point-at-bol)) ?*)
  15186. (org-on-heading-p))
  15187. (org-align-tags-here org-tags-column)))
  15188. (defun org-delete-backward-char (N)
  15189. "Like `delete-backward-char', insert whitespace at field end in tables.
  15190. When deleting backwards, in tables this function will insert whitespace in
  15191. front of the next \"|\" separator, to keep the table aligned. The table will
  15192. still be marked for re-alignment if the field did fill the entire column,
  15193. because, in this case the deletion might narrow the column."
  15194. (interactive "p")
  15195. (if (and (org-table-p)
  15196. (eq N 1)
  15197. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15198. (looking-at ".*?|"))
  15199. (let ((pos (point))
  15200. (noalign (looking-at "[^|\n\r]* |"))
  15201. (c org-table-may-need-update))
  15202. (backward-delete-char N)
  15203. (if (not overwrite-mode)
  15204. (progn
  15205. (skip-chars-forward "^|")
  15206. (insert " ")
  15207. (goto-char (1- pos))))
  15208. ;; noalign: if there were two spaces at the end, this field
  15209. ;; does not determine the width of the column.
  15210. (if noalign (setq org-table-may-need-update c)))
  15211. (backward-delete-char N)
  15212. (org-fix-tags-on-the-fly)))
  15213. (defun org-delete-char (N)
  15214. "Like `delete-char', but insert whitespace at field end in tables.
  15215. When deleting characters, in tables this function will insert whitespace in
  15216. front of the next \"|\" separator, to keep the table aligned. The table will
  15217. still be marked for re-alignment if the field did fill the entire column,
  15218. because, in this case the deletion might narrow the column."
  15219. (interactive "p")
  15220. (if (and (org-table-p)
  15221. (not (bolp))
  15222. (not (= (char-after) ?|))
  15223. (eq N 1))
  15224. (if (looking-at ".*?|")
  15225. (let ((pos (point))
  15226. (noalign (looking-at "[^|\n\r]* |"))
  15227. (c org-table-may-need-update))
  15228. (replace-match (concat
  15229. (substring (match-string 0) 1 -1)
  15230. " |"))
  15231. (goto-char pos)
  15232. ;; noalign: if there were two spaces at the end, this field
  15233. ;; does not determine the width of the column.
  15234. (if noalign (setq org-table-may-need-update c)))
  15235. (delete-char N))
  15236. (delete-char N)
  15237. (org-fix-tags-on-the-fly)))
  15238. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15239. (put 'org-self-insert-command 'delete-selection t)
  15240. (put 'orgtbl-self-insert-command 'delete-selection t)
  15241. (put 'org-delete-char 'delete-selection 'supersede)
  15242. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15243. (put 'org-yank 'delete-selection 'yank)
  15244. ;; Make `flyspell-mode' delay after some commands
  15245. (put 'org-self-insert-command 'flyspell-delayed t)
  15246. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15247. (put 'org-delete-char 'flyspell-delayed t)
  15248. (put 'org-delete-backward-char 'flyspell-delayed t)
  15249. ;; Make pabbrev-mode expand after org-mode commands
  15250. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15251. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15252. ;; How to do this: Measure non-white length of current string
  15253. ;; If equal to column width, we should realign.
  15254. (defun org-remap (map &rest commands)
  15255. "In MAP, remap the functions given in COMMANDS.
  15256. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15257. (let (new old)
  15258. (while commands
  15259. (setq old (pop commands) new (pop commands))
  15260. (if (fboundp 'command-remapping)
  15261. (org-defkey map (vector 'remap old) new)
  15262. (substitute-key-definition old new map global-map)))))
  15263. (when (eq org-enable-table-editor 'optimized)
  15264. ;; If the user wants maximum table support, we need to hijack
  15265. ;; some standard editing functions
  15266. (org-remap org-mode-map
  15267. 'self-insert-command 'org-self-insert-command
  15268. 'delete-char 'org-delete-char
  15269. 'delete-backward-char 'org-delete-backward-char)
  15270. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15271. (defvar org-ctrl-c-ctrl-c-hook nil
  15272. "Hook for functions attaching themselves to `C-c C-c'.
  15273. This can be used to add additional functionality to the C-c C-c key which
  15274. executes context-dependent commands.
  15275. Each function will be called with no arguments. The function must check
  15276. if the context is appropriate for it to act. If yes, it should do its
  15277. thing and then return a non-nil value. If the context is wrong,
  15278. just do nothing and return nil.")
  15279. (defvar org-tab-first-hook nil
  15280. "Hook for functions to attach themselves to TAB.
  15281. See `org-ctrl-c-ctrl-c-hook' for more information.
  15282. This hook runs as the first action when TAB is pressed, even before
  15283. `org-cycle' messes around with the `outline-regexp' to cater for
  15284. inline tasks and plain list item folding.
  15285. If any function in this hook returns t, any other actions that
  15286. would have been caused by TAB (such as table field motion or visibility
  15287. cycling) will not occur.")
  15288. (defvar org-tab-after-check-for-table-hook nil
  15289. "Hook for functions to attach themselves to TAB.
  15290. See `org-ctrl-c-ctrl-c-hook' for more information.
  15291. This hook runs after it has been established that the cursor is not in a
  15292. table, but before checking if the cursor is in a headline or if global cycling
  15293. should be done.
  15294. If any function in this hook returns t, not other actions like visibility
  15295. cycling will be done.")
  15296. (defvar org-tab-after-check-for-cycling-hook nil
  15297. "Hook for functions to attach themselves to TAB.
  15298. See `org-ctrl-c-ctrl-c-hook' for more information.
  15299. This hook runs after it has been established that not table field motion and
  15300. not visibility should be done because of current context. This is probably
  15301. the place where a package like yasnippets can hook in.")
  15302. (defvar org-tab-before-tab-emulation-hook nil
  15303. "Hook for functions to attach themselves to TAB.
  15304. See `org-ctrl-c-ctrl-c-hook' for more information.
  15305. This hook runs after every other options for TAB have been exhausted, but
  15306. before indentation and \t insertion takes place.")
  15307. (defvar org-metaleft-hook nil
  15308. "Hook for functions attaching themselves to `M-left'.
  15309. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15310. (defvar org-metaright-hook nil
  15311. "Hook for functions attaching themselves to `M-right'.
  15312. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15313. (defvar org-metaup-hook nil
  15314. "Hook for functions attaching themselves to `M-up'.
  15315. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15316. (defvar org-metadown-hook nil
  15317. "Hook for functions attaching themselves to `M-down'.
  15318. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15319. (defvar org-shiftmetaleft-hook nil
  15320. "Hook for functions attaching themselves to `M-S-left'.
  15321. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15322. (defvar org-shiftmetaright-hook nil
  15323. "Hook for functions attaching themselves to `M-S-right'.
  15324. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15325. (defvar org-shiftmetaup-hook nil
  15326. "Hook for functions attaching themselves to `M-S-up'.
  15327. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15328. (defvar org-shiftmetadown-hook nil
  15329. "Hook for functions attaching themselves to `M-S-down'.
  15330. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15331. (defvar org-metareturn-hook nil
  15332. "Hook for functions attaching themselves to `M-RET'.
  15333. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15334. (defvar org-shiftup-hook nil
  15335. "Hook for functions attaching themselves to `S-up'.
  15336. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15337. (defvar org-shiftup-final-hook nil
  15338. "Hook for functions attaching themselves to `S-up'.
  15339. This one runs after all other options except shift-select have been excluded.
  15340. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15341. (defvar org-shiftdown-hook nil
  15342. "Hook for functions attaching themselves to `S-down'.
  15343. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15344. (defvar org-shiftdown-final-hook nil
  15345. "Hook for functions attaching themselves to `S-down'.
  15346. This one runs after all other options except shift-select have been excluded.
  15347. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15348. (defvar org-shiftleft-hook nil
  15349. "Hook for functions attaching themselves to `S-left'.
  15350. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15351. (defvar org-shiftleft-final-hook nil
  15352. "Hook for functions attaching themselves to `S-left'.
  15353. This one runs after all other options except shift-select have been excluded.
  15354. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15355. (defvar org-shiftright-hook nil
  15356. "Hook for functions attaching themselves to `S-right'.
  15357. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15358. (defvar org-shiftright-final-hook nil
  15359. "Hook for functions attaching themselves to `S-right'.
  15360. This one runs after all other options except shift-select have been excluded.
  15361. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15362. (defun org-modifier-cursor-error ()
  15363. "Throw an error, a modified cursor command was applied in wrong context."
  15364. (error "This command is active in special context like tables, headlines or items"))
  15365. (defun org-shiftselect-error ()
  15366. "Throw an error because Shift-Cursor command was applied in wrong context."
  15367. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15368. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15369. (error "This command works only in special context like headlines or timestamps")))
  15370. (defun org-call-for-shift-select (cmd)
  15371. (let ((this-command-keys-shift-translated t))
  15372. (call-interactively cmd)))
  15373. (defun org-shifttab (&optional arg)
  15374. "Global visibility cycling or move to previous table field.
  15375. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15376. on context.
  15377. See the individual commands for more information."
  15378. (interactive "P")
  15379. (cond
  15380. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15381. ((integerp arg)
  15382. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15383. (message "Content view to level: %d" arg)
  15384. (org-content (prefix-numeric-value arg2))
  15385. (setq org-cycle-global-status 'overview)))
  15386. (t (call-interactively 'org-global-cycle))))
  15387. (defun org-shiftmetaleft ()
  15388. "Promote subtree or delete table column.
  15389. Calls `org-promote-subtree', `org-outdent-item',
  15390. or `org-table-delete-column', depending on context.
  15391. See the individual commands for more information."
  15392. (interactive)
  15393. (cond
  15394. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15395. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15396. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15397. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15398. (t (org-modifier-cursor-error))))
  15399. (defun org-shiftmetaright ()
  15400. "Demote subtree or insert table column.
  15401. Calls `org-demote-subtree', `org-indent-item',
  15402. or `org-table-insert-column', depending on context.
  15403. See the individual commands for more information."
  15404. (interactive)
  15405. (cond
  15406. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15407. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15408. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15409. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15410. (t (org-modifier-cursor-error))))
  15411. (defun org-shiftmetaup (&optional arg)
  15412. "Move subtree up or kill table row.
  15413. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15414. `org-move-item-up' depending on context. See the individual commands
  15415. for more information."
  15416. (interactive "P")
  15417. (cond
  15418. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15419. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15420. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15421. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15422. (t (org-modifier-cursor-error))))
  15423. (defun org-shiftmetadown (&optional arg)
  15424. "Move subtree down or insert table row.
  15425. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15426. `org-move-item-down', depending on context. See the individual
  15427. commands for more information."
  15428. (interactive "P")
  15429. (cond
  15430. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15431. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15432. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15433. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15434. (t (org-modifier-cursor-error))))
  15435. (defsubst org-hidden-tree-error ()
  15436. (error
  15437. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15438. (defun org-metaleft (&optional arg)
  15439. "Promote heading or move table column to left.
  15440. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15441. With no specific context, calls the Emacs default `backward-word'.
  15442. See the individual commands for more information."
  15443. (interactive "P")
  15444. (cond
  15445. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15446. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15447. ((or (org-on-heading-p)
  15448. (and (org-region-active-p)
  15449. (save-excursion
  15450. (goto-char (region-beginning))
  15451. (org-on-heading-p))))
  15452. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15453. (call-interactively 'org-do-promote))
  15454. ((or (org-at-item-p)
  15455. (and (org-region-active-p)
  15456. (save-excursion
  15457. (goto-char (region-beginning))
  15458. (org-at-item-p))))
  15459. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15460. (call-interactively 'org-outdent-item))
  15461. (t (call-interactively 'backward-word))))
  15462. (defun org-metaright (&optional arg)
  15463. "Demote subtree or move table column to right.
  15464. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15465. With no specific context, calls the Emacs default `forward-word'.
  15466. See the individual commands for more information."
  15467. (interactive "P")
  15468. (cond
  15469. ((run-hook-with-args-until-success 'org-metaright-hook))
  15470. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15471. ((or (org-on-heading-p)
  15472. (and (org-region-active-p)
  15473. (save-excursion
  15474. (goto-char (region-beginning))
  15475. (org-on-heading-p))))
  15476. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15477. (call-interactively 'org-do-demote))
  15478. ((or (org-at-item-p)
  15479. (and (org-region-active-p)
  15480. (save-excursion
  15481. (goto-char (region-beginning))
  15482. (org-at-item-p))))
  15483. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15484. (call-interactively 'org-indent-item))
  15485. (t (call-interactively 'forward-word))))
  15486. (defun org-check-for-hidden (what)
  15487. "Check if there are hidden headlines/items in the current visual line.
  15488. WHAT can be either `headlines' or `items'. If the current line is
  15489. an outline or item heading and it has a folded subtree below it,
  15490. this function returns t, nil otherwise."
  15491. (let ((re (cond
  15492. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15493. ((eq what 'items) (concat "^" (org-item-re t)))
  15494. (t (error "This should not happen"))))
  15495. beg end)
  15496. (save-excursion
  15497. (catch 'exit
  15498. (unless (org-region-active-p)
  15499. (setq beg (point-at-bol))
  15500. (beginning-of-line 2)
  15501. (while (and (not (eobp)) ;; this is like `next-line'
  15502. (get-char-property (1- (point)) 'invisible))
  15503. (beginning-of-line 2))
  15504. (setq end (point))
  15505. (goto-char beg)
  15506. (goto-char (point-at-eol))
  15507. (setq end (max end (point)))
  15508. (while (re-search-forward re end t)
  15509. (if (get-char-property (match-beginning 0) 'invisible)
  15510. (throw 'exit t))))
  15511. nil))))
  15512. (defun org-metaup (&optional arg)
  15513. "Move subtree up or move table row up.
  15514. Calls `org-move-subtree-up' or `org-table-move-row' or
  15515. `org-move-item-up', depending on context. See the individual commands
  15516. for more information."
  15517. (interactive "P")
  15518. (cond
  15519. ((run-hook-with-args-until-success 'org-metaup-hook))
  15520. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15521. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15522. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15523. (t (transpose-lines 1) (beginning-of-line -1))))
  15524. (defun org-metadown (&optional arg)
  15525. "Move subtree down or move table row down.
  15526. Calls `org-move-subtree-down' or `org-table-move-row' or
  15527. `org-move-item-down', depending on context. See the individual
  15528. commands for more information."
  15529. (interactive "P")
  15530. (cond
  15531. ((run-hook-with-args-until-success 'org-metadown-hook))
  15532. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15533. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15534. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15535. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15536. (defun org-shiftup (&optional arg)
  15537. "Increase item in timestamp or increase priority of current headline.
  15538. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15539. depending on context. See the individual commands for more information."
  15540. (interactive "P")
  15541. (cond
  15542. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15543. ((and org-support-shift-select (org-region-active-p))
  15544. (org-call-for-shift-select 'previous-line))
  15545. ((org-at-timestamp-p t)
  15546. (call-interactively (if org-edit-timestamp-down-means-later
  15547. 'org-timestamp-down 'org-timestamp-up)))
  15548. ((and (not (eq org-support-shift-select 'always))
  15549. org-enable-priority-commands
  15550. (org-on-heading-p))
  15551. (call-interactively 'org-priority-up))
  15552. ((and (not org-support-shift-select) (org-at-item-p))
  15553. (call-interactively 'org-previous-item))
  15554. ((org-clocktable-try-shift 'up arg))
  15555. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15556. (org-support-shift-select
  15557. (org-call-for-shift-select 'previous-line))
  15558. (t (org-shiftselect-error))))
  15559. (defun org-shiftdown (&optional arg)
  15560. "Decrease item in timestamp or decrease priority of current headline.
  15561. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15562. depending on context. See the individual commands for more information."
  15563. (interactive "P")
  15564. (cond
  15565. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15566. ((and org-support-shift-select (org-region-active-p))
  15567. (org-call-for-shift-select 'next-line))
  15568. ((org-at-timestamp-p t)
  15569. (call-interactively (if org-edit-timestamp-down-means-later
  15570. 'org-timestamp-up 'org-timestamp-down)))
  15571. ((and (not (eq org-support-shift-select 'always))
  15572. org-enable-priority-commands
  15573. (org-on-heading-p))
  15574. (call-interactively 'org-priority-down))
  15575. ((and (not org-support-shift-select) (org-at-item-p))
  15576. (call-interactively 'org-next-item))
  15577. ((org-clocktable-try-shift 'down arg))
  15578. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15579. (org-support-shift-select
  15580. (org-call-for-shift-select 'next-line))
  15581. (t (org-shiftselect-error))))
  15582. (defun org-shiftright (&optional arg)
  15583. "Cycle the thing at point or in the current line, depending on context.
  15584. Depending on context, this does one of the following:
  15585. - switch a timestamp at point one day into the future
  15586. - on a headline, switch to the next TODO keyword.
  15587. - on an item, switch entire list to the next bullet type
  15588. - on a property line, switch to the next allowed value
  15589. - on a clocktable definition line, move time block into the future"
  15590. (interactive "P")
  15591. (cond
  15592. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15593. ((and org-support-shift-select (org-region-active-p))
  15594. (org-call-for-shift-select 'forward-char))
  15595. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15596. ((and (not (eq org-support-shift-select 'always))
  15597. (org-on-heading-p))
  15598. (let ((org-inhibit-logging
  15599. (not org-treat-S-cursor-todo-selection-as-state-change))
  15600. (org-inhibit-blocking
  15601. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15602. (org-call-with-arg 'org-todo 'right)))
  15603. ((or (and org-support-shift-select
  15604. (not (eq org-support-shift-select 'always))
  15605. (org-at-item-bullet-p))
  15606. (and (not org-support-shift-select) (org-at-item-p)))
  15607. (org-call-with-arg 'org-cycle-list-bullet nil))
  15608. ((and (not (eq org-support-shift-select 'always))
  15609. (org-at-property-p))
  15610. (call-interactively 'org-property-next-allowed-value))
  15611. ((org-clocktable-try-shift 'right arg))
  15612. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15613. (org-support-shift-select
  15614. (org-call-for-shift-select 'forward-char))
  15615. (t (org-shiftselect-error))))
  15616. (defun org-shiftleft (&optional arg)
  15617. "Cycle the thing at point or in the current line, depending on context.
  15618. Depending on context, this does one of the following:
  15619. - switch a timestamp at point one day into the past
  15620. - on a headline, switch to the previous TODO keyword.
  15621. - on an item, switch entire list to the previous bullet type
  15622. - on a property line, switch to the previous allowed value
  15623. - on a clocktable definition line, move time block into the past"
  15624. (interactive "P")
  15625. (cond
  15626. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15627. ((and org-support-shift-select (org-region-active-p))
  15628. (org-call-for-shift-select 'backward-char))
  15629. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15630. ((and (not (eq org-support-shift-select 'always))
  15631. (org-on-heading-p))
  15632. (let ((org-inhibit-logging
  15633. (not org-treat-S-cursor-todo-selection-as-state-change))
  15634. (org-inhibit-blocking
  15635. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15636. (org-call-with-arg 'org-todo 'left)))
  15637. ((or (and org-support-shift-select
  15638. (not (eq org-support-shift-select 'always))
  15639. (org-at-item-bullet-p))
  15640. (and (not org-support-shift-select) (org-at-item-p)))
  15641. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15642. ((and (not (eq org-support-shift-select 'always))
  15643. (org-at-property-p))
  15644. (call-interactively 'org-property-previous-allowed-value))
  15645. ((org-clocktable-try-shift 'left arg))
  15646. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15647. (org-support-shift-select
  15648. (org-call-for-shift-select 'backward-char))
  15649. (t (org-shiftselect-error))))
  15650. (defun org-shiftcontrolright ()
  15651. "Switch to next TODO set."
  15652. (interactive)
  15653. (cond
  15654. ((and org-support-shift-select (org-region-active-p))
  15655. (org-call-for-shift-select 'forward-word))
  15656. ((and (not (eq org-support-shift-select 'always))
  15657. (org-on-heading-p))
  15658. (org-call-with-arg 'org-todo 'nextset))
  15659. (org-support-shift-select
  15660. (org-call-for-shift-select 'forward-word))
  15661. (t (org-shiftselect-error))))
  15662. (defun org-shiftcontrolleft ()
  15663. "Switch to previous TODO set."
  15664. (interactive)
  15665. (cond
  15666. ((and org-support-shift-select (org-region-active-p))
  15667. (org-call-for-shift-select 'backward-word))
  15668. ((and (not (eq org-support-shift-select 'always))
  15669. (org-on-heading-p))
  15670. (org-call-with-arg 'org-todo 'previousset))
  15671. (org-support-shift-select
  15672. (org-call-for-shift-select 'backward-word))
  15673. (t (org-shiftselect-error))))
  15674. (defun org-ctrl-c-ret ()
  15675. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15676. (interactive)
  15677. (cond
  15678. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15679. (t (call-interactively 'org-insert-heading))))
  15680. (defun org-copy-special ()
  15681. "Copy region in table or copy current subtree.
  15682. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15683. See the individual commands for more information."
  15684. (interactive)
  15685. (call-interactively
  15686. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15687. (defun org-cut-special ()
  15688. "Cut region in table or cut current subtree.
  15689. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15690. See the individual commands for more information."
  15691. (interactive)
  15692. (call-interactively
  15693. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15694. (defun org-paste-special (arg)
  15695. "Paste rectangular region into table, or past subtree relative to level.
  15696. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15697. See the individual commands for more information."
  15698. (interactive "P")
  15699. (if (org-at-table-p)
  15700. (org-table-paste-rectangle)
  15701. (org-paste-subtree arg)))
  15702. (defun org-edit-special (&optional arg)
  15703. "Call a special editor for the stuff at point.
  15704. When at a table, call the formula editor with `org-table-edit-formulas'.
  15705. When at the first line of an src example, call `org-edit-src-code'.
  15706. When in an #+include line, visit the include file. Otherwise call
  15707. `ffap' to visit the file at point."
  15708. (interactive)
  15709. ;; possibly prep session before editing source
  15710. (when arg
  15711. (let* ((info (org-babel-get-src-block-info))
  15712. (lang (nth 0 info))
  15713. (params (nth 2 info))
  15714. (session (cdr (assoc :session params))))
  15715. (when (and info session) ;; we are in a source-code block with a session
  15716. (funcall
  15717. (intern (concat "org-babel-prep-session:" lang)) session params))))
  15718. (cond ;; proceed with `org-edit-special'
  15719. ((save-excursion
  15720. (beginning-of-line 1)
  15721. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15722. (find-file (org-trim (match-string 1))))
  15723. ((org-edit-src-code))
  15724. ((org-edit-fixed-width-region))
  15725. ((org-at-table.el-p)
  15726. (org-edit-src-code))
  15727. ((or (org-at-table-p)
  15728. (save-excursion
  15729. (beginning-of-line 1)
  15730. (looking-at "[ \t]*#\\+TBLFM:")))
  15731. (call-interactively 'org-table-edit-formulas))
  15732. (t (call-interactively 'ffap))))
  15733. (defun org-ctrl-c-ctrl-c (&optional arg)
  15734. "Set tags in headline, or update according to changed information at point.
  15735. This command does many different things, depending on context:
  15736. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15737. this is what we do.
  15738. - If the cursor is on a statistics cookie, update it.
  15739. - If the cursor is in a headline, prompt for tags and insert them
  15740. into the current line, aligned to `org-tags-column'. When called
  15741. with prefix arg, realign all tags in the current buffer.
  15742. - If the cursor is in one of the special #+KEYWORD lines, this
  15743. triggers scanning the buffer for these lines and updating the
  15744. information.
  15745. - If the cursor is inside a table, realign the table. This command
  15746. works even if the automatic table editor has been turned off.
  15747. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15748. the entire table.
  15749. - If the cursor is at a footnote reference or definition, jump to
  15750. the corresponding definition or references, respectively.
  15751. - If the cursor is a the beginning of a dynamic block, update it.
  15752. - If the current buffer is a capture buffer, close note and file it.
  15753. - If the cursor is on a <<<target>>>, update radio targets and
  15754. corresponding links in this buffer.
  15755. - If the cursor is on a numbered item in a plain list, renumber the
  15756. ordered list.
  15757. - If the cursor is on a checkbox, toggle it.
  15758. - If the cursor is on a code block, evaluate it. The variable
  15759. `org-confirm-babel-evaluate' can be used to control prompting
  15760. before code block evaluation, by default every code block
  15761. evaluation requires confirmation. Code block evaluation can be
  15762. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  15763. (interactive "P")
  15764. (let ((org-enable-table-editor t))
  15765. (cond
  15766. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15767. org-occur-highlights
  15768. org-latex-fragment-image-overlays)
  15769. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15770. (org-remove-occur-highlights)
  15771. (org-remove-latex-fragment-image-overlays)
  15772. (message "Temporary highlights/overlays removed from current buffer"))
  15773. ((and (local-variable-p 'org-finish-function (current-buffer))
  15774. (fboundp org-finish-function))
  15775. (funcall org-finish-function))
  15776. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15777. ((or (looking-at org-property-start-re)
  15778. (org-at-property-p))
  15779. (call-interactively 'org-property-action))
  15780. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15781. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15782. (or (org-on-heading-p) (org-at-item-p)))
  15783. (call-interactively 'org-update-statistics-cookies))
  15784. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15785. ((org-at-table.el-p)
  15786. (message "Use C-c ' to edit table.el tables"))
  15787. ((org-at-table-p)
  15788. (org-table-maybe-eval-formula)
  15789. (if arg
  15790. (call-interactively 'org-table-recalculate)
  15791. (org-table-maybe-recalculate-line))
  15792. (call-interactively 'org-table-align)
  15793. (orgtbl-send-table 'maybe))
  15794. ((or (org-footnote-at-reference-p)
  15795. (org-footnote-at-definition-p))
  15796. (call-interactively 'org-footnote-action))
  15797. ((org-at-item-checkbox-p)
  15798. (call-interactively 'org-list-repair)
  15799. (call-interactively 'org-toggle-checkbox)
  15800. (org-list-send-list 'maybe))
  15801. ((org-at-item-p)
  15802. (call-interactively 'org-list-repair)
  15803. (when arg (call-interactively 'org-toggle-checkbox))
  15804. (org-list-send-list 'maybe))
  15805. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15806. ;; Dynamic block
  15807. (beginning-of-line 1)
  15808. (save-excursion (org-update-dblock)))
  15809. ((save-excursion
  15810. (beginning-of-line 1)
  15811. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15812. (cond
  15813. ((equal (match-string 1) "TBLFM")
  15814. ;; Recalculate the table before this line
  15815. (save-excursion
  15816. (beginning-of-line 1)
  15817. (skip-chars-backward " \r\n\t")
  15818. (if (org-at-table-p)
  15819. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15820. (t
  15821. (let ((org-inhibit-startup-visibility-stuff t)
  15822. (org-startup-align-all-tables nil))
  15823. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15824. (message "Local setup has been refreshed"))))
  15825. ((org-clock-update-time-maybe))
  15826. (t (error "C-c C-c can do nothing useful at this location")))))
  15827. (defun org-mode-restart ()
  15828. "Restart Org-mode, to scan again for special lines.
  15829. Also updates the keyword regular expressions."
  15830. (interactive)
  15831. (org-mode)
  15832. (message "Org-mode restarted"))
  15833. (defun org-kill-note-or-show-branches ()
  15834. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15835. (interactive)
  15836. (if (not org-finish-function)
  15837. (progn
  15838. (hide-subtree)
  15839. (call-interactively 'show-branches))
  15840. (let ((org-note-abort t))
  15841. (funcall org-finish-function))))
  15842. (defun org-return (&optional indent)
  15843. "Goto next table row or insert a newline.
  15844. Calls `org-table-next-row' or `newline', depending on context.
  15845. See the individual commands for more information."
  15846. (interactive)
  15847. (cond
  15848. ((bobp) (if indent (newline-and-indent) (newline)))
  15849. ((org-at-table-p)
  15850. (org-table-justify-field-maybe)
  15851. (call-interactively 'org-table-next-row))
  15852. ((and org-return-follows-link
  15853. (eq (get-text-property (point) 'face) 'org-link))
  15854. (call-interactively 'org-open-at-point))
  15855. ((and (org-at-heading-p)
  15856. (looking-at
  15857. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  15858. (org-show-entry)
  15859. (end-of-line 1)
  15860. (newline))
  15861. (t (if indent (newline-and-indent) (newline)))))
  15862. (defun org-return-indent ()
  15863. "Goto next table row or insert a newline and indent.
  15864. Calls `org-table-next-row' or `newline-and-indent', depending on
  15865. context. See the individual commands for more information."
  15866. (interactive)
  15867. (org-return t))
  15868. (defun org-ctrl-c-star ()
  15869. "Compute table, or change heading status of lines.
  15870. Calls `org-table-recalculate' or `org-toggle-heading',
  15871. depending on context."
  15872. (interactive)
  15873. (cond
  15874. ((org-at-table-p)
  15875. (call-interactively 'org-table-recalculate))
  15876. (t
  15877. ;; Convert all lines in region to list items
  15878. (call-interactively 'org-toggle-heading))))
  15879. (defun org-ctrl-c-minus ()
  15880. "Insert separator line in table or modify bullet status of line.
  15881. Also turns a plain line or a region of lines into list items.
  15882. Calls `org-table-insert-hline', `org-toggle-item', or
  15883. `org-cycle-list-bullet', depending on context."
  15884. (interactive)
  15885. (cond
  15886. ((org-at-table-p)
  15887. (call-interactively 'org-table-insert-hline))
  15888. ((org-region-active-p)
  15889. (call-interactively 'org-toggle-item))
  15890. ((org-in-item-p)
  15891. (call-interactively 'org-cycle-list-bullet))
  15892. (t
  15893. (call-interactively 'org-toggle-item))))
  15894. (defun org-toggle-item ()
  15895. "Convert headings or normal lines to items, items to normal lines.
  15896. If there is no active region, only the current line is considered.
  15897. If the first line in the region is a headline, convert all headlines to items.
  15898. If the first line in the region is an item, convert all items to normal lines.
  15899. If the first line is normal text, add an item bullet to each line."
  15900. (interactive)
  15901. (let (l2 l beg end)
  15902. (if (org-region-active-p)
  15903. (setq beg (region-beginning) end (region-end))
  15904. (setq beg (point-at-bol)
  15905. end (min (1+ (point-at-eol)) (point-max))))
  15906. (save-excursion
  15907. (goto-char end)
  15908. (setq l2 (org-current-line))
  15909. (goto-char beg)
  15910. (beginning-of-line 1)
  15911. (setq l (1- (org-current-line)))
  15912. (if (org-at-item-p)
  15913. ;; We already have items, de-itemize
  15914. (while (< (setq l (1+ l)) l2)
  15915. (when (org-at-item-p)
  15916. (skip-chars-forward " \t")
  15917. (delete-region (point) (match-end 0)))
  15918. (beginning-of-line 2))
  15919. (if (org-on-heading-p)
  15920. ;; Headings, convert to items
  15921. (while (< (setq l (1+ l)) l2)
  15922. (if (looking-at org-outline-regexp)
  15923. (replace-match (org-list-bullet-string "-") t t))
  15924. (beginning-of-line 2))
  15925. ;; normal lines, turn them into items
  15926. (while (< (setq l (1+ l)) l2)
  15927. (unless (org-at-item-p)
  15928. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15929. (replace-match
  15930. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  15931. (beginning-of-line 2)))))))
  15932. (defun org-toggle-heading (&optional nstars)
  15933. "Convert headings to normal text, or items or text to headings.
  15934. If there is no active region, only the current line is considered.
  15935. If the first line is a heading, remove the stars from all headlines
  15936. in the region.
  15937. If the first line is a plain list item, turn all plain list items
  15938. into headings.
  15939. If the first line is a normal line, turn each and every line in the
  15940. region into a heading.
  15941. When converting a line into a heading, the number of stars is chosen
  15942. such that the lines become children of the current entry. However,
  15943. when a prefix argument is given, its value determines the number of
  15944. stars to add."
  15945. (interactive "P")
  15946. (let (l2 l itemp beg end)
  15947. (if (org-region-active-p)
  15948. (setq beg (region-beginning) end (region-end))
  15949. (setq beg (point-at-bol)
  15950. end (min (1+ (point-at-eol)) (point-max))))
  15951. (save-excursion
  15952. (goto-char end)
  15953. (setq l2 (org-current-line))
  15954. (goto-char beg)
  15955. (beginning-of-line 1)
  15956. (setq l (1- (org-current-line)))
  15957. (if (org-on-heading-p)
  15958. ;; We already have headlines, de-star them
  15959. (while (< (setq l (1+ l)) l2)
  15960. (when (org-on-heading-p t)
  15961. (and (looking-at outline-regexp) (replace-match "")))
  15962. (beginning-of-line 2))
  15963. (setq itemp (org-at-item-p))
  15964. (let* ((stars
  15965. (if nstars
  15966. (make-string (prefix-numeric-value current-prefix-arg)
  15967. ?*)
  15968. (save-excursion
  15969. (if (re-search-backward org-complex-heading-regexp nil t)
  15970. (match-string 1) ""))))
  15971. (add-stars (cond (nstars "")
  15972. ((equal stars "") "*")
  15973. (org-odd-levels-only "**")
  15974. (t "*")))
  15975. (rpl (concat stars add-stars " ")))
  15976. (while (< (setq l (1+ l)) l2)
  15977. (if itemp
  15978. (and (org-at-item-p) (replace-match rpl t t))
  15979. (unless (org-on-heading-p)
  15980. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15981. (replace-match (concat rpl (match-string 2))))))
  15982. (beginning-of-line 2)))))))
  15983. (defun org-meta-return (&optional arg)
  15984. "Insert a new heading or wrap a region in a table.
  15985. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15986. See the individual commands for more information."
  15987. (interactive "P")
  15988. (cond
  15989. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15990. ((org-at-table-p)
  15991. (call-interactively 'org-table-wrap-region))
  15992. (t (call-interactively 'org-insert-heading))))
  15993. ;;; Menu entries
  15994. ;; Define the Org-mode menus
  15995. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15996. '("Tbl"
  15997. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15998. ["Next Field" org-cycle (org-at-table-p)]
  15999. ["Previous Field" org-shifttab (org-at-table-p)]
  16000. ["Next Row" org-return (org-at-table-p)]
  16001. "--"
  16002. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16003. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16004. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16005. "--"
  16006. ("Column"
  16007. ["Move Column Left" org-metaleft (org-at-table-p)]
  16008. ["Move Column Right" org-metaright (org-at-table-p)]
  16009. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16010. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16011. ("Row"
  16012. ["Move Row Up" org-metaup (org-at-table-p)]
  16013. ["Move Row Down" org-metadown (org-at-table-p)]
  16014. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16015. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16016. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16017. "--"
  16018. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16019. ("Rectangle"
  16020. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16021. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16022. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16023. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16024. "--"
  16025. ("Calculate"
  16026. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16027. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16028. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16029. "--"
  16030. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16031. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16032. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16033. "--"
  16034. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16035. "--"
  16036. ["Sum Column/Rectangle" org-table-sum
  16037. (or (org-at-table-p) (org-region-active-p))]
  16038. ["Which Column?" org-table-current-column (org-at-table-p)])
  16039. ["Debug Formulas"
  16040. org-table-toggle-formula-debugger
  16041. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16042. ["Show Col/Row Numbers"
  16043. org-table-toggle-coordinate-overlays
  16044. :style toggle
  16045. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16046. "--"
  16047. ["Create" org-table-create (and (not (org-at-table-p))
  16048. org-enable-table-editor)]
  16049. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16050. ["Import from File" org-table-import (not (org-at-table-p))]
  16051. ["Export to File" org-table-export (org-at-table-p)]
  16052. "--"
  16053. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16054. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16055. '("Org"
  16056. ("Show/Hide"
  16057. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16058. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16059. ["Sparse Tree..." org-sparse-tree t]
  16060. ["Reveal Context" org-reveal t]
  16061. ["Show All" show-all t]
  16062. "--"
  16063. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16064. "--"
  16065. ["New Heading" org-insert-heading t]
  16066. ("Navigate Headings"
  16067. ["Up" outline-up-heading t]
  16068. ["Next" outline-next-visible-heading t]
  16069. ["Previous" outline-previous-visible-heading t]
  16070. ["Next Same Level" outline-forward-same-level t]
  16071. ["Previous Same Level" outline-backward-same-level t]
  16072. "--"
  16073. ["Jump" org-goto t])
  16074. ("Edit Structure"
  16075. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16076. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16077. "--"
  16078. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16079. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16080. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16081. "--"
  16082. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16083. "--"
  16084. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16085. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16086. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16087. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16088. "--"
  16089. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16090. "--"
  16091. ["Convert to odd levels" org-convert-to-odd-levels t]
  16092. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16093. ("Editing"
  16094. ["Emphasis..." org-emphasize t]
  16095. ["Edit Source Example" org-edit-special t]
  16096. "--"
  16097. ["Footnote new/jump" org-footnote-action t]
  16098. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16099. ("Archive"
  16100. ["Archive (default method)" org-archive-subtree-default t]
  16101. "--"
  16102. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16103. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16104. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16105. )
  16106. "--"
  16107. ("Hyperlinks"
  16108. ["Store Link (Global)" org-store-link t]
  16109. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16110. ["Insert Link" org-insert-link t]
  16111. ["Follow Link" org-open-at-point t]
  16112. "--"
  16113. ["Next link" org-next-link t]
  16114. ["Previous link" org-previous-link t]
  16115. "--"
  16116. ["Descriptive Links"
  16117. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16118. :style radio
  16119. :selected (member '(org-link) buffer-invisibility-spec)]
  16120. ["Literal Links"
  16121. (progn
  16122. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16123. :style radio
  16124. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16125. "--"
  16126. ("TODO Lists"
  16127. ["TODO/DONE/-" org-todo t]
  16128. ("Select keyword"
  16129. ["Next keyword" org-shiftright (org-on-heading-p)]
  16130. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16131. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16132. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16133. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16134. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16135. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16136. "--"
  16137. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16138. :selected org-enforce-todo-dependencies :style toggle :active t]
  16139. "Settings for tree at point"
  16140. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16141. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  16142. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16143. ["Do Children parallel" org-toggle-ordered-property :style radio
  16144. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  16145. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16146. "--"
  16147. ["Set Priority" org-priority t]
  16148. ["Priority Up" org-shiftup t]
  16149. ["Priority Down" org-shiftdown t]
  16150. "--"
  16151. ["Get news from all feeds" org-feed-update-all t]
  16152. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16153. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16154. ("TAGS and Properties"
  16155. ["Set Tags" org-set-tags-command t]
  16156. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16157. "--"
  16158. ["Set property" org-set-property t]
  16159. ["Column view of properties" org-columns t]
  16160. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16161. ("Dates and Scheduling"
  16162. ["Timestamp" org-time-stamp t]
  16163. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16164. ("Change Date"
  16165. ["1 Day Later" org-shiftright t]
  16166. ["1 Day Earlier" org-shiftleft t]
  16167. ["1 ... Later" org-shiftup t]
  16168. ["1 ... Earlier" org-shiftdown t])
  16169. ["Compute Time Range" org-evaluate-time-range t]
  16170. ["Schedule Item" org-schedule t]
  16171. ["Deadline" org-deadline t]
  16172. "--"
  16173. ["Custom time format" org-toggle-time-stamp-overlays
  16174. :style radio :selected org-display-custom-times]
  16175. "--"
  16176. ["Goto Calendar" org-goto-calendar t]
  16177. ["Date from Calendar" org-date-from-calendar t]
  16178. "--"
  16179. ["Start/Restart Timer" org-timer-start t]
  16180. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16181. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16182. ["Insert Timer String" org-timer t]
  16183. ["Insert Timer Item" org-timer-item t])
  16184. ("Logging work"
  16185. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16186. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16187. ["Clock out" org-clock-out t]
  16188. ["Clock cancel" org-clock-cancel t]
  16189. "--"
  16190. ["Mark as default task" org-clock-mark-default-task t]
  16191. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16192. ["Goto running clock" org-clock-goto t]
  16193. "--"
  16194. ["Display times" org-clock-display t]
  16195. ["Create clock table" org-clock-report t]
  16196. "--"
  16197. ["Record DONE time"
  16198. (progn (setq org-log-done (not org-log-done))
  16199. (message "Switching to %s will %s record a timestamp"
  16200. (car org-done-keywords)
  16201. (if org-log-done "automatically" "not")))
  16202. :style toggle :selected org-log-done])
  16203. "--"
  16204. ["Agenda Command..." org-agenda t]
  16205. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16206. ("File List for Agenda")
  16207. ("Special views current file"
  16208. ["TODO Tree" org-show-todo-tree t]
  16209. ["Check Deadlines" org-check-deadlines t]
  16210. ["Timeline" org-timeline t]
  16211. ["Tags/Property tree" org-match-sparse-tree t])
  16212. "--"
  16213. ["Export/Publish..." org-export t]
  16214. ("LaTeX"
  16215. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16216. :selected org-cdlatex-mode]
  16217. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16218. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16219. ["Modify math symbol" org-cdlatex-math-modify
  16220. (org-inside-LaTeX-fragment-p)]
  16221. ["Insert citation" org-reftex-citation t]
  16222. "--"
  16223. ["Template for BEAMER" org-insert-beamer-options-template t])
  16224. "--"
  16225. ("MobileOrg"
  16226. ["Push Files and Views" org-mobile-push t]
  16227. ["Get Captured and Flagged" org-mobile-pull t]
  16228. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16229. "--"
  16230. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16231. "--"
  16232. ("Documentation"
  16233. ["Show Version" org-version t]
  16234. ["Info Documentation" org-info t])
  16235. ("Customize"
  16236. ["Browse Org Group" org-customize t]
  16237. "--"
  16238. ["Expand This Menu" org-create-customize-menu
  16239. (fboundp 'customize-menu-create)])
  16240. ["Send bug report" org-submit-bug-report t]
  16241. "--"
  16242. ("Refresh/Reload"
  16243. ["Refresh setup current buffer" org-mode-restart t]
  16244. ["Reload Org (after update)" org-reload t]
  16245. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16246. ))
  16247. (defun org-info (&optional node)
  16248. "Read documentation for Org-mode in the info system.
  16249. With optional NODE, go directly to that node."
  16250. (interactive)
  16251. (info (format "(org)%s" (or node ""))))
  16252. ;;;###autoload
  16253. (defun org-submit-bug-report ()
  16254. "Submit a bug report on Org-mode via mail.
  16255. Don't hesitate to report any problems or inaccurate documentation.
  16256. If you don't have setup sending mail from (X)Emacs, please copy the
  16257. output buffer into your mail program, as it gives us important
  16258. information about your Org-mode version and configuration."
  16259. (interactive)
  16260. (require 'reporter)
  16261. (org-load-modules-maybe)
  16262. (org-require-autoloaded-modules)
  16263. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16264. (reporter-submit-bug-report
  16265. "emacs-orgmode@gnu.org"
  16266. (org-version)
  16267. (let (list)
  16268. (save-window-excursion
  16269. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16270. (delete-other-windows)
  16271. (erase-buffer)
  16272. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16273. We would like to add your full Org-mode and Outline configuration to the
  16274. bug report. This greatly simplifies the work of the maintainer and
  16275. other experts on the mailing list.
  16276. HOWEVER, some variables you have customized may contain private
  16277. information. The names of customers, colleagues, or friends, might
  16278. appear in the form of file names, tags, todo states, or search strings.
  16279. If you answer yes to the prompt, you might want to check and remove
  16280. such private information before sending the email.")
  16281. (add-text-properties (point-min) (point-max) '(face org-warning))
  16282. (when (yes-or-no-p "Include your Org-mode configuration ")
  16283. (mapatoms
  16284. (lambda (v)
  16285. (and (boundp v)
  16286. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16287. (or (and (symbol-value v)
  16288. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16289. (and
  16290. (get v 'custom-type) (get v 'standard-value)
  16291. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16292. (push v list)))))
  16293. (kill-buffer (get-buffer "*Warn about privacy*"))
  16294. list))
  16295. nil nil
  16296. "Remember to cover the basics, that is, what you expected to happen and
  16297. what in fact did happen. You don't know how to make a good report? See
  16298. http://orgmode.org/manual/Feedback.html#Feedback
  16299. Your bug report will be posted to the Org-mode mailing list.
  16300. ------------------------------------------------------------------------")
  16301. (save-excursion
  16302. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16303. (replace-match "\\1Bug: \\3 [\\2]")))))
  16304. (defun org-install-agenda-files-menu ()
  16305. (let ((bl (buffer-list)))
  16306. (save-excursion
  16307. (while bl
  16308. (set-buffer (pop bl))
  16309. (if (org-mode-p) (setq bl nil)))
  16310. (when (org-mode-p)
  16311. (easy-menu-change
  16312. '("Org") "File List for Agenda"
  16313. (append
  16314. (list
  16315. ["Edit File List" (org-edit-agenda-file-list) t]
  16316. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16317. ["Remove Current File from List" org-remove-file t]
  16318. ["Cycle through agenda files" org-cycle-agenda-files t]
  16319. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16320. "--")
  16321. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16322. ;;;; Documentation
  16323. ;;;###autoload
  16324. (defun org-require-autoloaded-modules ()
  16325. (interactive)
  16326. (mapc 'require
  16327. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16328. org-docbook org-exp org-html org-icalendar
  16329. org-id org-latex
  16330. org-publish org-remember org-table
  16331. org-timer org-xoxo)))
  16332. ;;;###autoload
  16333. (defun org-reload (&optional uncompiled)
  16334. "Reload all org lisp files.
  16335. With prefix arg UNCOMPILED, load the uncompiled versions."
  16336. (interactive "P")
  16337. (require 'find-func)
  16338. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16339. (dir-org (file-name-directory (org-find-library-name "org")))
  16340. (dir-org-contrib (ignore-errors
  16341. (file-name-directory
  16342. (org-find-library-name "org-contribdir"))))
  16343. (babel-files
  16344. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16345. (append (list nil "comint" "eval" "exp" "keys"
  16346. "lob" "ref" "table" "tangle")
  16347. (delq nil
  16348. (mapcar
  16349. (lambda (lang)
  16350. (when (cdr lang) (symbol-name (car lang))))
  16351. org-babel-load-languages)))))
  16352. (files
  16353. (append (directory-files dir-org t file-re)
  16354. babel-files
  16355. (and dir-org-contrib
  16356. (directory-files dir-org-contrib t file-re))))
  16357. (remove-re (concat (if (featurep 'xemacs)
  16358. "org-colview" "org-colview-xemacs")
  16359. "\\'")))
  16360. (setq files (mapcar 'file-name-sans-extension files))
  16361. (setq files (mapcar
  16362. (lambda (x) (if (string-match remove-re x) nil x))
  16363. files))
  16364. (setq files (delq nil files))
  16365. (mapc
  16366. (lambda (f)
  16367. (when (featurep (intern (file-name-nondirectory f)))
  16368. (if (and (not uncompiled)
  16369. (file-exists-p (concat f ".elc")))
  16370. (load (concat f ".elc") nil nil t)
  16371. (load (concat f ".el") nil nil t))))
  16372. files))
  16373. (org-version))
  16374. ;;;###autoload
  16375. (defun org-customize ()
  16376. "Call the customize function with org as argument."
  16377. (interactive)
  16378. (org-load-modules-maybe)
  16379. (org-require-autoloaded-modules)
  16380. (customize-browse 'org))
  16381. (defun org-create-customize-menu ()
  16382. "Create a full customization menu for Org-mode, insert it into the menu."
  16383. (interactive)
  16384. (org-load-modules-maybe)
  16385. (org-require-autoloaded-modules)
  16386. (if (fboundp 'customize-menu-create)
  16387. (progn
  16388. (easy-menu-change
  16389. '("Org") "Customize"
  16390. `(["Browse Org group" org-customize t]
  16391. "--"
  16392. ,(customize-menu-create 'org)
  16393. ["Set" Custom-set t]
  16394. ["Save" Custom-save t]
  16395. ["Reset to Current" Custom-reset-current t]
  16396. ["Reset to Saved" Custom-reset-saved t]
  16397. ["Reset to Standard Settings" Custom-reset-standard t]))
  16398. (message "\"Org\"-menu now contains full customization menu"))
  16399. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16400. ;;;; Miscellaneous stuff
  16401. ;;; Generally useful functions
  16402. (defun org-get-at-bol (property)
  16403. "Get text property PROPERTY at beginning of line."
  16404. (get-text-property (point-at-bol) property))
  16405. (defun org-find-text-property-in-string (prop s)
  16406. "Return the first non-nil value of property PROP in string S."
  16407. (or (get-text-property 0 prop s)
  16408. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16409. prop s)))
  16410. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16411. "Display the given MESSAGE as a warning."
  16412. (if (fboundp 'display-warning)
  16413. (display-warning 'org message
  16414. (if (featurep 'xemacs) 'warning :warning))
  16415. (let ((buf (get-buffer-create "*Org warnings*")))
  16416. (with-current-buffer buf
  16417. (goto-char (point-max))
  16418. (insert "Warning (Org): " message)
  16419. (unless (bolp)
  16420. (newline)))
  16421. (display-buffer buf)
  16422. (sit-for 0))))
  16423. (defun org-in-commented-line ()
  16424. "Is point in a line starting with `#'?"
  16425. (equal (char-after (point-at-bol)) ?#))
  16426. (defun org-in-indented-comment-line ()
  16427. "Is point in a line starting with `#' after some white space?"
  16428. (save-excursion
  16429. (save-match-data
  16430. (goto-char (point-at-bol))
  16431. (looking-at "[ \t]*#"))))
  16432. (defun org-in-verbatim-emphasis ()
  16433. (save-match-data
  16434. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16435. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16436. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16437. (if (and marker (marker-buffer marker)
  16438. (buffer-live-p (marker-buffer marker)))
  16439. (progn
  16440. (switch-to-buffer (marker-buffer marker))
  16441. (if (or (> marker (point-max)) (< marker (point-min)))
  16442. (widen))
  16443. (goto-char marker)
  16444. (org-show-context 'org-goto))
  16445. (if bookmark
  16446. (bookmark-jump bookmark)
  16447. (error "Cannot find location"))))
  16448. (defun org-quote-csv-field (s)
  16449. "Quote field for inclusion in CSV material."
  16450. (if (string-match "[\",]" s)
  16451. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16452. s))
  16453. (defun org-plist-delete (plist property)
  16454. "Delete PROPERTY from PLIST.
  16455. This is in contrast to merely setting it to 0."
  16456. (let (p)
  16457. (while plist
  16458. (if (not (eq property (car plist)))
  16459. (setq p (plist-put p (car plist) (nth 1 plist))))
  16460. (setq plist (cddr plist)))
  16461. p))
  16462. (defun org-force-self-insert (N)
  16463. "Needed to enforce self-insert under remapping."
  16464. (interactive "p")
  16465. (self-insert-command N))
  16466. (defun org-string-width (s)
  16467. "Compute width of string, ignoring invisible characters.
  16468. This ignores character with invisibility property `org-link', and also
  16469. characters with property `org-cwidth', because these will become invisible
  16470. upon the next fontification round."
  16471. (let (b l)
  16472. (when (or (eq t buffer-invisibility-spec)
  16473. (assq 'org-link buffer-invisibility-spec))
  16474. (while (setq b (text-property-any 0 (length s)
  16475. 'invisible 'org-link s))
  16476. (setq s (concat (substring s 0 b)
  16477. (substring s (or (next-single-property-change
  16478. b 'invisible s) (length s)))))))
  16479. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16480. (setq s (concat (substring s 0 b)
  16481. (substring s (or (next-single-property-change
  16482. b 'org-cwidth s) (length s))))))
  16483. (setq l (string-width s) b -1)
  16484. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16485. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16486. l))
  16487. (defun org-shorten-string (s maxlength)
  16488. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  16489. If the string is shorter or has length MAXLENGTH, just return the
  16490. original string. If it is longer, the functions finds a space in the
  16491. string, breaks this string off at that locations and adds three dots
  16492. as ellipsis. Including the ellipsis, the string will not be longer
  16493. than MAXLENGTH. If finding a good breaking point in the string does
  16494. not work, the string is just chopped off in the middle of a word
  16495. if necessary."
  16496. (if (<= (length s) maxlength)
  16497. s
  16498. (let* ((n (max (- maxlength 4) 1))
  16499. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  16500. (if (string-match re s)
  16501. (concat (match-string 1 s) "...")
  16502. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  16503. (defun org-get-indentation (&optional line)
  16504. "Get the indentation of the current line, interpreting tabs.
  16505. When LINE is given, assume it represents a line and compute its indentation."
  16506. (if line
  16507. (if (string-match "^ *" (org-remove-tabs line))
  16508. (match-end 0))
  16509. (save-excursion
  16510. (beginning-of-line 1)
  16511. (skip-chars-forward " \t")
  16512. (current-column))))
  16513. (defun org-remove-tabs (s &optional width)
  16514. "Replace tabulators in S with spaces.
  16515. Assumes that s is a single line, starting in column 0."
  16516. (setq width (or width tab-width))
  16517. (while (string-match "\t" s)
  16518. (setq s (replace-match
  16519. (make-string
  16520. (- (* width (/ (+ (match-beginning 0) width) width))
  16521. (match-beginning 0)) ?\ )
  16522. t t s)))
  16523. s)
  16524. (defun org-fix-indentation (line ind)
  16525. "Fix indentation in LINE.
  16526. IND is a cons cell with target and minimum indentation.
  16527. If the current indentation in LINE is smaller than the minimum,
  16528. leave it alone. If it is larger than ind, set it to the target."
  16529. (let* ((l (org-remove-tabs line))
  16530. (i (org-get-indentation l))
  16531. (i1 (car ind)) (i2 (cdr ind)))
  16532. (if (>= i i2) (setq l (substring line i2)))
  16533. (if (> i1 0)
  16534. (concat (make-string i1 ?\ ) l)
  16535. l)))
  16536. (defun org-remove-indentation (code &optional n)
  16537. "Remove the maximum common indentation from the lines in CODE.
  16538. N may optionally be the number of spaces to remove."
  16539. (with-temp-buffer
  16540. (insert code)
  16541. (org-do-remove-indentation n)
  16542. (buffer-string)))
  16543. (defun org-do-remove-indentation (&optional n)
  16544. "Remove the maximum common indentation from the buffer."
  16545. (untabify (point-min) (point-max))
  16546. (let ((min 10000) re)
  16547. (if n
  16548. (setq min n)
  16549. (goto-char (point-min))
  16550. (while (re-search-forward "^ *[^ \n]" nil t)
  16551. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16552. (unless (or (= min 0) (= min 10000))
  16553. (setq re (format "^ \\{%d\\}" min))
  16554. (goto-char (point-min))
  16555. (while (re-search-forward re nil t)
  16556. (replace-match "")
  16557. (end-of-line 1))
  16558. min)))
  16559. (defun org-fill-template (template alist)
  16560. "Find each %key of ALIST in TEMPLATE and replace it."
  16561. (let ((case-fold-search nil)
  16562. entry key value)
  16563. (setq alist (sort (copy-sequence alist)
  16564. (lambda (a b) (< (length (car a)) (length (car b))))))
  16565. (while (setq entry (pop alist))
  16566. (setq template
  16567. (replace-regexp-in-string
  16568. (concat "%" (regexp-quote (car entry)))
  16569. (cdr entry) template t t)))
  16570. template))
  16571. (defun org-base-buffer (buffer)
  16572. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16573. (if (not buffer)
  16574. buffer
  16575. (or (buffer-base-buffer buffer)
  16576. buffer)))
  16577. (defun org-trim (s)
  16578. "Remove whitespace at beginning and end of string."
  16579. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16580. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16581. s)
  16582. (defun org-wrap (string &optional width lines)
  16583. "Wrap string to either a number of lines, or a width in characters.
  16584. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16585. that costs. If there is a word longer than WIDTH, the text is actually
  16586. wrapped to the length of that word.
  16587. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16588. many lines, whatever width that takes.
  16589. The return value is a list of lines, without newlines at the end."
  16590. (let* ((words (org-split-string string "[ \t\n]+"))
  16591. (maxword (apply 'max (mapcar 'org-string-width words)))
  16592. w ll)
  16593. (cond (width
  16594. (org-do-wrap words (max maxword width)))
  16595. (lines
  16596. (setq w maxword)
  16597. (setq ll (org-do-wrap words maxword))
  16598. (if (<= (length ll) lines)
  16599. ll
  16600. (setq ll words)
  16601. (while (> (length ll) lines)
  16602. (setq w (1+ w))
  16603. (setq ll (org-do-wrap words w)))
  16604. ll))
  16605. (t (error "Cannot wrap this")))))
  16606. (defun org-do-wrap (words width)
  16607. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16608. (let (lines line)
  16609. (while words
  16610. (setq line (pop words))
  16611. (while (and words (< (+ (length line) (length (car words))) width))
  16612. (setq line (concat line " " (pop words))))
  16613. (setq lines (push line lines)))
  16614. (nreverse lines)))
  16615. (defun org-split-string (string &optional separators)
  16616. "Splits STRING into substrings at SEPARATORS.
  16617. No empty strings are returned if there are matches at the beginning
  16618. and end of string."
  16619. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16620. (start 0)
  16621. notfirst
  16622. (list nil))
  16623. (while (and (string-match rexp string
  16624. (if (and notfirst
  16625. (= start (match-beginning 0))
  16626. (< start (length string)))
  16627. (1+ start) start))
  16628. (< (match-beginning 0) (length string)))
  16629. (setq notfirst t)
  16630. (or (eq (match-beginning 0) 0)
  16631. (and (eq (match-beginning 0) (match-end 0))
  16632. (eq (match-beginning 0) start))
  16633. (setq list
  16634. (cons (substring string start (match-beginning 0))
  16635. list)))
  16636. (setq start (match-end 0)))
  16637. (or (eq start (length string))
  16638. (setq list
  16639. (cons (substring string start)
  16640. list)))
  16641. (nreverse list)))
  16642. (defun org-quote-vert (s)
  16643. "Replace \"|\" with \"\\vert\"."
  16644. (while (string-match "|" s)
  16645. (setq s (replace-match "\\vert" t t s)))
  16646. s)
  16647. (defun org-uuidgen-p (s)
  16648. "Is S an ID created by UUIDGEN?"
  16649. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16650. (defun org-context ()
  16651. "Return a list of contexts of the current cursor position.
  16652. If several contexts apply, all are returned.
  16653. Each context entry is a list with a symbol naming the context, and
  16654. two positions indicating start and end of the context. Possible
  16655. contexts are:
  16656. :headline anywhere in a headline
  16657. :headline-stars on the leading stars in a headline
  16658. :todo-keyword on a TODO keyword (including DONE) in a headline
  16659. :tags on the TAGS in a headline
  16660. :priority on the priority cookie in a headline
  16661. :item on the first line of a plain list item
  16662. :item-bullet on the bullet/number of a plain list item
  16663. :checkbox on the checkbox in a plain list item
  16664. :table in an org-mode table
  16665. :table-special on a special filed in a table
  16666. :table-table in a table.el table
  16667. :link on a hyperlink
  16668. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16669. :target on a <<target>>
  16670. :radio-target on a <<<radio-target>>>
  16671. :latex-fragment on a LaTeX fragment
  16672. :latex-preview on a LaTeX fragment with overlayed preview image
  16673. This function expects the position to be visible because it uses font-lock
  16674. faces as a help to recognize the following contexts: :table-special, :link,
  16675. and :keyword."
  16676. (let* ((f (get-text-property (point) 'face))
  16677. (faces (if (listp f) f (list f)))
  16678. (p (point)) clist o)
  16679. ;; First the large context
  16680. (cond
  16681. ((org-on-heading-p t)
  16682. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16683. (when (progn
  16684. (beginning-of-line 1)
  16685. (looking-at org-todo-line-tags-regexp))
  16686. (push (org-point-in-group p 1 :headline-stars) clist)
  16687. (push (org-point-in-group p 2 :todo-keyword) clist)
  16688. (push (org-point-in-group p 4 :tags) clist))
  16689. (goto-char p)
  16690. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16691. (if (looking-at "\\[#[A-Z0-9]\\]")
  16692. (push (org-point-in-group p 0 :priority) clist)))
  16693. ((org-at-item-p)
  16694. (push (org-point-in-group p 2 :item-bullet) clist)
  16695. (push (list :item (point-at-bol)
  16696. (save-excursion (org-end-of-item) (point)))
  16697. clist)
  16698. (and (org-at-item-checkbox-p)
  16699. (push (org-point-in-group p 0 :checkbox) clist)))
  16700. ((org-at-table-p)
  16701. (push (list :table (org-table-begin) (org-table-end)) clist)
  16702. (if (memq 'org-formula faces)
  16703. (push (list :table-special
  16704. (previous-single-property-change p 'face)
  16705. (next-single-property-change p 'face)) clist)))
  16706. ((org-at-table-p 'any)
  16707. (push (list :table-table) clist)))
  16708. (goto-char p)
  16709. ;; Now the small context
  16710. (cond
  16711. ((org-at-timestamp-p)
  16712. (push (org-point-in-group p 0 :timestamp) clist))
  16713. ((memq 'org-link faces)
  16714. (push (list :link
  16715. (previous-single-property-change p 'face)
  16716. (next-single-property-change p 'face)) clist))
  16717. ((memq 'org-special-keyword faces)
  16718. (push (list :keyword
  16719. (previous-single-property-change p 'face)
  16720. (next-single-property-change p 'face)) clist))
  16721. ((org-on-target-p)
  16722. (push (org-point-in-group p 0 :target) clist)
  16723. (goto-char (1- (match-beginning 0)))
  16724. (if (looking-at org-radio-target-regexp)
  16725. (push (org-point-in-group p 0 :radio-target) clist))
  16726. (goto-char p))
  16727. ((setq o (car (delq nil
  16728. (mapcar
  16729. (lambda (x)
  16730. (if (memq x org-latex-fragment-image-overlays) x))
  16731. (overlays-at (point))))))
  16732. (push (list :latex-fragment
  16733. (overlay-start o) (overlay-end o)) clist)
  16734. (push (list :latex-preview
  16735. (overlay-start o) (overlay-end o)) clist))
  16736. ((org-inside-LaTeX-fragment-p)
  16737. ;; FIXME: positions wrong.
  16738. (push (list :latex-fragment (point) (point)) clist)))
  16739. (setq clist (nreverse (delq nil clist)))
  16740. clist))
  16741. ;; FIXME: Compare with at-regexp-p Do we need both?
  16742. (defun org-in-regexp (re &optional nlines visually)
  16743. "Check if point is inside a match of regexp.
  16744. Normally only the current line is checked, but you can include NLINES extra
  16745. lines both before and after point into the search.
  16746. If VISUALLY is set, require that the cursor is not after the match but
  16747. really on, so that the block visually is on the match."
  16748. (catch 'exit
  16749. (let ((pos (point))
  16750. (eol (point-at-eol (+ 1 (or nlines 0))))
  16751. (inc (if visually 1 0)))
  16752. (save-excursion
  16753. (beginning-of-line (- 1 (or nlines 0)))
  16754. (while (re-search-forward re eol t)
  16755. (if (and (<= (match-beginning 0) pos)
  16756. (>= (+ inc (match-end 0)) pos))
  16757. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16758. (defun org-at-regexp-p (regexp)
  16759. "Is point inside a match of REGEXP in the current line?"
  16760. (catch 'exit
  16761. (save-excursion
  16762. (let ((pos (point)) (end (point-at-eol)))
  16763. (beginning-of-line 1)
  16764. (while (re-search-forward regexp end t)
  16765. (if (and (<= (match-beginning 0) pos)
  16766. (>= (match-end 0) pos))
  16767. (throw 'exit t)))
  16768. nil))))
  16769. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  16770. "Return t if the current point is between matches of START-RE and END-RE.
  16771. This will also return t if point is on one of the two matches or
  16772. in an unfinished block. END-RE can be a string or a form
  16773. returning a string.
  16774. An optional third argument bounds the search for START-RE. It
  16775. defaults to previous heading or `point-min'."
  16776. (let ((pos (point))
  16777. (limit (or bound (save-excursion (outline-previous-heading)))))
  16778. (save-excursion
  16779. ;; we're on a block when point is on start-re...
  16780. (or (org-at-regexp-p start-re)
  16781. ;; ... or start-re can be found above...
  16782. (and (re-search-backward start-re limit t)
  16783. ;; ... but no end-re between start-re and point.
  16784. (not (re-search-forward (eval end-re) pos t)))))))
  16785. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16786. "Call `multi-occur' with buffers for all agenda files."
  16787. (interactive "sOrg-files matching: \np")
  16788. (let* ((files (org-agenda-files))
  16789. (tnames (mapcar 'file-truename files))
  16790. (extra org-agenda-text-search-extra-files)
  16791. f)
  16792. (when (eq (car extra) 'agenda-archives)
  16793. (setq extra (cdr extra))
  16794. (setq files (org-add-archive-files files)))
  16795. (while (setq f (pop extra))
  16796. (unless (member (file-truename f) tnames)
  16797. (add-to-list 'files f 'append)
  16798. (add-to-list 'tnames (file-truename f) 'append)))
  16799. (multi-occur
  16800. (mapcar (lambda (x)
  16801. (with-current-buffer
  16802. (or (get-file-buffer x) (find-file-noselect x))
  16803. (widen)
  16804. (current-buffer)))
  16805. files)
  16806. regexp)))
  16807. (if (boundp 'occur-mode-find-occurrence-hook)
  16808. ;; Emacs 23
  16809. (add-hook 'occur-mode-find-occurrence-hook
  16810. (lambda ()
  16811. (when (org-mode-p)
  16812. (org-reveal))))
  16813. ;; Emacs 22
  16814. (defadvice occur-mode-goto-occurrence
  16815. (after org-occur-reveal activate)
  16816. (and (org-mode-p) (org-reveal)))
  16817. (defadvice occur-mode-goto-occurrence-other-window
  16818. (after org-occur-reveal activate)
  16819. (and (org-mode-p) (org-reveal)))
  16820. (defadvice occur-mode-display-occurrence
  16821. (after org-occur-reveal activate)
  16822. (when (org-mode-p)
  16823. (let ((pos (occur-mode-find-occurrence)))
  16824. (with-current-buffer (marker-buffer pos)
  16825. (save-excursion
  16826. (goto-char pos)
  16827. (org-reveal)))))))
  16828. (defun org-occur-link-in-agenda-files ()
  16829. "Create a link and search for it in the agendas.
  16830. The link is not stored in `org-stored-links', it is just created
  16831. for the search purpose."
  16832. (interactive)
  16833. (let ((link (condition-case nil
  16834. (org-store-link nil)
  16835. (error "Unable to create a link to here"))))
  16836. (org-occur-in-agenda-files (regexp-quote link))))
  16837. (defun org-uniquify (list)
  16838. "Remove duplicate elements from LIST."
  16839. (let (res)
  16840. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16841. res))
  16842. (defun org-delete-all (elts list)
  16843. "Remove all elements in ELTS from LIST."
  16844. (while elts
  16845. (setq list (delete (pop elts) list)))
  16846. list)
  16847. (defun org-count (cl-item cl-seq)
  16848. "Count the number of occurrences of ITEM in SEQ.
  16849. Taken from `count' in cl-seq.el with all keyword arguments removed."
  16850. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  16851. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  16852. (while (< cl-start cl-end)
  16853. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  16854. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  16855. (setq cl-start (1+ cl-start)))
  16856. cl-count))
  16857. (defun org-remove-if (predicate seq)
  16858. "Remove everything from SEQ that fulfills PREDICATE."
  16859. (let (res e)
  16860. (while seq
  16861. (setq e (pop seq))
  16862. (if (not (funcall predicate e)) (push e res)))
  16863. (nreverse res)))
  16864. (defun org-remove-if-not (predicate seq)
  16865. "Remove everything from SEQ that does not fulfill PREDICATE."
  16866. (let (res e)
  16867. (while seq
  16868. (setq e (pop seq))
  16869. (if (funcall predicate e) (push e res)))
  16870. (nreverse res)))
  16871. (defun org-back-over-empty-lines ()
  16872. "Move backwards over whitespace, to the beginning of the first empty line.
  16873. Returns the number of empty lines passed."
  16874. (let ((pos (point)))
  16875. (skip-chars-backward " \t\n\r")
  16876. (beginning-of-line 2)
  16877. (goto-char (min (point) pos))
  16878. (count-lines (point) pos)))
  16879. (defun org-skip-whitespace ()
  16880. (skip-chars-forward " \t\n\r"))
  16881. (defun org-point-in-group (point group &optional context)
  16882. "Check if POINT is in match-group GROUP.
  16883. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16884. match. If the match group does not exist or point is not inside it,
  16885. return nil."
  16886. (and (match-beginning group)
  16887. (>= point (match-beginning group))
  16888. (<= point (match-end group))
  16889. (if context
  16890. (list context (match-beginning group) (match-end group))
  16891. t)))
  16892. (defun org-switch-to-buffer-other-window (&rest args)
  16893. "Switch to buffer in a second window on the current frame.
  16894. In particular, do not allow pop-up frames.
  16895. Returns the newly created buffer."
  16896. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16897. special-display-function)
  16898. (apply 'switch-to-buffer-other-window args)))
  16899. (defun org-combine-plists (&rest plists)
  16900. "Create a single property list from all plists in PLISTS.
  16901. The process starts by copying the first list, and then setting properties
  16902. from the other lists. Settings in the last list are the most significant
  16903. ones and overrule settings in the other lists."
  16904. (let ((rtn (copy-sequence (pop plists)))
  16905. p v ls)
  16906. (while plists
  16907. (setq ls (pop plists))
  16908. (while ls
  16909. (setq p (pop ls) v (pop ls))
  16910. (setq rtn (plist-put rtn p v))))
  16911. rtn))
  16912. (defun org-move-line-down (arg)
  16913. "Move the current line down. With prefix argument, move it past ARG lines."
  16914. (interactive "p")
  16915. (let ((col (current-column))
  16916. beg end pos)
  16917. (beginning-of-line 1) (setq beg (point))
  16918. (beginning-of-line 2) (setq end (point))
  16919. (beginning-of-line (+ 1 arg))
  16920. (setq pos (move-marker (make-marker) (point)))
  16921. (insert (delete-and-extract-region beg end))
  16922. (goto-char pos)
  16923. (org-move-to-column col)))
  16924. (defun org-move-line-up (arg)
  16925. "Move the current line up. With prefix argument, move it past ARG lines."
  16926. (interactive "p")
  16927. (let ((col (current-column))
  16928. beg end pos)
  16929. (beginning-of-line 1) (setq beg (point))
  16930. (beginning-of-line 2) (setq end (point))
  16931. (beginning-of-line (- arg))
  16932. (setq pos (move-marker (make-marker) (point)))
  16933. (insert (delete-and-extract-region beg end))
  16934. (goto-char pos)
  16935. (org-move-to-column col)))
  16936. (defun org-replace-escapes (string table)
  16937. "Replace %-escapes in STRING with values in TABLE.
  16938. TABLE is an association list with keys like \"%a\" and string values.
  16939. The sequences in STRING may contain normal field width and padding information,
  16940. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16941. so values can contain further %-escapes if they are define later in TABLE."
  16942. (let ((tbl (copy-alist table))
  16943. (case-fold-search nil)
  16944. (pchg 0)
  16945. e re rpl)
  16946. (while (setq e (pop tbl))
  16947. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16948. (when (and (cdr e) (string-match re (cdr e)))
  16949. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16950. (safe "SREF"))
  16951. (add-text-properties 0 3 (list 'sref sref) safe)
  16952. (setcdr e (replace-match safe t t (cdr e)))))
  16953. (while (string-match re string)
  16954. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16955. (cdr e)))
  16956. (setq string (replace-match rpl t t string))))
  16957. (while (setq pchg (next-property-change pchg string))
  16958. (let ((sref (get-text-property pchg 'sref string)))
  16959. (when (and sref (string-match "SREF" string pchg))
  16960. (setq string (replace-match sref t t string)))))
  16961. string))
  16962. (defun org-sublist (list start end)
  16963. "Return a section of LIST, from START to END.
  16964. Counting starts at 1."
  16965. (let (rtn (c start))
  16966. (setq list (nthcdr (1- start) list))
  16967. (while (and list (<= c end))
  16968. (push (pop list) rtn)
  16969. (setq c (1+ c)))
  16970. (nreverse rtn)))
  16971. (defun org-find-base-buffer-visiting (file)
  16972. "Like `find-buffer-visiting' but always return the base buffer and
  16973. not an indirect buffer."
  16974. (let ((buf (or (get-file-buffer file)
  16975. (find-buffer-visiting file))))
  16976. (if buf
  16977. (or (buffer-base-buffer buf) buf)
  16978. nil)))
  16979. (defun org-image-file-name-regexp (&optional extensions)
  16980. "Return regexp matching the file names of images.
  16981. If EXTENSIONS is given, only match these."
  16982. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16983. (image-file-name-regexp)
  16984. (let ((image-file-name-extensions
  16985. (or extensions
  16986. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16987. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16988. (concat "\\."
  16989. (regexp-opt (nconc (mapcar 'upcase
  16990. image-file-name-extensions)
  16991. image-file-name-extensions)
  16992. t)
  16993. "\\'"))))
  16994. (defun org-file-image-p (file &optional extensions)
  16995. "Return non-nil if FILE is an image."
  16996. (save-match-data
  16997. (string-match (org-image-file-name-regexp extensions) file)))
  16998. (defun org-get-cursor-date ()
  16999. "Return the date at cursor in as a time.
  17000. This works in the calendar and in the agenda, anywhere else it just
  17001. returns the current time."
  17002. (let (date day defd)
  17003. (cond
  17004. ((eq major-mode 'calendar-mode)
  17005. (setq date (calendar-cursor-to-date)
  17006. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17007. ((eq major-mode 'org-agenda-mode)
  17008. (setq day (get-text-property (point) 'day))
  17009. (if day
  17010. (setq date (calendar-gregorian-from-absolute day)
  17011. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17012. (nth 2 date))))))
  17013. (or defd (current-time))))
  17014. (defvar org-agenda-action-marker (make-marker)
  17015. "Marker pointing to the entry for the next agenda action.")
  17016. (defun org-mark-entry-for-agenda-action ()
  17017. "Mark the current entry as target of an agenda action.
  17018. Agenda actions are actions executed from the agenda with the key `k',
  17019. which make use of the date at the cursor."
  17020. (interactive)
  17021. (move-marker org-agenda-action-marker
  17022. (save-excursion (org-back-to-heading t) (point))
  17023. (current-buffer))
  17024. (message
  17025. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17026. ;;; Paragraph filling stuff.
  17027. ;; We want this to be just right, so use the full arsenal.
  17028. (defun org-indent-line-function ()
  17029. "Indent line depending on context."
  17030. (interactive)
  17031. (let* ((pos (point))
  17032. (itemp (org-at-item-p))
  17033. (case-fold-search t)
  17034. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17035. (inline-task-p (and (featurep 'org-inlinetask)
  17036. (org-inlinetask-in-task-p)))
  17037. column bpos bcol tpos tcol)
  17038. (beginning-of-line 1)
  17039. (cond
  17040. ;; Comments
  17041. ((looking-at "# ") (setq column 0))
  17042. ;; Headings
  17043. ((looking-at "\\*+ ") (setq column 0))
  17044. ;; Literal examples
  17045. ((looking-at "[ \t]*:[ \t]")
  17046. (setq column (org-get-indentation))) ; do nothing
  17047. ;; Drawers
  17048. ((and (looking-at "[ \t]*:END:")
  17049. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17050. (save-excursion
  17051. (goto-char (1- (match-beginning 1)))
  17052. (setq column (current-column))))
  17053. ;; Special blocks
  17054. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17055. (save-excursion
  17056. (re-search-backward
  17057. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17058. (setq column (org-get-indentation (match-string 0))))
  17059. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17060. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17061. (save-excursion
  17062. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17063. (setq column
  17064. (if (equal (downcase (match-string 1)) "src")
  17065. ;; src blocks: let `org-edit-src-exit' handle them
  17066. (org-get-indentation)
  17067. (org-get-indentation (match-string 0)))))
  17068. ;; Lists
  17069. ((org-in-item-p)
  17070. (org-beginning-of-item)
  17071. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\(:?\\[@\\(:?start:\\)?[0-9]+\\][ \t]*\\)?\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  17072. (setq bpos (match-beginning 1) tpos (match-end 0)
  17073. bcol (progn (goto-char bpos) (current-column))
  17074. tcol (progn (goto-char tpos) (current-column)))
  17075. (if (> tcol (+ bcol org-description-max-indent))
  17076. (setq tcol (+ bcol 5)))
  17077. (goto-char pos)
  17078. (setq column (if itemp (org-get-indentation) tcol)))
  17079. ;; This line has nothing special, look at the previous relevant
  17080. ;; line to compute indentation
  17081. (t
  17082. (beginning-of-line 0)
  17083. (while (and (not (bobp))
  17084. (not (looking-at org-drawer-regexp))
  17085. ;; skip comments, verbatim, empty lines, tables,
  17086. ;; inline tasks, lists, drawers and blocks
  17087. (or (and (looking-at "[ \t]*:END:")
  17088. (re-search-backward org-drawer-regexp nil t))
  17089. (and (looking-at "[ \t]*#\\+end_")
  17090. (re-search-backward "[ \t]*#\\+begin_"nil t))
  17091. (looking-at "[ \t]*[\n:#|]")
  17092. (and (org-in-item-p) (goto-char (org-list-top-point)))
  17093. (and (not inline-task-p)
  17094. (featurep 'org-inlinetask)
  17095. (org-inlinetask-in-task-p))))
  17096. (beginning-of-line 0))
  17097. (cond
  17098. ;; There was an heading above.
  17099. ((looking-at "\\*+[ \t]+")
  17100. (if (not org-adapt-indentation)
  17101. (setq column 0)
  17102. (goto-char (match-end 0))
  17103. (setq column (current-column))))
  17104. ;; A drawer had started and is unfinished
  17105. ((looking-at org-drawer-regexp)
  17106. (goto-char (1- (match-beginning 1)))
  17107. (setq column (current-column)))
  17108. ;; Else, nothing noticeable found: get indentation and go on.
  17109. (t (setq column (org-get-indentation))))))
  17110. ;; Now apply indentation and move cursor accordingly
  17111. (goto-char pos)
  17112. (if (<= (current-column) (current-indentation))
  17113. (org-indent-line-to column)
  17114. (save-excursion (org-indent-line-to column)))
  17115. ;; Special polishing for properties, see `org-property-format'
  17116. (setq column (current-column))
  17117. (beginning-of-line 1)
  17118. (if (looking-at
  17119. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17120. (replace-match (concat (match-string 1)
  17121. (format org-property-format
  17122. (match-string 2) (match-string 3)))
  17123. t t))
  17124. (org-move-to-column column)))
  17125. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17126. "Variable to store copy of `adaptive-fill-regexp'.
  17127. Since `adaptive-fill-regexp' is set to never match, we need to
  17128. store a backup of its value before entering `org-mode' so that
  17129. the functionality can be provided as a fall-back.")
  17130. (defun org-set-autofill-regexps ()
  17131. (interactive)
  17132. ;; In the paragraph separator we include headlines, because filling
  17133. ;; text in a line directly attached to a headline would otherwise
  17134. ;; fill the headline as well.
  17135. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17136. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17137. ;; The paragraph starter includes hand-formatted lists.
  17138. (org-set-local
  17139. 'paragraph-start
  17140. (concat
  17141. "\f" "\\|"
  17142. "[ ]*$" "\\|"
  17143. "\\*+ " "\\|"
  17144. "[ \t]*#" "\\|"
  17145. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  17146. "[ \t]*[:|]" "\\|"
  17147. "\\$\\$" "\\|"
  17148. "\\\\\\(begin\\|end\\|[][]\\)"))
  17149. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17150. ;; But only if the user has not turned off tables or fixed-width regions
  17151. (org-set-local
  17152. 'auto-fill-inhibit-regexp
  17153. (concat "\\*+ \\|#\\+"
  17154. "\\|[ \t]*" org-keyword-time-regexp
  17155. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17156. (concat
  17157. "\\|[ \t]*["
  17158. (if org-enable-table-editor "|" "")
  17159. (if org-enable-fixed-width-editor ":" "")
  17160. "]"))))
  17161. ;; We use our own fill-paragraph function, to make sure that tables
  17162. ;; and fixed-width regions are not wrapped. That function will pass
  17163. ;; through to `fill-paragraph' when appropriate.
  17164. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17165. ;; Adaptive filling: To get full control, first make sure that
  17166. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17167. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17168. (org-set-local 'org-adaptive-fill-regexp-backup
  17169. adaptive-fill-regexp))
  17170. (org-set-local 'adaptive-fill-regexp "\000")
  17171. (org-set-local 'adaptive-fill-function
  17172. 'org-adaptive-fill-function)
  17173. (org-set-local
  17174. 'align-mode-rules-list
  17175. '((org-in-buffer-settings
  17176. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17177. (modes . '(org-mode))))))
  17178. (defun org-fill-paragraph (&optional justify)
  17179. "Re-align a table, pass through to fill-paragraph if no table."
  17180. (let ((table-p (org-at-table-p))
  17181. (table.el-p (org-at-table.el-p)))
  17182. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17183. (save-excursion (goto-char (point-at-bol))
  17184. (looking-at outline-regexp)))
  17185. t) ; skip headlines
  17186. (table.el-p t) ; skip table.el tables
  17187. (table-p (org-table-align) t) ; align org-mode tables
  17188. (t nil)))) ; call paragraph-fill
  17189. ;; For reference, this is the default value of adaptive-fill-regexp
  17190. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17191. (defun org-adaptive-fill-function ()
  17192. "Return a fill prefix for org-mode files.
  17193. In particular, this makes sure hanging paragraphs for hand-formatted lists
  17194. work correctly."
  17195. (cond
  17196. ;; Comment line
  17197. ((looking-at "#[ \t]+")
  17198. (match-string-no-properties 0))
  17199. ;; Description list
  17200. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  17201. (save-excursion
  17202. (if (> (match-end 1) (+ (match-beginning 1)
  17203. org-description-max-indent))
  17204. (goto-char (+ (match-beginning 1) 5))
  17205. (goto-char (match-end 0)))
  17206. (make-string (current-column) ?\ )))
  17207. ;; Ordered or unordered list
  17208. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  17209. (save-excursion
  17210. (goto-char (match-end 0))
  17211. (make-string (current-column) ?\ )))
  17212. ;; Other text
  17213. ((looking-at org-adaptive-fill-regexp-backup)
  17214. (match-string-no-properties 0))))
  17215. ;;; Other stuff.
  17216. (defun org-toggle-fixed-width-section (arg)
  17217. "Toggle the fixed-width export.
  17218. If there is no active region, the QUOTE keyword at the current headline is
  17219. inserted or removed. When present, it causes the text between this headline
  17220. and the next to be exported as fixed-width text, and unmodified.
  17221. If there is an active region, this command adds or removes a colon as the
  17222. first character of this line. If the first character of a line is a colon,
  17223. this line is also exported in fixed-width font."
  17224. (interactive "P")
  17225. (let* ((cc 0)
  17226. (regionp (org-region-active-p))
  17227. (beg (if regionp (region-beginning) (point)))
  17228. (end (if regionp (region-end)))
  17229. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17230. (case-fold-search nil)
  17231. (re "[ \t]*\\(: \\)")
  17232. off)
  17233. (if regionp
  17234. (save-excursion
  17235. (goto-char beg)
  17236. (setq cc (current-column))
  17237. (beginning-of-line 1)
  17238. (setq off (looking-at re))
  17239. (while (> nlines 0)
  17240. (setq nlines (1- nlines))
  17241. (beginning-of-line 1)
  17242. (cond
  17243. (arg
  17244. (org-move-to-column cc t)
  17245. (insert ": \n")
  17246. (forward-line -1))
  17247. ((and off (looking-at re))
  17248. (replace-match "" t t nil 1))
  17249. ((not off) (org-move-to-column cc t) (insert ": ")))
  17250. (forward-line 1)))
  17251. (save-excursion
  17252. (org-back-to-heading)
  17253. (if (looking-at (concat outline-regexp
  17254. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17255. (replace-match "" t t nil 1)
  17256. (if (looking-at outline-regexp)
  17257. (progn
  17258. (goto-char (match-end 0))
  17259. (insert org-quote-string " "))))))))
  17260. (defun org-reftex-citation ()
  17261. "Use reftex-citation to insert a citation into the buffer.
  17262. This looks for a line like
  17263. #+BIBLIOGRAPHY: foo plain option:-d
  17264. and derives from it that foo.bib is the bibliography file relevant
  17265. for this document. It then installs the necessary environment for RefTeX
  17266. to work in this buffer and calls `reftex-citation' to insert a citation
  17267. into the buffer.
  17268. Export of such citations to both LaTeX and HTML is handled by the contributed
  17269. package org-exp-bibtex by Taru Karttunen."
  17270. (interactive)
  17271. (let ((reftex-docstruct-symbol 'rds)
  17272. (reftex-cite-format "\\cite{%l}")
  17273. rds bib)
  17274. (save-excursion
  17275. (save-restriction
  17276. (widen)
  17277. (let ((case-fold-search t)
  17278. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17279. (if (not (save-excursion
  17280. (or (re-search-forward re nil t)
  17281. (re-search-backward re nil t))))
  17282. (error "No bibliography defined in file")
  17283. (setq bib (concat (match-string 1) ".bib")
  17284. rds (list (list 'bib bib)))))))
  17285. (call-interactively 'reftex-citation)))
  17286. ;;;; Functions extending outline functionality
  17287. (defun org-beginning-of-line (&optional arg)
  17288. "Go to the beginning of the current line. If that is invisible, continue
  17289. to a visible line beginning. This makes the function of C-a more intuitive.
  17290. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17291. first attempt, and only move to after the tags when the cursor is already
  17292. beyond the end of the headline."
  17293. (interactive "P")
  17294. (let ((pos (point))
  17295. (special (if (consp org-special-ctrl-a/e)
  17296. (car org-special-ctrl-a/e)
  17297. org-special-ctrl-a/e))
  17298. refpos)
  17299. (if (org-bound-and-true-p line-move-visual)
  17300. (beginning-of-visual-line 1)
  17301. (beginning-of-line 1))
  17302. (if (and arg (fboundp 'move-beginning-of-line))
  17303. (call-interactively 'move-beginning-of-line)
  17304. (if (bobp)
  17305. nil
  17306. (backward-char 1)
  17307. (if (org-truely-invisible-p)
  17308. (while (and (not (bobp)) (org-truely-invisible-p))
  17309. (backward-char 1)
  17310. (beginning-of-line 1))
  17311. (forward-char 1))))
  17312. (when special
  17313. (cond
  17314. ((and (looking-at org-complex-heading-regexp)
  17315. (= (char-after (match-end 1)) ?\ ))
  17316. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17317. (point-at-eol)))
  17318. (goto-char
  17319. (if (eq special t)
  17320. (cond ((> pos refpos) refpos)
  17321. ((= pos (point)) refpos)
  17322. (t (point)))
  17323. (cond ((> pos (point)) (point))
  17324. ((not (eq last-command this-command)) (point))
  17325. (t refpos)))))
  17326. ((org-at-item-p)
  17327. (goto-char
  17328. (if (eq special t)
  17329. (cond ((> pos (match-end 4)) (match-end 4))
  17330. ((= pos (point)) (match-end 4))
  17331. (t (point)))
  17332. (cond ((> pos (point)) (point))
  17333. ((not (eq last-command this-command)) (point))
  17334. (t (match-end 4))))))))
  17335. (org-no-warnings
  17336. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17337. (defun org-end-of-line (&optional arg)
  17338. "Go to the end of the line.
  17339. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17340. first attempt, and only move to after the tags when the cursor is already
  17341. beyond the end of the headline."
  17342. (interactive "P")
  17343. (let ((special (if (consp org-special-ctrl-a/e)
  17344. (cdr org-special-ctrl-a/e)
  17345. org-special-ctrl-a/e)))
  17346. (if (or (not special)
  17347. (not (org-on-heading-p))
  17348. arg)
  17349. (call-interactively
  17350. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17351. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17352. (t 'end-of-line)))
  17353. (let ((pos (point)))
  17354. (beginning-of-line 1)
  17355. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  17356. (if (eq special t)
  17357. (if (or (< pos (match-beginning 1))
  17358. (= pos (match-end 0)))
  17359. (goto-char (match-beginning 1))
  17360. (goto-char (match-end 0)))
  17361. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17362. (goto-char (match-end 0))
  17363. (goto-char (match-beginning 1))))
  17364. (call-interactively (if (fboundp 'move-end-of-line)
  17365. 'move-end-of-line
  17366. 'end-of-line)))))
  17367. (org-no-warnings
  17368. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17369. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17370. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17371. (defun org-backward-sentence (&optional arg)
  17372. "Go to beginning of sentence, or beginning of table field.
  17373. This will call `backward-sentence' or `org-table-beginning-of-field',
  17374. depending on context."
  17375. (interactive "P")
  17376. (cond
  17377. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17378. (t (call-interactively 'backward-sentence))))
  17379. (defun org-forward-sentence (&optional arg)
  17380. "Go to end of sentence, or end of table field.
  17381. This will call `forward-sentence' or `org-table-end-of-field',
  17382. depending on context."
  17383. (interactive "P")
  17384. (cond
  17385. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17386. (t (call-interactively 'forward-sentence))))
  17387. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17388. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17389. (defun org-kill-line (&optional arg)
  17390. "Kill line, to tags or end of line."
  17391. (interactive "P")
  17392. (cond
  17393. ((or (not org-special-ctrl-k)
  17394. (bolp)
  17395. (not (org-on-heading-p)))
  17396. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17397. org-ctrl-k-protect-subtree)
  17398. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17399. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17400. (error "C-k aborted - would kill hidden subtree")))
  17401. (call-interactively 'kill-line))
  17402. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  17403. (kill-region (point) (match-beginning 1))
  17404. (org-set-tags nil t))
  17405. (t (kill-region (point) (point-at-eol)))))
  17406. (define-key org-mode-map "\C-k" 'org-kill-line)
  17407. (defun org-yank (&optional arg)
  17408. "Yank. If the kill is a subtree, treat it specially.
  17409. This command will look at the current kill and check if is a single
  17410. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17411. cursor is at the beginning of a line or after the stars of a currently
  17412. empty headline, then the yank is handled specially. How exactly depends
  17413. on the value of the following variables, both set by default.
  17414. org-yank-folded-subtrees
  17415. When set, the subtree(s) will be folded after insertion, but only
  17416. if doing so would now swallow text after the yanked text.
  17417. org-yank-adjusted-subtrees
  17418. When set, the subtree will be promoted or demoted in order to
  17419. fit into the local outline tree structure, which means that the level
  17420. will be adjusted so that it becomes the smaller one of the two
  17421. *visible* surrounding headings.
  17422. Any prefix to this command will cause `yank' to be called directly with
  17423. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17424. will just
  17425. plainly yank the text as it is.
  17426. \[1] The test checks if the first non-white line is a heading
  17427. and if there are no other headings with fewer stars."
  17428. (interactive "P")
  17429. (org-yank-generic 'yank arg))
  17430. (defun org-yank-generic (command arg)
  17431. "Perform some yank-like command.
  17432. This function implements the behavior described in the `org-yank'
  17433. documentation. However, it has been generalized to work for any
  17434. interactive command with similar behavior."
  17435. ;; pretend to be command COMMAND
  17436. (setq this-command command)
  17437. (if arg
  17438. (call-interactively command)
  17439. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17440. (and (org-kill-is-subtree-p)
  17441. (or (bolp)
  17442. (and (looking-at "[ \t]*$")
  17443. (string-match
  17444. "\\`\\*+\\'"
  17445. (buffer-substring (point-at-bol) (point)))))))
  17446. swallowp)
  17447. (cond
  17448. ((and subtreep org-yank-folded-subtrees)
  17449. (let ((beg (point))
  17450. end)
  17451. (if (and subtreep org-yank-adjusted-subtrees)
  17452. (org-paste-subtree nil nil 'for-yank)
  17453. (call-interactively command))
  17454. (setq end (point))
  17455. (goto-char beg)
  17456. (when (and (bolp) subtreep
  17457. (not (setq swallowp
  17458. (org-yank-folding-would-swallow-text beg end))))
  17459. (or (looking-at outline-regexp)
  17460. (re-search-forward (concat "^" outline-regexp) end t))
  17461. (while (and (< (point) end) (looking-at outline-regexp))
  17462. (hide-subtree)
  17463. (org-cycle-show-empty-lines 'folded)
  17464. (condition-case nil
  17465. (outline-forward-same-level 1)
  17466. (error (goto-char end)))))
  17467. (when swallowp
  17468. (message
  17469. "Inserted text not folded because that would swallow text"))
  17470. (goto-char end)
  17471. (skip-chars-forward " \t\n\r")
  17472. (beginning-of-line 1)
  17473. (push-mark beg 'nomsg)))
  17474. ((and subtreep org-yank-adjusted-subtrees)
  17475. (let ((beg (point-at-bol)))
  17476. (org-paste-subtree nil nil 'for-yank)
  17477. (push-mark beg 'nomsg)))
  17478. (t
  17479. (call-interactively command))))))
  17480. (defun org-yank-folding-would-swallow-text (beg end)
  17481. "Would hide-subtree at BEG swallow any text after END?"
  17482. (let (level)
  17483. (save-excursion
  17484. (goto-char beg)
  17485. (when (or (looking-at outline-regexp)
  17486. (re-search-forward (concat "^" outline-regexp) end t))
  17487. (setq level (org-outline-level)))
  17488. (goto-char end)
  17489. (skip-chars-forward " \t\r\n\v\f")
  17490. (if (or (eobp)
  17491. (and (bolp) (looking-at org-outline-regexp)
  17492. (<= (org-outline-level) level)))
  17493. nil ; Nothing would be swallowed
  17494. t)))) ; something would swallow
  17495. (define-key org-mode-map "\C-y" 'org-yank)
  17496. (defun org-invisible-p ()
  17497. "Check if point is at a character currently not visible."
  17498. ;; Early versions of noutline don't have `outline-invisible-p'.
  17499. (if (fboundp 'outline-invisible-p)
  17500. (outline-invisible-p)
  17501. (get-char-property (point) 'invisible)))
  17502. (defun org-truely-invisible-p ()
  17503. "Check if point is at a character currently not visible.
  17504. This version does not only check the character property, but also
  17505. `visible-mode'."
  17506. ;; Early versions of noutline don't have `outline-invisible-p'.
  17507. (if (org-bound-and-true-p visible-mode)
  17508. nil
  17509. (if (fboundp 'outline-invisible-p)
  17510. (outline-invisible-p)
  17511. (get-char-property (point) 'invisible))))
  17512. (defun org-invisible-p2 ()
  17513. "Check if point is at a character currently not visible."
  17514. (save-excursion
  17515. (if (and (eolp) (not (bobp))) (backward-char 1))
  17516. ;; Early versions of noutline don't have `outline-invisible-p'.
  17517. (if (fboundp 'outline-invisible-p)
  17518. (outline-invisible-p)
  17519. (get-char-property (point) 'invisible))))
  17520. (defun org-back-to-heading (&optional invisible-ok)
  17521. "Call `outline-back-to-heading', but provide a better error message."
  17522. (condition-case nil
  17523. (outline-back-to-heading invisible-ok)
  17524. (error (error "Before first headline at position %d in buffer %s"
  17525. (point) (current-buffer)))))
  17526. (defun org-beginning-of-defun ()
  17527. "Go to the beginning of the subtree, i.e. back to the heading."
  17528. (org-back-to-heading))
  17529. (defun org-end-of-defun ()
  17530. "Go to the end of the subtree."
  17531. (org-end-of-subtree nil t))
  17532. (defun org-before-first-heading-p ()
  17533. "Before first heading?"
  17534. (save-excursion
  17535. (null (re-search-backward "^\\*+ " nil t))))
  17536. (defun org-on-heading-p (&optional ignored)
  17537. (outline-on-heading-p t))
  17538. (defun org-at-heading-p (&optional ignored)
  17539. (outline-on-heading-p t))
  17540. (defun org-point-at-end-of-empty-headline ()
  17541. "If point is at the end of an empty headline, return t, else nil.
  17542. If the heading only contains a TODO keyword, it is still still considered
  17543. empty."
  17544. (and (looking-at "[ \t]*$")
  17545. (save-excursion
  17546. (beginning-of-line 1)
  17547. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17548. "\\)?[ \t]*$")))))
  17549. (defun org-at-heading-or-item-p ()
  17550. (or (org-on-heading-p) (org-at-item-p)))
  17551. (defun org-on-target-p ()
  17552. (or (org-in-regexp org-radio-target-regexp)
  17553. (org-in-regexp org-target-regexp)))
  17554. (defun org-up-heading-all (arg)
  17555. "Move to the heading line of which the present line is a subheading.
  17556. This function considers both visible and invisible heading lines.
  17557. With argument, move up ARG levels."
  17558. (if (fboundp 'outline-up-heading-all)
  17559. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17560. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17561. (defun org-up-heading-safe ()
  17562. "Move to the heading line of which the present line is a subheading.
  17563. This version will not throw an error. It will return the level of the
  17564. headline found, or nil if no higher level is found.
  17565. Also, this function will be a lot faster than `outline-up-heading',
  17566. because it relies on stars being the outline starters. This can really
  17567. make a significant difference in outlines with very many siblings."
  17568. (let (start-level re)
  17569. (org-back-to-heading t)
  17570. (setq start-level (funcall outline-level))
  17571. (if (equal start-level 1)
  17572. nil
  17573. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17574. (if (re-search-backward re nil t)
  17575. (funcall outline-level)))))
  17576. (defun org-first-sibling-p ()
  17577. "Is this heading the first child of its parents?"
  17578. (interactive)
  17579. (let ((re (concat "^" outline-regexp))
  17580. level l)
  17581. (unless (org-at-heading-p t)
  17582. (error "Not at a heading"))
  17583. (setq level (funcall outline-level))
  17584. (save-excursion
  17585. (if (not (re-search-backward re nil t))
  17586. t
  17587. (setq l (funcall outline-level))
  17588. (< l level)))))
  17589. (defun org-goto-sibling (&optional previous)
  17590. "Goto the next sibling, even if it is invisible.
  17591. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17592. when a sibling was found. When none is found, return nil and don't
  17593. move point."
  17594. (let ((fun (if previous 're-search-backward 're-search-forward))
  17595. (pos (point))
  17596. (re (concat "^" outline-regexp))
  17597. level l)
  17598. (when (condition-case nil (org-back-to-heading t) (error nil))
  17599. (setq level (funcall outline-level))
  17600. (catch 'exit
  17601. (or previous (forward-char 1))
  17602. (while (funcall fun re nil t)
  17603. (setq l (funcall outline-level))
  17604. (when (< l level) (goto-char pos) (throw 'exit nil))
  17605. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17606. (goto-char pos)
  17607. nil))))
  17608. (defun org-show-siblings ()
  17609. "Show all siblings of the current headline."
  17610. (save-excursion
  17611. (while (org-goto-sibling) (org-flag-heading nil)))
  17612. (save-excursion
  17613. (while (org-goto-sibling 'previous)
  17614. (org-flag-heading nil))))
  17615. (defun org-goto-first-child ()
  17616. "Goto the first child, even if it is invisible.
  17617. Return t when a child was found. Otherwise don't move point and
  17618. return nil."
  17619. (let (level (pos (point)) (re (concat "^" outline-regexp)))
  17620. (when (condition-case nil (org-back-to-heading t) (error nil))
  17621. (setq level (outline-level))
  17622. (forward-char 1)
  17623. (if (and (re-search-forward re nil t) (> (outline-level) level))
  17624. (progn (goto-char (match-beginning 0)) t)
  17625. (goto-char pos) nil))))
  17626. (defun org-show-hidden-entry ()
  17627. "Show an entry where even the heading is hidden."
  17628. (save-excursion
  17629. (org-show-entry)))
  17630. (defun org-flag-heading (flag &optional entry)
  17631. "Flag the current heading. FLAG non-nil means make invisible.
  17632. When ENTRY is non-nil, show the entire entry."
  17633. (save-excursion
  17634. (org-back-to-heading t)
  17635. ;; Check if we should show the entire entry
  17636. (if entry
  17637. (progn
  17638. (org-show-entry)
  17639. (save-excursion
  17640. (and (outline-next-heading)
  17641. (org-flag-heading nil))))
  17642. (outline-flag-region (max (point-min) (1- (point)))
  17643. (save-excursion (outline-end-of-heading) (point))
  17644. flag))))
  17645. (defun org-get-next-sibling ()
  17646. "Move to next heading of the same level, and return point.
  17647. If there is no such heading, return nil.
  17648. This is like outline-next-sibling, but invisible headings are ok."
  17649. (let ((level (funcall outline-level)))
  17650. (outline-next-heading)
  17651. (while (and (not (eobp)) (> (funcall outline-level) level))
  17652. (outline-next-heading))
  17653. (if (or (eobp) (< (funcall outline-level) level))
  17654. nil
  17655. (point))))
  17656. (defun org-get-last-sibling ()
  17657. "Move to previous heading of the same level, and return point.
  17658. If there is no such heading, return nil."
  17659. (let ((opoint (point))
  17660. (level (funcall outline-level)))
  17661. (outline-previous-heading)
  17662. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17663. (while (and (> (funcall outline-level) level)
  17664. (not (bobp)))
  17665. (outline-previous-heading))
  17666. (if (< (funcall outline-level) level)
  17667. nil
  17668. (point)))))
  17669. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17670. ;; This contains an exact copy of the original function, but it uses
  17671. ;; `org-back-to-heading', to make it work also in invisible
  17672. ;; trees. And is uses an invisible-OK argument.
  17673. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17674. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17675. ;; with many children and grandchildren etc, this can be much faster
  17676. ;; than the outline version.
  17677. (org-back-to-heading invisible-OK)
  17678. (let ((first t)
  17679. (level (funcall outline-level)))
  17680. (if (and (org-mode-p) (< level 1000))
  17681. ;; A true heading (not a plain list item), in Org-mode
  17682. ;; This means we can easily find the end by looking
  17683. ;; only for the right number of stars. Using a regexp to do
  17684. ;; this is so much faster than using a Lisp loop.
  17685. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17686. (forward-char 1)
  17687. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17688. ;; something else, do it the slow way
  17689. (while (and (not (eobp))
  17690. (or first (> (funcall outline-level) level)))
  17691. (setq first nil)
  17692. (outline-next-heading)))
  17693. (unless to-heading
  17694. (if (memq (preceding-char) '(?\n ?\^M))
  17695. (progn
  17696. ;; Go to end of line before heading
  17697. (forward-char -1)
  17698. (if (memq (preceding-char) '(?\n ?\^M))
  17699. ;; leave blank line before heading
  17700. (forward-char -1))))))
  17701. (point))
  17702. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17703. "Use Org version in org-mode, for dramatic speed-up."
  17704. (if (eq major-mode 'org-mode)
  17705. (progn
  17706. (org-end-of-subtree nil t)
  17707. (unless (eobp) (backward-char 1)))
  17708. ad-do-it))
  17709. (defun org-forward-same-level (arg &optional invisible-ok)
  17710. "Move forward to the arg'th subheading at same level as this one.
  17711. Stop at the first and last subheadings of a superior heading.
  17712. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  17713. it wil also look at invisible ones."
  17714. (interactive "p")
  17715. (org-back-to-heading invisible-ok)
  17716. (org-on-heading-p)
  17717. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17718. (re (format "^\\*\\{1,%d\\} " level))
  17719. l)
  17720. (forward-char 1)
  17721. (while (> arg 0)
  17722. (while (and (re-search-forward re nil 'move)
  17723. (setq l (- (match-end 0) (match-beginning 0) 1))
  17724. (= l level)
  17725. (not invisible-ok)
  17726. (progn (backward-char 1) (org-invisible-p)))
  17727. (if (< l level) (setq arg 1)))
  17728. (setq arg (1- arg)))
  17729. (beginning-of-line 1)))
  17730. (defun org-backward-same-level (arg &optional invisible-ok)
  17731. "Move backward to the arg'th subheading at same level as this one.
  17732. Stop at the first and last subheadings of a superior heading."
  17733. (interactive "p")
  17734. (org-back-to-heading)
  17735. (org-on-heading-p)
  17736. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17737. (re (format "^\\*\\{1,%d\\} " level))
  17738. l)
  17739. (while (> arg 0)
  17740. (while (and (re-search-backward re nil 'move)
  17741. (setq l (- (match-end 0) (match-beginning 0) 1))
  17742. (= l level)
  17743. (not invisible-ok)
  17744. (org-invisible-p))
  17745. (if (< l level) (setq arg 1)))
  17746. (setq arg (1- arg)))))
  17747. (defun org-show-subtree ()
  17748. "Show everything after this heading at deeper levels."
  17749. (outline-flag-region
  17750. (point)
  17751. (save-excursion
  17752. (org-end-of-subtree t t))
  17753. nil))
  17754. (defun org-show-entry ()
  17755. "Show the body directly following this heading.
  17756. Show the heading too, if it is currently invisible."
  17757. (interactive)
  17758. (save-excursion
  17759. (condition-case nil
  17760. (progn
  17761. (org-back-to-heading t)
  17762. (outline-flag-region
  17763. (max (point-min) (1- (point)))
  17764. (save-excursion
  17765. (if (re-search-forward
  17766. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17767. (match-beginning 1)
  17768. (point-max)))
  17769. nil)
  17770. (org-cycle-hide-drawers 'children))
  17771. (error nil))))
  17772. (defun org-make-options-regexp (kwds &optional extra)
  17773. "Make a regular expression for keyword lines."
  17774. (concat
  17775. "^"
  17776. "#?[ \t]*\\+\\("
  17777. (mapconcat 'regexp-quote kwds "\\|")
  17778. (if extra (concat "\\|" extra))
  17779. "\\):[ \t]*"
  17780. "\\(.*\\)"))
  17781. ;; Make isearch reveal the necessary context
  17782. (defun org-isearch-end ()
  17783. "Reveal context after isearch exits."
  17784. (when isearch-success ; only if search was successful
  17785. (if (featurep 'xemacs)
  17786. ;; Under XEmacs, the hook is run in the correct place,
  17787. ;; we directly show the context.
  17788. (org-show-context 'isearch)
  17789. ;; In Emacs the hook runs *before* restoring the overlays.
  17790. ;; So we have to use a one-time post-command-hook to do this.
  17791. ;; (Emacs 22 has a special variable, see function `org-mode')
  17792. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17793. isearch-mode-end-hook-quit)
  17794. ;; Only when the isearch was not quitted.
  17795. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17796. 'append 'local)))))
  17797. (defun org-isearch-post-command ()
  17798. "Remove self from hook, and show context."
  17799. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17800. (org-show-context 'isearch))
  17801. ;;;; Integration with and fixes for other packages
  17802. ;;; Imenu support
  17803. (defvar org-imenu-markers nil
  17804. "All markers currently used by Imenu.")
  17805. (make-variable-buffer-local 'org-imenu-markers)
  17806. (defun org-imenu-new-marker (&optional pos)
  17807. "Return a new marker for use by Imenu, and remember the marker."
  17808. (let ((m (make-marker)))
  17809. (move-marker m (or pos (point)))
  17810. (push m org-imenu-markers)
  17811. m))
  17812. (defun org-imenu-get-tree ()
  17813. "Produce the index for Imenu."
  17814. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17815. (setq org-imenu-markers nil)
  17816. (let* ((n org-imenu-depth)
  17817. (re (concat "^" outline-regexp))
  17818. (subs (make-vector (1+ n) nil))
  17819. (last-level 0)
  17820. m level head)
  17821. (save-excursion
  17822. (save-restriction
  17823. (widen)
  17824. (goto-char (point-max))
  17825. (while (re-search-backward re nil t)
  17826. (setq level (org-reduced-level (funcall outline-level)))
  17827. (when (<= level n)
  17828. (looking-at org-complex-heading-regexp)
  17829. (setq head (org-link-display-format
  17830. (org-match-string-no-properties 4))
  17831. m (org-imenu-new-marker))
  17832. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17833. (if (>= level last-level)
  17834. (push (cons head m) (aref subs level))
  17835. (push (cons head (aref subs (1+ level))) (aref subs level))
  17836. (loop for i from (1+ level) to n do (aset subs i nil)))
  17837. (setq last-level level)))))
  17838. (aref subs 1)))
  17839. (eval-after-load "imenu"
  17840. '(progn
  17841. (add-hook 'imenu-after-jump-hook
  17842. (lambda ()
  17843. (if (eq major-mode 'org-mode)
  17844. (org-show-context 'org-goto))))))
  17845. (defun org-link-display-format (link)
  17846. "Replace a link with either the description, or the link target
  17847. if no description is present"
  17848. (save-match-data
  17849. (if (string-match org-bracket-link-analytic-regexp link)
  17850. (replace-match (if (match-end 5)
  17851. (match-string 5 link)
  17852. (concat (match-string 1 link)
  17853. (match-string 3 link)))
  17854. nil t link)
  17855. link)))
  17856. ;; Speedbar support
  17857. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17858. "Overlay marking the agenda restriction line in speedbar.")
  17859. (overlay-put org-speedbar-restriction-lock-overlay
  17860. 'face 'org-agenda-restriction-lock)
  17861. (overlay-put org-speedbar-restriction-lock-overlay
  17862. 'help-echo "Agendas are currently limited to this item.")
  17863. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17864. (defun org-speedbar-set-agenda-restriction ()
  17865. "Restrict future agenda commands to the location at point in speedbar.
  17866. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17867. (interactive)
  17868. (require 'org-agenda)
  17869. (let (p m tp np dir txt)
  17870. (cond
  17871. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17872. 'org-imenu t))
  17873. (setq m (get-text-property p 'org-imenu-marker))
  17874. (with-current-buffer (marker-buffer m)
  17875. (goto-char m)
  17876. (org-agenda-set-restriction-lock 'subtree)))
  17877. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17878. 'speedbar-function 'speedbar-find-file))
  17879. (setq tp (previous-single-property-change
  17880. (1+ p) 'speedbar-function)
  17881. np (next-single-property-change
  17882. tp 'speedbar-function)
  17883. dir (speedbar-line-directory)
  17884. txt (buffer-substring-no-properties (or tp (point-min))
  17885. (or np (point-max))))
  17886. (with-current-buffer (find-file-noselect
  17887. (let ((default-directory dir))
  17888. (expand-file-name txt)))
  17889. (unless (org-mode-p)
  17890. (error "Cannot restrict to non-Org-mode file"))
  17891. (org-agenda-set-restriction-lock 'file)))
  17892. (t (error "Don't know how to restrict Org-mode's agenda")))
  17893. (move-overlay org-speedbar-restriction-lock-overlay
  17894. (point-at-bol) (point-at-eol))
  17895. (setq current-prefix-arg nil)
  17896. (org-agenda-maybe-redo)))
  17897. (eval-after-load "speedbar"
  17898. '(progn
  17899. (speedbar-add-supported-extension ".org")
  17900. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17901. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17902. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17903. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17904. (add-hook 'speedbar-visiting-tag-hook
  17905. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17906. ;;; Fixes and Hacks for problems with other packages
  17907. ;; Make flyspell not check words in links, to not mess up our keymap
  17908. (defun org-mode-flyspell-verify ()
  17909. "Don't let flyspell put overlays at active buttons."
  17910. (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
  17911. (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
  17912. (defun org-remove-flyspell-overlays-in (beg end)
  17913. "Remove flyspell overlays in region."
  17914. (and (org-bound-and-true-p flyspell-mode)
  17915. (fboundp 'flyspell-delete-region-overlays)
  17916. (flyspell-delete-region-overlays beg end))
  17917. (add-text-properties beg end '(org-no-flyspell t)))
  17918. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17919. (eval-after-load "bookmark"
  17920. '(if (boundp 'bookmark-after-jump-hook)
  17921. ;; We can use the hook
  17922. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17923. ;; Hook not available, use advice
  17924. (defadvice bookmark-jump (after org-make-visible activate)
  17925. "Make the position visible."
  17926. (org-bookmark-jump-unhide))))
  17927. ;; Make sure saveplace shows the location if it was hidden
  17928. (eval-after-load "saveplace"
  17929. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17930. "Make the position visible."
  17931. (org-bookmark-jump-unhide)))
  17932. ;; Make sure ecb shows the location if it was hidden
  17933. (eval-after-load "ecb"
  17934. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17935. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17936. (if (eq major-mode 'org-mode)
  17937. (org-show-context))))
  17938. (defun org-bookmark-jump-unhide ()
  17939. "Unhide the current position, to show the bookmark location."
  17940. (and (org-mode-p)
  17941. (or (org-invisible-p)
  17942. (save-excursion (goto-char (max (point-min) (1- (point))))
  17943. (org-invisible-p)))
  17944. (org-show-context 'bookmark-jump)))
  17945. ;; Make session.el ignore our circular variable
  17946. (eval-after-load "session"
  17947. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17948. ;;;; Experimental code
  17949. (defun org-closed-in-range ()
  17950. "Sparse tree of items closed in a certain time range.
  17951. Still experimental, may disappear in the future."
  17952. (interactive)
  17953. ;; Get the time interval from the user.
  17954. (let* ((time1 (org-float-time
  17955. (org-read-date nil 'to-time nil "Starting date: ")))
  17956. (time2 (org-float-time
  17957. (org-read-date nil 'to-time nil "End date:")))
  17958. ;; callback function
  17959. (callback (lambda ()
  17960. (let ((time
  17961. (org-float-time
  17962. (apply 'encode-time
  17963. (org-parse-time-string
  17964. (match-string 1))))))
  17965. ;; check if time in interval
  17966. (and (>= time time1) (<= time time2))))))
  17967. ;; make tree, check each match with the callback
  17968. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17969. ;;;; Finish up
  17970. (provide 'org)
  17971. (run-hooks 'org-load-hook)
  17972. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17973. ;;; org.el ends here