org.el 852 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (let ((load-suffixes (list ".el")))
  74. (load "org-loaddefs" 'noerror nil nil 'mustsuffix))
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro org-with-limited-levels.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches an headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (when (fboundp 'defvaralias)
  93. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-at-clock-log-p "org-clock" ())
  106. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  107. (declare-function org-clock-timestamps-up "org-clock" ())
  108. (declare-function org-clock-timestamps-down "org-clock" ())
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function orgtbl-mode "org-table" (&optional arg))
  111. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  112. (declare-function org-beamer-mode "org-beamer" ())
  113. (declare-function org-table-edit-field "org-table" (arg))
  114. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (autoload 'org-element-at-point "org-element")
  124. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  125. (declare-function org-element-type "org-element" (element))
  126. (declare-function org-element-context "org-element" ())
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-property "org-element" (property element))
  129. (declare-function org-element-paragraph-parser "org-element" (limit))
  130. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  131. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  132. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  133. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  134. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  135. ;; load languages based on value of `org-babel-load-languages'
  136. (defvar org-babel-load-languages)
  137. ;;;###autoload
  138. (defun org-babel-do-load-languages (sym value)
  139. "Load the languages defined in `org-babel-load-languages'."
  140. (set-default sym value)
  141. (mapc (lambda (pair)
  142. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  143. (if active
  144. (progn
  145. (require (intern (concat "ob-" lang))))
  146. (progn
  147. (funcall 'fmakunbound
  148. (intern (concat "org-babel-execute:" lang)))
  149. (funcall 'fmakunbound
  150. (intern (concat "org-babel-expand-body:" lang)))))))
  151. org-babel-load-languages))
  152. (defcustom org-babel-load-languages '((emacs-lisp . t))
  153. "Languages which can be evaluated in Org-mode buffers.
  154. This list can be used to load support for any of the languages
  155. below, note that each language will depend on a different set of
  156. system executables and/or Emacs modes. When a language is
  157. \"loaded\", then code blocks in that language can be evaluated
  158. with `org-babel-execute-src-block' bound by default to C-c
  159. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  160. be set to remove code block evaluation from the C-c C-c
  161. keybinding. By default only Emacs Lisp (which has no
  162. requirements) is loaded."
  163. :group 'org-babel
  164. :set 'org-babel-do-load-languages
  165. :version "24.1"
  166. :type '(alist :tag "Babel Languages"
  167. :key-type
  168. (choice
  169. (const :tag "Awk" awk)
  170. (const :tag "C" C)
  171. (const :tag "R" R)
  172. (const :tag "Asymptote" asymptote)
  173. (const :tag "Calc" calc)
  174. (const :tag "Clojure" clojure)
  175. (const :tag "CSS" css)
  176. (const :tag "Ditaa" ditaa)
  177. (const :tag "Dot" dot)
  178. (const :tag "Emacs Lisp" emacs-lisp)
  179. (const :tag "Fortran" fortran)
  180. (const :tag "Gnuplot" gnuplot)
  181. (const :tag "Haskell" haskell)
  182. (const :tag "IO" io)
  183. (const :tag "Java" java)
  184. (const :tag "Javascript" js)
  185. (const :tag "LaTeX" latex)
  186. (const :tag "Ledger" ledger)
  187. (const :tag "Lilypond" lilypond)
  188. (const :tag "Lisp" lisp)
  189. (const :tag "Makefile" makefile)
  190. (const :tag "Maxima" maxima)
  191. (const :tag "Matlab" matlab)
  192. (const :tag "Mscgen" mscgen)
  193. (const :tag "Ocaml" ocaml)
  194. (const :tag "Octave" octave)
  195. (const :tag "Org" org)
  196. (const :tag "Perl" perl)
  197. (const :tag "Pico Lisp" picolisp)
  198. (const :tag "PlantUML" plantuml)
  199. (const :tag "Python" python)
  200. (const :tag "Ruby" ruby)
  201. (const :tag "Sass" sass)
  202. (const :tag "Scala" scala)
  203. (const :tag "Scheme" scheme)
  204. (const :tag "Screen" screen)
  205. (const :tag "Shell Script" sh)
  206. (const :tag "Shen" shen)
  207. (const :tag "Sql" sql)
  208. (const :tag "Sqlite" sqlite))
  209. :value-type (boolean :tag "Activate" :value t)))
  210. ;;;; Customization variables
  211. (defcustom org-clone-delete-id nil
  212. "Remove ID property of clones of a subtree.
  213. When non-nil, clones of a subtree don't inherit the ID property.
  214. Otherwise they inherit the ID property with a new unique
  215. identifier."
  216. :type 'boolean
  217. :version "24.1"
  218. :group 'org-id)
  219. ;;; Version
  220. (require 'org-compat)
  221. (org-check-version)
  222. ;;;###autoload
  223. (defun org-version (&optional here full message)
  224. "Show the org-mode version in the echo area.
  225. With prefix argument HERE, insert it at point.
  226. When FULL is non-nil, use a verbose version string.
  227. When MESSAGE is non-nil, display a message with the version."
  228. (interactive "P")
  229. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  230. (save-load-suffixes load-suffixes)
  231. (load-suffixes (list ".el"))
  232. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  233. (org-trash (or
  234. (and (fboundp 'org-release) (fboundp 'org-git-version))
  235. (load (concat org-dir "org-version")
  236. 'noerror 'nomessage nil 'mustsuffix)))
  237. (load-suffixes save-load-suffixes)
  238. (org-version (org-release))
  239. (git-version (org-git-version))
  240. (version (format "Org-mode version %s (%s @ %s)"
  241. org-version
  242. git-version
  243. (if org-install-dir
  244. (if (string= org-dir org-install-dir)
  245. org-install-dir
  246. (concat "mixed installation! " org-install-dir " and " org-dir))
  247. "org-loaddefs.el can not be found!")))
  248. (_version (if full version org-version)))
  249. (if (org-called-interactively-p 'interactive)
  250. (if here
  251. (insert version)
  252. (message version))
  253. (if message (message _version))
  254. _version)))
  255. (defconst org-version (org-version))
  256. ;;; Compatibility constants
  257. ;;; The custom variables
  258. (defgroup org nil
  259. "Outline-based notes management and organizer."
  260. :tag "Org"
  261. :group 'outlines
  262. :group 'calendar)
  263. (defcustom org-mode-hook nil
  264. "Mode hook for Org-mode, run after the mode was turned on."
  265. :group 'org
  266. :type 'hook)
  267. (defcustom org-load-hook nil
  268. "Hook that is run after org.el has been loaded."
  269. :group 'org
  270. :type 'hook)
  271. (defcustom org-log-buffer-setup-hook nil
  272. "Hook that is run after an Org log buffer is created."
  273. :group 'org
  274. :version "24.1"
  275. :type 'hook)
  276. (defvar org-modules) ; defined below
  277. (defvar org-modules-loaded nil
  278. "Have the modules been loaded already?")
  279. (defun org-load-modules-maybe (&optional force)
  280. "Load all extensions listed in `org-modules'."
  281. (when (or force (not org-modules-loaded))
  282. (mapc (lambda (ext)
  283. (condition-case nil (require ext)
  284. (error (message "Problems while trying to load feature `%s'" ext))))
  285. org-modules)
  286. (setq org-modules-loaded t)))
  287. (defun org-set-modules (var value)
  288. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  289. (set var value)
  290. (when (featurep 'org)
  291. (org-load-modules-maybe 'force)))
  292. (when (org-bound-and-true-p org-modules)
  293. (let ((a (member 'org-infojs org-modules)))
  294. (and a (setcar a 'org-jsinfo))))
  295. (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)
  296. "Modules that should always be loaded together with org.el.
  297. If a description starts with <C>, the file is not part of Emacs
  298. and loading it will require that you have downloaded and properly installed
  299. the org-mode distribution.
  300. You can also use this system to load external packages (i.e. neither Org
  301. core modules, nor modules from the CONTRIB directory). Just add symbols
  302. to the end of the list. If the package is called org-xyz.el, then you need
  303. to add the symbol `xyz', and the package must have a call to
  304. (provide 'org-xyz)"
  305. :group 'org
  306. :set 'org-set-modules
  307. :type
  308. '(set :greedy t
  309. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  310. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  311. (const :tag " crypt: Encryption of subtrees" org-crypt)
  312. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  313. (const :tag " docview: Links to doc-view buffers" org-docview)
  314. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  315. (const :tag " id: Global IDs for identifying entries" org-id)
  316. (const :tag " info: Links to Info nodes" org-info)
  317. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  318. (const :tag " habit: Track your consistency with habits" org-habit)
  319. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  320. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  321. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  322. (const :tag " mew Links to Mew folders/messages" org-mew)
  323. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  324. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  325. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  326. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  327. (const :tag " vm: Links to VM folders/messages" org-vm)
  328. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  329. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  330. (const :tag " mouse: Additional mouse support" org-mouse)
  331. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  332. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  333. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  334. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  335. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  336. (const :tag "C collector: Collect properties into tables" org-collector)
  337. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  338. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  339. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  340. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  341. (const :tag "C eval: Include command output as text" org-eval)
  342. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  343. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  344. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  345. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  346. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  347. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  348. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  349. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  350. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  351. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  352. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  353. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  354. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  355. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  356. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  357. (const :tag "C registry: A registry for Org-mode links" org-registry)
  358. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  359. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  360. (const :tag "C secretary: Team management with org-mode" org-secretary)
  361. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  362. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  363. (const :tag "C track: Keep up with Org-mode development" org-track)
  364. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  365. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  366. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  367. (defcustom org-support-shift-select nil
  368. "Non-nil means make shift-cursor commands select text when possible.
  369. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  370. start selecting a region, or enlarge regions started in this way.
  371. In Org-mode, in special contexts, these same keys are used for
  372. other purposes, important enough to compete with shift selection.
  373. Org tries to balance these needs by supporting `shift-select-mode'
  374. outside these special contexts, under control of this variable.
  375. The default of this variable is nil, to avoid confusing behavior. Shifted
  376. cursor keys will then execute Org commands in the following contexts:
  377. - on a headline, changing TODO state (left/right) and priority (up/down)
  378. - on a time stamp, changing the time
  379. - in a plain list item, changing the bullet type
  380. - in a property definition line, switching between allowed values
  381. - in the BEGIN line of a clock table (changing the time block).
  382. Outside these contexts, the commands will throw an error.
  383. When this variable is t and the cursor is not in a special
  384. context, Org-mode will support shift-selection for making and
  385. enlarging regions. To make this more effective, the bullet
  386. cycling will no longer happen anywhere in an item line, but only
  387. if the cursor is exactly on the bullet.
  388. If you set this variable to the symbol `always', then the keys
  389. will not be special in headlines, property lines, and item lines,
  390. to make shift selection work there as well. If this is what you
  391. want, you can use the following alternative commands: `C-c C-t'
  392. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  393. can be used to switch TODO sets, `C-c -' to cycle item bullet
  394. types, and properties can be edited by hand or in column view.
  395. However, when the cursor is on a timestamp, shift-cursor commands
  396. will still edit the time stamp - this is just too good to give up.
  397. XEmacs user should have this variable set to nil, because
  398. `shift-select-mode' is in Emacs 23 or later only."
  399. :group 'org
  400. :type '(choice
  401. (const :tag "Never" nil)
  402. (const :tag "When outside special context" t)
  403. (const :tag "Everywhere except timestamps" always)))
  404. (defcustom org-loop-over-headlines-in-active-region nil
  405. "Shall some commands act upon headlines in the active region?
  406. When set to `t', some commands will be performed in all headlines
  407. within the active region.
  408. When set to `start-level', some commands will be performed in all
  409. headlines within the active region, provided that these headlines
  410. are of the same level than the first one.
  411. When set to a string, those commands will be performed on the
  412. matching headlines within the active region. Such string must be
  413. a tags/property/todo match as it is used in the agenda tags view.
  414. The list of commands is: `org-schedule', `org-deadline',
  415. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  416. `org-archive-to-archive-sibling'. The archiving commands skip
  417. already archived entries."
  418. :type '(choice (const :tag "Don't loop" nil)
  419. (const :tag "All headlines in active region" t)
  420. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  421. (string :tag "Tags/Property/Todo matcher"))
  422. :version "24.1"
  423. :group 'org-todo
  424. :group 'org-archive)
  425. (defgroup org-startup nil
  426. "Options concerning startup of Org-mode."
  427. :tag "Org Startup"
  428. :group 'org)
  429. (defcustom org-startup-folded t
  430. "Non-nil means entering Org-mode will switch to OVERVIEW.
  431. This can also be configured on a per-file basis by adding one of
  432. the following lines anywhere in the buffer:
  433. #+STARTUP: fold (or `overview', this is equivalent)
  434. #+STARTUP: nofold (or `showall', this is equivalent)
  435. #+STARTUP: content
  436. #+STARTUP: showeverything"
  437. :group 'org-startup
  438. :type '(choice
  439. (const :tag "nofold: show all" nil)
  440. (const :tag "fold: overview" t)
  441. (const :tag "content: all headlines" content)
  442. (const :tag "show everything, even drawers" showeverything)))
  443. (defcustom org-startup-truncated t
  444. "Non-nil means entering Org-mode will set `truncate-lines'.
  445. This is useful since some lines containing links can be very long and
  446. uninteresting. Also tables look terrible when wrapped."
  447. :group 'org-startup
  448. :type 'boolean)
  449. (defcustom org-startup-indented nil
  450. "Non-nil means turn on `org-indent-mode' on startup.
  451. This can also be configured on a per-file basis by adding one of
  452. the following lines anywhere in the buffer:
  453. #+STARTUP: indent
  454. #+STARTUP: noindent"
  455. :group 'org-structure
  456. :type '(choice
  457. (const :tag "Not" nil)
  458. (const :tag "Globally (slow on startup in large files)" t)))
  459. (defcustom org-use-sub-superscripts t
  460. "Non-nil means interpret \"_\" and \"^\" for export.
  461. When this option is turned on, you can use TeX-like syntax for sub- and
  462. superscripts. Several characters after \"_\" or \"^\" will be
  463. considered as a single item - so grouping with {} is normally not
  464. needed. For example, the following things will be parsed as single
  465. sub- or superscripts.
  466. 10^24 or 10^tau several digits will be considered 1 item.
  467. 10^-12 or 10^-tau a leading sign with digits or a word
  468. x^2-y^3 will be read as x^2 - y^3, because items are
  469. terminated by almost any nonword/nondigit char.
  470. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  471. Still, ambiguity is possible - so when in doubt use {} to enclose the
  472. sub/superscript. If you set this variable to the symbol `{}',
  473. the braces are *required* in order to trigger interpretations as
  474. sub/superscript. This can be helpful in documents that need \"_\"
  475. frequently in plain text.
  476. Not all export backends support this, but HTML does.
  477. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  478. :group 'org-startup
  479. :group 'org-export-translation
  480. :version "24.1"
  481. :type '(choice
  482. (const :tag "Always interpret" t)
  483. (const :tag "Only with braces" {})
  484. (const :tag "Never interpret" nil)))
  485. (if (fboundp 'defvaralias)
  486. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  487. (defcustom org-startup-with-beamer-mode nil
  488. "Non-nil means turn on `org-beamer-mode' on startup.
  489. This can also be configured on a per-file basis by adding one of
  490. the following lines anywhere in the buffer:
  491. #+STARTUP: beamer"
  492. :group 'org-startup
  493. :version "24.1"
  494. :type 'boolean)
  495. (defcustom org-startup-align-all-tables nil
  496. "Non-nil means align all tables when visiting a file.
  497. This is useful when the column width in tables is forced with <N> cookies
  498. in table fields. Such tables will look correct only after the first re-align.
  499. This can also be configured on a per-file basis by adding one of
  500. the following lines anywhere in the buffer:
  501. #+STARTUP: align
  502. #+STARTUP: noalign"
  503. :group 'org-startup
  504. :type 'boolean)
  505. (defcustom org-startup-with-inline-images nil
  506. "Non-nil means show inline images when loading a new Org file.
  507. This can also be configured on a per-file basis by adding one of
  508. the following lines anywhere in the buffer:
  509. #+STARTUP: inlineimages
  510. #+STARTUP: noinlineimages"
  511. :group 'org-startup
  512. :version "24.1"
  513. :type 'boolean)
  514. (defcustom org-insert-mode-line-in-empty-file nil
  515. "Non-nil means insert the first line setting Org-mode in empty files.
  516. When the function `org-mode' is called interactively in an empty file, this
  517. normally means that the file name does not automatically trigger Org-mode.
  518. To ensure that the file will always be in Org-mode in the future, a
  519. line enforcing Org-mode will be inserted into the buffer, if this option
  520. has been set."
  521. :group 'org-startup
  522. :type 'boolean)
  523. (defcustom org-replace-disputed-keys nil
  524. "Non-nil means use alternative key bindings for some keys.
  525. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  526. These keys are also used by other packages like shift-selection-mode'
  527. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  528. If you want to use Org-mode together with one of these other modes,
  529. or more generally if you would like to move some Org-mode commands to
  530. other keys, set this variable and configure the keys with the variable
  531. `org-disputed-keys'.
  532. This option is only relevant at load-time of Org-mode, and must be set
  533. *before* org.el is loaded. Changing it requires a restart of Emacs to
  534. become effective."
  535. :group 'org-startup
  536. :type 'boolean)
  537. (defcustom org-use-extra-keys nil
  538. "Non-nil means use extra key sequence definitions for certain commands.
  539. This happens automatically if you run XEmacs or if `window-system'
  540. is nil. This variable lets you do the same manually. You must
  541. set it before loading org.
  542. Example: on Carbon Emacs 22 running graphically, with an external
  543. keyboard on a Powerbook, the default way of setting M-left might
  544. not work for either Alt or ESC. Setting this variable will make
  545. it work for ESC."
  546. :group 'org-startup
  547. :type 'boolean)
  548. (if (fboundp 'defvaralias)
  549. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  550. (defcustom org-disputed-keys
  551. '(([(shift up)] . [(meta p)])
  552. ([(shift down)] . [(meta n)])
  553. ([(shift left)] . [(meta -)])
  554. ([(shift right)] . [(meta +)])
  555. ([(control shift right)] . [(meta shift +)])
  556. ([(control shift left)] . [(meta shift -)]))
  557. "Keys for which Org-mode and other modes compete.
  558. This is an alist, cars are the default keys, second element specifies
  559. the alternative to use when `org-replace-disputed-keys' is t.
  560. Keys can be specified in any syntax supported by `define-key'.
  561. The value of this option takes effect only at Org-mode's startup,
  562. therefore you'll have to restart Emacs to apply it after changing."
  563. :group 'org-startup
  564. :type 'alist)
  565. (defun org-key (key)
  566. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  567. Or return the original if not disputed.
  568. Also apply the translations defined in `org-xemacs-key-equivalents'."
  569. (when org-replace-disputed-keys
  570. (let* ((nkey (key-description key))
  571. (x (org-find-if (lambda (x)
  572. (equal (key-description (car x)) nkey))
  573. org-disputed-keys)))
  574. (setq key (if x (cdr x) key))))
  575. (when (featurep 'xemacs)
  576. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  577. key)
  578. (defun org-find-if (predicate seq)
  579. (catch 'exit
  580. (while seq
  581. (if (funcall predicate (car seq))
  582. (throw 'exit (car seq))
  583. (pop seq)))))
  584. (defun org-defkey (keymap key def)
  585. "Define a key, possibly translated, as returned by `org-key'."
  586. (define-key keymap (org-key key) def))
  587. (defcustom org-ellipsis nil
  588. "The ellipsis to use in the Org-mode outline.
  589. When nil, just use the standard three dots. When a string, use that instead,
  590. When a face, use the standard 3 dots, but with the specified face.
  591. The change affects only Org-mode (which will then use its own display table).
  592. Changing this requires executing `M-x org-mode' in a buffer to become
  593. effective."
  594. :group 'org-startup
  595. :type '(choice (const :tag "Default" nil)
  596. (face :tag "Face" :value org-warning)
  597. (string :tag "String" :value "...#")))
  598. (defvar org-display-table nil
  599. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  600. (defgroup org-keywords nil
  601. "Keywords in Org-mode."
  602. :tag "Org Keywords"
  603. :group 'org)
  604. (defcustom org-deadline-string "DEADLINE:"
  605. "String to mark deadline entries.
  606. A deadline is this string, followed by a time stamp. Should be a word,
  607. terminated by a colon. You can insert a schedule keyword and
  608. a timestamp with \\[org-deadline].
  609. Changes become only effective after restarting Emacs."
  610. :group 'org-keywords
  611. :type 'string)
  612. (defcustom org-scheduled-string "SCHEDULED:"
  613. "String to mark scheduled TODO entries.
  614. A schedule is this string, followed by a time stamp. Should be a word,
  615. terminated by a colon. You can insert a schedule keyword and
  616. a timestamp with \\[org-schedule].
  617. Changes become only effective after restarting Emacs."
  618. :group 'org-keywords
  619. :type 'string)
  620. (defcustom org-closed-string "CLOSED:"
  621. "String used as the prefix for timestamps logging closing a TODO entry."
  622. :group 'org-keywords
  623. :type 'string)
  624. (defcustom org-clock-string "CLOCK:"
  625. "String used as prefix for timestamps clocking work hours on an item."
  626. :group 'org-keywords
  627. :type 'string)
  628. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  629. org-scheduled-string "\\|"
  630. org-deadline-string "\\|"
  631. org-closed-string "\\|"
  632. org-clock-string "\\)")
  633. "Matches a line with planning or clock info.")
  634. (defcustom org-comment-string "COMMENT"
  635. "Entries starting with this keyword will never be exported.
  636. An entry can be toggled between COMMENT and normal with
  637. \\[org-toggle-comment].
  638. Changes become only effective after restarting Emacs."
  639. :group 'org-keywords
  640. :type 'string)
  641. (defcustom org-quote-string "QUOTE"
  642. "Entries starting with this keyword will be exported in fixed-width font.
  643. Quoting applies only to the text in the entry following the headline, and does
  644. not extend beyond the next headline, even if that is lower level.
  645. An entry can be toggled between QUOTE and normal with
  646. \\[org-toggle-fixed-width-section]."
  647. :group 'org-keywords
  648. :type 'string)
  649. (defconst org-repeat-re
  650. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  651. "Regular expression for specifying repeated events.
  652. After a match, group 1 contains the repeat expression.")
  653. (defgroup org-structure nil
  654. "Options concerning the general structure of Org-mode files."
  655. :tag "Org Structure"
  656. :group 'org)
  657. (defgroup org-reveal-location nil
  658. "Options about how to make context of a location visible."
  659. :tag "Org Reveal Location"
  660. :group 'org-structure)
  661. (defconst org-context-choice
  662. '(choice
  663. (const :tag "Always" t)
  664. (const :tag "Never" nil)
  665. (repeat :greedy t :tag "Individual contexts"
  666. (cons
  667. (choice :tag "Context"
  668. (const agenda)
  669. (const org-goto)
  670. (const occur-tree)
  671. (const tags-tree)
  672. (const link-search)
  673. (const mark-goto)
  674. (const bookmark-jump)
  675. (const isearch)
  676. (const default))
  677. (boolean))))
  678. "Contexts for the reveal options.")
  679. (defcustom org-show-hierarchy-above '((default . t))
  680. "Non-nil means show full hierarchy when revealing a location.
  681. Org-mode often shows locations in an org-mode file which might have
  682. been invisible before. When this is set, the hierarchy of headings
  683. above the exposed location is shown.
  684. Turning this off for example for sparse trees makes them very compact.
  685. Instead of t, this can also be an alist specifying this option for different
  686. contexts. Valid contexts are
  687. agenda when exposing an entry from the agenda
  688. org-goto when using the command `org-goto' on key C-c C-j
  689. occur-tree when using the command `org-occur' on key C-c /
  690. tags-tree when constructing a sparse tree based on tags matches
  691. link-search when exposing search matches associated with a link
  692. mark-goto when exposing the jump goal of a mark
  693. bookmark-jump when exposing a bookmark location
  694. isearch when exiting from an incremental search
  695. default default for all contexts not set explicitly"
  696. :group 'org-reveal-location
  697. :type org-context-choice)
  698. (defcustom org-show-following-heading '((default . nil))
  699. "Non-nil means show following heading when revealing a location.
  700. Org-mode often shows locations in an org-mode file which might have
  701. been invisible before. When this is set, the heading following the
  702. match is shown.
  703. Turning this off for example for sparse trees makes them very compact,
  704. but makes it harder to edit the location of the match. In such a case,
  705. use the command \\[org-reveal] to show more context.
  706. Instead of t, this can also be an alist specifying this option for different
  707. contexts. See `org-show-hierarchy-above' for valid contexts."
  708. :group 'org-reveal-location
  709. :type org-context-choice)
  710. (defcustom org-show-siblings '((default . nil) (isearch t))
  711. "Non-nil means show all sibling heading when revealing a location.
  712. Org-mode often shows locations in an org-mode file which might have
  713. been invisible before. When this is set, the sibling of the current entry
  714. heading are all made visible. If `org-show-hierarchy-above' is t,
  715. the same happens on each level of the hierarchy above the current entry.
  716. By default this is on for the isearch context, off for all other contexts.
  717. Turning this off for example for sparse trees makes them very compact,
  718. but makes it harder to edit the location of the match. In such a case,
  719. use the command \\[org-reveal] to show more context.
  720. Instead of t, this can also be an alist specifying this option for different
  721. contexts. See `org-show-hierarchy-above' for valid contexts."
  722. :group 'org-reveal-location
  723. :type org-context-choice)
  724. (defcustom org-show-entry-below '((default . nil))
  725. "Non-nil means show the entry below a headline when revealing a location.
  726. Org-mode often shows locations in an org-mode file which might have
  727. been invisible before. When this is set, the text below the headline that is
  728. exposed is also shown.
  729. By default this is off for all contexts.
  730. Instead of t, this can also be an alist specifying this option for different
  731. contexts. See `org-show-hierarchy-above' for valid contexts."
  732. :group 'org-reveal-location
  733. :type org-context-choice)
  734. (defcustom org-indirect-buffer-display 'other-window
  735. "How should indirect tree buffers be displayed?
  736. This applies to indirect buffers created with the commands
  737. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  738. Valid values are:
  739. current-window Display in the current window
  740. other-window Just display in another window.
  741. dedicated-frame Create one new frame, and re-use it each time.
  742. new-frame Make a new frame each time. Note that in this case
  743. previously-made indirect buffers are kept, and you need to
  744. kill these buffers yourself."
  745. :group 'org-structure
  746. :group 'org-agenda-windows
  747. :type '(choice
  748. (const :tag "In current window" current-window)
  749. (const :tag "In current frame, other window" other-window)
  750. (const :tag "Each time a new frame" new-frame)
  751. (const :tag "One dedicated frame" dedicated-frame)))
  752. (defcustom org-use-speed-commands nil
  753. "Non-nil means activate single letter commands at beginning of a headline.
  754. This may also be a function to test for appropriate locations where speed
  755. commands should be active."
  756. :group 'org-structure
  757. :type '(choice
  758. (const :tag "Never" nil)
  759. (const :tag "At beginning of headline stars" t)
  760. (function)))
  761. (defcustom org-speed-commands-user nil
  762. "Alist of additional speed commands.
  763. This list will be checked before `org-speed-commands-default'
  764. when the variable `org-use-speed-commands' is non-nil
  765. and when the cursor is at the beginning of a headline.
  766. The car if each entry is a string with a single letter, which must
  767. be assigned to `self-insert-command' in the global map.
  768. The cdr is either a command to be called interactively, a function
  769. to be called, or a form to be evaluated.
  770. An entry that is just a list with a single string will be interpreted
  771. as a descriptive headline that will be added when listing the speed
  772. commands in the Help buffer using the `?' speed command."
  773. :group 'org-structure
  774. :type '(repeat :value ("k" . ignore)
  775. (choice :value ("k" . ignore)
  776. (list :tag "Descriptive Headline" (string :tag "Headline"))
  777. (cons :tag "Letter and Command"
  778. (string :tag "Command letter")
  779. (choice
  780. (function)
  781. (sexp))))))
  782. (defgroup org-cycle nil
  783. "Options concerning visibility cycling in Org-mode."
  784. :tag "Org Cycle"
  785. :group 'org-structure)
  786. (defcustom org-cycle-skip-children-state-if-no-children t
  787. "Non-nil means skip CHILDREN state in entries that don't have any."
  788. :group 'org-cycle
  789. :type 'boolean)
  790. (defcustom org-cycle-max-level nil
  791. "Maximum level which should still be subject to visibility cycling.
  792. Levels higher than this will, for cycling, be treated as text, not a headline.
  793. When `org-odd-levels-only' is set, a value of N in this variable actually
  794. means 2N-1 stars as the limiting headline.
  795. When nil, cycle all levels.
  796. Note that the limiting level of cycling is also influenced by
  797. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  798. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  799. than its value."
  800. :group 'org-cycle
  801. :type '(choice
  802. (const :tag "No limit" nil)
  803. (integer :tag "Maximum level")))
  804. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  805. "Names of drawers. Drawers are not opened by cycling on the headline above.
  806. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  807. this:
  808. :DRAWERNAME:
  809. .....
  810. :END:
  811. The drawer \"PROPERTIES\" is special for capturing properties through
  812. the property API.
  813. Drawers can be defined on the per-file basis with a line like:
  814. #+DRAWERS: HIDDEN STATE PROPERTIES"
  815. :group 'org-structure
  816. :group 'org-cycle
  817. :type '(repeat (string :tag "Drawer Name")))
  818. (defcustom org-hide-block-startup nil
  819. "Non-nil means entering Org-mode will fold all blocks.
  820. This can also be set in on a per-file basis with
  821. #+STARTUP: hideblocks
  822. #+STARTUP: showblocks"
  823. :group 'org-startup
  824. :group 'org-cycle
  825. :type 'boolean)
  826. (defcustom org-cycle-global-at-bob nil
  827. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  828. This makes it possible to do global cycling without having to use S-TAB or
  829. \\[universal-argument] TAB. For this special case to work, the first line
  830. of the buffer must not be a headline -- it may be empty or some other text.
  831. When used in this way, `org-cycle-hook' is disabled temporarily to make
  832. sure the cursor stays at the beginning of the buffer. When this option is
  833. nil, don't do anything special at the beginning of the buffer."
  834. :group 'org-cycle
  835. :type 'boolean)
  836. (defcustom org-cycle-level-after-item/entry-creation t
  837. "Non-nil means cycle entry level or item indentation in new empty entries.
  838. When the cursor is at the end of an empty headline, i.e with only stars
  839. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  840. and then all possible ancestor states, before returning to the original state.
  841. This makes data entry extremely fast: M-RET to create a new headline,
  842. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  843. When the cursor is at the end of an empty plain list item, one TAB will
  844. make it a subitem, two or more tabs will back up to make this an item
  845. higher up in the item hierarchy."
  846. :group 'org-cycle
  847. :type 'boolean)
  848. (defcustom org-cycle-emulate-tab t
  849. "Where should `org-cycle' emulate TAB.
  850. nil Never
  851. white Only in completely white lines
  852. whitestart Only at the beginning of lines, before the first non-white char
  853. t Everywhere except in headlines
  854. exc-hl-bol Everywhere except at the start of a headline
  855. If TAB is used in a place where it does not emulate TAB, the current subtree
  856. visibility is cycled."
  857. :group 'org-cycle
  858. :type '(choice (const :tag "Never" nil)
  859. (const :tag "Only in completely white lines" white)
  860. (const :tag "Before first char in a line" whitestart)
  861. (const :tag "Everywhere except in headlines" t)
  862. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  863. ))
  864. (defcustom org-cycle-separator-lines 2
  865. "Number of empty lines needed to keep an empty line between collapsed trees.
  866. If you leave an empty line between the end of a subtree and the following
  867. headline, this empty line is hidden when the subtree is folded.
  868. Org-mode will leave (exactly) one empty line visible if the number of
  869. empty lines is equal or larger to the number given in this variable.
  870. So the default 2 means at least 2 empty lines after the end of a subtree
  871. are needed to produce free space between a collapsed subtree and the
  872. following headline.
  873. If the number is negative, and the number of empty lines is at least -N,
  874. all empty lines are shown.
  875. Special case: when 0, never leave empty lines in collapsed view."
  876. :group 'org-cycle
  877. :type 'integer)
  878. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  879. (defcustom org-pre-cycle-hook nil
  880. "Hook that is run before visibility cycling is happening.
  881. The function(s) in this hook must accept a single argument which indicates
  882. the new state that will be set right after running this hook. The
  883. argument is a symbol. Before a global state change, it can have the values
  884. `overview', `content', or `all'. Before a local state change, it can have
  885. the values `folded', `children', or `subtree'."
  886. :group 'org-cycle
  887. :type 'hook)
  888. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  889. org-cycle-hide-drawers
  890. org-cycle-hide-inline-tasks
  891. org-cycle-show-empty-lines
  892. org-optimize-window-after-visibility-change)
  893. "Hook that is run after `org-cycle' has changed the buffer visibility.
  894. The function(s) in this hook must accept a single argument which indicates
  895. the new state that was set by the most recent `org-cycle' command. The
  896. argument is a symbol. After a global state change, it can have the values
  897. `overview', `contents', or `all'. After a local state change, it can have
  898. the values `folded', `children', or `subtree'."
  899. :group 'org-cycle
  900. :type 'hook)
  901. (defgroup org-edit-structure nil
  902. "Options concerning structure editing in Org-mode."
  903. :tag "Org Edit Structure"
  904. :group 'org-structure)
  905. (defcustom org-odd-levels-only nil
  906. "Non-nil means skip even levels and only use odd levels for the outline.
  907. This has the effect that two stars are being added/taken away in
  908. promotion/demotion commands. It also influences how levels are
  909. handled by the exporters.
  910. Changing it requires restart of `font-lock-mode' to become effective
  911. for fontification also in regions already fontified.
  912. You may also set this on a per-file basis by adding one of the following
  913. lines to the buffer:
  914. #+STARTUP: odd
  915. #+STARTUP: oddeven"
  916. :group 'org-edit-structure
  917. :group 'org-appearance
  918. :type 'boolean)
  919. (defcustom org-adapt-indentation t
  920. "Non-nil means adapt indentation to outline node level.
  921. When this variable is set, Org assumes that you write outlines by
  922. indenting text in each node to align with the headline (after the stars).
  923. The following issues are influenced by this variable:
  924. - When this is set and the *entire* text in an entry is indented, the
  925. indentation is increased by one space in a demotion command, and
  926. decreased by one in a promotion command. If any line in the entry
  927. body starts with text at column 0, indentation is not changed at all.
  928. - Property drawers and planning information is inserted indented when
  929. this variable s set. When nil, they will not be indented.
  930. - TAB indents a line relative to context. The lines below a headline
  931. will be indented when this variable is set.
  932. Note that this is all about true indentation, by adding and removing
  933. space characters. See also `org-indent.el' which does level-dependent
  934. indentation in a virtual way, i.e. at display time in Emacs."
  935. :group 'org-edit-structure
  936. :type 'boolean)
  937. (defcustom org-special-ctrl-a/e nil
  938. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  939. When t, `C-a' will bring back the cursor to the beginning of the
  940. headline text, i.e. after the stars and after a possible TODO
  941. keyword. In an item, this will be the position after bullet and
  942. check-box, if any. When the cursor is already at that position,
  943. another `C-a' will bring it to the beginning of the line.
  944. `C-e' will jump to the end of the headline, ignoring the presence
  945. of tags in the headline. A second `C-e' will then jump to the
  946. true end of the line, after any tags. This also means that, when
  947. this variable is non-nil, `C-e' also will never jump beyond the
  948. end of the heading of a folded section, i.e. not after the
  949. ellipses.
  950. When set to the symbol `reversed', the first `C-a' or `C-e' works
  951. normally, going to the true line boundary first. Only a directly
  952. following, identical keypress will bring the cursor to the
  953. special positions.
  954. This may also be a cons cell where the behavior for `C-a' and
  955. `C-e' is set separately."
  956. :group 'org-edit-structure
  957. :type '(choice
  958. (const :tag "off" nil)
  959. (const :tag "on: after stars/bullet and before tags first" t)
  960. (const :tag "reversed: true line boundary first" reversed)
  961. (cons :tag "Set C-a and C-e separately"
  962. (choice :tag "Special C-a"
  963. (const :tag "off" nil)
  964. (const :tag "on: after stars/bullet first" t)
  965. (const :tag "reversed: before stars/bullet first" reversed))
  966. (choice :tag "Special C-e"
  967. (const :tag "off" nil)
  968. (const :tag "on: before tags first" t)
  969. (const :tag "reversed: after tags first" reversed)))))
  970. (if (fboundp 'defvaralias)
  971. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  972. (defcustom org-special-ctrl-k nil
  973. "Non-nil means `C-k' will behave specially in headlines.
  974. When nil, `C-k' will call the default `kill-line' command.
  975. When t, the following will happen while the cursor is in the headline:
  976. - When the cursor is at the beginning of a headline, kill the entire
  977. line and possible the folded subtree below the line.
  978. - When in the middle of the headline text, kill the headline up to the tags.
  979. - When after the headline text, kill the tags."
  980. :group 'org-edit-structure
  981. :type 'boolean)
  982. (defcustom org-ctrl-k-protect-subtree nil
  983. "Non-nil means, do not delete a hidden subtree with C-k.
  984. When set to the symbol `error', simply throw an error when C-k is
  985. used to kill (part-of) a headline that has hidden text behind it.
  986. Any other non-nil value will result in a query to the user, if it is
  987. OK to kill that hidden subtree. When nil, kill without remorse."
  988. :group 'org-edit-structure
  989. :version "24.1"
  990. :type '(choice
  991. (const :tag "Do not protect hidden subtrees" nil)
  992. (const :tag "Protect hidden subtrees with a security query" t)
  993. (const :tag "Never kill a hidden subtree with C-k" error)))
  994. (defcustom org-catch-invisible-edits nil
  995. "Check if in invisible region before inserting or deleting a character.
  996. Valid values are:
  997. nil Do not check, so just do invisible edits.
  998. error Throw an error and do nothing.
  999. show Make point visible, and do the requested edit.
  1000. show-and-error Make point visible, then throw an error and abort the edit.
  1001. smart Make point visible, and do insertion/deletion if it is
  1002. adjacent to visible text and the change feels predictable.
  1003. Never delete a previously invisible character or add in the
  1004. middle or right after an invisible region. Basically, this
  1005. allows insertion and backward-delete right before ellipses.
  1006. FIXME: maybe in this case we should not even show?"
  1007. :group 'org-edit-structure
  1008. :version "24.1"
  1009. :type '(choice
  1010. (const :tag "Do not check" nil)
  1011. (const :tag "Throw error when trying to edit" error)
  1012. (const :tag "Unhide, but do not do the edit" show-and-error)
  1013. (const :tag "Show invisible part and do the edit" show)
  1014. (const :tag "Be smart and do the right thing" smart)))
  1015. (defcustom org-yank-folded-subtrees t
  1016. "Non-nil means when yanking subtrees, fold them.
  1017. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1018. it starts with a heading and all other headings in it are either children
  1019. or siblings, then fold all the subtrees. However, do this only if no
  1020. text after the yank would be swallowed into a folded tree by this action."
  1021. :group 'org-edit-structure
  1022. :type 'boolean)
  1023. (defcustom org-yank-adjusted-subtrees nil
  1024. "Non-nil means when yanking subtrees, adjust the level.
  1025. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1026. this function for details."
  1027. :group 'org-edit-structure
  1028. :type 'boolean)
  1029. (defcustom org-M-RET-may-split-line '((default . t))
  1030. "Non-nil means M-RET will split the line at the cursor position.
  1031. When nil, it will go to the end of the line before making a
  1032. new line.
  1033. You may also set this option in a different way for different
  1034. contexts. Valid contexts are:
  1035. headline when creating a new headline
  1036. item when creating a new item
  1037. table in a table field
  1038. default the value to be used for all contexts not explicitly
  1039. customized"
  1040. :group 'org-structure
  1041. :group 'org-table
  1042. :type '(choice
  1043. (const :tag "Always" t)
  1044. (const :tag "Never" nil)
  1045. (repeat :greedy t :tag "Individual contexts"
  1046. (cons
  1047. (choice :tag "Context"
  1048. (const headline)
  1049. (const item)
  1050. (const table)
  1051. (const default))
  1052. (boolean)))))
  1053. (defcustom org-insert-heading-respect-content nil
  1054. "Non-nil means insert new headings after the current subtree.
  1055. When nil, the new heading is created directly after the current line.
  1056. The commands \\[org-insert-heading-respect-content] and
  1057. \\[org-insert-todo-heading-respect-content] turn this variable on
  1058. for the duration of the command."
  1059. :group 'org-structure
  1060. :type 'boolean)
  1061. (defcustom org-blank-before-new-entry '((heading . auto)
  1062. (plain-list-item . auto))
  1063. "Should `org-insert-heading' leave a blank line before new heading/item?
  1064. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1065. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1066. which case Org will look at the surrounding headings/items and try to
  1067. make an intelligent decision whether to insert a blank line or not.
  1068. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1069. set, the setting here is ignored and no empty line is inserted, to avoid
  1070. breaking the list structure."
  1071. :group 'org-edit-structure
  1072. :type '(list
  1073. (cons (const heading)
  1074. (choice (const :tag "Never" nil)
  1075. (const :tag "Always" t)
  1076. (const :tag "Auto" auto)))
  1077. (cons (const plain-list-item)
  1078. (choice (const :tag "Never" nil)
  1079. (const :tag "Always" t)
  1080. (const :tag "Auto" auto)))))
  1081. (defcustom org-insert-heading-hook nil
  1082. "Hook being run after inserting a new heading."
  1083. :group 'org-edit-structure
  1084. :type 'hook)
  1085. (defcustom org-enable-fixed-width-editor t
  1086. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1087. This currently only means they are never auto-wrapped.
  1088. When nil, such lines will be treated like ordinary lines.
  1089. See also the QUOTE keyword."
  1090. :group 'org-edit-structure
  1091. :type 'boolean)
  1092. (defcustom org-goto-auto-isearch t
  1093. "Non-nil means typing characters in `org-goto' starts incremental search."
  1094. :group 'org-edit-structure
  1095. :type 'boolean)
  1096. (defgroup org-sparse-trees nil
  1097. "Options concerning sparse trees in Org-mode."
  1098. :tag "Org Sparse Trees"
  1099. :group 'org-structure)
  1100. (defcustom org-highlight-sparse-tree-matches t
  1101. "Non-nil means highlight all matches that define a sparse tree.
  1102. The highlights will automatically disappear the next time the buffer is
  1103. changed by an edit command."
  1104. :group 'org-sparse-trees
  1105. :type 'boolean)
  1106. (defcustom org-remove-highlights-with-change t
  1107. "Non-nil means any change to the buffer will remove temporary highlights.
  1108. Such highlights are created by `org-occur' and `org-clock-display'.
  1109. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1110. The highlights created by `org-preview-latex-fragment' always need
  1111. `C-c C-c' to be removed."
  1112. :group 'org-sparse-trees
  1113. :group 'org-time
  1114. :type 'boolean)
  1115. (defcustom org-occur-hook '(org-first-headline-recenter)
  1116. "Hook that is run after `org-occur' has constructed a sparse tree.
  1117. This can be used to recenter the window to show as much of the structure
  1118. as possible."
  1119. :group 'org-sparse-trees
  1120. :type 'hook)
  1121. (defgroup org-imenu-and-speedbar nil
  1122. "Options concerning imenu and speedbar in Org-mode."
  1123. :tag "Org Imenu and Speedbar"
  1124. :group 'org-structure)
  1125. (defcustom org-imenu-depth 2
  1126. "The maximum level for Imenu access to Org-mode headlines.
  1127. This also applied for speedbar access."
  1128. :group 'org-imenu-and-speedbar
  1129. :type 'integer)
  1130. (defgroup org-table nil
  1131. "Options concerning tables in Org-mode."
  1132. :tag "Org Table"
  1133. :group 'org)
  1134. (defcustom org-enable-table-editor 'optimized
  1135. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1136. When nil, such lines will be treated like ordinary lines.
  1137. When equal to the symbol `optimized', the table editor will be optimized to
  1138. do the following:
  1139. - Automatic overwrite mode in front of whitespace in table fields.
  1140. This makes the structure of the table stay in tact as long as the edited
  1141. field does not exceed the column width.
  1142. - Minimize the number of realigns. Normally, the table is aligned each time
  1143. TAB or RET are pressed to move to another field. With optimization this
  1144. happens only if changes to a field might have changed the column width.
  1145. Optimization requires replacing the functions `self-insert-command',
  1146. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1147. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1148. very good at guessing when a re-align will be necessary, but you can always
  1149. force one with \\[org-ctrl-c-ctrl-c].
  1150. If you would like to use the optimized version in Org-mode, but the
  1151. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1152. This variable can be used to turn on and off the table editor during a session,
  1153. but in order to toggle optimization, a restart is required.
  1154. See also the variable `org-table-auto-blank-field'."
  1155. :group 'org-table
  1156. :type '(choice
  1157. (const :tag "off" nil)
  1158. (const :tag "on" t)
  1159. (const :tag "on, optimized" optimized)))
  1160. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1161. (version<= emacs-version "24.1"))
  1162. "Non-nil means cluster self-insert commands for undo when possible.
  1163. If this is set, then, like in the Emacs command loop, 20 consecutive
  1164. characters will be undone together.
  1165. This is configurable, because there is some impact on typing performance."
  1166. :group 'org-table
  1167. :type 'boolean)
  1168. (defcustom org-table-tab-recognizes-table.el t
  1169. "Non-nil means TAB will automatically notice a table.el table.
  1170. When it sees such a table, it moves point into it and - if necessary -
  1171. calls `table-recognize-table'."
  1172. :group 'org-table-editing
  1173. :type 'boolean)
  1174. (defgroup org-link nil
  1175. "Options concerning links in Org-mode."
  1176. :tag "Org Link"
  1177. :group 'org)
  1178. (defvar org-link-abbrev-alist-local nil
  1179. "Buffer-local version of `org-link-abbrev-alist', which see.
  1180. The value of this is taken from the #+LINK lines.")
  1181. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1182. (defcustom org-link-abbrev-alist nil
  1183. "Alist of link abbreviations.
  1184. The car of each element is a string, to be replaced at the start of a link.
  1185. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1186. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1187. [[linkkey:tag][description]]
  1188. The 'linkkey' must be a word word, starting with a letter, followed
  1189. by letters, numbers, '-' or '_'.
  1190. If REPLACE is a string, the tag will simply be appended to create the link.
  1191. If the string contains \"%s\", the tag will be inserted there. If the string
  1192. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1193. at that point (see the function `url-hexify-string'). If the string contains
  1194. the specifier \"%(my-function)\", then the custom function `my-function' will
  1195. be invoked: this function takes the tag as its only argument and must return
  1196. a string.
  1197. REPLACE may also be a function that will be called with the tag as the
  1198. only argument to create the link, which should be returned as a string.
  1199. See the manual for examples."
  1200. :group 'org-link
  1201. :type '(repeat
  1202. (cons
  1203. (string :tag "Protocol")
  1204. (choice
  1205. (string :tag "Format")
  1206. (function)))))
  1207. (defcustom org-descriptive-links t
  1208. "Non-nil means Org will display descriptive links.
  1209. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1210. \"Org Website\", hiding the link itself and just displaying its
  1211. description. When set to `nil', Org will display the full links
  1212. literally.
  1213. You can interactively set the value of this variable by calling
  1214. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1215. :group 'org-link
  1216. :type 'boolean)
  1217. (defcustom org-link-file-path-type 'adaptive
  1218. "How the path name in file links should be stored.
  1219. Valid values are:
  1220. relative Relative to the current directory, i.e. the directory of the file
  1221. into which the link is being inserted.
  1222. absolute Absolute path, if possible with ~ for home directory.
  1223. noabbrev Absolute path, no abbreviation of home directory.
  1224. adaptive Use relative path for files in the current directory and sub-
  1225. directories of it. For other files, use an absolute path."
  1226. :group 'org-link
  1227. :type '(choice
  1228. (const relative)
  1229. (const absolute)
  1230. (const noabbrev)
  1231. (const adaptive)))
  1232. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1233. "Types of links that should be activated in Org-mode files.
  1234. This is a list of symbols, each leading to the activation of a certain link
  1235. type. In principle, it does not hurt to turn on most link types - there may
  1236. be a small gain when turning off unused link types. The types are:
  1237. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1238. angle Links in angular brackets that may contain whitespace like
  1239. <bbdb:Carsten Dominik>.
  1240. plain Plain links in normal text, no whitespace, like http://google.com.
  1241. radio Text that is matched by a radio target, see manual for details.
  1242. tag Tag settings in a headline (link to tag search).
  1243. date Time stamps (link to calendar).
  1244. footnote Footnote labels.
  1245. Changing this variable requires a restart of Emacs to become effective."
  1246. :group 'org-link
  1247. :type '(set :greedy t
  1248. (const :tag "Double bracket links" bracket)
  1249. (const :tag "Angular bracket links" angle)
  1250. (const :tag "Plain text links" plain)
  1251. (const :tag "Radio target matches" radio)
  1252. (const :tag "Tags" tag)
  1253. (const :tag "Timestamps" date)
  1254. (const :tag "Footnotes" footnote)))
  1255. (defcustom org-make-link-description-function nil
  1256. "Function to use for generating link descriptions from links.
  1257. When nil, the link location will be used. This function must take
  1258. two parameters: the first one is the link, the second one is the
  1259. description generated by `org-insert-link'. The function should
  1260. return the description to use."
  1261. :group 'org-link
  1262. :type 'function)
  1263. (defgroup org-link-store nil
  1264. "Options concerning storing links in Org-mode."
  1265. :tag "Org Store Link"
  1266. :group 'org-link)
  1267. (defcustom org-url-hexify-p t
  1268. "When non-nil, hexify URL when creating a link."
  1269. :type 'boolean
  1270. :version "24.3"
  1271. :group 'org-link-store)
  1272. (defcustom org-email-link-description-format "Email %c: %.30s"
  1273. "Format of the description part of a link to an email or usenet message.
  1274. The following %-escapes will be replaced by corresponding information:
  1275. %F full \"From\" field
  1276. %f name, taken from \"From\" field, address if no name
  1277. %T full \"To\" field
  1278. %t first name in \"To\" field, address if no name
  1279. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1280. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1281. %s subject
  1282. %d date
  1283. %m message-id.
  1284. You may use normal field width specification between the % and the letter.
  1285. This is for example useful to limit the length of the subject.
  1286. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1287. :group 'org-link-store
  1288. :type 'string)
  1289. (defcustom org-from-is-user-regexp
  1290. (let (r1 r2)
  1291. (when (and user-mail-address (not (string= user-mail-address "")))
  1292. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1293. (when (and user-full-name (not (string= user-full-name "")))
  1294. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1295. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1296. "Regexp matched against the \"From:\" header of an email or usenet message.
  1297. It should match if the message is from the user him/herself."
  1298. :group 'org-link-store
  1299. :type 'regexp)
  1300. (defcustom org-context-in-file-links t
  1301. "Non-nil means file links from `org-store-link' contain context.
  1302. A search string will be added to the file name with :: as separator and
  1303. used to find the context when the link is activated by the command
  1304. `org-open-at-point'. When this option is t, the entire active region
  1305. will be placed in the search string of the file link. If set to a
  1306. positive integer, only the first n lines of context will be stored.
  1307. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1308. negates this setting for the duration of the command."
  1309. :group 'org-link-store
  1310. :type '(choice boolean integer))
  1311. (defcustom org-keep-stored-link-after-insertion nil
  1312. "Non-nil means keep link in list for entire session.
  1313. The command `org-store-link' adds a link pointing to the current
  1314. location to an internal list. These links accumulate during a session.
  1315. The command `org-insert-link' can be used to insert links into any
  1316. Org-mode file (offering completion for all stored links). When this
  1317. option is nil, every link which has been inserted once using \\[org-insert-link]
  1318. will be removed from the list, to make completing the unused links
  1319. more efficient."
  1320. :group 'org-link-store
  1321. :type 'boolean)
  1322. (defgroup org-link-follow nil
  1323. "Options concerning following links in Org-mode."
  1324. :tag "Org Follow Link"
  1325. :group 'org-link)
  1326. (defcustom org-link-translation-function nil
  1327. "Function to translate links with different syntax to Org syntax.
  1328. This can be used to translate links created for example by the Planner
  1329. or emacs-wiki packages to Org syntax.
  1330. The function must accept two parameters, a TYPE containing the link
  1331. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1332. which is everything after the link protocol. It should return a cons
  1333. with possibly modified values of type and path.
  1334. Org contains a function for this, so if you set this variable to
  1335. `org-translate-link-from-planner', you should be able follow many
  1336. links created by planner."
  1337. :group 'org-link-follow
  1338. :type 'function)
  1339. (defcustom org-follow-link-hook nil
  1340. "Hook that is run after a link has been followed."
  1341. :group 'org-link-follow
  1342. :type 'hook)
  1343. (defcustom org-tab-follows-link nil
  1344. "Non-nil means on links TAB will follow the link.
  1345. Needs to be set before org.el is loaded.
  1346. This really should not be used, it does not make sense, and the
  1347. implementation is bad."
  1348. :group 'org-link-follow
  1349. :type 'boolean)
  1350. (defcustom org-return-follows-link nil
  1351. "Non-nil means on links RET will follow the link."
  1352. :group 'org-link-follow
  1353. :type 'boolean)
  1354. (defcustom org-mouse-1-follows-link
  1355. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1356. "Non-nil means mouse-1 on a link will follow the link.
  1357. A longer mouse click will still set point. Does not work on XEmacs.
  1358. Needs to be set before org.el is loaded."
  1359. :group 'org-link-follow
  1360. :type 'boolean)
  1361. (defcustom org-mark-ring-length 4
  1362. "Number of different positions to be recorded in the ring.
  1363. Changing this requires a restart of Emacs to work correctly."
  1364. :group 'org-link-follow
  1365. :type 'integer)
  1366. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1367. "Non-nil means internal links in Org files must exactly match a headline.
  1368. When nil, the link search tries to match a phrase with all words
  1369. in the search text."
  1370. :group 'org-link-follow
  1371. :version "24.1"
  1372. :type '(choice
  1373. (const :tag "Use fuzzy text search" nil)
  1374. (const :tag "Match only exact headline" t)
  1375. (const :tag "Match exact headline or query to create it"
  1376. query-to-create)))
  1377. (defcustom org-link-frame-setup
  1378. '((vm . vm-visit-folder-other-frame)
  1379. (vm-imap . vm-visit-imap-folder-other-frame)
  1380. (gnus . org-gnus-no-new-news)
  1381. (file . find-file-other-window)
  1382. (wl . wl-other-frame))
  1383. "Setup the frame configuration for following links.
  1384. When following a link with Emacs, it may often be useful to display
  1385. this link in another window or frame. This variable can be used to
  1386. set this up for the different types of links.
  1387. For VM, use any of
  1388. `vm-visit-folder'
  1389. `vm-visit-folder-other-window'
  1390. `vm-visit-folder-other-frame'
  1391. For Gnus, use any of
  1392. `gnus'
  1393. `gnus-other-frame'
  1394. `org-gnus-no-new-news'
  1395. For FILE, use any of
  1396. `find-file'
  1397. `find-file-other-window'
  1398. `find-file-other-frame'
  1399. For Wanderlust use any of
  1400. `wl'
  1401. `wl-other-frame'
  1402. For the calendar, use the variable `calendar-setup'.
  1403. For BBDB, it is currently only possible to display the matches in
  1404. another window."
  1405. :group 'org-link-follow
  1406. :type '(list
  1407. (cons (const vm)
  1408. (choice
  1409. (const vm-visit-folder)
  1410. (const vm-visit-folder-other-window)
  1411. (const vm-visit-folder-other-frame)))
  1412. (cons (const gnus)
  1413. (choice
  1414. (const gnus)
  1415. (const gnus-other-frame)
  1416. (const org-gnus-no-new-news)))
  1417. (cons (const file)
  1418. (choice
  1419. (const find-file)
  1420. (const find-file-other-window)
  1421. (const find-file-other-frame)))
  1422. (cons (const wl)
  1423. (choice
  1424. (const wl)
  1425. (const wl-other-frame)))))
  1426. (defcustom org-display-internal-link-with-indirect-buffer nil
  1427. "Non-nil means use indirect buffer to display infile links.
  1428. Activating internal links (from one location in a file to another location
  1429. in the same file) normally just jumps to the location. When the link is
  1430. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1431. is displayed in
  1432. another window. When this option is set, the other window actually displays
  1433. an indirect buffer clone of the current buffer, to avoid any visibility
  1434. changes to the current buffer."
  1435. :group 'org-link-follow
  1436. :type 'boolean)
  1437. (defcustom org-open-non-existing-files nil
  1438. "Non-nil means `org-open-file' will open non-existing files.
  1439. When nil, an error will be generated.
  1440. This variable applies only to external applications because they
  1441. might choke on non-existing files. If the link is to a file that
  1442. will be opened in Emacs, the variable is ignored."
  1443. :group 'org-link-follow
  1444. :type 'boolean)
  1445. (defcustom org-open-directory-means-index-dot-org nil
  1446. "Non-nil means a link to a directory really means to index.org.
  1447. When nil, following a directory link will run dired or open a finder/explorer
  1448. window on that directory."
  1449. :group 'org-link-follow
  1450. :type 'boolean)
  1451. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1452. "Function and arguments to call for following mailto links.
  1453. This is a list with the first element being a Lisp function, and the
  1454. remaining elements being arguments to the function. In string arguments,
  1455. %a will be replaced by the address, and %s will be replaced by the subject
  1456. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1457. :group 'org-link-follow
  1458. :type '(choice
  1459. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1460. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1461. (const :tag "message-mail" (message-mail "%a" "%s"))
  1462. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1463. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1464. "Non-nil means ask for confirmation before executing shell links.
  1465. Shell links can be dangerous: just think about a link
  1466. [[shell:rm -rf ~/*][Google Search]]
  1467. This link would show up in your Org-mode document as \"Google Search\",
  1468. but really it would remove your entire home directory.
  1469. Therefore we advise against setting this variable to nil.
  1470. Just change it to `y-or-n-p' if you want to confirm with a
  1471. single keystroke rather than having to type \"yes\"."
  1472. :group 'org-link-follow
  1473. :type '(choice
  1474. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1475. (const :tag "with y-or-n (faster)" y-or-n-p)
  1476. (const :tag "no confirmation (dangerous)" nil)))
  1477. (put 'org-confirm-shell-link-function
  1478. 'safe-local-variable
  1479. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1480. (defcustom org-confirm-shell-link-not-regexp ""
  1481. "A regexp to skip confirmation for shell links."
  1482. :group 'org-link-follow
  1483. :version "24.1"
  1484. :type 'regexp)
  1485. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1486. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1487. Elisp links can be dangerous: just think about a link
  1488. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1489. This link would show up in your Org-mode document as \"Google Search\",
  1490. but really it would remove your entire home directory.
  1491. Therefore we advise against setting this variable to nil.
  1492. Just change it to `y-or-n-p' if you want to confirm with a
  1493. single keystroke rather than having to type \"yes\"."
  1494. :group 'org-link-follow
  1495. :type '(choice
  1496. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1497. (const :tag "with y-or-n (faster)" y-or-n-p)
  1498. (const :tag "no confirmation (dangerous)" nil)))
  1499. (put 'org-confirm-shell-link-function
  1500. 'safe-local-variable
  1501. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1502. (defcustom org-confirm-elisp-link-not-regexp ""
  1503. "A regexp to skip confirmation for Elisp links."
  1504. :group 'org-link-follow
  1505. :version "24.1"
  1506. :type 'regexp)
  1507. (defconst org-file-apps-defaults-gnu
  1508. '((remote . emacs)
  1509. (system . mailcap)
  1510. (t . mailcap))
  1511. "Default file applications on a UNIX or GNU/Linux system.
  1512. See `org-file-apps'.")
  1513. (defconst org-file-apps-defaults-macosx
  1514. '((remote . emacs)
  1515. (t . "open %s")
  1516. (system . "open %s")
  1517. ("ps.gz" . "gv %s")
  1518. ("eps.gz" . "gv %s")
  1519. ("dvi" . "xdvi %s")
  1520. ("fig" . "xfig %s"))
  1521. "Default file applications on a MacOS X system.
  1522. The system \"open\" is known as a default, but we use X11 applications
  1523. for some files for which the OS does not have a good default.
  1524. See `org-file-apps'.")
  1525. (defconst org-file-apps-defaults-windowsnt
  1526. (list
  1527. '(remote . emacs)
  1528. (cons t
  1529. (list (if (featurep 'xemacs)
  1530. 'mswindows-shell-execute
  1531. 'w32-shell-execute)
  1532. "open" 'file))
  1533. (cons 'system
  1534. (list (if (featurep 'xemacs)
  1535. 'mswindows-shell-execute
  1536. 'w32-shell-execute)
  1537. "open" 'file)))
  1538. "Default file applications on a Windows NT system.
  1539. The system \"open\" is used for most files.
  1540. See `org-file-apps'.")
  1541. (defcustom org-file-apps
  1542. '(
  1543. (auto-mode . emacs)
  1544. ("\\.mm\\'" . default)
  1545. ("\\.x?html?\\'" . default)
  1546. ("\\.pdf\\'" . default)
  1547. )
  1548. "External applications for opening `file:path' items in a document.
  1549. Org-mode uses system defaults for different file types, but
  1550. you can use this variable to set the application for a given file
  1551. extension. The entries in this list are cons cells where the car identifies
  1552. files and the cdr the corresponding command. Possible values for the
  1553. file identifier are
  1554. \"string\" A string as a file identifier can be interpreted in different
  1555. ways, depending on its contents:
  1556. - Alphanumeric characters only:
  1557. Match links with this file extension.
  1558. Example: (\"pdf\" . \"evince %s\")
  1559. to open PDFs with evince.
  1560. - Regular expression: Match links where the
  1561. filename matches the regexp. If you want to
  1562. use groups here, use shy groups.
  1563. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1564. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1565. to open *.html and *.xhtml with firefox.
  1566. - Regular expression which contains (non-shy) groups:
  1567. Match links where the whole link, including \"::\", and
  1568. anything after that, matches the regexp.
  1569. In a custom command string, %1, %2, etc. are replaced with
  1570. the parts of the link that were matched by the groups.
  1571. For backwards compatibility, if a command string is given
  1572. that does not use any of the group matches, this case is
  1573. handled identically to the second one (i.e. match against
  1574. file name only).
  1575. In a custom lisp form, you can access the group matches with
  1576. (match-string n link).
  1577. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1578. to open [[file:document.pdf::5]] with evince at page 5.
  1579. `directory' Matches a directory
  1580. `remote' Matches a remote file, accessible through tramp or efs.
  1581. Remote files most likely should be visited through Emacs
  1582. because external applications cannot handle such paths.
  1583. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1584. so all files Emacs knows how to handle. Using this with
  1585. command `emacs' will open most files in Emacs. Beware that this
  1586. will also open html files inside Emacs, unless you add
  1587. (\"html\" . default) to the list as well.
  1588. t Default for files not matched by any of the other options.
  1589. `system' The system command to open files, like `open' on Windows
  1590. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1591. that will be selected if you call `C-c C-o' with a double
  1592. \\[universal-argument] \\[universal-argument] prefix.
  1593. Possible values for the command are:
  1594. `emacs' The file will be visited by the current Emacs process.
  1595. `default' Use the default application for this file type, which is the
  1596. association for t in the list, most likely in the system-specific
  1597. part.
  1598. This can be used to overrule an unwanted setting in the
  1599. system-specific variable.
  1600. `system' Use the system command for opening files, like \"open\".
  1601. This command is specified by the entry whose car is `system'.
  1602. Most likely, the system-specific version of this variable
  1603. does define this command, but you can overrule/replace it
  1604. here.
  1605. string A command to be executed by a shell; %s will be replaced
  1606. by the path to the file.
  1607. sexp A Lisp form which will be evaluated. The file path will
  1608. be available in the Lisp variable `file'.
  1609. For more examples, see the system specific constants
  1610. `org-file-apps-defaults-macosx'
  1611. `org-file-apps-defaults-windowsnt'
  1612. `org-file-apps-defaults-gnu'."
  1613. :group 'org-link-follow
  1614. :type '(repeat
  1615. (cons (choice :value ""
  1616. (string :tag "Extension")
  1617. (const :tag "System command to open files" system)
  1618. (const :tag "Default for unrecognized files" t)
  1619. (const :tag "Remote file" remote)
  1620. (const :tag "Links to a directory" directory)
  1621. (const :tag "Any files that have Emacs modes"
  1622. auto-mode))
  1623. (choice :value ""
  1624. (const :tag "Visit with Emacs" emacs)
  1625. (const :tag "Use default" default)
  1626. (const :tag "Use the system command" system)
  1627. (string :tag "Command")
  1628. (sexp :tag "Lisp form")))))
  1629. (defcustom org-doi-server-url "http://dx.doi.org/"
  1630. "The URL of the DOI server."
  1631. :type 'string
  1632. :version "24.3"
  1633. :group 'org-link-follow)
  1634. (defgroup org-refile nil
  1635. "Options concerning refiling entries in Org-mode."
  1636. :tag "Org Refile"
  1637. :group 'org)
  1638. (defcustom org-directory "~/org"
  1639. "Directory with org files.
  1640. This is just a default location to look for Org files. There is no need
  1641. at all to put your files into this directory. It is only used in the
  1642. following situations:
  1643. 1. When a capture template specifies a target file that is not an
  1644. absolute path. The path will then be interpreted relative to
  1645. `org-directory'
  1646. 2. When a capture note is filed away in an interactive way (when exiting the
  1647. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1648. with `org-directory' as the default path."
  1649. :group 'org-refile
  1650. :group 'org-remember
  1651. :group 'org-capture
  1652. :type 'directory)
  1653. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1654. "Default target for storing notes.
  1655. Used as a fall back file for org-remember.el and org-capture.el, for
  1656. templates that do not specify a target file."
  1657. :group 'org-refile
  1658. :group 'org-remember
  1659. :group 'org-capture
  1660. :type '(choice
  1661. (const :tag "Default from remember-data-file" nil)
  1662. file))
  1663. (defcustom org-goto-interface 'outline
  1664. "The default interface to be used for `org-goto'.
  1665. Allowed values are:
  1666. outline The interface shows an outline of the relevant file
  1667. and the correct heading is found by moving through
  1668. the outline or by searching with incremental search.
  1669. outline-path-completion Headlines in the current buffer are offered via
  1670. completion. This is the interface also used by
  1671. the refile command."
  1672. :group 'org-refile
  1673. :type '(choice
  1674. (const :tag "Outline" outline)
  1675. (const :tag "Outline-path-completion" outline-path-completion)))
  1676. (defcustom org-goto-max-level 5
  1677. "Maximum target level when running `org-goto' with refile interface."
  1678. :group 'org-refile
  1679. :type 'integer)
  1680. (defcustom org-reverse-note-order nil
  1681. "Non-nil means store new notes at the beginning of a file or entry.
  1682. When nil, new notes will be filed to the end of a file or entry.
  1683. This can also be a list with cons cells of regular expressions that
  1684. are matched against file names, and values."
  1685. :group 'org-remember
  1686. :group 'org-capture
  1687. :group 'org-refile
  1688. :type '(choice
  1689. (const :tag "Reverse always" t)
  1690. (const :tag "Reverse never" nil)
  1691. (repeat :tag "By file name regexp"
  1692. (cons regexp boolean))))
  1693. (defcustom org-log-refile nil
  1694. "Information to record when a task is refiled.
  1695. Possible values are:
  1696. nil Don't add anything
  1697. time Add a time stamp to the task
  1698. note Prompt for a note and add it with template `org-log-note-headings'
  1699. This option can also be set with on a per-file-basis with
  1700. #+STARTUP: nologrefile
  1701. #+STARTUP: logrefile
  1702. #+STARTUP: lognoterefile
  1703. You can have local logging settings for a subtree by setting the LOGGING
  1704. property to one or more of these keywords.
  1705. When bulk-refiling from the agenda, the value `note' is forbidden and
  1706. will temporarily be changed to `time'."
  1707. :group 'org-refile
  1708. :group 'org-progress
  1709. :version "24.1"
  1710. :type '(choice
  1711. (const :tag "No logging" nil)
  1712. (const :tag "Record timestamp" time)
  1713. (const :tag "Record timestamp with note." note)))
  1714. (defcustom org-refile-targets nil
  1715. "Targets for refiling entries with \\[org-refile].
  1716. This is a list of cons cells. Each cell contains:
  1717. - a specification of the files to be considered, either a list of files,
  1718. or a symbol whose function or variable value will be used to retrieve
  1719. a file name or a list of file names. If you use `org-agenda-files' for
  1720. that, all agenda files will be scanned for targets. Nil means consider
  1721. headings in the current buffer.
  1722. - A specification of how to find candidate refile targets. This may be
  1723. any of:
  1724. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1725. This tag has to be present in all target headlines, inheritance will
  1726. not be considered.
  1727. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1728. todo keyword.
  1729. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1730. headlines that are refiling targets.
  1731. - a cons cell (:level . N). Any headline of level N is considered a target.
  1732. Note that, when `org-odd-levels-only' is set, level corresponds to
  1733. order in hierarchy, not to the number of stars.
  1734. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1735. Note that, when `org-odd-levels-only' is set, level corresponds to
  1736. order in hierarchy, not to the number of stars.
  1737. Each element of this list generates a set of possible targets.
  1738. The union of these sets is presented (with completion) to
  1739. the user by `org-refile'.
  1740. You can set the variable `org-refile-target-verify-function' to a function
  1741. to verify each headline found by the simple criteria above.
  1742. When this variable is nil, all top-level headlines in the current buffer
  1743. are used, equivalent to the value `((nil . (:level . 1))'."
  1744. :group 'org-refile
  1745. :type '(repeat
  1746. (cons
  1747. (choice :value org-agenda-files
  1748. (const :tag "All agenda files" org-agenda-files)
  1749. (const :tag "Current buffer" nil)
  1750. (function) (variable) (file))
  1751. (choice :tag "Identify target headline by"
  1752. (cons :tag "Specific tag" (const :value :tag) (string))
  1753. (cons :tag "TODO keyword" (const :value :todo) (string))
  1754. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1755. (cons :tag "Level number" (const :value :level) (integer))
  1756. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1757. (defcustom org-refile-target-verify-function nil
  1758. "Function to verify if the headline at point should be a refile target.
  1759. The function will be called without arguments, with point at the
  1760. beginning of the headline. It should return t and leave point
  1761. where it is if the headline is a valid target for refiling.
  1762. If the target should not be selected, the function must return nil.
  1763. In addition to this, it may move point to a place from where the search
  1764. should be continued. For example, the function may decide that the entire
  1765. subtree of the current entry should be excluded and move point to the end
  1766. of the subtree."
  1767. :group 'org-refile
  1768. :type 'function)
  1769. (defcustom org-refile-use-cache nil
  1770. "Non-nil means cache refile targets to speed up the process.
  1771. The cache for a particular file will be updated automatically when
  1772. the buffer has been killed, or when any of the marker used for flagging
  1773. refile targets no longer points at a live buffer.
  1774. If you have added new entries to a buffer that might themselves be targets,
  1775. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1776. find that easier, `C-u C-u C-u C-c C-w'."
  1777. :group 'org-refile
  1778. :version "24.1"
  1779. :type 'boolean)
  1780. (defcustom org-refile-use-outline-path nil
  1781. "Non-nil means provide refile targets as paths.
  1782. So a level 3 headline will be available as level1/level2/level3.
  1783. When the value is `file', also include the file name (without directory)
  1784. into the path. In this case, you can also stop the completion after
  1785. the file name, to get entries inserted as top level in the file.
  1786. When `full-file-path', include the full file path."
  1787. :group 'org-refile
  1788. :type '(choice
  1789. (const :tag "Not" nil)
  1790. (const :tag "Yes" t)
  1791. (const :tag "Start with file name" file)
  1792. (const :tag "Start with full file path" full-file-path)))
  1793. (defcustom org-outline-path-complete-in-steps t
  1794. "Non-nil means complete the outline path in hierarchical steps.
  1795. When Org-mode uses the refile interface to select an outline path
  1796. \(see variable `org-refile-use-outline-path'), the completion of
  1797. the path can be done is a single go, or if can be done in steps down
  1798. the headline hierarchy. Going in steps is probably the best if you
  1799. do not use a special completion package like `ido' or `icicles'.
  1800. However, when using these packages, going in one step can be very
  1801. fast, while still showing the whole path to the entry."
  1802. :group 'org-refile
  1803. :type 'boolean)
  1804. (defcustom org-refile-allow-creating-parent-nodes nil
  1805. "Non-nil means allow to create new nodes as refile targets.
  1806. New nodes are then created by adding \"/new node name\" to the completion
  1807. of an existing node. When the value of this variable is `confirm',
  1808. new node creation must be confirmed by the user (recommended)
  1809. When nil, the completion must match an existing entry.
  1810. Note that, if the new heading is not seen by the criteria
  1811. listed in `org-refile-targets', multiple instances of the same
  1812. heading would be created by trying again to file under the new
  1813. heading."
  1814. :group 'org-refile
  1815. :type '(choice
  1816. (const :tag "Never" nil)
  1817. (const :tag "Always" t)
  1818. (const :tag "Prompt for confirmation" confirm)))
  1819. (defcustom org-refile-active-region-within-subtree nil
  1820. "Non-nil means also refile active region within a subtree.
  1821. By default `org-refile' doesn't allow refiling regions if they
  1822. don't contain a set of subtrees, but it might be convenient to
  1823. do so sometimes: in that case, the first line of the region is
  1824. converted to a headline before refiling."
  1825. :group 'org-refile
  1826. :version "24.1"
  1827. :type 'boolean)
  1828. (defgroup org-todo nil
  1829. "Options concerning TODO items in Org-mode."
  1830. :tag "Org TODO"
  1831. :group 'org)
  1832. (defgroup org-progress nil
  1833. "Options concerning Progress logging in Org-mode."
  1834. :tag "Org Progress"
  1835. :group 'org-time)
  1836. (defvar org-todo-interpretation-widgets
  1837. '((:tag "Sequence (cycling hits every state)" sequence)
  1838. (:tag "Type (cycling directly to DONE)" type))
  1839. "The available interpretation symbols for customizing `org-todo-keywords'.
  1840. Interested libraries should add to this list.")
  1841. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1842. "List of TODO entry keyword sequences and their interpretation.
  1843. \\<org-mode-map>This is a list of sequences.
  1844. Each sequence starts with a symbol, either `sequence' or `type',
  1845. indicating if the keywords should be interpreted as a sequence of
  1846. action steps, or as different types of TODO items. The first
  1847. keywords are states requiring action - these states will select a headline
  1848. for inclusion into the global TODO list Org-mode produces. If one of
  1849. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1850. signify that no further action is necessary. If \"|\" is not found,
  1851. the last keyword is treated as the only DONE state of the sequence.
  1852. The command \\[org-todo] cycles an entry through these states, and one
  1853. additional state where no keyword is present. For details about this
  1854. cycling, see the manual.
  1855. TODO keywords and interpretation can also be set on a per-file basis with
  1856. the special #+SEQ_TODO and #+TYP_TODO lines.
  1857. Each keyword can optionally specify a character for fast state selection
  1858. \(in combination with the variable `org-use-fast-todo-selection')
  1859. and specifiers for state change logging, using the same syntax that
  1860. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1861. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1862. indicates to record a time stamp each time this state is selected.
  1863. Each keyword may also specify if a timestamp or a note should be
  1864. recorded when entering or leaving the state, by adding additional
  1865. characters in the parenthesis after the keyword. This looks like this:
  1866. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1867. record only the time of the state change. With X and Y being either
  1868. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1869. Y when leaving the state if and only if the *target* state does not
  1870. define X. You may omit any of the fast-selection key or X or /Y,
  1871. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1872. For backward compatibility, this variable may also be just a list
  1873. of keywords. In this case the interpretation (sequence or type) will be
  1874. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1875. :group 'org-todo
  1876. :group 'org-keywords
  1877. :type '(choice
  1878. (repeat :tag "Old syntax, just keywords"
  1879. (string :tag "Keyword"))
  1880. (repeat :tag "New syntax"
  1881. (cons
  1882. (choice
  1883. :tag "Interpretation"
  1884. ;;Quick and dirty way to see
  1885. ;;`org-todo-interpretations'. This takes the
  1886. ;;place of item arguments
  1887. :convert-widget
  1888. (lambda (widget)
  1889. (widget-put widget
  1890. :args (mapcar
  1891. #'(lambda (x)
  1892. (widget-convert
  1893. (cons 'const x)))
  1894. org-todo-interpretation-widgets))
  1895. widget))
  1896. (repeat
  1897. (string :tag "Keyword"))))))
  1898. (defvar org-todo-keywords-1 nil
  1899. "All TODO and DONE keywords active in a buffer.")
  1900. (make-variable-buffer-local 'org-todo-keywords-1)
  1901. (defvar org-todo-keywords-for-agenda nil)
  1902. (defvar org-done-keywords-for-agenda nil)
  1903. (defvar org-drawers-for-agenda nil)
  1904. (defvar org-todo-keyword-alist-for-agenda nil)
  1905. (defvar org-tag-alist-for-agenda nil)
  1906. (defvar org-agenda-contributing-files nil)
  1907. (defvar org-not-done-keywords nil)
  1908. (make-variable-buffer-local 'org-not-done-keywords)
  1909. (defvar org-done-keywords nil)
  1910. (make-variable-buffer-local 'org-done-keywords)
  1911. (defvar org-todo-heads nil)
  1912. (make-variable-buffer-local 'org-todo-heads)
  1913. (defvar org-todo-sets nil)
  1914. (make-variable-buffer-local 'org-todo-sets)
  1915. (defvar org-todo-log-states nil)
  1916. (make-variable-buffer-local 'org-todo-log-states)
  1917. (defvar org-todo-kwd-alist nil)
  1918. (make-variable-buffer-local 'org-todo-kwd-alist)
  1919. (defvar org-todo-key-alist nil)
  1920. (make-variable-buffer-local 'org-todo-key-alist)
  1921. (defvar org-todo-key-trigger nil)
  1922. (make-variable-buffer-local 'org-todo-key-trigger)
  1923. (defcustom org-todo-interpretation 'sequence
  1924. "Controls how TODO keywords are interpreted.
  1925. This variable is in principle obsolete and is only used for
  1926. backward compatibility, if the interpretation of todo keywords is
  1927. not given already in `org-todo-keywords'. See that variable for
  1928. more information."
  1929. :group 'org-todo
  1930. :group 'org-keywords
  1931. :type '(choice (const sequence)
  1932. (const type)))
  1933. (defcustom org-use-fast-todo-selection t
  1934. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1935. This variable describes if and under what circumstances the cycling
  1936. mechanism for TODO keywords will be replaced by a single-key, direct
  1937. selection scheme.
  1938. When nil, fast selection is never used.
  1939. When the symbol `prefix', it will be used when `org-todo' is called
  1940. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1941. `C-u t' in an agenda buffer.
  1942. When t, fast selection is used by default. In this case, the prefix
  1943. argument forces cycling instead.
  1944. In all cases, the special interface is only used if access keys have
  1945. actually been assigned by the user, i.e. if keywords in the configuration
  1946. are followed by a letter in parenthesis, like TODO(t)."
  1947. :group 'org-todo
  1948. :type '(choice
  1949. (const :tag "Never" nil)
  1950. (const :tag "By default" t)
  1951. (const :tag "Only with C-u C-c C-t" prefix)))
  1952. (defcustom org-provide-todo-statistics t
  1953. "Non-nil means update todo statistics after insert and toggle.
  1954. ALL-HEADLINES means update todo statistics by including headlines
  1955. with no TODO keyword as well, counting them as not done.
  1956. A list of TODO keywords means the same, but skip keywords that are
  1957. not in this list.
  1958. When this is set, todo statistics is updated in the parent of the
  1959. current entry each time a todo state is changed."
  1960. :group 'org-todo
  1961. :type '(choice
  1962. (const :tag "Yes, only for TODO entries" t)
  1963. (const :tag "Yes, including all entries" 'all-headlines)
  1964. (repeat :tag "Yes, for TODOs in this list"
  1965. (string :tag "TODO keyword"))
  1966. (other :tag "No TODO statistics" nil)))
  1967. (defcustom org-hierarchical-todo-statistics t
  1968. "Non-nil means TODO statistics covers just direct children.
  1969. When nil, all entries in the subtree are considered.
  1970. This has only an effect if `org-provide-todo-statistics' is set.
  1971. To set this to nil for only a single subtree, use a COOKIE_DATA
  1972. property and include the word \"recursive\" into the value."
  1973. :group 'org-todo
  1974. :type 'boolean)
  1975. (defcustom org-after-todo-state-change-hook nil
  1976. "Hook which is run after the state of a TODO item was changed.
  1977. The new state (a string with a TODO keyword, or nil) is available in the
  1978. Lisp variable `org-state'."
  1979. :group 'org-todo
  1980. :type 'hook)
  1981. (defvar org-blocker-hook nil
  1982. "Hook for functions that are allowed to block a state change.
  1983. Each function gets as its single argument a property list, see
  1984. `org-trigger-hook' for more information about this list.
  1985. If any of the functions in this hook returns nil, the state change
  1986. is blocked.")
  1987. (defvar org-trigger-hook nil
  1988. "Hook for functions that are triggered by a state change.
  1989. Each function gets as its single argument a property list with at least
  1990. the following elements:
  1991. (:type type-of-change :position pos-at-entry-start
  1992. :from old-state :to new-state)
  1993. Depending on the type, more properties may be present.
  1994. This mechanism is currently implemented for:
  1995. TODO state changes
  1996. ------------------
  1997. :type todo-state-change
  1998. :from previous state (keyword as a string), or nil, or a symbol
  1999. 'todo' or 'done', to indicate the general type of state.
  2000. :to new state, like in :from")
  2001. (defcustom org-enforce-todo-dependencies nil
  2002. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2003. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2004. be blocked if any prior sibling is not yet done.
  2005. Finally, if the parent is blocked because of ordered siblings of its own,
  2006. the child will also be blocked."
  2007. :set (lambda (var val)
  2008. (set var val)
  2009. (if val
  2010. (add-hook 'org-blocker-hook
  2011. 'org-block-todo-from-children-or-siblings-or-parent)
  2012. (remove-hook 'org-blocker-hook
  2013. 'org-block-todo-from-children-or-siblings-or-parent)))
  2014. :group 'org-todo
  2015. :type 'boolean)
  2016. (defcustom org-enforce-todo-checkbox-dependencies nil
  2017. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2018. When this is nil, checkboxes have no influence on switching TODO states.
  2019. When non-nil, you first need to check off all check boxes before the TODO
  2020. entry can be switched to DONE.
  2021. This variable needs to be set before org.el is loaded, and you need to
  2022. restart Emacs after a change to make the change effective. The only way
  2023. to change is while Emacs is running is through the customize interface."
  2024. :set (lambda (var val)
  2025. (set var val)
  2026. (if val
  2027. (add-hook 'org-blocker-hook
  2028. 'org-block-todo-from-checkboxes)
  2029. (remove-hook 'org-blocker-hook
  2030. 'org-block-todo-from-checkboxes)))
  2031. :group 'org-todo
  2032. :type 'boolean)
  2033. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2034. "Non-nil means inserting a TODO heading is treated as state change.
  2035. So when the command \\[org-insert-todo-heading] is used, state change
  2036. logging will apply if appropriate. When nil, the new TODO item will
  2037. be inserted directly, and no logging will take place."
  2038. :group 'org-todo
  2039. :type 'boolean)
  2040. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2041. "Non-nil means switching TODO states with S-cursor counts as state change.
  2042. This is the default behavior. However, setting this to nil allows a
  2043. convenient way to select a TODO state and bypass any logging associated
  2044. with that."
  2045. :group 'org-todo
  2046. :type 'boolean)
  2047. (defcustom org-todo-state-tags-triggers nil
  2048. "Tag changes that should be triggered by TODO state changes.
  2049. This is a list. Each entry is
  2050. (state-change (tag . flag) .......)
  2051. State-change can be a string with a state, and empty string to indicate the
  2052. state that has no TODO keyword, or it can be one of the symbols `todo'
  2053. or `done', meaning any not-done or done state, respectively."
  2054. :group 'org-todo
  2055. :group 'org-tags
  2056. :type '(repeat
  2057. (cons (choice :tag "When changing to"
  2058. (const :tag "Not-done state" todo)
  2059. (const :tag "Done state" done)
  2060. (string :tag "State"))
  2061. (repeat
  2062. (cons :tag "Tag action"
  2063. (string :tag "Tag")
  2064. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2065. (defcustom org-log-done nil
  2066. "Information to record when a task moves to the DONE state.
  2067. Possible values are:
  2068. nil Don't add anything, just change the keyword
  2069. time Add a time stamp to the task
  2070. note Prompt for a note and add it with template `org-log-note-headings'
  2071. This option can also be set with on a per-file-basis with
  2072. #+STARTUP: nologdone
  2073. #+STARTUP: logdone
  2074. #+STARTUP: lognotedone
  2075. You can have local logging settings for a subtree by setting the LOGGING
  2076. property to one or more of these keywords."
  2077. :group 'org-todo
  2078. :group 'org-progress
  2079. :type '(choice
  2080. (const :tag "No logging" nil)
  2081. (const :tag "Record CLOSED timestamp" time)
  2082. (const :tag "Record CLOSED timestamp with note." note)))
  2083. ;; Normalize old uses of org-log-done.
  2084. (cond
  2085. ((eq org-log-done t) (setq org-log-done 'time))
  2086. ((and (listp org-log-done) (memq 'done org-log-done))
  2087. (setq org-log-done 'note)))
  2088. (defcustom org-log-reschedule nil
  2089. "Information to record when the scheduling date of a tasks is modified.
  2090. Possible values are:
  2091. nil Don't add anything, just change the date
  2092. time Add a time stamp to the task
  2093. note Prompt for a note and add it with template `org-log-note-headings'
  2094. This option can also be set with on a per-file-basis with
  2095. #+STARTUP: nologreschedule
  2096. #+STARTUP: logreschedule
  2097. #+STARTUP: lognotereschedule"
  2098. :group 'org-todo
  2099. :group 'org-progress
  2100. :type '(choice
  2101. (const :tag "No logging" nil)
  2102. (const :tag "Record timestamp" time)
  2103. (const :tag "Record timestamp with note." note)))
  2104. (defcustom org-log-redeadline nil
  2105. "Information to record when the deadline date of a tasks is modified.
  2106. Possible values are:
  2107. nil Don't add anything, just change the date
  2108. time Add a time stamp to the task
  2109. note Prompt for a note and add it with template `org-log-note-headings'
  2110. This option can also be set with on a per-file-basis with
  2111. #+STARTUP: nologredeadline
  2112. #+STARTUP: logredeadline
  2113. #+STARTUP: lognoteredeadline
  2114. You can have local logging settings for a subtree by setting the LOGGING
  2115. property to one or more of these keywords."
  2116. :group 'org-todo
  2117. :group 'org-progress
  2118. :type '(choice
  2119. (const :tag "No logging" nil)
  2120. (const :tag "Record timestamp" time)
  2121. (const :tag "Record timestamp with note." note)))
  2122. (defcustom org-log-note-clock-out nil
  2123. "Non-nil means record a note when clocking out of an item.
  2124. This can also be configured on a per-file basis by adding one of
  2125. the following lines anywhere in the buffer:
  2126. #+STARTUP: lognoteclock-out
  2127. #+STARTUP: nolognoteclock-out"
  2128. :group 'org-todo
  2129. :group 'org-progress
  2130. :type 'boolean)
  2131. (defcustom org-log-done-with-time t
  2132. "Non-nil means the CLOSED time stamp will contain date and time.
  2133. When nil, only the date will be recorded."
  2134. :group 'org-progress
  2135. :type 'boolean)
  2136. (defcustom org-log-note-headings
  2137. '((done . "CLOSING NOTE %t")
  2138. (state . "State %-12s from %-12S %t")
  2139. (note . "Note taken on %t")
  2140. (reschedule . "Rescheduled from %S on %t")
  2141. (delschedule . "Not scheduled, was %S on %t")
  2142. (redeadline . "New deadline from %S on %t")
  2143. (deldeadline . "Removed deadline, was %S on %t")
  2144. (refile . "Refiled on %t")
  2145. (clock-out . ""))
  2146. "Headings for notes added to entries.
  2147. The value is an alist, with the car being a symbol indicating the note
  2148. context, and the cdr is the heading to be used. The heading may also be the
  2149. empty string.
  2150. %t in the heading will be replaced by a time stamp.
  2151. %T will be an active time stamp instead the default inactive one
  2152. %d will be replaced by a short-format time stamp.
  2153. %D will be replaced by an active short-format time stamp.
  2154. %s will be replaced by the new TODO state, in double quotes.
  2155. %S will be replaced by the old TODO state, in double quotes.
  2156. %u will be replaced by the user name.
  2157. %U will be replaced by the full user name.
  2158. In fact, it is not a good idea to change the `state' entry, because
  2159. agenda log mode depends on the format of these entries."
  2160. :group 'org-todo
  2161. :group 'org-progress
  2162. :type '(list :greedy t
  2163. (cons (const :tag "Heading when closing an item" done) string)
  2164. (cons (const :tag
  2165. "Heading when changing todo state (todo sequence only)"
  2166. state) string)
  2167. (cons (const :tag "Heading when just taking a note" note) string)
  2168. (cons (const :tag "Heading when clocking out" clock-out) string)
  2169. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2170. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2171. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2172. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2173. (cons (const :tag "Heading when refiling" refile) string)))
  2174. (unless (assq 'note org-log-note-headings)
  2175. (push '(note . "%t") org-log-note-headings))
  2176. (defcustom org-log-into-drawer nil
  2177. "Non-nil means insert state change notes and time stamps into a drawer.
  2178. When nil, state changes notes will be inserted after the headline and
  2179. any scheduling and clock lines, but not inside a drawer.
  2180. The value of this variable should be the name of the drawer to use.
  2181. LOGBOOK is proposed as the default drawer for this purpose, you can
  2182. also set this to a string to define the drawer of your choice.
  2183. A value of t is also allowed, representing \"LOGBOOK\".
  2184. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2185. will be ignored.
  2186. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2187. a subtree."
  2188. :group 'org-todo
  2189. :group 'org-progress
  2190. :type '(choice
  2191. (const :tag "Not into a drawer" nil)
  2192. (const :tag "LOGBOOK" t)
  2193. (string :tag "Other")))
  2194. (if (fboundp 'defvaralias)
  2195. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2196. (defun org-log-into-drawer ()
  2197. "Return the value of `org-log-into-drawer', but let properties overrule.
  2198. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2199. used instead of the default value."
  2200. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2201. (cond
  2202. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2203. ((equal p "t") "LOGBOOK")
  2204. (t p))))
  2205. (defcustom org-log-state-notes-insert-after-drawers nil
  2206. "Non-nil means insert state change notes after any drawers in entry.
  2207. Only the drawers that *immediately* follow the headline and the
  2208. deadline/scheduled line are skipped.
  2209. When nil, insert notes right after the heading and perhaps the line
  2210. with deadline/scheduling if present.
  2211. This variable will have no effect if `org-log-into-drawer' is
  2212. set."
  2213. :group 'org-todo
  2214. :group 'org-progress
  2215. :type 'boolean)
  2216. (defcustom org-log-states-order-reversed t
  2217. "Non-nil means the latest state note will be directly after heading.
  2218. When nil, the state change notes will be ordered according to time."
  2219. :group 'org-todo
  2220. :group 'org-progress
  2221. :type 'boolean)
  2222. (defcustom org-todo-repeat-to-state nil
  2223. "The TODO state to which a repeater should return the repeating task.
  2224. By default this is the first task in a TODO sequence, or the previous state
  2225. in a TODO_TYP set. But you can specify another task here.
  2226. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2227. :group 'org-todo
  2228. :version "24.1"
  2229. :type '(choice (const :tag "Head of sequence" nil)
  2230. (string :tag "Specific state")))
  2231. (defcustom org-log-repeat 'time
  2232. "Non-nil means record moving through the DONE state when triggering repeat.
  2233. An auto-repeating task is immediately switched back to TODO when
  2234. marked DONE. If you are not logging state changes (by adding \"@\"
  2235. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2236. record a closing note, there will be no record of the task moving
  2237. through DONE. This variable forces taking a note anyway.
  2238. nil Don't force a record
  2239. time Record a time stamp
  2240. note Prompt for a note and add it with template `org-log-note-headings'
  2241. This option can also be set with on a per-file-basis with
  2242. #+STARTUP: nologrepeat
  2243. #+STARTUP: logrepeat
  2244. #+STARTUP: lognoterepeat
  2245. You can have local logging settings for a subtree by setting the LOGGING
  2246. property to one or more of these keywords."
  2247. :group 'org-todo
  2248. :group 'org-progress
  2249. :type '(choice
  2250. (const :tag "Don't force a record" nil)
  2251. (const :tag "Force recording the DONE state" time)
  2252. (const :tag "Force recording a note with the DONE state" note)))
  2253. (defgroup org-priorities nil
  2254. "Priorities in Org-mode."
  2255. :tag "Org Priorities"
  2256. :group 'org-todo)
  2257. (defcustom org-enable-priority-commands t
  2258. "Non-nil means priority commands are active.
  2259. When nil, these commands will be disabled, so that you never accidentally
  2260. set a priority."
  2261. :group 'org-priorities
  2262. :type 'boolean)
  2263. (defcustom org-highest-priority ?A
  2264. "The highest priority of TODO items. A character like ?A, ?B etc.
  2265. Must have a smaller ASCII number than `org-lowest-priority'."
  2266. :group 'org-priorities
  2267. :type 'character)
  2268. (defcustom org-lowest-priority ?C
  2269. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2270. Must have a larger ASCII number than `org-highest-priority'."
  2271. :group 'org-priorities
  2272. :type 'character)
  2273. (defcustom org-default-priority ?B
  2274. "The default priority of TODO items.
  2275. This is the priority an item gets if no explicit priority is given.
  2276. When starting to cycle on an empty priority the first step in the cycle
  2277. depends on `org-priority-start-cycle-with-default'. The resulting first
  2278. step priority must not exceed the range from `org-highest-priority' to
  2279. `org-lowest-priority' which means that `org-default-priority' has to be
  2280. in this range exclusive or inclusive the range boundaries. Else the
  2281. first step refuses to set the default and the second will fall back
  2282. to (depending on the command used) the highest or lowest priority."
  2283. :group 'org-priorities
  2284. :type 'character)
  2285. (defcustom org-priority-start-cycle-with-default t
  2286. "Non-nil means start with default priority when starting to cycle.
  2287. When this is nil, the first step in the cycle will be (depending on the
  2288. command used) one higher or lower than the default priority.
  2289. See also `org-default-priority'."
  2290. :group 'org-priorities
  2291. :type 'boolean)
  2292. (defcustom org-get-priority-function nil
  2293. "Function to extract the priority from a string.
  2294. The string is normally the headline. If this is nil Org computes the
  2295. priority from the priority cookie like [#A] in the headline. It returns
  2296. an integer, increasing by 1000 for each priority level.
  2297. The user can set a different function here, which should take a string
  2298. as an argument and return the numeric priority."
  2299. :group 'org-priorities
  2300. :version "24.1"
  2301. :type 'function)
  2302. (defgroup org-time nil
  2303. "Options concerning time stamps and deadlines in Org-mode."
  2304. :tag "Org Time"
  2305. :group 'org)
  2306. (defcustom org-insert-labeled-timestamps-at-point nil
  2307. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2308. When nil, these labeled time stamps are forces into the second line of an
  2309. entry, just after the headline. When scheduling from the global TODO list,
  2310. the time stamp will always be forced into the second line."
  2311. :group 'org-time
  2312. :type 'boolean)
  2313. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2314. "Formats for `format-time-string' which are used for time stamps.
  2315. It is not recommended to change this constant.")
  2316. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2317. "Number of minutes to round time stamps to.
  2318. These are two values, the first applies when first creating a time stamp.
  2319. The second applies when changing it with the commands `S-up' and `S-down'.
  2320. When changing the time stamp, this means that it will change in steps
  2321. of N minutes, as given by the second value.
  2322. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2323. numbers should be factors of 60, so for example 5, 10, 15.
  2324. When this is larger than 1, you can still force an exact time stamp by using
  2325. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2326. and by using a prefix arg to `S-up/down' to specify the exact number
  2327. of minutes to shift."
  2328. :group 'org-time
  2329. :get #'(lambda (var) ; Make sure both elements are there
  2330. (if (integerp (default-value var))
  2331. (list (default-value var) 5)
  2332. (default-value var)))
  2333. :type '(list
  2334. (integer :tag "when inserting times")
  2335. (integer :tag "when modifying times")))
  2336. ;; Normalize old customizations of this variable.
  2337. (when (integerp org-time-stamp-rounding-minutes)
  2338. (setq org-time-stamp-rounding-minutes
  2339. (list org-time-stamp-rounding-minutes
  2340. org-time-stamp-rounding-minutes)))
  2341. (defcustom org-display-custom-times nil
  2342. "Non-nil means overlay custom formats over all time stamps.
  2343. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2344. To turn this on on a per-file basis, insert anywhere in the file:
  2345. #+STARTUP: customtime"
  2346. :group 'org-time
  2347. :set 'set-default
  2348. :type 'sexp)
  2349. (make-variable-buffer-local 'org-display-custom-times)
  2350. (defcustom org-time-stamp-custom-formats
  2351. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2352. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2353. These are overlaid over the default ISO format if the variable
  2354. `org-display-custom-times' is set. Time like %H:%M should be at the
  2355. end of the second format. The custom formats are also honored by export
  2356. commands, if custom time display is turned on at the time of export."
  2357. :group 'org-time
  2358. :type 'sexp)
  2359. (defun org-time-stamp-format (&optional long inactive)
  2360. "Get the right format for a time string."
  2361. (let ((f (if long (cdr org-time-stamp-formats)
  2362. (car org-time-stamp-formats))))
  2363. (if inactive
  2364. (concat "[" (substring f 1 -1) "]")
  2365. f)))
  2366. (defcustom org-time-clocksum-format "%d:%02d"
  2367. "The format string used when creating CLOCKSUM lines.
  2368. This is also used when org-mode generates a time duration."
  2369. :group 'org-time
  2370. :type 'string)
  2371. (defcustom org-time-clocksum-use-fractional nil
  2372. "If non-nil, \\[org-clock-display] uses fractional times.
  2373. org-mode generates a time duration."
  2374. :group 'org-time
  2375. :type 'boolean)
  2376. (defcustom org-time-clocksum-fractional-format "%.2f"
  2377. "The format string used when creating CLOCKSUM lines, or when
  2378. org-mode generates a time duration."
  2379. :group 'org-time
  2380. :type 'string)
  2381. (defcustom org-deadline-warning-days 14
  2382. "No. of days before expiration during which a deadline becomes active.
  2383. This variable governs the display in sparse trees and in the agenda.
  2384. When 0 or negative, it means use this number (the absolute value of it)
  2385. even if a deadline has a different individual lead time specified.
  2386. Custom commands can set this variable in the options section."
  2387. :group 'org-time
  2388. :group 'org-agenda-daily/weekly
  2389. :type 'integer)
  2390. (defcustom org-read-date-prefer-future t
  2391. "Non-nil means assume future for incomplete date input from user.
  2392. This affects the following situations:
  2393. 1. The user gives a month but not a year.
  2394. For example, if it is April and you enter \"feb 2\", this will be read
  2395. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2396. 2. The user gives a day, but no month.
  2397. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2398. read this as the third of *next* month. However, if you enter \"17\",
  2399. it will be considered as *this* month.
  2400. If you set this variable to the symbol `time', then also the following
  2401. will work:
  2402. 3. If the user gives a time.
  2403. If the time is before now, it will be interpreted as tomorrow.
  2404. Currently none of this works for ISO week specifications.
  2405. When this option is nil, the current day, month and year will always be
  2406. used as defaults.
  2407. See also `org-agenda-jump-prefer-future'."
  2408. :group 'org-time
  2409. :type '(choice
  2410. (const :tag "Never" nil)
  2411. (const :tag "Check month and day" t)
  2412. (const :tag "Check month, day, and time" time)))
  2413. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2414. "Should the agenda jump command prefer the future for incomplete dates?
  2415. The default is to do the same as configured in `org-read-date-prefer-future'.
  2416. But you can also set a deviating value here.
  2417. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2418. :group 'org-agenda
  2419. :group 'org-time
  2420. :version "24.1"
  2421. :type '(choice
  2422. (const :tag "Use org-read-date-prefer-future"
  2423. org-read-date-prefer-future)
  2424. (const :tag "Never" nil)
  2425. (const :tag "Always" t)))
  2426. (defcustom org-read-date-force-compatible-dates t
  2427. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2428. Depending on the system Emacs is running on, certain dates cannot
  2429. be represented with the type used internally to represent time.
  2430. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2431. correctly. Some systems allow for earlier dates, some for later,
  2432. some for both. One way to find out it to insert any date into an
  2433. Org buffer, putting the cursor on the year and hitting S-up and
  2434. S-down to test the range.
  2435. When this variable is set to t, the date/time prompt will not let
  2436. you specify dates outside the 1970-2037 range, so it is certain that
  2437. these dates will work in whatever version of Emacs you are
  2438. running, and also that you can move a file from one Emacs implementation
  2439. to another. WHenever Org is forcing the year for you, it will display
  2440. a message and beep.
  2441. When this variable is nil, Org will check if the date is
  2442. representable in the specific Emacs implementation you are using.
  2443. If not, it will force a year, usually the current year, and beep
  2444. to remind you. Currently this setting is not recommended because
  2445. the likelihood that you will open your Org files in an Emacs that
  2446. has limited date range is not negligible.
  2447. A workaround for this problem is to use diary sexp dates for time
  2448. stamps outside of this range."
  2449. :group 'org-time
  2450. :version "24.1"
  2451. :type 'boolean)
  2452. (defcustom org-read-date-display-live t
  2453. "Non-nil means display current interpretation of date prompt live.
  2454. This display will be in an overlay, in the minibuffer."
  2455. :group 'org-time
  2456. :type 'boolean)
  2457. (defcustom org-read-date-popup-calendar t
  2458. "Non-nil means pop up a calendar when prompting for a date.
  2459. In the calendar, the date can be selected with mouse-1. However, the
  2460. minibuffer will also be active, and you can simply enter the date as well.
  2461. When nil, only the minibuffer will be available."
  2462. :group 'org-time
  2463. :type 'boolean)
  2464. (if (fboundp 'defvaralias)
  2465. (defvaralias 'org-popup-calendar-for-date-prompt
  2466. 'org-read-date-popup-calendar))
  2467. (defcustom org-read-date-minibuffer-setup-hook nil
  2468. "Hook to be used to set up keys for the date/time interface.
  2469. Add key definitions to `minibuffer-local-map', which will be a temporary
  2470. copy."
  2471. :group 'org-time
  2472. :type 'hook)
  2473. (defcustom org-extend-today-until 0
  2474. "The hour when your day really ends. Must be an integer.
  2475. This has influence for the following applications:
  2476. - When switching the agenda to \"today\". It it is still earlier than
  2477. the time given here, the day recognized as TODAY is actually yesterday.
  2478. - When a date is read from the user and it is still before the time given
  2479. here, the current date and time will be assumed to be yesterday, 23:59.
  2480. Also, timestamps inserted in capture templates follow this rule.
  2481. IMPORTANT: This is a feature whose implementation is and likely will
  2482. remain incomplete. Really, it is only here because past midnight seems to
  2483. be the favorite working time of John Wiegley :-)"
  2484. :group 'org-time
  2485. :type 'integer)
  2486. (defcustom org-use-effective-time nil
  2487. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2488. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2489. \"effective time\" of any timestamps between midnight and 8am will be
  2490. 23:59 of the previous day."
  2491. :group 'org-time
  2492. :version "24.1"
  2493. :type 'boolean)
  2494. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2495. "When non-nil, use the last clock out time for `org-todo'.
  2496. Note that this option has precedence over the combined use of
  2497. `org-use-effective-time' and `org-extend-today-until'."
  2498. :group 'org-time
  2499. ;; :version "24.3"
  2500. :type 'boolean)
  2501. (defcustom org-edit-timestamp-down-means-later nil
  2502. "Non-nil means S-down will increase the time in a time stamp.
  2503. When nil, S-up will increase."
  2504. :group 'org-time
  2505. :type 'boolean)
  2506. (defcustom org-calendar-follow-timestamp-change t
  2507. "Non-nil means make the calendar window follow timestamp changes.
  2508. When a timestamp is modified and the calendar window is visible, it will be
  2509. moved to the new date."
  2510. :group 'org-time
  2511. :type 'boolean)
  2512. (defgroup org-tags nil
  2513. "Options concerning tags in Org-mode."
  2514. :tag "Org Tags"
  2515. :group 'org)
  2516. (defcustom org-tag-alist nil
  2517. "List of tags allowed in Org-mode files.
  2518. When this list is nil, Org-mode will base TAG input on what is already in the
  2519. buffer.
  2520. The value of this variable is an alist, the car of each entry must be a
  2521. keyword as a string, the cdr may be a character that is used to select
  2522. that tag through the fast-tag-selection interface.
  2523. See the manual for details."
  2524. :group 'org-tags
  2525. :type '(repeat
  2526. (choice
  2527. (cons (string :tag "Tag name")
  2528. (character :tag "Access char"))
  2529. (list :tag "Start radio group"
  2530. (const :startgroup)
  2531. (option (string :tag "Group description")))
  2532. (list :tag "End radio group"
  2533. (const :endgroup)
  2534. (option (string :tag "Group description")))
  2535. (const :tag "New line" (:newline)))))
  2536. (defcustom org-tag-persistent-alist nil
  2537. "List of tags that will always appear in all Org-mode files.
  2538. This is in addition to any in buffer settings or customizations
  2539. of `org-tag-alist'.
  2540. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2541. The value of this variable is an alist, the car of each entry must be a
  2542. keyword as a string, the cdr may be a character that is used to select
  2543. that tag through the fast-tag-selection interface.
  2544. See the manual for details.
  2545. To disable these tags on a per-file basis, insert anywhere in the file:
  2546. #+STARTUP: noptag"
  2547. :group 'org-tags
  2548. :type '(repeat
  2549. (choice
  2550. (cons (string :tag "Tag name")
  2551. (character :tag "Access char"))
  2552. (const :tag "Start radio group" (:startgroup))
  2553. (const :tag "End radio group" (:endgroup))
  2554. (const :tag "New line" (:newline)))))
  2555. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2556. "If non-nil, always offer completion for all tags of all agenda files.
  2557. Instead of customizing this variable directly, you might want to
  2558. set it locally for capture buffers, because there no list of
  2559. tags in that file can be created dynamically (there are none).
  2560. (add-hook 'org-capture-mode-hook
  2561. (lambda ()
  2562. (set (make-local-variable
  2563. 'org-complete-tags-always-offer-all-agenda-tags)
  2564. t)))"
  2565. :group 'org-tags
  2566. :version "24.1"
  2567. :type 'boolean)
  2568. (defvar org-file-tags nil
  2569. "List of tags that can be inherited by all entries in the file.
  2570. The tags will be inherited if the variable `org-use-tag-inheritance'
  2571. says they should be.
  2572. This variable is populated from #+FILETAGS lines.")
  2573. (defcustom org-use-fast-tag-selection 'auto
  2574. "Non-nil means use fast tag selection scheme.
  2575. This is a special interface to select and deselect tags with single keys.
  2576. When nil, fast selection is never used.
  2577. When the symbol `auto', fast selection is used if and only if selection
  2578. characters for tags have been configured, either through the variable
  2579. `org-tag-alist' or through a #+TAGS line in the buffer.
  2580. When t, fast selection is always used and selection keys are assigned
  2581. automatically if necessary."
  2582. :group 'org-tags
  2583. :type '(choice
  2584. (const :tag "Always" t)
  2585. (const :tag "Never" nil)
  2586. (const :tag "When selection characters are configured" 'auto)))
  2587. (defcustom org-fast-tag-selection-single-key nil
  2588. "Non-nil means fast tag selection exits after first change.
  2589. When nil, you have to press RET to exit it.
  2590. During fast tag selection, you can toggle this flag with `C-c'.
  2591. This variable can also have the value `expert'. In this case, the window
  2592. displaying the tags menu is not even shown, until you press C-c again."
  2593. :group 'org-tags
  2594. :type '(choice
  2595. (const :tag "No" nil)
  2596. (const :tag "Yes" t)
  2597. (const :tag "Expert" expert)))
  2598. (defvar org-fast-tag-selection-include-todo nil
  2599. "Non-nil means fast tags selection interface will also offer TODO states.
  2600. This is an undocumented feature, you should not rely on it.")
  2601. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2602. "The column to which tags should be indented in a headline.
  2603. If this number is positive, it specifies the column. If it is negative,
  2604. it means that the tags should be flushright to that column. For example,
  2605. -80 works well for a normal 80 character screen.
  2606. When 0, place tags directly after headline text, with only one space in
  2607. between."
  2608. :group 'org-tags
  2609. :type 'integer)
  2610. (defcustom org-auto-align-tags t
  2611. "Non-nil keeps tags aligned when modifying headlines.
  2612. Some operations (i.e. demoting) change the length of a headline and
  2613. therefore shift the tags around. With this option turned on, after
  2614. each such operation the tags are again aligned to `org-tags-column'."
  2615. :group 'org-tags
  2616. :type 'boolean)
  2617. (defcustom org-use-tag-inheritance t
  2618. "Non-nil means tags in levels apply also for sublevels.
  2619. When nil, only the tags directly given in a specific line apply there.
  2620. This may also be a list of tags that should be inherited, or a regexp that
  2621. matches tags that should be inherited. Additional control is possible
  2622. with the variable `org-tags-exclude-from-inheritance' which gives an
  2623. explicit list of tags to be excluded from inheritance., even if the value of
  2624. `org-use-tag-inheritance' would select it for inheritance.
  2625. If this option is t, a match early-on in a tree can lead to a large
  2626. number of matches in the subtree when constructing the agenda or creating
  2627. a sparse tree. If you only want to see the first match in a tree during
  2628. a search, check out the variable `org-tags-match-list-sublevels'."
  2629. :group 'org-tags
  2630. :type '(choice
  2631. (const :tag "Not" nil)
  2632. (const :tag "Always" t)
  2633. (repeat :tag "Specific tags" (string :tag "Tag"))
  2634. (regexp :tag "Tags matched by regexp")))
  2635. (defcustom org-tags-exclude-from-inheritance nil
  2636. "List of tags that should never be inherited.
  2637. This is a way to exclude a few tags from inheritance. For way to do
  2638. the opposite, to actively allow inheritance for selected tags,
  2639. see the variable `org-use-tag-inheritance'."
  2640. :group 'org-tags
  2641. :type '(repeat (string :tag "Tag")))
  2642. (defun org-tag-inherit-p (tag)
  2643. "Check if TAG is one that should be inherited."
  2644. (cond
  2645. ((member tag org-tags-exclude-from-inheritance) nil)
  2646. ((eq org-use-tag-inheritance t) t)
  2647. ((not org-use-tag-inheritance) nil)
  2648. ((stringp org-use-tag-inheritance)
  2649. (string-match org-use-tag-inheritance tag))
  2650. ((listp org-use-tag-inheritance)
  2651. (member tag org-use-tag-inheritance))
  2652. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2653. (defcustom org-tags-match-list-sublevels t
  2654. "Non-nil means list also sublevels of headlines matching a search.
  2655. This variable applies to tags/property searches, and also to stuck
  2656. projects because this search is based on a tags match as well.
  2657. When set to the symbol `indented', sublevels are indented with
  2658. leading dots.
  2659. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2660. the sublevels of a headline matching a tag search often also match
  2661. the same search. Listing all of them can create very long lists.
  2662. Setting this variable to nil causes subtrees of a match to be skipped.
  2663. This variable is semi-obsolete and probably should always be true. It
  2664. is better to limit inheritance to certain tags using the variables
  2665. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2666. :group 'org-tags
  2667. :type '(choice
  2668. (const :tag "No, don't list them" nil)
  2669. (const :tag "Yes, do list them" t)
  2670. (const :tag "List them, indented with leading dots" indented)))
  2671. (defcustom org-tags-sort-function nil
  2672. "When set, tags are sorted using this function as a comparator."
  2673. :group 'org-tags
  2674. :type '(choice
  2675. (const :tag "No sorting" nil)
  2676. (const :tag "Alphabetical" string<)
  2677. (const :tag "Reverse alphabetical" string>)
  2678. (function :tag "Custom function" nil)))
  2679. (defvar org-tags-history nil
  2680. "History of minibuffer reads for tags.")
  2681. (defvar org-last-tags-completion-table nil
  2682. "The last used completion table for tags.")
  2683. (defvar org-after-tags-change-hook nil
  2684. "Hook that is run after the tags in a line have changed.")
  2685. (defgroup org-properties nil
  2686. "Options concerning properties in Org-mode."
  2687. :tag "Org Properties"
  2688. :group 'org)
  2689. (defcustom org-property-format "%-10s %s"
  2690. "How property key/value pairs should be formatted by `indent-line'.
  2691. When `indent-line' hits a property definition, it will format the line
  2692. according to this format, mainly to make sure that the values are
  2693. lined-up with respect to each other."
  2694. :group 'org-properties
  2695. :type 'string)
  2696. (defcustom org-properties-postprocess-alist nil
  2697. "Alist of properties and functions to adjust inserted values.
  2698. Elements of this alist must be of the form
  2699. ([string] [function])
  2700. where [string] must be a property name and [function] must be a
  2701. lambda expression: this lambda expression must take one argument,
  2702. the value to adjust, and return the new value as a string.
  2703. For example, this element will allow the property \"Remaining\"
  2704. to be updated wrt the relation between the \"Effort\" property
  2705. and the clock summary:
  2706. ((\"Remaining\" (lambda(value)
  2707. (let ((clocksum (org-clock-sum-current-item))
  2708. (effort (org-duration-string-to-minutes
  2709. (org-entry-get (point) \"Effort\"))))
  2710. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2711. :group 'org-properties
  2712. :version "24.1"
  2713. :type '(alist :key-type (string :tag "Property")
  2714. :value-type (function :tag "Function")))
  2715. (defcustom org-use-property-inheritance nil
  2716. "Non-nil means properties apply also for sublevels.
  2717. This setting is chiefly used during property searches. Turning it on can
  2718. cause significant overhead when doing a search, which is why it is not
  2719. on by default.
  2720. When nil, only the properties directly given in the current entry count.
  2721. When t, every property is inherited. The value may also be a list of
  2722. properties that should have inheritance, or a regular expression matching
  2723. properties that should be inherited.
  2724. However, note that some special properties use inheritance under special
  2725. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2726. and the properties ending in \"_ALL\" when they are used as descriptor
  2727. for valid values of a property.
  2728. Note for programmers:
  2729. When querying an entry with `org-entry-get', you can control if inheritance
  2730. should be used. By default, `org-entry-get' looks only at the local
  2731. properties. You can request inheritance by setting the inherit argument
  2732. to t (to force inheritance) or to `selective' (to respect the setting
  2733. in this variable)."
  2734. :group 'org-properties
  2735. :type '(choice
  2736. (const :tag "Not" nil)
  2737. (const :tag "Always" t)
  2738. (repeat :tag "Specific properties" (string :tag "Property"))
  2739. (regexp :tag "Properties matched by regexp")))
  2740. (defun org-property-inherit-p (property)
  2741. "Check if PROPERTY is one that should be inherited."
  2742. (cond
  2743. ((eq org-use-property-inheritance t) t)
  2744. ((not org-use-property-inheritance) nil)
  2745. ((stringp org-use-property-inheritance)
  2746. (string-match org-use-property-inheritance property))
  2747. ((listp org-use-property-inheritance)
  2748. (member property org-use-property-inheritance))
  2749. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2750. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2751. "The default column format, if no other format has been defined.
  2752. This variable can be set on the per-file basis by inserting a line
  2753. #+COLUMNS: %25ITEM ....."
  2754. :group 'org-properties
  2755. :type 'string)
  2756. (defcustom org-columns-ellipses ".."
  2757. "The ellipses to be used when a field in column view is truncated.
  2758. When this is the empty string, as many characters as possible are shown,
  2759. but then there will be no visual indication that the field has been truncated.
  2760. When this is a string of length N, the last N characters of a truncated
  2761. field are replaced by this string. If the column is narrower than the
  2762. ellipses string, only part of the ellipses string will be shown."
  2763. :group 'org-properties
  2764. :type 'string)
  2765. (defcustom org-columns-modify-value-for-display-function nil
  2766. "Function that modifies values for display in column view.
  2767. For example, it can be used to cut out a certain part from a time stamp.
  2768. The function must take 2 arguments:
  2769. column-title The title of the column (*not* the property name)
  2770. value The value that should be modified.
  2771. The function should return the value that should be displayed,
  2772. or nil if the normal value should be used."
  2773. :group 'org-properties
  2774. :type 'function)
  2775. (defcustom org-effort-property "Effort"
  2776. "The property that is being used to keep track of effort estimates.
  2777. Effort estimates given in this property need to have the format H:MM."
  2778. :group 'org-properties
  2779. :group 'org-progress
  2780. :type '(string :tag "Property"))
  2781. (defconst org-global-properties-fixed
  2782. '(("VISIBILITY_ALL" . "folded children content all")
  2783. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2784. "List of property/value pairs that can be inherited by any entry.
  2785. These are fixed values, for the preset properties. The user variable
  2786. that can be used to add to this list is `org-global-properties'.
  2787. The entries in this list are cons cells where the car is a property
  2788. name and cdr is a string with the value. If the value represents
  2789. multiple items like an \"_ALL\" property, separate the items by
  2790. spaces.")
  2791. (defcustom org-global-properties nil
  2792. "List of property/value pairs that can be inherited by any entry.
  2793. This list will be combined with the constant `org-global-properties-fixed'.
  2794. The entries in this list are cons cells where the car is a property
  2795. name and cdr is a string with the value.
  2796. You can set buffer-local values for the same purpose in the variable
  2797. `org-file-properties' this by adding lines like
  2798. #+PROPERTY: NAME VALUE"
  2799. :group 'org-properties
  2800. :type '(repeat
  2801. (cons (string :tag "Property")
  2802. (string :tag "Value"))))
  2803. (defvar org-file-properties nil
  2804. "List of property/value pairs that can be inherited by any entry.
  2805. Valid for the current buffer.
  2806. This variable is populated from #+PROPERTY lines.")
  2807. (make-variable-buffer-local 'org-file-properties)
  2808. (defgroup org-agenda nil
  2809. "Options concerning agenda views in Org-mode."
  2810. :tag "Org Agenda"
  2811. :group 'org)
  2812. (defvar org-category nil
  2813. "Variable used by org files to set a category for agenda display.
  2814. Such files should use a file variable to set it, for example
  2815. # -*- mode: org; org-category: \"ELisp\"
  2816. or contain a special line
  2817. #+CATEGORY: ELisp
  2818. If the file does not specify a category, then file's base name
  2819. is used instead.")
  2820. (make-variable-buffer-local 'org-category)
  2821. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2822. (defcustom org-agenda-files nil
  2823. "The files to be used for agenda display.
  2824. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2825. \\[org-remove-file]. You can also use customize to edit the list.
  2826. If an entry is a directory, all files in that directory that are matched by
  2827. `org-agenda-file-regexp' will be part of the file list.
  2828. If the value of the variable is not a list but a single file name, then
  2829. the list of agenda files is actually stored and maintained in that file, one
  2830. agenda file per line. In this file paths can be given relative to
  2831. `org-directory'. Tilde expansion and environment variable substitution
  2832. are also made."
  2833. :group 'org-agenda
  2834. :type '(choice
  2835. (repeat :tag "List of files and directories" file)
  2836. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2837. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2838. "Regular expression to match files for `org-agenda-files'.
  2839. If any element in the list in that variable contains a directory instead
  2840. of a normal file, all files in that directory that are matched by this
  2841. regular expression will be included."
  2842. :group 'org-agenda
  2843. :type 'regexp)
  2844. (defcustom org-agenda-text-search-extra-files nil
  2845. "List of extra files to be searched by text search commands.
  2846. These files will be search in addition to the agenda files by the
  2847. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2848. Note that these files will only be searched for text search commands,
  2849. not for the other agenda views like todo lists, tag searches or the weekly
  2850. agenda. This variable is intended to list notes and possibly archive files
  2851. that should also be searched by these two commands.
  2852. In fact, if the first element in the list is the symbol `agenda-archives',
  2853. than all archive files of all agenda files will be added to the search
  2854. scope."
  2855. :group 'org-agenda
  2856. :type '(set :greedy t
  2857. (const :tag "Agenda Archives" agenda-archives)
  2858. (repeat :inline t (file))))
  2859. (if (fboundp 'defvaralias)
  2860. (defvaralias 'org-agenda-multi-occur-extra-files
  2861. 'org-agenda-text-search-extra-files))
  2862. (defcustom org-agenda-skip-unavailable-files nil
  2863. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2864. A nil value means to remove them, after a query, from the list."
  2865. :group 'org-agenda
  2866. :type 'boolean)
  2867. (defcustom org-calendar-to-agenda-key [?c]
  2868. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2869. The command `org-calendar-goto-agenda' will be bound to this key. The
  2870. default is the character `c' because then `c' can be used to switch back and
  2871. forth between agenda and calendar."
  2872. :group 'org-agenda
  2873. :type 'sexp)
  2874. (defcustom org-calendar-insert-diary-entry-key [?i]
  2875. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2876. This option is irrelevant until `org-agenda-diary-file' has been configured
  2877. to point to an Org-mode file. When that is the case, the command
  2878. `org-agenda-diary-entry' will be bound to the key given here, by default
  2879. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2880. if you want to continue doing this, you need to change this to a different
  2881. key."
  2882. :group 'org-agenda
  2883. :type 'sexp)
  2884. (defcustom org-agenda-diary-file 'diary-file
  2885. "File to which to add new entries with the `i' key in agenda and calendar.
  2886. When this is the symbol `diary-file', the functionality in the Emacs
  2887. calendar will be used to add entries to the `diary-file'. But when this
  2888. points to a file, `org-agenda-diary-entry' will be used instead."
  2889. :group 'org-agenda
  2890. :type '(choice
  2891. (const :tag "The standard Emacs diary file" diary-file)
  2892. (file :tag "Special Org file diary entries")))
  2893. (eval-after-load "calendar"
  2894. '(progn
  2895. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2896. 'org-calendar-goto-agenda)
  2897. (add-hook 'calendar-mode-hook
  2898. (lambda ()
  2899. (unless (eq org-agenda-diary-file 'diary-file)
  2900. (define-key calendar-mode-map
  2901. org-calendar-insert-diary-entry-key
  2902. 'org-agenda-diary-entry))))))
  2903. (defgroup org-latex nil
  2904. "Options for embedding LaTeX code into Org-mode."
  2905. :tag "Org LaTeX"
  2906. :group 'org)
  2907. (defcustom org-format-latex-options
  2908. '(:foreground default :background default :scale 1.0
  2909. :html-foreground "Black" :html-background "Transparent"
  2910. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2911. "Options for creating images from LaTeX fragments.
  2912. This is a property list with the following properties:
  2913. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2914. `default' means use the foreground of the default face.
  2915. `auto' means use the foreground from the text face.
  2916. :background the background color, or \"Transparent\".
  2917. `default' means use the background of the default face.
  2918. `auto' means use the background from the text face.
  2919. :scale a scaling factor for the size of the images, to get more pixels
  2920. :html-foreground, :html-background, :html-scale
  2921. the same numbers for HTML export.
  2922. :matchers a list indicating which matchers should be used to
  2923. find LaTeX fragments. Valid members of this list are:
  2924. \"begin\" find environments
  2925. \"$1\" find single characters surrounded by $.$
  2926. \"$\" find math expressions surrounded by $...$
  2927. \"$$\" find math expressions surrounded by $$....$$
  2928. \"\\(\" find math expressions surrounded by \\(...\\)
  2929. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2930. :group 'org-latex
  2931. :type 'plist)
  2932. (defcustom org-format-latex-signal-error t
  2933. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2934. When nil, just push out a message."
  2935. :group 'org-latex
  2936. :version "24.1"
  2937. :type 'boolean)
  2938. (defcustom org-latex-to-mathml-jar-file nil
  2939. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2940. Use this to specify additional executable file say a jar file.
  2941. When using MathToWeb as the converter, specify the full-path to
  2942. your mathtoweb.jar file."
  2943. :group 'org-latex
  2944. :version "24.1"
  2945. :type '(choice
  2946. (const :tag "None" nil)
  2947. (file :tag "JAR file" :must-match t)))
  2948. (defcustom org-latex-to-mathml-convert-command nil
  2949. "Command to convert LaTeX fragments to MathML.
  2950. Replace format-specifiers in the command as noted below and use
  2951. `shell-command' to convert LaTeX to MathML.
  2952. %j: Executable file in fully expanded form as specified by
  2953. `org-latex-to-mathml-jar-file'.
  2954. %I: Input LaTeX file in fully expanded form
  2955. %o: Output MathML file
  2956. This command is used by `org-create-math-formula'.
  2957. When using MathToWeb as the converter, set this to
  2958. \"java -jar %j -unicode -force -df %o %I\"."
  2959. :group 'org-latex
  2960. :version "24.1"
  2961. :type '(choice
  2962. (const :tag "None" nil)
  2963. (string :tag "\nShell command")))
  2964. (defcustom org-latex-create-formula-image-program 'dvipng
  2965. "Program to convert LaTeX fragments with.
  2966. dvipng Process the LaTeX fragments to dvi file, then convert
  2967. dvi files to png files using dvipng.
  2968. This will also include processing of non-math environments.
  2969. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2970. to convert pdf files to png files"
  2971. :group 'org-latex
  2972. :version "24.1"
  2973. :type '(choice
  2974. (const :tag "dvipng" dvipng)
  2975. (const :tag "imagemagick" imagemagick)))
  2976. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  2977. "Path to store latex preview images. A relative path here creates many
  2978. directories relative to the processed org files paths. An absolute path
  2979. puts all preview images at the same place."
  2980. :group 'org-latex
  2981. :version "24.3"
  2982. :type 'string)
  2983. (defun org-format-latex-mathml-available-p ()
  2984. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2985. (save-match-data
  2986. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2987. org-latex-to-mathml-convert-command)
  2988. (let ((executable (car (split-string
  2989. org-latex-to-mathml-convert-command))))
  2990. (when (executable-find executable)
  2991. (if (string-match
  2992. "%j" org-latex-to-mathml-convert-command)
  2993. (file-readable-p org-latex-to-mathml-jar-file)
  2994. t))))))
  2995. (defcustom org-format-latex-header "\\documentclass{article}
  2996. \\usepackage[usenames]{color}
  2997. \\usepackage{amsmath}
  2998. \\usepackage[mathscr]{eucal}
  2999. \\pagestyle{empty} % do not remove
  3000. \[PACKAGES]
  3001. \[DEFAULT-PACKAGES]
  3002. % The settings below are copied from fullpage.sty
  3003. \\setlength{\\textwidth}{\\paperwidth}
  3004. \\addtolength{\\textwidth}{-3cm}
  3005. \\setlength{\\oddsidemargin}{1.5cm}
  3006. \\addtolength{\\oddsidemargin}{-2.54cm}
  3007. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3008. \\setlength{\\textheight}{\\paperheight}
  3009. \\addtolength{\\textheight}{-\\headheight}
  3010. \\addtolength{\\textheight}{-\\headsep}
  3011. \\addtolength{\\textheight}{-\\footskip}
  3012. \\addtolength{\\textheight}{-3cm}
  3013. \\setlength{\\topmargin}{1.5cm}
  3014. \\addtolength{\\topmargin}{-2.54cm}"
  3015. "The document header used for processing LaTeX fragments.
  3016. It is imperative that this header make sure that no page number
  3017. appears on the page. The package defined in the variables
  3018. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3019. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3020. will be appended."
  3021. :group 'org-latex
  3022. :type 'string)
  3023. (defvar org-format-latex-header-extra nil)
  3024. (defun org-set-packages-alist (var val)
  3025. "Set the packages alist and make sure it has 3 elements per entry."
  3026. (set var (mapcar (lambda (x)
  3027. (if (and (consp x) (= (length x) 2))
  3028. (list (car x) (nth 1 x) t)
  3029. x))
  3030. val)))
  3031. (defun org-get-packages-alist (var)
  3032. "Get the packages alist and make sure it has 3 elements per entry."
  3033. (mapcar (lambda (x)
  3034. (if (and (consp x) (= (length x) 2))
  3035. (list (car x) (nth 1 x) t)
  3036. x))
  3037. (default-value var)))
  3038. ;; The following variables are defined here because is it also used
  3039. ;; when formatting latex fragments. Originally it was part of the
  3040. ;; LaTeX exporter, which is why the name includes "export".
  3041. (defcustom org-export-latex-default-packages-alist
  3042. '(("AUTO" "inputenc" t)
  3043. ("T1" "fontenc" t)
  3044. ("" "fixltx2e" nil)
  3045. ("" "graphicx" t)
  3046. ("" "longtable" nil)
  3047. ("" "float" nil)
  3048. ("" "wrapfig" nil)
  3049. ("" "soul" t)
  3050. ("" "textcomp" t)
  3051. ("" "marvosym" t)
  3052. ("" "wasysym" t)
  3053. ("" "latexsym" t)
  3054. ("" "amssymb" t)
  3055. ("" "hyperref" nil)
  3056. "\\tolerance=1000"
  3057. )
  3058. "Alist of default packages to be inserted in the header.
  3059. Change this only if one of the packages here causes an incompatibility
  3060. with another package you are using.
  3061. The packages in this list are needed by one part or another of Org-mode
  3062. to function properly.
  3063. - inputenc, fontenc: for basic font and character selection
  3064. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3065. for interpreting the entities in `org-entities'. You can skip some of these
  3066. packages if you don't use any of the symbols in it.
  3067. - graphicx: for including images
  3068. - float, wrapfig: for figure placement
  3069. - longtable: for long tables
  3070. - hyperref: for cross references
  3071. Therefore you should not modify this variable unless you know what you
  3072. are doing. The one reason to change it anyway is that you might be loading
  3073. some other package that conflicts with one of the default packages.
  3074. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3075. If SNIPPET-FLAG is t, the package also needs to be included when
  3076. compiling LaTeX snippets into images for inclusion into HTML."
  3077. :group 'org-export-latex
  3078. :set 'org-set-packages-alist
  3079. :get 'org-get-packages-alist
  3080. :version "24.1"
  3081. :type '(repeat
  3082. (choice
  3083. (list :tag "options/package pair"
  3084. (string :tag "options")
  3085. (string :tag "package")
  3086. (boolean :tag "Snippet"))
  3087. (string :tag "A line of LaTeX"))))
  3088. (defcustom org-export-latex-packages-alist nil
  3089. "Alist of packages to be inserted in every LaTeX header.
  3090. These will be inserted after `org-export-latex-default-packages-alist'.
  3091. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3092. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3093. turning LaTeX snippets into images for inclusion into HTML.
  3094. Make sure that you only list packages here which:
  3095. - you want in every file
  3096. - do not conflict with the default packages in
  3097. `org-export-latex-default-packages-alist'
  3098. - do not conflict with the setup in `org-format-latex-header'."
  3099. :group 'org-export-latex
  3100. :set 'org-set-packages-alist
  3101. :get 'org-get-packages-alist
  3102. :type '(repeat
  3103. (choice
  3104. (list :tag "options/package pair"
  3105. (string :tag "options")
  3106. (string :tag "package")
  3107. (boolean :tag "Snippet"))
  3108. (string :tag "A line of LaTeX"))))
  3109. (defgroup org-appearance nil
  3110. "Settings for Org-mode appearance."
  3111. :tag "Org Appearance"
  3112. :group 'org)
  3113. (defcustom org-level-color-stars-only nil
  3114. "Non-nil means fontify only the stars in each headline.
  3115. When nil, the entire headline is fontified.
  3116. Changing it requires restart of `font-lock-mode' to become effective
  3117. also in regions already fontified."
  3118. :group 'org-appearance
  3119. :type 'boolean)
  3120. (defcustom org-hide-leading-stars nil
  3121. "Non-nil means hide the first N-1 stars in a headline.
  3122. This works by using the face `org-hide' for these stars. This
  3123. face is white for a light background, and black for a dark
  3124. background. You may have to customize the face `org-hide' to
  3125. make this work.
  3126. Changing it requires restart of `font-lock-mode' to become effective
  3127. also in regions already fontified.
  3128. You may also set this on a per-file basis by adding one of the following
  3129. lines to the buffer:
  3130. #+STARTUP: hidestars
  3131. #+STARTUP: showstars"
  3132. :group 'org-appearance
  3133. :type 'boolean)
  3134. (defcustom org-hidden-keywords nil
  3135. "List of symbols corresponding to keywords to be hidden the org buffer.
  3136. For example, a value '(title) for this list will make the document's title
  3137. appear in the buffer without the initial #+TITLE: keyword."
  3138. :group 'org-appearance
  3139. :version "24.1"
  3140. :type '(set (const :tag "#+AUTHOR" author)
  3141. (const :tag "#+DATE" date)
  3142. (const :tag "#+EMAIL" email)
  3143. (const :tag "#+TITLE" title)))
  3144. (defcustom org-custom-properties nil
  3145. "List of properties (as strings) with a special meaning.
  3146. The default use of these custom properties is to let the user
  3147. hide them with `org-toggle-custom-properties-visibility'."
  3148. :group 'org-properties
  3149. :group 'org-appearance
  3150. :version "24.3"
  3151. :type '(repeat (string :tag "Property Name")))
  3152. (defcustom org-fontify-done-headline nil
  3153. "Non-nil means change the face of a headline if it is marked DONE.
  3154. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3155. When this is non-nil, the headline after the keyword is set to the
  3156. `org-headline-done' as an additional indication."
  3157. :group 'org-appearance
  3158. :type 'boolean)
  3159. (defcustom org-fontify-emphasized-text t
  3160. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3161. Changing this variable requires a restart of Emacs to take effect."
  3162. :group 'org-appearance
  3163. :type 'boolean)
  3164. (defcustom org-fontify-whole-heading-line nil
  3165. "Non-nil means fontify the whole line for headings.
  3166. This is useful when setting a background color for the
  3167. org-level-* faces."
  3168. :group 'org-appearance
  3169. :type 'boolean)
  3170. (defcustom org-highlight-latex-fragments-and-specials nil
  3171. "Non-nil means fontify what is treated specially by the exporters."
  3172. :group 'org-appearance
  3173. :type 'boolean)
  3174. (defcustom org-hide-emphasis-markers nil
  3175. "Non-nil mean font-lock should hide the emphasis marker characters."
  3176. :group 'org-appearance
  3177. :type 'boolean)
  3178. (defcustom org-pretty-entities nil
  3179. "Non-nil means show entities as UTF8 characters.
  3180. When nil, the \\name form remains in the buffer."
  3181. :group 'org-appearance
  3182. :version "24.1"
  3183. :type 'boolean)
  3184. (defcustom org-pretty-entities-include-sub-superscripts t
  3185. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3186. :group 'org-appearance
  3187. :version "24.1"
  3188. :type 'boolean)
  3189. (defvar org-emph-re nil
  3190. "Regular expression for matching emphasis.
  3191. After a match, the match groups contain these elements:
  3192. 0 The match of the full regular expression, including the characters
  3193. before and after the proper match
  3194. 1 The character before the proper match, or empty at beginning of line
  3195. 2 The proper match, including the leading and trailing markers
  3196. 3 The leading marker like * or /, indicating the type of highlighting
  3197. 4 The text between the emphasis markers, not including the markers
  3198. 5 The character after the match, empty at the end of a line")
  3199. (defvar org-verbatim-re nil
  3200. "Regular expression for matching verbatim text.")
  3201. (defvar org-emphasis-regexp-components) ; defined just below
  3202. (defvar org-emphasis-alist) ; defined just below
  3203. (defun org-set-emph-re (var val)
  3204. "Set variable and compute the emphasis regular expression."
  3205. (set var val)
  3206. (when (and (boundp 'org-emphasis-alist)
  3207. (boundp 'org-emphasis-regexp-components)
  3208. org-emphasis-alist org-emphasis-regexp-components)
  3209. (let* ((e org-emphasis-regexp-components)
  3210. (pre (car e))
  3211. (post (nth 1 e))
  3212. (border (nth 2 e))
  3213. (body (nth 3 e))
  3214. (nl (nth 4 e))
  3215. (body1 (concat body "*?"))
  3216. (markers (mapconcat 'car org-emphasis-alist ""))
  3217. (vmarkers (mapconcat
  3218. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3219. org-emphasis-alist "")))
  3220. ;; make sure special characters appear at the right position in the class
  3221. (if (string-match "\\^" markers)
  3222. (setq markers (concat (replace-match "" t t markers) "^")))
  3223. (if (string-match "-" markers)
  3224. (setq markers (concat (replace-match "" t t markers) "-")))
  3225. (if (string-match "\\^" vmarkers)
  3226. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3227. (if (string-match "-" vmarkers)
  3228. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3229. (if (> nl 0)
  3230. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3231. (int-to-string nl) "\\}")))
  3232. ;; Make the regexp
  3233. (setq org-emph-re
  3234. (concat "\\([" pre "]\\|^\\)"
  3235. "\\("
  3236. "\\([" markers "]\\)"
  3237. "\\("
  3238. "[^" border "]\\|"
  3239. "[^" border "]"
  3240. body1
  3241. "[^" border "]"
  3242. "\\)"
  3243. "\\3\\)"
  3244. "\\([" post "]\\|$\\)"))
  3245. (setq org-verbatim-re
  3246. (concat "\\([" pre "]\\|^\\)"
  3247. "\\("
  3248. "\\([" vmarkers "]\\)"
  3249. "\\("
  3250. "[^" border "]\\|"
  3251. "[^" border "]"
  3252. body1
  3253. "[^" border "]"
  3254. "\\)"
  3255. "\\3\\)"
  3256. "\\([" post "]\\|$\\)")))))
  3257. (defcustom org-emphasis-regexp-components
  3258. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3259. "Components used to build the regular expression for emphasis.
  3260. This is a list with five entries. Terminology: In an emphasis string
  3261. like \" *strong word* \", we call the initial space PREMATCH, the final
  3262. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3263. and \"trong wor\" is the body. The different components in this variable
  3264. specify what is allowed/forbidden in each part:
  3265. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3266. post Chars allowed as postmatch. End of line will be allowed too.
  3267. border The chars *forbidden* as border characters.
  3268. body-regexp A regexp like \".\" to match a body character. Don't use
  3269. non-shy groups here, and don't allow newline here.
  3270. newline The maximum number of newlines allowed in an emphasis exp.
  3271. Use customize to modify this, or restart Emacs after changing it."
  3272. :group 'org-appearance
  3273. :set 'org-set-emph-re
  3274. :type '(list
  3275. (sexp :tag "Allowed chars in pre ")
  3276. (sexp :tag "Allowed chars in post ")
  3277. (sexp :tag "Forbidden chars in border ")
  3278. (sexp :tag "Regexp for body ")
  3279. (integer :tag "number of newlines allowed")
  3280. (option (boolean :tag "Please ignore this button"))))
  3281. (defcustom org-emphasis-alist
  3282. `(("*" bold "<b>" "</b>")
  3283. ("/" italic "<i>" "</i>")
  3284. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3285. ("=" org-code "<code>" "</code>" verbatim)
  3286. ("~" org-verbatim "<code>" "</code>" verbatim)
  3287. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3288. "<del>" "</del>")
  3289. )
  3290. "Special syntax for emphasized text.
  3291. Text starting and ending with a special character will be emphasized, for
  3292. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3293. characters, the face to be used by font-lock for highlighting in Org-mode
  3294. Emacs buffers, and the HTML tags to be used for this.
  3295. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3296. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3297. Use customize to modify this, or restart Emacs after changing it."
  3298. :group 'org-appearance
  3299. :set 'org-set-emph-re
  3300. :type '(repeat
  3301. (list
  3302. (string :tag "Marker character")
  3303. (choice
  3304. (face :tag "Font-lock-face")
  3305. (plist :tag "Face property list"))
  3306. (string :tag "HTML start tag")
  3307. (string :tag "HTML end tag")
  3308. (option (const verbatim)))))
  3309. (defvar org-protecting-blocks
  3310. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3311. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3312. This is needed for font-lock setup.")
  3313. ;;; Miscellaneous options
  3314. (defgroup org-completion nil
  3315. "Completion in Org-mode."
  3316. :tag "Org Completion"
  3317. :group 'org)
  3318. (defcustom org-completion-use-ido nil
  3319. "Non-nil means use ido completion wherever possible.
  3320. Note that `ido-mode' must be active for this variable to be relevant.
  3321. If you decide to turn this variable on, you might well want to turn off
  3322. `org-outline-path-complete-in-steps'.
  3323. See also `org-completion-use-iswitchb'."
  3324. :group 'org-completion
  3325. :type 'boolean)
  3326. (defcustom org-completion-use-iswitchb nil
  3327. "Non-nil means use iswitchb completion wherever possible.
  3328. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3329. If you decide to turn this variable on, you might well want to turn off
  3330. `org-outline-path-complete-in-steps'.
  3331. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3332. :group 'org-completion
  3333. :type 'boolean)
  3334. (defcustom org-completion-fallback-command 'hippie-expand
  3335. "The expansion command called by \\[pcomplete] in normal context.
  3336. Normal means, no org-mode-specific context."
  3337. :group 'org-completion
  3338. :type 'function)
  3339. ;;; Functions and variables from their packages
  3340. ;; Declared here to avoid compiler warnings
  3341. ;; XEmacs only
  3342. (defvar outline-mode-menu-heading)
  3343. (defvar outline-mode-menu-show)
  3344. (defvar outline-mode-menu-hide)
  3345. (defvar zmacs-regions) ; XEmacs regions
  3346. ;; Emacs only
  3347. (defvar mark-active)
  3348. ;; Various packages
  3349. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3350. (declare-function calendar-forward-day "cal-move" (arg))
  3351. (declare-function calendar-goto-date "cal-move" (date))
  3352. (declare-function calendar-goto-today "cal-move" ())
  3353. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3354. (defvar calc-embedded-close-formula)
  3355. (defvar calc-embedded-open-formula)
  3356. (declare-function cdlatex-tab "ext:cdlatex" ())
  3357. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3358. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3359. (defvar font-lock-unfontify-region-function)
  3360. (declare-function iswitchb-read-buffer "iswitchb"
  3361. (prompt &optional default require-match start matches-set))
  3362. (defvar iswitchb-temp-buflist)
  3363. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3364. (defvar org-agenda-tags-todo-honor-ignore-options)
  3365. (declare-function org-agenda-skip "org-agenda" ())
  3366. (declare-function
  3367. org-agenda-format-item "org-agenda"
  3368. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3369. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3370. (declare-function org-agenda-change-all-lines "org-agenda"
  3371. (newhead hdmarker &optional fixface just-this))
  3372. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3373. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3374. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3375. (beg end))
  3376. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3377. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3378. "org-agenda" (&optional end))
  3379. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3380. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3381. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3382. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3383. (declare-function org-indent-mode "org-indent" (&optional arg))
  3384. (declare-function parse-time-string "parse-time" (string))
  3385. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3386. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3387. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3388. (defvar remember-data-file)
  3389. (defvar texmathp-why)
  3390. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3391. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3392. (defvar w3m-current-url)
  3393. (defvar w3m-current-title)
  3394. (defvar org-latex-regexps)
  3395. ;;; Autoload and prepare some org modules
  3396. ;; Some table stuff that needs to be defined here, because it is used
  3397. ;; by the functions setting up org-mode or checking for table context.
  3398. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3399. "Detect an org-type or table-type table.")
  3400. (defconst org-table-line-regexp "^[ \t]*|"
  3401. "Detect an org-type table line.")
  3402. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3403. "Detect an org-type table line.")
  3404. (defconst org-table-hline-regexp "^[ \t]*|-"
  3405. "Detect an org-type table hline.")
  3406. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3407. "Detect a table-type table hline.")
  3408. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3409. "Detect the first line outside a table when searching from within it.
  3410. This works for both table types.")
  3411. ;; Autoload the functions in org-table.el that are needed by functions here.
  3412. (eval-and-compile
  3413. (org-autoload "org-table"
  3414. '(org-table-begin org-table-blank-field org-table-end)))
  3415. ;;;###autoload
  3416. (defun turn-on-orgtbl ()
  3417. "Unconditionally turn on `orgtbl-mode'."
  3418. (require 'org-table)
  3419. (orgtbl-mode 1))
  3420. (defun org-at-table-p (&optional table-type)
  3421. "Return t if the cursor is inside an org-type table.
  3422. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3423. (if org-enable-table-editor
  3424. (save-excursion
  3425. (beginning-of-line 1)
  3426. (looking-at (if table-type org-table-any-line-regexp
  3427. org-table-line-regexp)))
  3428. nil))
  3429. (defsubst org-table-p () (org-at-table-p))
  3430. (defun org-at-table.el-p ()
  3431. "Return t if and only if we are at a table.el table."
  3432. (and (org-at-table-p 'any)
  3433. (save-excursion
  3434. (goto-char (org-table-begin 'any))
  3435. (looking-at org-table1-hline-regexp))))
  3436. (defun org-table-recognize-table.el ()
  3437. "If there is a table.el table nearby, recognize it and move into it."
  3438. (if org-table-tab-recognizes-table.el
  3439. (if (org-at-table.el-p)
  3440. (progn
  3441. (beginning-of-line 1)
  3442. (if (looking-at org-table-dataline-regexp)
  3443. nil
  3444. (if (looking-at org-table1-hline-regexp)
  3445. (progn
  3446. (beginning-of-line 2)
  3447. (if (looking-at org-table-any-border-regexp)
  3448. (beginning-of-line -1)))))
  3449. (if (re-search-forward "|" (org-table-end t) t)
  3450. (progn
  3451. (require 'table)
  3452. (if (table--at-cell-p (point))
  3453. t
  3454. (message "recognizing table.el table...")
  3455. (table-recognize-table)
  3456. (message "recognizing table.el table...done")))
  3457. (error "This should not happen"))
  3458. t)
  3459. nil)
  3460. nil))
  3461. (defun org-at-table-hline-p ()
  3462. "Return t if the cursor is inside a hline in a table."
  3463. (if org-enable-table-editor
  3464. (save-excursion
  3465. (beginning-of-line 1)
  3466. (looking-at org-table-hline-regexp))
  3467. nil))
  3468. (defvar org-table-clean-did-remove-column nil)
  3469. (defun org-table-map-tables (function &optional quietly)
  3470. "Apply FUNCTION to the start of all tables in the buffer."
  3471. (save-excursion
  3472. (save-restriction
  3473. (widen)
  3474. (goto-char (point-min))
  3475. (while (re-search-forward org-table-any-line-regexp nil t)
  3476. (unless quietly
  3477. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3478. (beginning-of-line 1)
  3479. (when (and (looking-at org-table-line-regexp)
  3480. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3481. (not (org-in-block-p '("src" "example"))))
  3482. (save-excursion (funcall function))
  3483. (or (looking-at org-table-line-regexp)
  3484. (forward-char 1)))
  3485. (re-search-forward org-table-any-border-regexp nil 1))))
  3486. (unless quietly (message "Mapping tables: done")))
  3487. ;; Declare and autoload functions from org-exp.el & Co
  3488. (declare-function org-default-export-plist "org-exp")
  3489. (declare-function org-infile-export-plist "org-exp")
  3490. (declare-function org-get-current-options "org-exp")
  3491. ;; Declare and autoload functions from org-agenda.el
  3492. (eval-and-compile
  3493. (org-autoload "org-agenda"
  3494. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3495. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3496. (declare-function org-clock-update-mode-line "org-clock" ())
  3497. (declare-function org-resolve-clocks "org-clock"
  3498. (&optional also-non-dangling-p prompt last-valid))
  3499. (defvar org-clock-start-time)
  3500. (defvar org-clock-marker (make-marker)
  3501. "Marker recording the last clock-in.")
  3502. (defvar org-clock-hd-marker (make-marker)
  3503. "Marker recording the last clock-in, but the headline position.")
  3504. (defvar org-clock-heading ""
  3505. "The heading of the current clock entry.")
  3506. (defun org-clock-is-active ()
  3507. "Return non-nil if clock is currently running.
  3508. The return value is actually the clock marker."
  3509. (marker-buffer org-clock-marker))
  3510. (eval-and-compile
  3511. (org-autoload "org-clock" '(org-clock-remove-overlays
  3512. org-clock-update-time-maybe
  3513. org-clocktable-shift)))
  3514. (defun org-check-running-clock ()
  3515. "Check if the current buffer contains the running clock.
  3516. If yes, offer to stop it and to save the buffer with the changes."
  3517. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3518. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3519. (buffer-name))))
  3520. (org-clock-out)
  3521. (when (y-or-n-p "Save changed buffer?")
  3522. (save-buffer))))
  3523. (defun org-clocktable-try-shift (dir n)
  3524. "Check if this line starts a clock table, if yes, shift the time block."
  3525. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3526. (org-clocktable-shift dir n)))
  3527. ;;;###autoload
  3528. (defun org-clock-persistence-insinuate ()
  3529. "Set up hooks for clock persistence."
  3530. (require 'org-clock)
  3531. (add-hook 'org-mode-hook 'org-clock-load)
  3532. (add-hook 'kill-emacs-hook 'org-clock-save))
  3533. ;; Define the variable already here, to make sure we have it.
  3534. (defvar org-indent-mode nil
  3535. "Non-nil if Org-Indent mode is enabled.
  3536. Use the command `org-indent-mode' to change this variable.")
  3537. ;; Autoload archiving code
  3538. ;; The stuff that is needed for cycling and tags has to be defined here.
  3539. (defgroup org-archive nil
  3540. "Options concerning archiving in Org-mode."
  3541. :tag "Org Archive"
  3542. :group 'org-structure)
  3543. (defcustom org-archive-location "%s_archive::"
  3544. "The location where subtrees should be archived.
  3545. The value of this variable is a string, consisting of two parts,
  3546. separated by a double-colon. The first part is a filename and
  3547. the second part is a headline.
  3548. When the filename is omitted, archiving happens in the same file.
  3549. %s in the filename will be replaced by the current file
  3550. name (without the directory part). Archiving to a different file
  3551. is useful to keep archived entries from contributing to the
  3552. Org-mode Agenda.
  3553. The archived entries will be filed as subtrees of the specified
  3554. headline. When the headline is omitted, the subtrees are simply
  3555. filed away at the end of the file, as top-level entries. Also in
  3556. the heading you can use %s to represent the file name, this can be
  3557. useful when using the same archive for a number of different files.
  3558. Here are a few examples:
  3559. \"%s_archive::\"
  3560. If the current file is Projects.org, archive in file
  3561. Projects.org_archive, as top-level trees. This is the default.
  3562. \"::* Archived Tasks\"
  3563. Archive in the current file, under the top-level headline
  3564. \"* Archived Tasks\".
  3565. \"~/org/archive.org::\"
  3566. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3567. \"~/org/archive.org::* From %s\"
  3568. Archive in file ~/org/archive.org (absolute path), under headlines
  3569. \"From FILENAME\" where file name is the current file name.
  3570. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3571. The \"datetree/\" string is special, signifying to archive
  3572. items to the datetree. Items are placed in either the CLOSED
  3573. date of the item, or the current date if there is no CLOSED date.
  3574. The heading will be a subentry to the current date. There doesn't
  3575. need to be a heading, but there always needs to be a slash after
  3576. datetree. For example, to store archived items directly in the
  3577. datetree, use \"~/org/datetree.org::datetree/\".
  3578. \"basement::** Finished Tasks\"
  3579. Archive in file ./basement (relative path), as level 3 trees
  3580. below the level 2 heading \"** Finished Tasks\".
  3581. You may set this option on a per-file basis by adding to the buffer a
  3582. line like
  3583. #+ARCHIVE: basement::** Finished Tasks
  3584. You may also define it locally for a subtree by setting an ARCHIVE property
  3585. in the entry. If such a property is found in an entry, or anywhere up
  3586. the hierarchy, it will be used."
  3587. :group 'org-archive
  3588. :type 'string)
  3589. (defcustom org-archive-tag "ARCHIVE"
  3590. "The tag that marks a subtree as archived.
  3591. An archived subtree does not open during visibility cycling, and does
  3592. not contribute to the agenda listings.
  3593. After changing this, font-lock must be restarted in the relevant buffers to
  3594. get the proper fontification."
  3595. :group 'org-archive
  3596. :group 'org-keywords
  3597. :type 'string)
  3598. (defcustom org-agenda-skip-archived-trees t
  3599. "Non-nil means the agenda will skip any items located in archived trees.
  3600. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3601. variable is no longer recommended, you should leave it at the value t.
  3602. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3603. :group 'org-archive
  3604. :group 'org-agenda-skip
  3605. :type 'boolean)
  3606. (defcustom org-columns-skip-archived-trees t
  3607. "Non-nil means ignore archived trees when creating column view."
  3608. :group 'org-archive
  3609. :group 'org-properties
  3610. :type 'boolean)
  3611. (defcustom org-cycle-open-archived-trees nil
  3612. "Non-nil means `org-cycle' will open archived trees.
  3613. An archived tree is a tree marked with the tag ARCHIVE.
  3614. When nil, archived trees will stay folded. You can still open them with
  3615. normal outline commands like `show-all', but not with the cycling commands."
  3616. :group 'org-archive
  3617. :group 'org-cycle
  3618. :type 'boolean)
  3619. (defcustom org-sparse-tree-open-archived-trees nil
  3620. "Non-nil means sparse tree construction shows matches in archived trees.
  3621. When nil, matches in these trees are highlighted, but the trees are kept in
  3622. collapsed state."
  3623. :group 'org-archive
  3624. :group 'org-sparse-trees
  3625. :type 'boolean)
  3626. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3627. "The default date type when building a sparse tree.
  3628. When this is nil, a date is a scheduled or a deadline timestamp.
  3629. Otherwise, these types are allowed:
  3630. all: all timestamps
  3631. active: only active timestamps (<...>)
  3632. inactive: only inactive timestamps (<...)
  3633. scheduled: only scheduled timestamps
  3634. deadline: only deadline timestamps"
  3635. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3636. (const :tag "All timestamps" all)
  3637. (const :tag "Only active timestamps" active)
  3638. (const :tag "Only inactive timestamps" inactive)
  3639. (const :tag "Only scheduled timestamps" scheduled)
  3640. (const :tag "Only deadline timestamps" deadline))
  3641. :version "24.3"
  3642. :group 'org-sparse-trees)
  3643. (defun org-cycle-hide-archived-subtrees (state)
  3644. "Re-hide all archived subtrees after a visibility state change."
  3645. (when (and (not org-cycle-open-archived-trees)
  3646. (not (memq state '(overview folded))))
  3647. (save-excursion
  3648. (let* ((globalp (memq state '(contents all)))
  3649. (beg (if globalp (point-min) (point)))
  3650. (end (if globalp (point-max) (org-end-of-subtree t))))
  3651. (org-hide-archived-subtrees beg end)
  3652. (goto-char beg)
  3653. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3654. (message "%s" (substitute-command-keys
  3655. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3656. (defun org-force-cycle-archived ()
  3657. "Cycle subtree even if it is archived."
  3658. (interactive)
  3659. (setq this-command 'org-cycle)
  3660. (let ((org-cycle-open-archived-trees t))
  3661. (call-interactively 'org-cycle)))
  3662. (defun org-hide-archived-subtrees (beg end)
  3663. "Re-hide all archived subtrees after a visibility state change."
  3664. (save-excursion
  3665. (let* ((re (concat ":" org-archive-tag ":")))
  3666. (goto-char beg)
  3667. (while (re-search-forward re end t)
  3668. (when (org-at-heading-p)
  3669. (org-flag-subtree t)
  3670. (org-end-of-subtree t))))))
  3671. (declare-function outline-end-of-heading "outline" ())
  3672. (declare-function outline-flag-region "outline" (from to flag))
  3673. (defun org-flag-subtree (flag)
  3674. (save-excursion
  3675. (org-back-to-heading t)
  3676. (outline-end-of-heading)
  3677. (outline-flag-region (point)
  3678. (progn (org-end-of-subtree t) (point))
  3679. flag)))
  3680. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3681. (eval-and-compile
  3682. (org-autoload "org-archive"
  3683. '(org-add-archive-files)))
  3684. ;; Autoload Column View Code
  3685. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3686. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3687. (declare-function org-columns-compute "org-colview" (property))
  3688. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3689. '(org-columns-number-to-string
  3690. org-columns-get-format-and-top-level
  3691. org-columns-compute
  3692. org-columns-remove-overlays))
  3693. ;; Autoload ID code
  3694. (declare-function org-id-store-link "org-id")
  3695. (declare-function org-id-locations-load "org-id")
  3696. (declare-function org-id-locations-save "org-id")
  3697. (defvar org-id-track-globally)
  3698. (org-autoload "org-id"
  3699. '(org-id-new
  3700. org-id-copy
  3701. org-id-get-with-outline-path-completion
  3702. org-id-get-with-outline-drilling))
  3703. ;;; Variables for pre-computed regular expressions, all buffer local
  3704. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3705. "Matches first line of a hidden block.")
  3706. (make-variable-buffer-local 'org-drawer-regexp)
  3707. (defvar org-todo-regexp nil
  3708. "Matches any of the TODO state keywords.")
  3709. (make-variable-buffer-local 'org-todo-regexp)
  3710. (defvar org-not-done-regexp nil
  3711. "Matches any of the TODO state keywords except the last one.")
  3712. (make-variable-buffer-local 'org-not-done-regexp)
  3713. (defvar org-not-done-heading-regexp nil
  3714. "Matches a TODO headline that is not done.")
  3715. (make-variable-buffer-local 'org-not-done-regexp)
  3716. (defvar org-todo-line-regexp nil
  3717. "Matches a headline and puts TODO state into group 2 if present.")
  3718. (make-variable-buffer-local 'org-todo-line-regexp)
  3719. (defvar org-complex-heading-regexp nil
  3720. "Matches a headline and puts everything into groups:
  3721. group 1: the stars
  3722. group 2: The todo keyword, maybe
  3723. group 3: Priority cookie
  3724. group 4: True headline
  3725. group 5: Tags")
  3726. (make-variable-buffer-local 'org-complex-heading-regexp)
  3727. (defvar org-complex-heading-regexp-format nil
  3728. "Printf format to make regexp to match an exact headline.
  3729. This regexp will match the headline of any node which has the
  3730. exact headline text that is put into the format, but may have any
  3731. TODO state, priority and tags.")
  3732. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3733. (defvar org-todo-line-tags-regexp nil
  3734. "Matches a headline and puts TODO state into group 2 if present.
  3735. Also put tags into group 4 if tags are present.")
  3736. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3737. (defvar org-ds-keyword-length 12
  3738. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3739. (make-variable-buffer-local 'org-ds-keyword-length)
  3740. (defvar org-deadline-regexp nil
  3741. "Matches the DEADLINE keyword.")
  3742. (make-variable-buffer-local 'org-deadline-regexp)
  3743. (defvar org-deadline-time-regexp nil
  3744. "Matches the DEADLINE keyword together with a time stamp.")
  3745. (make-variable-buffer-local 'org-deadline-time-regexp)
  3746. (defvar org-deadline-line-regexp nil
  3747. "Matches the DEADLINE keyword and the rest of the line.")
  3748. (make-variable-buffer-local 'org-deadline-line-regexp)
  3749. (defvar org-scheduled-regexp nil
  3750. "Matches the SCHEDULED keyword.")
  3751. (make-variable-buffer-local 'org-scheduled-regexp)
  3752. (defvar org-scheduled-time-regexp nil
  3753. "Matches the SCHEDULED keyword together with a time stamp.")
  3754. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3755. (defvar org-closed-time-regexp nil
  3756. "Matches the CLOSED keyword together with a time stamp.")
  3757. (make-variable-buffer-local 'org-closed-time-regexp)
  3758. (defvar org-keyword-time-regexp nil
  3759. "Matches any of the 4 keywords, together with the time stamp.")
  3760. (make-variable-buffer-local 'org-keyword-time-regexp)
  3761. (defvar org-keyword-time-not-clock-regexp nil
  3762. "Matches any of the 3 keywords, together with the time stamp.")
  3763. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3764. (defvar org-maybe-keyword-time-regexp nil
  3765. "Matches a timestamp, possibly preceded by a keyword.")
  3766. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3767. (defvar org-all-time-keywords nil
  3768. "List of time keywords.")
  3769. (make-variable-buffer-local 'org-all-time-keywords)
  3770. (defconst org-plain-time-of-day-regexp
  3771. (concat
  3772. "\\(\\<[012]?[0-9]"
  3773. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3774. "\\(--?"
  3775. "\\(\\<[012]?[0-9]"
  3776. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3777. "\\)?")
  3778. "Regular expression to match a plain time or time range.
  3779. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3780. groups carry important information:
  3781. 0 the full match
  3782. 1 the first time, range or not
  3783. 8 the second time, if it is a range.")
  3784. (defconst org-plain-time-extension-regexp
  3785. (concat
  3786. "\\(\\<[012]?[0-9]"
  3787. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3788. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3789. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3790. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3791. groups carry important information:
  3792. 0 the full match
  3793. 7 hours of duration
  3794. 9 minutes of duration")
  3795. (defconst org-stamp-time-of-day-regexp
  3796. (concat
  3797. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3798. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3799. "\\(--?"
  3800. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3801. "Regular expression to match a timestamp time or time range.
  3802. After a match, the following groups carry important information:
  3803. 0 the full match
  3804. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3805. 2 the first time, range or not
  3806. 4 the second time, if it is a range.")
  3807. (defconst org-startup-options
  3808. '(("fold" org-startup-folded t)
  3809. ("overview" org-startup-folded t)
  3810. ("nofold" org-startup-folded nil)
  3811. ("showall" org-startup-folded nil)
  3812. ("showeverything" org-startup-folded showeverything)
  3813. ("content" org-startup-folded content)
  3814. ("indent" org-startup-indented t)
  3815. ("noindent" org-startup-indented nil)
  3816. ("hidestars" org-hide-leading-stars t)
  3817. ("showstars" org-hide-leading-stars nil)
  3818. ("odd" org-odd-levels-only t)
  3819. ("oddeven" org-odd-levels-only nil)
  3820. ("align" org-startup-align-all-tables t)
  3821. ("noalign" org-startup-align-all-tables nil)
  3822. ("inlineimages" org-startup-with-inline-images t)
  3823. ("noinlineimages" org-startup-with-inline-images nil)
  3824. ("customtime" org-display-custom-times t)
  3825. ("logdone" org-log-done time)
  3826. ("lognotedone" org-log-done note)
  3827. ("nologdone" org-log-done nil)
  3828. ("lognoteclock-out" org-log-note-clock-out t)
  3829. ("nolognoteclock-out" org-log-note-clock-out nil)
  3830. ("logrepeat" org-log-repeat state)
  3831. ("lognoterepeat" org-log-repeat note)
  3832. ("nologrepeat" org-log-repeat nil)
  3833. ("logreschedule" org-log-reschedule time)
  3834. ("lognotereschedule" org-log-reschedule note)
  3835. ("nologreschedule" org-log-reschedule nil)
  3836. ("logredeadline" org-log-redeadline time)
  3837. ("lognoteredeadline" org-log-redeadline note)
  3838. ("nologredeadline" org-log-redeadline nil)
  3839. ("logrefile" org-log-refile time)
  3840. ("lognoterefile" org-log-refile note)
  3841. ("nologrefile" org-log-refile nil)
  3842. ("fninline" org-footnote-define-inline t)
  3843. ("nofninline" org-footnote-define-inline nil)
  3844. ("fnlocal" org-footnote-section nil)
  3845. ("fnauto" org-footnote-auto-label t)
  3846. ("fnprompt" org-footnote-auto-label nil)
  3847. ("fnconfirm" org-footnote-auto-label confirm)
  3848. ("fnplain" org-footnote-auto-label plain)
  3849. ("fnadjust" org-footnote-auto-adjust t)
  3850. ("nofnadjust" org-footnote-auto-adjust nil)
  3851. ("constcgs" constants-unit-system cgs)
  3852. ("constSI" constants-unit-system SI)
  3853. ("noptag" org-tag-persistent-alist nil)
  3854. ("hideblocks" org-hide-block-startup t)
  3855. ("nohideblocks" org-hide-block-startup nil)
  3856. ("beamer" org-startup-with-beamer-mode t)
  3857. ("entitiespretty" org-pretty-entities t)
  3858. ("entitiesplain" org-pretty-entities nil))
  3859. "Variable associated with STARTUP options for org-mode.
  3860. Each element is a list of three items: the startup options (as written
  3861. in the #+STARTUP line), the corresponding variable, and the value to set
  3862. this variable to if the option is found. An optional forth element PUSH
  3863. means to push this value onto the list in the variable.")
  3864. (defun org-update-property-plist (key val props)
  3865. "Update PROPS with KEY and VAL."
  3866. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3867. (key (if appending (substring key 0 (- (length key) 1)) key))
  3868. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3869. (previous (cdr (assoc key props))))
  3870. (if appending
  3871. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3872. (cons (cons key val) remainder))))
  3873. (defconst org-block-regexp
  3874. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3875. "Regular expression for hiding blocks.")
  3876. (defconst org-heading-keyword-regexp-format
  3877. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3878. "Printf format for a regexp matching an headline with some keyword.
  3879. This regexp will match the headline of any node which has the
  3880. exact keyword that is put into the format. The keyword isn't in
  3881. any group by default, but the stars and the body are.")
  3882. (defconst org-heading-keyword-maybe-regexp-format
  3883. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3884. "Printf format for a regexp matching an headline, possibly with some keyword.
  3885. This regexp can match any headline with the specified keyword, or
  3886. without a keyword. The keyword isn't in any group by default,
  3887. but the stars and the body are.")
  3888. (defun org-set-regexps-and-options ()
  3889. "Precompute regular expressions for current buffer."
  3890. (when (derived-mode-p 'org-mode)
  3891. (org-set-local 'org-todo-kwd-alist nil)
  3892. (org-set-local 'org-todo-key-alist nil)
  3893. (org-set-local 'org-todo-key-trigger nil)
  3894. (org-set-local 'org-todo-keywords-1 nil)
  3895. (org-set-local 'org-done-keywords nil)
  3896. (org-set-local 'org-todo-heads nil)
  3897. (org-set-local 'org-todo-sets nil)
  3898. (org-set-local 'org-todo-log-states nil)
  3899. (org-set-local 'org-file-properties nil)
  3900. (org-set-local 'org-file-tags nil)
  3901. (let ((re (org-make-options-regexp
  3902. '("CATEGORY" "TODO" "COLUMNS"
  3903. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3904. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3905. "OPTIONS")
  3906. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3907. (splitre "[ \t]+")
  3908. (scripts org-use-sub-superscripts)
  3909. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3910. tail sep kws1 prio props ftags drawers beamer-p
  3911. ext-setup-or-nil setup-contents (start 0))
  3912. (save-excursion
  3913. (save-restriction
  3914. (widen)
  3915. (goto-char (point-min))
  3916. (while (or (and ext-setup-or-nil
  3917. (string-match re ext-setup-or-nil start)
  3918. (setq start (match-end 0)))
  3919. (and (setq ext-setup-or-nil nil start 0)
  3920. (re-search-forward re nil t)))
  3921. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3922. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3923. (if (stringp value) (setq value (org-trim value)))
  3924. (cond
  3925. ((equal key "CATEGORY")
  3926. (setq cat value))
  3927. ((member key '("SEQ_TODO" "TODO"))
  3928. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3929. ((equal key "TYP_TODO")
  3930. (push (cons 'type (org-split-string value splitre)) kwds))
  3931. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3932. ;; general TODO-like setup
  3933. (push (cons (intern (downcase (match-string 1 key)))
  3934. (org-split-string value splitre)) kwds))
  3935. ((equal key "TAGS")
  3936. (setq tags (append tags (if tags '("\\n") nil)
  3937. (org-split-string value splitre))))
  3938. ((equal key "COLUMNS")
  3939. (org-set-local 'org-columns-default-format value))
  3940. ((equal key "LINK")
  3941. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3942. (push (cons (match-string 1 value)
  3943. (org-trim (match-string 2 value)))
  3944. links)))
  3945. ((equal key "PRIORITIES")
  3946. (setq prio (org-split-string value " +")))
  3947. ((equal key "PROPERTY")
  3948. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3949. (setq props (org-update-property-plist (match-string 1 value)
  3950. (match-string 2 value)
  3951. props))))
  3952. ((equal key "FILETAGS")
  3953. (when (string-match "\\S-" value)
  3954. (setq ftags
  3955. (append
  3956. ftags
  3957. (apply 'append
  3958. (mapcar (lambda (x) (org-split-string x ":"))
  3959. (org-split-string value)))))))
  3960. ((equal key "DRAWERS")
  3961. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  3962. ((equal key "CONSTANTS")
  3963. (setq const (append const (org-split-string value splitre))))
  3964. ((equal key "STARTUP")
  3965. (let ((opts (org-split-string value splitre))
  3966. l var val)
  3967. (while (setq l (pop opts))
  3968. (when (setq l (assoc l org-startup-options))
  3969. (setq var (nth 1 l) val (nth 2 l))
  3970. (if (not (nth 3 l))
  3971. (set (make-local-variable var) val)
  3972. (if (not (listp (symbol-value var)))
  3973. (set (make-local-variable var) nil))
  3974. (set (make-local-variable var) (symbol-value var))
  3975. (add-to-list var val))))))
  3976. ((equal key "ARCHIVE")
  3977. (setq arch value)
  3978. (remove-text-properties 0 (length arch)
  3979. '(face t fontified t) arch))
  3980. ((equal key "LATEX_CLASS")
  3981. (setq beamer-p (equal value "beamer")))
  3982. ((equal key "OPTIONS")
  3983. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3984. (setq scripts (read (match-string 2 value)))))
  3985. ((equal key "SETUPFILE")
  3986. (setq setup-contents (org-file-contents
  3987. (expand-file-name
  3988. (org-remove-double-quotes value))
  3989. 'noerror))
  3990. (if (not ext-setup-or-nil)
  3991. (setq ext-setup-or-nil setup-contents start 0)
  3992. (setq ext-setup-or-nil
  3993. (concat (substring ext-setup-or-nil 0 start)
  3994. "\n" setup-contents "\n"
  3995. (substring ext-setup-or-nil start)))))))
  3996. ;; search for property blocks
  3997. (goto-char (point-min))
  3998. (while (re-search-forward org-block-regexp nil t)
  3999. (when (equal "PROPERTY" (upcase (match-string 1)))
  4000. (setq value (replace-regexp-in-string
  4001. "[\n\r]" " " (match-string 4)))
  4002. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4003. (setq props (org-update-property-plist (match-string 1 value)
  4004. (match-string 2 value)
  4005. props)))))))
  4006. (org-set-local 'org-use-sub-superscripts scripts)
  4007. (when cat
  4008. (org-set-local 'org-category (intern cat))
  4009. (push (cons "CATEGORY" cat) props))
  4010. (when prio
  4011. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4012. (setq prio (mapcar 'string-to-char prio))
  4013. (org-set-local 'org-highest-priority (nth 0 prio))
  4014. (org-set-local 'org-lowest-priority (nth 1 prio))
  4015. (org-set-local 'org-default-priority (nth 2 prio)))
  4016. (and props (org-set-local 'org-file-properties (nreverse props)))
  4017. (and ftags (org-set-local 'org-file-tags
  4018. (mapcar 'org-add-prop-inherited ftags)))
  4019. (and drawers (org-set-local 'org-drawers drawers))
  4020. (and arch (org-set-local 'org-archive-location arch))
  4021. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4022. ;; Process the TODO keywords
  4023. (unless kwds
  4024. ;; Use the global values as if they had been given locally.
  4025. (setq kwds (default-value 'org-todo-keywords))
  4026. (if (stringp (car kwds))
  4027. (setq kwds (list (cons org-todo-interpretation
  4028. (default-value 'org-todo-keywords)))))
  4029. (setq kwds (reverse kwds)))
  4030. (setq kwds (nreverse kwds))
  4031. (let (inter kws kw)
  4032. (while (setq kws (pop kwds))
  4033. (let ((kws (or
  4034. (run-hook-with-args-until-success
  4035. 'org-todo-setup-filter-hook kws)
  4036. kws)))
  4037. (setq inter (pop kws) sep (member "|" kws)
  4038. kws0 (delete "|" (copy-sequence kws))
  4039. kwsa nil
  4040. kws1 (mapcar
  4041. (lambda (x)
  4042. ;; 1 2
  4043. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4044. (progn
  4045. (setq kw (match-string 1 x)
  4046. key (and (match-end 2) (match-string 2 x))
  4047. log (org-extract-log-state-settings x))
  4048. (push (cons kw (and key (string-to-char key))) kwsa)
  4049. (and log (push log org-todo-log-states))
  4050. kw)
  4051. (error "Invalid TODO keyword %s" x)))
  4052. kws0)
  4053. kwsa (if kwsa (append '((:startgroup))
  4054. (nreverse kwsa)
  4055. '((:endgroup))))
  4056. hw (car kws1)
  4057. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4058. tail (list inter hw (car dws) (org-last dws))))
  4059. (add-to-list 'org-todo-heads hw 'append)
  4060. (push kws1 org-todo-sets)
  4061. (setq org-done-keywords (append org-done-keywords dws nil))
  4062. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4063. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4064. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4065. (setq org-todo-sets (nreverse org-todo-sets)
  4066. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4067. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4068. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4069. ;; Process the constants
  4070. (when const
  4071. (let (e cst)
  4072. (while (setq e (pop const))
  4073. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4074. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4075. (setq org-table-formula-constants-local cst)))
  4076. ;; Process the tags.
  4077. (when tags
  4078. (let (e tgs)
  4079. (while (setq e (pop tags))
  4080. (cond
  4081. ((equal e "{") (push '(:startgroup) tgs))
  4082. ((equal e "}") (push '(:endgroup) tgs))
  4083. ((equal e "\\n") (push '(:newline) tgs))
  4084. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4085. (push (cons (match-string 1 e)
  4086. (string-to-char (match-string 2 e)))
  4087. tgs))
  4088. (t (push (list e) tgs))))
  4089. (org-set-local 'org-tag-alist nil)
  4090. (while (setq e (pop tgs))
  4091. (or (and (stringp (car e))
  4092. (assoc (car e) org-tag-alist))
  4093. (push e org-tag-alist)))))
  4094. ;; Compute the regular expressions and other local variables.
  4095. ;; Using `org-outline-regexp-bol' would complicate them much,
  4096. ;; because of the fixed white space at the end of that string.
  4097. (if (not org-done-keywords)
  4098. (setq org-done-keywords (and org-todo-keywords-1
  4099. (list (org-last org-todo-keywords-1)))))
  4100. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4101. (length org-scheduled-string)
  4102. (length org-clock-string)
  4103. (length org-closed-string)))
  4104. org-drawer-regexp
  4105. (concat "^[ \t]*:\\("
  4106. (mapconcat 'regexp-quote org-drawers "\\|")
  4107. "\\):[ \t]*$")
  4108. org-not-done-keywords
  4109. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4110. org-todo-regexp
  4111. (concat "\\("
  4112. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4113. "\\)")
  4114. org-not-done-regexp
  4115. (concat "\\("
  4116. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4117. "\\)")
  4118. org-not-done-heading-regexp
  4119. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4120. org-todo-line-regexp
  4121. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4122. org-complex-heading-regexp
  4123. (concat "^\\(\\*+\\)"
  4124. "\\(?: +" org-todo-regexp "\\)?"
  4125. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4126. "\\(?: +\\(.*?\\)\\)??"
  4127. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4128. "[ \t]*$")
  4129. org-complex-heading-regexp-format
  4130. (concat "^\\(\\*+\\)"
  4131. "\\(?: +" org-todo-regexp "\\)?"
  4132. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4133. "\\(?: +"
  4134. ;; Stats cookies can be stuck to body.
  4135. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4136. "\\(%s\\)"
  4137. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4138. "\\)"
  4139. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4140. "[ \t]*$")
  4141. org-todo-line-tags-regexp
  4142. (concat "^\\(\\*+\\)"
  4143. "\\(?: +" org-todo-regexp "\\)?"
  4144. "\\(?: +\\(.*?\\)\\)??"
  4145. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4146. "[ \t]*$")
  4147. org-deadline-regexp (concat "\\<" org-deadline-string)
  4148. org-deadline-time-regexp
  4149. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4150. org-deadline-line-regexp
  4151. (concat "\\<\\(" org-deadline-string "\\).*")
  4152. org-scheduled-regexp
  4153. (concat "\\<" org-scheduled-string)
  4154. org-scheduled-time-regexp
  4155. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4156. org-closed-time-regexp
  4157. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4158. org-keyword-time-regexp
  4159. (concat "\\<\\(" org-scheduled-string
  4160. "\\|" org-deadline-string
  4161. "\\|" org-closed-string
  4162. "\\|" org-clock-string "\\)"
  4163. " *[[<]\\([^]>]+\\)[]>]")
  4164. org-keyword-time-not-clock-regexp
  4165. (concat "\\<\\(" org-scheduled-string
  4166. "\\|" org-deadline-string
  4167. "\\|" org-closed-string
  4168. "\\)"
  4169. " *[[<]\\([^]>]+\\)[]>]")
  4170. org-maybe-keyword-time-regexp
  4171. (concat "\\(\\<\\(" org-scheduled-string
  4172. "\\|" org-deadline-string
  4173. "\\|" org-closed-string
  4174. "\\|" org-clock-string "\\)\\)?"
  4175. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4176. org-all-time-keywords
  4177. (mapcar (lambda (w) (substring w 0 -1))
  4178. (list org-scheduled-string org-deadline-string
  4179. org-clock-string org-closed-string))
  4180. )
  4181. (org-compute-latex-and-specials-regexp)
  4182. (org-set-font-lock-defaults))))
  4183. (defun org-file-contents (file &optional noerror)
  4184. "Return the contents of FILE, as a string."
  4185. (if (or (not file)
  4186. (not (file-readable-p file)))
  4187. (if noerror
  4188. (progn
  4189. (message "Cannot read file \"%s\"" file)
  4190. (ding) (sit-for 2)
  4191. "")
  4192. (error "Cannot read file \"%s\"" file))
  4193. (with-temp-buffer
  4194. (insert-file-contents file)
  4195. (buffer-string))))
  4196. (defun org-extract-log-state-settings (x)
  4197. "Extract the log state setting from a TODO keyword string.
  4198. This will extract info from a string like \"WAIT(w@/!)\"."
  4199. (let (kw key log1 log2)
  4200. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4201. (setq kw (match-string 1 x)
  4202. key (and (match-end 2) (match-string 2 x))
  4203. log1 (and (match-end 3) (match-string 3 x))
  4204. log2 (and (match-end 4) (match-string 4 x)))
  4205. (and (or log1 log2)
  4206. (list kw
  4207. (and log1 (if (equal log1 "!") 'time 'note))
  4208. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4209. (defun org-remove-keyword-keys (list)
  4210. "Remove a pair of parenthesis at the end of each string in LIST."
  4211. (mapcar (lambda (x)
  4212. (if (string-match "(.*)$" x)
  4213. (substring x 0 (match-beginning 0))
  4214. x))
  4215. list))
  4216. (defun org-assign-fast-keys (alist)
  4217. "Assign fast keys to a keyword-key alist.
  4218. Respect keys that are already there."
  4219. (let (new e (alt ?0))
  4220. (while (setq e (pop alist))
  4221. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4222. (cdr e)) ;; Key already assigned.
  4223. (push e new)
  4224. (let ((clist (string-to-list (downcase (car e))))
  4225. (used (append new alist)))
  4226. (when (= (car clist) ?@)
  4227. (pop clist))
  4228. (while (and clist (rassoc (car clist) used))
  4229. (pop clist))
  4230. (unless clist
  4231. (while (rassoc alt used)
  4232. (incf alt)))
  4233. (push (cons (car e) (or (car clist) alt)) new))))
  4234. (nreverse new)))
  4235. ;;; Some variables used in various places
  4236. (defvar org-window-configuration nil
  4237. "Used in various places to store a window configuration.")
  4238. (defvar org-selected-window nil
  4239. "Used in various places to store a window configuration.")
  4240. (defvar org-finish-function nil
  4241. "Function to be called when `C-c C-c' is used.
  4242. This is for getting out of special buffers like capture.")
  4243. ;; FIXME: Occasionally check by commenting these, to make sure
  4244. ;; no other functions uses these, forgetting to let-bind them.
  4245. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4246. (defvar org-last-state)
  4247. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4248. ;; Defined somewhere in this file, but used before definition.
  4249. (defvar org-entities) ;; defined in org-entities.el
  4250. (defvar org-struct-menu)
  4251. (defvar org-org-menu)
  4252. (defvar org-tbl-menu)
  4253. ;;;; Define the Org-mode
  4254. ;; We use a before-change function to check if a table might need
  4255. ;; an update.
  4256. (defvar org-table-may-need-update t
  4257. "Indicates that a table might need an update.
  4258. This variable is set by `org-before-change-function'.
  4259. `org-table-align' sets it back to nil.")
  4260. (defun org-before-change-function (beg end)
  4261. "Every change indicates that a table might need an update."
  4262. (setq org-table-may-need-update t))
  4263. (defvar org-mode-map)
  4264. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4265. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4266. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4267. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4268. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4269. (defvar org-table-buffer-is-an nil)
  4270. (defvar bidi-paragraph-direction)
  4271. (defvar buffer-face-mode-face)
  4272. (require 'outline)
  4273. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4274. (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"))
  4275. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4276. ;; Other stuff we need.
  4277. (require 'time-date)
  4278. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4279. (require 'easymenu)
  4280. (require 'overlay)
  4281. (require 'org-macs)
  4282. (require 'org-entities)
  4283. ;; (require 'org-compat) moved higher up in the file before it is first used
  4284. (require 'org-faces)
  4285. (require 'org-list)
  4286. (require 'org-pcomplete)
  4287. (require 'org-src)
  4288. (require 'org-footnote)
  4289. ;; babel
  4290. (require 'ob)
  4291. (require 'ob-table)
  4292. (require 'ob-lob)
  4293. (require 'ob-ref)
  4294. (require 'ob-tangle)
  4295. (require 'ob-comint)
  4296. (require 'ob-keys)
  4297. ;;;###autoload
  4298. (define-derived-mode org-mode outline-mode "Org"
  4299. "Outline-based notes management and organizer, alias
  4300. \"Carsten's outline-mode for keeping track of everything.\"
  4301. Org-mode develops organizational tasks around a NOTES file which
  4302. contains information about projects as plain text. Org-mode is
  4303. implemented on top of outline-mode, which is ideal to keep the content
  4304. of large files well structured. It supports ToDo items, deadlines and
  4305. time stamps, which magically appear in the diary listing of the Emacs
  4306. calendar. Tables are easily created with a built-in table editor.
  4307. Plain text URL-like links connect to websites, emails (VM), Usenet
  4308. messages (Gnus), BBDB entries, and any files related to the project.
  4309. For printing and sharing of notes, an Org-mode file (or a part of it)
  4310. can be exported as a structured ASCII or HTML file.
  4311. The following commands are available:
  4312. \\{org-mode-map}"
  4313. ;; Get rid of Outline menus, they are not needed
  4314. ;; Need to do this here because define-derived-mode sets up
  4315. ;; the keymap so late. Still, it is a waste to call this each time
  4316. ;; we switch another buffer into org-mode.
  4317. (if (featurep 'xemacs)
  4318. (when (boundp 'outline-mode-menu-heading)
  4319. ;; Assume this is Greg's port, it uses easymenu
  4320. (easy-menu-remove outline-mode-menu-heading)
  4321. (easy-menu-remove outline-mode-menu-show)
  4322. (easy-menu-remove outline-mode-menu-hide))
  4323. (define-key org-mode-map [menu-bar headings] 'undefined)
  4324. (define-key org-mode-map [menu-bar hide] 'undefined)
  4325. (define-key org-mode-map [menu-bar show] 'undefined))
  4326. (org-load-modules-maybe)
  4327. (easy-menu-add org-org-menu)
  4328. (easy-menu-add org-tbl-menu)
  4329. (org-install-agenda-files-menu)
  4330. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4331. (add-to-invisibility-spec '(org-cwidth))
  4332. (add-to-invisibility-spec '(org-hide-block . t))
  4333. (when (featurep 'xemacs)
  4334. (org-set-local 'line-move-ignore-invisible t))
  4335. (org-set-local 'outline-regexp org-outline-regexp)
  4336. (org-set-local 'outline-level 'org-outline-level)
  4337. (setq bidi-paragraph-direction 'left-to-right)
  4338. (when (and org-ellipsis
  4339. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4340. (fboundp 'make-glyph-code))
  4341. (unless org-display-table
  4342. (setq org-display-table (make-display-table)))
  4343. (set-display-table-slot
  4344. org-display-table 4
  4345. (vconcat (mapcar
  4346. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4347. org-ellipsis)))
  4348. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4349. (setq buffer-display-table org-display-table))
  4350. (org-set-regexps-and-options)
  4351. (when (and org-tag-faces (not org-tags-special-faces-re))
  4352. ;; tag faces set outside customize.... force initialization.
  4353. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4354. ;; Calc embedded
  4355. (org-set-local 'calc-embedded-open-mode "# ")
  4356. (modify-syntax-entry ?@ "w")
  4357. (modify-syntax-entry ?\" "\"")
  4358. (if org-startup-truncated (setq truncate-lines t))
  4359. (org-set-local 'font-lock-unfontify-region-function
  4360. 'org-unfontify-region)
  4361. ;; Activate before-change-function
  4362. (org-set-local 'org-table-may-need-update t)
  4363. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4364. 'local)
  4365. ;; Check for running clock before killing a buffer
  4366. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4367. ;; Initialize macros templates.
  4368. (org-macro-initialize-templates)
  4369. ;; Initialize radio targets.
  4370. (org-update-radio-target-regexp)
  4371. ;; Indentation.
  4372. (org-set-local 'indent-line-function 'org-indent-line)
  4373. (org-set-local 'indent-region-function 'org-indent-region)
  4374. ;; Filling and auto-filling.
  4375. (org-setup-filling)
  4376. ;; Comments.
  4377. (org-setup-comments-handling)
  4378. ;; Beginning/end of defun
  4379. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4380. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4381. ;; Next error for sparse trees
  4382. (org-set-local 'next-error-function 'org-occur-next-match)
  4383. ;; Make sure dependence stuff works reliably, even for users who set it
  4384. ;; too late :-(
  4385. (if org-enforce-todo-dependencies
  4386. (add-hook 'org-blocker-hook
  4387. 'org-block-todo-from-children-or-siblings-or-parent)
  4388. (remove-hook 'org-blocker-hook
  4389. 'org-block-todo-from-children-or-siblings-or-parent))
  4390. (if org-enforce-todo-checkbox-dependencies
  4391. (add-hook 'org-blocker-hook
  4392. 'org-block-todo-from-checkboxes)
  4393. (remove-hook 'org-blocker-hook
  4394. 'org-block-todo-from-checkboxes))
  4395. ;; Align options lines
  4396. (org-set-local
  4397. 'align-mode-rules-list
  4398. '((org-in-buffer-settings
  4399. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4400. (modes . '(org-mode)))))
  4401. ;; Imenu
  4402. (org-set-local 'imenu-create-index-function
  4403. 'org-imenu-get-tree)
  4404. ;; Make isearch reveal context
  4405. (if (or (featurep 'xemacs)
  4406. (not (boundp 'outline-isearch-open-invisible-function)))
  4407. ;; Emacs 21 and XEmacs make use of the hook
  4408. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4409. ;; Emacs 22 deals with this through a special variable
  4410. (org-set-local 'outline-isearch-open-invisible-function
  4411. (lambda (&rest ignore) (org-show-context 'isearch))))
  4412. ;; Turn on org-beamer-mode?
  4413. (and org-startup-with-beamer-mode (org-beamer-mode))
  4414. ;; Setup the pcomplete hooks
  4415. (set (make-local-variable 'pcomplete-command-completion-function)
  4416. 'org-pcomplete-initial)
  4417. (set (make-local-variable 'pcomplete-command-name-function)
  4418. 'org-command-at-point)
  4419. (set (make-local-variable 'pcomplete-default-completion-function)
  4420. 'ignore)
  4421. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4422. 'org-parse-arguments)
  4423. (set (make-local-variable 'pcomplete-termination-string) "")
  4424. (when (>= emacs-major-version 23)
  4425. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4426. ;; If empty file that did not turn on org-mode automatically, make it to.
  4427. (if (and org-insert-mode-line-in-empty-file
  4428. (org-called-interactively-p 'any)
  4429. (= (point-min) (point-max)))
  4430. (insert "# -*- mode: org -*-\n\n"))
  4431. (unless org-inhibit-startup
  4432. (when org-startup-align-all-tables
  4433. (let ((bmp (buffer-modified-p)))
  4434. (org-table-map-tables 'org-table-align 'quietly)
  4435. (set-buffer-modified-p bmp)))
  4436. (when org-startup-with-inline-images
  4437. (org-display-inline-images))
  4438. (when org-startup-indented
  4439. (require 'org-indent)
  4440. (org-indent-mode 1))
  4441. (unless org-inhibit-startup-visibility-stuff
  4442. (org-set-startup-visibility)))
  4443. ;; Try to set org-hide correctly
  4444. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4445. (when (fboundp 'abbrev-table-put)
  4446. (abbrev-table-put org-mode-abbrev-table
  4447. :parents (list text-mode-abbrev-table)))
  4448. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4449. (defun org-find-invisible-foreground ()
  4450. (let ((candidates (remove
  4451. "unspecified-bg"
  4452. (list
  4453. (face-background 'default)
  4454. (face-background 'org-default)
  4455. (cdr (assoc 'background-color default-frame-alist))
  4456. (cdr (assoc 'background-color initial-frame-alist))
  4457. (cdr (assoc 'background-color window-system-default-frame-alist))
  4458. (face-foreground 'org-hide)))))
  4459. (car (remove nil candidates))))
  4460. (defun org-current-time ()
  4461. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4462. (if (> (car org-time-stamp-rounding-minutes) 1)
  4463. (let ((r (car org-time-stamp-rounding-minutes))
  4464. (time (decode-time)))
  4465. (apply 'encode-time
  4466. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4467. (nthcdr 2 time))))
  4468. (current-time)))
  4469. (defun org-today ()
  4470. "Return today date, considering `org-extend-today-until'."
  4471. (time-to-days
  4472. (time-subtract (current-time)
  4473. (list 0 (* 3600 org-extend-today-until) 0))))
  4474. ;;;; Font-Lock stuff, including the activators
  4475. (defvar org-mouse-map (make-sparse-keymap))
  4476. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4477. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4478. (when org-mouse-1-follows-link
  4479. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4480. (when org-tab-follows-link
  4481. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4482. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4483. (require 'font-lock)
  4484. (defconst org-non-link-chars "]\t\n\r<>")
  4485. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4486. "shell" "elisp" "doi" "message"))
  4487. (defvar org-link-types-re nil
  4488. "Matches a link that has a url-like prefix like \"http:\"")
  4489. (defvar org-link-re-with-space nil
  4490. "Matches a link with spaces, optional angular brackets around it.")
  4491. (defvar org-link-re-with-space2 nil
  4492. "Matches a link with spaces, optional angular brackets around it.")
  4493. (defvar org-link-re-with-space3 nil
  4494. "Matches a link with spaces, only for internal part in bracket links.")
  4495. (defvar org-angle-link-re nil
  4496. "Matches link with angular brackets, spaces are allowed.")
  4497. (defvar org-plain-link-re nil
  4498. "Matches plain link, without spaces.")
  4499. (defvar org-bracket-link-regexp nil
  4500. "Matches a link in double brackets.")
  4501. (defvar org-bracket-link-analytic-regexp nil
  4502. "Regular expression used to analyze links.
  4503. Here is what the match groups contain after a match:
  4504. 1: http:
  4505. 2: http
  4506. 3: path
  4507. 4: [desc]
  4508. 5: desc")
  4509. (defvar org-bracket-link-analytic-regexp++ nil
  4510. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4511. (defvar org-any-link-re nil
  4512. "Regular expression matching any link.")
  4513. (defcustom org-match-sexp-depth 3
  4514. "Number of stacked braces for sub/superscript matching.
  4515. This has to be set before loading org.el to be effective."
  4516. :group 'org-export-translation ; ??????????????????????????/
  4517. :type 'integer)
  4518. (defun org-create-multibrace-regexp (left right n)
  4519. "Create a regular expression which will match a balanced sexp.
  4520. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4521. as single character strings.
  4522. The regexp returned will match the entire expression including the
  4523. delimiters. It will also define a single group which contains the
  4524. match except for the outermost delimiters. The maximum depth of
  4525. stacked delimiters is N. Escaping delimiters is not possible."
  4526. (let* ((nothing (concat "[^" left right "]*?"))
  4527. (or "\\|")
  4528. (re nothing)
  4529. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4530. (while (> n 1)
  4531. (setq n (1- n)
  4532. re (concat re or next)
  4533. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4534. (concat left "\\(" re "\\)" right)))
  4535. (defvar org-match-substring-regexp
  4536. (concat
  4537. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4538. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4539. "\\|"
  4540. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4541. "\\|"
  4542. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4543. "The regular expression matching a sub- or superscript.")
  4544. (defvar org-match-substring-with-braces-regexp
  4545. (concat
  4546. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4547. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4548. "\\)")
  4549. "The regular expression matching a sub- or superscript, forcing braces.")
  4550. (defun org-make-link-regexps ()
  4551. "Update the link regular expressions.
  4552. This should be called after the variable `org-link-types' has changed."
  4553. (setq org-link-types-re
  4554. (concat
  4555. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4556. org-link-re-with-space
  4557. (concat
  4558. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4559. "\\([^" org-non-link-chars " ]"
  4560. "[^" org-non-link-chars "]*"
  4561. "[^" org-non-link-chars " ]\\)>?")
  4562. org-link-re-with-space2
  4563. (concat
  4564. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4565. "\\([^" org-non-link-chars " ]"
  4566. "[^\t\n\r]*"
  4567. "[^" org-non-link-chars " ]\\)>?")
  4568. org-link-re-with-space3
  4569. (concat
  4570. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4571. "\\([^" org-non-link-chars " ]"
  4572. "[^\t\n\r]*\\)")
  4573. org-angle-link-re
  4574. (concat
  4575. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4576. "\\([^" org-non-link-chars " ]"
  4577. "[^" org-non-link-chars "]*"
  4578. "\\)>")
  4579. org-plain-link-re
  4580. (concat
  4581. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4582. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4583. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4584. org-bracket-link-regexp
  4585. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4586. org-bracket-link-analytic-regexp
  4587. (concat
  4588. "\\[\\["
  4589. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4590. "\\([^]]+\\)"
  4591. "\\]"
  4592. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4593. "\\]")
  4594. org-bracket-link-analytic-regexp++
  4595. (concat
  4596. "\\[\\["
  4597. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4598. "\\([^]]+\\)"
  4599. "\\]"
  4600. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4601. "\\]")
  4602. org-any-link-re
  4603. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4604. org-angle-link-re "\\)\\|\\("
  4605. org-plain-link-re "\\)")))
  4606. (org-make-link-regexps)
  4607. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4608. "Regular expression for fast time stamp matching.")
  4609. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4610. "Regular expression for fast time stamp matching.")
  4611. (defconst org-ts-regexp0
  4612. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4613. "Regular expression matching time strings for analysis.
  4614. This one does not require the space after the date, so it can be used
  4615. on a string that terminates immediately after the date.")
  4616. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4617. "Regular expression matching time strings for analysis.")
  4618. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4619. "Regular expression matching time stamps, with groups.")
  4620. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4621. "Regular expression matching time stamps (also [..]), with groups.")
  4622. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4623. "Regular expression matching a time stamp range.")
  4624. (defconst org-tr-regexp-both
  4625. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4626. "Regular expression matching a time stamp range.")
  4627. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4628. org-ts-regexp "\\)?")
  4629. "Regular expression matching a time stamp or time stamp range.")
  4630. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4631. org-ts-regexp-both "\\)?")
  4632. "Regular expression matching a time stamp or time stamp range.
  4633. The time stamps may be either active or inactive.")
  4634. (defvar org-emph-face nil)
  4635. (defun org-do-emphasis-faces (limit)
  4636. "Run through the buffer and add overlays to emphasized strings."
  4637. (let (rtn a)
  4638. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4639. (if (not (= (char-after (match-beginning 3))
  4640. (char-after (match-beginning 4))))
  4641. (progn
  4642. (setq rtn t)
  4643. (setq a (assoc (match-string 3) org-emphasis-alist))
  4644. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4645. 'face
  4646. (nth 1 a))
  4647. (and (nth 4 a)
  4648. (org-remove-flyspell-overlays-in
  4649. (match-beginning 0) (match-end 0)))
  4650. (add-text-properties (match-beginning 2) (match-end 2)
  4651. '(font-lock-multiline t org-emphasis t))
  4652. (when org-hide-emphasis-markers
  4653. (add-text-properties (match-end 4) (match-beginning 5)
  4654. '(invisible org-link))
  4655. (add-text-properties (match-beginning 3) (match-end 3)
  4656. '(invisible org-link)))))
  4657. (backward-char 1))
  4658. rtn))
  4659. (defun org-emphasize (&optional char)
  4660. "Insert or change an emphasis, i.e. a font like bold or italic.
  4661. If there is an active region, change that region to a new emphasis.
  4662. If there is no region, just insert the marker characters and position
  4663. the cursor between them.
  4664. CHAR should be either the marker character, or the first character of the
  4665. HTML tag associated with that emphasis. If CHAR is a space, the means
  4666. to remove the emphasis of the selected region.
  4667. If char is not given (for example in an interactive call) it
  4668. will be prompted for."
  4669. (interactive)
  4670. (let ((eal org-emphasis-alist) e det
  4671. (erc org-emphasis-regexp-components)
  4672. (prompt "")
  4673. (string "") beg end move tag c s)
  4674. (if (org-region-active-p)
  4675. (setq beg (region-beginning) end (region-end)
  4676. string (buffer-substring beg end))
  4677. (setq move t))
  4678. (while (setq e (pop eal))
  4679. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4680. c (aref tag 0))
  4681. (push (cons c (string-to-char (car e))) det)
  4682. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4683. (substring tag 1)))))
  4684. (setq det (nreverse det))
  4685. (unless char
  4686. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4687. (setq char (read-char-exclusive)))
  4688. (setq char (or (cdr (assoc char det)) char))
  4689. (if (equal char ?\ )
  4690. (setq s "" move nil)
  4691. (unless (assoc (char-to-string char) org-emphasis-alist)
  4692. (error "No such emphasis marker: \"%c\"" char))
  4693. (setq s (char-to-string char)))
  4694. (while (and (> (length string) 1)
  4695. (equal (substring string 0 1) (substring string -1))
  4696. (assoc (substring string 0 1) org-emphasis-alist))
  4697. (setq string (substring string 1 -1)))
  4698. (setq string (concat s string s))
  4699. (if beg (delete-region beg end))
  4700. (unless (or (bolp)
  4701. (string-match (concat "[" (nth 0 erc) "\n]")
  4702. (char-to-string (char-before (point)))))
  4703. (insert " "))
  4704. (unless (or (eobp)
  4705. (string-match (concat "[" (nth 1 erc) "\n]")
  4706. (char-to-string (char-after (point)))))
  4707. (insert " ") (backward-char 1))
  4708. (insert string)
  4709. (and move (backward-char 1))))
  4710. (defconst org-nonsticky-props
  4711. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4712. (defsubst org-rear-nonsticky-at (pos)
  4713. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4714. (defun org-activate-plain-links (limit)
  4715. "Run through the buffer and add overlays to links."
  4716. (catch 'exit
  4717. (let (f hl)
  4718. (when (re-search-forward (concat org-plain-link-re) limit t)
  4719. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4720. (setq f (get-text-property (match-beginning 0) 'face))
  4721. (setq hl (org-match-string-no-properties 0))
  4722. (if (or (eq f 'org-tag)
  4723. (and (listp f) (memq 'org-tag f)))
  4724. nil
  4725. (add-text-properties (match-beginning 0) (match-end 0)
  4726. (list 'mouse-face 'highlight
  4727. 'face 'org-link
  4728. 'htmlize-link `(:uri ,hl)
  4729. 'keymap org-mouse-map))
  4730. (org-rear-nonsticky-at (match-end 0)))
  4731. t))))
  4732. (defun org-activate-code (limit)
  4733. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4734. (progn
  4735. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4736. (remove-text-properties (match-beginning 0) (match-end 0)
  4737. '(display t invisible t intangible t))
  4738. t)))
  4739. (defcustom org-src-fontify-natively nil
  4740. "When non-nil, fontify code in code blocks."
  4741. :type 'boolean
  4742. :version "24.1"
  4743. :group 'org-appearance
  4744. :group 'org-babel)
  4745. (defcustom org-allow-promoting-top-level-subtree nil
  4746. "When non-nil, allow promoting a top level subtree.
  4747. The leading star of the top level headline will be replaced
  4748. by a #."
  4749. :type 'boolean
  4750. :version "24.1"
  4751. :group 'org-appearance)
  4752. (defun org-fontify-meta-lines-and-blocks (limit)
  4753. (condition-case nil
  4754. (org-fontify-meta-lines-and-blocks-1 limit)
  4755. (error (message "org-mode fontification error"))))
  4756. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4757. "Fontify #+ lines and blocks, in the correct ways."
  4758. (let ((case-fold-search t))
  4759. (if (re-search-forward
  4760. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4761. limit t)
  4762. (let ((beg (match-beginning 0))
  4763. (block-start (match-end 0))
  4764. (block-end nil)
  4765. (lang (match-string 7))
  4766. (beg1 (line-beginning-position 2))
  4767. (dc1 (downcase (match-string 2)))
  4768. (dc3 (downcase (match-string 3)))
  4769. end end1 quoting block-type ovl)
  4770. (cond
  4771. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4772. ;; a single line of backend-specific content
  4773. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4774. (remove-text-properties (match-beginning 0) (match-end 0)
  4775. '(display t invisible t intangible t))
  4776. (add-text-properties (match-beginning 1) (match-end 3)
  4777. '(font-lock-fontified t face org-meta-line))
  4778. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4779. '(font-lock-fontified t face org-block))
  4780. ; for backend-specific code
  4781. t)
  4782. ((and (match-end 4) (equal dc3 "+begin"))
  4783. ;; Truly a block
  4784. (setq block-type (downcase (match-string 5))
  4785. quoting (member block-type org-protecting-blocks))
  4786. (when (re-search-forward
  4787. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4788. nil t) ;; on purpose, we look further than LIMIT
  4789. (setq end (min (point-max) (match-end 0))
  4790. end1 (min (point-max) (1- (match-beginning 0))))
  4791. (setq block-end (match-beginning 0))
  4792. (when quoting
  4793. (remove-text-properties beg end
  4794. '(display t invisible t intangible t)))
  4795. (add-text-properties
  4796. beg end
  4797. '(font-lock-fontified t font-lock-multiline t))
  4798. (add-text-properties beg beg1 '(face org-meta-line))
  4799. (add-text-properties end1 (min (point-max) (1+ end))
  4800. '(face org-meta-line)) ; for end_src
  4801. (cond
  4802. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4803. (org-src-font-lock-fontify-block lang block-start block-end)
  4804. ;; remove old background overlays
  4805. (mapc (lambda (ov)
  4806. (if (eq (overlay-get ov 'face) 'org-block-background)
  4807. (delete-overlay ov)))
  4808. (overlays-at (/ (+ beg1 block-end) 2)))
  4809. ;; add a background overlay
  4810. (setq ovl (make-overlay beg1 block-end))
  4811. (overlay-put ovl 'face 'org-block-background)
  4812. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4813. (quoting
  4814. (add-text-properties beg1 (min (point-max) (1+ end1))
  4815. '(face org-block))) ; end of source block
  4816. ((not org-fontify-quote-and-verse-blocks))
  4817. ((string= block-type "quote")
  4818. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4819. ((string= block-type "verse")
  4820. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4821. (add-text-properties beg beg1 '(face org-block-begin-line))
  4822. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4823. '(face org-block-end-line))
  4824. t))
  4825. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4826. (add-text-properties
  4827. beg (match-end 3)
  4828. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4829. '(font-lock-fontified t invisible t)
  4830. '(font-lock-fontified t face org-document-info-keyword)))
  4831. (add-text-properties
  4832. (match-beginning 6) (match-end 6)
  4833. (if (string-equal dc1 "+title:")
  4834. '(font-lock-fontified t face org-document-title)
  4835. '(font-lock-fontified t face org-document-info))))
  4836. ((or (equal dc1 "+results")
  4837. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4838. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4839. "+call:" "+header:" "+headers:" "+name:"))
  4840. (and (match-end 4) (equal dc3 "+attr")))
  4841. (add-text-properties
  4842. beg (match-end 0)
  4843. '(font-lock-fontified t face org-meta-line))
  4844. t)
  4845. ((member dc3 '(" " ""))
  4846. (add-text-properties
  4847. beg (match-end 0)
  4848. '(font-lock-fontified t face font-lock-comment-face)))
  4849. ((not (member (char-after beg) '(?\ ?\t)))
  4850. ;; just any other in-buffer setting, but not indented
  4851. (add-text-properties
  4852. beg (match-end 0)
  4853. '(font-lock-fontified t face org-meta-line))
  4854. t)
  4855. (t nil))))))
  4856. (defun org-activate-angle-links (limit)
  4857. "Run through the buffer and add overlays to links."
  4858. (if (re-search-forward org-angle-link-re limit t)
  4859. (progn
  4860. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4861. (add-text-properties (match-beginning 0) (match-end 0)
  4862. (list 'mouse-face 'highlight
  4863. 'keymap org-mouse-map))
  4864. (org-rear-nonsticky-at (match-end 0))
  4865. t)))
  4866. (defun org-activate-footnote-links (limit)
  4867. "Run through the buffer and add overlays to footnotes."
  4868. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4869. (when fn
  4870. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4871. (org-remove-flyspell-overlays-in beg end)
  4872. (add-text-properties beg end
  4873. (list 'mouse-face 'highlight
  4874. 'keymap org-mouse-map
  4875. 'help-echo
  4876. (if (= (point-at-bol) beg)
  4877. "Footnote definition"
  4878. "Footnote reference")
  4879. 'font-lock-fontified t
  4880. 'font-lock-multiline t
  4881. 'face 'org-footnote))))))
  4882. (defun org-activate-bracket-links (limit)
  4883. "Run through the buffer and add overlays to bracketed links."
  4884. (if (re-search-forward org-bracket-link-regexp limit t)
  4885. (let* ((hl (org-match-string-no-properties 1))
  4886. (help (concat "LINK: " hl))
  4887. ;; FIXME: Above we should remove the escapes. But that
  4888. ;; requires another match, protecting match data, a lot
  4889. ;; of overhead for font-lock.
  4890. (ip (org-maybe-intangible
  4891. (list 'invisible 'org-link
  4892. 'keymap org-mouse-map 'mouse-face 'highlight
  4893. 'font-lock-multiline t 'help-echo help
  4894. 'htmlize-link `(:uri ,hl))))
  4895. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4896. 'font-lock-multiline t 'help-echo help
  4897. 'htmlize-link `(:uri ,hl))))
  4898. ;; We need to remove the invisible property here. Table narrowing
  4899. ;; may have made some of this invisible.
  4900. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4901. (remove-text-properties (match-beginning 0) (match-end 0)
  4902. '(invisible nil))
  4903. (if (match-end 3)
  4904. (progn
  4905. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4906. (org-rear-nonsticky-at (match-beginning 3))
  4907. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4908. (org-rear-nonsticky-at (match-end 3))
  4909. (add-text-properties (match-end 3) (match-end 0) ip)
  4910. (org-rear-nonsticky-at (match-end 0)))
  4911. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4912. (org-rear-nonsticky-at (match-beginning 1))
  4913. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4914. (org-rear-nonsticky-at (match-end 1))
  4915. (add-text-properties (match-end 1) (match-end 0) ip)
  4916. (org-rear-nonsticky-at (match-end 0)))
  4917. t)))
  4918. (defun org-activate-dates (limit)
  4919. "Run through the buffer and add overlays to dates."
  4920. (if (re-search-forward org-tsr-regexp-both limit t)
  4921. (progn
  4922. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4923. (add-text-properties (match-beginning 0) (match-end 0)
  4924. (list 'mouse-face 'highlight
  4925. 'keymap org-mouse-map))
  4926. (org-rear-nonsticky-at (match-end 0))
  4927. (when org-display-custom-times
  4928. (if (match-end 3)
  4929. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4930. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4931. t)))
  4932. (defvar org-target-link-regexp nil
  4933. "Regular expression matching radio targets in plain text.")
  4934. (make-variable-buffer-local 'org-target-link-regexp)
  4935. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4936. "Regular expression matching a link target.")
  4937. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4938. "Regular expression matching a radio target.")
  4939. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4940. "Regular expression matching any target.")
  4941. (defun org-activate-target-links (limit)
  4942. "Run through the buffer and add overlays to target matches."
  4943. (when org-target-link-regexp
  4944. (let ((case-fold-search t))
  4945. (if (re-search-forward org-target-link-regexp limit t)
  4946. (progn
  4947. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4948. (add-text-properties (match-beginning 0) (match-end 0)
  4949. (list 'mouse-face 'highlight
  4950. 'keymap org-mouse-map
  4951. 'help-echo "Radio target link"
  4952. 'org-linked-text t))
  4953. (org-rear-nonsticky-at (match-end 0))
  4954. t)))))
  4955. (defun org-update-radio-target-regexp ()
  4956. "Find all radio targets in this file and update the regular expression."
  4957. (interactive)
  4958. (when (memq 'radio org-activate-links)
  4959. (setq org-target-link-regexp
  4960. (org-make-target-link-regexp (org-all-targets 'radio)))
  4961. (org-restart-font-lock)))
  4962. (defun org-hide-wide-columns (limit)
  4963. (let (s e)
  4964. (setq s (text-property-any (point) (or limit (point-max))
  4965. 'org-cwidth t))
  4966. (when s
  4967. (setq e (next-single-property-change s 'org-cwidth))
  4968. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4969. (goto-char e)
  4970. t)))
  4971. (defvar org-latex-and-specials-regexp nil
  4972. "Regular expression for highlighting export special stuff.")
  4973. (defvar org-match-substring-regexp)
  4974. (defvar org-match-substring-with-braces-regexp)
  4975. ;; This should be with the exporter code, but we also use if for font-locking
  4976. (defconst org-export-html-special-string-regexps
  4977. '(("\\\\-" . "&shy;")
  4978. ("---\\([^-]\\)" . "&mdash;\\1")
  4979. ("--\\([^-]\\)" . "&ndash;\\1")
  4980. ("\\.\\.\\." . "&hellip;"))
  4981. "Regular expressions for special string conversion.")
  4982. (defun org-compute-latex-and-specials-regexp ()
  4983. "Compute regular expression for stuff treated specially by exporters."
  4984. (if (not org-highlight-latex-fragments-and-specials)
  4985. (org-set-local 'org-latex-and-specials-regexp nil)
  4986. (require 'org-exp)
  4987. (let*
  4988. ((matchers (plist-get org-format-latex-options :matchers))
  4989. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4990. org-latex-regexps)))
  4991. (org-export-allow-BIND nil)
  4992. (options (org-combine-plists (org-default-export-plist)
  4993. (org-infile-export-plist)))
  4994. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4995. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4996. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4997. (org-export-html-expand (plist-get options :expand-quoted-html))
  4998. (org-export-with-special-strings (plist-get options :special-strings))
  4999. (re-sub
  5000. (cond
  5001. ((equal org-export-with-sub-superscripts '{})
  5002. (list org-match-substring-with-braces-regexp))
  5003. (org-export-with-sub-superscripts
  5004. (list org-match-substring-regexp))))
  5005. (re-latex
  5006. (if org-export-with-LaTeX-fragments
  5007. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5008. (re-macros
  5009. (if org-export-with-TeX-macros
  5010. (list (concat "\\\\"
  5011. (regexp-opt
  5012. (append
  5013. (delq nil
  5014. (mapcar 'car-safe
  5015. (append org-entities-user
  5016. org-entities)))
  5017. (if (boundp 'org-latex-entities)
  5018. (mapcar (lambda (x)
  5019. (or (car-safe x) x))
  5020. org-latex-entities)
  5021. nil))
  5022. 'words))) ; FIXME
  5023. ))
  5024. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5025. (re-special (if org-export-with-special-strings
  5026. (mapcar (lambda (x) (car x))
  5027. org-export-html-special-string-regexps)))
  5028. (re-rest
  5029. (delq nil
  5030. (list
  5031. (if org-export-html-expand "@<[^>\n]+>")
  5032. ))))
  5033. (org-set-local
  5034. 'org-latex-and-specials-regexp
  5035. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5036. re-rest) "\\|")))))
  5037. (defun org-do-latex-and-special-faces (limit)
  5038. "Run through the buffer and add overlays to links."
  5039. (when org-latex-and-specials-regexp
  5040. (let (rtn d)
  5041. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5042. limit t))
  5043. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5044. 'face))
  5045. '(org-code org-verbatim underline)))
  5046. (progn
  5047. (setq rtn t
  5048. d (cond ((member (char-after (1+ (match-beginning 0)))
  5049. '(?_ ?^)) 1)
  5050. (t 0)))
  5051. (font-lock-prepend-text-property
  5052. (+ d (match-beginning 0)) (match-end 0)
  5053. 'face 'org-latex-and-export-specials)
  5054. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5055. '(font-lock-multiline t)))))
  5056. rtn)))
  5057. (defun org-restart-font-lock ()
  5058. "Restart `font-lock-mode', to force refontification."
  5059. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5060. (font-lock-mode -1)
  5061. (font-lock-mode 1)))
  5062. (defun org-all-targets (&optional radio)
  5063. "Return a list of all targets in this file.
  5064. When optional argument RADIO is non-nil, only find radio
  5065. targets."
  5066. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5067. (save-excursion
  5068. (goto-char (point-min))
  5069. (while (re-search-forward re nil t)
  5070. (let ((obj (org-element-context)))
  5071. (when (memq (org-element-type obj) '(radio-target target))
  5072. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5073. rtn)))
  5074. (defun org-make-target-link-regexp (targets)
  5075. "Make regular expression matching all strings in TARGETS.
  5076. The regular expression finds the targets also if there is a line break
  5077. between words."
  5078. (and targets
  5079. (concat
  5080. "\\<\\("
  5081. (mapconcat
  5082. (lambda (x)
  5083. (setq x (regexp-quote x))
  5084. (while (string-match " +" x)
  5085. (setq x (replace-match "\\s-+" t t x)))
  5086. x)
  5087. targets
  5088. "\\|")
  5089. "\\)\\>")))
  5090. (defun org-activate-tags (limit)
  5091. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5092. (progn
  5093. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5094. (add-text-properties (match-beginning 1) (match-end 1)
  5095. (list 'mouse-face 'highlight
  5096. 'keymap org-mouse-map))
  5097. (org-rear-nonsticky-at (match-end 1))
  5098. t)))
  5099. (defun org-outline-level ()
  5100. "Compute the outline level of the heading at point.
  5101. If this is called at a normal headline, the level is the number of stars.
  5102. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5103. (save-excursion
  5104. (if (not (condition-case nil
  5105. (org-back-to-heading t)
  5106. (error nil)))
  5107. 0
  5108. (looking-at org-outline-regexp)
  5109. (1- (- (match-end 0) (match-beginning 0))))))
  5110. (defvar org-font-lock-keywords nil)
  5111. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5112. "Regular expression matching a property line.")
  5113. (defvar org-font-lock-hook nil
  5114. "Functions to be called for special font lock stuff.")
  5115. (defvar org-font-lock-set-keywords-hook nil
  5116. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5117. This is called after `org-font-lock-extra-keywords' is defined, but before
  5118. it is installed to be used by font lock. This can be useful if something
  5119. needs to be inserted at a specific position in the font-lock sequence.")
  5120. (defun org-font-lock-hook (limit)
  5121. "Run `org-font-lock-hook' within LIMIT."
  5122. (run-hook-with-args 'org-font-lock-hook limit))
  5123. (defun org-set-font-lock-defaults ()
  5124. "Set font lock defaults for the current buffer."
  5125. (let* ((em org-fontify-emphasized-text)
  5126. (lk org-activate-links)
  5127. (org-font-lock-extra-keywords
  5128. (list
  5129. ;; Call the hook
  5130. '(org-font-lock-hook)
  5131. ;; Headlines
  5132. `(,(if org-fontify-whole-heading-line
  5133. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5134. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5135. (1 (org-get-level-face 1))
  5136. (2 (org-get-level-face 2))
  5137. (3 (org-get-level-face 3)))
  5138. ;; Table lines
  5139. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5140. (1 'org-table t))
  5141. ;; Table internals
  5142. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5143. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5144. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5145. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5146. ;; Drawers
  5147. (list org-drawer-regexp '(0 'org-special-keyword t))
  5148. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5149. ;; Properties
  5150. (list org-property-re
  5151. '(1 'org-special-keyword t)
  5152. '(3 'org-property-value t))
  5153. ;; Links
  5154. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5155. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5156. (if (memq 'plain lk) '(org-activate-plain-links))
  5157. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5158. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5159. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5160. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5161. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5162. '(org-hide-wide-columns (0 nil append))
  5163. ;; TODO keyword
  5164. (list (format org-heading-keyword-regexp-format
  5165. org-todo-regexp)
  5166. '(2 (org-get-todo-face 2) t))
  5167. ;; DONE
  5168. (if org-fontify-done-headline
  5169. (list (format org-heading-keyword-regexp-format
  5170. (concat
  5171. "\\(?:"
  5172. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5173. "\\)"))
  5174. '(2 'org-headline-done t))
  5175. nil)
  5176. ;; Priorities
  5177. '(org-font-lock-add-priority-faces)
  5178. ;; Tags
  5179. '(org-font-lock-add-tag-faces)
  5180. ;; Special keywords
  5181. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5182. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5183. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5184. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5185. ;; Emphasis
  5186. (if em
  5187. (if (featurep 'xemacs)
  5188. '(org-do-emphasis-faces (0 nil append))
  5189. '(org-do-emphasis-faces)))
  5190. ;; Checkboxes
  5191. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5192. 1 'org-checkbox prepend)
  5193. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5194. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5195. (0 (org-get-checkbox-statistics-face) t)))
  5196. ;; Description list items
  5197. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5198. 1 'org-list-dt prepend)
  5199. ;; ARCHIVEd headings
  5200. (list (concat
  5201. org-outline-regexp-bol
  5202. "\\(.*:" org-archive-tag ":.*\\)")
  5203. '(1 'org-archived prepend))
  5204. ;; Specials
  5205. '(org-do-latex-and-special-faces)
  5206. '(org-fontify-entities)
  5207. '(org-raise-scripts)
  5208. ;; Code
  5209. '(org-activate-code (1 'org-code t))
  5210. ;; COMMENT
  5211. (list (format org-heading-keyword-regexp-format
  5212. (concat "\\("
  5213. org-comment-string "\\|" org-quote-string
  5214. "\\)"))
  5215. '(2 'org-special-keyword t))
  5216. ;; Blocks and meta lines
  5217. '(org-fontify-meta-lines-and-blocks)
  5218. )))
  5219. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5220. (run-hooks 'org-font-lock-set-keywords-hook)
  5221. ;; Now set the full font-lock-keywords
  5222. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5223. (org-set-local 'font-lock-defaults
  5224. '(org-font-lock-keywords t nil nil backward-paragraph))
  5225. (kill-local-variable 'font-lock-keywords) nil))
  5226. (defun org-toggle-pretty-entities ()
  5227. "Toggle the composition display of entities as UTF8 characters."
  5228. (interactive)
  5229. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5230. (org-restart-font-lock)
  5231. (if org-pretty-entities
  5232. (message "Entities are displayed as UTF8 characters")
  5233. (save-restriction
  5234. (widen)
  5235. (org-decompose-region (point-min) (point-max))
  5236. (message "Entities are displayed plain"))))
  5237. (defvar org-custom-properties-overlays nil
  5238. "List of overlays used for custom properties.")
  5239. (make-variable-buffer-local 'org-custom-properties-overlays)
  5240. (defun org-toggle-custom-properties-visibility ()
  5241. "Display or hide properties in `org-custom-properties'."
  5242. (interactive)
  5243. (if org-custom-properties-overlays
  5244. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5245. (setq org-custom-properties-overlays nil))
  5246. (unless (not org-custom-properties)
  5247. (save-excursion
  5248. (save-restriction
  5249. (widen)
  5250. (goto-char (point-min))
  5251. (while (re-search-forward org-property-re nil t)
  5252. (mapc (lambda(p)
  5253. (when (equal p (substring (match-string 1) 1 -1))
  5254. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5255. (overlay-put o 'invisible t)
  5256. (overlay-put o 'org-custom-property t)
  5257. (push o org-custom-properties-overlays))))
  5258. org-custom-properties)))))))
  5259. (defun org-fontify-entities (limit)
  5260. "Find an entity to fontify."
  5261. (let (ee)
  5262. (when org-pretty-entities
  5263. (catch 'match
  5264. (while (re-search-forward
  5265. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5266. limit t)
  5267. (if (and (not (org-in-indented-comment-line))
  5268. (setq ee (org-entity-get (match-string 1)))
  5269. (= (length (nth 6 ee)) 1))
  5270. (let*
  5271. ((end (if (equal (match-string 2) "{}")
  5272. (match-end 2)
  5273. (match-end 1))))
  5274. (add-text-properties
  5275. (match-beginning 0) end
  5276. (list 'font-lock-fontified t))
  5277. (compose-region (match-beginning 0) end
  5278. (nth 6 ee) nil)
  5279. (backward-char 1)
  5280. (throw 'match t))))
  5281. nil))))
  5282. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5283. "Fontify string S like in Org-mode."
  5284. (with-temp-buffer
  5285. (insert s)
  5286. (let ((org-odd-levels-only odd-levels))
  5287. (org-mode)
  5288. (font-lock-fontify-buffer)
  5289. (buffer-string))))
  5290. (defvar org-m nil)
  5291. (defvar org-l nil)
  5292. (defvar org-f nil)
  5293. (defun org-get-level-face (n)
  5294. "Get the right face for match N in font-lock matching of headlines."
  5295. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5296. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5297. (if org-cycle-level-faces
  5298. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5299. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5300. (cond
  5301. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5302. ((eq n 2) org-f)
  5303. (t (if org-level-color-stars-only nil org-f))))
  5304. (defun org-get-todo-face (kwd)
  5305. "Get the right face for a TODO keyword KWD.
  5306. If KWD is a number, get the corresponding match group."
  5307. (if (numberp kwd) (setq kwd (match-string kwd)))
  5308. (or (org-face-from-face-or-color
  5309. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5310. (and (member kwd org-done-keywords) 'org-done)
  5311. 'org-todo))
  5312. (defun org-face-from-face-or-color (context inherit face-or-color)
  5313. "Create a face list that inherits INHERIT, but sets the foreground color.
  5314. When FACE-OR-COLOR is not a string, just return it."
  5315. (if (stringp face-or-color)
  5316. (list :inherit inherit
  5317. (cdr (assoc context org-faces-easy-properties))
  5318. face-or-color)
  5319. face-or-color))
  5320. (defun org-font-lock-add-tag-faces (limit)
  5321. "Add the special tag faces."
  5322. (when (and org-tag-faces org-tags-special-faces-re)
  5323. (while (re-search-forward org-tags-special-faces-re limit t)
  5324. (add-text-properties (match-beginning 1) (match-end 1)
  5325. (list 'face (org-get-tag-face 1)
  5326. 'font-lock-fontified t))
  5327. (backward-char 1))))
  5328. (defun org-font-lock-add-priority-faces (limit)
  5329. "Add the special priority faces."
  5330. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5331. (when (save-match-data (org-at-heading-p))
  5332. (add-text-properties
  5333. (match-beginning 0) (match-end 0)
  5334. (list 'face (or (org-face-from-face-or-color
  5335. 'priority 'org-special-keyword
  5336. (cdr (assoc (char-after (match-beginning 1))
  5337. org-priority-faces)))
  5338. 'org-special-keyword)
  5339. 'font-lock-fontified t)))))
  5340. (defun org-get-tag-face (kwd)
  5341. "Get the right face for a TODO keyword KWD.
  5342. If KWD is a number, get the corresponding match group."
  5343. (if (numberp kwd) (setq kwd (match-string kwd)))
  5344. (or (org-face-from-face-or-color
  5345. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5346. 'org-tag))
  5347. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5348. "Remove fontification and activation overlays from links."
  5349. (font-lock-default-unfontify-region beg end)
  5350. (let* ((buffer-undo-list t)
  5351. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5352. (inhibit-modification-hooks t)
  5353. deactivate-mark buffer-file-name buffer-file-truename)
  5354. (org-decompose-region beg end)
  5355. (remove-text-properties beg end
  5356. '(mouse-face t keymap t org-linked-text t
  5357. invisible t intangible t
  5358. org-no-flyspell t org-emphasis t))
  5359. (org-remove-font-lock-display-properties beg end)))
  5360. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5361. ((raise 0.3) (height 0.7))
  5362. ((raise -0.5))
  5363. ((raise 0.5)))
  5364. "Display properties for showing superscripts and subscripts.")
  5365. (defun org-remove-font-lock-display-properties (beg end)
  5366. "Remove specific display properties that have been added by font lock.
  5367. The will remove the raise properties that are used to show superscripts
  5368. and subscripts."
  5369. (let (next prop)
  5370. (while (< beg end)
  5371. (setq next (next-single-property-change beg 'display nil end)
  5372. prop (get-text-property beg 'display))
  5373. (if (member prop org-script-display)
  5374. (put-text-property beg next 'display nil))
  5375. (setq beg next))))
  5376. (defun org-raise-scripts (limit)
  5377. "Add raise properties to sub/superscripts."
  5378. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5379. (if (re-search-forward
  5380. (if (eq org-use-sub-superscripts t)
  5381. org-match-substring-regexp
  5382. org-match-substring-with-braces-regexp)
  5383. limit t)
  5384. (let* ((pos (point)) table-p comment-p
  5385. (mpos (match-beginning 3))
  5386. (emph-p (get-text-property mpos 'org-emphasis))
  5387. (link-p (get-text-property mpos 'mouse-face))
  5388. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5389. (goto-char (point-at-bol))
  5390. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5391. comment-p (org-looking-at-p "[ \t]*#"))
  5392. (goto-char pos)
  5393. ;; FIXME: Should we go back one character here, for a_b^c
  5394. ;; (goto-char (1- pos)) ;????????????????????
  5395. (if (or comment-p emph-p link-p keyw-p)
  5396. t
  5397. (put-text-property (match-beginning 3) (match-end 0)
  5398. 'display
  5399. (if (equal (char-after (match-beginning 2)) ?^)
  5400. (nth (if table-p 3 1) org-script-display)
  5401. (nth (if table-p 2 0) org-script-display)))
  5402. (add-text-properties (match-beginning 2) (match-end 2)
  5403. (list 'invisible t
  5404. 'org-dwidth t 'org-dwidth-n 1))
  5405. (if (and (eq (char-after (match-beginning 3)) ?{)
  5406. (eq (char-before (match-end 3)) ?}))
  5407. (progn
  5408. (add-text-properties
  5409. (match-beginning 3) (1+ (match-beginning 3))
  5410. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5411. (add-text-properties
  5412. (1- (match-end 3)) (match-end 3)
  5413. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5414. t)))))
  5415. ;;;; Visibility cycling, including org-goto and indirect buffer
  5416. ;;; Cycling
  5417. (defvar org-cycle-global-status nil)
  5418. (make-variable-buffer-local 'org-cycle-global-status)
  5419. (defvar org-cycle-subtree-status nil)
  5420. (make-variable-buffer-local 'org-cycle-subtree-status)
  5421. (defvar org-inlinetask-min-level)
  5422. ;;;###autoload
  5423. (defun org-cycle (&optional arg)
  5424. "TAB-action and visibility cycling for Org-mode.
  5425. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5426. is outline visibility cycling, but it also invokes other actions
  5427. in special contexts.
  5428. - When this function is called with a prefix argument, rotate the entire
  5429. buffer through 3 states (global cycling)
  5430. 1. OVERVIEW: Show only top-level headlines.
  5431. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5432. 3. SHOW ALL: Show everything.
  5433. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5434. determined by the variable `org-startup-folded', and by any VISIBILITY
  5435. properties in the buffer.
  5436. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5437. including any drawers.
  5438. - When inside a table, re-align the table and move to the next field.
  5439. - When point is at the beginning of a headline, rotate the subtree started
  5440. by this line through 3 different states (local cycling)
  5441. 1. FOLDED: Only the main headline is shown.
  5442. 2. CHILDREN: The main headline and the direct children are shown.
  5443. From this state, you can move to one of the children
  5444. and zoom in further.
  5445. 3. SUBTREE: Show the entire subtree, including body text.
  5446. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5447. - When point is at the beginning of an empty headline and the variable
  5448. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5449. of the headline by demoting and promoting it to likely levels. This
  5450. speeds up creation document structure by pressing TAB once or several
  5451. times right after creating a new headline.
  5452. - When there is a numeric prefix, go up to a heading with level ARG, do
  5453. a `show-subtree' and return to the previous cursor position. If ARG
  5454. is negative, go up that many levels.
  5455. - When point is not at the beginning of a headline, execute the global
  5456. binding for TAB, which is re-indenting the line. See the option
  5457. `org-cycle-emulate-tab' for details.
  5458. - Special case: if point is at the beginning of the buffer and there is
  5459. no headline in line 1, this function will act as if called with prefix arg
  5460. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5461. But only if also the variable `org-cycle-global-at-bob' is t."
  5462. (interactive "P")
  5463. (org-load-modules-maybe)
  5464. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5465. (and org-cycle-level-after-item/entry-creation
  5466. (or (org-cycle-level)
  5467. (org-cycle-item-indentation))))
  5468. (let* ((limit-level
  5469. (or org-cycle-max-level
  5470. (and (boundp 'org-inlinetask-min-level)
  5471. org-inlinetask-min-level
  5472. (1- org-inlinetask-min-level))))
  5473. (nstars (and limit-level
  5474. (if org-odd-levels-only
  5475. (and limit-level (1- (* limit-level 2)))
  5476. limit-level)))
  5477. (org-outline-regexp
  5478. (if (not (derived-mode-p 'org-mode))
  5479. outline-regexp
  5480. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5481. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5482. (not (looking-at org-outline-regexp))))
  5483. (org-cycle-hook
  5484. (if bob-special
  5485. (delq 'org-optimize-window-after-visibility-change
  5486. (copy-sequence org-cycle-hook))
  5487. org-cycle-hook))
  5488. (pos (point)))
  5489. (if (or bob-special (equal arg '(4)))
  5490. ;; special case: use global cycling
  5491. (setq arg t))
  5492. (cond
  5493. ((equal arg '(16))
  5494. (setq last-command 'dummy)
  5495. (org-set-startup-visibility)
  5496. (message "Startup visibility, plus VISIBILITY properties"))
  5497. ((equal arg '(64))
  5498. (show-all)
  5499. (message "Entire buffer visible, including drawers"))
  5500. ;; Table: enter it or move to the next field.
  5501. ((org-at-table-p 'any)
  5502. (if (org-at-table.el-p)
  5503. (message "Use C-c ' to edit table.el tables")
  5504. (if arg (org-table-edit-field t)
  5505. (org-table-justify-field-maybe)
  5506. (call-interactively 'org-table-next-field))))
  5507. ((run-hook-with-args-until-success
  5508. 'org-tab-after-check-for-table-hook))
  5509. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5510. ((eq arg t) (org-cycle-internal-global))
  5511. ;; Drawers: delegate to `org-flag-drawer'.
  5512. ((and org-drawers org-drawer-regexp
  5513. (save-excursion
  5514. (beginning-of-line 1)
  5515. (looking-at org-drawer-regexp)))
  5516. (org-flag-drawer ; toggle block visibility
  5517. (not (get-char-property (match-end 0) 'invisible))))
  5518. ;; Show-subtree, ARG levels up from here.
  5519. ((integerp arg)
  5520. (save-excursion
  5521. (org-back-to-heading)
  5522. (outline-up-heading (if (< arg 0) (- arg)
  5523. (- (funcall outline-level) arg)))
  5524. (org-show-subtree)))
  5525. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5526. ((and (featurep 'org-inlinetask)
  5527. (org-inlinetask-at-task-p)
  5528. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5529. (org-inlinetask-toggle-visibility))
  5530. ((org-try-cdlatex-tab))
  5531. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5532. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5533. (save-excursion (beginning-of-line 1)
  5534. (looking-at org-outline-regexp)))
  5535. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5536. (org-cycle-internal-local))
  5537. ;; From there: TAB emulation and template completion.
  5538. (buffer-read-only (org-back-to-heading))
  5539. ((run-hook-with-args-until-success
  5540. 'org-tab-after-check-for-cycling-hook))
  5541. ((org-try-structure-completion))
  5542. ((run-hook-with-args-until-success
  5543. 'org-tab-before-tab-emulation-hook))
  5544. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5545. (or (not (bolp))
  5546. (not (looking-at org-outline-regexp))))
  5547. (call-interactively (global-key-binding "\t")))
  5548. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5549. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5550. (or (and (eq org-cycle-emulate-tab 'white)
  5551. (= (match-end 0) (point-at-eol)))
  5552. (and (eq org-cycle-emulate-tab 'whitestart)
  5553. (>= (match-end 0) pos))))
  5554. t
  5555. (eq org-cycle-emulate-tab t))
  5556. (call-interactively (global-key-binding "\t")))
  5557. (t (save-excursion
  5558. (org-back-to-heading)
  5559. (org-cycle)))))))
  5560. (defun org-cycle-internal-global ()
  5561. "Do the global cycling action."
  5562. ;; Hack to avoid display of messages for .org attachments in Gnus
  5563. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5564. (cond
  5565. ((and (eq last-command this-command)
  5566. (eq org-cycle-global-status 'overview))
  5567. ;; We just created the overview - now do table of contents
  5568. ;; This can be slow in very large buffers, so indicate action
  5569. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5570. (unless ga (message "CONTENTS..."))
  5571. (org-content)
  5572. (unless ga (message "CONTENTS...done"))
  5573. (setq org-cycle-global-status 'contents)
  5574. (run-hook-with-args 'org-cycle-hook 'contents))
  5575. ((and (eq last-command this-command)
  5576. (eq org-cycle-global-status 'contents))
  5577. ;; We just showed the table of contents - now show everything
  5578. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5579. (show-all)
  5580. (unless ga (message "SHOW ALL"))
  5581. (setq org-cycle-global-status 'all)
  5582. (run-hook-with-args 'org-cycle-hook 'all))
  5583. (t
  5584. ;; Default action: go to overview
  5585. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5586. (org-overview)
  5587. (unless ga (message "OVERVIEW"))
  5588. (setq org-cycle-global-status 'overview)
  5589. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5590. (defun org-cycle-internal-local ()
  5591. "Do the local cycling action."
  5592. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5593. ;; First, determine end of headline (EOH), end of subtree or item
  5594. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5595. (save-excursion
  5596. (if (org-at-item-p)
  5597. (progn
  5598. (beginning-of-line)
  5599. (setq struct (org-list-struct))
  5600. (setq eoh (point-at-eol))
  5601. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5602. (setq has-children (org-list-has-child-p (point) struct)))
  5603. (org-back-to-heading)
  5604. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5605. (setq eos (save-excursion
  5606. (org-end-of-subtree t)
  5607. (unless (eobp)
  5608. (skip-chars-forward " \t\n"))
  5609. (if (eobp) (point) (1- (point)))))
  5610. (setq has-children
  5611. (or (save-excursion
  5612. (let ((level (funcall outline-level)))
  5613. (outline-next-heading)
  5614. (and (org-at-heading-p t)
  5615. (> (funcall outline-level) level))))
  5616. (save-excursion
  5617. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5618. ;; Determine end invisible part of buffer (EOL)
  5619. (beginning-of-line 2)
  5620. ;; XEmacs doesn't have `next-single-char-property-change'
  5621. (if (featurep 'xemacs)
  5622. (while (and (not (eobp)) ;; this is like `next-line'
  5623. (get-char-property (1- (point)) 'invisible))
  5624. (beginning-of-line 2))
  5625. (while (and (not (eobp)) ;; this is like `next-line'
  5626. (get-char-property (1- (point)) 'invisible))
  5627. (goto-char (next-single-char-property-change (point) 'invisible))
  5628. (and (eolp) (beginning-of-line 2))))
  5629. (setq eol (point)))
  5630. ;; Find out what to do next and set `this-command'
  5631. (cond
  5632. ((= eos eoh)
  5633. ;; Nothing is hidden behind this heading
  5634. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5635. (message "EMPTY ENTRY")
  5636. (setq org-cycle-subtree-status nil)
  5637. (save-excursion
  5638. (goto-char eos)
  5639. (outline-next-heading)
  5640. (if (outline-invisible-p) (org-flag-heading nil))))
  5641. ((and (or (>= eol eos)
  5642. (not (string-match "\\S-" (buffer-substring eol eos))))
  5643. (or has-children
  5644. (not (setq children-skipped
  5645. org-cycle-skip-children-state-if-no-children))))
  5646. ;; Entire subtree is hidden in one line: children view
  5647. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5648. (if (org-at-item-p)
  5649. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5650. (org-show-entry)
  5651. (org-with-limited-levels (show-children))
  5652. ;; FIXME: This slows down the func way too much.
  5653. ;; How keep drawers hidden in subtree anyway?
  5654. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5655. ;; (org-cycle-hide-drawers 'subtree))
  5656. ;; Fold every list in subtree to top-level items.
  5657. (when (eq org-cycle-include-plain-lists 'integrate)
  5658. (save-excursion
  5659. (org-back-to-heading)
  5660. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5661. (beginning-of-line 1)
  5662. (let* ((struct (org-list-struct))
  5663. (prevs (org-list-prevs-alist struct))
  5664. (end (org-list-get-bottom-point struct)))
  5665. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5666. (org-list-get-all-items (point) struct prevs))
  5667. (goto-char end))))))
  5668. (message "CHILDREN")
  5669. (save-excursion
  5670. (goto-char eos)
  5671. (outline-next-heading)
  5672. (if (outline-invisible-p) (org-flag-heading nil)))
  5673. (setq org-cycle-subtree-status 'children)
  5674. (run-hook-with-args 'org-cycle-hook 'children))
  5675. ((or children-skipped
  5676. (and (eq last-command this-command)
  5677. (eq org-cycle-subtree-status 'children)))
  5678. ;; We just showed the children, or no children are there,
  5679. ;; now show everything.
  5680. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5681. (outline-flag-region eoh eos nil)
  5682. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5683. (setq org-cycle-subtree-status 'subtree)
  5684. (run-hook-with-args 'org-cycle-hook 'subtree))
  5685. (t
  5686. ;; Default action: hide the subtree.
  5687. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5688. (outline-flag-region eoh eos t)
  5689. (message "FOLDED")
  5690. (setq org-cycle-subtree-status 'folded)
  5691. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5692. ;;;###autoload
  5693. (defun org-global-cycle (&optional arg)
  5694. "Cycle the global visibility. For details see `org-cycle'.
  5695. With \\[universal-argument] prefix arg, switch to startup visibility.
  5696. With a numeric prefix, show all headlines up to that level."
  5697. (interactive "P")
  5698. (let ((org-cycle-include-plain-lists
  5699. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5700. (cond
  5701. ((integerp arg)
  5702. (show-all)
  5703. (hide-sublevels arg)
  5704. (setq org-cycle-global-status 'contents))
  5705. ((equal arg '(4))
  5706. (org-set-startup-visibility)
  5707. (message "Startup visibility, plus VISIBILITY properties."))
  5708. (t
  5709. (org-cycle '(4))))))
  5710. (defun org-set-startup-visibility ()
  5711. "Set the visibility required by startup options and properties."
  5712. (cond
  5713. ((eq org-startup-folded t)
  5714. (org-cycle '(4)))
  5715. ((eq org-startup-folded 'content)
  5716. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5717. (org-cycle '(4)) (org-cycle '(4)))))
  5718. (unless (eq org-startup-folded 'showeverything)
  5719. (if org-hide-block-startup (org-hide-block-all))
  5720. (org-set-visibility-according-to-property 'no-cleanup)
  5721. (org-cycle-hide-archived-subtrees 'all)
  5722. (org-cycle-hide-drawers 'all)
  5723. (org-cycle-show-empty-lines t)))
  5724. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5725. "Switch subtree visibilities according to :VISIBILITY: property."
  5726. (interactive)
  5727. (let (org-show-entry-below state)
  5728. (save-excursion
  5729. (goto-char (point-min))
  5730. (while (re-search-forward
  5731. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5732. nil t)
  5733. (setq state (match-string 1))
  5734. (save-excursion
  5735. (org-back-to-heading t)
  5736. (hide-subtree)
  5737. (org-reveal)
  5738. (cond
  5739. ((equal state '("fold" "folded"))
  5740. (hide-subtree))
  5741. ((equal state "children")
  5742. (org-show-hidden-entry)
  5743. (show-children))
  5744. ((equal state "content")
  5745. (save-excursion
  5746. (save-restriction
  5747. (org-narrow-to-subtree)
  5748. (org-content))))
  5749. ((member state '("all" "showall"))
  5750. (show-subtree)))))
  5751. (unless no-cleanup
  5752. (org-cycle-hide-archived-subtrees 'all)
  5753. (org-cycle-hide-drawers 'all)
  5754. (org-cycle-show-empty-lines 'all)))))
  5755. ;; This function uses outline-regexp instead of the more fundamental
  5756. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5757. ;; buffers, where outline-regexp is needed.
  5758. (defun org-overview ()
  5759. "Switch to overview mode, showing only top-level headlines.
  5760. Really, this shows all headlines with level equal or greater than the level
  5761. of the first headline in the buffer. This is important, because if the
  5762. first headline is not level one, then (hide-sublevels 1) gives confusing
  5763. results."
  5764. (interactive)
  5765. (let ((level (save-excursion
  5766. (goto-char (point-min))
  5767. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5768. (progn
  5769. (goto-char (match-beginning 0))
  5770. (funcall outline-level))))))
  5771. (and level (hide-sublevels level))))
  5772. (defun org-content (&optional arg)
  5773. "Show all headlines in the buffer, like a table of contents.
  5774. With numerical argument N, show content up to level N."
  5775. (interactive "P")
  5776. (save-excursion
  5777. ;; Visit all headings and show their offspring
  5778. (and (integerp arg) (org-overview))
  5779. (goto-char (point-max))
  5780. (catch 'exit
  5781. (while (and (progn (condition-case nil
  5782. (outline-previous-visible-heading 1)
  5783. (error (goto-char (point-min))))
  5784. t)
  5785. (looking-at org-outline-regexp))
  5786. (if (integerp arg)
  5787. (show-children (1- arg))
  5788. (show-branches))
  5789. (if (bobp) (throw 'exit nil))))))
  5790. (defun org-optimize-window-after-visibility-change (state)
  5791. "Adjust the window after a change in outline visibility.
  5792. This function is the default value of the hook `org-cycle-hook'."
  5793. (when (get-buffer-window (current-buffer))
  5794. (cond
  5795. ((eq state 'content) nil)
  5796. ((eq state 'all) nil)
  5797. ((eq state 'folded) nil)
  5798. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5799. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5800. (defun org-remove-empty-overlays-at (pos)
  5801. "Remove outline overlays that do not contain non-white stuff."
  5802. (mapc
  5803. (lambda (o)
  5804. (and (eq 'outline (overlay-get o 'invisible))
  5805. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5806. (overlay-end o))))
  5807. (delete-overlay o)))
  5808. (overlays-at pos)))
  5809. (defun org-clean-visibility-after-subtree-move ()
  5810. "Fix visibility issues after moving a subtree."
  5811. ;; First, find a reasonable region to look at:
  5812. ;; Start two siblings above, end three below
  5813. (let* ((beg (save-excursion
  5814. (and (org-get-last-sibling)
  5815. (org-get-last-sibling))
  5816. (point)))
  5817. (end (save-excursion
  5818. (and (org-get-next-sibling)
  5819. (org-get-next-sibling)
  5820. (org-get-next-sibling))
  5821. (if (org-at-heading-p)
  5822. (point-at-eol)
  5823. (point))))
  5824. (level (looking-at "\\*+"))
  5825. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5826. (save-excursion
  5827. (save-restriction
  5828. (narrow-to-region beg end)
  5829. (when re
  5830. ;; Properly fold already folded siblings
  5831. (goto-char (point-min))
  5832. (while (re-search-forward re nil t)
  5833. (if (and (not (outline-invisible-p))
  5834. (save-excursion
  5835. (goto-char (point-at-eol)) (outline-invisible-p)))
  5836. (hide-entry))))
  5837. (org-cycle-show-empty-lines 'overview)
  5838. (org-cycle-hide-drawers 'overview)))))
  5839. (defun org-cycle-show-empty-lines (state)
  5840. "Show empty lines above all visible headlines.
  5841. The region to be covered depends on STATE when called through
  5842. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5843. entire buffer covered. Note that an empty line is only shown if there
  5844. are at least `org-cycle-separator-lines' empty lines before the headline."
  5845. (when (not (= org-cycle-separator-lines 0))
  5846. (save-excursion
  5847. (let* ((n (abs org-cycle-separator-lines))
  5848. (re (cond
  5849. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5850. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5851. (t (let ((ns (number-to-string (- n 2))))
  5852. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5853. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5854. beg end b e)
  5855. (cond
  5856. ((memq state '(overview contents t))
  5857. (setq beg (point-min) end (point-max)))
  5858. ((memq state '(children folded))
  5859. (setq beg (point) end (progn (org-end-of-subtree t t)
  5860. (beginning-of-line 2)
  5861. (point)))))
  5862. (when beg
  5863. (goto-char beg)
  5864. (while (re-search-forward re end t)
  5865. (unless (get-char-property (match-end 1) 'invisible)
  5866. (setq e (match-end 1))
  5867. (if (< org-cycle-separator-lines 0)
  5868. (setq b (save-excursion
  5869. (goto-char (match-beginning 0))
  5870. (org-back-over-empty-lines)
  5871. (if (save-excursion
  5872. (goto-char (max (point-min) (1- (point))))
  5873. (org-at-heading-p))
  5874. (1- (point))
  5875. (point))))
  5876. (setq b (match-beginning 1)))
  5877. (outline-flag-region b e nil)))))))
  5878. ;; Never hide empty lines at the end of the file.
  5879. (save-excursion
  5880. (goto-char (point-max))
  5881. (outline-previous-heading)
  5882. (outline-end-of-heading)
  5883. (if (and (looking-at "[ \t\n]+")
  5884. (= (match-end 0) (point-max)))
  5885. (outline-flag-region (point) (match-end 0) nil))))
  5886. (defun org-show-empty-lines-in-parent ()
  5887. "Move to the parent and re-show empty lines before visible headlines."
  5888. (save-excursion
  5889. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5890. (org-cycle-show-empty-lines context))))
  5891. (defun org-files-list ()
  5892. "Return `org-agenda-files' list, plus all open org-mode files.
  5893. This is useful for operations that need to scan all of a user's
  5894. open and agenda-wise Org files."
  5895. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5896. (dolist (buf (buffer-list))
  5897. (with-current-buffer buf
  5898. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5899. (let ((file (expand-file-name (buffer-file-name))))
  5900. (unless (member file files)
  5901. (push file files))))))
  5902. files))
  5903. (defsubst org-entry-beginning-position ()
  5904. "Return the beginning position of the current entry."
  5905. (save-excursion (outline-back-to-heading t) (point)))
  5906. (defsubst org-entry-end-position ()
  5907. "Return the end position of the current entry."
  5908. (save-excursion (outline-next-heading) (point)))
  5909. (defun org-cycle-hide-drawers (state)
  5910. "Re-hide all drawers after a visibility state change."
  5911. (when (and (derived-mode-p 'org-mode)
  5912. (not (memq state '(overview folded contents))))
  5913. (save-excursion
  5914. (let* ((globalp (memq state '(contents all)))
  5915. (beg (if globalp (point-min) (point)))
  5916. (end (if globalp (point-max)
  5917. (if (eq state 'children)
  5918. (save-excursion (outline-next-heading) (point))
  5919. (org-end-of-subtree t)))))
  5920. (goto-char beg)
  5921. (while (re-search-forward org-drawer-regexp end t)
  5922. (org-flag-drawer t))))))
  5923. (defun org-cycle-hide-inline-tasks (state)
  5924. "Re-hide inline task when switching to 'contents visibility state."
  5925. (when (and (eq state 'contents)
  5926. (boundp 'org-inlinetask-min-level)
  5927. org-inlinetask-min-level)
  5928. (hide-sublevels (1- org-inlinetask-min-level))))
  5929. (defun org-flag-drawer (flag)
  5930. "When FLAG is non-nil, hide the drawer we are within.
  5931. Otherwise make it visible."
  5932. (save-excursion
  5933. (beginning-of-line 1)
  5934. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5935. (let ((b (match-end 0)))
  5936. (if (re-search-forward
  5937. "^[ \t]*:END:"
  5938. (save-excursion (outline-next-heading) (point)) t)
  5939. (outline-flag-region b (point-at-eol) flag)
  5940. (error ":END: line missing at position %s" b))))))
  5941. (defun org-subtree-end-visible-p ()
  5942. "Is the end of the current subtree visible?"
  5943. (pos-visible-in-window-p
  5944. (save-excursion (org-end-of-subtree t) (point))))
  5945. (defun org-first-headline-recenter (&optional N)
  5946. "Move cursor to the first headline and recenter the headline.
  5947. Optional argument N means put the headline into the Nth line of the window."
  5948. (goto-char (point-min))
  5949. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5950. (beginning-of-line)
  5951. (recenter (prefix-numeric-value N))))
  5952. ;;; Saving and restoring visibility
  5953. (defun org-outline-overlay-data (&optional use-markers)
  5954. "Return a list of the locations of all outline overlays.
  5955. These are overlays with the `invisible' property value `outline'.
  5956. The return value is a list of cons cells, with start and stop
  5957. positions for each overlay.
  5958. If USE-MARKERS is set, return the positions as markers."
  5959. (let (beg end)
  5960. (save-excursion
  5961. (save-restriction
  5962. (widen)
  5963. (delq nil
  5964. (mapcar (lambda (o)
  5965. (when (eq (overlay-get o 'invisible) 'outline)
  5966. (setq beg (overlay-start o)
  5967. end (overlay-end o))
  5968. (and beg end (> end beg)
  5969. (if use-markers
  5970. (cons (move-marker (make-marker) beg)
  5971. (move-marker (make-marker) end))
  5972. (cons beg end)))))
  5973. (overlays-in (point-min) (point-max))))))))
  5974. (defun org-set-outline-overlay-data (data)
  5975. "Create visibility overlays for all positions in DATA.
  5976. DATA should have been made by `org-outline-overlay-data'."
  5977. (let (o)
  5978. (save-excursion
  5979. (save-restriction
  5980. (widen)
  5981. (show-all)
  5982. (mapc (lambda (c)
  5983. (outline-flag-region (car c) (cdr c) t))
  5984. data)))))
  5985. ;;; Folding of blocks
  5986. (defvar org-hide-block-overlays nil
  5987. "Overlays hiding blocks.")
  5988. (make-variable-buffer-local 'org-hide-block-overlays)
  5989. (defun org-block-map (function &optional start end)
  5990. "Call FUNCTION at the head of all source blocks in the current buffer.
  5991. Optional arguments START and END can be used to limit the range."
  5992. (let ((start (or start (point-min)))
  5993. (end (or end (point-max))))
  5994. (save-excursion
  5995. (goto-char start)
  5996. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5997. (save-excursion
  5998. (save-match-data
  5999. (goto-char (match-beginning 0))
  6000. (funcall function)))))))
  6001. (defun org-hide-block-toggle-all ()
  6002. "Toggle the visibility of all blocks in the current buffer."
  6003. (org-block-map #'org-hide-block-toggle))
  6004. (defun org-hide-block-all ()
  6005. "Fold all blocks in the current buffer."
  6006. (interactive)
  6007. (org-show-block-all)
  6008. (org-block-map #'org-hide-block-toggle-maybe))
  6009. (defun org-show-block-all ()
  6010. "Unfold all blocks in the current buffer."
  6011. (interactive)
  6012. (mapc 'delete-overlay org-hide-block-overlays)
  6013. (setq org-hide-block-overlays nil))
  6014. (defun org-hide-block-toggle-maybe ()
  6015. "Toggle visibility of block at point."
  6016. (interactive)
  6017. (let ((case-fold-search t))
  6018. (if (save-excursion
  6019. (beginning-of-line 1)
  6020. (looking-at org-block-regexp))
  6021. (progn (org-hide-block-toggle)
  6022. t) ;; to signal that we took action
  6023. nil))) ;; to signal that we did not
  6024. (defun org-hide-block-toggle (&optional force)
  6025. "Toggle the visibility of the current block."
  6026. (interactive)
  6027. (save-excursion
  6028. (beginning-of-line)
  6029. (if (re-search-forward org-block-regexp nil t)
  6030. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6031. (end (match-end 0)) ;; end of entire body
  6032. ov)
  6033. (if (memq t (mapcar (lambda (overlay)
  6034. (eq (overlay-get overlay 'invisible)
  6035. 'org-hide-block))
  6036. (overlays-at start)))
  6037. (if (or (not force) (eq force 'off))
  6038. (mapc (lambda (ov)
  6039. (when (member ov org-hide-block-overlays)
  6040. (setq org-hide-block-overlays
  6041. (delq ov org-hide-block-overlays)))
  6042. (when (eq (overlay-get ov 'invisible)
  6043. 'org-hide-block)
  6044. (delete-overlay ov)))
  6045. (overlays-at start)))
  6046. (setq ov (make-overlay start end))
  6047. (overlay-put ov 'invisible 'org-hide-block)
  6048. ;; make the block accessible to isearch
  6049. (overlay-put
  6050. ov 'isearch-open-invisible
  6051. (lambda (ov)
  6052. (when (member ov org-hide-block-overlays)
  6053. (setq org-hide-block-overlays
  6054. (delq ov org-hide-block-overlays)))
  6055. (when (eq (overlay-get ov 'invisible)
  6056. 'org-hide-block)
  6057. (delete-overlay ov))))
  6058. (push ov org-hide-block-overlays)))
  6059. (error "Not looking at a source block"))))
  6060. ;; org-tab-after-check-for-cycling-hook
  6061. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6062. ;; Remove overlays when changing major mode
  6063. (add-hook 'org-mode-hook
  6064. (lambda () (org-add-hook 'change-major-mode-hook
  6065. 'org-show-block-all 'append 'local)))
  6066. ;;; Org-goto
  6067. (defvar org-goto-window-configuration nil)
  6068. (defvar org-goto-marker nil)
  6069. (defvar org-goto-map
  6070. (let ((map (make-sparse-keymap)))
  6071. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6072. (while (setq cmd (pop cmds))
  6073. (substitute-key-definition cmd cmd map global-map)))
  6074. (suppress-keymap map)
  6075. (org-defkey map "\C-m" 'org-goto-ret)
  6076. (org-defkey map [(return)] 'org-goto-ret)
  6077. (org-defkey map [(left)] 'org-goto-left)
  6078. (org-defkey map [(right)] 'org-goto-right)
  6079. (org-defkey map [(control ?g)] 'org-goto-quit)
  6080. (org-defkey map "\C-i" 'org-cycle)
  6081. (org-defkey map [(tab)] 'org-cycle)
  6082. (org-defkey map [(down)] 'outline-next-visible-heading)
  6083. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6084. (if org-goto-auto-isearch
  6085. (if (fboundp 'define-key-after)
  6086. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6087. nil)
  6088. (org-defkey map "q" 'org-goto-quit)
  6089. (org-defkey map "n" 'outline-next-visible-heading)
  6090. (org-defkey map "p" 'outline-previous-visible-heading)
  6091. (org-defkey map "f" 'outline-forward-same-level)
  6092. (org-defkey map "b" 'outline-backward-same-level)
  6093. (org-defkey map "u" 'outline-up-heading))
  6094. (org-defkey map "/" 'org-occur)
  6095. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6096. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6097. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6098. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6099. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6100. map))
  6101. (defconst org-goto-help
  6102. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6103. RET=jump to location [Q]uit and return to previous location
  6104. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6105. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6106. ;; FIXME: Docstring does not mention both interfaces
  6107. (defun org-goto (&optional alternative-interface)
  6108. "Look up a different location in the current file, keeping current visibility.
  6109. When you want look-up or go to a different location in a
  6110. document, the fastest way is often to fold the entire buffer and
  6111. then dive into the tree. This method has the disadvantage, that
  6112. the previous location will be folded, which may not be what you
  6113. want.
  6114. This command works around this by showing a copy of the current
  6115. buffer in an indirect buffer, in overview mode. You can dive
  6116. into the tree in that copy, use org-occur and incremental search
  6117. to find a location. When pressing RET or `Q', the command
  6118. returns to the original buffer in which the visibility is still
  6119. unchanged. After RET it will also jump to the location selected
  6120. in the indirect buffer and expose the headline hierarchy above.
  6121. With a prefix argument, use the alternative interface: e.g. if
  6122. `org-goto-interface' is 'outline use 'outline-path-completion."
  6123. (interactive "P")
  6124. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6125. (org-refile-use-outline-path t)
  6126. (org-refile-target-verify-function nil)
  6127. (interface
  6128. (if (not alternative-interface)
  6129. org-goto-interface
  6130. (if (eq org-goto-interface 'outline)
  6131. 'outline-path-completion
  6132. 'outline)))
  6133. (org-goto-start-pos (point))
  6134. (selected-point
  6135. (if (eq interface 'outline)
  6136. (car (org-get-location (current-buffer) org-goto-help))
  6137. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6138. (org-refile-check-position pa)
  6139. (nth 3 pa)))))
  6140. (if selected-point
  6141. (progn
  6142. (org-mark-ring-push org-goto-start-pos)
  6143. (goto-char selected-point)
  6144. (if (or (outline-invisible-p) (org-invisible-p2))
  6145. (org-show-context 'org-goto)))
  6146. (message "Quit"))))
  6147. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6148. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6149. (defvar org-goto-local-auto-isearch-map) ; defined below
  6150. (defun org-get-location (buf help)
  6151. "Let the user select a location in the Org-mode buffer BUF.
  6152. This function uses a recursive edit. It returns the selected position
  6153. or nil."
  6154. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6155. (isearch-hide-immediately nil)
  6156. (isearch-search-fun-function
  6157. (lambda () 'org-goto-local-search-headings))
  6158. (org-goto-selected-point org-goto-exit-command)
  6159. (pop-up-frames nil)
  6160. (special-display-buffer-names nil)
  6161. (special-display-regexps nil)
  6162. (special-display-function nil))
  6163. (save-excursion
  6164. (save-window-excursion
  6165. (delete-other-windows)
  6166. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6167. (org-pop-to-buffer-same-window
  6168. (condition-case nil
  6169. (make-indirect-buffer (current-buffer) "*org-goto*")
  6170. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6171. (with-output-to-temp-buffer "*Help*"
  6172. (princ help))
  6173. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6174. (setq buffer-read-only nil)
  6175. (let ((org-startup-truncated t)
  6176. (org-startup-folded nil)
  6177. (org-startup-align-all-tables nil))
  6178. (org-mode)
  6179. (org-overview))
  6180. (setq buffer-read-only t)
  6181. (if (and (boundp 'org-goto-start-pos)
  6182. (integer-or-marker-p org-goto-start-pos))
  6183. (let ((org-show-hierarchy-above t)
  6184. (org-show-siblings t)
  6185. (org-show-following-heading t))
  6186. (goto-char org-goto-start-pos)
  6187. (and (outline-invisible-p) (org-show-context)))
  6188. (goto-char (point-min)))
  6189. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6190. (message "Select location and press RET")
  6191. (use-local-map org-goto-map)
  6192. (recursive-edit)
  6193. ))
  6194. (kill-buffer "*org-goto*")
  6195. (cons org-goto-selected-point org-goto-exit-command)))
  6196. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6197. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6198. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6199. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6200. (defun org-goto-local-search-headings (string bound noerror)
  6201. "Search and make sure that any matches are in headlines."
  6202. (catch 'return
  6203. (while (if isearch-forward
  6204. (search-forward string bound noerror)
  6205. (search-backward string bound noerror))
  6206. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6207. (and (member :headline context)
  6208. (not (member :tags context))))
  6209. (throw 'return (point))))))
  6210. (defun org-goto-local-auto-isearch ()
  6211. "Start isearch."
  6212. (interactive)
  6213. (goto-char (point-min))
  6214. (let ((keys (this-command-keys)))
  6215. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6216. (isearch-mode t)
  6217. (isearch-process-search-char (string-to-char keys)))))
  6218. (defun org-goto-ret (&optional arg)
  6219. "Finish `org-goto' by going to the new location."
  6220. (interactive "P")
  6221. (setq org-goto-selected-point (point)
  6222. org-goto-exit-command 'return)
  6223. (throw 'exit nil))
  6224. (defun org-goto-left ()
  6225. "Finish `org-goto' by going to the new location."
  6226. (interactive)
  6227. (if (org-at-heading-p)
  6228. (progn
  6229. (beginning-of-line 1)
  6230. (setq org-goto-selected-point (point)
  6231. org-goto-exit-command 'left)
  6232. (throw 'exit nil))
  6233. (error "Not on a heading")))
  6234. (defun org-goto-right ()
  6235. "Finish `org-goto' by going to the new location."
  6236. (interactive)
  6237. (if (org-at-heading-p)
  6238. (progn
  6239. (setq org-goto-selected-point (point)
  6240. org-goto-exit-command 'right)
  6241. (throw 'exit nil))
  6242. (error "Not on a heading")))
  6243. (defun org-goto-quit ()
  6244. "Finish `org-goto' without cursor motion."
  6245. (interactive)
  6246. (setq org-goto-selected-point nil)
  6247. (setq org-goto-exit-command 'quit)
  6248. (throw 'exit nil))
  6249. ;;; Indirect buffer display of subtrees
  6250. (defvar org-indirect-dedicated-frame nil
  6251. "This is the frame being used for indirect tree display.")
  6252. (defvar org-last-indirect-buffer nil)
  6253. (defun org-tree-to-indirect-buffer (&optional arg)
  6254. "Create indirect buffer and narrow it to current subtree.
  6255. With a numerical prefix ARG, go up to this level and then take that tree.
  6256. If ARG is negative, go up that many levels.
  6257. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6258. indirect buffer previously made with this command, to avoid proliferation of
  6259. indirect buffers. However, when you call the command with a \
  6260. \\[universal-argument] prefix, or
  6261. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6262. is kept so that you can work with several indirect buffers at the same time.
  6263. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6264. \\[universal-argument] prefix also
  6265. requests that a new frame be made for the new buffer, so that the dedicated
  6266. frame is not changed."
  6267. (interactive "P")
  6268. (let ((cbuf (current-buffer))
  6269. (cwin (selected-window))
  6270. (pos (point))
  6271. beg end level heading ibuf)
  6272. (save-excursion
  6273. (org-back-to-heading t)
  6274. (when (numberp arg)
  6275. (setq level (org-outline-level))
  6276. (if (< arg 0) (setq arg (+ level arg)))
  6277. (while (> (setq level (org-outline-level)) arg)
  6278. (org-up-heading-safe)))
  6279. (setq beg (point)
  6280. heading (org-get-heading))
  6281. (org-end-of-subtree t t)
  6282. (if (org-at-heading-p) (backward-char 1))
  6283. (setq end (point)))
  6284. (if (and (buffer-live-p org-last-indirect-buffer)
  6285. (not (eq org-indirect-buffer-display 'new-frame))
  6286. (not arg))
  6287. (kill-buffer org-last-indirect-buffer))
  6288. (setq ibuf (org-get-indirect-buffer cbuf)
  6289. org-last-indirect-buffer ibuf)
  6290. (cond
  6291. ((or (eq org-indirect-buffer-display 'new-frame)
  6292. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6293. (select-frame (make-frame))
  6294. (delete-other-windows)
  6295. (org-pop-to-buffer-same-window ibuf)
  6296. (org-set-frame-title heading))
  6297. ((eq org-indirect-buffer-display 'dedicated-frame)
  6298. (raise-frame
  6299. (select-frame (or (and org-indirect-dedicated-frame
  6300. (frame-live-p org-indirect-dedicated-frame)
  6301. org-indirect-dedicated-frame)
  6302. (setq org-indirect-dedicated-frame (make-frame)))))
  6303. (delete-other-windows)
  6304. (org-pop-to-buffer-same-window ibuf)
  6305. (org-set-frame-title (concat "Indirect: " heading)))
  6306. ((eq org-indirect-buffer-display 'current-window)
  6307. (org-pop-to-buffer-same-window ibuf))
  6308. ((eq org-indirect-buffer-display 'other-window)
  6309. (pop-to-buffer ibuf))
  6310. (t (error "Invalid value")))
  6311. (if (featurep 'xemacs)
  6312. (save-excursion (org-mode) (turn-on-font-lock)))
  6313. (narrow-to-region beg end)
  6314. (show-all)
  6315. (goto-char pos)
  6316. (run-hook-with-args 'org-cycle-hook 'all)
  6317. (and (window-live-p cwin) (select-window cwin))))
  6318. (defun org-get-indirect-buffer (&optional buffer)
  6319. (setq buffer (or buffer (current-buffer)))
  6320. (let ((n 1) (base (buffer-name buffer)) bname)
  6321. (while (buffer-live-p
  6322. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6323. (setq n (1+ n)))
  6324. (condition-case nil
  6325. (make-indirect-buffer buffer bname 'clone)
  6326. (error (make-indirect-buffer buffer bname)))))
  6327. (defun org-set-frame-title (title)
  6328. "Set the title of the current frame to the string TITLE."
  6329. ;; FIXME: how to name a single frame in XEmacs???
  6330. (unless (featurep 'xemacs)
  6331. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6332. ;;;; Structure editing
  6333. ;;; Inserting headlines
  6334. (defun org-previous-line-empty-p ()
  6335. (save-excursion
  6336. (and (not (bobp))
  6337. (or (beginning-of-line 0) t)
  6338. (save-match-data
  6339. (looking-at "[ \t]*$")))))
  6340. (defun org-insert-heading (&optional force-heading invisible-ok)
  6341. "Insert a new heading or item with same depth at point.
  6342. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6343. If point is at the beginning of a headline, insert a sibling before the
  6344. current headline. If point is not at the beginning, split the line,
  6345. create the new headline with the text in the current line after point
  6346. \(but see also the variable `org-M-RET-may-split-line').
  6347. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6348. This is important for non-interactive uses of the command."
  6349. (interactive "P")
  6350. (if (or (= (buffer-size) 0)
  6351. (and (not (save-excursion
  6352. (and (ignore-errors (org-back-to-heading invisible-ok))
  6353. (org-at-heading-p))))
  6354. (or force-heading (not (org-in-item-p)))))
  6355. (progn
  6356. (insert "\n* ")
  6357. (run-hooks 'org-insert-heading-hook))
  6358. (when (or force-heading (not (org-insert-item)))
  6359. (let* ((empty-line-p nil)
  6360. (level nil)
  6361. (on-heading (org-at-heading-p))
  6362. (head (save-excursion
  6363. (condition-case nil
  6364. (progn
  6365. (org-back-to-heading invisible-ok)
  6366. (when (and (not on-heading)
  6367. (featurep 'org-inlinetask)
  6368. (integerp org-inlinetask-min-level)
  6369. (>= (length (match-string 0))
  6370. org-inlinetask-min-level))
  6371. ;; Find a heading level before the inline task
  6372. (while (and (setq level (org-up-heading-safe))
  6373. (>= level org-inlinetask-min-level)))
  6374. (if (org-at-heading-p)
  6375. (org-back-to-heading invisible-ok)
  6376. (error "This should not happen")))
  6377. (setq empty-line-p (org-previous-line-empty-p))
  6378. (match-string 0))
  6379. (error "*"))))
  6380. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6381. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6382. pos hide-previous previous-pos)
  6383. (cond
  6384. ((and (org-at-heading-p) (bolp)
  6385. (or (bobp)
  6386. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6387. ;; insert before the current line
  6388. (open-line (if blank 2 1)))
  6389. ((and (bolp)
  6390. (not org-insert-heading-respect-content)
  6391. (or (bobp)
  6392. (save-excursion
  6393. (backward-char 1) (not (outline-invisible-p)))))
  6394. ;; insert right here
  6395. nil)
  6396. (t
  6397. ;; somewhere in the line
  6398. (save-excursion
  6399. (setq previous-pos (point-at-bol))
  6400. (end-of-line)
  6401. (setq hide-previous (outline-invisible-p)))
  6402. (and org-insert-heading-respect-content (org-show-subtree))
  6403. (let ((split
  6404. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6405. (save-excursion
  6406. (let ((p (point)))
  6407. (goto-char (point-at-bol))
  6408. (and (looking-at org-complex-heading-regexp)
  6409. (match-beginning 4)
  6410. (> p (match-beginning 4)))))))
  6411. tags pos)
  6412. (cond
  6413. (org-insert-heading-respect-content
  6414. (org-end-of-subtree nil t)
  6415. (when (featurep 'org-inlinetask)
  6416. (while (and (not (eobp))
  6417. (looking-at "\\(\\*+\\)[ \t]+")
  6418. (>= (length (match-string 1))
  6419. org-inlinetask-min-level))
  6420. (org-end-of-subtree nil t)))
  6421. (or (bolp) (newline))
  6422. (or (org-previous-line-empty-p)
  6423. (and blank (newline)))
  6424. (open-line 1))
  6425. ((org-at-heading-p)
  6426. (when hide-previous
  6427. (show-children)
  6428. (org-show-entry))
  6429. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6430. (setq tags (and (match-end 2) (match-string 2)))
  6431. (and (match-end 1)
  6432. (delete-region (match-beginning 1) (match-end 1)))
  6433. (setq pos (point-at-bol))
  6434. (or split (end-of-line 1))
  6435. (delete-horizontal-space)
  6436. (if (string-match "\\`\\*+\\'"
  6437. (buffer-substring (point-at-bol) (point)))
  6438. (insert " "))
  6439. (newline (if blank 2 1))
  6440. (when tags
  6441. (save-excursion
  6442. (goto-char pos)
  6443. (end-of-line 1)
  6444. (insert " " tags)
  6445. (org-set-tags nil 'align))))
  6446. (t
  6447. (or split (end-of-line 1))
  6448. (newline (if blank 2 1)))))))
  6449. (insert head) (just-one-space)
  6450. (setq pos (point))
  6451. (end-of-line 1)
  6452. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6453. (when (and org-insert-heading-respect-content hide-previous)
  6454. (save-excursion
  6455. (goto-char previous-pos)
  6456. (hide-subtree)))
  6457. (run-hooks 'org-insert-heading-hook)))))
  6458. (defun org-get-heading (&optional no-tags no-todo)
  6459. "Return the heading of the current entry, without the stars.
  6460. When NO-TAGS is non-nil, don't include tags.
  6461. When NO-TODO is non-nil, don't include TODO keywords."
  6462. (save-excursion
  6463. (org-back-to-heading t)
  6464. (cond
  6465. ((and no-tags no-todo)
  6466. (looking-at org-complex-heading-regexp)
  6467. (match-string 4))
  6468. (no-tags
  6469. (looking-at (concat org-outline-regexp
  6470. "\\(.*?\\)"
  6471. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6472. (match-string 1))
  6473. (no-todo
  6474. (looking-at org-todo-line-regexp)
  6475. (match-string 3))
  6476. (t (looking-at org-heading-regexp)
  6477. (match-string 2)))))
  6478. (defun org-heading-components ()
  6479. "Return the components of the current heading.
  6480. This is a list with the following elements:
  6481. - the level as an integer
  6482. - the reduced level, different if `org-odd-levels-only' is set.
  6483. - the TODO keyword, or nil
  6484. - the priority character, like ?A, or nil if no priority is given
  6485. - the headline text itself, or the tags string if no headline text
  6486. - the tags string, or nil."
  6487. (save-excursion
  6488. (org-back-to-heading t)
  6489. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6490. (list (length (match-string 1))
  6491. (org-reduced-level (length (match-string 1)))
  6492. (org-match-string-no-properties 2)
  6493. (and (match-end 3) (aref (match-string 3) 2))
  6494. (org-match-string-no-properties 4)
  6495. (org-match-string-no-properties 5)))))
  6496. (defun org-get-entry ()
  6497. "Get the entry text, after heading, entire subtree."
  6498. (save-excursion
  6499. (org-back-to-heading t)
  6500. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6501. (defun org-insert-heading-after-current ()
  6502. "Insert a new heading with same level as current, after current subtree."
  6503. (interactive)
  6504. (org-back-to-heading)
  6505. (org-insert-heading)
  6506. (org-move-subtree-down)
  6507. (end-of-line 1))
  6508. (defun org-insert-heading-respect-content ()
  6509. (interactive)
  6510. (let ((org-insert-heading-respect-content t))
  6511. (org-insert-heading t)))
  6512. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6513. (interactive "P")
  6514. (let ((org-insert-heading-respect-content t))
  6515. (org-insert-todo-heading force-state t)))
  6516. (defun org-insert-todo-heading (arg &optional force-heading)
  6517. "Insert a new heading with the same level and TODO state as current heading.
  6518. If the heading has no TODO state, or if the state is DONE, use the first
  6519. state (TODO by default). Also with prefix arg, force first state."
  6520. (interactive "P")
  6521. (when (or force-heading (not (org-insert-item 'checkbox)))
  6522. (org-insert-heading force-heading)
  6523. (save-excursion
  6524. (org-back-to-heading)
  6525. (outline-previous-heading)
  6526. (looking-at org-todo-line-regexp))
  6527. (let*
  6528. ((new-mark-x
  6529. (if (or arg
  6530. (not (match-beginning 2))
  6531. (member (match-string 2) org-done-keywords))
  6532. (car org-todo-keywords-1)
  6533. (match-string 2)))
  6534. (new-mark
  6535. (or
  6536. (run-hook-with-args-until-success
  6537. 'org-todo-get-default-hook new-mark-x nil)
  6538. new-mark-x)))
  6539. (beginning-of-line 1)
  6540. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6541. (if org-treat-insert-todo-heading-as-state-change
  6542. (org-todo new-mark)
  6543. (insert new-mark " "))))
  6544. (when org-provide-todo-statistics
  6545. (org-update-parent-todo-statistics))))
  6546. (defun org-insert-subheading (arg)
  6547. "Insert a new subheading and demote it.
  6548. Works for outline headings and for plain lists alike."
  6549. (interactive "P")
  6550. (org-insert-heading arg)
  6551. (cond
  6552. ((org-at-heading-p) (org-do-demote))
  6553. ((org-at-item-p) (org-indent-item))))
  6554. (defun org-insert-todo-subheading (arg)
  6555. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6556. Works for outline headings and for plain lists alike."
  6557. (interactive "P")
  6558. (org-insert-todo-heading arg)
  6559. (cond
  6560. ((org-at-heading-p) (org-do-demote))
  6561. ((org-at-item-p) (org-indent-item))))
  6562. ;;; Promotion and Demotion
  6563. (defvar org-after-demote-entry-hook nil
  6564. "Hook run after an entry has been demoted.
  6565. The cursor will be at the beginning of the entry.
  6566. When a subtree is being demoted, the hook will be called for each node.")
  6567. (defvar org-after-promote-entry-hook nil
  6568. "Hook run after an entry has been promoted.
  6569. The cursor will be at the beginning of the entry.
  6570. When a subtree is being promoted, the hook will be called for each node.")
  6571. (defun org-promote-subtree ()
  6572. "Promote the entire subtree.
  6573. See also `org-promote'."
  6574. (interactive)
  6575. (save-excursion
  6576. (org-with-limited-levels (org-map-tree 'org-promote)))
  6577. (org-fix-position-after-promote))
  6578. (defun org-demote-subtree ()
  6579. "Demote the entire subtree. See `org-demote'.
  6580. See also `org-promote'."
  6581. (interactive)
  6582. (save-excursion
  6583. (org-with-limited-levels (org-map-tree 'org-demote)))
  6584. (org-fix-position-after-promote))
  6585. (defun org-do-promote ()
  6586. "Promote the current heading higher up the tree.
  6587. If the region is active in `transient-mark-mode', promote all headings
  6588. in the region."
  6589. (interactive)
  6590. (save-excursion
  6591. (if (org-region-active-p)
  6592. (org-map-region 'org-promote (region-beginning) (region-end))
  6593. (org-promote)))
  6594. (org-fix-position-after-promote))
  6595. (defun org-do-demote ()
  6596. "Demote the current heading lower down the tree.
  6597. If the region is active in `transient-mark-mode', demote all headings
  6598. in the region."
  6599. (interactive)
  6600. (save-excursion
  6601. (if (org-region-active-p)
  6602. (org-map-region 'org-demote (region-beginning) (region-end))
  6603. (org-demote)))
  6604. (org-fix-position-after-promote))
  6605. (defun org-fix-position-after-promote ()
  6606. "Make sure that after pro/demotion cursor position is right."
  6607. (let ((pos (point)))
  6608. (when (save-excursion
  6609. (beginning-of-line 1)
  6610. (looking-at org-todo-line-regexp)
  6611. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6612. (cond ((eobp) (insert " "))
  6613. ((eolp) (insert " "))
  6614. ((equal (char-after) ?\ ) (forward-char 1))))))
  6615. (defun org-current-level ()
  6616. "Return the level of the current entry, or nil if before the first headline.
  6617. The level is the number of stars at the beginning of the headline."
  6618. (save-excursion
  6619. (org-with-limited-levels
  6620. (if (ignore-errors (org-back-to-heading t))
  6621. (funcall outline-level)))))
  6622. (defun org-get-previous-line-level ()
  6623. "Return the outline depth of the last headline before the current line.
  6624. Returns 0 for the first headline in the buffer, and nil if before the
  6625. first headline."
  6626. (let ((current-level (org-current-level))
  6627. (prev-level (when (> (line-number-at-pos) 1)
  6628. (save-excursion
  6629. (beginning-of-line 0)
  6630. (org-current-level)))))
  6631. (cond ((null current-level) nil) ; Before first headline
  6632. ((null prev-level) 0) ; At first headline
  6633. (prev-level))))
  6634. (defun org-reduced-level (l)
  6635. "Compute the effective level of a heading.
  6636. This takes into account the setting of `org-odd-levels-only'."
  6637. (cond
  6638. ((zerop l) 0)
  6639. (org-odd-levels-only (1+ (floor (/ l 2))))
  6640. (t l)))
  6641. (defun org-level-increment ()
  6642. "Return the number of stars that will be added or removed at a
  6643. time to headlines when structure editing, based on the value of
  6644. `org-odd-levels-only'."
  6645. (if org-odd-levels-only 2 1))
  6646. (defun org-get-valid-level (level &optional change)
  6647. "Rectify a level change under the influence of `org-odd-levels-only'
  6648. LEVEL is a current level, CHANGE is by how much the level should be
  6649. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6650. even level numbers will become the next higher odd number."
  6651. (if org-odd-levels-only
  6652. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6653. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6654. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6655. (max 1 (+ level (or change 0)))))
  6656. (if (boundp 'define-obsolete-function-alias)
  6657. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6658. (define-obsolete-function-alias 'org-get-legal-level
  6659. 'org-get-valid-level)
  6660. (define-obsolete-function-alias 'org-get-legal-level
  6661. 'org-get-valid-level "23.1")))
  6662. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6663. ;; ̀org-with-limited-levels'
  6664. (defun org-promote ()
  6665. "Promote the current heading higher up the tree.
  6666. If the region is active in `transient-mark-mode', promote all headings
  6667. in the region."
  6668. (org-back-to-heading t)
  6669. (let* ((level (save-match-data (funcall outline-level)))
  6670. (after-change-functions (remove 'flyspell-after-change-function
  6671. after-change-functions))
  6672. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6673. (diff (abs (- level (length up-head) -1))))
  6674. (cond ((and (= level 1) org-called-with-limited-levels
  6675. org-allow-promoting-top-level-subtree)
  6676. (replace-match "# " nil t))
  6677. ((= level 1)
  6678. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6679. (t (replace-match up-head nil t)))
  6680. ;; Fixup tag positioning
  6681. (unless (= level 1)
  6682. (and org-auto-align-tags (org-set-tags nil t))
  6683. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6684. (run-hooks 'org-after-promote-entry-hook)))
  6685. (defun org-demote ()
  6686. "Demote the current heading lower down the tree.
  6687. If the region is active in `transient-mark-mode', demote all headings
  6688. in the region."
  6689. (org-back-to-heading t)
  6690. (let* ((level (save-match-data (funcall outline-level)))
  6691. (after-change-functions (remove 'flyspell-after-change-function
  6692. after-change-functions))
  6693. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6694. (diff (abs (- level (length down-head) -1))))
  6695. (replace-match down-head nil t)
  6696. ;; Fixup tag positioning
  6697. (and org-auto-align-tags (org-set-tags nil t))
  6698. (if org-adapt-indentation (org-fixup-indentation diff))
  6699. (run-hooks 'org-after-demote-entry-hook)))
  6700. (defun org-cycle-level ()
  6701. "Cycle the level of an empty headline through possible states.
  6702. This goes first to child, then to parent, level, then up the hierarchy.
  6703. After top level, it switches back to sibling level."
  6704. (interactive)
  6705. (let ((org-adapt-indentation nil))
  6706. (when (org-point-at-end-of-empty-headline)
  6707. (setq this-command 'org-cycle-level) ; Only needed for caching
  6708. (let ((cur-level (org-current-level))
  6709. (prev-level (org-get-previous-line-level)))
  6710. (cond
  6711. ;; If first headline in file, promote to top-level.
  6712. ((= prev-level 0)
  6713. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6714. do (org-do-promote)))
  6715. ;; If same level as prev, demote one.
  6716. ((= prev-level cur-level)
  6717. (org-do-demote))
  6718. ;; If parent is top-level, promote to top level if not already.
  6719. ((= prev-level 1)
  6720. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6721. do (org-do-promote)))
  6722. ;; If top-level, return to prev-level.
  6723. ((= cur-level 1)
  6724. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6725. do (org-do-demote)))
  6726. ;; If less than prev-level, promote one.
  6727. ((< cur-level prev-level)
  6728. (org-do-promote))
  6729. ;; If deeper than prev-level, promote until higher than
  6730. ;; prev-level.
  6731. ((> cur-level prev-level)
  6732. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6733. do (org-do-promote))))
  6734. t))))
  6735. (defun org-map-tree (fun)
  6736. "Call FUN for every heading underneath the current one."
  6737. (org-back-to-heading)
  6738. (let ((level (funcall outline-level)))
  6739. (save-excursion
  6740. (funcall fun)
  6741. (while (and (progn
  6742. (outline-next-heading)
  6743. (> (funcall outline-level) level))
  6744. (not (eobp)))
  6745. (funcall fun)))))
  6746. (defun org-map-region (fun beg end)
  6747. "Call FUN for every heading between BEG and END."
  6748. (let ((org-ignore-region t))
  6749. (save-excursion
  6750. (setq end (copy-marker end))
  6751. (goto-char beg)
  6752. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6753. (< (point) end))
  6754. (funcall fun))
  6755. (while (and (progn
  6756. (outline-next-heading)
  6757. (< (point) end))
  6758. (not (eobp)))
  6759. (funcall fun)))))
  6760. (defvar org-property-end-re) ; silence byte-compiler
  6761. (defun org-fixup-indentation (diff)
  6762. "Change the indentation in the current entry by DIFF.
  6763. However, if any line in the current entry has no indentation, or if it
  6764. would end up with no indentation after the change, nothing at all is done."
  6765. (save-excursion
  6766. (let ((end (save-excursion (outline-next-heading)
  6767. (point-marker)))
  6768. (prohibit (if (> diff 0)
  6769. "^\\S-"
  6770. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6771. col)
  6772. (unless (save-excursion (end-of-line 1)
  6773. (re-search-forward prohibit end t))
  6774. (while (and (< (point) end)
  6775. (re-search-forward "^[ \t]+" end t))
  6776. (goto-char (match-end 0))
  6777. (setq col (current-column))
  6778. (if (< diff 0) (replace-match ""))
  6779. (org-indent-to-column (+ diff col))))
  6780. (move-marker end nil))))
  6781. (defun org-convert-to-odd-levels ()
  6782. "Convert an org-mode file with all levels allowed to one with odd levels.
  6783. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6784. level 5 etc."
  6785. (interactive)
  6786. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6787. (let ((outline-level 'org-outline-level)
  6788. (org-odd-levels-only nil) n)
  6789. (save-excursion
  6790. (goto-char (point-min))
  6791. (while (re-search-forward "^\\*\\*+ " nil t)
  6792. (setq n (- (length (match-string 0)) 2))
  6793. (while (>= (setq n (1- n)) 0)
  6794. (org-demote))
  6795. (end-of-line 1))))))
  6796. (defun org-convert-to-oddeven-levels ()
  6797. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6798. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6799. file contains a section with an even level, conversion would
  6800. destroy the structure of the file. An error is signaled in this
  6801. case."
  6802. (interactive)
  6803. (goto-char (point-min))
  6804. ;; First check if there are no even levels
  6805. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6806. (org-show-context t)
  6807. (error "Not all levels are odd in this file. Conversion not possible"))
  6808. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6809. (let ((outline-regexp org-outline-regexp)
  6810. (outline-level 'org-outline-level)
  6811. (org-odd-levels-only nil) n)
  6812. (save-excursion
  6813. (goto-char (point-min))
  6814. (while (re-search-forward "^\\*\\*+ " nil t)
  6815. (setq n (/ (1- (length (match-string 0))) 2))
  6816. (while (>= (setq n (1- n)) 0)
  6817. (org-promote))
  6818. (end-of-line 1))))))
  6819. (defun org-tr-level (n)
  6820. "Make N odd if required."
  6821. (if org-odd-levels-only (1+ (/ n 2)) n))
  6822. ;;; Vertical tree motion, cutting and pasting of subtrees
  6823. (defun org-move-subtree-up (&optional arg)
  6824. "Move the current subtree up past ARG headlines of the same level."
  6825. (interactive "p")
  6826. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6827. (defun org-move-subtree-down (&optional arg)
  6828. "Move the current subtree down past ARG headlines of the same level."
  6829. (interactive "p")
  6830. (setq arg (prefix-numeric-value arg))
  6831. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6832. 'org-get-last-sibling))
  6833. (ins-point (make-marker))
  6834. (cnt (abs arg))
  6835. (col (current-column))
  6836. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6837. ;; Select the tree
  6838. (org-back-to-heading)
  6839. (setq beg0 (point))
  6840. (save-excursion
  6841. (setq ne-beg (org-back-over-empty-lines))
  6842. (setq beg (point)))
  6843. (save-match-data
  6844. (save-excursion (outline-end-of-heading)
  6845. (setq folded (outline-invisible-p)))
  6846. (outline-end-of-subtree))
  6847. (outline-next-heading)
  6848. (setq ne-end (org-back-over-empty-lines))
  6849. (setq end (point))
  6850. (goto-char beg0)
  6851. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6852. ;; include less whitespace
  6853. (save-excursion
  6854. (goto-char beg)
  6855. (forward-line (- ne-beg ne-end))
  6856. (setq beg (point))))
  6857. ;; Find insertion point, with error handling
  6858. (while (> cnt 0)
  6859. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6860. (progn (goto-char beg0)
  6861. (error "Cannot move past superior level or buffer limit")))
  6862. (setq cnt (1- cnt)))
  6863. (if (> arg 0)
  6864. ;; Moving forward - still need to move over subtree
  6865. (progn (org-end-of-subtree t t)
  6866. (save-excursion
  6867. (org-back-over-empty-lines)
  6868. (or (bolp) (newline)))))
  6869. (setq ne-ins (org-back-over-empty-lines))
  6870. (move-marker ins-point (point))
  6871. (setq txt (buffer-substring beg end))
  6872. (org-save-markers-in-region beg end)
  6873. (delete-region beg end)
  6874. (org-remove-empty-overlays-at beg)
  6875. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6876. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6877. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6878. (let ((bbb (point)))
  6879. (insert-before-markers txt)
  6880. (org-reinstall-markers-in-region bbb)
  6881. (move-marker ins-point bbb))
  6882. (or (bolp) (insert "\n"))
  6883. (setq ins-end (point))
  6884. (goto-char ins-point)
  6885. (org-skip-whitespace)
  6886. (when (and (< arg 0)
  6887. (org-first-sibling-p)
  6888. (> ne-ins ne-beg))
  6889. ;; Move whitespace back to beginning
  6890. (save-excursion
  6891. (goto-char ins-end)
  6892. (let ((kill-whole-line t))
  6893. (kill-line (- ne-ins ne-beg)) (point)))
  6894. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6895. (move-marker ins-point nil)
  6896. (if folded
  6897. (hide-subtree)
  6898. (org-show-entry)
  6899. (show-children)
  6900. (org-cycle-hide-drawers 'children))
  6901. (org-clean-visibility-after-subtree-move)
  6902. ;; move back to the initial column we were at
  6903. (move-to-column col)))
  6904. (defvar org-subtree-clip ""
  6905. "Clipboard for cut and paste of subtrees.
  6906. This is actually only a copy of the kill, because we use the normal kill
  6907. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6908. (defvar org-subtree-clip-folded nil
  6909. "Was the last copied subtree folded?
  6910. This is used to fold the tree back after pasting.")
  6911. (defun org-cut-subtree (&optional n)
  6912. "Cut the current subtree into the clipboard.
  6913. With prefix arg N, cut this many sequential subtrees.
  6914. This is a short-hand for marking the subtree and then cutting it."
  6915. (interactive "p")
  6916. (org-copy-subtree n 'cut))
  6917. (defun org-copy-subtree (&optional n cut force-store-markers)
  6918. "Cut the current subtree into the clipboard.
  6919. With prefix arg N, cut this many sequential subtrees.
  6920. This is a short-hand for marking the subtree and then copying it.
  6921. If CUT is non-nil, actually cut the subtree.
  6922. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6923. of some markers in the region, even if CUT is non-nil. This is
  6924. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6925. (interactive "p")
  6926. (let (beg end folded (beg0 (point)))
  6927. (if (org-called-interactively-p 'any)
  6928. (org-back-to-heading nil) ; take what looks like a subtree
  6929. (org-back-to-heading t)) ; take what is really there
  6930. (org-back-over-empty-lines)
  6931. (setq beg (point))
  6932. (skip-chars-forward " \t\r\n")
  6933. (save-match-data
  6934. (save-excursion (outline-end-of-heading)
  6935. (setq folded (outline-invisible-p)))
  6936. (condition-case nil
  6937. (org-forward-heading-same-level (1- n) t)
  6938. (error nil))
  6939. (org-end-of-subtree t t))
  6940. (org-back-over-empty-lines)
  6941. (setq end (point))
  6942. (goto-char beg0)
  6943. (when (> end beg)
  6944. (setq org-subtree-clip-folded folded)
  6945. (when (or cut force-store-markers)
  6946. (org-save-markers-in-region beg end))
  6947. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6948. (setq org-subtree-clip (current-kill 0))
  6949. (message "%s: Subtree(s) with %d characters"
  6950. (if cut "Cut" "Copied")
  6951. (length org-subtree-clip)))))
  6952. (defun org-paste-subtree (&optional level tree for-yank)
  6953. "Paste the clipboard as a subtree, with modification of headline level.
  6954. The entire subtree is promoted or demoted in order to match a new headline
  6955. level.
  6956. If the cursor is at the beginning of a headline, the same level as
  6957. that headline is used to paste the tree
  6958. If not, the new level is derived from the *visible* headings
  6959. before and after the insertion point, and taken to be the inferior headline
  6960. level of the two. So if the previous visible heading is level 3 and the
  6961. next is level 4 (or vice versa), level 4 will be used for insertion.
  6962. This makes sure that the subtree remains an independent subtree and does
  6963. not swallow low level entries.
  6964. You can also force a different level, either by using a numeric prefix
  6965. argument, or by inserting the heading marker by hand. For example, if the
  6966. cursor is after \"*****\", then the tree will be shifted to level 5.
  6967. If optional TREE is given, use this text instead of the kill ring.
  6968. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6969. move back over whitespace before inserting, and move point to the end of
  6970. the inserted text when done."
  6971. (interactive "P")
  6972. (setq tree (or tree (and kill-ring (current-kill 0))))
  6973. (unless (org-kill-is-subtree-p tree)
  6974. (error "%s"
  6975. (substitute-command-keys
  6976. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6977. (org-with-limited-levels
  6978. (let* ((visp (not (outline-invisible-p)))
  6979. (txt tree)
  6980. (^re_ "\\(\\*+\\)[ \t]*")
  6981. (old-level (if (string-match org-outline-regexp-bol txt)
  6982. (- (match-end 0) (match-beginning 0) 1)
  6983. -1))
  6984. (force-level (cond (level (prefix-numeric-value level))
  6985. ((and (looking-at "[ \t]*$")
  6986. (string-match
  6987. "^\\*+$" (buffer-substring
  6988. (point-at-bol) (point))))
  6989. (- (match-end 1) (match-beginning 1)))
  6990. ((and (bolp)
  6991. (looking-at org-outline-regexp))
  6992. (- (match-end 0) (point) 1))))
  6993. (previous-level (save-excursion
  6994. (condition-case nil
  6995. (progn
  6996. (outline-previous-visible-heading 1)
  6997. (if (looking-at ^re_)
  6998. (- (match-end 0) (match-beginning 0) 1)
  6999. 1))
  7000. (error 1))))
  7001. (next-level (save-excursion
  7002. (condition-case nil
  7003. (progn
  7004. (or (looking-at org-outline-regexp)
  7005. (outline-next-visible-heading 1))
  7006. (if (looking-at ^re_)
  7007. (- (match-end 0) (match-beginning 0) 1)
  7008. 1))
  7009. (error 1))))
  7010. (new-level (or force-level (max previous-level next-level)))
  7011. (shift (if (or (= old-level -1)
  7012. (= new-level -1)
  7013. (= old-level new-level))
  7014. 0
  7015. (- new-level old-level)))
  7016. (delta (if (> shift 0) -1 1))
  7017. (func (if (> shift 0) 'org-demote 'org-promote))
  7018. (org-odd-levels-only nil)
  7019. beg end newend)
  7020. ;; Remove the forced level indicator
  7021. (if force-level
  7022. (delete-region (point-at-bol) (point)))
  7023. ;; Paste
  7024. (beginning-of-line (if (bolp) 1 2))
  7025. (unless for-yank (org-back-over-empty-lines))
  7026. (setq beg (point))
  7027. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7028. (insert-before-markers txt)
  7029. (unless (string-match "\n\\'" txt) (insert "\n"))
  7030. (setq newend (point))
  7031. (org-reinstall-markers-in-region beg)
  7032. (setq end (point))
  7033. (goto-char beg)
  7034. (skip-chars-forward " \t\n\r")
  7035. (setq beg (point))
  7036. (if (and (outline-invisible-p) visp)
  7037. (save-excursion (outline-show-heading)))
  7038. ;; Shift if necessary
  7039. (unless (= shift 0)
  7040. (save-restriction
  7041. (narrow-to-region beg end)
  7042. (while (not (= shift 0))
  7043. (org-map-region func (point-min) (point-max))
  7044. (setq shift (+ delta shift)))
  7045. (goto-char (point-min))
  7046. (setq newend (point-max))))
  7047. (when (or (org-called-interactively-p 'interactive) for-yank)
  7048. (message "Clipboard pasted as level %d subtree" new-level))
  7049. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7050. kill-ring
  7051. (eq org-subtree-clip (current-kill 0))
  7052. org-subtree-clip-folded)
  7053. ;; The tree was folded before it was killed/copied
  7054. (hide-subtree))
  7055. (and for-yank (goto-char newend)))))
  7056. (defun org-kill-is-subtree-p (&optional txt)
  7057. "Check if the current kill is an outline subtree, or a set of trees.
  7058. Returns nil if kill does not start with a headline, or if the first
  7059. headline level is not the largest headline level in the tree.
  7060. So this will actually accept several entries of equal levels as well,
  7061. which is OK for `org-paste-subtree'.
  7062. If optional TXT is given, check this string instead of the current kill."
  7063. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7064. (re (org-get-limited-outline-regexp))
  7065. (^re (concat "^" re))
  7066. (start-level (and kill
  7067. (string-match
  7068. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7069. kill)
  7070. (- (match-end 2) (match-beginning 2) 1)))
  7071. (start (1+ (or (match-beginning 2) -1))))
  7072. (if (not start-level)
  7073. (progn
  7074. nil) ;; does not even start with a heading
  7075. (catch 'exit
  7076. (while (setq start (string-match ^re kill (1+ start)))
  7077. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7078. (throw 'exit nil)))
  7079. t))))
  7080. (defvar org-markers-to-move nil
  7081. "Markers that should be moved with a cut-and-paste operation.
  7082. Those markers are stored together with their positions relative to
  7083. the start of the region.")
  7084. (defun org-save-markers-in-region (beg end)
  7085. "Check markers in region.
  7086. If these markers are between BEG and END, record their position relative
  7087. to BEG, so that after moving the block of text, we can put the markers back
  7088. into place.
  7089. This function gets called just before an entry or tree gets cut from the
  7090. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7091. called immediately, to move the markers with the entries."
  7092. (setq org-markers-to-move nil)
  7093. (when (featurep 'org-clock)
  7094. (org-clock-save-markers-for-cut-and-paste beg end))
  7095. (when (featurep 'org-agenda)
  7096. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7097. (defun org-check-and-save-marker (marker beg end)
  7098. "Check if MARKER is between BEG and END.
  7099. If yes, remember the marker and the distance to BEG."
  7100. (when (and (marker-buffer marker)
  7101. (equal (marker-buffer marker) (current-buffer)))
  7102. (if (and (>= marker beg) (< marker end))
  7103. (push (cons marker (- marker beg)) org-markers-to-move))))
  7104. (defun org-reinstall-markers-in-region (beg)
  7105. "Move all remembered markers to their position relative to BEG."
  7106. (mapc (lambda (x)
  7107. (move-marker (car x) (+ beg (cdr x))))
  7108. org-markers-to-move)
  7109. (setq org-markers-to-move nil))
  7110. (defun org-narrow-to-subtree ()
  7111. "Narrow buffer to the current subtree."
  7112. (interactive)
  7113. (save-excursion
  7114. (save-match-data
  7115. (org-with-limited-levels
  7116. (narrow-to-region
  7117. (progn (org-back-to-heading t) (point))
  7118. (progn (org-end-of-subtree t t)
  7119. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7120. (point)))))))
  7121. (defun org-narrow-to-block ()
  7122. "Narrow buffer to the current block."
  7123. (interactive)
  7124. (let* ((case-fold-search t)
  7125. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7126. "^[ \t]*#\\+end_.*")))
  7127. (if blockp
  7128. (narrow-to-region (car blockp) (cdr blockp))
  7129. (error "Not in a block"))))
  7130. (eval-when-compile
  7131. (defvar org-property-drawer-re))
  7132. (defvar org-property-start-re) ;; defined below
  7133. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7134. "Clone the task (subtree) at point N times.
  7135. The clones will be inserted as siblings.
  7136. In interactive use, the user will be prompted for the number of
  7137. clones to be produced, and for a time SHIFT, which may be a
  7138. repeater as used in time stamps, for example `+3d'.
  7139. When a valid repeater is given and the entry contains any time
  7140. stamps, the clones will become a sequence in time, with time
  7141. stamps in the subtree shifted for each clone produced. If SHIFT
  7142. is nil or the empty string, time stamps will be left alone. The
  7143. ID property of the original subtree is removed.
  7144. If the original subtree did contain time stamps with a repeater,
  7145. the following will happen:
  7146. - the repeater will be removed in each clone
  7147. - an additional clone will be produced, with the current, unshifted
  7148. date(s) in the entry.
  7149. - the original entry will be placed *after* all the clones, with
  7150. repeater intact.
  7151. - the start days in the repeater in the original entry will be shifted
  7152. to past the last clone.
  7153. In this way you can spell out a number of instances of a repeating task,
  7154. and still retain the repeater to cover future instances of the task."
  7155. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7156. (let (beg end template task idprop
  7157. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7158. (drawer-re org-drawer-regexp))
  7159. (if (not (and (integerp n) (> n 0)))
  7160. (error "Invalid number of replications %s" n))
  7161. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7162. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7163. shift)))
  7164. (error "Invalid shift specification %s" shift))
  7165. (when doshift
  7166. (setq shift-n (string-to-number (match-string 1 shift))
  7167. shift-what (cdr (assoc (match-string 2 shift)
  7168. '(("d" . day) ("w" . week)
  7169. ("m" . month) ("y" . year))))))
  7170. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7171. (setq nmin 1 nmax n)
  7172. (org-back-to-heading t)
  7173. (setq beg (point))
  7174. (setq idprop (org-entry-get nil "ID"))
  7175. (org-end-of-subtree t t)
  7176. (or (bolp) (insert "\n"))
  7177. (setq end (point))
  7178. (setq template (buffer-substring beg end))
  7179. (when (and doshift
  7180. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7181. (delete-region beg end)
  7182. (setq end beg)
  7183. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7184. (goto-char end)
  7185. (loop for n from nmin to nmax do
  7186. ;; prepare clone
  7187. (with-temp-buffer
  7188. (insert template)
  7189. (org-mode)
  7190. (goto-char (point-min))
  7191. (org-show-subtree)
  7192. (and idprop (if org-clone-delete-id
  7193. (org-entry-delete nil "ID")
  7194. (org-id-get-create t)))
  7195. (unless (= n 0)
  7196. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7197. (kill-whole-line))
  7198. (goto-char (point-min))
  7199. (while (re-search-forward drawer-re nil t)
  7200. (mapc (lambda (d)
  7201. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7202. (goto-char (point-min))
  7203. (when doshift
  7204. (while (re-search-forward org-ts-regexp-both nil t)
  7205. (org-timestamp-change (* n shift-n) shift-what))
  7206. (unless (= n n-no-remove)
  7207. (goto-char (point-min))
  7208. (while (re-search-forward org-ts-regexp nil t)
  7209. (save-excursion
  7210. (goto-char (match-beginning 0))
  7211. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7212. (delete-region (match-beginning 1) (match-end 1)))))))
  7213. (setq task (buffer-string)))
  7214. (insert task))
  7215. (goto-char beg)))
  7216. ;;; Outline Sorting
  7217. (defun org-sort (with-case)
  7218. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7219. Optional argument WITH-CASE means sort case-sensitively."
  7220. (interactive "P")
  7221. (cond
  7222. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7223. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7224. (t
  7225. (org-call-with-arg 'org-sort-entries with-case))))
  7226. (defun org-sort-remove-invisible (s)
  7227. (remove-text-properties 0 (length s) org-rm-props s)
  7228. (while (string-match org-bracket-link-regexp s)
  7229. (setq s (replace-match (if (match-end 2)
  7230. (match-string 3 s)
  7231. (match-string 1 s)) t t s)))
  7232. s)
  7233. (defvar org-priority-regexp) ; defined later in the file
  7234. (defvar org-after-sorting-entries-or-items-hook nil
  7235. "Hook that is run after a bunch of entries or items have been sorted.
  7236. When children are sorted, the cursor is in the parent line when this
  7237. hook gets called. When a region or a plain list is sorted, the cursor
  7238. will be in the first entry of the sorted region/list.")
  7239. (defun org-sort-entries
  7240. (&optional with-case sorting-type getkey-func compare-func property)
  7241. "Sort entries on a certain level of an outline tree.
  7242. If there is an active region, the entries in the region are sorted.
  7243. Else, if the cursor is before the first entry, sort the top-level items.
  7244. Else, the children of the entry at point are sorted.
  7245. Sorting can be alphabetically, numerically, by date/time as given by
  7246. a time stamp, by a property or by priority.
  7247. The command prompts for the sorting type unless it has been given to the
  7248. function through the SORTING-TYPE argument, which needs to be a character,
  7249. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7250. precise meaning of each character:
  7251. n Numerically, by converting the beginning of the entry/item to a number.
  7252. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7253. t By date/time, either the first active time stamp in the entry, or, if
  7254. none exist, by the first inactive one.
  7255. s By the scheduled date/time.
  7256. d By deadline date/time.
  7257. c By creation time, which is assumed to be the first inactive time stamp
  7258. at the beginning of a line.
  7259. p By priority according to the cookie.
  7260. r By the value of a property.
  7261. Capital letters will reverse the sort order.
  7262. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7263. called with point at the beginning of the record. It must return either
  7264. a string or a number that should serve as the sorting key for that record.
  7265. Comparing entries ignores case by default. However, with an optional argument
  7266. WITH-CASE, the sorting considers case as well."
  7267. (interactive "P")
  7268. (let ((case-func (if with-case 'identity 'downcase))
  7269. start beg end stars re re2
  7270. txt what tmp)
  7271. ;; Find beginning and end of region to sort
  7272. (cond
  7273. ((org-region-active-p)
  7274. ;; we will sort the region
  7275. (setq end (region-end)
  7276. what "region")
  7277. (goto-char (region-beginning))
  7278. (if (not (org-at-heading-p)) (outline-next-heading))
  7279. (setq start (point)))
  7280. ((or (org-at-heading-p)
  7281. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7282. ;; we will sort the children of the current headline
  7283. (org-back-to-heading)
  7284. (setq start (point)
  7285. end (progn (org-end-of-subtree t t)
  7286. (or (bolp) (insert "\n"))
  7287. (org-back-over-empty-lines)
  7288. (point))
  7289. what "children")
  7290. (goto-char start)
  7291. (show-subtree)
  7292. (outline-next-heading))
  7293. (t
  7294. ;; we will sort the top-level entries in this file
  7295. (goto-char (point-min))
  7296. (or (org-at-heading-p) (outline-next-heading))
  7297. (setq start (point))
  7298. (goto-char (point-max))
  7299. (beginning-of-line 1)
  7300. (when (looking-at ".*?\\S-")
  7301. ;; File ends in a non-white line
  7302. (end-of-line 1)
  7303. (insert "\n"))
  7304. (setq end (point-max))
  7305. (setq what "top-level")
  7306. (goto-char start)
  7307. (show-all)))
  7308. (setq beg (point))
  7309. (if (>= beg end) (error "Nothing to sort"))
  7310. (looking-at "\\(\\*+\\)")
  7311. (setq stars (match-string 1)
  7312. re (concat "^" (regexp-quote stars) " +")
  7313. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7314. txt (buffer-substring beg end))
  7315. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7316. (if (and (not (equal stars "*")) (string-match re2 txt))
  7317. (error "Region to sort contains a level above the first entry"))
  7318. (unless sorting-type
  7319. (message
  7320. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7321. [t]ime [s]cheduled [d]eadline [c]reated
  7322. A/N/T/S/D/C/P/O/F means reversed:"
  7323. what)
  7324. (setq sorting-type (read-char-exclusive))
  7325. (unless getkey-func
  7326. (and (= (downcase sorting-type) ?f)
  7327. (setq getkey-func
  7328. (org-icompleting-read "Sort using function: "
  7329. obarray 'fboundp t nil nil))
  7330. (setq getkey-func (intern getkey-func))))
  7331. (and (= (downcase sorting-type) ?r)
  7332. (not property)
  7333. (setq property
  7334. (org-icompleting-read "Property: "
  7335. (mapcar 'list (org-buffer-property-keys t))
  7336. nil t))))
  7337. (message "Sorting entries...")
  7338. (save-restriction
  7339. (narrow-to-region start end)
  7340. (let ((dcst (downcase sorting-type))
  7341. (case-fold-search nil)
  7342. (now (current-time)))
  7343. (sort-subr
  7344. (/= dcst sorting-type)
  7345. ;; This function moves to the beginning character of the "record" to
  7346. ;; be sorted.
  7347. (lambda nil
  7348. (if (re-search-forward re nil t)
  7349. (goto-char (match-beginning 0))
  7350. (goto-char (point-max))))
  7351. ;; This function moves to the last character of the "record" being
  7352. ;; sorted.
  7353. (lambda nil
  7354. (save-match-data
  7355. (condition-case nil
  7356. (outline-forward-same-level 1)
  7357. (error
  7358. (goto-char (point-max))))))
  7359. ;; This function returns the value that gets sorted against.
  7360. (lambda nil
  7361. (cond
  7362. ((= dcst ?n)
  7363. (if (looking-at org-complex-heading-regexp)
  7364. (string-to-number (match-string 4))
  7365. nil))
  7366. ((= dcst ?a)
  7367. (if (looking-at org-complex-heading-regexp)
  7368. (funcall case-func (match-string 4))
  7369. nil))
  7370. ((= dcst ?t)
  7371. (let ((end (save-excursion (outline-next-heading) (point))))
  7372. (if (or (re-search-forward org-ts-regexp end t)
  7373. (re-search-forward org-ts-regexp-both end t))
  7374. (org-time-string-to-seconds (match-string 0))
  7375. (org-float-time now))))
  7376. ((= dcst ?c)
  7377. (let ((end (save-excursion (outline-next-heading) (point))))
  7378. (if (re-search-forward
  7379. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7380. end t)
  7381. (org-time-string-to-seconds (match-string 0))
  7382. (org-float-time now))))
  7383. ((= dcst ?s)
  7384. (let ((end (save-excursion (outline-next-heading) (point))))
  7385. (if (re-search-forward org-scheduled-time-regexp end t)
  7386. (org-time-string-to-seconds (match-string 1))
  7387. (org-float-time now))))
  7388. ((= dcst ?d)
  7389. (let ((end (save-excursion (outline-next-heading) (point))))
  7390. (if (re-search-forward org-deadline-time-regexp end t)
  7391. (org-time-string-to-seconds (match-string 1))
  7392. (org-float-time now))))
  7393. ((= dcst ?p)
  7394. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7395. (string-to-char (match-string 2))
  7396. org-default-priority))
  7397. ((= dcst ?r)
  7398. (or (org-entry-get nil property) ""))
  7399. ((= dcst ?o)
  7400. (if (looking-at org-complex-heading-regexp)
  7401. (- 9999 (length (member (match-string 2)
  7402. org-todo-keywords-1)))))
  7403. ((= dcst ?f)
  7404. (if getkey-func
  7405. (progn
  7406. (setq tmp (funcall getkey-func))
  7407. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7408. tmp)
  7409. (error "Invalid key function `%s'" getkey-func)))
  7410. (t (error "Invalid sorting type `%c'" sorting-type))))
  7411. nil
  7412. (cond
  7413. ((= dcst ?a) 'string<)
  7414. ((= dcst ?f) compare-func)
  7415. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7416. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7417. (message "Sorting entries...done")))
  7418. (defun org-do-sort (table what &optional with-case sorting-type)
  7419. "Sort TABLE of WHAT according to SORTING-TYPE.
  7420. The user will be prompted for the SORTING-TYPE if the call to this
  7421. function does not specify it. WHAT is only for the prompt, to indicate
  7422. what is being sorted. The sorting key will be extracted from
  7423. the car of the elements of the table.
  7424. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7425. (unless sorting-type
  7426. (message
  7427. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7428. what)
  7429. (setq sorting-type (read-char-exclusive)))
  7430. (let ((dcst (downcase sorting-type))
  7431. extractfun comparefun)
  7432. ;; Define the appropriate functions
  7433. (cond
  7434. ((= dcst ?n)
  7435. (setq extractfun 'string-to-number
  7436. comparefun (if (= dcst sorting-type) '< '>)))
  7437. ((= dcst ?a)
  7438. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7439. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7440. comparefun (if (= dcst sorting-type)
  7441. 'string<
  7442. (lambda (a b) (and (not (string< a b))
  7443. (not (string= a b)))))))
  7444. ((= dcst ?t)
  7445. (setq extractfun
  7446. (lambda (x)
  7447. (if (or (string-match org-ts-regexp x)
  7448. (string-match org-ts-regexp-both x))
  7449. (org-float-time
  7450. (org-time-string-to-time (match-string 0 x)))
  7451. 0))
  7452. comparefun (if (= dcst sorting-type) '< '>)))
  7453. (t (error "Invalid sorting type `%c'" sorting-type)))
  7454. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7455. table)
  7456. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7457. ;;; The orgstruct minor mode
  7458. ;; Define a minor mode which can be used in other modes in order to
  7459. ;; integrate the org-mode structure editing commands.
  7460. ;; This is really a hack, because the org-mode structure commands use
  7461. ;; keys which normally belong to the major mode. Here is how it
  7462. ;; works: The minor mode defines all the keys necessary to operate the
  7463. ;; structure commands, but wraps the commands into a function which
  7464. ;; tests if the cursor is currently at a headline or a plain list
  7465. ;; item. If that is the case, the structure command is used,
  7466. ;; temporarily setting many Org-mode variables like regular
  7467. ;; expressions for filling etc. However, when any of those keys is
  7468. ;; used at a different location, function uses `key-binding' to look
  7469. ;; up if the key has an associated command in another currently active
  7470. ;; keymap (minor modes, major mode, global), and executes that
  7471. ;; command. There might be problems if any of the keys is otherwise
  7472. ;; used as a prefix key.
  7473. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7474. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7475. ;; addresses this by checking explicitly for both bindings.
  7476. (defvar orgstruct-mode-map (make-sparse-keymap)
  7477. "Keymap for the minor `orgstruct-mode'.")
  7478. (defvar org-local-vars nil
  7479. "List of local variables, for use by `orgstruct-mode'.")
  7480. ;;;###autoload
  7481. (define-minor-mode orgstruct-mode
  7482. "Toggle the minor mode `orgstruct-mode'.
  7483. This mode is for using Org-mode structure commands in other
  7484. modes. The following keys behave as if Org-mode were active, if
  7485. the cursor is on a headline, or on a plain list item (both as
  7486. defined by Org-mode).
  7487. M-up Move entry/item up
  7488. M-down Move entry/item down
  7489. M-left Promote
  7490. M-right Demote
  7491. M-S-up Move entry/item up
  7492. M-S-down Move entry/item down
  7493. M-S-left Promote subtree
  7494. M-S-right Demote subtree
  7495. M-q Fill paragraph and items like in Org-mode
  7496. C-c ^ Sort entries
  7497. C-c - Cycle list bullet
  7498. TAB Cycle item visibility
  7499. M-RET Insert new heading/item
  7500. S-M-RET Insert new TODO heading / Checkbox item
  7501. C-c C-c Set tags / toggle checkbox"
  7502. nil " OrgStruct" nil
  7503. (org-load-modules-maybe)
  7504. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7505. ;;;###autoload
  7506. (defun turn-on-orgstruct ()
  7507. "Unconditionally turn on `orgstruct-mode'."
  7508. (orgstruct-mode 1))
  7509. (defvar org-fb-vars nil)
  7510. (make-variable-buffer-local 'org-fb-vars)
  7511. (defun orgstruct++-mode (&optional arg)
  7512. "Toggle `orgstruct-mode', the enhanced version of it.
  7513. In addition to setting orgstruct-mode, this also exports all
  7514. indentation and autofilling variables from org-mode into the
  7515. buffer. It will also recognize item context in multiline items."
  7516. (interactive "P")
  7517. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7518. (if (< arg 1)
  7519. (progn (orgstruct-mode -1)
  7520. (mapc (lambda(v)
  7521. (org-set-local (car v)
  7522. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7523. org-fb-vars))
  7524. (orgstruct-mode 1)
  7525. (setq org-fb-vars nil)
  7526. (let (var val)
  7527. (mapc
  7528. (lambda (x)
  7529. (when (string-match
  7530. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7531. (symbol-name (car x)))
  7532. (setq var (car x) val (nth 1 x))
  7533. (push (list var `(quote ,(eval var))) org-fb-vars)
  7534. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7535. org-local-vars)
  7536. (org-set-local 'orgstruct-is-++ t))))
  7537. (defvar orgstruct-is-++ nil
  7538. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7539. (make-variable-buffer-local 'orgstruct-is-++)
  7540. ;;;###autoload
  7541. (defun turn-on-orgstruct++ ()
  7542. "Unconditionally turn on `orgstruct++-mode'."
  7543. (orgstruct++-mode 1))
  7544. (defun orgstruct-error ()
  7545. "Error when there is no default binding for a structure key."
  7546. (interactive)
  7547. (error "This key has no function outside structure elements"))
  7548. (defun orgstruct-setup ()
  7549. "Setup orgstruct keymaps."
  7550. (let ((nfunc 0)
  7551. (bindings
  7552. (list
  7553. '([(meta up)] org-metaup)
  7554. '([(meta down)] org-metadown)
  7555. '([(meta left)] org-metaleft)
  7556. '([(meta right)] org-metaright)
  7557. '([(meta shift up)] org-shiftmetaup)
  7558. '([(meta shift down)] org-shiftmetadown)
  7559. '([(meta shift left)] org-shiftmetaleft)
  7560. '([(meta shift right)] org-shiftmetaright)
  7561. '([?\e (up)] org-metaup)
  7562. '([?\e (down)] org-metadown)
  7563. '([?\e (left)] org-metaleft)
  7564. '([?\e (right)] org-metaright)
  7565. '([?\e (shift up)] org-shiftmetaup)
  7566. '([?\e (shift down)] org-shiftmetadown)
  7567. '([?\e (shift left)] org-shiftmetaleft)
  7568. '([?\e (shift right)] org-shiftmetaright)
  7569. '([(shift up)] org-shiftup)
  7570. '([(shift down)] org-shiftdown)
  7571. '([(shift left)] org-shiftleft)
  7572. '([(shift right)] org-shiftright)
  7573. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7574. '("\M-q" fill-paragraph)
  7575. '("\C-c^" org-sort)
  7576. '("\C-c-" org-cycle-list-bullet)))
  7577. elt key fun cmd)
  7578. (while (setq elt (pop bindings))
  7579. (setq nfunc (1+ nfunc))
  7580. (setq key (org-key (car elt))
  7581. fun (nth 1 elt)
  7582. cmd (orgstruct-make-binding fun nfunc key))
  7583. (org-defkey orgstruct-mode-map key cmd))
  7584. ;; Prevent an error for users who forgot to make autoloads
  7585. (require 'org-element)
  7586. ;; Special treatment needed for TAB and RET
  7587. (org-defkey orgstruct-mode-map [(tab)]
  7588. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7589. (org-defkey orgstruct-mode-map "\C-i"
  7590. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7591. (org-defkey orgstruct-mode-map "\M-\C-m"
  7592. (orgstruct-make-binding 'org-insert-heading 105
  7593. "\M-\C-m" [(meta return)]))
  7594. (org-defkey orgstruct-mode-map [(meta return)]
  7595. (orgstruct-make-binding 'org-insert-heading 106
  7596. [(meta return)] "\M-\C-m"))
  7597. (org-defkey orgstruct-mode-map [(shift meta return)]
  7598. (orgstruct-make-binding 'org-insert-todo-heading 107
  7599. [(meta return)] "\M-\C-m"))
  7600. (org-defkey orgstruct-mode-map "\e\C-m"
  7601. (orgstruct-make-binding 'org-insert-heading 108
  7602. "\e\C-m" [?\e (return)]))
  7603. (org-defkey orgstruct-mode-map [?\e (return)]
  7604. (orgstruct-make-binding 'org-insert-heading 109
  7605. [?\e (return)] "\e\C-m"))
  7606. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7607. (orgstruct-make-binding 'org-insert-todo-heading 110
  7608. [?\e (return)] "\e\C-m"))
  7609. (unless org-local-vars
  7610. (setq org-local-vars (org-get-local-variables)))
  7611. t))
  7612. (defun orgstruct-make-binding (fun n &rest keys)
  7613. "Create a function for binding in the structure minor mode.
  7614. FUN is the command to call inside a table. N is used to create a unique
  7615. command name. KEYS are keys that should be checked in for a command
  7616. to execute outside of tables."
  7617. (eval
  7618. (list 'defun
  7619. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7620. '(arg)
  7621. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7622. "Outside of structure, run the binding of `"
  7623. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7624. "'.")
  7625. '(interactive "p")
  7626. (list 'if
  7627. `(org-context-p 'headline 'item
  7628. (and orgstruct-is-++
  7629. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7630. 'item-body))
  7631. (list 'org-run-like-in-org-mode (list 'quote fun))
  7632. (list 'let '(orgstruct-mode)
  7633. (list 'call-interactively
  7634. (append '(or)
  7635. (mapcar (lambda (k)
  7636. (list 'key-binding k))
  7637. keys)
  7638. '('orgstruct-error))))))))
  7639. (defun org-contextualize-keys (alist contexts)
  7640. "Return valid elements in ALIST depending on CONTEXTS.
  7641. `org-agenda-custom-commands' or `org-capture-templates' are the
  7642. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7643. or `org-capture-templates-contexts' are the associated contexts
  7644. definitions."
  7645. (let ((contexts
  7646. ;; normalize contexts
  7647. (mapcar
  7648. (lambda(c) (cond ((listp (cadr c))
  7649. (list (car c) (car c) (cadr c)))
  7650. ((string= "" (cadr c))
  7651. (list (car c) (car c) (caddr c)))
  7652. (t c))) contexts))
  7653. (a alist) c r s)
  7654. ;; loop over all commands or templates
  7655. (while (setq c (pop a))
  7656. (let (vrules repl)
  7657. (cond
  7658. ((not (assoc (car c) contexts))
  7659. (push c r))
  7660. ((and (assoc (car c) contexts)
  7661. (setq vrules (org-contextualize-validate-key
  7662. (car c) contexts)))
  7663. (mapc (lambda (vr)
  7664. (when (not (equal (car vr) (cadr vr)))
  7665. (setq repl vr))) vrules)
  7666. (if (not repl) (push c r)
  7667. (push (cadr repl) s)
  7668. (push
  7669. (cons (car c)
  7670. (cdr (or (assoc (cadr repl) alist)
  7671. (error "Undefined key `%s' as contextual replacement for `%s'"
  7672. (cadr repl) (car c)))))
  7673. r))))))
  7674. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7675. (delq
  7676. nil
  7677. (delete-dups
  7678. (mapcar (lambda (x)
  7679. (let ((tpl (car x)))
  7680. (when (not (delq
  7681. nil
  7682. (mapcar (lambda(y)
  7683. (equal y tpl)) s))) x)))
  7684. (reverse r))))))
  7685. (defun org-contextualize-validate-key (key contexts)
  7686. "Check CONTEXTS for agenda or capture KEY."
  7687. (let (r rr res)
  7688. (while (setq r (pop contexts))
  7689. (mapc
  7690. (lambda (rr)
  7691. (when
  7692. (and (equal key (car r))
  7693. (if (functionp rr) (funcall rr)
  7694. (or (and (eq (car rr) 'in-file)
  7695. (buffer-file-name)
  7696. (string-match (cdr rr) (buffer-file-name)))
  7697. (and (eq (car rr) 'in-mode)
  7698. (string-match (cdr rr) (symbol-name major-mode)))
  7699. (when (and (eq (car rr) 'not-in-file)
  7700. (buffer-file-name))
  7701. (not (string-match (cdr rr) (buffer-file-name))))
  7702. (when (eq (car rr) 'not-in-mode)
  7703. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7704. (push r res)))
  7705. (car (last r))))
  7706. (delete-dups (delq nil res))))
  7707. (defun org-context-p (&rest contexts)
  7708. "Check if local context is any of CONTEXTS.
  7709. Possible values in the list of contexts are `table', `headline', and `item'."
  7710. (let ((pos (point)))
  7711. (goto-char (point-at-bol))
  7712. (prog1 (or (and (memq 'table contexts)
  7713. (looking-at "[ \t]*|"))
  7714. (and (memq 'headline contexts)
  7715. (looking-at org-outline-regexp))
  7716. (and (memq 'item contexts)
  7717. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7718. (and (memq 'item-body contexts)
  7719. (org-in-item-p)))
  7720. (goto-char pos))))
  7721. (defun org-get-local-variables ()
  7722. "Return a list of all local variables in an Org mode buffer."
  7723. (let (varlist)
  7724. (with-current-buffer (get-buffer-create "*Org tmp*")
  7725. (erase-buffer)
  7726. (org-mode)
  7727. (setq varlist (buffer-local-variables)))
  7728. (kill-buffer "*Org tmp*")
  7729. (delq nil
  7730. (mapcar
  7731. (lambda (x)
  7732. (setq x
  7733. (if (symbolp x)
  7734. (list x)
  7735. (list (car x) (list 'quote (cdr x)))))
  7736. (if (string-match
  7737. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7738. (symbol-name (car x)))
  7739. x nil))
  7740. varlist))))
  7741. (defun org-clone-local-variables (from-buffer &optional regexp)
  7742. "Clone local variables from FROM-BUFFER.
  7743. Optional argument REGEXP selects variables to clone."
  7744. (mapc
  7745. (lambda (pair)
  7746. (and (symbolp (car pair))
  7747. (or (null regexp)
  7748. (string-match regexp (symbol-name (car pair))))
  7749. (set (make-local-variable (car pair))
  7750. (cdr pair))))
  7751. (buffer-local-variables from-buffer)))
  7752. ;;;###autoload
  7753. (defun org-run-like-in-org-mode (cmd)
  7754. "Run a command, pretending that the current buffer is in Org-mode.
  7755. This will temporarily bind local variables that are typically bound in
  7756. Org-mode to the values they have in Org-mode, and then interactively
  7757. call CMD."
  7758. (org-load-modules-maybe)
  7759. (unless org-local-vars
  7760. (setq org-local-vars (org-get-local-variables)))
  7761. (eval (list 'let org-local-vars
  7762. (list 'call-interactively (list 'quote cmd)))))
  7763. ;;;; Archiving
  7764. (defun org-get-category (&optional pos force-refresh)
  7765. "Get the category applying to position POS."
  7766. (save-match-data
  7767. (if force-refresh (org-refresh-category-properties))
  7768. (let ((pos (or pos (point))))
  7769. (or (get-text-property pos 'org-category)
  7770. (progn (org-refresh-category-properties)
  7771. (get-text-property pos 'org-category))))))
  7772. (defun org-refresh-category-properties ()
  7773. "Refresh category text properties in the buffer."
  7774. (let ((case-fold-search t)
  7775. (inhibit-read-only t)
  7776. (def-cat (cond
  7777. ((null org-category)
  7778. (if buffer-file-name
  7779. (file-name-sans-extension
  7780. (file-name-nondirectory buffer-file-name))
  7781. "???"))
  7782. ((symbolp org-category) (symbol-name org-category))
  7783. (t org-category)))
  7784. beg end cat pos optionp)
  7785. (org-unmodified
  7786. (save-excursion
  7787. (save-restriction
  7788. (widen)
  7789. (goto-char (point-min))
  7790. (put-text-property (point) (point-max) 'org-category def-cat)
  7791. (while (re-search-forward
  7792. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7793. (setq pos (match-end 0)
  7794. optionp (equal (char-after (match-beginning 0)) ?#)
  7795. cat (org-trim (match-string 2)))
  7796. (if optionp
  7797. (setq beg (point-at-bol) end (point-max))
  7798. (org-back-to-heading t)
  7799. (setq beg (point) end (org-end-of-subtree t t)))
  7800. (put-text-property beg end 'org-category cat)
  7801. (put-text-property beg end 'org-category-position beg)
  7802. (goto-char pos)))))))
  7803. ;;;; Link Stuff
  7804. ;;; Link abbreviations
  7805. (defun org-link-expand-abbrev (link)
  7806. "Apply replacements as defined in `org-link-abbrev-alist'."
  7807. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7808. (let* ((key (match-string 1 link))
  7809. (as (or (assoc key org-link-abbrev-alist-local)
  7810. (assoc key org-link-abbrev-alist)))
  7811. (tag (and (match-end 2) (match-string 3 link)))
  7812. rpl)
  7813. (if (not as)
  7814. link
  7815. (setq rpl (cdr as))
  7816. (cond
  7817. ((symbolp rpl) (funcall rpl tag))
  7818. ((string-match "%(\\([^)]+\\))" rpl)
  7819. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7820. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7821. ((string-match "%h" rpl)
  7822. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7823. (t (concat rpl tag)))))
  7824. link))
  7825. ;;; Storing and inserting links
  7826. (defvar org-insert-link-history nil
  7827. "Minibuffer history for links inserted with `org-insert-link'.")
  7828. (defvar org-stored-links nil
  7829. "Contains the links stored with `org-store-link'.")
  7830. (defvar org-store-link-plist nil
  7831. "Plist with info about the most recently link created with `org-store-link'.")
  7832. (defvar org-link-protocols nil
  7833. "Link protocols added to Org-mode using `org-add-link-type'.")
  7834. (defvar org-store-link-functions nil
  7835. "List of functions that are called to create and store a link.
  7836. Each function will be called in turn until one returns a non-nil
  7837. value. Each function should check if it is responsible for creating
  7838. this link (for example by looking at the major mode).
  7839. If not, it must exit and return nil.
  7840. If yes, it should return a non-nil value after a calling
  7841. `org-store-link-props' with a list of properties and values.
  7842. Special properties are:
  7843. :type The link prefix, like \"http\". This must be given.
  7844. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7845. This is obligatory as well.
  7846. :description Optional default description for the second pair
  7847. of brackets in an Org-mode link. The user can still change
  7848. this when inserting this link into an Org-mode buffer.
  7849. In addition to these, any additional properties can be specified
  7850. and then used in capture templates.")
  7851. (defun org-add-link-type (type &optional follow export)
  7852. "Add TYPE to the list of `org-link-types'.
  7853. Re-compute all regular expressions depending on `org-link-types'
  7854. FOLLOW and EXPORT are two functions.
  7855. FOLLOW should take the link path as the single argument and do whatever
  7856. is necessary to follow the link, for example find a file or display
  7857. a mail message.
  7858. EXPORT should format the link path for export to one of the export formats.
  7859. It should be a function accepting three arguments:
  7860. path the path of the link, the text after the prefix (like \"http:\")
  7861. desc the description of the link, if any, or a description added by
  7862. org-export-normalize-links if there is none
  7863. format the export format, a symbol like `html' or `latex' or `ascii'..
  7864. The function may use the FORMAT information to return different values
  7865. depending on the format. The return value will be put literally into
  7866. the exported file. If the return value is nil, this means Org should
  7867. do what it normally does with links which do not have EXPORT defined.
  7868. Org-mode has a built-in default for exporting links. If you are happy with
  7869. this default, there is no need to define an export function for the link
  7870. type. For a simple example of an export function, see `org-bbdb.el'."
  7871. (add-to-list 'org-link-types type t)
  7872. (org-make-link-regexps)
  7873. (if (assoc type org-link-protocols)
  7874. (setcdr (assoc type org-link-protocols) (list follow export))
  7875. (push (list type follow export) org-link-protocols)))
  7876. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7877. (defvar org-link-to-org-use-id) ; Defined in org-id.el
  7878. ;;;###autoload
  7879. (defun org-store-link (arg)
  7880. "\\<org-mode-map>Store an org-link to the current location.
  7881. This link is added to `org-stored-links' and can later be inserted
  7882. into an org-buffer with \\[org-insert-link].
  7883. For some link types, a prefix arg is interpreted:
  7884. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7885. For file links, arg negates `org-context-in-file-links'."
  7886. (interactive "P")
  7887. (org-load-modules-maybe)
  7888. (setq org-store-link-plist nil) ; reset
  7889. (org-with-limited-levels
  7890. (let (link cpltxt desc description search txt custom-id agenda-link)
  7891. (cond
  7892. ((run-hook-with-args-until-success 'org-store-link-functions)
  7893. (setq link (plist-get org-store-link-plist :link)
  7894. desc (or (plist-get org-store-link-plist :description) link)))
  7895. ((org-src-edit-buffer-p)
  7896. (let (label gc)
  7897. (while (or (not label)
  7898. (save-excursion
  7899. (save-restriction
  7900. (widen)
  7901. (goto-char (point-min))
  7902. (re-search-forward
  7903. (regexp-quote (format org-coderef-label-format label))
  7904. nil t))))
  7905. (when label (message "Label exists already") (sit-for 2))
  7906. (setq label (read-string "Code line label: " label)))
  7907. (end-of-line 1)
  7908. (setq link (format org-coderef-label-format label))
  7909. (setq gc (- 79 (length link)))
  7910. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7911. (insert link)
  7912. (setq link (concat "(" label ")") desc nil)))
  7913. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7914. ;; We are in the agenda, link to referenced location
  7915. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7916. (get-text-property (point) 'org-marker))))
  7917. (when m
  7918. (org-with-point-at m
  7919. (setq agenda-link
  7920. (if (org-called-interactively-p 'any)
  7921. (call-interactively 'org-store-link)
  7922. (org-store-link nil)))))))
  7923. ((eq major-mode 'calendar-mode)
  7924. (let ((cd (calendar-cursor-to-date)))
  7925. (setq link
  7926. (format-time-string
  7927. (car org-time-stamp-formats)
  7928. (apply 'encode-time
  7929. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7930. nil nil nil))))
  7931. (org-store-link-props :type "calendar" :date cd)))
  7932. ((eq major-mode 'help-mode)
  7933. (setq link (concat "help:" (save-excursion
  7934. (goto-char (point-min))
  7935. (looking-at "^[^ ]+")
  7936. (match-string 0))))
  7937. (org-store-link-props :type "help"))
  7938. ((eq major-mode 'w3-mode)
  7939. (setq cpltxt (if (and (buffer-name)
  7940. (not (string-match "Untitled" (buffer-name))))
  7941. (buffer-name)
  7942. (url-view-url t))
  7943. link (url-view-url t))
  7944. (org-store-link-props :type "w3" :url (url-view-url t)))
  7945. ((eq major-mode 'w3m-mode)
  7946. (setq cpltxt (or w3m-current-title w3m-current-url)
  7947. link w3m-current-url)
  7948. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7949. ((setq search (run-hook-with-args-until-success
  7950. 'org-create-file-search-functions))
  7951. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7952. "::" search))
  7953. (setq cpltxt (or description link)))
  7954. ((eq major-mode 'image-mode)
  7955. (setq cpltxt (concat "file:"
  7956. (abbreviate-file-name buffer-file-name))
  7957. link cpltxt)
  7958. (org-store-link-props :type "image" :file buffer-file-name))
  7959. ((eq major-mode 'dired-mode)
  7960. ;; link to the file in the current line
  7961. (let ((file (dired-get-filename nil t)))
  7962. (setq file (if file
  7963. (abbreviate-file-name
  7964. (expand-file-name (dired-get-filename nil t)))
  7965. ;; otherwise, no file so use current directory.
  7966. default-directory))
  7967. (setq cpltxt (concat "file:" file)
  7968. link cpltxt)))
  7969. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7970. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7971. (cond
  7972. ((org-in-regexp "<<\\(.*?\\)>>")
  7973. (setq cpltxt
  7974. (concat "file:"
  7975. (abbreviate-file-name
  7976. (buffer-file-name (buffer-base-buffer)))
  7977. "::" (match-string 1))
  7978. link cpltxt))
  7979. ((and (featurep 'org-id)
  7980. (or (eq org-link-to-org-use-id t)
  7981. (and (org-called-interactively-p 'any)
  7982. (or (eq org-link-to-org-use-id 'create-if-interactive)
  7983. (and (eq org-link-to-org-use-id
  7984. 'create-if-interactive-and-no-custom-id)
  7985. (not custom-id))))
  7986. (and org-link-to-org-use-id (org-entry-get nil "ID"))))
  7987. ;; We can make a link using the ID.
  7988. (setq link (condition-case nil
  7989. (prog1 (org-id-store-link)
  7990. (setq desc (plist-get org-store-link-plist :description)))
  7991. (error
  7992. ;; probably before first headline, link to file only
  7993. (concat "file:"
  7994. (abbreviate-file-name
  7995. (buffer-file-name (buffer-base-buffer))))))))
  7996. (t
  7997. ;; Just link to current headline
  7998. (setq cpltxt (concat "file:"
  7999. (abbreviate-file-name
  8000. (buffer-file-name (buffer-base-buffer)))))
  8001. ;; Add a context search string
  8002. (when (org-xor org-context-in-file-links arg)
  8003. (setq txt (cond
  8004. ((org-at-heading-p) nil)
  8005. ((org-region-active-p)
  8006. (buffer-substring (region-beginning) (region-end)))))
  8007. (when (or (null txt) (string-match "\\S-" txt))
  8008. (setq cpltxt
  8009. (concat cpltxt "::"
  8010. (condition-case nil
  8011. (org-make-org-heading-search-string txt)
  8012. (error "")))
  8013. desc (or (nth 4 (ignore-errors
  8014. (org-heading-components))) "NONE"))))
  8015. (if (string-match "::\\'" cpltxt)
  8016. (setq cpltxt (substring cpltxt 0 -2)))
  8017. (setq link cpltxt))))
  8018. ((buffer-file-name (buffer-base-buffer))
  8019. ;; Just link to this file here.
  8020. (setq cpltxt (concat "file:"
  8021. (abbreviate-file-name
  8022. (buffer-file-name (buffer-base-buffer)))))
  8023. ;; Add a context string
  8024. (when (org-xor org-context-in-file-links arg)
  8025. (setq txt (if (org-region-active-p)
  8026. (buffer-substring (region-beginning) (region-end))
  8027. (buffer-substring (point-at-bol) (point-at-eol))))
  8028. ;; Only use search option if there is some text.
  8029. (when (string-match "\\S-" txt)
  8030. (setq cpltxt
  8031. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8032. desc "NONE")))
  8033. (setq link cpltxt))
  8034. ((org-called-interactively-p 'interactive)
  8035. (error "Cannot link to a buffer which is not visiting a file"))
  8036. (t (setq link nil)))
  8037. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8038. (setq link (or link cpltxt)
  8039. desc (or desc cpltxt))
  8040. (if (equal desc "NONE") (setq desc nil))
  8041. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8042. (progn
  8043. (setq org-stored-links
  8044. (cons (list link desc) org-stored-links))
  8045. (message "Stored: %s" (or desc link))
  8046. (when custom-id
  8047. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8048. "::#" custom-id))
  8049. (setq org-stored-links
  8050. (cons (list link desc) org-stored-links))))
  8051. (or agenda-link (and link (org-make-link-string link desc)))))))
  8052. (defun org-store-link-props (&rest plist)
  8053. "Store link properties, extract names and addresses."
  8054. (let (x adr)
  8055. (when (setq x (plist-get plist :from))
  8056. (setq adr (mail-extract-address-components x))
  8057. (setq plist (plist-put plist :fromname (car adr)))
  8058. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8059. (when (setq x (plist-get plist :to))
  8060. (setq adr (mail-extract-address-components x))
  8061. (setq plist (plist-put plist :toname (car adr)))
  8062. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8063. (let ((from (plist-get plist :from))
  8064. (to (plist-get plist :to)))
  8065. (when (and from to org-from-is-user-regexp)
  8066. (setq plist
  8067. (plist-put plist :fromto
  8068. (if (string-match org-from-is-user-regexp from)
  8069. (concat "to %t")
  8070. (concat "from %f"))))))
  8071. (setq org-store-link-plist plist))
  8072. (defun org-add-link-props (&rest plist)
  8073. "Add these properties to the link property list."
  8074. (let (key value)
  8075. (while plist
  8076. (setq key (pop plist) value (pop plist))
  8077. (setq org-store-link-plist
  8078. (plist-put org-store-link-plist key value)))))
  8079. (defun org-email-link-description (&optional fmt)
  8080. "Return the description part of an email link.
  8081. This takes information from `org-store-link-plist' and formats it
  8082. according to FMT (default from `org-email-link-description-format')."
  8083. (setq fmt (or fmt org-email-link-description-format))
  8084. (let* ((p org-store-link-plist)
  8085. (to (plist-get p :toaddress))
  8086. (from (plist-get p :fromaddress))
  8087. (table
  8088. (list
  8089. (cons "%c" (plist-get p :fromto))
  8090. (cons "%F" (plist-get p :from))
  8091. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8092. (cons "%T" (plist-get p :to))
  8093. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8094. (cons "%s" (plist-get p :subject))
  8095. (cons "%d" (plist-get p :date))
  8096. (cons "%m" (plist-get p :message-id)))))
  8097. (when (string-match "%c" fmt)
  8098. ;; Check if the user wrote this message
  8099. (if (and org-from-is-user-regexp from to
  8100. (save-match-data (string-match org-from-is-user-regexp from)))
  8101. (setq fmt (replace-match "to %t" t t fmt))
  8102. (setq fmt (replace-match "from %f" t t fmt))))
  8103. (org-replace-escapes fmt table)))
  8104. (defun org-make-org-heading-search-string (&optional string heading)
  8105. "Make search string for STRING or current headline."
  8106. (interactive)
  8107. (let ((s (or string (org-get-heading)))
  8108. (lines org-context-in-file-links))
  8109. (unless (and string (not heading))
  8110. ;; We are using a headline, clean up garbage in there.
  8111. (if (string-match org-todo-regexp s)
  8112. (setq s (replace-match "" t t s)))
  8113. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8114. (setq s (replace-match "" t t s)))
  8115. (setq s (org-trim s))
  8116. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8117. org-comment-string "\\)") s)
  8118. (setq s (replace-match "" t t s)))
  8119. (while (string-match org-ts-regexp s)
  8120. (setq s (replace-match "" t t s))))
  8121. (or string (setq s (concat "*" s))) ; Add * for headlines
  8122. (when (and string (integerp lines) (> lines 0))
  8123. (let ((slines (org-split-string s "\n")))
  8124. (when (< lines (length slines))
  8125. (setq s (mapconcat
  8126. 'identity
  8127. (reverse (nthcdr (- (length slines) lines)
  8128. (reverse slines))) "\n")))))
  8129. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8130. (defun org-make-link-string (link &optional description)
  8131. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8132. (unless (string-match "\\S-" link)
  8133. (error "Empty link"))
  8134. (when (and description
  8135. (stringp description)
  8136. (not (string-match "\\S-" description)))
  8137. (setq description nil))
  8138. (when (stringp description)
  8139. ;; Remove brackets from the description, they are fatal.
  8140. (while (string-match "\\[" description)
  8141. (setq description (replace-match "{" t t description)))
  8142. (while (string-match "\\]" description)
  8143. (setq description (replace-match "}" t t description))))
  8144. (when (equal link description)
  8145. ;; No description needed, it is identical
  8146. (setq description nil))
  8147. (when (and (not description)
  8148. (not (string-match (org-image-file-name-regexp) link))
  8149. (not (equal link (org-link-escape link))))
  8150. (setq description (org-extract-attributes link)))
  8151. (setq link
  8152. (cond ((string-match (org-image-file-name-regexp) link) link)
  8153. ((string-match org-link-types-re link)
  8154. (concat (match-string 1 link)
  8155. (org-link-escape (substring link (match-end 1)))))
  8156. (t (org-link-escape link))))
  8157. (concat "[[" link "]"
  8158. (if description (concat "[" description "]") "")
  8159. "]"))
  8160. (defconst org-link-escape-chars
  8161. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8162. "List of characters that should be escaped in link.
  8163. This is the list that is used for internal purposes.")
  8164. (defconst org-link-escape-chars-browser
  8165. '(?\ )
  8166. "List of escapes for characters that are problematic in links.
  8167. This is the list that is used before handing over to the browser.")
  8168. (defun org-link-escape (text &optional table merge)
  8169. "Return percent escaped representation of TEXT.
  8170. TEXT is a string with the text to escape.
  8171. Optional argument TABLE is a list with characters that should be
  8172. escaped. When nil, `org-link-escape-chars' is used.
  8173. If optional argument MERGE is set, merge TABLE into
  8174. `org-link-escape-chars'."
  8175. (cond
  8176. ((and table merge)
  8177. (mapc (lambda (defchr)
  8178. (unless (member defchr table)
  8179. (setq table (cons defchr table)))) org-link-escape-chars))
  8180. ((null table)
  8181. (setq table org-link-escape-chars)))
  8182. (mapconcat
  8183. (lambda (char)
  8184. (if (or (member char table)
  8185. (and (or (< char 32) (= char 37) (> char 126))
  8186. org-url-hexify-p))
  8187. (mapconcat (lambda (sequence-element)
  8188. (format "%%%.2X" sequence-element))
  8189. (or (encode-coding-char char 'utf-8)
  8190. (error "Unable to percent escape character: %s"
  8191. (char-to-string char))) "")
  8192. (char-to-string char))) text ""))
  8193. (defun org-link-unescape (str)
  8194. "Unhex hexified Unicode strings as returned from the JavaScript function
  8195. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8196. (unless (and (null str) (string= "" str))
  8197. (let ((pos 0) (case-fold-search t) unhexed)
  8198. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8199. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8200. (setq str (replace-match unhexed t t str))
  8201. (setq pos (+ pos (length unhexed))))))
  8202. str)
  8203. (defun org-link-unescape-compound (hex)
  8204. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8205. Note: this function also decodes single byte encodings like
  8206. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8207. (save-match-data
  8208. (let* ((bytes (cdr (split-string hex "%")))
  8209. (ret "")
  8210. (eat 0)
  8211. (sum 0))
  8212. (while bytes
  8213. (let* ((val (string-to-number (pop bytes) 16))
  8214. (shift-xor
  8215. (if (= 0 eat)
  8216. (cond
  8217. ((>= val 252) (cons 6 252))
  8218. ((>= val 248) (cons 5 248))
  8219. ((>= val 240) (cons 4 240))
  8220. ((>= val 224) (cons 3 224))
  8221. ((>= val 192) (cons 2 192))
  8222. (t (cons 0 0)))
  8223. (cons 6 128))))
  8224. (if (>= val 192) (setq eat (car shift-xor)))
  8225. (setq val (logxor val (cdr shift-xor)))
  8226. (setq sum (+ (lsh sum (car shift-xor)) val))
  8227. (if (> eat 0) (setq eat (- eat 1)))
  8228. (cond
  8229. ((= 0 eat) ;multi byte
  8230. (setq ret (concat ret (org-char-to-string sum)))
  8231. (setq sum 0))
  8232. ((not bytes) ; single byte(s)
  8233. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8234. )) ;; end (while bytes
  8235. ret )))
  8236. (defun org-link-unescape-single-byte-sequence (hex)
  8237. "Unhexify hex-encoded single byte character sequences."
  8238. (mapconcat (lambda (byte)
  8239. (char-to-string (string-to-number byte 16)))
  8240. (cdr (split-string hex "%")) ""))
  8241. (defun org-xor (a b)
  8242. "Exclusive or."
  8243. (if a (not b) b))
  8244. (defun org-fixup-message-id-for-http (s)
  8245. "Replace special characters in a message id, so it can be used in an http query."
  8246. (when (string-match "%" s)
  8247. (setq s (mapconcat (lambda (c)
  8248. (if (eq c ?%)
  8249. "%25"
  8250. (char-to-string c)))
  8251. s "")))
  8252. (while (string-match "<" s)
  8253. (setq s (replace-match "%3C" t t s)))
  8254. (while (string-match ">" s)
  8255. (setq s (replace-match "%3E" t t s)))
  8256. (while (string-match "@" s)
  8257. (setq s (replace-match "%40" t t s)))
  8258. s)
  8259. (defun org-link-prettify (link)
  8260. "Return a human-readable representation of LINK.
  8261. The car of LINK must be a raw link the cdr of LINK must be either
  8262. a link description or nil."
  8263. (let ((desc (or (cadr link) "<no description>")))
  8264. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8265. "<" (car link) ">")))
  8266. ;;;###autoload
  8267. (defun org-insert-link-global ()
  8268. "Insert a link like Org-mode does.
  8269. This command can be called in any mode to insert a link in Org-mode syntax."
  8270. (interactive)
  8271. (org-load-modules-maybe)
  8272. (org-run-like-in-org-mode 'org-insert-link))
  8273. (defun org-insert-all-links (&optional keep)
  8274. "Insert all links in `org-stored-links'."
  8275. (interactive "P")
  8276. (let ((links (copy-sequence org-stored-links)) l)
  8277. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8278. (insert "- ")
  8279. (org-insert-link nil (car l) (cadr l))
  8280. (insert "\n"))))
  8281. (defun org-link-fontify-links-to-this-file ()
  8282. "Fontify links to the current file in `org-stored-links'."
  8283. (let ((f (buffer-file-name)) a b)
  8284. (setq a (mapcar (lambda(l)
  8285. (let ((ll (car l)))
  8286. (when (and (string-match "^file:\\(.+\\)::" ll)
  8287. (equal f (expand-file-name (match-string 1 ll))))
  8288. ll)))
  8289. org-stored-links))
  8290. (when (featurep 'org-id)
  8291. (setq b (mapcar (lambda(l)
  8292. (let ((ll (car l)))
  8293. (when (and (string-match "^id:\\(.+\\)$" ll)
  8294. (equal f (expand-file-name
  8295. (or (org-id-find-id-file
  8296. (match-string 1 ll)) ""))))
  8297. ll)))
  8298. org-stored-links)))
  8299. (mapcar (lambda(l)
  8300. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8301. (delq nil (append a b)))))
  8302. (defvar org-link-links-in-this-file nil)
  8303. (defun org-insert-link (&optional complete-file link-location default-description)
  8304. "Insert a link. At the prompt, enter the link.
  8305. Completion can be used to insert any of the link protocol prefixes like
  8306. http or ftp in use.
  8307. The history can be used to select a link previously stored with
  8308. `org-store-link'. When the empty string is entered (i.e. if you just
  8309. press RET at the prompt), the link defaults to the most recently
  8310. stored link. As SPC triggers completion in the minibuffer, you need to
  8311. use M-SPC or C-q SPC to force the insertion of a space character.
  8312. You will also be prompted for a description, and if one is given, it will
  8313. be displayed in the buffer instead of the link.
  8314. If there is already a link at point, this command will allow you to edit link
  8315. and description parts.
  8316. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8317. be selected using completion. The path to the file will be relative to the
  8318. current directory if the file is in the current directory or a subdirectory.
  8319. Otherwise, the link will be the absolute path as completed in the minibuffer
  8320. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8321. option `org-link-file-path-type'.
  8322. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8323. the current directory or below.
  8324. With three \\[universal-argument] prefixes, negate the meaning of
  8325. `org-keep-stored-link-after-insertion'.
  8326. If `org-make-link-description-function' is non-nil, this function will be
  8327. called with the link target, and the result will be the default
  8328. link description.
  8329. If the LINK-LOCATION parameter is non-nil, this value will be
  8330. used as the link location instead of reading one interactively.
  8331. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8332. be used as the default description."
  8333. (interactive "P")
  8334. (let* ((wcf (current-window-configuration))
  8335. (origbuf (current-buffer))
  8336. (region (if (org-region-active-p)
  8337. (buffer-substring (region-beginning) (region-end))))
  8338. (remove (and region (list (region-beginning) (region-end))))
  8339. (desc region)
  8340. tmphist ; byte-compile incorrectly complains about this
  8341. (link link-location)
  8342. (abbrevs org-link-abbrev-alist-local)
  8343. entry file all-prefixes auto-desc)
  8344. (cond
  8345. (link-location) ; specified by arg, just use it.
  8346. ((org-in-regexp org-bracket-link-regexp 1)
  8347. ;; We do have a link at point, and we are going to edit it.
  8348. (setq remove (list (match-beginning 0) (match-end 0)))
  8349. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8350. (setq link (read-string "Link: "
  8351. (org-link-unescape
  8352. (org-match-string-no-properties 1)))))
  8353. ((or (org-in-regexp org-angle-link-re)
  8354. (org-in-regexp org-plain-link-re))
  8355. ;; Convert to bracket link
  8356. (setq remove (list (match-beginning 0) (match-end 0))
  8357. link (read-string "Link: "
  8358. (org-remove-angle-brackets (match-string 0)))))
  8359. ((member complete-file '((4) (16)))
  8360. ;; Completing read for file names.
  8361. (setq link (org-file-complete-link complete-file)))
  8362. (t
  8363. ;; Read link, with completion for stored links.
  8364. (org-link-fontify-links-to-this-file)
  8365. (org-switch-to-buffer-other-window "*Org Links*")
  8366. (with-current-buffer "*Org Links*"
  8367. (erase-buffer)
  8368. (insert "Insert a link.
  8369. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8370. (when org-stored-links
  8371. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8372. (insert (mapconcat 'org-link-prettify
  8373. (reverse org-stored-links) "\n")))
  8374. (goto-char (point-min)))
  8375. (let ((cw (selected-window)))
  8376. (select-window (get-buffer-window "*Org Links*" 'visible))
  8377. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8378. (unless (pos-visible-in-window-p (point-max))
  8379. (org-fit-window-to-buffer))
  8380. (and (window-live-p cw) (select-window cw)))
  8381. ;; Fake a link history, containing the stored links.
  8382. (setq tmphist (append (mapcar 'car org-stored-links)
  8383. org-insert-link-history))
  8384. (setq all-prefixes (append (mapcar 'car abbrevs)
  8385. (mapcar 'car org-link-abbrev-alist)
  8386. org-link-types))
  8387. (unwind-protect
  8388. (progn
  8389. (setq link
  8390. (org-completing-read
  8391. "Link: "
  8392. (append
  8393. (mapcar (lambda (x) (concat x ":"))
  8394. all-prefixes)
  8395. (mapcar 'car org-stored-links))
  8396. nil nil nil
  8397. 'tmphist
  8398. (caar org-stored-links)))
  8399. (if (not (string-match "\\S-" link))
  8400. (error "No link selected"))
  8401. (mapc (lambda(l)
  8402. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8403. org-stored-links)
  8404. (if (or (member link all-prefixes)
  8405. (and (equal ":" (substring link -1))
  8406. (member (substring link 0 -1) all-prefixes)
  8407. (setq link (substring link 0 -1))))
  8408. (setq link (with-current-buffer origbuf
  8409. (org-link-try-special-completion link)))))
  8410. (set-window-configuration wcf)
  8411. (kill-buffer "*Org Links*"))
  8412. (setq entry (assoc link org-stored-links))
  8413. (or entry (push link org-insert-link-history))
  8414. (setq desc (or desc (nth 1 entry)))))
  8415. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8416. (not org-keep-stored-link-after-insertion))
  8417. (setq org-stored-links (delq (assoc link org-stored-links)
  8418. org-stored-links)))
  8419. (if (string-match org-plain-link-re link)
  8420. ;; URL-like link, normalize the use of angular brackets.
  8421. (setq link (org-remove-angle-brackets link)))
  8422. ;; Check if we are linking to the current file with a search option
  8423. ;; If yes, simplify the link by using only the search option.
  8424. (when (and buffer-file-name
  8425. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8426. (let* ((path (match-string 1 link))
  8427. (case-fold-search nil)
  8428. (search (match-string 2 link)))
  8429. (save-match-data
  8430. (if (equal (file-truename buffer-file-name) (file-truename path))
  8431. ;; We are linking to this same file, with a search option
  8432. (setq link search)))))
  8433. ;; Check if we can/should use a relative path. If yes, simplify the link
  8434. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8435. (let* ((type (match-string 1 link))
  8436. (path (match-string 2 link))
  8437. (origpath path)
  8438. (case-fold-search nil))
  8439. (cond
  8440. ((or (eq org-link-file-path-type 'absolute)
  8441. (equal complete-file '(16)))
  8442. (setq path (abbreviate-file-name (expand-file-name path))))
  8443. ((eq org-link-file-path-type 'noabbrev)
  8444. (setq path (expand-file-name path)))
  8445. ((eq org-link-file-path-type 'relative)
  8446. (setq path (file-relative-name path)))
  8447. (t
  8448. (save-match-data
  8449. (if (string-match (concat "^" (regexp-quote
  8450. (expand-file-name
  8451. (file-name-as-directory
  8452. default-directory))))
  8453. (expand-file-name path))
  8454. ;; We are linking a file with relative path name.
  8455. (setq path (substring (expand-file-name path)
  8456. (match-end 0)))
  8457. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8458. (setq link (concat type path))
  8459. (if (equal desc origpath)
  8460. (setq desc path))))
  8461. (if org-make-link-description-function
  8462. (setq desc
  8463. (or (condition-case nil
  8464. (funcall org-make-link-description-function link desc)
  8465. (error (progn (message "Can't get link description from `%s'"
  8466. (symbol-name org-make-link-description-function))
  8467. (sit-for 2) nil)))
  8468. (read-string "Description: " default-description)))
  8469. (if default-description (setq desc default-description)
  8470. (setq desc (or (and auto-desc desc)
  8471. (read-string "Description: " desc)))))
  8472. (unless (string-match "\\S-" desc) (setq desc nil))
  8473. (if remove (apply 'delete-region remove))
  8474. (insert (org-make-link-string link desc))))
  8475. (defun org-link-try-special-completion (type)
  8476. "If there is completion support for link type TYPE, offer it."
  8477. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8478. (if (functionp fun)
  8479. (funcall fun)
  8480. (read-string "Link (no completion support): " (concat type ":")))))
  8481. (defun org-file-complete-link (&optional arg)
  8482. "Create a file link using completion."
  8483. (let (file link)
  8484. (setq file (org-iread-file-name "File: "))
  8485. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8486. (pwd1 (file-name-as-directory (abbreviate-file-name
  8487. (expand-file-name ".")))))
  8488. (cond
  8489. ((equal arg '(16))
  8490. (setq link (concat
  8491. "file:"
  8492. (abbreviate-file-name (expand-file-name file)))))
  8493. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8494. (setq link (concat "file:" (match-string 1 file))))
  8495. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8496. (expand-file-name file))
  8497. (setq link (concat
  8498. "file:" (match-string 1 (expand-file-name file)))))
  8499. (t (setq link (concat "file:" file)))))
  8500. link))
  8501. (defun org-iread-file-name (&rest args)
  8502. "Read-file-name using `ido-mode' speedup if available.
  8503. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8504. See `read-file-name' for a description of parameters."
  8505. (org-without-partial-completion
  8506. (if (and org-completion-use-ido
  8507. (fboundp 'ido-read-file-name)
  8508. (boundp 'ido-mode) ido-mode
  8509. (listp (second args)))
  8510. (let ((ido-enter-matching-directory nil))
  8511. (apply 'ido-read-file-name args))
  8512. (apply 'read-file-name args))))
  8513. (defun org-completing-read (&rest args)
  8514. "Completing-read with SPACE being a normal character."
  8515. (let ((enable-recursive-minibuffers t)
  8516. (minibuffer-local-completion-map
  8517. (copy-keymap minibuffer-local-completion-map)))
  8518. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8519. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8520. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8521. (apply 'org-icompleting-read args)))
  8522. (defun org-completing-read-no-i (&rest args)
  8523. (let (org-completion-use-ido org-completion-use-iswitchb)
  8524. (apply 'org-completing-read args)))
  8525. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8526. "Use iswitch as a completing-read replacement to choose from choices.
  8527. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8528. from."
  8529. (let* ((iswitchb-use-virtual-buffers nil)
  8530. (iswitchb-make-buflist-hook
  8531. (lambda ()
  8532. (setq iswitchb-temp-buflist choices))))
  8533. (iswitchb-read-buffer prompt)))
  8534. (defun org-icompleting-read (&rest args)
  8535. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8536. (org-without-partial-completion
  8537. (if (and org-completion-use-ido
  8538. (fboundp 'ido-completing-read)
  8539. (boundp 'ido-mode) ido-mode
  8540. (listp (second args)))
  8541. (let ((ido-enter-matching-directory nil))
  8542. (apply 'ido-completing-read (concat (car args))
  8543. (if (consp (car (nth 1 args)))
  8544. (mapcar 'car (nth 1 args))
  8545. (nth 1 args))
  8546. (cddr args)))
  8547. (if (and org-completion-use-iswitchb
  8548. (boundp 'iswitchb-mode) iswitchb-mode
  8549. (listp (second args)))
  8550. (apply 'org-iswitchb-completing-read (concat (car args))
  8551. (if (consp (car (nth 1 args)))
  8552. (mapcar 'car (nth 1 args))
  8553. (nth 1 args))
  8554. (cddr args))
  8555. (apply 'completing-read args)))))
  8556. (defun org-extract-attributes (s)
  8557. "Extract the attributes cookie from a string and set as text property."
  8558. (let (a attr (start 0) key value)
  8559. (save-match-data
  8560. (when (string-match "{{\\([^}]+\\)}}$" s)
  8561. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8562. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8563. (setq key (match-string 1 a) value (match-string 2 a)
  8564. start (match-end 0)
  8565. attr (plist-put attr (intern key) value))))
  8566. (org-add-props s nil 'org-attr attr))
  8567. s))
  8568. (defun org-extract-attributes-from-string (tag)
  8569. (let (key value attr)
  8570. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8571. (setq key (match-string 1 tag) value (match-string 2 tag)
  8572. tag (replace-match "" t t tag)
  8573. attr (plist-put attr (intern key) value)))
  8574. (cons tag attr)))
  8575. (defun org-attributes-to-string (plist)
  8576. "Format a property list into an HTML attribute list."
  8577. (let ((s "") key value)
  8578. (while plist
  8579. (setq key (pop plist) value (pop plist))
  8580. (and value
  8581. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8582. s))
  8583. ;;; Opening/following a link
  8584. (defvar org-link-search-failed nil)
  8585. (defvar org-open-link-functions nil
  8586. "Hook for functions finding a plain text link.
  8587. These functions must take a single argument, the link content.
  8588. They will be called for links that look like [[link text][description]]
  8589. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8590. like a filename (e.g. \"./blue.png\").
  8591. These functions will be called *before* Org attempts to resolve the
  8592. link by doing text searches in the current buffer - so if you want a
  8593. link \"[[target]]\" to still find \"<<target>>\", your function should
  8594. handle this as a special case.
  8595. When the function does handle the link, it must return a non-nil value.
  8596. If it decides that it is not responsible for this link, it must return
  8597. nil to indicate that that Org-mode can continue with other options
  8598. like exact and fuzzy text search.")
  8599. (defun org-next-link ()
  8600. "Move forward to the next link.
  8601. If the link is in hidden text, expose it."
  8602. (interactive)
  8603. (when (and org-link-search-failed (eq this-command last-command))
  8604. (goto-char (point-min))
  8605. (message "Link search wrapped back to beginning of buffer"))
  8606. (setq org-link-search-failed nil)
  8607. (let* ((pos (point))
  8608. (ct (org-context))
  8609. (a (assoc :link ct)))
  8610. (if a (goto-char (nth 2 a)))
  8611. (if (re-search-forward org-any-link-re nil t)
  8612. (progn
  8613. (goto-char (match-beginning 0))
  8614. (if (outline-invisible-p) (org-show-context)))
  8615. (goto-char pos)
  8616. (setq org-link-search-failed t)
  8617. (error "No further link found"))))
  8618. (defun org-previous-link ()
  8619. "Move backward to the previous link.
  8620. If the link is in hidden text, expose it."
  8621. (interactive)
  8622. (when (and org-link-search-failed (eq this-command last-command))
  8623. (goto-char (point-max))
  8624. (message "Link search wrapped back to end of buffer"))
  8625. (setq org-link-search-failed nil)
  8626. (let* ((pos (point))
  8627. (ct (org-context))
  8628. (a (assoc :link ct)))
  8629. (if a (goto-char (nth 1 a)))
  8630. (if (re-search-backward org-any-link-re nil t)
  8631. (progn
  8632. (goto-char (match-beginning 0))
  8633. (if (outline-invisible-p) (org-show-context)))
  8634. (goto-char pos)
  8635. (setq org-link-search-failed t)
  8636. (error "No further link found"))))
  8637. (defun org-translate-link (s)
  8638. "Translate a link string if a translation function has been defined."
  8639. (if (and org-link-translation-function
  8640. (fboundp org-link-translation-function)
  8641. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8642. (progn
  8643. (setq s (funcall org-link-translation-function
  8644. (match-string 1 s) (match-string 2 s)))
  8645. (concat (car s) ":" (cdr s)))
  8646. s))
  8647. (defun org-translate-link-from-planner (type path)
  8648. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8649. This is still an experimental function, your mileage may vary."
  8650. (cond
  8651. ((member type '("http" "https" "news" "ftp"))
  8652. ;; standard Internet links are the same.
  8653. nil)
  8654. ((and (equal type "irc") (string-match "^//" path))
  8655. ;; Planner has two / at the beginning of an irc link, we have 1.
  8656. ;; We should have zero, actually....
  8657. (setq path (substring path 1)))
  8658. ((and (equal type "lisp") (string-match "^/" path))
  8659. ;; Planner has a slash, we do not.
  8660. (setq type "elisp" path (substring path 1)))
  8661. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8662. ;; A typical message link. Planner has the id after the final slash,
  8663. ;; we separate it with a hash mark
  8664. (setq path (concat (match-string 1 path) "#"
  8665. (org-remove-angle-brackets (match-string 2 path)))))
  8666. )
  8667. (cons type path))
  8668. (defun org-find-file-at-mouse (ev)
  8669. "Open file link or URL at mouse."
  8670. (interactive "e")
  8671. (mouse-set-point ev)
  8672. (org-open-at-point 'in-emacs))
  8673. (defun org-open-at-mouse (ev)
  8674. "Open file link or URL at mouse.
  8675. See the docstring of `org-open-file' for details."
  8676. (interactive "e")
  8677. (mouse-set-point ev)
  8678. (if (eq major-mode 'org-agenda-mode)
  8679. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8680. (org-open-at-point))
  8681. (defvar org-window-config-before-follow-link nil
  8682. "The window configuration before following a link.
  8683. This is saved in case the need arises to restore it.")
  8684. (defvar org-open-link-marker (make-marker)
  8685. "Marker pointing to the location where `org-open-at-point; was called.")
  8686. ;;;###autoload
  8687. (defun org-open-at-point-global ()
  8688. "Follow a link like Org-mode does.
  8689. This command can be called in any mode to follow a link that has
  8690. Org-mode syntax."
  8691. (interactive)
  8692. (org-run-like-in-org-mode 'org-open-at-point))
  8693. ;;;###autoload
  8694. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8695. "Open a link in the string S, as if it was in Org-mode."
  8696. (interactive "sLink: \nP")
  8697. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8698. (with-temp-buffer
  8699. (let ((org-inhibit-startup (not reference-buffer)))
  8700. (org-mode)
  8701. (insert s)
  8702. (goto-char (point-min))
  8703. (when reference-buffer
  8704. (setq org-link-abbrev-alist-local
  8705. (with-current-buffer reference-buffer
  8706. org-link-abbrev-alist-local)))
  8707. (org-open-at-point arg reference-buffer)))))
  8708. (defvar org-open-at-point-functions nil
  8709. "Hook that is run when following a link at point.
  8710. Functions in this hook must return t if they identify and follow
  8711. a link at point. If they don't find anything interesting at point,
  8712. they must return nil.")
  8713. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8714. (defun org-open-at-point (&optional arg reference-buffer)
  8715. "Open link at or after point.
  8716. If there is no link at point, this function will search forward up to
  8717. the end of the current line.
  8718. Normally, files will be opened by an appropriate application. If the
  8719. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8720. With a double prefix argument, try to open outside of Emacs, in the
  8721. application the system uses for this file type."
  8722. (interactive "P")
  8723. ;; if in a code block, then open the block's results
  8724. (unless (call-interactively #'org-babel-open-src-block-result)
  8725. (org-load-modules-maybe)
  8726. (move-marker org-open-link-marker (point))
  8727. (setq org-window-config-before-follow-link (current-window-configuration))
  8728. (org-remove-occur-highlights nil nil t)
  8729. (cond
  8730. ((and (org-at-heading-p)
  8731. (not (org-at-timestamp-p t))
  8732. (not (org-in-regexp
  8733. (concat org-plain-link-re "\\|"
  8734. org-bracket-link-regexp "\\|"
  8735. org-angle-link-re "\\|"
  8736. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8737. (not (get-text-property (point) 'org-linked-text)))
  8738. (or (org-offer-links-in-entry arg)
  8739. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8740. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8741. ((and (org-at-timestamp-p t)
  8742. (not (org-in-regexp org-bracket-link-regexp)))
  8743. (org-follow-timestamp-link))
  8744. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8745. (not (org-in-regexp org-bracket-link-regexp)))
  8746. (org-footnote-action))
  8747. (t
  8748. (let (type path link line search (pos (point)))
  8749. (catch 'match
  8750. (save-excursion
  8751. (skip-chars-forward "^]\n\r")
  8752. (when (org-in-regexp org-bracket-link-regexp 1)
  8753. (setq link (org-extract-attributes
  8754. (org-link-unescape (org-match-string-no-properties 1))))
  8755. (while (string-match " *\n *" link)
  8756. (setq link (replace-match " " t t link)))
  8757. (setq link (org-link-expand-abbrev link))
  8758. (cond
  8759. ((or (file-name-absolute-p link)
  8760. (string-match "^\\.\\.?/" link))
  8761. (setq type "file" path link))
  8762. ((string-match org-link-re-with-space3 link)
  8763. (setq type (match-string 1 link) path (match-string 2 link)))
  8764. ((string-match "^help:+\\(.+\\)" link)
  8765. (setq type "help" path (match-string 1 link)))
  8766. (t (setq type "thisfile" path link)))
  8767. (throw 'match t)))
  8768. (when (get-text-property (point) 'org-linked-text)
  8769. (setq type "thisfile"
  8770. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8771. (1+ (point)) (point))
  8772. path (buffer-substring
  8773. (or (previous-single-property-change pos 'org-linked-text)
  8774. (point-min))
  8775. (or (next-single-property-change pos 'org-linked-text)
  8776. (point-max))))
  8777. (throw 'match t))
  8778. (save-excursion
  8779. (when (or (org-in-regexp org-angle-link-re)
  8780. (and (goto-char (car (org-in-regexp org-plain-link-re)))
  8781. (save-match-data (not (looking-back "\\[\\[")))))
  8782. (setq type (match-string 1)
  8783. path (org-link-unescape (match-string 2)))
  8784. (throw 'match t)))
  8785. (save-excursion
  8786. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8787. (setq type "tags"
  8788. path (match-string 1))
  8789. (while (string-match ":" path)
  8790. (setq path (replace-match "+" t t path)))
  8791. (throw 'match t)))
  8792. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8793. (setq type "tree-match"
  8794. path (match-string 1))
  8795. (throw 'match t)))
  8796. (unless path
  8797. (error "No link found"))
  8798. ;; switch back to reference buffer
  8799. ;; needed when if called in a temporary buffer through
  8800. ;; org-open-link-from-string
  8801. (with-current-buffer (or reference-buffer (current-buffer))
  8802. ;; Remove any trailing spaces in path
  8803. (if (string-match " +\\'" path)
  8804. (setq path (replace-match "" t t path)))
  8805. (if (and org-link-translation-function
  8806. (fboundp org-link-translation-function))
  8807. ;; Check if we need to translate the link
  8808. (let ((tmp (funcall org-link-translation-function type path)))
  8809. (setq type (car tmp) path (cdr tmp))))
  8810. (cond
  8811. ((assoc type org-link-protocols)
  8812. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8813. ((equal type "help")
  8814. (let ((f-or-v (intern path)))
  8815. (cond ((fboundp f-or-v)
  8816. (describe-function f-or-v))
  8817. ((boundp f-or-v)
  8818. (describe-variable f-or-v))
  8819. (t (error "Not a known function or variable")))))
  8820. ((equal type "mailto")
  8821. (let ((cmd (car org-link-mailto-program))
  8822. (args (cdr org-link-mailto-program)) args1
  8823. (address path) (subject "") a)
  8824. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8825. (setq address (match-string 1 path)
  8826. subject (org-link-escape (match-string 2 path))))
  8827. (while args
  8828. (cond
  8829. ((not (stringp (car args))) (push (pop args) args1))
  8830. (t (setq a (pop args))
  8831. (if (string-match "%a" a)
  8832. (setq a (replace-match address t t a)))
  8833. (if (string-match "%s" a)
  8834. (setq a (replace-match subject t t a)))
  8835. (push a args1))))
  8836. (apply cmd (nreverse args1))))
  8837. ((member type '("http" "https" "ftp" "news"))
  8838. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8839. (org-link-escape
  8840. path org-link-escape-chars-browser)
  8841. path))))
  8842. ((string= type "doi")
  8843. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8844. (org-link-escape
  8845. path org-link-escape-chars-browser)
  8846. path))))
  8847. ((member type '("message"))
  8848. (browse-url (concat type ":" path)))
  8849. ((string= type "tags")
  8850. (org-tags-view arg path))
  8851. ((string= type "tree-match")
  8852. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8853. ((string= type "file")
  8854. (if (string-match "::\\([0-9]+\\)\\'" path)
  8855. (setq line (string-to-number (match-string 1 path))
  8856. path (substring path 0 (match-beginning 0)))
  8857. (if (string-match "::\\(.+\\)\\'" path)
  8858. (setq search (match-string 1 path)
  8859. path (substring path 0 (match-beginning 0)))))
  8860. (if (string-match "[*?{]" (file-name-nondirectory path))
  8861. (dired path)
  8862. (org-open-file path arg line search)))
  8863. ((string= type "shell")
  8864. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8865. (cmd path))
  8866. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8867. (string-match org-confirm-shell-link-not-regexp cmd))
  8868. (not org-confirm-shell-link-function)
  8869. (funcall org-confirm-shell-link-function
  8870. (format "Execute \"%s\" in shell? "
  8871. (org-add-props cmd nil
  8872. 'face 'org-warning))))
  8873. (progn
  8874. (message "Executing %s" cmd)
  8875. (shell-command cmd buf)
  8876. (if (featurep 'midnight)
  8877. (setq clean-buffer-list-kill-buffer-names
  8878. (cons buf clean-buffer-list-kill-buffer-names))))
  8879. (error "Abort"))))
  8880. ((string= type "elisp")
  8881. (let ((cmd path))
  8882. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8883. (string-match org-confirm-elisp-link-not-regexp cmd))
  8884. (not org-confirm-elisp-link-function)
  8885. (funcall org-confirm-elisp-link-function
  8886. (format "Execute \"%s\" as elisp? "
  8887. (org-add-props cmd nil
  8888. 'face 'org-warning))))
  8889. (message "%s => %s" cmd
  8890. (if (equal (string-to-char cmd) ?\()
  8891. (eval (read cmd))
  8892. (call-interactively (read cmd))))
  8893. (error "Abort"))))
  8894. ((and (string= type "thisfile")
  8895. (run-hook-with-args-until-success
  8896. 'org-open-link-functions path)))
  8897. ((string= type "thisfile")
  8898. (if arg
  8899. (switch-to-buffer-other-window
  8900. (org-get-buffer-for-internal-link (current-buffer)))
  8901. (org-mark-ring-push))
  8902. (let ((cmd `(org-link-search
  8903. ,path
  8904. ,(cond ((equal arg '(4)) ''occur)
  8905. ((equal arg '(16)) ''org-occur))
  8906. ,pos)))
  8907. (condition-case nil (let ((org-link-search-inhibit-query t))
  8908. (eval cmd))
  8909. (error (progn (widen) (eval cmd))))))
  8910. (t (browse-url-at-point)))))))
  8911. (move-marker org-open-link-marker nil)
  8912. (run-hook-with-args 'org-follow-link-hook)))
  8913. (defun org-offer-links-in-entry (&optional nth zero)
  8914. "Offer links in the current entry and follow the selected link.
  8915. If there is only one link, follow it immediately as well.
  8916. If NTH is an integer, immediately pick the NTH link found.
  8917. If ZERO is a string, check also this string for a link, and if
  8918. there is one, offer it as link number zero."
  8919. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8920. "\\(" org-angle-link-re "\\)\\|"
  8921. "\\(" org-plain-link-re "\\)"))
  8922. (cnt ?0)
  8923. (in-emacs (if (integerp nth) nil nth))
  8924. have-zero end links link c)
  8925. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8926. (push (match-string 0 zero) links)
  8927. (setq cnt (1- cnt) have-zero t))
  8928. (save-excursion
  8929. (org-back-to-heading t)
  8930. (setq end (save-excursion (outline-next-heading) (point)))
  8931. (while (re-search-forward re end t)
  8932. (push (match-string 0) links))
  8933. (setq links (org-uniquify (reverse links))))
  8934. (cond
  8935. ((null links)
  8936. (message "No links"))
  8937. ((equal (length links) 1)
  8938. (setq link (list (car links))))
  8939. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8940. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8941. (t ; we have to select a link
  8942. (save-excursion
  8943. (save-window-excursion
  8944. (delete-other-windows)
  8945. (with-output-to-temp-buffer "*Select Link*"
  8946. (mapc (lambda (l)
  8947. (if (not (string-match org-bracket-link-regexp l))
  8948. (princ (format "[%c] %s\n" (incf cnt)
  8949. (org-remove-angle-brackets l)))
  8950. (if (match-end 3)
  8951. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8952. (match-string 3 l) (match-string 1 l)))
  8953. (princ (format "[%c] %s\n" (incf cnt)
  8954. (match-string 1 l))))))
  8955. links))
  8956. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8957. (message "Select link to open, RET to open all:")
  8958. (setq c (read-char-exclusive))
  8959. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8960. (when (equal c ?q) (error "Abort"))
  8961. (if (equal c ?\C-m)
  8962. (setq link links)
  8963. (setq nth (- c ?0))
  8964. (if have-zero (setq nth (1+ nth)))
  8965. (unless (and (integerp nth) (>= (length links) nth))
  8966. (error "Invalid link selection"))
  8967. (setq link (list (nth (1- nth) links))))))
  8968. (if link
  8969. (let ((buf (current-buffer)))
  8970. (dolist (l link)
  8971. (org-open-link-from-string l in-emacs buf))
  8972. t)
  8973. nil)))
  8974. ;; Add special file links that specify the way of opening
  8975. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8976. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8977. (defun org-open-file-with-system (path)
  8978. "Open file at PATH using the system way of opening it."
  8979. (org-open-file path 'system))
  8980. (defun org-open-file-with-emacs (path)
  8981. "Open file at PATH in Emacs."
  8982. (org-open-file path 'emacs))
  8983. (defun org-remove-file-link-modifiers ()
  8984. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8985. (goto-char (point-min))
  8986. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8987. (org-if-unprotected
  8988. (replace-match "file:" t t))))
  8989. (eval-after-load "org-exp"
  8990. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8991. 'org-remove-file-link-modifiers))
  8992. ;;;; Time estimates
  8993. (defun org-get-effort (&optional pom)
  8994. "Get the effort estimate for the current entry."
  8995. (org-entry-get pom org-effort-property))
  8996. ;;; File search
  8997. (defvar org-create-file-search-functions nil
  8998. "List of functions to construct the right search string for a file link.
  8999. These functions are called in turn with point at the location to
  9000. which the link should point.
  9001. A function in the hook should first test if it would like to
  9002. handle this file type, for example by checking the `major-mode'
  9003. or the file extension. If it decides not to handle this file, it
  9004. should just return nil to give other functions a chance. If it
  9005. does handle the file, it must return the search string to be used
  9006. when following the link. The search string will be part of the
  9007. file link, given after a double colon, and `org-open-at-point'
  9008. will automatically search for it. If special measures must be
  9009. taken to make the search successful, another function should be
  9010. added to the companion hook `org-execute-file-search-functions',
  9011. which see.
  9012. A function in this hook may also use `setq' to set the variable
  9013. `description' to provide a suggestion for the descriptive text to
  9014. be used for this link when it gets inserted into an Org-mode
  9015. buffer with \\[org-insert-link].")
  9016. (defvar org-execute-file-search-functions nil
  9017. "List of functions to execute a file search triggered by a link.
  9018. Functions added to this hook must accept a single argument, the
  9019. search string that was part of the file link, the part after the
  9020. double colon. The function must first check if it would like to
  9021. handle this search, for example by checking the `major-mode' or
  9022. the file extension. If it decides not to handle this search, it
  9023. should just return nil to give other functions a chance. If it
  9024. does handle the search, it must return a non-nil value to keep
  9025. other functions from trying.
  9026. Each function can access the current prefix argument through the
  9027. variable `current-prefix-argument'. Note that a single prefix is
  9028. used to force opening a link in Emacs, so it may be good to only
  9029. use a numeric or double prefix to guide the search function.
  9030. In case this is needed, a function in this hook can also restore
  9031. the window configuration before `org-open-at-point' was called using:
  9032. (set-window-configuration org-window-config-before-follow-link)")
  9033. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9034. (defun org-link-search (s &optional type avoid-pos stealth)
  9035. "Search for a link search option.
  9036. If S is surrounded by forward slashes, it is interpreted as a
  9037. regular expression. In org-mode files, this will create an `org-occur'
  9038. sparse tree. In ordinary files, `occur' will be used to list matches.
  9039. If the current buffer is in `dired-mode', grep will be used to search
  9040. in all files. If AVOID-POS is given, ignore matches near that position.
  9041. When optional argument STEALTH is non-nil, do not modify
  9042. visibility around point, thus ignoring
  9043. `org-show-hierarchy-above', `org-show-following-heading' and
  9044. `org-show-siblings' variables."
  9045. (let ((case-fold-search t)
  9046. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9047. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9048. (append '(("") (" ") ("\t") ("\n"))
  9049. org-emphasis-alist)
  9050. "\\|") "\\)"))
  9051. (pos (point))
  9052. (pre nil) (post nil)
  9053. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9054. (cond
  9055. ;; First check if there are any special search functions
  9056. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9057. ;; Now try the builtin stuff
  9058. ((and (equal (string-to-char s0) ?#)
  9059. (> (length s0) 1)
  9060. (save-excursion
  9061. (goto-char (point-min))
  9062. (and
  9063. (re-search-forward
  9064. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9065. (setq type 'dedicated
  9066. pos (match-beginning 0))))
  9067. ;; There is an exact target for this
  9068. (goto-char pos)
  9069. (org-back-to-heading t)))
  9070. ((save-excursion
  9071. (goto-char (point-min))
  9072. (and
  9073. (re-search-forward
  9074. (concat "<<" (regexp-quote s0) ">>") nil t)
  9075. (setq type 'dedicated
  9076. pos (match-beginning 0))))
  9077. ;; There is an exact target for this
  9078. (goto-char pos))
  9079. ((save-excursion
  9080. (goto-char (point-min))
  9081. (and
  9082. (re-search-forward
  9083. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9084. (setq type 'dedicated pos (match-beginning 0))))
  9085. ;; Found an invisible target.
  9086. (goto-char pos))
  9087. ((save-excursion
  9088. (goto-char (point-min))
  9089. (and
  9090. (re-search-forward
  9091. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9092. (setq type 'dedicated pos (match-beginning 0))))
  9093. ;; Found an element with a matching #+name affiliated keyword.
  9094. (goto-char pos))
  9095. ((and (string-match "^(\\(.*\\))$" s0)
  9096. (save-excursion
  9097. (goto-char (point-min))
  9098. (and
  9099. (re-search-forward
  9100. (concat "[^[]" (regexp-quote
  9101. (format org-coderef-label-format
  9102. (match-string 1 s0))))
  9103. nil t)
  9104. (setq type 'dedicated
  9105. pos (1+ (match-beginning 0))))))
  9106. ;; There is a coderef target for this
  9107. (goto-char pos))
  9108. ((string-match "^/\\(.*\\)/$" s)
  9109. ;; A regular expression
  9110. (cond
  9111. ((derived-mode-p 'org-mode)
  9112. (org-occur (match-string 1 s)))
  9113. ;;((eq major-mode 'dired-mode)
  9114. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9115. (t (org-do-occur (match-string 1 s)))))
  9116. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9117. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9118. (goto-char (point-min))
  9119. (cond
  9120. ((let (case-fold-search)
  9121. (re-search-forward (format org-complex-heading-regexp-format
  9122. (regexp-quote s))
  9123. nil t))
  9124. ;; OK, found a match
  9125. (setq type 'dedicated)
  9126. (goto-char (match-beginning 0)))
  9127. ((and (not org-link-search-inhibit-query)
  9128. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9129. (y-or-n-p "No match - create this as a new heading? "))
  9130. (goto-char (point-max))
  9131. (or (bolp) (newline))
  9132. (insert "* " s "\n")
  9133. (beginning-of-line 0))
  9134. (t
  9135. (goto-char pos)
  9136. (error "No match"))))
  9137. (t
  9138. ;; A normal search string
  9139. (when (equal (string-to-char s) ?*)
  9140. ;; Anchor on headlines, post may include tags.
  9141. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9142. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9143. s (substring s 1)))
  9144. (remove-text-properties
  9145. 0 (length s)
  9146. '(face nil mouse-face nil keymap nil fontified nil) s)
  9147. ;; Make a series of regular expressions to find a match
  9148. (setq words (org-split-string s "[ \n\r\t]+")
  9149. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9150. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9151. "\\)" markers)
  9152. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9153. re2a (concat "[ \t\r\n]" re2a_)
  9154. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9155. re4 (concat "[^a-zA-Z_]" re4_)
  9156. re1 (concat pre re2 post)
  9157. re3 (concat pre (if pre re4_ re4) post)
  9158. re5 (concat pre ".*" re4)
  9159. re2 (concat pre re2)
  9160. re2a (concat pre (if pre re2a_ re2a))
  9161. re4 (concat pre (if pre re4_ re4))
  9162. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9163. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9164. re5 "\\)"
  9165. ))
  9166. (cond
  9167. ((eq type 'org-occur) (org-occur reall))
  9168. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9169. (t (goto-char (point-min))
  9170. (setq type 'fuzzy)
  9171. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9172. (org-search-not-self 1 re1 nil t)
  9173. (org-search-not-self 1 re2 nil t)
  9174. (org-search-not-self 1 re2a nil t)
  9175. (org-search-not-self 1 re3 nil t)
  9176. (org-search-not-self 1 re4 nil t)
  9177. (org-search-not-self 1 re5 nil t)
  9178. )
  9179. (goto-char (match-beginning 1))
  9180. (goto-char pos)
  9181. (error "No match"))))))
  9182. (and (derived-mode-p 'org-mode)
  9183. (not stealth)
  9184. (org-show-context 'link-search))
  9185. type))
  9186. (defun org-search-not-self (group &rest args)
  9187. "Execute `re-search-forward', but only accept matches that do not
  9188. enclose the position of `org-open-link-marker'."
  9189. (let ((m org-open-link-marker))
  9190. (catch 'exit
  9191. (while (apply 're-search-forward args)
  9192. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9193. (goto-char (match-end group))
  9194. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9195. (> (match-beginning 0) (marker-position m))
  9196. (< (match-end 0) (marker-position m)))
  9197. (save-match-data
  9198. (or (not (org-in-regexp
  9199. org-bracket-link-analytic-regexp 1))
  9200. (not (match-end 4)) ; no description
  9201. (and (<= (match-beginning 4) (point))
  9202. (>= (match-end 4) (point))))))
  9203. (throw 'exit (point))))))))
  9204. (defun org-get-buffer-for-internal-link (buffer)
  9205. "Return a buffer to be used for displaying the link target of internal links."
  9206. (cond
  9207. ((not org-display-internal-link-with-indirect-buffer)
  9208. buffer)
  9209. ((string-match "(Clone)$" (buffer-name buffer))
  9210. (message "Buffer is already a clone, not making another one")
  9211. ;; we also do not modify visibility in this case
  9212. buffer)
  9213. (t ; make a new indirect buffer for displaying the link
  9214. (let* ((bn (buffer-name buffer))
  9215. (ibn (concat bn "(Clone)"))
  9216. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9217. (with-current-buffer ib (org-overview))
  9218. ib))))
  9219. (defun org-do-occur (regexp &optional cleanup)
  9220. "Call the Emacs command `occur'.
  9221. If CLEANUP is non-nil, remove the printout of the regular expression
  9222. in the *Occur* buffer. This is useful if the regex is long and not useful
  9223. to read."
  9224. (occur regexp)
  9225. (when cleanup
  9226. (let ((cwin (selected-window)) win beg end)
  9227. (when (setq win (get-buffer-window "*Occur*"))
  9228. (select-window win))
  9229. (goto-char (point-min))
  9230. (when (re-search-forward "match[a-z]+" nil t)
  9231. (setq beg (match-end 0))
  9232. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9233. (setq end (1- (match-beginning 0)))))
  9234. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9235. (goto-char (point-min))
  9236. (select-window cwin))))
  9237. ;;; The mark ring for links jumps
  9238. (defvar org-mark-ring nil
  9239. "Mark ring for positions before jumps in Org-mode.")
  9240. (defvar org-mark-ring-last-goto nil
  9241. "Last position in the mark ring used to go back.")
  9242. ;; Fill and close the ring
  9243. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9244. (loop for i from 1 to org-mark-ring-length do
  9245. (push (make-marker) org-mark-ring))
  9246. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9247. org-mark-ring)
  9248. (defun org-mark-ring-push (&optional pos buffer)
  9249. "Put the current position or POS into the mark ring and rotate it."
  9250. (interactive)
  9251. (setq pos (or pos (point)))
  9252. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9253. (move-marker (car org-mark-ring)
  9254. (or pos (point))
  9255. (or buffer (current-buffer)))
  9256. (message "%s"
  9257. (substitute-command-keys
  9258. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9259. (defun org-mark-ring-goto (&optional n)
  9260. "Jump to the previous position in the mark ring.
  9261. With prefix arg N, jump back that many stored positions. When
  9262. called several times in succession, walk through the entire ring.
  9263. Org-mode commands jumping to a different position in the current file,
  9264. or to another Org-mode file, automatically push the old position
  9265. onto the ring."
  9266. (interactive "p")
  9267. (let (p m)
  9268. (if (eq last-command this-command)
  9269. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9270. (setq p org-mark-ring))
  9271. (setq org-mark-ring-last-goto p)
  9272. (setq m (car p))
  9273. (org-pop-to-buffer-same-window (marker-buffer m))
  9274. (goto-char m)
  9275. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9276. (defun org-remove-angle-brackets (s)
  9277. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9278. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9279. s)
  9280. (defun org-add-angle-brackets (s)
  9281. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9282. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9283. s)
  9284. (defun org-remove-double-quotes (s)
  9285. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9286. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9287. s)
  9288. ;;; Following specific links
  9289. (defun org-follow-timestamp-link ()
  9290. "Open an agenda view for the time-stamp date/range at point."
  9291. (cond
  9292. ((org-at-date-range-p t)
  9293. (let ((org-agenda-start-on-weekday)
  9294. (t1 (match-string 1))
  9295. (t2 (match-string 2)) tt1 tt2)
  9296. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9297. tt2 (time-to-days (org-time-string-to-time t2)))
  9298. (let ((org-agenda-buffer-tmp-name
  9299. (format "*Org Agenda(a:%s)"
  9300. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9301. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9302. ((org-at-timestamp-p t)
  9303. (let ((org-agenda-buffer-tmp-name
  9304. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9305. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9306. (substring (match-string 1) 0 10)))
  9307. 1)))
  9308. (t (error "This should not happen"))))
  9309. ;;; Following file links
  9310. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9311. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9312. (declare-function mailcap-mime-info
  9313. "mailcap" (string &optional request no-decode))
  9314. (defvar org-wait nil)
  9315. (defun org-open-file (path &optional in-emacs line search)
  9316. "Open the file at PATH.
  9317. First, this expands any special file name abbreviations. Then the
  9318. configuration variable `org-file-apps' is checked if it contains an
  9319. entry for this file type, and if yes, the corresponding command is launched.
  9320. If no application is found, Emacs simply visits the file.
  9321. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9322. With a double \\[universal-argument] \\[universal-argument] \
  9323. prefix arg, Org tries to avoid opening in Emacs
  9324. and to use an external application to visit the file.
  9325. Optional LINE specifies a line to go to, optional SEARCH a string
  9326. to search for. If LINE or SEARCH is given, the file will be
  9327. opened in Emacs, unless an entry from org-file-apps that makes
  9328. use of groups in a regexp matches.
  9329. If you want to change the way frames are used when following a
  9330. link, please customize `org-link-frame-setup'.
  9331. If the file does not exist, an error is thrown."
  9332. (let* ((file (if (equal path "")
  9333. buffer-file-name
  9334. (substitute-in-file-name (expand-file-name path))))
  9335. (file-apps (append org-file-apps (org-default-apps)))
  9336. (apps (org-remove-if
  9337. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9338. (apps-dlink (org-remove-if-not
  9339. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9340. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9341. (dirp (if remp nil (file-directory-p file)))
  9342. (file (if (and dirp org-open-directory-means-index-dot-org)
  9343. (concat (file-name-as-directory file) "index.org")
  9344. file))
  9345. (a-m-a-p (assq 'auto-mode apps))
  9346. (dfile (downcase file))
  9347. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9348. (link (cond ((and (eq line nil)
  9349. (eq search nil))
  9350. file)
  9351. (line
  9352. (concat file "::" (number-to-string line)))
  9353. (search
  9354. (concat file "::" search))))
  9355. (dlink (downcase link))
  9356. (old-buffer (current-buffer))
  9357. (old-pos (point))
  9358. (old-mode major-mode)
  9359. ext cmd link-match-data)
  9360. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9361. (setq ext (match-string 1 dfile))
  9362. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9363. (setq ext (match-string 1 dfile))))
  9364. (cond
  9365. ((member in-emacs '((16) system))
  9366. (setq cmd (cdr (assoc 'system apps))))
  9367. (in-emacs (setq cmd 'emacs))
  9368. (t
  9369. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9370. (and dirp (cdr (assoc 'directory apps)))
  9371. ; first, try matching against apps-dlink
  9372. ; if we get a match here, store the match data for later
  9373. (let ((match (assoc-default dlink apps-dlink
  9374. 'string-match)))
  9375. (if match
  9376. (progn (setq link-match-data (match-data))
  9377. match)
  9378. (progn (setq in-emacs (or in-emacs line search))
  9379. nil))) ; if we have no match in apps-dlink,
  9380. ; always open the file in emacs if line or search
  9381. ; is given (for backwards compatibility)
  9382. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9383. 'string-match)
  9384. (cdr (assoc ext apps))
  9385. (cdr (assoc t apps))))))
  9386. (when (eq cmd 'system)
  9387. (setq cmd (cdr (assoc 'system apps))))
  9388. (when (eq cmd 'default)
  9389. (setq cmd (cdr (assoc t apps))))
  9390. (when (eq cmd 'mailcap)
  9391. (require 'mailcap)
  9392. (mailcap-parse-mailcaps)
  9393. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9394. (command (mailcap-mime-info mime-type)))
  9395. (if (stringp command)
  9396. (setq cmd command)
  9397. (setq cmd 'emacs))))
  9398. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9399. (not (file-exists-p file))
  9400. (not org-open-non-existing-files))
  9401. (error "No such file: %s" file))
  9402. (cond
  9403. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9404. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9405. (while (string-match "['\"]%s['\"]" cmd)
  9406. (setq cmd (replace-match "%s" t t cmd)))
  9407. (while (string-match "%s" cmd)
  9408. (setq cmd (replace-match
  9409. (save-match-data
  9410. (shell-quote-argument
  9411. (convert-standard-filename file)))
  9412. t t cmd)))
  9413. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9414. (save-match-data
  9415. (let ((match-index 1)
  9416. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9417. (set-match-data link-match-data)
  9418. (while (<= match-index number-of-groups)
  9419. (let ((regex (concat "%" (number-to-string match-index)))
  9420. (replace-with (match-string match-index dlink)))
  9421. (while (string-match regex cmd)
  9422. (setq cmd (replace-match replace-with t t cmd))))
  9423. (setq match-index (+ match-index 1)))))
  9424. (save-window-excursion
  9425. (message "Running %s...done" cmd)
  9426. (start-process-shell-command cmd nil cmd)
  9427. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9428. ((or (stringp cmd)
  9429. (eq cmd 'emacs))
  9430. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9431. (widen)
  9432. (if line (org-goto-line line)
  9433. (if search (org-link-search search))))
  9434. ((consp cmd)
  9435. (let ((file (convert-standard-filename file)))
  9436. (save-match-data
  9437. (set-match-data link-match-data)
  9438. (eval cmd))))
  9439. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9440. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9441. (or (not (equal old-buffer (current-buffer)))
  9442. (not (equal old-pos (point))))
  9443. (org-mark-ring-push old-pos old-buffer))))
  9444. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9445. "This function returns non-nil if `entry' uses a regular
  9446. expression which should be matched against the whole link by
  9447. org-open-file.
  9448. It assumes that is the case when the entry uses a regular
  9449. expression which has at least one grouping construct and the
  9450. action is either a lisp form or a command string containing
  9451. '%1', i.e. using at least one subexpression match as a
  9452. parameter."
  9453. (let ((selector (car entry))
  9454. (action (cdr entry)))
  9455. (if (stringp selector)
  9456. (and (> (regexp-opt-depth selector) 0)
  9457. (or (and (stringp action)
  9458. (string-match "%[0-9]" action))
  9459. (consp action)))
  9460. nil)))
  9461. (defun org-default-apps ()
  9462. "Return the default applications for this operating system."
  9463. (cond
  9464. ((eq system-type 'darwin)
  9465. org-file-apps-defaults-macosx)
  9466. ((eq system-type 'windows-nt)
  9467. org-file-apps-defaults-windowsnt)
  9468. (t org-file-apps-defaults-gnu)))
  9469. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9470. "Convert extensions to regular expressions in the cars of LIST.
  9471. Also, weed out any non-string entries, because the return value is used
  9472. only for regexp matching.
  9473. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9474. point to the symbol `emacs', indicating that the file should
  9475. be opened in Emacs."
  9476. (append
  9477. (delq nil
  9478. (mapcar (lambda (x)
  9479. (if (not (stringp (car x)))
  9480. nil
  9481. (if (string-match "\\W" (car x))
  9482. x
  9483. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9484. list))
  9485. (if add-auto-mode
  9486. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9487. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9488. (defun org-file-remote-p (file)
  9489. "Test whether FILE specifies a location on a remote system.
  9490. Return non-nil if the location is indeed remote.
  9491. For example, the filename \"/user@host:/foo\" specifies a location
  9492. on the system \"/user@host:\"."
  9493. (cond ((fboundp 'file-remote-p)
  9494. (file-remote-p file))
  9495. ((fboundp 'tramp-handle-file-remote-p)
  9496. (tramp-handle-file-remote-p file))
  9497. ((and (boundp 'ange-ftp-name-format)
  9498. (string-match (car ange-ftp-name-format) file))
  9499. t)))
  9500. ;;;; Refiling
  9501. (defun org-get-org-file ()
  9502. "Read a filename, with default directory `org-directory'."
  9503. (let ((default (or org-default-notes-file remember-data-file)))
  9504. (read-file-name (format "File name [%s]: " default)
  9505. (file-name-as-directory org-directory)
  9506. default)))
  9507. (defun org-notes-order-reversed-p ()
  9508. "Check if the current file should receive notes in reversed order."
  9509. (cond
  9510. ((not org-reverse-note-order) nil)
  9511. ((eq t org-reverse-note-order) t)
  9512. ((not (listp org-reverse-note-order)) nil)
  9513. (t (catch 'exit
  9514. (let ((all org-reverse-note-order)
  9515. entry)
  9516. (while (setq entry (pop all))
  9517. (if (string-match (car entry) buffer-file-name)
  9518. (throw 'exit (cdr entry))))
  9519. nil)))))
  9520. (defvar org-refile-target-table nil
  9521. "The list of refile targets, created by `org-refile'.")
  9522. (defvar org-agenda-new-buffers nil
  9523. "Buffers created to visit agenda files.")
  9524. (defvar org-refile-cache nil
  9525. "Cache for refile targets.")
  9526. (defvar org-refile-markers nil
  9527. "All the markers used for caching refile locations.")
  9528. (defun org-refile-marker (pos)
  9529. "Get a new refile marker, but only if caching is in use."
  9530. (if (not org-refile-use-cache)
  9531. pos
  9532. (let ((m (make-marker)))
  9533. (move-marker m pos)
  9534. (push m org-refile-markers)
  9535. m)))
  9536. (defun org-refile-cache-clear ()
  9537. "Clear the refile cache and disable all the markers."
  9538. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9539. (setq org-refile-markers nil)
  9540. (setq org-refile-cache nil)
  9541. (message "Refile cache has been cleared"))
  9542. (defun org-refile-cache-check-set (set)
  9543. "Check if all the markers in the cache still have live buffers."
  9544. (let (marker)
  9545. (catch 'exit
  9546. (while (and set (setq marker (nth 3 (pop set))))
  9547. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9548. (when (and marker (null (marker-buffer marker)))
  9549. (message "not found") (sit-for 3)
  9550. (throw 'exit nil)))
  9551. t)))
  9552. (defun org-refile-cache-put (set &rest identifiers)
  9553. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9554. (let* ((key (sha1 (prin1-to-string identifiers)))
  9555. (entry (assoc key org-refile-cache)))
  9556. (if entry
  9557. (setcdr entry set)
  9558. (push (cons key set) org-refile-cache))))
  9559. (defun org-refile-cache-get (&rest identifiers)
  9560. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9561. (cond
  9562. ((not org-refile-cache) nil)
  9563. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9564. (t
  9565. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9566. org-refile-cache))))
  9567. (and set (org-refile-cache-check-set set) set)))))
  9568. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9569. "Produce a table with refile targets."
  9570. (let ((case-fold-search nil)
  9571. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9572. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9573. targets tgs txt re files f desc descre fast-path-p level pos0)
  9574. (message "Getting targets...")
  9575. (with-current-buffer (or default-buffer (current-buffer))
  9576. (while (setq entry (pop entries))
  9577. (setq files (car entry) desc (cdr entry))
  9578. (setq fast-path-p nil)
  9579. (cond
  9580. ((null files) (setq files (list (current-buffer))))
  9581. ((eq files 'org-agenda-files)
  9582. (setq files (org-agenda-files 'unrestricted)))
  9583. ((and (symbolp files) (fboundp files))
  9584. (setq files (funcall files)))
  9585. ((and (symbolp files) (boundp files))
  9586. (setq files (symbol-value files))))
  9587. (if (stringp files) (setq files (list files)))
  9588. (cond
  9589. ((eq (car desc) :tag)
  9590. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9591. ((eq (car desc) :todo)
  9592. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9593. ((eq (car desc) :regexp)
  9594. (setq descre (cdr desc)))
  9595. ((eq (car desc) :level)
  9596. (setq descre (concat "^\\*\\{" (number-to-string
  9597. (if org-odd-levels-only
  9598. (1- (* 2 (cdr desc)))
  9599. (cdr desc)))
  9600. "\\}[ \t]")))
  9601. ((eq (car desc) :maxlevel)
  9602. (setq fast-path-p t)
  9603. (setq descre (concat "^\\*\\{1," (number-to-string
  9604. (if org-odd-levels-only
  9605. (1- (* 2 (cdr desc)))
  9606. (cdr desc)))
  9607. "\\}[ \t]")))
  9608. (t (error "Bad refiling target description %s" desc)))
  9609. (while (setq f (pop files))
  9610. (with-current-buffer
  9611. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9612. (or
  9613. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9614. (progn
  9615. (if (bufferp f) (setq f (buffer-file-name
  9616. (buffer-base-buffer f))))
  9617. (setq f (and f (expand-file-name f)))
  9618. (if (eq org-refile-use-outline-path 'file)
  9619. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9620. (save-excursion
  9621. (save-restriction
  9622. (widen)
  9623. (goto-char (point-min))
  9624. (while (re-search-forward descre nil t)
  9625. (goto-char (setq pos0 (point-at-bol)))
  9626. (catch 'next
  9627. (when org-refile-target-verify-function
  9628. (save-match-data
  9629. (or (funcall org-refile-target-verify-function)
  9630. (throw 'next t))))
  9631. (when (and (looking-at org-complex-heading-regexp)
  9632. (not (member (match-string 4) excluded-entries))
  9633. (match-string 4))
  9634. (setq level (org-reduced-level
  9635. (- (match-end 1) (match-beginning 1)))
  9636. txt (org-link-display-format (match-string 4))
  9637. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9638. re (format org-complex-heading-regexp-format
  9639. (regexp-quote (match-string 4))))
  9640. (when org-refile-use-outline-path
  9641. (setq txt (mapconcat
  9642. 'org-protect-slash
  9643. (append
  9644. (if (eq org-refile-use-outline-path
  9645. 'file)
  9646. (list (file-name-nondirectory
  9647. (buffer-file-name
  9648. (buffer-base-buffer))))
  9649. (if (eq org-refile-use-outline-path
  9650. 'full-file-path)
  9651. (list (buffer-file-name
  9652. (buffer-base-buffer)))))
  9653. (org-get-outline-path fast-path-p
  9654. level txt)
  9655. (list txt))
  9656. "/")))
  9657. (push (list txt f re (org-refile-marker (point)))
  9658. tgs)))
  9659. (when (= (point) pos0)
  9660. ;; verification function has not moved point
  9661. (goto-char (point-at-eol))))))))
  9662. (when org-refile-use-cache
  9663. (org-refile-cache-put tgs (buffer-file-name) descre))
  9664. (setq targets (append tgs targets))
  9665. ))))
  9666. (message "Getting targets...done")
  9667. (nreverse targets)))
  9668. (defun org-protect-slash (s)
  9669. (while (string-match "/" s)
  9670. (setq s (replace-match "\\" t t s)))
  9671. s)
  9672. (defvar org-olpa (make-vector 20 nil))
  9673. (defun org-get-outline-path (&optional fastp level heading)
  9674. "Return the outline path to the current entry, as a list.
  9675. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9676. routine which makes outline path derivations for an entire file,
  9677. avoiding backtracing. Refile target collection makes use of that."
  9678. (if fastp
  9679. (progn
  9680. (if (> level 19)
  9681. (error "Outline path failure, more than 19 levels"))
  9682. (loop for i from level upto 19 do
  9683. (aset org-olpa i nil))
  9684. (prog1
  9685. (delq nil (append org-olpa nil))
  9686. (aset org-olpa level heading)))
  9687. (let (rtn case-fold-search)
  9688. (save-excursion
  9689. (save-restriction
  9690. (widen)
  9691. (while (org-up-heading-safe)
  9692. (when (looking-at org-complex-heading-regexp)
  9693. (push (org-match-string-no-properties 4) rtn)))
  9694. rtn)))))
  9695. (defun org-format-outline-path (path &optional width prefix separator)
  9696. "Format the outline path PATH for display.
  9697. WIDTH is the maximum number of characters that is available.
  9698. PREFIX is a prefix to be included in the returned string,
  9699. such as the file name.
  9700. SEPARATOR is inserted between the different parts of the path,
  9701. the default is \"/\"."
  9702. (setq width (or width 79))
  9703. (if prefix (setq width (- width (length prefix))))
  9704. (if (not path)
  9705. (or prefix "")
  9706. (let* ((nsteps (length path))
  9707. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9708. (maxwidth (if (<= total-width width)
  9709. 10000 ;; everything fits
  9710. ;; we need to shorten the level headings
  9711. (/ (- width nsteps) nsteps)))
  9712. (org-odd-levels-only nil)
  9713. (n 0)
  9714. (total (1+ (length prefix))))
  9715. (setq maxwidth (max maxwidth 10))
  9716. (concat prefix
  9717. (if prefix (or separator "/"))
  9718. (mapconcat
  9719. (lambda (h)
  9720. (setq n (1+ n))
  9721. (if (and (= n nsteps) (< maxwidth 10000))
  9722. (setq maxwidth (- total-width total)))
  9723. (if (< (length h) maxwidth)
  9724. (progn (setq total (+ total (length h) 1)) h)
  9725. (setq h (substring h 0 (- maxwidth 2))
  9726. total (+ total maxwidth 1))
  9727. (if (string-match "[ \t]+\\'" h)
  9728. (setq h (substring h 0 (match-beginning 0))))
  9729. (setq h (concat h "..")))
  9730. (org-add-props h nil 'face
  9731. (nth (% (1- n) org-n-level-faces)
  9732. org-level-faces))
  9733. h)
  9734. path (or separator "/"))))))
  9735. (defun org-display-outline-path (&optional file current separator just-return-string)
  9736. "Display the current outline path in the echo area.
  9737. If FILE is non-nil, prepend the output with the file name.
  9738. If CURRENT is non-nil, append the current heading to the output.
  9739. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9740. the different parts of the path and defaults to \"/\".
  9741. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9742. (interactive "P")
  9743. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9744. (case-fold-search nil)
  9745. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9746. res)
  9747. (if current (setq path (append path
  9748. (save-excursion
  9749. (org-back-to-heading t)
  9750. (if (looking-at org-complex-heading-regexp)
  9751. (list (match-string 4)))))))
  9752. (setq res
  9753. (org-format-outline-path
  9754. path
  9755. (1- (frame-width))
  9756. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9757. separator))
  9758. (if just-return-string
  9759. (org-no-properties res)
  9760. (message "%s" res))))
  9761. (defvar org-refile-history nil
  9762. "History for refiling operations.")
  9763. (defvar org-after-refile-insert-hook nil
  9764. "Hook run after `org-refile' has inserted its stuff at the new location.
  9765. Note that this is still *before* the stuff will be removed from
  9766. the *old* location.")
  9767. (defvar org-capture-last-stored-marker)
  9768. (defvar org-refile-keep nil
  9769. "Non-nil means `org-refile' will copy instead of refile.")
  9770. (defun org-copy ()
  9771. "Like `org-refile', but copy."
  9772. (interactive)
  9773. (let ((org-refile-keep t))
  9774. (funcall 'org-refile nil nil nil "Copy")))
  9775. (defun org-refile (&optional goto default-buffer rfloc msg)
  9776. "Move the entry or entries at point to another heading.
  9777. The list of target headings is compiled using the information in
  9778. `org-refile-targets', which see.
  9779. At the target location, the entry is filed as a subitem of the target
  9780. heading. Depending on `org-reverse-note-order', the new subitem will
  9781. either be the first or the last subitem.
  9782. If there is an active region, all entries in that region will be moved.
  9783. However, the region must fulfill the requirement that the first heading
  9784. is the first one sets the top-level of the moved text - at most siblings
  9785. below it are allowed.
  9786. With prefix arg GOTO, the command will only visit the target location
  9787. and not actually move anything.
  9788. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9789. go to the location where the last refiling operation has put the subtree.
  9790. With a prefix argument of `2', refile to the running clock.
  9791. RFLOC can be a refile location obtained in a different way.
  9792. MSG is a string to replace \"Refile\" in the default prompt with
  9793. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9794. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9795. If you are using target caching (see `org-refile-use-cache'),
  9796. you have to clear the target cache in order to find new targets.
  9797. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9798. prefix argument (`C-u C-u C-u C-c C-w')."
  9799. (interactive "P")
  9800. (if (member goto '(0 (64)))
  9801. (org-refile-cache-clear)
  9802. (let* ((actionmsg (or msg "Refile"))
  9803. (cbuf (current-buffer))
  9804. (regionp (org-region-active-p))
  9805. (region-start (and regionp (region-beginning)))
  9806. (region-end (and regionp (region-end)))
  9807. (region-length (and regionp (- region-end region-start)))
  9808. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9809. pos it nbuf file re level reversed)
  9810. (setq last-command nil)
  9811. (when regionp
  9812. (goto-char region-start)
  9813. (or (bolp) (goto-char (point-at-bol)))
  9814. (setq region-start (point))
  9815. (unless (or (org-kill-is-subtree-p
  9816. (buffer-substring region-start region-end))
  9817. (prog1 org-refile-active-region-within-subtree
  9818. (org-toggle-heading)))
  9819. (error "The region is not a (sequence of) subtree(s)")))
  9820. (if (equal goto '(16))
  9821. (org-refile-goto-last-stored)
  9822. (when (or
  9823. (and (equal goto 2)
  9824. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9825. (prog1
  9826. (setq it (list (or org-clock-heading "running clock")
  9827. (buffer-file-name
  9828. (marker-buffer org-clock-hd-marker))
  9829. ""
  9830. (marker-position org-clock-hd-marker)))
  9831. (setq goto nil)))
  9832. (setq it (or rfloc
  9833. (let (heading-text)
  9834. (save-excursion
  9835. (unless goto
  9836. (org-back-to-heading t)
  9837. (setq heading-text
  9838. (nth 4 (org-heading-components))))
  9839. (org-refile-get-location
  9840. (cond (goto "Goto")
  9841. (regionp (concat actionmsg " region to"))
  9842. (t (concat actionmsg " subtree \""
  9843. heading-text "\" to")))
  9844. default-buffer
  9845. (and (not (equal '(4) goto))
  9846. org-refile-allow-creating-parent-nodes)
  9847. goto))))))
  9848. (setq file (nth 1 it)
  9849. re (nth 2 it)
  9850. pos (nth 3 it))
  9851. (if (and (not goto)
  9852. pos
  9853. (equal (buffer-file-name) file)
  9854. (if regionp
  9855. (and (>= pos region-start)
  9856. (<= pos region-end))
  9857. (and (>= pos (point))
  9858. (< pos (save-excursion
  9859. (org-end-of-subtree t t))))))
  9860. (error "Cannot refile to position inside the tree or region"))
  9861. (setq nbuf (or (find-buffer-visiting file)
  9862. (find-file-noselect file)))
  9863. (if goto
  9864. (progn
  9865. (org-pop-to-buffer-same-window nbuf)
  9866. (goto-char pos)
  9867. (org-show-context 'org-goto))
  9868. (if regionp
  9869. (progn
  9870. (org-kill-new (buffer-substring region-start region-end))
  9871. (org-save-markers-in-region region-start region-end))
  9872. (org-copy-subtree 1 nil t))
  9873. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9874. (find-file-noselect file)))
  9875. (setq reversed (org-notes-order-reversed-p))
  9876. (save-excursion
  9877. (save-restriction
  9878. (widen)
  9879. (if pos
  9880. (progn
  9881. (goto-char pos)
  9882. (looking-at org-outline-regexp)
  9883. (setq level (org-get-valid-level (funcall outline-level) 1))
  9884. (goto-char
  9885. (if reversed
  9886. (or (outline-next-heading) (point-max))
  9887. (or (save-excursion (org-get-next-sibling))
  9888. (org-end-of-subtree t t)
  9889. (point-max)))))
  9890. (setq level 1)
  9891. (if (not reversed)
  9892. (goto-char (point-max))
  9893. (goto-char (point-min))
  9894. (or (outline-next-heading) (goto-char (point-max)))))
  9895. (if (not (bolp)) (newline))
  9896. (org-paste-subtree level)
  9897. (when org-log-refile
  9898. (org-add-log-setup 'refile nil nil 'findpos
  9899. org-log-refile)
  9900. (unless (eq org-log-refile 'note)
  9901. (save-excursion (org-add-log-note))))
  9902. (and org-auto-align-tags
  9903. (let ((org-loop-over-headlines-in-active-region nil))
  9904. (org-set-tags nil t)))
  9905. (bookmark-set "org-refile-last-stored")
  9906. ;; If we are refiling for capture, make sure that the
  9907. ;; last-capture pointers point here
  9908. (when (org-bound-and-true-p org-refile-for-capture)
  9909. (bookmark-set "org-capture-last-stored-marker")
  9910. (move-marker org-capture-last-stored-marker (point)))
  9911. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9912. (run-hooks 'org-after-refile-insert-hook))))
  9913. (unless org-refile-keep
  9914. (if regionp
  9915. (delete-region (point) (+ (point) region-length))
  9916. (org-cut-subtree)))
  9917. (when (featurep 'org-inlinetask)
  9918. (org-inlinetask-remove-END-maybe))
  9919. (setq org-markers-to-move nil)
  9920. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  9921. (defun org-refile-goto-last-stored ()
  9922. "Go to the location where the last refile was stored."
  9923. (interactive)
  9924. (bookmark-jump "org-refile-last-stored")
  9925. (message "This is the location of the last refile"))
  9926. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9927. no-exclude)
  9928. "Prompt the user for a refile location, using PROMPT.
  9929. PROMPT should not be suffixed with a colon and a space, because
  9930. this function appends the default value from
  9931. `org-refile-history' automatically, if that is not empty.
  9932. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9933. this is used for the GOTO interface."
  9934. (let ((org-refile-targets org-refile-targets)
  9935. (org-refile-use-outline-path org-refile-use-outline-path)
  9936. excluded-entries)
  9937. (when (and (derived-mode-p 'org-mode)
  9938. (not org-refile-use-cache)
  9939. (not no-exclude))
  9940. (org-map-tree
  9941. (lambda()
  9942. (setq excluded-entries
  9943. (append excluded-entries (list (org-get-heading t t)))))))
  9944. (setq org-refile-target-table
  9945. (org-refile-get-targets default-buffer excluded-entries)))
  9946. (unless org-refile-target-table
  9947. (error "No refile targets"))
  9948. (let* ((cbuf (current-buffer))
  9949. (partial-completion-mode nil)
  9950. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9951. (cfunc (if (and org-refile-use-outline-path
  9952. org-outline-path-complete-in-steps)
  9953. 'org-olpath-completing-read
  9954. 'org-icompleting-read))
  9955. (extra (if org-refile-use-outline-path "/" ""))
  9956. (cbnex (concat (buffer-name) extra))
  9957. (filename (and cfn (expand-file-name cfn)))
  9958. (tbl (mapcar
  9959. (lambda (x)
  9960. (if (and (not (member org-refile-use-outline-path
  9961. '(file full-file-path)))
  9962. (not (equal filename (nth 1 x))))
  9963. (cons (concat (car x) extra " ("
  9964. (file-name-nondirectory (nth 1 x)) ")")
  9965. (cdr x))
  9966. (cons (concat (car x) extra) (cdr x))))
  9967. org-refile-target-table))
  9968. (completion-ignore-case t)
  9969. cdef
  9970. (prompt (concat prompt
  9971. (or (and (car org-refile-history)
  9972. (concat " (default " (car org-refile-history) ")"))
  9973. (and (assoc cbnex tbl) (setq cdef cbnex)
  9974. (concat " (default " cbnex ")"))) ": "))
  9975. pa answ parent-target child parent old-hist)
  9976. (setq old-hist org-refile-history)
  9977. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9978. nil 'org-refile-history (or cdef (car org-refile-history))))
  9979. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9980. (org-refile-check-position pa)
  9981. (if pa
  9982. (progn
  9983. (when (or (not org-refile-history)
  9984. (not (eq old-hist org-refile-history))
  9985. (not (equal (car pa) (car org-refile-history))))
  9986. (setq org-refile-history
  9987. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9988. org-refile-history
  9989. (cdr org-refile-history))))
  9990. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9991. (pop org-refile-history)))
  9992. pa)
  9993. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9994. (progn
  9995. (setq parent (match-string 1 answ)
  9996. child (match-string 2 answ))
  9997. (setq parent-target (or (assoc parent tbl)
  9998. (assoc (concat parent "/") tbl)))
  9999. (when (and parent-target
  10000. (or (eq new-nodes t)
  10001. (and (eq new-nodes 'confirm)
  10002. (y-or-n-p (format "Create new node \"%s\"? "
  10003. child)))))
  10004. (org-refile-new-child parent-target child)))
  10005. (error "Invalid target location")))))
  10006. (declare-function org-string-nw-p "org-macs" (s))
  10007. (defun org-refile-check-position (refile-pointer)
  10008. "Check if the refile pointer matches the readline to which it points."
  10009. (let* ((file (nth 1 refile-pointer))
  10010. (re (nth 2 refile-pointer))
  10011. (pos (nth 3 refile-pointer))
  10012. buffer)
  10013. (when (org-string-nw-p re)
  10014. (setq buffer (if (markerp pos)
  10015. (marker-buffer pos)
  10016. (or (find-buffer-visiting file)
  10017. (find-file-noselect file))))
  10018. (with-current-buffer buffer
  10019. (save-excursion
  10020. (save-restriction
  10021. (widen)
  10022. (goto-char pos)
  10023. (beginning-of-line 1)
  10024. (unless (org-looking-at-p re)
  10025. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  10026. (defun org-refile-new-child (parent-target child)
  10027. "Use refile target PARENT-TARGET to add new CHILD below it."
  10028. (unless parent-target
  10029. (error "Cannot find parent for new node"))
  10030. (let ((file (nth 1 parent-target))
  10031. (pos (nth 3 parent-target))
  10032. level)
  10033. (with-current-buffer (or (find-buffer-visiting file)
  10034. (find-file-noselect file))
  10035. (save-excursion
  10036. (save-restriction
  10037. (widen)
  10038. (if pos
  10039. (goto-char pos)
  10040. (goto-char (point-max))
  10041. (if (not (bolp)) (newline)))
  10042. (when (looking-at org-outline-regexp)
  10043. (setq level (funcall outline-level))
  10044. (org-end-of-subtree t t))
  10045. (org-back-over-empty-lines)
  10046. (insert "\n" (make-string
  10047. (if pos (org-get-valid-level level 1) 1) ?*)
  10048. " " child "\n")
  10049. (beginning-of-line 0)
  10050. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10051. (defun org-olpath-completing-read (prompt collection &rest args)
  10052. "Read an outline path like a file name."
  10053. (let ((thetable collection)
  10054. (org-completion-use-ido nil) ; does not work with ido.
  10055. (org-completion-use-iswitchb nil)) ; or iswitchb
  10056. (apply
  10057. 'org-icompleting-read prompt
  10058. (lambda (string predicate &optional flag)
  10059. (let (rtn r f (l (length string)))
  10060. (cond
  10061. ((eq flag nil)
  10062. ;; try completion
  10063. (try-completion string thetable))
  10064. ((eq flag t)
  10065. ;; all-completions
  10066. (setq rtn (all-completions string thetable predicate))
  10067. (mapcar
  10068. (lambda (x)
  10069. (setq r (substring x l))
  10070. (if (string-match " ([^)]*)$" x)
  10071. (setq f (match-string 0 x))
  10072. (setq f ""))
  10073. (if (string-match "/" r)
  10074. (concat string (substring r 0 (match-end 0)) f)
  10075. x))
  10076. rtn))
  10077. ((eq flag 'lambda)
  10078. ;; exact match?
  10079. (assoc string thetable)))))
  10080. args)))
  10081. ;;;; Dynamic blocks
  10082. (defun org-find-dblock (name)
  10083. "Find the first dynamic block with name NAME in the buffer.
  10084. If not found, stay at current position and return nil."
  10085. (let ((case-fold-search t) pos)
  10086. (save-excursion
  10087. (goto-char (point-min))
  10088. (setq pos (and (re-search-forward
  10089. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10090. (match-beginning 0))))
  10091. (if pos (goto-char pos))
  10092. pos))
  10093. (defconst org-dblock-start-re
  10094. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10095. "Matches the start line of a dynamic block, with parameters.")
  10096. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10097. "Matches the end of a dynamic block.")
  10098. (defun org-create-dblock (plist)
  10099. "Create a dynamic block section, with parameters taken from PLIST.
  10100. PLIST must contain a :name entry which is used as name of the block."
  10101. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10102. (end-of-line 1)
  10103. (newline))
  10104. (let ((col (current-column))
  10105. (name (plist-get plist :name)))
  10106. (insert "#+BEGIN: " name)
  10107. (while plist
  10108. (if (eq (car plist) :name)
  10109. (setq plist (cddr plist))
  10110. (insert " " (prin1-to-string (pop plist)))))
  10111. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10112. (beginning-of-line -2)))
  10113. (defun org-prepare-dblock ()
  10114. "Prepare dynamic block for refresh.
  10115. This empties the block, puts the cursor at the insert position and returns
  10116. the property list including an extra property :name with the block name."
  10117. (unless (looking-at org-dblock-start-re)
  10118. (error "Not at a dynamic block"))
  10119. (let* ((begdel (1+ (match-end 0)))
  10120. (name (org-no-properties (match-string 1)))
  10121. (params (append (list :name name)
  10122. (read (concat "(" (match-string 3) ")")))))
  10123. (save-excursion
  10124. (beginning-of-line 1)
  10125. (skip-chars-forward " \t")
  10126. (setq params (plist-put params :indentation-column (current-column))))
  10127. (unless (re-search-forward org-dblock-end-re nil t)
  10128. (error "Dynamic block not terminated"))
  10129. (setq params
  10130. (append params
  10131. (list :content (buffer-substring
  10132. begdel (match-beginning 0)))))
  10133. (delete-region begdel (match-beginning 0))
  10134. (goto-char begdel)
  10135. (open-line 1)
  10136. params))
  10137. (defun org-map-dblocks (&optional command)
  10138. "Apply COMMAND to all dynamic blocks in the current buffer.
  10139. If COMMAND is not given, use `org-update-dblock'."
  10140. (let ((cmd (or command 'org-update-dblock)))
  10141. (save-excursion
  10142. (goto-char (point-min))
  10143. (while (re-search-forward org-dblock-start-re nil t)
  10144. (goto-char (match-beginning 0))
  10145. (save-excursion
  10146. (condition-case nil
  10147. (funcall cmd)
  10148. (error (message "Error during update of dynamic block"))))
  10149. (unless (re-search-forward org-dblock-end-re nil t)
  10150. (error "Dynamic block not terminated"))))))
  10151. (defun org-dblock-update (&optional arg)
  10152. "User command for updating dynamic blocks.
  10153. Update the dynamic block at point. With prefix ARG, update all dynamic
  10154. blocks in the buffer."
  10155. (interactive "P")
  10156. (if arg
  10157. (org-update-all-dblocks)
  10158. (or (looking-at org-dblock-start-re)
  10159. (org-beginning-of-dblock))
  10160. (org-update-dblock)))
  10161. (defun org-update-dblock ()
  10162. "Update the dynamic block at point.
  10163. This means to empty the block, parse for parameters and then call
  10164. the correct writing function."
  10165. (interactive)
  10166. (save-window-excursion
  10167. (let* ((pos (point))
  10168. (line (org-current-line))
  10169. (params (org-prepare-dblock))
  10170. (name (plist-get params :name))
  10171. (indent (plist-get params :indentation-column))
  10172. (cmd (intern (concat "org-dblock-write:" name))))
  10173. (message "Updating dynamic block `%s' at line %d..." name line)
  10174. (funcall cmd params)
  10175. (message "Updating dynamic block `%s' at line %d...done" name line)
  10176. (goto-char pos)
  10177. (when (and indent (> indent 0))
  10178. (setq indent (make-string indent ?\ ))
  10179. (save-excursion
  10180. (org-beginning-of-dblock)
  10181. (forward-line 1)
  10182. (while (not (looking-at org-dblock-end-re))
  10183. (insert indent)
  10184. (beginning-of-line 2))
  10185. (when (looking-at org-dblock-end-re)
  10186. (and (looking-at "[ \t]+")
  10187. (replace-match ""))
  10188. (insert indent)))))))
  10189. (defun org-beginning-of-dblock ()
  10190. "Find the beginning of the dynamic block at point.
  10191. Error if there is no such block at point."
  10192. (let ((pos (point))
  10193. beg)
  10194. (end-of-line 1)
  10195. (if (and (re-search-backward org-dblock-start-re nil t)
  10196. (setq beg (match-beginning 0))
  10197. (re-search-forward org-dblock-end-re nil t)
  10198. (> (match-end 0) pos))
  10199. (goto-char beg)
  10200. (goto-char pos)
  10201. (error "Not in a dynamic block"))))
  10202. (defun org-update-all-dblocks ()
  10203. "Update all dynamic blocks in the buffer.
  10204. This function can be used in a hook."
  10205. (interactive)
  10206. (when (derived-mode-p 'org-mode)
  10207. (org-map-dblocks 'org-update-dblock)))
  10208. ;;;; Completion
  10209. (defconst org-additional-option-like-keywords
  10210. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10211. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10212. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10213. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10214. "BEGIN:" "END:"
  10215. "ORGTBL" "TBLFM:" "TBLNAME:"
  10216. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10217. "BEGIN_VERBATIM" "END_VERBATIM"
  10218. "BEGIN_QUOTE" "END_QUOTE"
  10219. "BEGIN_VERSE" "END_VERSE"
  10220. "BEGIN_CENTER" "END_CENTER"
  10221. "BEGIN_SRC" "END_SRC"
  10222. "BEGIN_RESULT" "END_RESULT"
  10223. "BEGIN_lstlisting" "END_lstlisting"
  10224. "NAME:" "RESULTS:"
  10225. "HEADER:" "HEADERS:"
  10226. "COLUMNS:" "PROPERTY:"
  10227. "CAPTION:" "LABEL:"
  10228. "SETUPFILE:"
  10229. "INCLUDE:"
  10230. "BIND:"
  10231. "MACRO:"))
  10232. (defconst org-options-keywords
  10233. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10234. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10235. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10236. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10237. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10238. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10239. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10240. (defconst org-additional-option-like-keywords-for-flyspell
  10241. (delete-dups
  10242. (split-string
  10243. (mapconcat (lambda(k)
  10244. (replace-regexp-in-string
  10245. "_\\|:" " "
  10246. (concat k " " (downcase k) " " (upcase k))))
  10247. (append org-options-keywords org-additional-option-like-keywords)
  10248. " ")
  10249. " +" t)))
  10250. (defcustom org-structure-template-alist
  10251. '(
  10252. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10253. "<src lang=\"?\">\n\n</src>")
  10254. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10255. "<example>\n?\n</example>")
  10256. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10257. "<quote>\n?\n</quote>")
  10258. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10259. "<verse>\n?\n</verse>")
  10260. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10261. "<center>\n?\n</center>")
  10262. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10263. "<literal style=\"latex\">\n?\n</literal>")
  10264. ("L" "#+LaTeX: "
  10265. "<literal style=\"latex\">?</literal>")
  10266. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10267. "<literal style=\"html\">\n?\n</literal>")
  10268. ("H" "#+HTML: "
  10269. "<literal style=\"html\">?</literal>")
  10270. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10271. ("A" "#+ASCII: ")
  10272. ("i" "#+INDEX: ?"
  10273. "#+INDEX: ?")
  10274. ("I" "#+INCLUDE: %file ?"
  10275. "<include file=%file markup=\"?\">")
  10276. )
  10277. "Structure completion elements.
  10278. This is a list of abbreviation keys and values. The value gets inserted
  10279. if you type `<' followed by the key and then press the completion key,
  10280. usually `M-TAB'. %file will be replaced by a file name after prompting
  10281. for the file using completion. The cursor will be placed at the position
  10282. of the `?` in the template.
  10283. There are two templates for each key, the first uses the original Org syntax,
  10284. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10285. the default when the /org-mtags.el/ module has been loaded. See also the
  10286. variable `org-mtags-prefer-muse-templates'."
  10287. :group 'org-completion
  10288. :type '(repeat
  10289. (string :tag "Key")
  10290. (string :tag "Template")
  10291. (string :tag "Muse Template")))
  10292. (defun org-try-structure-completion ()
  10293. "Try to complete a structure template before point.
  10294. This looks for strings like \"<e\" on an otherwise empty line and
  10295. expands them."
  10296. (let ((l (buffer-substring (point-at-bol) (point)))
  10297. a)
  10298. (when (and (looking-at "[ \t]*$")
  10299. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10300. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10301. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10302. (match-beginning 1)) a)
  10303. t)))
  10304. (defun org-complete-expand-structure-template (start cell)
  10305. "Expand a structure template."
  10306. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10307. (rpl (nth (if musep 2 1) cell))
  10308. (ind ""))
  10309. (delete-region start (point))
  10310. (when (string-match "\\`#\\+" rpl)
  10311. (cond
  10312. ((bolp))
  10313. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10314. (setq ind (buffer-substring (point-at-bol) (point))))
  10315. (t (newline))))
  10316. (setq start (point))
  10317. (if (string-match "%file" rpl)
  10318. (setq rpl (replace-match
  10319. (concat
  10320. "\""
  10321. (save-match-data
  10322. (abbreviate-file-name (read-file-name "Include file: ")))
  10323. "\"")
  10324. t t rpl)))
  10325. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10326. (concat "\n" ind)))
  10327. (insert rpl)
  10328. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10329. ;;;; TODO, DEADLINE, Comments
  10330. (defun org-toggle-comment ()
  10331. "Change the COMMENT state of an entry."
  10332. (interactive)
  10333. (save-excursion
  10334. (org-back-to-heading)
  10335. (let (case-fold-search)
  10336. (cond
  10337. ((looking-at (format org-heading-keyword-regexp-format
  10338. org-comment-string))
  10339. (goto-char (match-end 1))
  10340. (looking-at (concat " +" org-comment-string))
  10341. (replace-match "" t t)
  10342. (when (eolp) (insert " ")))
  10343. ((looking-at org-outline-regexp)
  10344. (goto-char (match-end 0))
  10345. (insert org-comment-string " "))))))
  10346. (defvar org-last-todo-state-is-todo nil
  10347. "This is non-nil when the last TODO state change led to a TODO state.
  10348. If the last change removed the TODO tag or switched to DONE, then
  10349. this is nil.")
  10350. (defvar org-setting-tags nil) ; dynamically skipped
  10351. (defvar org-todo-setup-filter-hook nil
  10352. "Hook for functions that pre-filter todo specs.
  10353. Each function takes a todo spec and returns either nil or the spec
  10354. transformed into canonical form." )
  10355. (defvar org-todo-get-default-hook nil
  10356. "Hook for functions that get a default item for todo.
  10357. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10358. nil or a string to be used for the todo mark." )
  10359. (defvar org-agenda-headline-snapshot-before-repeat)
  10360. (defun org-current-effective-time ()
  10361. "Return current time adjusted for `org-extend-today-until' variable."
  10362. (let* ((ct (org-current-time))
  10363. (dct (decode-time ct))
  10364. (ct1
  10365. (cond
  10366. (org-use-last-clock-out-time-as-effective-time
  10367. (or (org-clock-get-last-clock-out-time) ct))
  10368. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10369. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10370. (t ct))))
  10371. ct1))
  10372. (defun org-todo-yesterday (&optional arg)
  10373. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10374. (interactive "P")
  10375. (if (eq major-mode 'org-agenda-mode)
  10376. (apply 'org-agenda-todo-yesterday arg)
  10377. (let* ((hour (third (decode-time
  10378. (org-current-time))))
  10379. (org-extend-today-until (1+ hour)))
  10380. (org-todo arg))))
  10381. (defun org-todo (&optional arg)
  10382. "Change the TODO state of an item.
  10383. The state of an item is given by a keyword at the start of the heading,
  10384. like
  10385. *** TODO Write paper
  10386. *** DONE Call mom
  10387. The different keywords are specified in the variable `org-todo-keywords'.
  10388. By default the available states are \"TODO\" and \"DONE\".
  10389. So for this example: when the item starts with TODO, it is changed to DONE.
  10390. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10391. DONE are present, add TODO at the beginning of the heading.
  10392. With \\[universal-argument] prefix arg, use completion to determine the new \
  10393. state.
  10394. With numeric prefix arg, switch to that state.
  10395. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10396. keywords (nextset).
  10397. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10398. With a numeric prefix arg of 0, inhibit note taking for the change.
  10399. For calling through lisp, arg is also interpreted in the following way:
  10400. 'none -> empty state
  10401. \"\"(empty string) -> switch to empty state
  10402. 'done -> switch to DONE
  10403. 'nextset -> switch to the next set of keywords
  10404. 'previousset -> switch to the previous set of keywords
  10405. \"WAITING\" -> switch to the specified keyword, but only if it
  10406. really is a member of `org-todo-keywords'."
  10407. (interactive "P")
  10408. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10409. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10410. 'region-start-level 'region))
  10411. org-loop-over-headlines-in-active-region)
  10412. (org-map-entries
  10413. `(org-todo ,arg)
  10414. org-loop-over-headlines-in-active-region
  10415. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10416. (if (equal arg '(16)) (setq arg 'nextset))
  10417. (let ((org-blocker-hook org-blocker-hook)
  10418. (case-fold-search nil))
  10419. (when (equal arg '(64))
  10420. (setq arg nil org-blocker-hook nil))
  10421. (when (and org-blocker-hook
  10422. (or org-inhibit-blocking
  10423. (org-entry-get nil "NOBLOCKING")))
  10424. (setq org-blocker-hook nil))
  10425. (save-excursion
  10426. (catch 'exit
  10427. (org-back-to-heading t)
  10428. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10429. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10430. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10431. (let* ((match-data (match-data))
  10432. (startpos (point-at-bol))
  10433. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10434. (org-log-done org-log-done)
  10435. (org-log-repeat org-log-repeat)
  10436. (org-todo-log-states org-todo-log-states)
  10437. (org-inhibit-logging
  10438. (if (equal arg 0)
  10439. (progn (setq arg nil) 'note) org-inhibit-logging))
  10440. (this (match-string 1))
  10441. (hl-pos (match-beginning 0))
  10442. (head (org-get-todo-sequence-head this))
  10443. (ass (assoc head org-todo-kwd-alist))
  10444. (interpret (nth 1 ass))
  10445. (done-word (nth 3 ass))
  10446. (final-done-word (nth 4 ass))
  10447. (org-last-state (or this ""))
  10448. (completion-ignore-case t)
  10449. (member (member this org-todo-keywords-1))
  10450. (tail (cdr member))
  10451. (org-state (cond
  10452. ((and org-todo-key-trigger
  10453. (or (and (equal arg '(4))
  10454. (eq org-use-fast-todo-selection 'prefix))
  10455. (and (not arg) org-use-fast-todo-selection
  10456. (not (eq org-use-fast-todo-selection
  10457. 'prefix)))))
  10458. ;; Use fast selection
  10459. (org-fast-todo-selection))
  10460. ((and (equal arg '(4))
  10461. (or (not org-use-fast-todo-selection)
  10462. (not org-todo-key-trigger)))
  10463. ;; Read a state with completion
  10464. (org-icompleting-read
  10465. "State: " (mapcar (lambda(x) (list x))
  10466. org-todo-keywords-1)
  10467. nil t))
  10468. ((eq arg 'right)
  10469. (if this
  10470. (if tail (car tail) nil)
  10471. (car org-todo-keywords-1)))
  10472. ((eq arg 'left)
  10473. (if (equal member org-todo-keywords-1)
  10474. nil
  10475. (if this
  10476. (nth (- (length org-todo-keywords-1)
  10477. (length tail) 2)
  10478. org-todo-keywords-1)
  10479. (org-last org-todo-keywords-1))))
  10480. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10481. (setq arg nil))) ; hack to fall back to cycling
  10482. (arg
  10483. ;; user or caller requests a specific state
  10484. (cond
  10485. ((equal arg "") nil)
  10486. ((eq arg 'none) nil)
  10487. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10488. ((eq arg 'nextset)
  10489. (or (car (cdr (member head org-todo-heads)))
  10490. (car org-todo-heads)))
  10491. ((eq arg 'previousset)
  10492. (let ((org-todo-heads (reverse org-todo-heads)))
  10493. (or (car (cdr (member head org-todo-heads)))
  10494. (car org-todo-heads))))
  10495. ((car (member arg org-todo-keywords-1)))
  10496. ((stringp arg)
  10497. (error "State `%s' not valid in this file" arg))
  10498. ((nth (1- (prefix-numeric-value arg))
  10499. org-todo-keywords-1))))
  10500. ((null member) (or head (car org-todo-keywords-1)))
  10501. ((equal this final-done-word) nil) ;; -> make empty
  10502. ((null tail) nil) ;; -> first entry
  10503. ((memq interpret '(type priority))
  10504. (if (eq this-command last-command)
  10505. (car tail)
  10506. (if (> (length tail) 0)
  10507. (or done-word (car org-done-keywords))
  10508. nil)))
  10509. (t
  10510. (car tail))))
  10511. (org-state (or
  10512. (run-hook-with-args-until-success
  10513. 'org-todo-get-default-hook org-state org-last-state)
  10514. org-state))
  10515. (next (if org-state (concat " " org-state " ") " "))
  10516. (change-plist (list :type 'todo-state-change :from this :to org-state
  10517. :position startpos))
  10518. dolog now-done-p)
  10519. (when org-blocker-hook
  10520. (setq org-last-todo-state-is-todo
  10521. (not (member this org-done-keywords)))
  10522. (unless (save-excursion
  10523. (save-match-data
  10524. (org-with-wide-buffer
  10525. (run-hook-with-args-until-failure
  10526. 'org-blocker-hook change-plist))))
  10527. (if (org-called-interactively-p 'interactive)
  10528. (error "TODO state change from %s to %s blocked" this org-state)
  10529. ;; fail silently
  10530. (message "TODO state change from %s to %s blocked" this org-state)
  10531. (throw 'exit nil))))
  10532. (store-match-data match-data)
  10533. (replace-match next t t)
  10534. (unless (pos-visible-in-window-p hl-pos)
  10535. (message "TODO state changed to %s" (org-trim next)))
  10536. (unless head
  10537. (setq head (org-get-todo-sequence-head org-state)
  10538. ass (assoc head org-todo-kwd-alist)
  10539. interpret (nth 1 ass)
  10540. done-word (nth 3 ass)
  10541. final-done-word (nth 4 ass)))
  10542. (when (memq arg '(nextset previousset))
  10543. (message "Keyword-Set %d/%d: %s"
  10544. (- (length org-todo-sets) -1
  10545. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10546. (length org-todo-sets)
  10547. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10548. (setq org-last-todo-state-is-todo
  10549. (not (member org-state org-done-keywords)))
  10550. (setq now-done-p (and (member org-state org-done-keywords)
  10551. (not (member this org-done-keywords))))
  10552. (and logging (org-local-logging logging))
  10553. (when (and (or org-todo-log-states org-log-done)
  10554. (not (eq org-inhibit-logging t))
  10555. (not (memq arg '(nextset previousset))))
  10556. ;; we need to look at recording a time and note
  10557. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10558. (nth 2 (assoc this org-todo-log-states))))
  10559. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10560. (setq dolog 'time))
  10561. (when (and org-state
  10562. (member org-state org-not-done-keywords)
  10563. (not (member this org-not-done-keywords)))
  10564. ;; This is now a todo state and was not one before
  10565. ;; If there was a CLOSED time stamp, get rid of it.
  10566. (org-add-planning-info nil nil 'closed))
  10567. (when (and now-done-p org-log-done)
  10568. ;; It is now done, and it was not done before
  10569. (org-add-planning-info 'closed (org-current-effective-time))
  10570. (if (and (not dolog) (eq 'note org-log-done))
  10571. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10572. (when (and org-state dolog)
  10573. ;; This is a non-nil state, and we need to log it
  10574. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10575. ;; Fixup tag positioning
  10576. (org-todo-trigger-tag-changes org-state)
  10577. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10578. (when org-provide-todo-statistics
  10579. (org-update-parent-todo-statistics))
  10580. (run-hooks 'org-after-todo-state-change-hook)
  10581. (if (and arg (not (member org-state org-done-keywords)))
  10582. (setq head (org-get-todo-sequence-head org-state)))
  10583. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10584. ;; Do we need to trigger a repeat?
  10585. (when now-done-p
  10586. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10587. ;; This is for the agenda, take a snapshot of the headline.
  10588. (save-match-data
  10589. (setq org-agenda-headline-snapshot-before-repeat
  10590. (org-get-heading))))
  10591. (org-auto-repeat-maybe org-state))
  10592. ;; Fixup cursor location if close to the keyword
  10593. (if (and (outline-on-heading-p)
  10594. (not (bolp))
  10595. (save-excursion (beginning-of-line 1)
  10596. (looking-at org-todo-line-regexp))
  10597. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10598. (progn
  10599. (goto-char (or (match-end 2) (match-end 1)))
  10600. (and (looking-at " ") (just-one-space))))
  10601. (when org-trigger-hook
  10602. (save-excursion
  10603. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10604. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10605. "Block turning an entry into a TODO, using the hierarchy.
  10606. This checks whether the current task should be blocked from state
  10607. changes. Such blocking occurs when:
  10608. 1. The task has children which are not all in a completed state.
  10609. 2. A task has a parent with the property :ORDERED:, and there
  10610. are siblings prior to the current task with incomplete
  10611. status.
  10612. 3. The parent of the task is blocked because it has siblings that should
  10613. be done first, or is child of a block grandparent TODO entry."
  10614. (if (not org-enforce-todo-dependencies)
  10615. t ; if locally turned off don't block
  10616. (catch 'dont-block
  10617. ;; If this is not a todo state change, or if this entry is already DONE,
  10618. ;; do not block
  10619. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10620. (member (plist-get change-plist :from)
  10621. (cons 'done org-done-keywords))
  10622. (member (plist-get change-plist :to)
  10623. (cons 'todo org-not-done-keywords))
  10624. (not (plist-get change-plist :to)))
  10625. (throw 'dont-block t))
  10626. ;; If this task has children, and any are undone, it's blocked
  10627. (save-excursion
  10628. (org-back-to-heading t)
  10629. (let ((this-level (funcall outline-level)))
  10630. (outline-next-heading)
  10631. (let ((child-level (funcall outline-level)))
  10632. (while (and (not (eobp))
  10633. (> child-level this-level))
  10634. ;; this todo has children, check whether they are all
  10635. ;; completed
  10636. (if (and (not (org-entry-is-done-p))
  10637. (org-entry-is-todo-p))
  10638. (throw 'dont-block nil))
  10639. (outline-next-heading)
  10640. (setq child-level (funcall outline-level))))))
  10641. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10642. ;; any previous siblings are undone, it's blocked
  10643. (save-excursion
  10644. (org-back-to-heading t)
  10645. (let* ((pos (point))
  10646. (parent-pos (and (org-up-heading-safe) (point))))
  10647. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10648. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10649. (forward-line 1)
  10650. (re-search-forward org-not-done-heading-regexp pos t))
  10651. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10652. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10653. (while t
  10654. (goto-char parent-pos)
  10655. (if (not (looking-at org-not-done-heading-regexp))
  10656. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10657. (setq pos (point))
  10658. (setq parent-pos (and (org-up-heading-safe) (point)))
  10659. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10660. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10661. (forward-line 1)
  10662. (re-search-forward org-not-done-heading-regexp pos t))
  10663. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10664. (defcustom org-track-ordered-property-with-tag nil
  10665. "Should the ORDERED property also be shown as a tag?
  10666. The ORDERED property decides if an entry should require subtasks to be
  10667. completed in sequence. Since a property is not very visible, setting
  10668. this option means that toggling the ORDERED property with the command
  10669. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10670. not relevant for the behavior, but it makes things more visible.
  10671. Note that toggling the tag with tags commands will not change the property
  10672. and therefore not influence behavior!
  10673. This can be t, meaning the tag ORDERED should be used, It can also be a
  10674. string to select a different tag for this task."
  10675. :group 'org-todo
  10676. :type '(choice
  10677. (const :tag "No tracking" nil)
  10678. (const :tag "Track with ORDERED tag" t)
  10679. (string :tag "Use other tag")))
  10680. (defun org-toggle-ordered-property ()
  10681. "Toggle the ORDERED property of the current entry.
  10682. For better visibility, you can track the value of this property with a tag.
  10683. See variable `org-track-ordered-property-with-tag'."
  10684. (interactive)
  10685. (let* ((t1 org-track-ordered-property-with-tag)
  10686. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10687. (save-excursion
  10688. (org-back-to-heading)
  10689. (if (org-entry-get nil "ORDERED")
  10690. (progn
  10691. (org-delete-property "ORDERED")
  10692. (and tag (org-toggle-tag tag 'off))
  10693. (message "Subtasks can be completed in arbitrary order"))
  10694. (org-entry-put nil "ORDERED" "t")
  10695. (and tag (org-toggle-tag tag 'on))
  10696. (message "Subtasks must be completed in sequence")))))
  10697. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10698. (defun org-block-todo-from-checkboxes (change-plist)
  10699. "Block turning an entry into a TODO, using checkboxes.
  10700. This checks whether the current task should be blocked from state
  10701. changes because there are unchecked boxes in this entry."
  10702. (if (not org-enforce-todo-checkbox-dependencies)
  10703. t ; if locally turned off don't block
  10704. (catch 'dont-block
  10705. ;; If this is not a todo state change, or if this entry is already DONE,
  10706. ;; do not block
  10707. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10708. (member (plist-get change-plist :from)
  10709. (cons 'done org-done-keywords))
  10710. (member (plist-get change-plist :to)
  10711. (cons 'todo org-not-done-keywords))
  10712. (not (plist-get change-plist :to)))
  10713. (throw 'dont-block t))
  10714. ;; If this task has checkboxes that are not checked, it's blocked
  10715. (save-excursion
  10716. (org-back-to-heading t)
  10717. (let ((beg (point)) end)
  10718. (outline-next-heading)
  10719. (setq end (point))
  10720. (goto-char beg)
  10721. (if (org-list-search-forward
  10722. (concat (org-item-beginning-re)
  10723. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10724. "\\[[- ]\\]")
  10725. end t)
  10726. (progn
  10727. (if (boundp 'org-blocked-by-checkboxes)
  10728. (setq org-blocked-by-checkboxes t))
  10729. (throw 'dont-block nil)))))
  10730. t))) ; do not block
  10731. (defun org-entry-blocked-p ()
  10732. "Is the current entry blocked?"
  10733. (if (org-entry-get nil "NOBLOCKING")
  10734. nil ;; Never block this entry
  10735. (not
  10736. (run-hook-with-args-until-failure
  10737. 'org-blocker-hook
  10738. (list :type 'todo-state-change
  10739. :position (point)
  10740. :from 'todo
  10741. :to 'done)))))
  10742. (defun org-update-statistics-cookies (all)
  10743. "Update the statistics cookie, either from TODO or from checkboxes.
  10744. This should be called with the cursor in a line with a statistics cookie."
  10745. (interactive "P")
  10746. (if all
  10747. (progn
  10748. (org-update-checkbox-count 'all)
  10749. (org-map-entries 'org-update-parent-todo-statistics))
  10750. (if (not (org-at-heading-p))
  10751. (org-update-checkbox-count)
  10752. (let ((pos (move-marker (make-marker) (point)))
  10753. end l1 l2)
  10754. (ignore-errors (org-back-to-heading t))
  10755. (if (not (org-at-heading-p))
  10756. (org-update-checkbox-count)
  10757. (setq l1 (org-outline-level))
  10758. (setq end (save-excursion
  10759. (outline-next-heading)
  10760. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10761. (point)))
  10762. (if (and (save-excursion
  10763. (re-search-forward
  10764. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10765. (not (save-excursion (re-search-forward
  10766. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10767. (org-update-checkbox-count)
  10768. (if (and l2 (> l2 l1))
  10769. (progn
  10770. (goto-char end)
  10771. (org-update-parent-todo-statistics))
  10772. (goto-char pos)
  10773. (beginning-of-line 1)
  10774. (while (re-search-forward
  10775. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10776. (point-at-eol) t)
  10777. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10778. (goto-char pos)
  10779. (move-marker pos nil)))))
  10780. (defvar org-entry-property-inherited-from) ;; defined below
  10781. (defun org-update-parent-todo-statistics ()
  10782. "Update any statistics cookie in the parent of the current headline.
  10783. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10784. the entire subtree and this will travel up the hierarchy and update
  10785. statistics everywhere."
  10786. (let* ((prop (save-excursion (org-up-heading-safe)
  10787. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10788. (recursive (or (not org-hierarchical-todo-statistics)
  10789. (and prop (string-match "\\<recursive\\>" prop))))
  10790. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10791. 0))
  10792. (first t)
  10793. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10794. level ltoggle l1 new ndel
  10795. (cnt-all 0) (cnt-done 0) is-percent kwd
  10796. checkbox-beg ov ovs ove cookie-present)
  10797. (catch 'exit
  10798. (save-excursion
  10799. (beginning-of-line 1)
  10800. (setq ltoggle (funcall outline-level))
  10801. ;; Three situations are to consider:
  10802. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10803. ;; to the top-level ancestor on the headline;
  10804. ;; 2. If parent has "recursive" property, repeat up to the
  10805. ;; headline setting that property, taking inheritance into
  10806. ;; account;
  10807. ;; 3. Else, move up to direct parent and proceed only once.
  10808. (while (and (setq level (org-up-heading-safe))
  10809. (or recursive first)
  10810. (>= (point) lim))
  10811. (setq first nil cookie-present nil)
  10812. (unless (and level
  10813. (not (string-match
  10814. "\\<checkbox\\>"
  10815. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10816. "")))))
  10817. (throw 'exit nil))
  10818. (while (re-search-forward box-re (point-at-eol) t)
  10819. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10820. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10821. (save-match-data
  10822. (unless (outline-next-heading) (throw 'exit nil))
  10823. (while (and (looking-at org-complex-heading-regexp)
  10824. (> (setq l1 (length (match-string 1))) level))
  10825. (setq kwd (and (or recursive (= l1 ltoggle))
  10826. (match-string 2)))
  10827. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10828. (and (listp org-provide-todo-statistics)
  10829. (or (member kwd org-provide-todo-statistics)
  10830. (member kwd org-done-keywords))))
  10831. (setq cnt-all (1+ cnt-all))
  10832. (if (eq org-provide-todo-statistics t)
  10833. (and kwd (setq cnt-all (1+ cnt-all)))))
  10834. (and (member kwd org-done-keywords)
  10835. (setq cnt-done (1+ cnt-done)))
  10836. (outline-next-heading)))
  10837. (setq new
  10838. (if is-percent
  10839. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10840. (format "[%d/%d]" cnt-done cnt-all))
  10841. ndel (- (match-end 0) checkbox-beg))
  10842. ;; handle overlays when updating cookie from column view
  10843. (when (setq ov (car (overlays-at checkbox-beg)))
  10844. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10845. (delete-overlay ov))
  10846. (goto-char checkbox-beg)
  10847. (insert new)
  10848. (delete-region (point) (+ (point) ndel))
  10849. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10850. (when ov (move-overlay ov ovs ove)))
  10851. (when cookie-present
  10852. (run-hook-with-args 'org-after-todo-statistics-hook
  10853. cnt-done (- cnt-all cnt-done))))))
  10854. (run-hooks 'org-todo-statistics-hook)))
  10855. (defvar org-after-todo-statistics-hook nil
  10856. "Hook that is called after a TODO statistics cookie has been updated.
  10857. Each function is called with two arguments: the number of not-done entries
  10858. and the number of done entries.
  10859. For example, the following function, when added to this hook, will switch
  10860. an entry to DONE when all children are done, and back to TODO when new
  10861. entries are set to a TODO status. Note that this hook is only called
  10862. when there is a statistics cookie in the headline!
  10863. (defun org-summary-todo (n-done n-not-done)
  10864. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10865. (let (org-log-done org-log-states) ; turn off logging
  10866. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10867. ")
  10868. (defvar org-todo-statistics-hook nil
  10869. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10870. This hook runs even if there is no statistics cookie present, in which case
  10871. `org-after-todo-statistics-hook' would not run.")
  10872. (defun org-todo-trigger-tag-changes (state)
  10873. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10874. (let ((l org-todo-state-tags-triggers)
  10875. changes)
  10876. (when (or (not state) (equal state ""))
  10877. (setq changes (append changes (cdr (assoc "" l)))))
  10878. (when (and (stringp state) (> (length state) 0))
  10879. (setq changes (append changes (cdr (assoc state l)))))
  10880. (when (member state org-not-done-keywords)
  10881. (setq changes (append changes (cdr (assoc 'todo l)))))
  10882. (when (member state org-done-keywords)
  10883. (setq changes (append changes (cdr (assoc 'done l)))))
  10884. (dolist (c changes)
  10885. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10886. (defun org-local-logging (value)
  10887. "Get logging settings from a property VALUE."
  10888. (let* (words w a)
  10889. ;; directly set the variables, they are already local.
  10890. (setq org-log-done nil
  10891. org-log-repeat nil
  10892. org-todo-log-states nil)
  10893. (setq words (org-split-string value))
  10894. (while (setq w (pop words))
  10895. (cond
  10896. ((setq a (assoc w org-startup-options))
  10897. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10898. (set (nth 1 a) (nth 2 a))))
  10899. ((setq a (org-extract-log-state-settings w))
  10900. (and (member (car a) org-todo-keywords-1)
  10901. (push a org-todo-log-states)))))))
  10902. (defun org-get-todo-sequence-head (kwd)
  10903. "Return the head of the TODO sequence to which KWD belongs.
  10904. If KWD is not set, check if there is a text property remembering the
  10905. right sequence."
  10906. (let (p)
  10907. (cond
  10908. ((not kwd)
  10909. (or (get-text-property (point-at-bol) 'org-todo-head)
  10910. (progn
  10911. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10912. nil (point-at-eol)))
  10913. (get-text-property p 'org-todo-head))))
  10914. ((not (member kwd org-todo-keywords-1))
  10915. (car org-todo-keywords-1))
  10916. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10917. (defun org-fast-todo-selection ()
  10918. "Fast TODO keyword selection with single keys.
  10919. Returns the new TODO keyword, or nil if no state change should occur."
  10920. (let* ((fulltable org-todo-key-alist)
  10921. (done-keywords org-done-keywords) ;; needed for the faces.
  10922. (maxlen (apply 'max (mapcar
  10923. (lambda (x)
  10924. (if (stringp (car x)) (string-width (car x)) 0))
  10925. fulltable)))
  10926. (expert nil)
  10927. (fwidth (+ maxlen 3 1 3))
  10928. (ncol (/ (- (window-width) 4) fwidth))
  10929. tg cnt e c tbl
  10930. groups ingroup)
  10931. (save-excursion
  10932. (save-window-excursion
  10933. (if expert
  10934. (set-buffer (get-buffer-create " *Org todo*"))
  10935. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10936. (erase-buffer)
  10937. (org-set-local 'org-done-keywords done-keywords)
  10938. (setq tbl fulltable cnt 0)
  10939. (while (setq e (pop tbl))
  10940. (cond
  10941. ((equal e '(:startgroup))
  10942. (push '() groups) (setq ingroup t)
  10943. (when (not (= cnt 0))
  10944. (setq cnt 0)
  10945. (insert "\n"))
  10946. (insert "{ "))
  10947. ((equal e '(:endgroup))
  10948. (setq ingroup nil cnt 0)
  10949. (insert "}\n"))
  10950. ((equal e '(:newline))
  10951. (when (not (= cnt 0))
  10952. (setq cnt 0)
  10953. (insert "\n")
  10954. (setq e (car tbl))
  10955. (while (equal (car tbl) '(:newline))
  10956. (insert "\n")
  10957. (setq tbl (cdr tbl)))))
  10958. (t
  10959. (setq tg (car e) c (cdr e))
  10960. (if ingroup (push tg (car groups)))
  10961. (setq tg (org-add-props tg nil 'face
  10962. (org-get-todo-face tg)))
  10963. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10964. (insert "[" c "] " tg (make-string
  10965. (- fwidth 4 (length tg)) ?\ ))
  10966. (when (= (setq cnt (1+ cnt)) ncol)
  10967. (insert "\n")
  10968. (if ingroup (insert " "))
  10969. (setq cnt 0)))))
  10970. (insert "\n")
  10971. (goto-char (point-min))
  10972. (if (not expert) (org-fit-window-to-buffer))
  10973. (message "[a-z..]:Set [SPC]:clear")
  10974. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10975. (cond
  10976. ((or (= c ?\C-g)
  10977. (and (= c ?q) (not (rassoc c fulltable))))
  10978. (setq quit-flag t))
  10979. ((= c ?\ ) nil)
  10980. ((setq e (rassoc c fulltable) tg (car e))
  10981. tg)
  10982. (t (setq quit-flag t)))))))
  10983. (defun org-entry-is-todo-p ()
  10984. (member (org-get-todo-state) org-not-done-keywords))
  10985. (defun org-entry-is-done-p ()
  10986. (member (org-get-todo-state) org-done-keywords))
  10987. (defun org-get-todo-state ()
  10988. (save-excursion
  10989. (org-back-to-heading t)
  10990. (and (looking-at org-todo-line-regexp)
  10991. (match-end 2)
  10992. (match-string 2))))
  10993. (defun org-at-date-range-p (&optional inactive-ok)
  10994. "Is the cursor inside a date range?"
  10995. (interactive)
  10996. (save-excursion
  10997. (catch 'exit
  10998. (let ((pos (point)))
  10999. (skip-chars-backward "^[<\r\n")
  11000. (skip-chars-backward "<[")
  11001. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11002. (>= (match-end 0) pos)
  11003. (throw 'exit t))
  11004. (skip-chars-backward "^<[\r\n")
  11005. (skip-chars-backward "<[")
  11006. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11007. (>= (match-end 0) pos)
  11008. (throw 'exit t)))
  11009. nil)))
  11010. (defun org-get-repeat (&optional tagline)
  11011. "Check if there is a deadline/schedule with repeater in this entry."
  11012. (save-match-data
  11013. (save-excursion
  11014. (org-back-to-heading t)
  11015. (and (re-search-forward (if tagline
  11016. (concat tagline "\\s-*" org-repeat-re)
  11017. org-repeat-re)
  11018. (org-entry-end-position) t)
  11019. (match-string-no-properties 1)))))
  11020. (defvar org-last-changed-timestamp)
  11021. (defvar org-last-inserted-timestamp)
  11022. (defvar org-log-post-message)
  11023. (defvar org-log-note-purpose)
  11024. (defvar org-log-note-how)
  11025. (defvar org-log-note-extra)
  11026. (defun org-auto-repeat-maybe (done-word)
  11027. "Check if the current headline contains a repeated deadline/schedule.
  11028. If yes, set TODO state back to what it was and change the base date
  11029. of repeating deadline/scheduled time stamps to new date.
  11030. This function is run automatically after each state change to a DONE state."
  11031. ;; last-state is dynamically scoped into this function
  11032. (let* ((repeat (org-get-repeat))
  11033. (aa (assoc org-last-state org-todo-kwd-alist))
  11034. (interpret (nth 1 aa))
  11035. (head (nth 2 aa))
  11036. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11037. (msg "Entry repeats: ")
  11038. (org-log-done nil)
  11039. (org-todo-log-states nil)
  11040. re type n what ts time to-state)
  11041. (when repeat
  11042. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11043. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11044. org-todo-repeat-to-state))
  11045. (unless (and to-state (member to-state org-todo-keywords-1))
  11046. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11047. (org-todo to-state)
  11048. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11049. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11050. (org-time-stamp-format t t))))
  11051. (when org-log-repeat
  11052. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11053. (memq 'org-add-log-note post-command-hook))
  11054. ;; OK, we are already setup for some record
  11055. (if (eq org-log-repeat 'note)
  11056. ;; make sure we take a note, not only a time stamp
  11057. (setq org-log-note-how 'note))
  11058. ;; Set up for taking a record
  11059. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11060. org-last-state
  11061. 'findpos org-log-repeat)))
  11062. (org-back-to-heading t)
  11063. (org-add-planning-info nil nil 'closed)
  11064. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11065. org-deadline-time-regexp "\\)\\|\\("
  11066. org-ts-regexp "\\)"))
  11067. (while (re-search-forward
  11068. re (save-excursion (outline-next-heading) (point)) t)
  11069. (setq type (if (match-end 1) org-scheduled-string
  11070. (if (match-end 3) org-deadline-string "Plain:"))
  11071. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11072. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11073. (setq n (string-to-number (match-string 2 ts))
  11074. what (match-string 3 ts))
  11075. (if (equal what "w") (setq n (* n 7) what "d"))
  11076. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11077. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11078. ;; Preparation, see if we need to modify the start date for the change
  11079. (when (match-end 1)
  11080. (setq time (save-match-data (org-time-string-to-time ts)))
  11081. (cond
  11082. ((equal (match-string 1 ts) ".")
  11083. ;; Shift starting date to today
  11084. (org-timestamp-change
  11085. (- (org-today) (time-to-days time))
  11086. 'day))
  11087. ((equal (match-string 1 ts) "+")
  11088. (let ((nshiftmax 10) (nshift 0))
  11089. (while (or (= nshift 0)
  11090. (<= (time-to-days time)
  11091. (time-to-days (current-time))))
  11092. (when (= (incf nshift) nshiftmax)
  11093. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11094. (error "Abort")))
  11095. (org-timestamp-change n (cdr (assoc what whata)))
  11096. (org-at-timestamp-p t)
  11097. (setq ts (match-string 1))
  11098. (setq time (save-match-data (org-time-string-to-time ts)))))
  11099. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11100. ;; rematch, so that we have everything in place for the real shift
  11101. (org-at-timestamp-p t)
  11102. (setq ts (match-string 1))
  11103. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11104. (org-timestamp-change n (cdr (assoc what whata)))
  11105. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11106. (setq org-log-post-message msg)
  11107. (message "%s" msg))))
  11108. (defun org-show-todo-tree (arg)
  11109. "Make a compact tree which shows all headlines marked with TODO.
  11110. The tree will show the lines where the regexp matches, and all higher
  11111. headlines above the match.
  11112. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11113. With a numeric prefix N, construct a sparse tree for the Nth element
  11114. of `org-todo-keywords-1'."
  11115. (interactive "P")
  11116. (let ((case-fold-search nil)
  11117. (kwd-re
  11118. (cond ((null arg) org-not-done-regexp)
  11119. ((equal arg '(4))
  11120. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11121. (mapcar 'list org-todo-keywords-1))))
  11122. (concat "\\("
  11123. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11124. "\\)\\>")))
  11125. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11126. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11127. org-todo-keywords-1)))
  11128. (t (error "Invalid prefix argument: %s" arg)))))
  11129. (message "%d TODO entries found"
  11130. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11131. (defun org-deadline (&optional remove time)
  11132. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11133. With argument REMOVE, remove any deadline from the item.
  11134. With argument TIME, set the deadline at the corresponding date. TIME
  11135. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11136. (interactive "P")
  11137. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11138. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11139. 'region-start-level 'region))
  11140. org-loop-over-headlines-in-active-region)
  11141. (org-map-entries
  11142. `(org-deadline ',remove ,time)
  11143. org-loop-over-headlines-in-active-region
  11144. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11145. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11146. (repeater (and old-date
  11147. (string-match
  11148. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11149. old-date)
  11150. (match-string 1 old-date))))
  11151. (if remove
  11152. (progn
  11153. (when (and old-date org-log-redeadline)
  11154. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11155. org-log-redeadline))
  11156. (org-remove-timestamp-with-keyword org-deadline-string)
  11157. (message "Item no longer has a deadline."))
  11158. (org-add-planning-info 'deadline time 'closed)
  11159. (when (and old-date org-log-redeadline
  11160. (not (equal old-date
  11161. (substring org-last-inserted-timestamp 1 -1))))
  11162. (org-add-log-setup 'redeadline nil old-date 'findpos
  11163. org-log-redeadline))
  11164. (when repeater
  11165. (save-excursion
  11166. (org-back-to-heading t)
  11167. (when (re-search-forward (concat org-deadline-string " "
  11168. org-last-inserted-timestamp)
  11169. (save-excursion
  11170. (outline-next-heading) (point)) t)
  11171. (goto-char (1- (match-end 0)))
  11172. (insert " " repeater)
  11173. (setq org-last-inserted-timestamp
  11174. (concat (substring org-last-inserted-timestamp 0 -1)
  11175. " " repeater
  11176. (substring org-last-inserted-timestamp -1))))))
  11177. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11178. (defun org-schedule (&optional remove time)
  11179. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11180. With argument REMOVE, remove any scheduling date from the item.
  11181. With argument TIME, scheduled at the corresponding date. TIME can
  11182. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11183. (interactive "P")
  11184. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11185. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11186. 'region-start-level 'region))
  11187. org-loop-over-headlines-in-active-region)
  11188. (org-map-entries
  11189. `(org-schedule ',remove ,time)
  11190. org-loop-over-headlines-in-active-region
  11191. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11192. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11193. (repeater (and old-date
  11194. (string-match
  11195. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11196. old-date)
  11197. (match-string 1 old-date))))
  11198. (if remove
  11199. (progn
  11200. (when (and old-date org-log-reschedule)
  11201. (org-add-log-setup 'delschedule nil old-date 'findpos
  11202. org-log-reschedule))
  11203. (org-remove-timestamp-with-keyword org-scheduled-string)
  11204. (message "Item is no longer scheduled."))
  11205. (org-add-planning-info 'scheduled time 'closed)
  11206. (when (and old-date org-log-reschedule
  11207. (not (equal old-date
  11208. (substring org-last-inserted-timestamp 1 -1))))
  11209. (org-add-log-setup 'reschedule nil old-date 'findpos
  11210. org-log-reschedule))
  11211. (when repeater
  11212. (save-excursion
  11213. (org-back-to-heading t)
  11214. (when (re-search-forward (concat org-scheduled-string " "
  11215. org-last-inserted-timestamp)
  11216. (save-excursion
  11217. (outline-next-heading) (point)) t)
  11218. (goto-char (1- (match-end 0)))
  11219. (insert " " repeater)
  11220. (setq org-last-inserted-timestamp
  11221. (concat (substring org-last-inserted-timestamp 0 -1)
  11222. " " repeater
  11223. (substring org-last-inserted-timestamp -1))))))
  11224. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11225. (defun org-get-scheduled-time (pom &optional inherit)
  11226. "Get the scheduled time as a time tuple, of a format suitable
  11227. for calling org-schedule with, or if there is no scheduling,
  11228. returns nil."
  11229. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11230. (when time
  11231. (apply 'encode-time (org-parse-time-string time)))))
  11232. (defun org-get-deadline-time (pom &optional inherit)
  11233. "Get the deadline as a time tuple, of a format suitable for
  11234. calling org-deadline with, or if there is no scheduling, returns
  11235. nil."
  11236. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11237. (when time
  11238. (apply 'encode-time (org-parse-time-string time)))))
  11239. (defun org-remove-timestamp-with-keyword (keyword)
  11240. "Remove all time stamps with KEYWORD in the current entry."
  11241. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11242. beg)
  11243. (save-excursion
  11244. (org-back-to-heading t)
  11245. (setq beg (point))
  11246. (outline-next-heading)
  11247. (while (re-search-backward re beg t)
  11248. (replace-match "")
  11249. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11250. (equal (char-before) ?\ ))
  11251. (backward-delete-char 1)
  11252. (if (string-match "^[ \t]*$" (buffer-substring
  11253. (point-at-bol) (point-at-eol)))
  11254. (delete-region (point-at-bol)
  11255. (min (point-max) (1+ (point-at-eol))))))))))
  11256. (defun org-add-planning-info (what &optional time &rest remove)
  11257. "Insert new timestamp with keyword in the line directly after the headline.
  11258. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11259. If non is given, the user is prompted for a date.
  11260. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11261. be removed."
  11262. (interactive)
  11263. (let (org-time-was-given org-end-time-was-given ts
  11264. end default-time default-input)
  11265. (catch 'exit
  11266. (when (and (memq what '(scheduled deadline))
  11267. (or (not time)
  11268. (and (stringp time)
  11269. (string-match "^[-+]+[0-9]" time))))
  11270. ;; Try to get a default date/time from existing timestamp
  11271. (save-excursion
  11272. (org-back-to-heading t)
  11273. (setq end (save-excursion (outline-next-heading) (point)))
  11274. (when (re-search-forward (if (eq what 'scheduled)
  11275. org-scheduled-time-regexp
  11276. org-deadline-time-regexp)
  11277. end t)
  11278. (setq ts (match-string 1)
  11279. default-time
  11280. (apply 'encode-time (org-parse-time-string ts))
  11281. default-input (and ts (org-get-compact-tod ts))))))
  11282. (when what
  11283. (setq time
  11284. (if (stringp time)
  11285. ;; This is a string (relative or absolute), set proper date
  11286. (apply 'encode-time
  11287. (org-read-date-analyze
  11288. time default-time (decode-time default-time)))
  11289. ;; If necessary, get the time from the user
  11290. (or time (org-read-date nil 'to-time nil nil
  11291. default-time default-input)))))
  11292. (when (and org-insert-labeled-timestamps-at-point
  11293. (member what '(scheduled deadline)))
  11294. (insert
  11295. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11296. (org-insert-time-stamp time org-time-was-given
  11297. nil nil nil (list org-end-time-was-given))
  11298. (setq what nil))
  11299. (save-excursion
  11300. (save-restriction
  11301. (let (col list elt ts buffer-invisibility-spec)
  11302. (org-back-to-heading t)
  11303. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11304. (goto-char (match-end 1))
  11305. (setq col (current-column))
  11306. (goto-char (match-end 0))
  11307. (if (eobp) (insert "\n") (forward-char 1))
  11308. (when (and (not what)
  11309. (not (looking-at
  11310. (concat "[ \t]*"
  11311. org-keyword-time-not-clock-regexp))))
  11312. ;; Nothing to add, nothing to remove...... :-)
  11313. (throw 'exit nil))
  11314. (if (and (not (looking-at org-outline-regexp))
  11315. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11316. "[^\r\n]*"))
  11317. (not (equal (match-string 1) org-clock-string)))
  11318. (narrow-to-region (match-beginning 0) (match-end 0))
  11319. (insert-before-markers "\n")
  11320. (backward-char 1)
  11321. (narrow-to-region (point) (point))
  11322. (and org-adapt-indentation (org-indent-to-column col)))
  11323. ;; Check if we have to remove something.
  11324. (setq list (cons what remove))
  11325. (while list
  11326. (setq elt (pop list))
  11327. (when (or (and (eq elt 'scheduled)
  11328. (re-search-forward org-scheduled-time-regexp nil t))
  11329. (and (eq elt 'deadline)
  11330. (re-search-forward org-deadline-time-regexp nil t))
  11331. (and (eq elt 'closed)
  11332. (re-search-forward org-closed-time-regexp nil t)))
  11333. (replace-match "")
  11334. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11335. (and (looking-at "[ \t]+") (replace-match ""))
  11336. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11337. (when what
  11338. (insert
  11339. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11340. (cond ((eq what 'scheduled) org-scheduled-string)
  11341. ((eq what 'deadline) org-deadline-string)
  11342. ((eq what 'closed) org-closed-string))
  11343. " ")
  11344. (setq ts (org-insert-time-stamp
  11345. time
  11346. (or org-time-was-given
  11347. (and (eq what 'closed) org-log-done-with-time))
  11348. (eq what 'closed)
  11349. nil nil (list org-end-time-was-given)))
  11350. (insert
  11351. (if (not (or (bolp) (eq (char-before) ?\ )
  11352. (memq (char-after) '(32 10))
  11353. (eobp))) " " ""))
  11354. (end-of-line 1))
  11355. (goto-char (point-min))
  11356. (widen)
  11357. (if (and (looking-at "[ \t]*\n")
  11358. (equal (char-before) ?\n))
  11359. (delete-region (1- (point)) (point-at-eol)))
  11360. ts))))))
  11361. (defvar org-log-note-marker (make-marker))
  11362. (defvar org-log-note-purpose nil)
  11363. (defvar org-log-note-state nil)
  11364. (defvar org-log-note-previous-state nil)
  11365. (defvar org-log-note-how nil)
  11366. (defvar org-log-note-extra nil)
  11367. (defvar org-log-note-window-configuration nil)
  11368. (defvar org-log-note-return-to (make-marker))
  11369. (defvar org-log-note-effective-time nil
  11370. "Remembered current time so that dynamically scoped
  11371. `org-extend-today-until' affects tha timestamps in state change
  11372. log")
  11373. (defvar org-log-post-message nil
  11374. "Message to be displayed after a log note has been stored.
  11375. The auto-repeater uses this.")
  11376. (defun org-add-note ()
  11377. "Add a note to the current entry.
  11378. This is done in the same way as adding a state change note."
  11379. (interactive)
  11380. (org-add-log-setup 'note nil nil 'findpos nil))
  11381. (defvar org-property-end-re)
  11382. (defun org-add-log-setup (&optional purpose state prev-state
  11383. findpos how extra)
  11384. "Set up the post command hook to take a note.
  11385. If this is about to TODO state change, the new state is expected in STATE.
  11386. When FINDPOS is non-nil, find the correct position for the note in
  11387. the current entry. If not, assume that it can be inserted at point.
  11388. HOW is an indicator what kind of note should be created.
  11389. EXTRA is additional text that will be inserted into the notes buffer."
  11390. (let* ((org-log-into-drawer (org-log-into-drawer))
  11391. (drawer (cond ((stringp org-log-into-drawer)
  11392. org-log-into-drawer)
  11393. (org-log-into-drawer "LOGBOOK"))))
  11394. (save-restriction
  11395. (save-excursion
  11396. (when findpos
  11397. (org-back-to-heading t)
  11398. (narrow-to-region (point) (save-excursion
  11399. (outline-next-heading) (point)))
  11400. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11401. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11402. "[^\r\n]*\\)?"))
  11403. (goto-char (match-end 0))
  11404. (cond
  11405. (drawer
  11406. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11407. nil t)
  11408. (progn
  11409. (goto-char (match-end 0))
  11410. (or org-log-states-order-reversed
  11411. (and (re-search-forward org-property-end-re nil t)
  11412. (goto-char (1- (match-beginning 0))))))
  11413. (insert "\n:" drawer ":\n:END:")
  11414. (beginning-of-line 0)
  11415. (org-indent-line)
  11416. (beginning-of-line 2)
  11417. (org-indent-line)
  11418. (end-of-line 0)))
  11419. ((and org-log-state-notes-insert-after-drawers
  11420. (save-excursion
  11421. (forward-line) (looking-at org-drawer-regexp)))
  11422. (forward-line)
  11423. (while (looking-at org-drawer-regexp)
  11424. (goto-char (match-end 0))
  11425. (re-search-forward org-property-end-re (point-max) t)
  11426. (forward-line))
  11427. (forward-line -1)))
  11428. (unless org-log-states-order-reversed
  11429. (and (= (char-after) ?\n) (forward-char 1))
  11430. (org-skip-over-state-notes)
  11431. (skip-chars-backward " \t\n\r")))
  11432. (move-marker org-log-note-marker (point))
  11433. (setq org-log-note-purpose purpose
  11434. org-log-note-state state
  11435. org-log-note-previous-state prev-state
  11436. org-log-note-how how
  11437. org-log-note-extra extra
  11438. org-log-note-effective-time (org-current-effective-time))
  11439. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11440. (defun org-skip-over-state-notes ()
  11441. "Skip past the list of State notes in an entry."
  11442. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11443. (when (ignore-errors (goto-char (org-in-item-p)))
  11444. (let* ((struct (org-list-struct))
  11445. (prevs (org-list-prevs-alist struct)))
  11446. (while (looking-at "[ \t]*- State")
  11447. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11448. (org-list-get-item-end (point) struct)))))))
  11449. (defun org-add-log-note (&optional purpose)
  11450. "Pop up a window for taking a note, and add this note later at point."
  11451. (remove-hook 'post-command-hook 'org-add-log-note)
  11452. (setq org-log-note-window-configuration (current-window-configuration))
  11453. (delete-other-windows)
  11454. (move-marker org-log-note-return-to (point))
  11455. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11456. (goto-char org-log-note-marker)
  11457. (org-switch-to-buffer-other-window "*Org Note*")
  11458. (erase-buffer)
  11459. (if (memq org-log-note-how '(time state))
  11460. (let (current-prefix-arg) (org-store-log-note))
  11461. (let ((org-inhibit-startup t)) (org-mode))
  11462. (insert (format "# Insert note for %s.
  11463. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11464. (cond
  11465. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11466. ((eq org-log-note-purpose 'done) "closed todo item")
  11467. ((eq org-log-note-purpose 'state)
  11468. (format "state change from \"%s\" to \"%s\""
  11469. (or org-log-note-previous-state "")
  11470. (or org-log-note-state "")))
  11471. ((eq org-log-note-purpose 'reschedule)
  11472. "rescheduling")
  11473. ((eq org-log-note-purpose 'delschedule)
  11474. "no longer scheduled")
  11475. ((eq org-log-note-purpose 'redeadline)
  11476. "changing deadline")
  11477. ((eq org-log-note-purpose 'deldeadline)
  11478. "removing deadline")
  11479. ((eq org-log-note-purpose 'refile)
  11480. "refiling")
  11481. ((eq org-log-note-purpose 'note)
  11482. "this entry")
  11483. (t (error "This should not happen")))))
  11484. (if org-log-note-extra (insert org-log-note-extra))
  11485. (org-set-local 'org-finish-function 'org-store-log-note)
  11486. (run-hooks 'org-log-buffer-setup-hook)))
  11487. (defvar org-note-abort nil) ; dynamically scoped
  11488. (defun org-store-log-note ()
  11489. "Finish taking a log note, and insert it to where it belongs."
  11490. (let ((txt (buffer-string))
  11491. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11492. lines ind bul)
  11493. (kill-buffer (current-buffer))
  11494. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11495. (setq txt (replace-match "" t t txt)))
  11496. (if (string-match "\\s-+\\'" txt)
  11497. (setq txt (replace-match "" t t txt)))
  11498. (setq lines (org-split-string txt "\n"))
  11499. (when (and note (string-match "\\S-" note))
  11500. (setq note
  11501. (org-replace-escapes
  11502. note
  11503. (list (cons "%u" (user-login-name))
  11504. (cons "%U" user-full-name)
  11505. (cons "%t" (format-time-string
  11506. (org-time-stamp-format 'long 'inactive)
  11507. org-log-note-effective-time))
  11508. (cons "%T" (format-time-string
  11509. (org-time-stamp-format 'long nil)
  11510. org-log-note-effective-time))
  11511. (cons "%d" (format-time-string
  11512. (org-time-stamp-format nil 'inactive)
  11513. org-log-note-effective-time))
  11514. (cons "%D" (format-time-string
  11515. (org-time-stamp-format nil nil)
  11516. org-log-note-effective-time))
  11517. (cons "%s" (if org-log-note-state
  11518. (concat "\"" org-log-note-state "\"")
  11519. ""))
  11520. (cons "%S" (if org-log-note-previous-state
  11521. (concat "\"" org-log-note-previous-state "\"")
  11522. "\"\"")))))
  11523. (if lines (setq note (concat note " \\\\")))
  11524. (push note lines))
  11525. (when (or current-prefix-arg org-note-abort)
  11526. (when org-log-into-drawer
  11527. (org-remove-empty-drawer-at
  11528. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11529. org-log-note-marker))
  11530. (setq lines nil))
  11531. (when lines
  11532. (with-current-buffer (marker-buffer org-log-note-marker)
  11533. (save-excursion
  11534. (goto-char org-log-note-marker)
  11535. (move-marker org-log-note-marker nil)
  11536. (end-of-line 1)
  11537. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11538. (setq ind (save-excursion
  11539. (if (ignore-errors (goto-char (org-in-item-p)))
  11540. (let ((struct (org-list-struct)))
  11541. (org-list-get-ind
  11542. (org-list-get-top-point struct) struct))
  11543. (skip-chars-backward " \r\t\n")
  11544. (cond
  11545. ((and (org-at-heading-p)
  11546. org-adapt-indentation)
  11547. (1+ (org-current-level)))
  11548. ((org-at-heading-p) 0)
  11549. (t (org-get-indentation))))))
  11550. (setq bul (org-list-bullet-string "-"))
  11551. (org-indent-line-to ind)
  11552. (insert bul (pop lines))
  11553. (let ((ind-body (+ (length bul) ind)))
  11554. (while lines
  11555. (insert "\n")
  11556. (org-indent-line-to ind-body)
  11557. (insert (pop lines))))
  11558. (message "Note stored")
  11559. (org-back-to-heading t)
  11560. (org-cycle-hide-drawers 'children)))))
  11561. (set-window-configuration org-log-note-window-configuration)
  11562. (with-current-buffer (marker-buffer org-log-note-return-to)
  11563. (goto-char org-log-note-return-to))
  11564. (move-marker org-log-note-return-to nil)
  11565. (and org-log-post-message (message "%s" org-log-post-message)))
  11566. (defun org-remove-empty-drawer-at (drawer pos)
  11567. "Remove an empty drawer DRAWER at position POS.
  11568. POS may also be a marker."
  11569. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11570. (save-excursion
  11571. (save-restriction
  11572. (widen)
  11573. (goto-char pos)
  11574. (if (org-in-regexp
  11575. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11576. (replace-match ""))))))
  11577. (defvar org-ts-type nil)
  11578. (defun org-sparse-tree (&optional arg type)
  11579. "Create a sparse tree, prompt for the details.
  11580. This command can create sparse trees. You first need to select the type
  11581. of match used to create the tree:
  11582. t Show all TODO entries.
  11583. T Show entries with a specific TODO keyword.
  11584. m Show entries selected by a tags/property match.
  11585. p Enter a property name and its value (both with completion on existing
  11586. names/values) and show entries with that property.
  11587. r Show entries matching a regular expression (`/' can be used as well).
  11588. b Show deadlines and scheduled items before a date.
  11589. a Show deadlines and scheduled items after a date.
  11590. d Show deadlines due within `org-deadline-warning-days'.
  11591. D Show deadlines and scheduled items between a date range."
  11592. (interactive "P")
  11593. (let (ans kwd value ts-type)
  11594. (setq type (or type org-sparse-tree-default-date-type))
  11595. (setq org-ts-type type)
  11596. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11597. (cond ((eq type 'all) "all timestamps")
  11598. ((eq type 'scheduled) "only scheduled")
  11599. ((eq type 'deadline) "only deadline")
  11600. ((eq type 'active) "only active timestamps")
  11601. ((eq type 'inactive) "only inactive timestamps")
  11602. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11603. (t "scheduled/deadline")))
  11604. (setq ans (read-char-exclusive))
  11605. (cond
  11606. ((equal ans ?c)
  11607. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11608. ((equal ans ?d)
  11609. (call-interactively 'org-check-deadlines))
  11610. ((equal ans ?b)
  11611. (call-interactively 'org-check-before-date))
  11612. ((equal ans ?a)
  11613. (call-interactively 'org-check-after-date))
  11614. ((equal ans ?D)
  11615. (call-interactively 'org-check-dates-range))
  11616. ((equal ans ?t)
  11617. (call-interactively 'org-show-todo-tree))
  11618. ((equal ans ?T)
  11619. (org-show-todo-tree '(4)))
  11620. ((member ans '(?T ?m))
  11621. (call-interactively 'org-match-sparse-tree))
  11622. ((member ans '(?p ?P))
  11623. (setq kwd (org-icompleting-read "Property: "
  11624. (mapcar 'list (org-buffer-property-keys))))
  11625. (setq value (org-icompleting-read "Value: "
  11626. (mapcar 'list (org-property-values kwd))))
  11627. (unless (string-match "\\`{.*}\\'" value)
  11628. (setq value (concat "\"" value "\"")))
  11629. (org-match-sparse-tree arg (concat kwd "=" value)))
  11630. ((member ans '(?r ?R ?/))
  11631. (call-interactively 'org-occur))
  11632. (t (error "No such sparse tree command \"%c\"" ans)))))
  11633. (defvar org-occur-highlights nil
  11634. "List of overlays used for occur matches.")
  11635. (make-variable-buffer-local 'org-occur-highlights)
  11636. (defvar org-occur-parameters nil
  11637. "Parameters of the active org-occur calls.
  11638. This is a list, each call to org-occur pushes as cons cell,
  11639. containing the regular expression and the callback, onto the list.
  11640. The list can contain several entries if `org-occur' has been called
  11641. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11642. will only contain one set of parameters. When the highlights are
  11643. removed (for example with `C-c C-c', or with the next edit (depending
  11644. on `org-remove-highlights-with-change'), this variable is emptied
  11645. as well.")
  11646. (make-variable-buffer-local 'org-occur-parameters)
  11647. (defun org-occur (regexp &optional keep-previous callback)
  11648. "Make a compact tree which shows all matches of REGEXP.
  11649. The tree will show the lines where the regexp matches, and all higher
  11650. headlines above the match. It will also show the heading after the match,
  11651. to make sure editing the matching entry is easy.
  11652. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11653. call to `org-occur' will be kept, to allow stacking of calls to this
  11654. command.
  11655. If CALLBACK is non-nil, it is a function which is called to confirm
  11656. that the match should indeed be shown."
  11657. (interactive "sRegexp: \nP")
  11658. (when (equal regexp "")
  11659. (error "Regexp cannot be empty"))
  11660. (unless keep-previous
  11661. (org-remove-occur-highlights nil nil t))
  11662. (push (cons regexp callback) org-occur-parameters)
  11663. (let ((cnt 0))
  11664. (save-excursion
  11665. (goto-char (point-min))
  11666. (if (or (not keep-previous) ; do not want to keep
  11667. (not org-occur-highlights)) ; no previous matches
  11668. ;; hide everything
  11669. (org-overview))
  11670. (while (re-search-forward regexp nil t)
  11671. (when (or (not callback)
  11672. (save-match-data (funcall callback)))
  11673. (setq cnt (1+ cnt))
  11674. (when org-highlight-sparse-tree-matches
  11675. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11676. (org-show-context 'occur-tree))))
  11677. (when org-remove-highlights-with-change
  11678. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11679. nil 'local))
  11680. (unless org-sparse-tree-open-archived-trees
  11681. (org-hide-archived-subtrees (point-min) (point-max)))
  11682. (run-hooks 'org-occur-hook)
  11683. (if (org-called-interactively-p 'interactive)
  11684. (message "%d match(es) for regexp %s" cnt regexp))
  11685. cnt))
  11686. (defun org-occur-next-match (&optional n reset)
  11687. "Function for `next-error-function' to find sparse tree matches.
  11688. N is the number of matches to move, when negative move backwards.
  11689. RESET is entirely ignored - this function always goes back to the
  11690. starting point when no match is found."
  11691. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11692. (search-func (if (< n 0)
  11693. 'previous-single-char-property-change
  11694. 'next-single-char-property-change))
  11695. (n (abs n))
  11696. (pos (point))
  11697. p1)
  11698. (catch 'exit
  11699. (while (setq p1 (funcall search-func (point) 'org-type))
  11700. (when (equal p1 limit)
  11701. (goto-char pos)
  11702. (error "No more matches"))
  11703. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11704. (setq n (1- n))
  11705. (when (= n 0)
  11706. (goto-char p1)
  11707. (throw 'exit (point))))
  11708. (goto-char p1))
  11709. (goto-char p1)
  11710. (error "No more matches"))))
  11711. (defun org-show-context (&optional key)
  11712. "Make sure point and context are visible.
  11713. How much context is shown depends upon the variables
  11714. `org-show-hierarchy-above', `org-show-following-heading',
  11715. `org-show-entry-below' and `org-show-siblings'."
  11716. (let ((heading-p (org-at-heading-p t))
  11717. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11718. (following-p (org-get-alist-option org-show-following-heading key))
  11719. (entry-p (org-get-alist-option org-show-entry-below key))
  11720. (siblings-p (org-get-alist-option org-show-siblings key)))
  11721. (catch 'exit
  11722. ;; Show heading or entry text
  11723. (if (and heading-p (not entry-p))
  11724. (org-flag-heading nil) ; only show the heading
  11725. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11726. (org-show-hidden-entry))) ; show entire entry
  11727. (when following-p
  11728. ;; Show next sibling, or heading below text
  11729. (save-excursion
  11730. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11731. (org-flag-heading nil))))
  11732. (when siblings-p (org-show-siblings))
  11733. (when hierarchy-p
  11734. ;; show all higher headings, possibly with siblings
  11735. (save-excursion
  11736. (while (and (condition-case nil
  11737. (progn (org-up-heading-all 1) t)
  11738. (error nil))
  11739. (not (bobp)))
  11740. (org-flag-heading nil)
  11741. (when siblings-p (org-show-siblings))))))))
  11742. (defvar org-reveal-start-hook nil
  11743. "Hook run before revealing a location.")
  11744. (defun org-reveal (&optional siblings)
  11745. "Show current entry, hierarchy above it, and the following headline.
  11746. This can be used to show a consistent set of context around locations
  11747. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11748. not t for the search context.
  11749. With optional argument SIBLINGS, on each level of the hierarchy all
  11750. siblings are shown. This repairs the tree structure to what it would
  11751. look like when opened with hierarchical calls to `org-cycle'.
  11752. With double optional argument \\[universal-argument] \\[universal-argument], \
  11753. go to the parent and show the
  11754. entire tree."
  11755. (interactive "P")
  11756. (run-hooks 'org-reveal-start-hook)
  11757. (let ((org-show-hierarchy-above t)
  11758. (org-show-following-heading t)
  11759. (org-show-siblings (if siblings t org-show-siblings)))
  11760. (org-show-context nil))
  11761. (when (equal siblings '(16))
  11762. (save-excursion
  11763. (when (org-up-heading-safe)
  11764. (org-show-subtree)
  11765. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11766. (defun org-highlight-new-match (beg end)
  11767. "Highlight from BEG to END and mark the highlight is an occur headline."
  11768. (let ((ov (make-overlay beg end)))
  11769. (overlay-put ov 'face 'secondary-selection)
  11770. (overlay-put ov 'org-type 'org-occur)
  11771. (push ov org-occur-highlights)))
  11772. (defun org-remove-occur-highlights (&optional beg end noremove)
  11773. "Remove the occur highlights from the buffer.
  11774. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11775. from the `before-change-functions' in the current buffer."
  11776. (interactive)
  11777. (unless org-inhibit-highlight-removal
  11778. (mapc 'delete-overlay org-occur-highlights)
  11779. (setq org-occur-highlights nil)
  11780. (setq org-occur-parameters nil)
  11781. (unless noremove
  11782. (remove-hook 'before-change-functions
  11783. 'org-remove-occur-highlights 'local))))
  11784. ;;;; Priorities
  11785. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11786. "Regular expression matching the priority indicator.")
  11787. (defvar org-remove-priority-next-time nil)
  11788. (defun org-priority-up ()
  11789. "Increase the priority of the current item."
  11790. (interactive)
  11791. (org-priority 'up))
  11792. (defun org-priority-down ()
  11793. "Decrease the priority of the current item."
  11794. (interactive)
  11795. (org-priority 'down))
  11796. (defun org-priority (&optional action show)
  11797. "Change the priority of an item.
  11798. ACTION can be `set', `up', `down', or a character."
  11799. (interactive "P")
  11800. (if (equal action '(4))
  11801. (org-show-priority)
  11802. (unless org-enable-priority-commands
  11803. (error "Priority commands are disabled"))
  11804. (setq action (or action 'set))
  11805. (let (current new news have remove)
  11806. (save-excursion
  11807. (org-back-to-heading t)
  11808. (if (looking-at org-priority-regexp)
  11809. (setq current (string-to-char (match-string 2))
  11810. have t))
  11811. (cond
  11812. ((eq action 'remove)
  11813. (setq remove t new ?\ ))
  11814. ((or (eq action 'set)
  11815. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11816. (if (not (eq action 'set))
  11817. (setq new action)
  11818. (message "Priority %c-%c, SPC to remove: "
  11819. org-highest-priority org-lowest-priority)
  11820. (save-match-data
  11821. (setq new (read-char-exclusive))))
  11822. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11823. (= (upcase org-lowest-priority) org-lowest-priority))
  11824. (setq new (upcase new)))
  11825. (cond ((equal new ?\ ) (setq remove t))
  11826. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11827. (error "Priority must be between `%c' and `%c'"
  11828. org-highest-priority org-lowest-priority))))
  11829. ((eq action 'up)
  11830. (setq new (if have
  11831. (1- current) ; normal cycling
  11832. ;; last priority was empty
  11833. (if (eq last-command this-command)
  11834. org-lowest-priority ; wrap around empty to lowest
  11835. ;; default
  11836. (if org-priority-start-cycle-with-default
  11837. org-default-priority
  11838. (1- org-default-priority))))))
  11839. ((eq action 'down)
  11840. (setq new (if have
  11841. (1+ current) ; normal cycling
  11842. ;; last priority was empty
  11843. (if (eq last-command this-command)
  11844. org-highest-priority ; wrap around empty to highest
  11845. ;; default
  11846. (if org-priority-start-cycle-with-default
  11847. org-default-priority
  11848. (1+ org-default-priority))))))
  11849. (t (error "Invalid action")))
  11850. (if (or (< (upcase new) org-highest-priority)
  11851. (> (upcase new) org-lowest-priority))
  11852. (if (and (memq action '(up down))
  11853. (not have) (not (eq last-command this-command)))
  11854. ;; `new' is from default priority
  11855. (error
  11856. "The default can not be set, see `org-default-priority' why")
  11857. ;; normal cycling: `new' is beyond highest/lowest priority
  11858. ;; and is wrapped around to the empty priority
  11859. (setq remove t)))
  11860. (setq news (format "%c" new))
  11861. (if have
  11862. (if remove
  11863. (replace-match "" t t nil 1)
  11864. (replace-match news t t nil 2))
  11865. (if remove
  11866. (error "No priority cookie found in line")
  11867. (let ((case-fold-search nil))
  11868. (looking-at org-todo-line-regexp))
  11869. (if (match-end 2)
  11870. (progn
  11871. (goto-char (match-end 2))
  11872. (insert " [#" news "]"))
  11873. (goto-char (match-beginning 3))
  11874. (insert "[#" news "] "))))
  11875. (org-preserve-lc (org-set-tags nil 'align)))
  11876. (if remove
  11877. (message "Priority removed")
  11878. (message "Priority of current item set to %s" news)))))
  11879. (defun org-show-priority ()
  11880. "Show the priority of the current item.
  11881. This priority is composed of the main priority given with the [#A] cookies,
  11882. and by additional input from the age of a schedules or deadline entry."
  11883. (interactive)
  11884. (let ((pri (if (eq major-mode 'org-agenda-mode)
  11885. (org-get-at-bol 'priority)
  11886. (save-excursion
  11887. (save-match-data
  11888. (beginning-of-line)
  11889. (and (looking-at org-heading-regexp)
  11890. (org-get-priority (match-string 0))))))))
  11891. (message "Priority is %d" (if pri pri -1000))))
  11892. (defun org-get-priority (s)
  11893. "Find priority cookie and return priority."
  11894. (if (functionp org-get-priority-function)
  11895. (funcall org-get-priority-function)
  11896. (save-match-data
  11897. (if (not (string-match org-priority-regexp s))
  11898. (* 1000 (- org-lowest-priority org-default-priority))
  11899. (* 1000 (- org-lowest-priority
  11900. (string-to-char (match-string 2 s))))))))
  11901. ;;;; Tags
  11902. (defvar org-agenda-archives-mode)
  11903. (defvar org-map-continue-from nil
  11904. "Position from where mapping should continue.
  11905. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11906. (defvar org-scanner-tags nil
  11907. "The current tag list while the tags scanner is running.")
  11908. (defvar org-trust-scanner-tags nil
  11909. "Should `org-get-tags-at' use the tags for the scanner.
  11910. This is for internal dynamical scoping only.
  11911. When this is non-nil, the function `org-get-tags-at' will return the value
  11912. of `org-scanner-tags' instead of building the list by itself. This
  11913. can lead to large speed-ups when the tags scanner is used in a file with
  11914. many entries, and when the list of tags is retrieved, for example to
  11915. obtain a list of properties. Building the tags list for each entry in such
  11916. a file becomes an N^2 operation - but with this variable set, it scales
  11917. as N.")
  11918. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11919. "Scan headline tags with inheritance and produce output ACTION.
  11920. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11921. or `agenda' to produce an entry list for an agenda view. It can also be
  11922. a Lisp form or a function that should be called at each matched headline, in
  11923. this case the return value is a list of all return values from these calls.
  11924. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11925. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11926. only lines with a not-done TODO keyword are included in the output.
  11927. This should be the same variable that was scoped into
  11928. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11929. START-LEVEL can be a string with asterisks, reducing the scope to
  11930. headlines matching this string."
  11931. (require 'org-agenda)
  11932. (let* ((re (concat "^"
  11933. (if start-level
  11934. ;; Get the correct level to match
  11935. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11936. org-outline-regexp)
  11937. " *\\(\\<\\("
  11938. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11939. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11940. (props (list 'face 'default
  11941. 'done-face 'org-agenda-done
  11942. 'undone-face 'default
  11943. 'mouse-face 'highlight
  11944. 'org-not-done-regexp org-not-done-regexp
  11945. 'org-todo-regexp org-todo-regexp
  11946. 'org-complex-heading-regexp org-complex-heading-regexp
  11947. 'help-echo
  11948. (format "mouse-2 or RET jump to org file %s"
  11949. (abbreviate-file-name
  11950. (or (buffer-file-name (buffer-base-buffer))
  11951. (buffer-name (buffer-base-buffer)))))))
  11952. (case-fold-search nil)
  11953. (org-map-continue-from nil)
  11954. lspos tags tags-list
  11955. (tags-alist (list (cons 0 org-file-tags)))
  11956. (llast 0) rtn rtn1 level category i txt
  11957. todo marker entry priority)
  11958. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11959. (setq action (list 'lambda nil action)))
  11960. (save-excursion
  11961. (goto-char (point-min))
  11962. (when (eq action 'sparse-tree)
  11963. (org-overview)
  11964. (org-remove-occur-highlights))
  11965. (while (re-search-forward re nil t)
  11966. (setq org-map-continue-from nil)
  11967. (catch :skip
  11968. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11969. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11970. (goto-char (setq lspos (match-beginning 0)))
  11971. (setq level (org-reduced-level (org-outline-level))
  11972. category (org-get-category))
  11973. (setq i llast llast level)
  11974. ;; remove tag lists from same and sublevels
  11975. (while (>= i level)
  11976. (when (setq entry (assoc i tags-alist))
  11977. (setq tags-alist (delete entry tags-alist)))
  11978. (setq i (1- i)))
  11979. ;; add the next tags
  11980. (when tags
  11981. (setq tags (org-split-string tags ":")
  11982. tags-alist
  11983. (cons (cons level tags) tags-alist)))
  11984. ;; compile tags for current headline
  11985. (setq tags-list
  11986. (if org-use-tag-inheritance
  11987. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11988. tags)
  11989. org-scanner-tags tags-list)
  11990. (when org-use-tag-inheritance
  11991. (setcdr (car tags-alist)
  11992. (mapcar (lambda (x)
  11993. (setq x (copy-sequence x))
  11994. (org-add-prop-inherited x))
  11995. (cdar tags-alist))))
  11996. (when (and tags org-use-tag-inheritance
  11997. (or (not (eq t org-use-tag-inheritance))
  11998. org-tags-exclude-from-inheritance))
  11999. ;; selective inheritance, remove uninherited ones
  12000. (setcdr (car tags-alist)
  12001. (org-remove-uninherited-tags (cdar tags-alist))))
  12002. (when (and
  12003. ;; eval matcher only when the todo condition is OK
  12004. (and (or (not todo-only) (member todo org-not-done-keywords))
  12005. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12006. (eval matcher)))
  12007. ;; Call the skipper, but return t if it does not skip,
  12008. ;; so that the `and' form continues evaluating
  12009. (progn
  12010. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12011. t)
  12012. ;; Check if timestamps are deselecting this entry
  12013. (or (not todo-only)
  12014. (and (member todo org-not-done-keywords)
  12015. (or (not org-agenda-tags-todo-honor-ignore-options)
  12016. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  12017. ;; Extra check for the archive tag
  12018. ;; FIXME: Does the skipper already do this????
  12019. (or
  12020. (not (member org-archive-tag tags-list))
  12021. ;; we have an archive tag, should we use this anyway?
  12022. (or (not org-agenda-skip-archived-trees)
  12023. (and (eq action 'agenda) org-agenda-archives-mode))))
  12024. ;; select this headline
  12025. (cond
  12026. ((eq action 'sparse-tree)
  12027. (and org-highlight-sparse-tree-matches
  12028. (org-get-heading) (match-end 0)
  12029. (org-highlight-new-match
  12030. (match-beginning 1) (match-end 1)))
  12031. (org-show-context 'tags-tree))
  12032. ((eq action 'agenda)
  12033. (setq txt (org-agenda-format-item
  12034. ""
  12035. (concat
  12036. (if (eq org-tags-match-list-sublevels 'indented)
  12037. (make-string (1- level) ?.) "")
  12038. (org-get-heading))
  12039. level category
  12040. tags-list)
  12041. priority (org-get-priority txt))
  12042. (goto-char lspos)
  12043. (setq marker (org-agenda-new-marker))
  12044. (org-add-props txt props
  12045. 'org-marker marker 'org-hd-marker marker 'org-category category
  12046. 'todo-state todo
  12047. 'priority priority 'type "tagsmatch")
  12048. (push txt rtn))
  12049. ((functionp action)
  12050. (setq org-map-continue-from nil)
  12051. (save-excursion
  12052. (setq rtn1 (funcall action))
  12053. (push rtn1 rtn)))
  12054. (t (error "Invalid action")))
  12055. ;; if we are to skip sublevels, jump to end of subtree
  12056. (unless org-tags-match-list-sublevels
  12057. (org-end-of-subtree t)
  12058. (backward-char 1))))
  12059. ;; Get the correct position from where to continue
  12060. (if org-map-continue-from
  12061. (goto-char org-map-continue-from)
  12062. (and (= (point) lspos) (end-of-line 1)))))
  12063. (when (and (eq action 'sparse-tree)
  12064. (not org-sparse-tree-open-archived-trees))
  12065. (org-hide-archived-subtrees (point-min) (point-max)))
  12066. (nreverse rtn)))
  12067. (defun org-remove-uninherited-tags (tags)
  12068. "Remove all tags that are not inherited from the list TAGS."
  12069. (cond
  12070. ((eq org-use-tag-inheritance t)
  12071. (if org-tags-exclude-from-inheritance
  12072. (org-delete-all org-tags-exclude-from-inheritance tags)
  12073. tags))
  12074. ((not org-use-tag-inheritance) nil)
  12075. ((stringp org-use-tag-inheritance)
  12076. (delq nil (mapcar
  12077. (lambda (x)
  12078. (if (and (string-match org-use-tag-inheritance x)
  12079. (not (member x org-tags-exclude-from-inheritance)))
  12080. x nil))
  12081. tags)))
  12082. ((listp org-use-tag-inheritance)
  12083. (delq nil (mapcar
  12084. (lambda (x)
  12085. (if (member x org-use-tag-inheritance) x nil))
  12086. tags)))))
  12087. (defun org-match-sparse-tree (&optional todo-only match)
  12088. "Create a sparse tree according to tags string MATCH.
  12089. MATCH can contain positive and negative selection of tags, like
  12090. \"+WORK+URGENT-WITHBOSS\".
  12091. If optional argument TODO-ONLY is non-nil, only select lines that are
  12092. also TODO lines."
  12093. (interactive "P")
  12094. (org-agenda-prepare-buffers (list (current-buffer)))
  12095. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12096. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12097. (defvar org-cached-props nil)
  12098. (defun org-cached-entry-get (pom property)
  12099. (if (or (eq t org-use-property-inheritance)
  12100. (and (stringp org-use-property-inheritance)
  12101. (string-match org-use-property-inheritance property))
  12102. (and (listp org-use-property-inheritance)
  12103. (member property org-use-property-inheritance)))
  12104. ;; Caching is not possible, check it directly
  12105. (org-entry-get pom property 'inherit)
  12106. ;; Get all properties, so that we can do complicated checks easily
  12107. (cdr (assoc property (or org-cached-props
  12108. (setq org-cached-props
  12109. (org-entry-properties pom)))))))
  12110. (defun org-global-tags-completion-table (&optional files)
  12111. "Return the list of all tags in all agenda buffer/files.
  12112. Optional FILES argument is a list of files which can be used
  12113. instead of the agenda files."
  12114. (save-excursion
  12115. (org-uniquify
  12116. (delq nil
  12117. (apply 'append
  12118. (mapcar
  12119. (lambda (file)
  12120. (set-buffer (find-file-noselect file))
  12121. (append (org-get-buffer-tags)
  12122. (mapcar (lambda (x) (if (stringp (car-safe x))
  12123. (list (car-safe x)) nil))
  12124. org-tag-alist)))
  12125. (if (and files (car files))
  12126. files
  12127. (org-agenda-files))))))))
  12128. (defun org-make-tags-matcher (match)
  12129. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12130. The variable `todo-only' is scoped dynamically into this function.
  12131. It will be set to t if the matcher restricts matching to TODO entries,
  12132. otherwise will not be touched.
  12133. Returns a cons of the selection string MATCH and the constructed
  12134. lisp form implementing the matcher. The matcher is to be evaluated
  12135. at an Org entry, with point on the headline, and returns t if the
  12136. entry matches the selection string MATCH. The returned lisp form
  12137. references two variables with information about the entry, which
  12138. must be bound around the form's evaluation: todo, the TODO keyword
  12139. at the entry (or nil of none); and tags-list, the list of all tags
  12140. at the entry including inherited ones. Additionally, the category
  12141. of the entry (if any) must be specified as the text property
  12142. 'org-category on the headline.
  12143. See also `org-scan-tags'.
  12144. "
  12145. (declare (special todo-only))
  12146. (unless (boundp 'todo-only)
  12147. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12148. (unless match
  12149. ;; Get a new match request, with completion
  12150. (let ((org-last-tags-completion-table
  12151. (org-global-tags-completion-table)))
  12152. (setq match (org-completing-read-no-i
  12153. "Match: " 'org-tags-completion-function nil nil nil
  12154. 'org-tags-history))))
  12155. ;; Parse the string and create a lisp form
  12156. (let ((match0 match)
  12157. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12158. minus tag mm
  12159. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12160. orterms term orlist re-p str-p level-p level-op time-p
  12161. prop-p pn pv po gv rest)
  12162. (if (string-match "/+" match)
  12163. ;; match contains also a todo-matching request
  12164. (progn
  12165. (setq tagsmatch (substring match 0 (match-beginning 0))
  12166. todomatch (substring match (match-end 0)))
  12167. (if (string-match "^!" todomatch)
  12168. (setq todo-only t todomatch (substring todomatch 1)))
  12169. (if (string-match "^\\s-*$" todomatch)
  12170. (setq todomatch nil)))
  12171. ;; only matching tags
  12172. (setq tagsmatch match todomatch nil))
  12173. ;; Make the tags matcher
  12174. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12175. (setq tagsmatcher t)
  12176. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12177. (while (setq term (pop orterms))
  12178. (while (and (equal (substring term -1) "\\") orterms)
  12179. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12180. (while (string-match re term)
  12181. (setq rest (substring term (match-end 0))
  12182. minus (and (match-end 1)
  12183. (equal (match-string 1 term) "-"))
  12184. tag (save-match-data (replace-regexp-in-string
  12185. "\\\\-" "-"
  12186. (match-string 2 term)))
  12187. re-p (equal (string-to-char tag) ?{)
  12188. level-p (match-end 4)
  12189. prop-p (match-end 5)
  12190. mm (cond
  12191. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12192. (level-p
  12193. (setq level-op (org-op-to-function (match-string 3 term)))
  12194. `(,level-op level ,(string-to-number
  12195. (match-string 4 term))))
  12196. (prop-p
  12197. (setq pn (match-string 5 term)
  12198. po (match-string 6 term)
  12199. pv (match-string 7 term)
  12200. re-p (equal (string-to-char pv) ?{)
  12201. str-p (equal (string-to-char pv) ?\")
  12202. time-p (save-match-data
  12203. (string-match "^\"[[<].*[]>]\"$" pv))
  12204. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12205. (if time-p (setq pv (org-matcher-time pv)))
  12206. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12207. (cond
  12208. ((equal pn "CATEGORY")
  12209. (setq gv '(get-text-property (point) 'org-category)))
  12210. ((equal pn "TODO")
  12211. (setq gv 'todo))
  12212. (t
  12213. (setq gv `(org-cached-entry-get nil ,pn))))
  12214. (if re-p
  12215. (if (eq po 'org<>)
  12216. `(not (string-match ,pv (or ,gv "")))
  12217. `(string-match ,pv (or ,gv "")))
  12218. (if str-p
  12219. `(,po (or ,gv "") ,pv)
  12220. `(,po (string-to-number (or ,gv ""))
  12221. ,(string-to-number pv) ))))
  12222. (t `(member ,tag tags-list)))
  12223. mm (if minus (list 'not mm) mm)
  12224. term rest)
  12225. (push mm tagsmatcher))
  12226. (push (if (> (length tagsmatcher) 1)
  12227. (cons 'and tagsmatcher)
  12228. (car tagsmatcher))
  12229. orlist)
  12230. (setq tagsmatcher nil))
  12231. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12232. (setq tagsmatcher
  12233. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12234. ;; Make the todo matcher
  12235. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12236. (setq todomatcher t)
  12237. (setq orterms (org-split-string todomatch "|") orlist nil)
  12238. (while (setq term (pop orterms))
  12239. (while (string-match re term)
  12240. (setq minus (and (match-end 1)
  12241. (equal (match-string 1 term) "-"))
  12242. kwd (match-string 2 term)
  12243. re-p (equal (string-to-char kwd) ?{)
  12244. term (substring term (match-end 0))
  12245. mm (if re-p
  12246. `(string-match ,(substring kwd 1 -1) todo)
  12247. (list 'equal 'todo kwd))
  12248. mm (if minus (list 'not mm) mm))
  12249. (push mm todomatcher))
  12250. (push (if (> (length todomatcher) 1)
  12251. (cons 'and todomatcher)
  12252. (car todomatcher))
  12253. orlist)
  12254. (setq todomatcher nil))
  12255. (setq todomatcher (if (> (length orlist) 1)
  12256. (cons 'or orlist) (car orlist))))
  12257. ;; Return the string and lisp forms of the matcher
  12258. (setq matcher (if todomatcher
  12259. (list 'and tagsmatcher todomatcher)
  12260. tagsmatcher))
  12261. (when todo-only
  12262. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12263. matcher)))
  12264. (cons match0 matcher)))
  12265. (defun org-op-to-function (op &optional stringp)
  12266. "Turn an operator into the appropriate function."
  12267. (setq op
  12268. (cond
  12269. ((equal op "<" ) '(< string< org-time<))
  12270. ((equal op ">" ) '(> org-string> org-time>))
  12271. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12272. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12273. ((member op '("=" "==")) '(= string= org-time=))
  12274. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12275. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12276. (defun org<> (a b) (not (= a b)))
  12277. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12278. (defun org-string>= (a b) (not (string< a b)))
  12279. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12280. (defun org-string<> (a b) (not (string= a b)))
  12281. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12282. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12283. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12284. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12285. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12286. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12287. (defun org-2ft (s)
  12288. "Convert S to a floating point time.
  12289. If S is already a number, just return it. If it is a string, parse
  12290. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12291. (cond
  12292. ((numberp s) s)
  12293. ((stringp s)
  12294. (condition-case nil
  12295. (float-time (apply 'encode-time (org-parse-time-string s)))
  12296. (error 0.)))
  12297. (t 0.)))
  12298. (defun org-time-today ()
  12299. "Time in seconds today at 0:00.
  12300. Returns the float number of seconds since the beginning of the
  12301. epoch to the beginning of today (00:00)."
  12302. (float-time (apply 'encode-time
  12303. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12304. (defun org-matcher-time (s)
  12305. "Interpret a time comparison value."
  12306. (save-match-data
  12307. (cond
  12308. ((string= s "<now>") (float-time))
  12309. ((string= s "<today>") (org-time-today))
  12310. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12311. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12312. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12313. (+ (org-time-today)
  12314. (* (string-to-number (match-string 1 s))
  12315. (cdr (assoc (match-string 2 s)
  12316. '(("d" . 86400.0) ("w" . 604800.0)
  12317. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12318. (t (org-2ft s)))))
  12319. (defun org-match-any-p (re list)
  12320. "Does re match any element of list?"
  12321. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12322. (delq nil list))
  12323. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12324. (defvar org-tags-overlay (make-overlay 1 1))
  12325. (org-detach-overlay org-tags-overlay)
  12326. (defun org-get-local-tags-at (&optional pos)
  12327. "Get a list of tags defined in the current headline."
  12328. (org-get-tags-at pos 'local))
  12329. (defun org-get-local-tags ()
  12330. "Get a list of tags defined in the current headline."
  12331. (org-get-tags-at nil 'local))
  12332. (defun org-get-tags-at (&optional pos local)
  12333. "Get a list of all headline tags applicable at POS.
  12334. POS defaults to point. If tags are inherited, the list contains
  12335. the targets in the same sequence as the headlines appear, i.e.
  12336. the tags of the current headline come last.
  12337. When LOCAL is non-nil, only return tags from the current headline,
  12338. ignore inherited ones."
  12339. (interactive)
  12340. (if (and org-trust-scanner-tags
  12341. (or (not pos) (equal pos (point)))
  12342. (not local))
  12343. org-scanner-tags
  12344. (let (tags ltags lastpos parent)
  12345. (save-excursion
  12346. (save-restriction
  12347. (widen)
  12348. (goto-char (or pos (point)))
  12349. (save-match-data
  12350. (catch 'done
  12351. (condition-case nil
  12352. (progn
  12353. (org-back-to-heading t)
  12354. (while (not (equal lastpos (point)))
  12355. (setq lastpos (point))
  12356. (when (looking-at
  12357. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12358. (setq ltags (org-split-string
  12359. (org-match-string-no-properties 1) ":"))
  12360. (when parent
  12361. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12362. (setq tags (append
  12363. (if parent
  12364. (org-remove-uninherited-tags ltags)
  12365. ltags)
  12366. tags)))
  12367. (or org-use-tag-inheritance (throw 'done t))
  12368. (if local (throw 'done t))
  12369. (or (org-up-heading-safe) (error nil))
  12370. (setq parent t)))
  12371. (error nil)))))
  12372. (if local
  12373. tags
  12374. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12375. (defun org-add-prop-inherited (s)
  12376. (add-text-properties 0 (length s) '(inherited t) s)
  12377. s)
  12378. (defun org-toggle-tag (tag &optional onoff)
  12379. "Toggle the tag TAG for the current line.
  12380. If ONOFF is `on' or `off', don't toggle but set to this state."
  12381. (let (res current)
  12382. (save-excursion
  12383. (org-back-to-heading t)
  12384. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12385. (point-at-eol) t)
  12386. (progn
  12387. (setq current (match-string 1))
  12388. (replace-match ""))
  12389. (setq current ""))
  12390. (setq current (nreverse (org-split-string current ":")))
  12391. (cond
  12392. ((eq onoff 'on)
  12393. (setq res t)
  12394. (or (member tag current) (push tag current)))
  12395. ((eq onoff 'off)
  12396. (or (not (member tag current)) (setq current (delete tag current))))
  12397. (t (if (member tag current)
  12398. (setq current (delete tag current))
  12399. (setq res t)
  12400. (push tag current))))
  12401. (end-of-line 1)
  12402. (if current
  12403. (progn
  12404. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12405. (org-set-tags nil t))
  12406. (delete-horizontal-space))
  12407. (run-hooks 'org-after-tags-change-hook))
  12408. res))
  12409. (defun org-align-tags-here (to-col)
  12410. ;; Assumes that this is a headline
  12411. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12412. (beginning-of-line 1)
  12413. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12414. (< pos (match-beginning 2)))
  12415. (progn
  12416. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12417. (goto-char (match-beginning 1))
  12418. (insert " ")
  12419. (delete-region (point) (1+ (match-beginning 2)))
  12420. (setq ncol (max (current-column)
  12421. (1+ col)
  12422. (if (> to-col 0)
  12423. to-col
  12424. (- (abs to-col) tags-l))))
  12425. (setq p (point))
  12426. (insert (make-string (- ncol (current-column)) ?\ ))
  12427. (setq ncol (current-column))
  12428. (when indent-tabs-mode (tabify p (point-at-eol)))
  12429. (org-move-to-column (min ncol col) t))
  12430. (goto-char pos))))
  12431. (defun org-set-tags-command (&optional arg just-align)
  12432. "Call the set-tags command for the current entry."
  12433. (interactive "P")
  12434. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12435. (org-set-tags arg just-align)
  12436. (save-excursion
  12437. (org-back-to-heading t)
  12438. (org-set-tags arg just-align))))
  12439. (defun org-set-tags-to (data)
  12440. "Set the tags of the current entry to DATA, replacing the current tags.
  12441. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12442. If DATA is nil or the empty string, any tags will be removed."
  12443. (interactive "sTags: ")
  12444. (setq data
  12445. (cond
  12446. ((eq data nil) "")
  12447. ((equal data "") "")
  12448. ((stringp data)
  12449. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12450. ":"))
  12451. ((listp data)
  12452. (concat ":" (mapconcat 'identity data ":") ":"))))
  12453. (when data
  12454. (save-excursion
  12455. (org-back-to-heading t)
  12456. (when (looking-at org-complex-heading-regexp)
  12457. (if (match-end 5)
  12458. (progn
  12459. (goto-char (match-beginning 5))
  12460. (insert data)
  12461. (delete-region (point) (point-at-eol))
  12462. (org-set-tags nil 'align))
  12463. (goto-char (point-at-eol))
  12464. (insert " " data)
  12465. (org-set-tags nil 'align)))
  12466. (beginning-of-line 1)
  12467. (if (looking-at ".*?\\([ \t]+\\)$")
  12468. (delete-region (match-beginning 1) (match-end 1))))))
  12469. (defun org-align-all-tags ()
  12470. "Align the tags i all headings."
  12471. (interactive)
  12472. (save-excursion
  12473. (or (ignore-errors (org-back-to-heading t))
  12474. (outline-next-heading))
  12475. (if (org-at-heading-p)
  12476. (org-set-tags t)
  12477. (message "No headings"))))
  12478. (defvar org-indent-indentation-per-level)
  12479. (defun org-set-tags (&optional arg just-align)
  12480. "Set the tags for the current headline.
  12481. With prefix ARG, realign all tags in headings in the current buffer."
  12482. (interactive "P")
  12483. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12484. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12485. 'region-start-level 'region))
  12486. org-loop-over-headlines-in-active-region)
  12487. (org-map-entries
  12488. ;; We don't use ARG and JUST-ALIGN here these args are not
  12489. ;; useful when looping over headlines
  12490. `(org-set-tags)
  12491. org-loop-over-headlines-in-active-region
  12492. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12493. (let* ((re org-outline-regexp-bol)
  12494. (current (unless arg (org-get-tags-string)))
  12495. (col (current-column))
  12496. (org-setting-tags t)
  12497. table current-tags inherited-tags ; computed below when needed
  12498. tags p0 c0 c1 rpl di tc level)
  12499. (if arg
  12500. (save-excursion
  12501. (goto-char (point-min))
  12502. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12503. (while (re-search-forward re nil t)
  12504. (org-set-tags nil t)
  12505. (end-of-line 1)))
  12506. (message "All tags realigned to column %d" org-tags-column))
  12507. (if just-align
  12508. (setq tags current)
  12509. ;; Get a new set of tags from the user
  12510. (save-excursion
  12511. (setq table (append org-tag-persistent-alist
  12512. (or org-tag-alist (org-get-buffer-tags))
  12513. (and
  12514. org-complete-tags-always-offer-all-agenda-tags
  12515. (org-global-tags-completion-table
  12516. (org-agenda-files))))
  12517. org-last-tags-completion-table table
  12518. current-tags (org-split-string current ":")
  12519. inherited-tags (nreverse
  12520. (nthcdr (length current-tags)
  12521. (nreverse (org-get-tags-at))))
  12522. tags
  12523. (if (or (eq t org-use-fast-tag-selection)
  12524. (and org-use-fast-tag-selection
  12525. (delq nil (mapcar 'cdr table))))
  12526. (org-fast-tag-selection
  12527. current-tags inherited-tags table
  12528. (if org-fast-tag-selection-include-todo
  12529. org-todo-key-alist))
  12530. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12531. (org-trim
  12532. (org-icompleting-read "Tags: "
  12533. 'org-tags-completion-function
  12534. nil nil current 'org-tags-history))))))
  12535. (while (string-match "[-+&]+" tags)
  12536. ;; No boolean logic, just a list
  12537. (setq tags (replace-match ":" t t tags))))
  12538. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12539. (if org-tags-sort-function
  12540. (setq tags (mapconcat 'identity
  12541. (sort (org-split-string
  12542. tags (org-re "[^[:alnum:]_@#%]+"))
  12543. org-tags-sort-function) ":")))
  12544. (if (string-match "\\`[\t ]*\\'" tags)
  12545. (setq tags "")
  12546. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12547. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12548. ;; Insert new tags at the correct column
  12549. (beginning-of-line 1)
  12550. (setq level (or (and (looking-at org-outline-regexp)
  12551. (- (match-end 0) (point) 1))
  12552. 1))
  12553. (cond
  12554. ((and (equal current "") (equal tags "")))
  12555. ((re-search-forward
  12556. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12557. (point-at-eol) t)
  12558. (if (equal tags "")
  12559. (setq rpl "")
  12560. (goto-char (match-beginning 0))
  12561. (setq c0 (current-column)
  12562. ;; compute offset for the case of org-indent-mode active
  12563. di (if org-indent-mode
  12564. (* (1- org-indent-indentation-per-level) (1- level))
  12565. 0)
  12566. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12567. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12568. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12569. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12570. (replace-match rpl t t)
  12571. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12572. tags)
  12573. (t (error "Tags alignment failed")))
  12574. (org-move-to-column col)
  12575. (unless just-align
  12576. (run-hooks 'org-after-tags-change-hook))))))
  12577. (defun org-change-tag-in-region (beg end tag off)
  12578. "Add or remove TAG for each entry in the region.
  12579. This works in the agenda, and also in an org-mode buffer."
  12580. (interactive
  12581. (list (region-beginning) (region-end)
  12582. (let ((org-last-tags-completion-table
  12583. (if (derived-mode-p 'org-mode)
  12584. (org-get-buffer-tags)
  12585. (org-global-tags-completion-table))))
  12586. (org-icompleting-read
  12587. "Tag: " 'org-tags-completion-function nil nil nil
  12588. 'org-tags-history))
  12589. (progn
  12590. (message "[s]et or [r]emove? ")
  12591. (equal (read-char-exclusive) ?r))))
  12592. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12593. (let ((agendap (equal major-mode 'org-agenda-mode))
  12594. l1 l2 m buf pos newhead (cnt 0))
  12595. (goto-char end)
  12596. (setq l2 (1- (org-current-line)))
  12597. (goto-char beg)
  12598. (setq l1 (org-current-line))
  12599. (loop for l from l1 to l2 do
  12600. (org-goto-line l)
  12601. (setq m (get-text-property (point) 'org-hd-marker))
  12602. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12603. (and agendap m))
  12604. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12605. pos (if agendap m (point)))
  12606. (with-current-buffer buf
  12607. (save-excursion
  12608. (save-restriction
  12609. (goto-char pos)
  12610. (setq cnt (1+ cnt))
  12611. (org-toggle-tag tag (if off 'off 'on))
  12612. (setq newhead (org-get-heading)))))
  12613. (and agendap (org-agenda-change-all-lines newhead m))))
  12614. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12615. (defun org-tags-completion-function (string predicate &optional flag)
  12616. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12617. (confirm (lambda (x) (stringp (car x)))))
  12618. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12619. (setq s1 (match-string 1 string)
  12620. s2 (match-string 2 string))
  12621. (setq s1 "" s2 string))
  12622. (cond
  12623. ((eq flag nil)
  12624. ;; try completion
  12625. (setq rtn (try-completion s2 ctable confirm))
  12626. (if (stringp rtn)
  12627. (setq rtn
  12628. (concat s1 s2 (substring rtn (length s2))
  12629. (if (and org-add-colon-after-tag-completion
  12630. (assoc rtn ctable))
  12631. ":" ""))))
  12632. rtn)
  12633. ((eq flag t)
  12634. ;; all-completions
  12635. (all-completions s2 ctable confirm)
  12636. )
  12637. ((eq flag 'lambda)
  12638. ;; exact match?
  12639. (assoc s2 ctable)))
  12640. ))
  12641. (defun org-fast-tag-insert (kwd tags face &optional end)
  12642. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12643. (insert (format "%-12s" (concat kwd ":"))
  12644. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12645. (or end "")))
  12646. (defun org-fast-tag-show-exit (flag)
  12647. (save-excursion
  12648. (org-goto-line 3)
  12649. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12650. (replace-match ""))
  12651. (when flag
  12652. (end-of-line 1)
  12653. (org-move-to-column (- (window-width) 19) t)
  12654. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12655. (defun org-set-current-tags-overlay (current prefix)
  12656. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12657. (if (featurep 'xemacs)
  12658. (org-overlay-display org-tags-overlay (concat prefix s)
  12659. 'secondary-selection)
  12660. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12661. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12662. (defvar org-last-tag-selection-key nil)
  12663. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12664. "Fast tag selection with single keys.
  12665. CURRENT is the current list of tags in the headline, INHERITED is the
  12666. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12667. possibly with grouping information. TODO-TABLE is a similar table with
  12668. TODO keywords, should these have keys assigned to them.
  12669. If the keys are nil, a-z are automatically assigned.
  12670. Returns the new tags string, or nil to not change the current settings."
  12671. (let* ((fulltable (append table todo-table))
  12672. (maxlen (apply 'max (mapcar
  12673. (lambda (x)
  12674. (if (stringp (car x)) (string-width (car x)) 0))
  12675. fulltable)))
  12676. (buf (current-buffer))
  12677. (expert (eq org-fast-tag-selection-single-key 'expert))
  12678. (buffer-tags nil)
  12679. (fwidth (+ maxlen 3 1 3))
  12680. (ncol (/ (- (window-width) 4) fwidth))
  12681. (i-face 'org-done)
  12682. (c-face 'org-todo)
  12683. tg cnt e c char c1 c2 ntable tbl rtn
  12684. ov-start ov-end ov-prefix
  12685. (exit-after-next org-fast-tag-selection-single-key)
  12686. (done-keywords org-done-keywords)
  12687. groups ingroup)
  12688. (save-excursion
  12689. (beginning-of-line 1)
  12690. (if (looking-at
  12691. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12692. (setq ov-start (match-beginning 1)
  12693. ov-end (match-end 1)
  12694. ov-prefix "")
  12695. (setq ov-start (1- (point-at-eol))
  12696. ov-end (1+ ov-start))
  12697. (skip-chars-forward "^\n\r")
  12698. (setq ov-prefix
  12699. (concat
  12700. (buffer-substring (1- (point)) (point))
  12701. (if (> (current-column) org-tags-column)
  12702. " "
  12703. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12704. (move-overlay org-tags-overlay ov-start ov-end)
  12705. (save-window-excursion
  12706. (if expert
  12707. (set-buffer (get-buffer-create " *Org tags*"))
  12708. (delete-other-windows)
  12709. (split-window-vertically)
  12710. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12711. (erase-buffer)
  12712. (org-set-local 'org-done-keywords done-keywords)
  12713. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12714. (org-fast-tag-insert "Current" current c-face "\n\n")
  12715. (org-fast-tag-show-exit exit-after-next)
  12716. (org-set-current-tags-overlay current ov-prefix)
  12717. (setq tbl fulltable char ?a cnt 0)
  12718. (while (setq e (pop tbl))
  12719. (cond
  12720. ((equal (car e) :startgroup)
  12721. (push '() groups) (setq ingroup t)
  12722. (when (not (= cnt 0))
  12723. (setq cnt 0)
  12724. (insert "\n"))
  12725. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12726. ((equal (car e) :endgroup)
  12727. (setq ingroup nil cnt 0)
  12728. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12729. ((equal e '(:newline))
  12730. (when (not (= cnt 0))
  12731. (setq cnt 0)
  12732. (insert "\n")
  12733. (setq e (car tbl))
  12734. (while (equal (car tbl) '(:newline))
  12735. (insert "\n")
  12736. (setq tbl (cdr tbl)))))
  12737. (t
  12738. (setq tg (copy-sequence (car e)) c2 nil)
  12739. (if (cdr e)
  12740. (setq c (cdr e))
  12741. ;; automatically assign a character.
  12742. (setq c1 (string-to-char
  12743. (downcase (substring
  12744. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12745. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12746. (while (or (rassoc char ntable) (rassoc char table))
  12747. (setq char (1+ char)))
  12748. (setq c2 c1))
  12749. (setq c (or c2 char)))
  12750. (if ingroup (push tg (car groups)))
  12751. (setq tg (org-add-props tg nil 'face
  12752. (cond
  12753. ((not (assoc tg table))
  12754. (org-get-todo-face tg))
  12755. ((member tg current) c-face)
  12756. ((member tg inherited) i-face))))
  12757. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12758. (insert "[" c "] " tg (make-string
  12759. (- fwidth 4 (length tg)) ?\ ))
  12760. (push (cons tg c) ntable)
  12761. (when (= (setq cnt (1+ cnt)) ncol)
  12762. (insert "\n")
  12763. (if ingroup (insert " "))
  12764. (setq cnt 0)))))
  12765. (setq ntable (nreverse ntable))
  12766. (insert "\n")
  12767. (goto-char (point-min))
  12768. (if (not expert) (org-fit-window-to-buffer))
  12769. (setq rtn
  12770. (catch 'exit
  12771. (while t
  12772. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12773. (if (not groups) "no " "")
  12774. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12775. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12776. (setq org-last-tag-selection-key c)
  12777. (cond
  12778. ((= c ?\r) (throw 'exit t))
  12779. ((= c ?!)
  12780. (setq groups (not groups))
  12781. (goto-char (point-min))
  12782. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12783. ((= c ?\C-c)
  12784. (if (not expert)
  12785. (org-fast-tag-show-exit
  12786. (setq exit-after-next (not exit-after-next)))
  12787. (setq expert nil)
  12788. (delete-other-windows)
  12789. (set-window-buffer (split-window-vertically) " *Org tags*")
  12790. (org-switch-to-buffer-other-window " *Org tags*")
  12791. (org-fit-window-to-buffer)))
  12792. ((or (= c ?\C-g)
  12793. (and (= c ?q) (not (rassoc c ntable))))
  12794. (org-detach-overlay org-tags-overlay)
  12795. (setq quit-flag t))
  12796. ((= c ?\ )
  12797. (setq current nil)
  12798. (if exit-after-next (setq exit-after-next 'now)))
  12799. ((= c ?\t)
  12800. (condition-case nil
  12801. (setq tg (org-icompleting-read
  12802. "Tag: "
  12803. (or buffer-tags
  12804. (with-current-buffer buf
  12805. (org-get-buffer-tags)))))
  12806. (quit (setq tg "")))
  12807. (when (string-match "\\S-" tg)
  12808. (add-to-list 'buffer-tags (list tg))
  12809. (if (member tg current)
  12810. (setq current (delete tg current))
  12811. (push tg current)))
  12812. (if exit-after-next (setq exit-after-next 'now)))
  12813. ((setq e (rassoc c todo-table) tg (car e))
  12814. (with-current-buffer buf
  12815. (save-excursion (org-todo tg)))
  12816. (if exit-after-next (setq exit-after-next 'now)))
  12817. ((setq e (rassoc c ntable) tg (car e))
  12818. (if (member tg current)
  12819. (setq current (delete tg current))
  12820. (loop for g in groups do
  12821. (if (member tg g)
  12822. (mapc (lambda (x)
  12823. (setq current (delete x current)))
  12824. g)))
  12825. (push tg current))
  12826. (if exit-after-next (setq exit-after-next 'now))))
  12827. ;; Create a sorted list
  12828. (setq current
  12829. (sort current
  12830. (lambda (a b)
  12831. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12832. (if (eq exit-after-next 'now) (throw 'exit t))
  12833. (goto-char (point-min))
  12834. (beginning-of-line 2)
  12835. (delete-region (point) (point-at-eol))
  12836. (org-fast-tag-insert "Current" current c-face)
  12837. (org-set-current-tags-overlay current ov-prefix)
  12838. (while (re-search-forward
  12839. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12840. (setq tg (match-string 1))
  12841. (add-text-properties
  12842. (match-beginning 1) (match-end 1)
  12843. (list 'face
  12844. (cond
  12845. ((member tg current) c-face)
  12846. ((member tg inherited) i-face)
  12847. (t (get-text-property (match-beginning 1) 'face))))))
  12848. (goto-char (point-min)))))
  12849. (org-detach-overlay org-tags-overlay)
  12850. (if rtn
  12851. (mapconcat 'identity current ":")
  12852. nil))))
  12853. (defun org-get-tags-string ()
  12854. "Get the TAGS string in the current headline."
  12855. (unless (org-at-heading-p t)
  12856. (error "Not on a heading"))
  12857. (save-excursion
  12858. (beginning-of-line 1)
  12859. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12860. (org-match-string-no-properties 1)
  12861. "")))
  12862. (defun org-get-tags ()
  12863. "Get the list of tags specified in the current headline."
  12864. (org-split-string (org-get-tags-string) ":"))
  12865. (defun org-get-buffer-tags ()
  12866. "Get a table of all tags used in the buffer, for completion."
  12867. (let (tags)
  12868. (save-excursion
  12869. (goto-char (point-min))
  12870. (while (re-search-forward
  12871. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12872. (when (equal (char-after (point-at-bol 0)) ?*)
  12873. (mapc (lambda (x) (add-to-list 'tags x))
  12874. (org-split-string (org-match-string-no-properties 1) ":")))))
  12875. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12876. (mapcar 'list tags)))
  12877. ;;;; The mapping API
  12878. (defun org-map-entries (func &optional match scope &rest skip)
  12879. "Call FUNC at each headline selected by MATCH in SCOPE.
  12880. FUNC is a function or a lisp form. The function will be called without
  12881. arguments, with the cursor positioned at the beginning of the headline.
  12882. The return values of all calls to the function will be collected and
  12883. returned as a list.
  12884. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12885. does not need to preserve point. After evaluation, the cursor will be
  12886. moved to the end of the line (presumably of the headline of the
  12887. processed entry) and search continues from there. Under some
  12888. circumstances, this may not produce the wanted results. For example,
  12889. if you have removed (e.g. archived) the current (sub)tree it could
  12890. mean that the next entry will be skipped entirely. In such cases, you
  12891. can specify the position from where search should continue by making
  12892. FUNC set the variable `org-map-continue-from' to the desired buffer
  12893. position.
  12894. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12895. Only headlines that are matched by this query will be considered during
  12896. the iteration. When MATCH is nil or t, all headlines will be
  12897. visited by the iteration.
  12898. SCOPE determines the scope of this command. It can be any of:
  12899. nil The current buffer, respecting the restriction if any
  12900. tree The subtree started with the entry at point
  12901. region The entries within the active region, if any
  12902. region-start-level
  12903. The entries within the active region, but only those at
  12904. the same level than the first one.
  12905. file The current buffer, without restriction
  12906. file-with-archives
  12907. The current buffer, and any archives associated with it
  12908. agenda All agenda files
  12909. agenda-with-archives
  12910. All agenda files with any archive files associated with them
  12911. \(file1 file2 ...)
  12912. If this is a list, all files in the list will be scanned
  12913. The remaining args are treated as settings for the skipping facilities of
  12914. the scanner. The following items can be given here:
  12915. archive skip trees with the archive tag.
  12916. comment skip trees with the COMMENT keyword
  12917. function or Emacs Lisp form:
  12918. will be used as value for `org-agenda-skip-function', so whenever
  12919. the function returns t, FUNC will not be called for that
  12920. entry and search will continue from the point where the
  12921. function leaves it.
  12922. If your function needs to retrieve the tags including inherited tags
  12923. at the *current* entry, you can use the value of the variable
  12924. `org-scanner-tags' which will be much faster than getting the value
  12925. with `org-get-tags-at'. If your function gets properties with
  12926. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12927. to t around the call to `org-entry-properties' to get the same speedup.
  12928. Note that if your function moves around to retrieve tags and properties at
  12929. a *different* entry, you cannot use these techniques."
  12930. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12931. (not (org-region-active-p)))
  12932. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12933. (org-agenda-skip-archived-trees (memq 'archive skip))
  12934. (org-agenda-skip-comment-trees (memq 'comment skip))
  12935. (org-agenda-skip-function
  12936. (car (org-delete-all '(comment archive) skip)))
  12937. (org-tags-match-list-sublevels t)
  12938. (start-level (eq scope 'region-start-level))
  12939. matcher file res
  12940. org-todo-keywords-for-agenda
  12941. org-done-keywords-for-agenda
  12942. org-todo-keyword-alist-for-agenda
  12943. org-drawers-for-agenda
  12944. org-tag-alist-for-agenda
  12945. todo-only)
  12946. (cond
  12947. ((eq match t) (setq matcher t))
  12948. ((eq match nil) (setq matcher t))
  12949. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12950. (save-excursion
  12951. (save-restriction
  12952. (cond ((eq scope 'tree)
  12953. (org-back-to-heading t)
  12954. (org-narrow-to-subtree)
  12955. (setq scope nil))
  12956. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12957. (org-region-active-p))
  12958. ;; If needed, set start-level to a string like "2"
  12959. (when start-level
  12960. (save-excursion
  12961. (goto-char (region-beginning))
  12962. (unless (org-at-heading-p) (outline-next-heading))
  12963. (setq start-level (org-current-level))))
  12964. (narrow-to-region (region-beginning)
  12965. (save-excursion
  12966. (goto-char (region-end))
  12967. (unless (and (bolp) (org-at-heading-p))
  12968. (outline-next-heading))
  12969. (point)))
  12970. (setq scope nil)))
  12971. (if (not scope)
  12972. (progn
  12973. (org-agenda-prepare-buffers
  12974. (list (buffer-file-name (current-buffer))))
  12975. (setq res (org-scan-tags func matcher todo-only start-level)))
  12976. ;; Get the right scope
  12977. (cond
  12978. ((and scope (listp scope) (symbolp (car scope)))
  12979. (setq scope (eval scope)))
  12980. ((eq scope 'agenda)
  12981. (setq scope (org-agenda-files t)))
  12982. ((eq scope 'agenda-with-archives)
  12983. (setq scope (org-agenda-files t))
  12984. (setq scope (org-add-archive-files scope)))
  12985. ((eq scope 'file)
  12986. (setq scope (list (buffer-file-name))))
  12987. ((eq scope 'file-with-archives)
  12988. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12989. (org-agenda-prepare-buffers scope)
  12990. (while (setq file (pop scope))
  12991. (with-current-buffer (org-find-base-buffer-visiting file)
  12992. (save-excursion
  12993. (save-restriction
  12994. (widen)
  12995. (goto-char (point-min))
  12996. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12997. res)))
  12998. ;;;; Properties
  12999. ;;; Setting and retrieving properties
  13000. (defconst org-special-properties
  13001. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13002. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13003. "The special properties valid in Org-mode.
  13004. These are properties that are not defined in the property drawer,
  13005. but in some other way.")
  13006. (defconst org-default-properties
  13007. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13008. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13009. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13010. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13011. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13012. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13013. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13014. "Some properties that are used by Org-mode for various purposes.
  13015. Being in this list makes sure that they are offered for completion.")
  13016. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13017. "Regular expression matching the first line of a property drawer.")
  13018. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13019. "Regular expression matching the last line of a property drawer.")
  13020. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13021. "Regular expression matching the first line of a property drawer.")
  13022. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13023. "Regular expression matching the first line of a property drawer.")
  13024. (defconst org-property-drawer-re
  13025. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13026. org-property-end-re "\\)\n?")
  13027. "Matches an entire property drawer.")
  13028. (defconst org-clock-drawer-re
  13029. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13030. org-property-end-re "\\)\n?")
  13031. "Matches an entire clock drawer.")
  13032. (defsubst org-re-property (property)
  13033. "Return a regexp matching a PROPERTY line.
  13034. Match group 1 will be set to the value."
  13035. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13036. (defsubst org-re-property-keyword (property)
  13037. "Return a regexp matching a PROPERTY line, possibly with no
  13038. value for the property."
  13039. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13040. (defun org-property-action ()
  13041. "Do an action on properties."
  13042. (interactive)
  13043. (let (c)
  13044. (org-at-property-p)
  13045. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13046. (setq c (read-char-exclusive))
  13047. (cond
  13048. ((equal c ?s)
  13049. (call-interactively 'org-set-property))
  13050. ((equal c ?d)
  13051. (call-interactively 'org-delete-property))
  13052. ((equal c ?D)
  13053. (call-interactively 'org-delete-property-globally))
  13054. ((equal c ?c)
  13055. (call-interactively 'org-compute-property-at-point))
  13056. (t (error "No such property action %c" c)))))
  13057. (defun org-inc-effort ()
  13058. "Increment the value of the effort property in the current entry."
  13059. (interactive)
  13060. (org-set-effort nil t))
  13061. (defun org-set-effort (&optional value increment)
  13062. "Set the effort property of the current entry.
  13063. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13064. 10th allowed value.
  13065. When INCREMENT is non-nil, set the property to the next allowed value."
  13066. (interactive "P")
  13067. (if (equal value 0) (setq value 10))
  13068. (let* ((completion-ignore-case t)
  13069. (prop org-effort-property)
  13070. (cur (org-entry-get nil prop))
  13071. (allowed (org-property-get-allowed-values nil prop 'table))
  13072. (existing (mapcar 'list (org-property-values prop)))
  13073. rpl
  13074. (val (cond
  13075. ((stringp value) value)
  13076. ((and allowed (integerp value))
  13077. (or (car (nth (1- value) allowed))
  13078. (car (org-last allowed))))
  13079. ((and allowed increment)
  13080. (or (caadr (member (list cur) allowed))
  13081. (error "Allowed effort values are not set")))
  13082. (allowed
  13083. (message "Select 1-9,0, [RET%s]: %s"
  13084. (if cur (concat "=" cur) "")
  13085. (mapconcat 'car allowed " "))
  13086. (setq rpl (read-char-exclusive))
  13087. (if (equal rpl ?\r)
  13088. cur
  13089. (setq rpl (- rpl ?0))
  13090. (if (equal rpl 0) (setq rpl 10))
  13091. (if (and (> rpl 0) (<= rpl (length allowed)))
  13092. (car (nth (1- rpl) allowed))
  13093. (org-completing-read "Effort: " allowed nil))))
  13094. (t
  13095. (let (org-completion-use-ido org-completion-use-iswitchb)
  13096. (org-completing-read
  13097. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13098. (concat "[" cur "]") "")
  13099. ": ")
  13100. existing nil nil "" nil cur))))))
  13101. (unless (equal (org-entry-get nil prop) val)
  13102. (org-entry-put nil prop val))
  13103. (message "%s is now %s" prop val)))
  13104. (defun org-at-property-p ()
  13105. "Is cursor inside a property drawer?"
  13106. (save-excursion
  13107. (beginning-of-line 1)
  13108. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13109. (save-match-data ;; Used by calling procedures
  13110. (let ((p (point))
  13111. (range (unless (org-before-first-heading-p)
  13112. (org-get-property-block))))
  13113. (and range (<= (car range) p) (< p (cdr range))))))))
  13114. (defun org-get-property-block (&optional beg end force)
  13115. "Return the (beg . end) range of the body of the property drawer.
  13116. BEG and END are the beginning and end of the current subtree, or of
  13117. the part before the first headline. If they are not given, they will
  13118. be found. If the drawer does not exist and FORCE is non-nil, create
  13119. the drawer."
  13120. (catch 'exit
  13121. (save-excursion
  13122. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13123. (progn (org-back-to-heading t) (point))))
  13124. (end (or end (and (not (outline-next-heading)) (point-max))
  13125. (point))))
  13126. (goto-char beg)
  13127. (if (re-search-forward org-property-start-re end t)
  13128. (setq beg (1+ (match-end 0)))
  13129. (if force
  13130. (save-excursion
  13131. (org-insert-property-drawer)
  13132. (setq end (progn (outline-next-heading) (point))))
  13133. (throw 'exit nil))
  13134. (goto-char beg)
  13135. (if (re-search-forward org-property-start-re end t)
  13136. (setq beg (1+ (match-end 0)))))
  13137. (if (re-search-forward org-property-end-re end t)
  13138. (setq end (match-beginning 0))
  13139. (or force (throw 'exit nil))
  13140. (goto-char beg)
  13141. (setq end beg)
  13142. (org-indent-line)
  13143. (insert ":END:\n"))
  13144. (cons beg end)))))
  13145. (defun org-entry-properties (&optional pom which specific)
  13146. "Get all properties of the entry at point-or-marker POM.
  13147. This includes the TODO keyword, the tags, time strings for deadline,
  13148. scheduled, and clocking, and any additional properties defined in the
  13149. entry. The return value is an alist, keys may occur multiple times
  13150. if the property key was used several times.
  13151. POM may also be nil, in which case the current entry is used.
  13152. If WHICH is nil or `all', get all properties. If WHICH is
  13153. `special' or `standard', only get that subclass. If WHICH
  13154. is a string only get exactly this property. SPECIFIC can be a string, the
  13155. specific property we are interested in. Specifying it can speed
  13156. things up because then unnecessary parsing is avoided."
  13157. (setq which (or which 'all))
  13158. (org-with-point-at pom
  13159. (let ((clockstr (substring org-clock-string 0 -1))
  13160. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13161. (case-fold-search nil)
  13162. beg end range props sum-props key key1 value string clocksum clocksumt)
  13163. (save-excursion
  13164. (when (condition-case nil
  13165. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13166. (error nil))
  13167. (setq beg (point))
  13168. (setq sum-props (get-text-property (point) 'org-summaries))
  13169. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13170. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13171. (outline-next-heading)
  13172. (setq end (point))
  13173. (when (memq which '(all special))
  13174. ;; Get the special properties, like TODO and tags
  13175. (goto-char beg)
  13176. (when (and (or (not specific) (string= specific "TODO"))
  13177. (looking-at org-todo-line-regexp) (match-end 2))
  13178. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13179. (when (and (or (not specific) (string= specific "PRIORITY"))
  13180. (looking-at org-priority-regexp))
  13181. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13182. (when (or (not specific) (string= specific "FILE"))
  13183. (push (cons "FILE" buffer-file-name) props))
  13184. (when (and (or (not specific) (string= specific "TAGS"))
  13185. (setq value (org-get-tags-string))
  13186. (string-match "\\S-" value))
  13187. (push (cons "TAGS" value) props))
  13188. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13189. (setq value (org-get-tags-at)))
  13190. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13191. ":"))
  13192. props))
  13193. (when (or (not specific) (string= specific "BLOCKED"))
  13194. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13195. (when (or (not specific)
  13196. (member specific
  13197. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13198. "TIMESTAMP" "TIMESTAMP_IA")))
  13199. (catch 'match
  13200. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13201. (setq key (if (match-end 1)
  13202. (substring (org-match-string-no-properties 1)
  13203. 0 -1))
  13204. string (if (equal key clockstr)
  13205. (org-trim
  13206. (buffer-substring-no-properties
  13207. (match-beginning 3) (goto-char
  13208. (point-at-eol))))
  13209. (substring (org-match-string-no-properties 3)
  13210. 1 -1)))
  13211. ;; Get the correct property name from the key. This is
  13212. ;; necessary if the user has configured time keywords.
  13213. (setq key1 (concat key ":"))
  13214. (cond
  13215. ((not key)
  13216. (setq key
  13217. (if (= (char-after (match-beginning 3)) ?\[)
  13218. "TIMESTAMP_IA" "TIMESTAMP")))
  13219. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13220. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13221. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13222. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13223. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13224. (progn
  13225. (push (cons key string) props)
  13226. ;; no need to search further if match is found
  13227. (throw 'match t))
  13228. (when (or (equal key "CLOCK") (not (assoc key props)))
  13229. (push (cons key string) props)))))))
  13230. (when (memq which '(all standard))
  13231. ;; Get the standard properties, like :PROP: ...
  13232. (setq range (org-get-property-block beg end))
  13233. (when range
  13234. (goto-char (car range))
  13235. (while (re-search-forward
  13236. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13237. (cdr range) t)
  13238. (setq key (org-match-string-no-properties 1)
  13239. value (org-trim (or (org-match-string-no-properties 2) "")))
  13240. (unless (member key excluded)
  13241. (push (cons key (or value "")) props)))))
  13242. (if clocksum
  13243. (push (cons "CLOCKSUM"
  13244. (org-columns-number-to-string (/ (float clocksum) 60.)
  13245. 'add_times))
  13246. props))
  13247. (if clocksumt
  13248. (push (cons "CLOCKSUM_T"
  13249. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13250. 'add_times))
  13251. props))
  13252. (unless (assoc "CATEGORY" props)
  13253. (push (cons "CATEGORY" (org-get-category)) props))
  13254. (append sum-props (nreverse props)))))))
  13255. (defun org-entry-get (pom property &optional inherit literal-nil)
  13256. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13257. If INHERIT is non-nil and the entry does not have the property,
  13258. then also check higher levels of the hierarchy.
  13259. If INHERIT is the symbol `selective', use inheritance only if the setting
  13260. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13261. If the property is present but empty, the return value is the empty string.
  13262. If the property is not present at all, nil is returned.
  13263. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13264. do not interpret it as the list atom nil. This is used for inheritance
  13265. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13266. (org-with-point-at pom
  13267. (if (and inherit (if (eq inherit 'selective)
  13268. (org-property-inherit-p property)
  13269. t))
  13270. (org-entry-get-with-inheritance property literal-nil)
  13271. (if (member property org-special-properties)
  13272. ;; We need a special property. Use `org-entry-properties' to
  13273. ;; retrieve it, but specify the wanted property
  13274. (cdr (assoc property (org-entry-properties nil 'special property)))
  13275. (let* ((range (org-get-property-block))
  13276. (props (list (or (assoc property org-file-properties)
  13277. (assoc property org-global-properties)
  13278. (assoc property org-global-properties-fixed))))
  13279. (ap (lambda (key)
  13280. (when (re-search-forward
  13281. (org-re-property key) (cdr range) t)
  13282. (setq props
  13283. (org-update-property-plist
  13284. key
  13285. (if (match-end 1)
  13286. (org-match-string-no-properties 1) "")
  13287. props)))))
  13288. val)
  13289. (when (and range (goto-char (car range)))
  13290. (funcall ap property)
  13291. (goto-char (car range))
  13292. (while (funcall ap (concat property "+")))
  13293. (setq val (cdr (assoc property props)))
  13294. (when val (if literal-nil val (org-not-nil val)))))))))
  13295. (defun org-property-or-variable-value (var &optional inherit)
  13296. "Check if there is a property fixing the value of VAR.
  13297. If yes, return this value. If not, return the current value of the variable."
  13298. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13299. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13300. (read prop)
  13301. (symbol-value var))))
  13302. (defun org-entry-delete (pom property)
  13303. "Delete the property PROPERTY from entry at point-or-marker POM."
  13304. (org-with-point-at pom
  13305. (if (member property org-special-properties)
  13306. nil ; cannot delete these properties.
  13307. (let ((range (org-get-property-block)))
  13308. (if (and range
  13309. (goto-char (car range))
  13310. (re-search-forward
  13311. (org-re-property property)
  13312. (cdr range) t))
  13313. (progn
  13314. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13315. t)
  13316. nil)))))
  13317. ;; Multi-values properties are properties that contain multiple values
  13318. ;; These values are assumed to be single words, separated by whitespace.
  13319. (defun org-entry-add-to-multivalued-property (pom property value)
  13320. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13321. (let* ((old (org-entry-get pom property))
  13322. (values (and old (org-split-string old "[ \t]"))))
  13323. (setq value (org-entry-protect-space value))
  13324. (unless (member value values)
  13325. (setq values (cons value values))
  13326. (org-entry-put pom property
  13327. (mapconcat 'identity values " ")))))
  13328. (defun org-entry-remove-from-multivalued-property (pom property value)
  13329. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13330. (let* ((old (org-entry-get pom property))
  13331. (values (and old (org-split-string old "[ \t]"))))
  13332. (setq value (org-entry-protect-space value))
  13333. (when (member value values)
  13334. (setq values (delete value values))
  13335. (org-entry-put pom property
  13336. (mapconcat 'identity values " ")))))
  13337. (defun org-entry-member-in-multivalued-property (pom property value)
  13338. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13339. (let* ((old (org-entry-get pom property))
  13340. (values (and old (org-split-string old "[ \t]"))))
  13341. (setq value (org-entry-protect-space value))
  13342. (member value values)))
  13343. (defun org-entry-get-multivalued-property (pom property)
  13344. "Return a list of values in a multivalued property."
  13345. (let* ((value (org-entry-get pom property))
  13346. (values (and value (org-split-string value "[ \t]"))))
  13347. (mapcar 'org-entry-restore-space values)))
  13348. (defun org-entry-put-multivalued-property (pom property &rest values)
  13349. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13350. VALUES should be a list of strings. Spaces will be protected."
  13351. (org-entry-put pom property
  13352. (mapconcat 'org-entry-protect-space values " "))
  13353. (let* ((value (org-entry-get pom property))
  13354. (values (and value (org-split-string value "[ \t]"))))
  13355. (mapcar 'org-entry-restore-space values)))
  13356. (defun org-entry-protect-space (s)
  13357. "Protect spaces and newline in string S."
  13358. (while (string-match " " s)
  13359. (setq s (replace-match "%20" t t s)))
  13360. (while (string-match "\n" s)
  13361. (setq s (replace-match "%0A" t t s)))
  13362. s)
  13363. (defun org-entry-restore-space (s)
  13364. "Restore spaces and newline in string S."
  13365. (while (string-match "%20" s)
  13366. (setq s (replace-match " " t t s)))
  13367. (while (string-match "%0A" s)
  13368. (setq s (replace-match "\n" t t s)))
  13369. s)
  13370. (defvar org-entry-property-inherited-from (make-marker)
  13371. "Marker pointing to the entry from where a property was inherited.
  13372. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13373. location of the entry where the inheritance search matched. If there was
  13374. no match, the marker will point nowhere.
  13375. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13376. is set.")
  13377. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13378. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13379. The search will stop at the first ancestor which has the property defined.
  13380. If the value found is \"nil\", return nil to show that the property
  13381. should be considered as undefined (this is the meaning of nil here).
  13382. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13383. (move-marker org-entry-property-inherited-from nil)
  13384. (let (tmp)
  13385. (save-excursion
  13386. (save-restriction
  13387. (widen)
  13388. (catch 'ex
  13389. (while t
  13390. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13391. (or (ignore-errors (org-back-to-heading t))
  13392. (goto-char (point-min)))
  13393. (move-marker org-entry-property-inherited-from (point))
  13394. (throw 'ex tmp))
  13395. (or (ignore-errors (org-up-heading-safe))
  13396. (throw 'ex nil))))))
  13397. (setq tmp (or tmp
  13398. (cdr (assoc property org-file-properties))
  13399. (cdr (assoc property org-global-properties))
  13400. (cdr (assoc property org-global-properties-fixed))))
  13401. (if literal-nil tmp (org-not-nil tmp))))
  13402. (defvar org-property-changed-functions nil
  13403. "Hook called when the value of a property has changed.
  13404. Each hook function should accept two arguments, the name of the property
  13405. and the new value.")
  13406. (defun org-entry-put (pom property value)
  13407. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13408. (org-with-point-at pom
  13409. (org-back-to-heading t)
  13410. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13411. range)
  13412. (cond
  13413. ((equal property "TODO")
  13414. (when (and (stringp value) (string-match "\\S-" value)
  13415. (not (member value org-todo-keywords-1)))
  13416. (error "\"%s\" is not a valid TODO state" value))
  13417. (if (or (not value)
  13418. (not (string-match "\\S-" value)))
  13419. (setq value 'none))
  13420. (org-todo value)
  13421. (org-set-tags nil 'align))
  13422. ((equal property "PRIORITY")
  13423. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13424. (string-to-char value) ?\ ))
  13425. (org-set-tags nil 'align))
  13426. ((equal property "CLOCKSUM")
  13427. (if (not (re-search-forward
  13428. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13429. (error "Cannot find a clock log")
  13430. (goto-char (- (match-end 1) 2))
  13431. (cond
  13432. ((eq value 'earlier) (org-timestamp-down))
  13433. ((eq value 'later) (org-timestamp-up)))
  13434. (org-clock-sum-current-item)))
  13435. ((equal property "SCHEDULED")
  13436. (if (re-search-forward org-scheduled-time-regexp end t)
  13437. (cond
  13438. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13439. ((eq value 'later) (org-timestamp-change 1 'day))
  13440. (t (call-interactively 'org-schedule)))
  13441. (call-interactively 'org-schedule)))
  13442. ((equal property "DEADLINE")
  13443. (if (re-search-forward org-deadline-time-regexp end t)
  13444. (cond
  13445. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13446. ((eq value 'later) (org-timestamp-change 1 'day))
  13447. (t (call-interactively 'org-deadline)))
  13448. (call-interactively 'org-deadline)))
  13449. ((member property org-special-properties)
  13450. (error "The %s property can not yet be set with `org-entry-put'"
  13451. property))
  13452. (t ; a non-special property
  13453. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13454. (setq range (org-get-property-block beg end 'force))
  13455. (goto-char (car range))
  13456. (if (re-search-forward
  13457. (org-re-property-keyword property) (cdr range) t)
  13458. (progn
  13459. (delete-region (match-beginning 0) (match-end 0))
  13460. (goto-char (match-beginning 0)))
  13461. (goto-char (cdr range))
  13462. (insert "\n")
  13463. (backward-char 1)
  13464. (org-indent-line))
  13465. (insert ":" property ":")
  13466. (and value (insert " " value))
  13467. (org-indent-line)))))
  13468. (run-hook-with-args 'org-property-changed-functions property value)))
  13469. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13470. "Get all property keys in the current buffer.
  13471. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13472. like tags and TODO state.
  13473. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13474. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13475. and others.
  13476. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13477. formats in the current buffer."
  13478. (let (rtn range cfmt s p)
  13479. (save-excursion
  13480. (save-restriction
  13481. (widen)
  13482. (goto-char (point-min))
  13483. (while (re-search-forward org-property-start-re nil t)
  13484. (setq range (org-get-property-block))
  13485. (goto-char (car range))
  13486. (while (re-search-forward
  13487. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13488. (cdr range) t)
  13489. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13490. (outline-next-heading))))
  13491. (when include-specials
  13492. (setq rtn (append org-special-properties rtn)))
  13493. (when include-defaults
  13494. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13495. (add-to-list 'rtn org-effort-property))
  13496. (when include-columns
  13497. (save-excursion
  13498. (save-restriction
  13499. (widen)
  13500. (goto-char (point-min))
  13501. (while (re-search-forward
  13502. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13503. nil t)
  13504. (setq cfmt (match-string 2) s 0)
  13505. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13506. cfmt s)
  13507. (setq s (match-end 0)
  13508. p (match-string 1 cfmt))
  13509. (unless (or (equal p "ITEM")
  13510. (member p org-special-properties))
  13511. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13512. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13513. (defun org-property-values (key)
  13514. "Return a list of all values of property KEY in the current buffer."
  13515. (save-excursion
  13516. (save-restriction
  13517. (widen)
  13518. (goto-char (point-min))
  13519. (let ((re (org-re-property key))
  13520. values)
  13521. (while (re-search-forward re nil t)
  13522. (add-to-list 'values (org-trim (match-string 1))))
  13523. (delete "" values)))))
  13524. (defun org-insert-property-drawer ()
  13525. "Insert a property drawer into the current entry."
  13526. (org-back-to-heading t)
  13527. (looking-at org-outline-regexp)
  13528. (let ((indent (if org-adapt-indentation
  13529. (- (match-end 0) (match-beginning 0))
  13530. 0))
  13531. (beg (point))
  13532. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13533. end hiddenp)
  13534. (outline-next-heading)
  13535. (setq end (point))
  13536. (goto-char beg)
  13537. (while (re-search-forward re end t))
  13538. (setq hiddenp (outline-invisible-p))
  13539. (end-of-line 1)
  13540. (and (equal (char-after) ?\n) (forward-char 1))
  13541. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13542. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13543. ;; just skip this line
  13544. (beginning-of-line 2)
  13545. ;; Drawer start, find the end
  13546. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13547. (beginning-of-line 1)))
  13548. (org-skip-over-state-notes)
  13549. (skip-chars-backward " \t\n\r")
  13550. (if (eq (char-before) ?*) (forward-char 1))
  13551. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13552. (beginning-of-line 0)
  13553. (org-indent-to-column indent)
  13554. (beginning-of-line 2)
  13555. (org-indent-to-column indent)
  13556. (beginning-of-line 0)
  13557. (if hiddenp
  13558. (save-excursion
  13559. (org-back-to-heading t)
  13560. (hide-entry))
  13561. (org-flag-drawer t))))
  13562. (defun org-insert-drawer (&optional arg drawer)
  13563. "Insert a drawer at point.
  13564. Optional argument DRAWER, when non-nil, is a string representing
  13565. drawer's name. Otherwise, the user is prompted for a name.
  13566. If a region is active, insert the drawer around that region
  13567. instead.
  13568. Point is left between drawer's boundaries."
  13569. (interactive "P")
  13570. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13571. "LOGBOOK"))
  13572. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13573. ;; internally by Org. They are meant to be inserted
  13574. ;; automatically.
  13575. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13576. ;; Remove system drawers from list. Note: For some reason,
  13577. ;; `org-completing-read' ignores the predicate while
  13578. ;; `completing-read' handles it fine.
  13579. (drawer (if arg "PROPERTIES"
  13580. (or drawer
  13581. (completing-read
  13582. "Drawer: " org-drawers
  13583. (lambda (d) (not (member d system-drawers))))))))
  13584. (cond
  13585. ;; With C-u, fall back on `org-insert-property-drawer'
  13586. (arg (org-insert-property-drawer))
  13587. ;; With an active region, insert a drawer at point.
  13588. ((not (org-region-active-p))
  13589. (progn
  13590. (unless (bolp) (insert "\n"))
  13591. (insert (format ":%s:\n\n:END:\n" drawer))
  13592. (forward-line -2)))
  13593. ;; Otherwise, insert the drawer at point
  13594. (t
  13595. (let ((rbeg (region-beginning))
  13596. (rend (copy-marker (region-end))))
  13597. (unwind-protect
  13598. (progn
  13599. (goto-char rbeg)
  13600. (beginning-of-line)
  13601. (when (save-excursion
  13602. (re-search-forward org-outline-regexp-bol rend t))
  13603. (error "Drawers cannot contain headlines"))
  13604. ;; Position point at the beginning of the first
  13605. ;; non-blank line in region. Insert drawer's opening
  13606. ;; there, then indent it.
  13607. (org-skip-whitespace)
  13608. (beginning-of-line)
  13609. (insert ":" drawer ":\n")
  13610. (forward-line -1)
  13611. (indent-for-tab-command)
  13612. ;; Move point to the beginning of the first blank line
  13613. ;; after the last non-blank line in region. Insert
  13614. ;; drawer's closing, then indent it.
  13615. (goto-char rend)
  13616. (skip-chars-backward " \r\t\n")
  13617. (insert "\n:END:")
  13618. (deactivate-mark t)
  13619. (indent-for-tab-command)
  13620. (unless (eolp) (insert "\n")))
  13621. ;; Clear marker, whatever the outcome of insertion is.
  13622. (set-marker rend nil)))))))
  13623. (defvar org-property-set-functions-alist nil
  13624. "Property set function alist.
  13625. Each entry should have the following format:
  13626. (PROPERTY . READ-FUNCTION)
  13627. The read function will be called with the same argument as
  13628. `org-completing-read'.")
  13629. (defun org-set-property-function (property)
  13630. "Get the function that should be used to set PROPERTY.
  13631. This is computed according to `org-property-set-functions-alist'."
  13632. (or (cdr (assoc property org-property-set-functions-alist))
  13633. 'org-completing-read))
  13634. (defun org-read-property-value (property)
  13635. "Read PROPERTY value from user."
  13636. (let* ((completion-ignore-case t)
  13637. (allowed (org-property-get-allowed-values nil property 'table))
  13638. (cur (org-entry-get nil property))
  13639. (prompt (concat property " value"
  13640. (if (and cur (string-match "\\S-" cur))
  13641. (concat " [" cur "]") "") ": "))
  13642. (set-function (org-set-property-function property))
  13643. (val (if allowed
  13644. (funcall set-function prompt allowed nil
  13645. (not (get-text-property 0 'org-unrestricted
  13646. (caar allowed))))
  13647. (let (org-completion-use-ido org-completion-use-iswitchb)
  13648. (funcall set-function prompt
  13649. (mapcar 'list (org-property-values property))
  13650. nil nil "" nil cur)))))
  13651. (if (equal val "")
  13652. cur
  13653. val)))
  13654. (defvar org-last-set-property nil)
  13655. (defun org-read-property-name ()
  13656. "Read a property name."
  13657. (let* ((completion-ignore-case t)
  13658. (keys (org-buffer-property-keys nil t t))
  13659. (default-prop (or (save-excursion
  13660. (save-match-data
  13661. (beginning-of-line)
  13662. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13663. (null (string= (match-string 1) "END"))
  13664. (match-string 1))))
  13665. org-last-set-property))
  13666. (property (org-icompleting-read
  13667. (concat "Property"
  13668. (if default-prop (concat " [" default-prop "]") "")
  13669. ": ")
  13670. (mapcar 'list keys)
  13671. nil nil nil nil
  13672. default-prop
  13673. )))
  13674. (if (member property keys)
  13675. property
  13676. (or (cdr (assoc (downcase property)
  13677. (mapcar (lambda (x) (cons (downcase x) x))
  13678. keys)))
  13679. property))))
  13680. (defun org-set-property (property value)
  13681. "In the current entry, set PROPERTY to VALUE.
  13682. When called interactively, this will prompt for a property name, offering
  13683. completion on existing and default properties. And then it will prompt
  13684. for a value, offering completion either on allowed values (via an inherited
  13685. xxx_ALL property) or on existing values in other instances of this property
  13686. in the current file."
  13687. (interactive (list nil nil))
  13688. (let* ((property (or property (org-read-property-name)))
  13689. (value (or value (org-read-property-value property)))
  13690. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13691. (setq org-last-set-property property)
  13692. ;; Possibly postprocess the inserted value:
  13693. (when fn (setq value (funcall fn value)))
  13694. (unless (equal (org-entry-get nil property) value)
  13695. (org-entry-put nil property value))))
  13696. (defun org-delete-property (property)
  13697. "In the current entry, delete PROPERTY."
  13698. (interactive
  13699. (let* ((completion-ignore-case t)
  13700. (prop (org-icompleting-read "Property: "
  13701. (org-entry-properties nil 'standard))))
  13702. (list prop)))
  13703. (message "Property %s %s" property
  13704. (if (org-entry-delete nil property)
  13705. "deleted"
  13706. "was not present in the entry")))
  13707. (defun org-delete-property-globally (property)
  13708. "Remove PROPERTY globally, from all entries."
  13709. (interactive
  13710. (let* ((completion-ignore-case t)
  13711. (prop (org-icompleting-read
  13712. "Globally remove property: "
  13713. (mapcar 'list (org-buffer-property-keys)))))
  13714. (list prop)))
  13715. (save-excursion
  13716. (save-restriction
  13717. (widen)
  13718. (goto-char (point-min))
  13719. (let ((cnt 0))
  13720. (while (re-search-forward
  13721. (org-re-property property)
  13722. nil t)
  13723. (setq cnt (1+ cnt))
  13724. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13725. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13726. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13727. (defun org-compute-property-at-point ()
  13728. "Compute the property at point.
  13729. This looks for an enclosing column format, extracts the operator and
  13730. then applies it to the property in the column format's scope."
  13731. (interactive)
  13732. (unless (org-at-property-p)
  13733. (error "Not at a property"))
  13734. (let ((prop (org-match-string-no-properties 2)))
  13735. (org-columns-get-format-and-top-level)
  13736. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13737. (error "No operator defined for property %s" prop))
  13738. (org-columns-compute prop)))
  13739. (defvar org-property-allowed-value-functions nil
  13740. "Hook for functions supplying allowed values for a specific property.
  13741. The functions must take a single argument, the name of the property, and
  13742. return a flat list of allowed values. If \":ETC\" is one of
  13743. the values, this means that these values are intended as defaults for
  13744. completion, but that other values should be allowed too.
  13745. The functions must return nil if they are not responsible for this
  13746. property.")
  13747. (defun org-property-get-allowed-values (pom property &optional table)
  13748. "Get allowed values for the property PROPERTY.
  13749. When TABLE is non-nil, return an alist that can directly be used for
  13750. completion."
  13751. (let (vals)
  13752. (cond
  13753. ((equal property "TODO")
  13754. (setq vals (org-with-point-at pom
  13755. (append org-todo-keywords-1 '("")))))
  13756. ((equal property "PRIORITY")
  13757. (let ((n org-lowest-priority))
  13758. (while (>= n org-highest-priority)
  13759. (push (char-to-string n) vals)
  13760. (setq n (1- n)))))
  13761. ((member property org-special-properties))
  13762. ((setq vals (run-hook-with-args-until-success
  13763. 'org-property-allowed-value-functions property)))
  13764. (t
  13765. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13766. (when (and vals (string-match "\\S-" vals))
  13767. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13768. (setq vals (mapcar (lambda (x)
  13769. (cond ((stringp x) x)
  13770. ((numberp x) (number-to-string x))
  13771. ((symbolp x) (symbol-name x))
  13772. (t "???")))
  13773. vals)))))
  13774. (when (member ":ETC" vals)
  13775. (setq vals (remove ":ETC" vals))
  13776. (org-add-props (car vals) '(org-unrestricted t)))
  13777. (if table (mapcar 'list vals) vals)))
  13778. (defun org-property-previous-allowed-value (&optional previous)
  13779. "Switch to the next allowed value for this property."
  13780. (interactive)
  13781. (org-property-next-allowed-value t))
  13782. (defun org-property-next-allowed-value (&optional previous)
  13783. "Switch to the next allowed value for this property."
  13784. (interactive)
  13785. (unless (org-at-property-p)
  13786. (error "Not at a property"))
  13787. (let* ((key (match-string 2))
  13788. (value (match-string 3))
  13789. (allowed (or (org-property-get-allowed-values (point) key)
  13790. (and (member value '("[ ]" "[-]" "[X]"))
  13791. '("[ ]" "[X]"))))
  13792. nval)
  13793. (unless allowed
  13794. (error "Allowed values for this property have not been defined"))
  13795. (if previous (setq allowed (reverse allowed)))
  13796. (if (member value allowed)
  13797. (setq nval (car (cdr (member value allowed)))))
  13798. (setq nval (or nval (car allowed)))
  13799. (if (equal nval value)
  13800. (error "Only one allowed value for this property"))
  13801. (org-at-property-p)
  13802. (replace-match (concat " :" key ": " nval) t t)
  13803. (org-indent-line)
  13804. (beginning-of-line 1)
  13805. (skip-chars-forward " \t")
  13806. (run-hook-with-args 'org-property-changed-functions key nval)))
  13807. (defun org-find-olp (path &optional this-buffer)
  13808. "Return a marker pointing to the entry at outline path OLP.
  13809. If anything goes wrong, throw an error.
  13810. You can wrap this call to catch the error like this:
  13811. (condition-case msg
  13812. (org-mobile-locate-entry (match-string 4))
  13813. (error (nth 1 msg)))
  13814. The return value will then be either a string with the error message,
  13815. or a marker if everything is OK.
  13816. If THIS-BUFFER is set, the outline path does not contain a file,
  13817. only headings."
  13818. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13819. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13820. (level 1)
  13821. (lmin 1)
  13822. (lmax 1)
  13823. limit re end found pos heading cnt flevel)
  13824. (unless buffer (error "File not found :%s" file))
  13825. (with-current-buffer buffer
  13826. (save-excursion
  13827. (save-restriction
  13828. (widen)
  13829. (setq limit (point-max))
  13830. (goto-char (point-min))
  13831. (while (setq heading (pop path))
  13832. (setq re (format org-complex-heading-regexp-format
  13833. (regexp-quote heading)))
  13834. (setq cnt 0 pos (point))
  13835. (while (re-search-forward re end t)
  13836. (setq level (- (match-end 1) (match-beginning 1)))
  13837. (if (and (>= level lmin) (<= level lmax))
  13838. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13839. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13840. lmax heading))
  13841. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13842. lmax heading))
  13843. (goto-char found)
  13844. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13845. (setq end (save-excursion (org-end-of-subtree t t))))
  13846. (when (org-at-heading-p)
  13847. (move-marker (make-marker) (point))))))))
  13848. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13849. "Find node HEADING in BUFFER.
  13850. Return a marker to the heading if it was found, or nil if not.
  13851. If POS-ONLY is set, return just the position instead of a marker.
  13852. The heading text must match exact, but it may have a TODO keyword,
  13853. a priority cookie and tags in the standard locations."
  13854. (with-current-buffer (or buffer (current-buffer))
  13855. (save-excursion
  13856. (save-restriction
  13857. (widen)
  13858. (goto-char (point-min))
  13859. (let (case-fold-search)
  13860. (if (re-search-forward
  13861. (format org-complex-heading-regexp-format
  13862. (regexp-quote heading)) nil t)
  13863. (if pos-only
  13864. (match-beginning 0)
  13865. (move-marker (make-marker) (match-beginning 0)))))))))
  13866. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13867. "Find Org node headline HEADING in all .org files in directory DIR.
  13868. When the target headline is found, return a marker to this location."
  13869. (let ((files (directory-files (or dir default-directory)
  13870. nil "\\`[^.#].*\\.org\\'"))
  13871. file visiting m buffer)
  13872. (catch 'found
  13873. (while (setq file (pop files))
  13874. (message "trying %s" file)
  13875. (setq visiting (org-find-base-buffer-visiting file))
  13876. (setq buffer (or visiting (find-file-noselect file)))
  13877. (setq m (org-find-exact-headline-in-buffer
  13878. heading buffer))
  13879. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13880. (and m (throw 'found m))))))
  13881. (defun org-find-entry-with-id (ident)
  13882. "Locate the entry that contains the ID property with exact value IDENT.
  13883. IDENT can be a string, a symbol or a number, this function will search for
  13884. the string representation of it.
  13885. Return the position where this entry starts, or nil if there is no such entry."
  13886. (interactive "sID: ")
  13887. (let ((id (cond
  13888. ((stringp ident) ident)
  13889. ((symbol-name ident) (symbol-name ident))
  13890. ((numberp ident) (number-to-string ident))
  13891. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13892. (case-fold-search nil))
  13893. (save-excursion
  13894. (save-restriction
  13895. (widen)
  13896. (goto-char (point-min))
  13897. (when (re-search-forward
  13898. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13899. nil t)
  13900. (org-back-to-heading t)
  13901. (point))))))
  13902. ;;;; Timestamps
  13903. (defvar org-last-changed-timestamp nil)
  13904. (defvar org-last-inserted-timestamp nil
  13905. "The last time stamp inserted with `org-insert-time-stamp'.")
  13906. (defvar org-time-was-given) ; dynamically scoped parameter
  13907. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13908. (defvar org-ts-what) ; dynamically scoped parameter
  13909. (defun org-time-stamp (arg &optional inactive)
  13910. "Prompt for a date/time and insert a time stamp.
  13911. If the user specifies a time like HH:MM or if this command is
  13912. called with at least one prefix argument, the time stamp contains
  13913. the date and the time. Otherwise, only the date is be included.
  13914. All parts of a date not specified by the user is filled in from
  13915. the current date/time. So if you just press return without
  13916. typing anything, the time stamp will represent the current
  13917. date/time.
  13918. If there is already a timestamp at the cursor, it will be
  13919. modified.
  13920. With two universal prefix arguments, insert an active timestamp
  13921. with the current time without prompting the user."
  13922. (interactive "P")
  13923. (let* ((ts nil)
  13924. (default-time
  13925. ;; Default time is either today, or, when entering a range,
  13926. ;; the range start.
  13927. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13928. (save-excursion
  13929. (re-search-backward
  13930. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13931. (- (point) 20) t)))
  13932. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13933. (current-time)))
  13934. (default-input (and ts (org-get-compact-tod ts)))
  13935. (repeater (save-excursion
  13936. (save-match-data
  13937. (beginning-of-line)
  13938. (when (re-search-forward
  13939. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13940. (save-excursion (progn (end-of-line) (point))) t)
  13941. (match-string 0)))))
  13942. org-time-was-given org-end-time-was-given time)
  13943. (cond
  13944. ((and (org-at-timestamp-p t)
  13945. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13946. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13947. (insert "--")
  13948. (setq time (let ((this-command this-command))
  13949. (org-read-date arg 'totime nil nil
  13950. default-time default-input inactive)))
  13951. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13952. ((org-at-timestamp-p t)
  13953. (setq time (let ((this-command this-command))
  13954. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13955. (when (org-at-timestamp-p t) ; just to get the match data
  13956. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13957. (replace-match "")
  13958. (setq org-last-changed-timestamp
  13959. (org-insert-time-stamp
  13960. time (or org-time-was-given arg)
  13961. inactive nil nil (list org-end-time-was-given)))
  13962. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13963. (setq org-last-changed-timestamp
  13964. (concat (substring org-last-inserted-timestamp 0 -1)
  13965. " " repeater ">"))))
  13966. (message "Timestamp updated"))
  13967. ((equal arg '(16))
  13968. (org-insert-time-stamp (current-time) t))
  13969. (t
  13970. (setq time (let ((this-command this-command))
  13971. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13972. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13973. nil nil (list org-end-time-was-given))))))
  13974. ;; FIXME: can we use this for something else, like computing time differences?
  13975. (defun org-get-compact-tod (s)
  13976. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13977. (let* ((t1 (match-string 1 s))
  13978. (h1 (string-to-number (match-string 2 s)))
  13979. (m1 (string-to-number (match-string 3 s)))
  13980. (t2 (and (match-end 4) (match-string 5 s)))
  13981. (h2 (and t2 (string-to-number (match-string 6 s))))
  13982. (m2 (and t2 (string-to-number (match-string 7 s))))
  13983. dh dm)
  13984. (if (not t2)
  13985. t1
  13986. (setq dh (- h2 h1) dm (- m2 m1))
  13987. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13988. (concat t1 "+" (number-to-string dh)
  13989. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13990. (defun org-time-stamp-inactive (&optional arg)
  13991. "Insert an inactive time stamp.
  13992. An inactive time stamp is enclosed in square brackets instead of angle
  13993. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13994. does not link to the calendar and cannot be changed with the S-cursor keys.
  13995. So these are more for recording a certain time/date."
  13996. (interactive "P")
  13997. (org-time-stamp arg 'inactive))
  13998. (defvar org-date-ovl (make-overlay 1 1))
  13999. (overlay-put org-date-ovl 'face 'org-date-selected)
  14000. (org-detach-overlay org-date-ovl)
  14001. (defvar org-ans1) ; dynamically scoped parameter
  14002. (defvar org-ans2) ; dynamically scoped parameter
  14003. (defvar org-plain-time-of-day-regexp) ; defined below
  14004. (defvar org-overriding-default-time nil) ; dynamically scoped
  14005. (defvar org-read-date-overlay nil)
  14006. (defvar org-dcst nil) ; dynamically scoped
  14007. (defvar org-read-date-history nil)
  14008. (defvar org-read-date-final-answer nil)
  14009. (defvar org-read-date-analyze-futurep nil)
  14010. (defvar org-read-date-analyze-forced-year nil)
  14011. (defvar org-read-date-inactive)
  14012. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14013. default-time default-input inactive)
  14014. "Read a date, possibly a time, and make things smooth for the user.
  14015. The prompt will suggest to enter an ISO date, but you can also enter anything
  14016. which will at least partially be understood by `parse-time-string'.
  14017. Unrecognized parts of the date will default to the current day, month, year,
  14018. hour and minute. If this command is called to replace a timestamp at point,
  14019. or to enter the second timestamp of a range, the default time is taken
  14020. from the existing stamp. Furthermore, the command prefers the future,
  14021. so if you are giving a date where the year is not given, and the day-month
  14022. combination is already past in the current year, it will assume you
  14023. mean next year. For details, see the manual. A few examples:
  14024. 3-2-5 --> 2003-02-05
  14025. feb 15 --> currentyear-02-15
  14026. 2/15 --> currentyear-02-15
  14027. sep 12 9 --> 2009-09-12
  14028. 12:45 --> today 12:45
  14029. 22 sept 0:34 --> currentyear-09-22 0:34
  14030. 12 --> currentyear-currentmonth-12
  14031. Fri --> nearest Friday (today or later)
  14032. etc.
  14033. Furthermore you can specify a relative date by giving, as the *first* thing
  14034. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14035. change in days weeks, months, years.
  14036. With a single plus or minus, the date is relative to today. With a double
  14037. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14038. +4d --> four days from today
  14039. +4 --> same as above
  14040. +2w --> two weeks from today
  14041. ++5 --> five days from default date
  14042. The function understands only English month and weekday abbreviations.
  14043. While prompting, a calendar is popped up - you can also select the
  14044. date with the mouse (button 1). The calendar shows a period of three
  14045. months. To scroll it to other months, use the keys `>' and `<'.
  14046. If you don't like the calendar, turn it off with
  14047. \(setq org-read-date-popup-calendar nil)
  14048. With optional argument TO-TIME, the date will immediately be converted
  14049. to an internal time.
  14050. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14051. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14052. still enter a time, and this function will inform the calling routine
  14053. about this change. The calling routine may then choose to change the
  14054. format used to insert the time stamp into the buffer to include the time.
  14055. With optional argument FROM-STRING, read from this string instead from
  14056. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14057. the time/date that is used for everything that is not specified by the
  14058. user."
  14059. (require 'parse-time)
  14060. (let* ((org-time-stamp-rounding-minutes
  14061. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14062. (org-dcst org-display-custom-times)
  14063. (ct (org-current-time))
  14064. (org-def (or org-overriding-default-time default-time ct))
  14065. (org-defdecode (decode-time org-def))
  14066. (dummy (progn
  14067. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14068. (setcar (nthcdr 2 org-defdecode) -1)
  14069. (setcar (nthcdr 1 org-defdecode) 59)
  14070. (setq org-def (apply 'encode-time org-defdecode)
  14071. org-defdecode (decode-time org-def)))))
  14072. (calendar-frame-setup nil)
  14073. (calendar-setup nil)
  14074. (calendar-move-hook nil)
  14075. (calendar-view-diary-initially-flag nil)
  14076. (calendar-view-holidays-initially-flag nil)
  14077. (timestr (format-time-string
  14078. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14079. (prompt (concat (if prompt (concat prompt " ") "")
  14080. (format "Date+time [%s]: " timestr)))
  14081. ans (org-ans0 "") org-ans1 org-ans2 final)
  14082. (cond
  14083. (from-string (setq ans from-string))
  14084. (org-read-date-popup-calendar
  14085. (save-excursion
  14086. (save-window-excursion
  14087. (calendar)
  14088. (org-eval-in-calendar '(setq cursor-type nil) t)
  14089. (unwind-protect
  14090. (progn
  14091. (calendar-forward-day (- (time-to-days org-def)
  14092. (calendar-absolute-from-gregorian
  14093. (calendar-current-date))))
  14094. (org-eval-in-calendar nil t)
  14095. (let* ((old-map (current-local-map))
  14096. (map (copy-keymap calendar-mode-map))
  14097. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14098. (org-defkey map (kbd "RET") 'org-calendar-select)
  14099. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14100. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14101. (org-defkey minibuffer-local-map [(meta shift left)]
  14102. (lambda () (interactive)
  14103. (org-eval-in-calendar '(calendar-backward-month 1))))
  14104. (org-defkey minibuffer-local-map [(meta shift right)]
  14105. (lambda () (interactive)
  14106. (org-eval-in-calendar '(calendar-forward-month 1))))
  14107. (org-defkey minibuffer-local-map [(meta shift up)]
  14108. (lambda () (interactive)
  14109. (org-eval-in-calendar '(calendar-backward-year 1))))
  14110. (org-defkey minibuffer-local-map [(meta shift down)]
  14111. (lambda () (interactive)
  14112. (org-eval-in-calendar '(calendar-forward-year 1))))
  14113. (org-defkey minibuffer-local-map [?\e (shift left)]
  14114. (lambda () (interactive)
  14115. (org-eval-in-calendar '(calendar-backward-month 1))))
  14116. (org-defkey minibuffer-local-map [?\e (shift right)]
  14117. (lambda () (interactive)
  14118. (org-eval-in-calendar '(calendar-forward-month 1))))
  14119. (org-defkey minibuffer-local-map [?\e (shift up)]
  14120. (lambda () (interactive)
  14121. (org-eval-in-calendar '(calendar-backward-year 1))))
  14122. (org-defkey minibuffer-local-map [?\e (shift down)]
  14123. (lambda () (interactive)
  14124. (org-eval-in-calendar '(calendar-forward-year 1))))
  14125. (org-defkey minibuffer-local-map [(shift up)]
  14126. (lambda () (interactive)
  14127. (org-eval-in-calendar '(calendar-backward-week 1))))
  14128. (org-defkey minibuffer-local-map [(shift down)]
  14129. (lambda () (interactive)
  14130. (org-eval-in-calendar '(calendar-forward-week 1))))
  14131. (org-defkey minibuffer-local-map [(shift left)]
  14132. (lambda () (interactive)
  14133. (org-eval-in-calendar '(calendar-backward-day 1))))
  14134. (org-defkey minibuffer-local-map [(shift right)]
  14135. (lambda () (interactive)
  14136. (org-eval-in-calendar '(calendar-forward-day 1))))
  14137. (org-defkey minibuffer-local-map ">"
  14138. (lambda () (interactive)
  14139. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14140. (org-defkey minibuffer-local-map "<"
  14141. (lambda () (interactive)
  14142. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14143. (org-defkey minibuffer-local-map "\C-v"
  14144. (lambda () (interactive)
  14145. (org-eval-in-calendar
  14146. '(calendar-scroll-left-three-months 1))))
  14147. (org-defkey minibuffer-local-map "\M-v"
  14148. (lambda () (interactive)
  14149. (org-eval-in-calendar
  14150. '(calendar-scroll-right-three-months 1))))
  14151. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14152. (unwind-protect
  14153. (progn
  14154. (use-local-map map)
  14155. (setq org-read-date-inactive inactive)
  14156. (add-hook 'post-command-hook 'org-read-date-display)
  14157. (setq org-ans0 (read-string prompt default-input
  14158. 'org-read-date-history nil))
  14159. ;; org-ans0: from prompt
  14160. ;; org-ans1: from mouse click
  14161. ;; org-ans2: from calendar motion
  14162. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14163. (remove-hook 'post-command-hook 'org-read-date-display)
  14164. (use-local-map old-map)
  14165. (when org-read-date-overlay
  14166. (delete-overlay org-read-date-overlay)
  14167. (setq org-read-date-overlay nil)))))
  14168. (bury-buffer "*Calendar*")))))
  14169. (t ; Naked prompt only
  14170. (unwind-protect
  14171. (setq ans (read-string prompt default-input
  14172. 'org-read-date-history timestr))
  14173. (when org-read-date-overlay
  14174. (delete-overlay org-read-date-overlay)
  14175. (setq org-read-date-overlay nil)))))
  14176. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14177. (when org-read-date-analyze-forced-year
  14178. (message "Year was forced into %s"
  14179. (if org-read-date-force-compatible-dates
  14180. "compatible range (1970-2037)"
  14181. "range representable on this machine"))
  14182. (ding))
  14183. ;; One round trip to get rid of 34th of August and stuff like that....
  14184. (setq final (decode-time (apply 'encode-time final)))
  14185. (setq org-read-date-final-answer ans)
  14186. (if to-time
  14187. (apply 'encode-time final)
  14188. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14189. (format "%04d-%02d-%02d %02d:%02d"
  14190. (nth 5 final) (nth 4 final) (nth 3 final)
  14191. (nth 2 final) (nth 1 final))
  14192. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14193. (defvar org-def)
  14194. (defvar org-defdecode)
  14195. (defvar org-with-time)
  14196. (defun org-read-date-display ()
  14197. "Display the current date prompt interpretation in the minibuffer."
  14198. (when org-read-date-display-live
  14199. (when org-read-date-overlay
  14200. (delete-overlay org-read-date-overlay))
  14201. (when (minibufferp (current-buffer))
  14202. (save-excursion
  14203. (end-of-line 1)
  14204. (while (not (equal (buffer-substring
  14205. (max (point-min) (- (point) 4)) (point))
  14206. " "))
  14207. (insert " ")))
  14208. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14209. " " (or org-ans1 org-ans2)))
  14210. (org-end-time-was-given nil)
  14211. (f (org-read-date-analyze ans org-def org-defdecode))
  14212. (fmts (if org-dcst
  14213. org-time-stamp-custom-formats
  14214. org-time-stamp-formats))
  14215. (fmt (if (or org-with-time
  14216. (and (boundp 'org-time-was-given) org-time-was-given))
  14217. (cdr fmts)
  14218. (car fmts)))
  14219. (txt (format-time-string fmt (apply 'encode-time f)))
  14220. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14221. (txt (concat "=> " txt)))
  14222. (when (and org-end-time-was-given
  14223. (string-match org-plain-time-of-day-regexp txt))
  14224. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14225. org-end-time-was-given
  14226. (substring txt (match-end 0)))))
  14227. (when org-read-date-analyze-futurep
  14228. (setq txt (concat txt " (=>F)")))
  14229. (setq org-read-date-overlay
  14230. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14231. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14232. (defun org-read-date-analyze (ans org-def org-defdecode)
  14233. "Analyze the combined answer of the date prompt."
  14234. ;; FIXME: cleanup and comment
  14235. (let ((nowdecode (decode-time (current-time)))
  14236. delta deltan deltaw deltadef year month day
  14237. hour minute second wday pm h2 m2 tl wday1
  14238. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14239. (setq org-read-date-analyze-futurep nil
  14240. org-read-date-analyze-forced-year nil)
  14241. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14242. (setq ans "+0"))
  14243. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14244. (setq ans (replace-match "" t t ans)
  14245. deltan (car delta)
  14246. deltaw (nth 1 delta)
  14247. deltadef (nth 2 delta)))
  14248. ;; Check if there is an iso week date in there. If yes, store the
  14249. ;; info and postpone interpreting it until the rest of the parsing
  14250. ;; is done.
  14251. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14252. (setq iso-year (if (match-end 1)
  14253. (org-small-year-to-year
  14254. (string-to-number (match-string 1 ans))))
  14255. iso-weekday (if (match-end 3)
  14256. (string-to-number (match-string 3 ans)))
  14257. iso-week (string-to-number (match-string 2 ans)))
  14258. (setq ans (replace-match "" t t ans)))
  14259. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14260. (when (string-match
  14261. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14262. (setq year (if (match-end 2)
  14263. (string-to-number (match-string 2 ans))
  14264. (progn (setq kill-year t)
  14265. (string-to-number (format-time-string "%Y"))))
  14266. month (string-to-number (match-string 3 ans))
  14267. day (string-to-number (match-string 4 ans)))
  14268. (if (< year 100) (setq year (+ 2000 year)))
  14269. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14270. t nil ans)))
  14271. ;; Help matching dotted european dates
  14272. (when (string-match
  14273. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14274. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14275. (setq kill-year t)
  14276. (string-to-number (format-time-string "%Y")))
  14277. day (string-to-number (match-string 1 ans))
  14278. month (string-to-number (match-string 2 ans))
  14279. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14280. t nil ans)))
  14281. ;; Help matching american dates, like 5/30 or 5/30/7
  14282. (when (string-match
  14283. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14284. (setq year (if (match-end 4)
  14285. (string-to-number (match-string 4 ans))
  14286. (progn (setq kill-year t)
  14287. (string-to-number (format-time-string "%Y"))))
  14288. month (string-to-number (match-string 1 ans))
  14289. day (string-to-number (match-string 2 ans)))
  14290. (if (< year 100) (setq year (+ 2000 year)))
  14291. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14292. t nil ans)))
  14293. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14294. ;; If there is a time with am/pm, and *no* time without it, we convert
  14295. ;; so that matching will be successful.
  14296. (loop for i from 1 to 2 do ; twice, for end time as well
  14297. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14298. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14299. (setq hour (string-to-number (match-string 1 ans))
  14300. minute (if (match-end 3)
  14301. (string-to-number (match-string 3 ans))
  14302. 0)
  14303. pm (equal ?p
  14304. (string-to-char (downcase (match-string 4 ans)))))
  14305. (if (and (= hour 12) (not pm))
  14306. (setq hour 0)
  14307. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14308. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14309. t t ans))))
  14310. ;; Check if a time range is given as a duration
  14311. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14312. (setq hour (string-to-number (match-string 1 ans))
  14313. h2 (+ hour (string-to-number (match-string 3 ans)))
  14314. minute (string-to-number (match-string 2 ans))
  14315. m2 (+ minute (if (match-end 5) (string-to-number
  14316. (match-string 5 ans))0)))
  14317. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14318. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14319. t t ans)))
  14320. ;; Check if there is a time range
  14321. (when (boundp 'org-end-time-was-given)
  14322. (setq org-time-was-given nil)
  14323. (when (and (string-match org-plain-time-of-day-regexp ans)
  14324. (match-end 8))
  14325. (setq org-end-time-was-given (match-string 8 ans))
  14326. (setq ans (concat (substring ans 0 (match-beginning 7))
  14327. (substring ans (match-end 7))))))
  14328. (setq tl (parse-time-string ans)
  14329. day (or (nth 3 tl) (nth 3 org-defdecode))
  14330. month (or (nth 4 tl)
  14331. (if (and org-read-date-prefer-future
  14332. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14333. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14334. (nth 4 org-defdecode)))
  14335. year (or (and (not kill-year) (nth 5 tl))
  14336. (if (and org-read-date-prefer-future
  14337. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14338. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14339. (nth 5 org-defdecode)))
  14340. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14341. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14342. second (or (nth 0 tl) 0)
  14343. wday (nth 6 tl))
  14344. (when (and (eq org-read-date-prefer-future 'time)
  14345. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14346. (equal day (nth 3 nowdecode))
  14347. (equal month (nth 4 nowdecode))
  14348. (equal year (nth 5 nowdecode))
  14349. (nth 2 tl)
  14350. (or (< (nth 2 tl) (nth 2 nowdecode))
  14351. (and (= (nth 2 tl) (nth 2 nowdecode))
  14352. (nth 1 tl)
  14353. (< (nth 1 tl) (nth 1 nowdecode)))))
  14354. (setq day (1+ day)
  14355. futurep t))
  14356. ;; Special date definitions below
  14357. (cond
  14358. (iso-week
  14359. ;; There was an iso week
  14360. (require 'cal-iso)
  14361. (setq futurep nil)
  14362. (setq year (or iso-year year)
  14363. day (or iso-weekday wday 1)
  14364. wday nil ; to make sure that the trigger below does not match
  14365. iso-date (calendar-gregorian-from-absolute
  14366. (calendar-absolute-from-iso
  14367. (list iso-week day year))))
  14368. ; FIXME: Should we also push ISO weeks into the future?
  14369. ; (when (and org-read-date-prefer-future
  14370. ; (not iso-year)
  14371. ; (< (calendar-absolute-from-gregorian iso-date)
  14372. ; (time-to-days (current-time))))
  14373. ; (setq year (1+ year)
  14374. ; iso-date (calendar-gregorian-from-absolute
  14375. ; (calendar-absolute-from-iso
  14376. ; (list iso-week day year)))))
  14377. (setq month (car iso-date)
  14378. year (nth 2 iso-date)
  14379. day (nth 1 iso-date)))
  14380. (deltan
  14381. (setq futurep nil)
  14382. (unless deltadef
  14383. (let ((now (decode-time (current-time))))
  14384. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14385. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14386. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14387. ((equal deltaw "m") (setq month (+ month deltan)))
  14388. ((equal deltaw "y") (setq year (+ year deltan)))))
  14389. ((and wday (not (nth 3 tl)))
  14390. ;; Weekday was given, but no day, so pick that day in the week
  14391. ;; on or after the derived date.
  14392. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14393. (unless (equal wday wday1)
  14394. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14395. (if (and (boundp 'org-time-was-given)
  14396. (nth 2 tl))
  14397. (setq org-time-was-given t))
  14398. (if (< year 100) (setq year (+ 2000 year)))
  14399. ;; Check of the date is representable
  14400. (if org-read-date-force-compatible-dates
  14401. (progn
  14402. (if (< year 1970)
  14403. (setq year 1970 org-read-date-analyze-forced-year t))
  14404. (if (> year 2037)
  14405. (setq year 2037 org-read-date-analyze-forced-year t)))
  14406. (condition-case nil
  14407. (ignore (encode-time second minute hour day month year))
  14408. (error
  14409. (setq year (nth 5 org-defdecode))
  14410. (setq org-read-date-analyze-forced-year t))))
  14411. (setq org-read-date-analyze-futurep futurep)
  14412. (list second minute hour day month year)))
  14413. (defvar parse-time-weekdays)
  14414. (defun org-read-date-get-relative (s today default)
  14415. "Check string S for special relative date string.
  14416. TODAY and DEFAULT are internal times, for today and for a default.
  14417. Return shift list (N what def-flag)
  14418. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14419. N is the number of WHATs to shift.
  14420. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14421. the DEFAULT date rather than TODAY."
  14422. (require 'parse-time)
  14423. (when (and
  14424. (string-match
  14425. (concat
  14426. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14427. "\\([0-9]+\\)?"
  14428. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14429. "\\([ \t]\\|$\\)") s)
  14430. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14431. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14432. (string-to-char (substring (match-string 1 s) -1))
  14433. ?+))
  14434. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14435. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14436. (what (if (match-end 3) (match-string 3 s) "d"))
  14437. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14438. (date (if rel default today))
  14439. (wday (nth 6 (decode-time date)))
  14440. delta)
  14441. (if wday1
  14442. (progn
  14443. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14444. (if (= dir ?-) (setq delta (- delta 7)))
  14445. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14446. (list delta "d" rel))
  14447. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14448. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14449. "Turn a user-specified date into the internal representation.
  14450. The internal representation needed by the calendar is (month day year).
  14451. This is a wrapper to handle the brain-dead convention in calendar that
  14452. user function argument order change dependent on argument order."
  14453. (if (boundp 'calendar-date-style)
  14454. (cond
  14455. ((eq calendar-date-style 'american)
  14456. (list arg1 arg2 arg3))
  14457. ((eq calendar-date-style 'european)
  14458. (list arg2 arg1 arg3))
  14459. ((eq calendar-date-style 'iso)
  14460. (list arg2 arg3 arg1)))
  14461. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14462. (if (org-bound-and-true-p european-calendar-style)
  14463. (list arg2 arg1 arg3)
  14464. (list arg1 arg2 arg3)))))
  14465. (defun org-eval-in-calendar (form &optional keepdate)
  14466. "Eval FORM in the calendar window and return to current window.
  14467. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14468. otherwise stick to the current value of `org-ans2'."
  14469. (let ((sf (selected-frame))
  14470. (sw (selected-window)))
  14471. (select-window (get-buffer-window "*Calendar*" t))
  14472. (eval form)
  14473. (when (and (not keepdate) (calendar-cursor-to-date))
  14474. (let* ((date (calendar-cursor-to-date))
  14475. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14476. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14477. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14478. (select-window sw)
  14479. (org-select-frame-set-input-focus sf)))
  14480. (defun org-calendar-select ()
  14481. "Return to `org-read-date' with the date currently selected.
  14482. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14483. (interactive)
  14484. (when (calendar-cursor-to-date)
  14485. (let* ((date (calendar-cursor-to-date))
  14486. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14487. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14488. (if (active-minibuffer-window) (exit-minibuffer))))
  14489. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14490. "Insert a date stamp for the date given by the internal TIME.
  14491. WITH-HM means use the stamp format that includes the time of the day.
  14492. INACTIVE means use square brackets instead of angular ones, so that the
  14493. stamp will not contribute to the agenda.
  14494. PRE and POST are optional strings to be inserted before and after the
  14495. stamp.
  14496. The command returns the inserted time stamp."
  14497. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14498. stamp)
  14499. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14500. (insert-before-markers (or pre ""))
  14501. (when (listp extra)
  14502. (setq extra (car extra))
  14503. (if (and (stringp extra)
  14504. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14505. (setq extra (format "-%02d:%02d"
  14506. (string-to-number (match-string 1 extra))
  14507. (string-to-number (match-string 2 extra))))
  14508. (setq extra nil)))
  14509. (when extra
  14510. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14511. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14512. (insert-before-markers (or post ""))
  14513. (setq org-last-inserted-timestamp stamp)))
  14514. (defun org-toggle-time-stamp-overlays ()
  14515. "Toggle the use of custom time stamp formats."
  14516. (interactive)
  14517. (setq org-display-custom-times (not org-display-custom-times))
  14518. (unless org-display-custom-times
  14519. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14520. (while (setq p (next-single-property-change p 'display))
  14521. (if (and (get-text-property p 'display)
  14522. (eq (get-text-property p 'face) 'org-date))
  14523. (remove-text-properties
  14524. p (setq p (next-single-property-change p 'display))
  14525. '(display t))))
  14526. (set-buffer-modified-p bmp)))
  14527. (if (featurep 'xemacs)
  14528. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14529. (org-restart-font-lock)
  14530. (setq org-table-may-need-update t)
  14531. (if org-display-custom-times
  14532. (message "Time stamps are overlaid with custom format")
  14533. (message "Time stamp overlays removed")))
  14534. (defun org-display-custom-time (beg end)
  14535. "Overlay modified time stamp format over timestamp between BEG and END."
  14536. (let* ((ts (buffer-substring beg end))
  14537. t1 w1 with-hm tf time str w2 (off 0))
  14538. (save-match-data
  14539. (setq t1 (org-parse-time-string ts t))
  14540. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14541. (setq off (- (match-end 0) (match-beginning 0)))))
  14542. (setq end (- end off))
  14543. (setq w1 (- end beg)
  14544. with-hm (and (nth 1 t1) (nth 2 t1))
  14545. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14546. time (org-fix-decoded-time t1)
  14547. str (org-add-props
  14548. (format-time-string
  14549. (substring tf 1 -1) (apply 'encode-time time))
  14550. nil 'mouse-face 'highlight)
  14551. w2 (length str))
  14552. (if (not (= w2 w1))
  14553. (add-text-properties (1+ beg) (+ 2 beg)
  14554. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14555. (if (featurep 'xemacs)
  14556. (progn
  14557. (put-text-property beg end 'invisible t)
  14558. (put-text-property beg end 'end-glyph (make-glyph str)))
  14559. (put-text-property beg end 'display str))))
  14560. (defun org-translate-time (string)
  14561. "Translate all timestamps in STRING to custom format.
  14562. But do this only if the variable `org-display-custom-times' is set."
  14563. (when org-display-custom-times
  14564. (save-match-data
  14565. (let* ((start 0)
  14566. (re org-ts-regexp-both)
  14567. t1 with-hm inactive tf time str beg end)
  14568. (while (setq start (string-match re string start))
  14569. (setq beg (match-beginning 0)
  14570. end (match-end 0)
  14571. t1 (save-match-data
  14572. (org-parse-time-string (substring string beg end) t))
  14573. with-hm (and (nth 1 t1) (nth 2 t1))
  14574. inactive (equal (substring string beg (1+ beg)) "[")
  14575. tf (funcall (if with-hm 'cdr 'car)
  14576. org-time-stamp-custom-formats)
  14577. time (org-fix-decoded-time t1)
  14578. str (format-time-string
  14579. (concat
  14580. (if inactive "[" "<") (substring tf 1 -1)
  14581. (if inactive "]" ">"))
  14582. (apply 'encode-time time))
  14583. string (replace-match str t t string)
  14584. start (+ start (length str)))))))
  14585. string)
  14586. (defun org-fix-decoded-time (time)
  14587. "Set 0 instead of nil for the first 6 elements of time.
  14588. Don't touch the rest."
  14589. (let ((n 0))
  14590. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14591. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14592. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14593. "Difference between TIMESTAMP-STRING and now in days.
  14594. If SECONDS is non-nil, return the difference in seconds."
  14595. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14596. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14597. (funcall fdiff (current-time)))))
  14598. (defun org-deadline-close (timestamp-string &optional ndays)
  14599. "Is the time in TIMESTAMP-STRING close to the current date?"
  14600. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14601. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14602. (not (org-entry-is-done-p))))
  14603. (defun org-get-wdays (ts)
  14604. "Get the deadline lead time appropriate for timestring TS."
  14605. (cond
  14606. ((<= org-deadline-warning-days 0)
  14607. ;; 0 or negative, enforce this value no matter what
  14608. (- org-deadline-warning-days))
  14609. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14610. ;; lead time is specified.
  14611. (floor (* (string-to-number (match-string 1 ts))
  14612. (cdr (assoc (match-string 2 ts)
  14613. '(("d" . 1) ("w" . 7)
  14614. ("m" . 30.4) ("y" . 365.25)))))))
  14615. ;; go for the default.
  14616. (t org-deadline-warning-days)))
  14617. (defun org-calendar-select-mouse (ev)
  14618. "Return to `org-read-date' with the date currently selected.
  14619. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14620. (interactive "e")
  14621. (mouse-set-point ev)
  14622. (when (calendar-cursor-to-date)
  14623. (let* ((date (calendar-cursor-to-date))
  14624. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14625. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14626. (if (active-minibuffer-window) (exit-minibuffer))))
  14627. (defun org-check-deadlines (ndays)
  14628. "Check if there are any deadlines due or past due.
  14629. A deadline is considered due if it happens within `org-deadline-warning-days'
  14630. days from today's date. If the deadline appears in an entry marked DONE,
  14631. it is not shown. The prefix arg NDAYS can be used to test that many
  14632. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14633. (interactive "P")
  14634. (let* ((org-warn-days
  14635. (cond
  14636. ((equal ndays '(4)) 100000)
  14637. (ndays (prefix-numeric-value ndays))
  14638. (t (abs org-deadline-warning-days))))
  14639. (case-fold-search nil)
  14640. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14641. (callback
  14642. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14643. (message "%d deadlines past-due or due within %d days"
  14644. (org-occur regexp nil callback)
  14645. org-warn-days)))
  14646. (defsubst org-re-timestamp (type)
  14647. "Return a regexp for timestamp TYPE.
  14648. Allowed values for TYPE are:
  14649. all: all timestamps
  14650. active: only active timestamps (<...>)
  14651. inactive: only inactive timestamps ([...])
  14652. scheduled: only scheduled timestamps
  14653. deadline: only deadline timestamps
  14654. When TYPE is nil, fall back on returning a regexp that matches
  14655. both scheduled and deadline timestamps."
  14656. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14657. ((eq type 'active) org-ts-regexp)
  14658. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14659. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14660. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14661. ((eq type 'scheduled-or-deadline)
  14662. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14663. (defun org-check-before-date (date)
  14664. "Check if there are deadlines or scheduled entries before DATE."
  14665. (interactive (list (org-read-date)))
  14666. (let ((case-fold-search nil)
  14667. (regexp (org-re-timestamp org-ts-type))
  14668. (callback
  14669. (lambda () (time-less-p
  14670. (org-time-string-to-time (match-string 1))
  14671. (org-time-string-to-time date)))))
  14672. (message "%d entries before %s"
  14673. (org-occur regexp nil callback) date)))
  14674. (defun org-check-after-date (date)
  14675. "Check if there are deadlines or scheduled entries after DATE."
  14676. (interactive (list (org-read-date)))
  14677. (let ((case-fold-search nil)
  14678. (regexp (org-re-timestamp org-ts-type))
  14679. (callback
  14680. (lambda () (not
  14681. (time-less-p
  14682. (org-time-string-to-time (match-string 1))
  14683. (org-time-string-to-time date))))))
  14684. (message "%d entries after %s"
  14685. (org-occur regexp nil callback) date)))
  14686. (defun org-check-dates-range (start-date end-date)
  14687. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14688. (interactive (list (org-read-date nil nil nil "Range starts")
  14689. (org-read-date nil nil nil "Range end")))
  14690. (let ((case-fold-search nil)
  14691. (regexp (org-re-timestamp org-ts-type))
  14692. (callback
  14693. (lambda ()
  14694. (let ((match (match-string 1)))
  14695. (and
  14696. (not (time-less-p
  14697. (org-time-string-to-time match)
  14698. (org-time-string-to-time start-date)))
  14699. (time-less-p
  14700. (org-time-string-to-time match)
  14701. (org-time-string-to-time end-date)))))))
  14702. (message "%d entries between %s and %s"
  14703. (org-occur regexp nil callback) start-date end-date)))
  14704. (defun org-evaluate-time-range (&optional to-buffer)
  14705. "Evaluate a time range by computing the difference between start and end.
  14706. Normally the result is just printed in the echo area, but with prefix arg
  14707. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14708. If the time range is actually in a table, the result is inserted into the
  14709. next column.
  14710. For time difference computation, a year is assumed to be exactly 365
  14711. days in order to avoid rounding problems."
  14712. (interactive "P")
  14713. (or
  14714. (org-clock-update-time-maybe)
  14715. (save-excursion
  14716. (unless (org-at-date-range-p t)
  14717. (goto-char (point-at-bol))
  14718. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14719. (if (not (org-at-date-range-p t))
  14720. (error "Not at a time-stamp range, and none found in current line")))
  14721. (let* ((ts1 (match-string 1))
  14722. (ts2 (match-string 2))
  14723. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14724. (match-end (match-end 0))
  14725. (time1 (org-time-string-to-time ts1))
  14726. (time2 (org-time-string-to-time ts2))
  14727. (t1 (org-float-time time1))
  14728. (t2 (org-float-time time2))
  14729. (diff (abs (- t2 t1)))
  14730. (negative (< (- t2 t1) 0))
  14731. ;; (ys (floor (* 365 24 60 60)))
  14732. (ds (* 24 60 60))
  14733. (hs (* 60 60))
  14734. (fy "%dy %dd %02d:%02d")
  14735. (fy1 "%dy %dd")
  14736. (fd "%dd %02d:%02d")
  14737. (fd1 "%dd")
  14738. (fh "%02d:%02d")
  14739. y d h m align)
  14740. (if havetime
  14741. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14742. y 0
  14743. d (floor (/ diff ds)) diff (mod diff ds)
  14744. h (floor (/ diff hs)) diff (mod diff hs)
  14745. m (floor (/ diff 60)))
  14746. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14747. y 0
  14748. d (floor (+ (/ diff ds) 0.5))
  14749. h 0 m 0))
  14750. (if (not to-buffer)
  14751. (message "%s" (org-make-tdiff-string y d h m))
  14752. (if (org-at-table-p)
  14753. (progn
  14754. (goto-char match-end)
  14755. (setq align t)
  14756. (and (looking-at " *|") (goto-char (match-end 0))))
  14757. (goto-char match-end))
  14758. (if (looking-at
  14759. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14760. (replace-match ""))
  14761. (if negative (insert " -"))
  14762. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14763. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14764. (insert " " (format fh h m))))
  14765. (if align (org-table-align))
  14766. (message "Time difference inserted")))))
  14767. (defun org-make-tdiff-string (y d h m)
  14768. (let ((fmt "")
  14769. (l nil))
  14770. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14771. l (push y l)))
  14772. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14773. l (push d l)))
  14774. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14775. l (push h l)))
  14776. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14777. l (push m l)))
  14778. (apply 'format fmt (nreverse l))))
  14779. (defun org-time-string-to-time (s &optional buffer pos)
  14780. "Convert a timestamp string into internal time."
  14781. (condition-case errdata
  14782. (apply 'encode-time (org-parse-time-string s))
  14783. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14784. s (if (not (and buffer pos))
  14785. ""
  14786. (format " at %d in buffer `%s'" pos buffer))
  14787. (cdr errdata)))))
  14788. (defun org-time-string-to-seconds (s)
  14789. "Convert a timestamp string to a number of seconds."
  14790. (org-float-time (org-time-string-to-time s)))
  14791. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14792. "Convert a time stamp to an absolute day number.
  14793. If there is a specifier for a cyclic time stamp, get the closest date to
  14794. DAYNR.
  14795. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14796. The variable date is bound by the calendar when this is called."
  14797. (cond
  14798. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14799. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14800. daynr
  14801. (+ daynr 1000)))
  14802. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14803. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14804. (time-to-days (current-time))) (match-string 0 s)
  14805. prefer show-all))
  14806. (t (time-to-days
  14807. (condition-case errdata
  14808. (apply 'encode-time (org-parse-time-string s))
  14809. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14810. s (if (not (and buffer pos))
  14811. ""
  14812. (format " at %d in buffer `%s'" pos buffer))
  14813. (cdr errdata))))))))
  14814. (defun org-days-to-iso-week (days)
  14815. "Return the iso week number."
  14816. (require 'cal-iso)
  14817. (car (calendar-iso-from-absolute days)))
  14818. (defun org-small-year-to-year (year)
  14819. "Convert 2-digit years into 4-digit years.
  14820. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14821. The year 2000 cannot be abbreviated. Any year larger than 99
  14822. is returned unchanged."
  14823. (if (< year 38)
  14824. (setq year (+ 2000 year))
  14825. (if (< year 100)
  14826. (setq year (+ 1900 year))))
  14827. year)
  14828. (defun org-time-from-absolute (d)
  14829. "Return the time corresponding to date D.
  14830. D may be an absolute day number, or a calendar-type list (month day year)."
  14831. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14832. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14833. (defun org-calendar-holiday ()
  14834. "List of holidays, for Diary display in Org-mode."
  14835. (require 'holidays)
  14836. (let ((hl (funcall
  14837. (if (fboundp 'calendar-check-holidays)
  14838. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14839. (if hl (mapconcat 'identity hl "; "))))
  14840. (defun org-diary-sexp-entry (sexp entry date)
  14841. "Process a SEXP diary ENTRY for DATE."
  14842. (require 'diary-lib)
  14843. (let ((result (if calendar-debug-sexp
  14844. (let ((stack-trace-on-error t))
  14845. (eval (car (read-from-string sexp))))
  14846. (condition-case nil
  14847. (eval (car (read-from-string sexp)))
  14848. (error
  14849. (beep)
  14850. (message "Bad sexp at line %d in %s: %s"
  14851. (org-current-line)
  14852. (buffer-file-name) sexp)
  14853. (sleep-for 2))))))
  14854. (cond ((stringp result) (split-string result "; "))
  14855. ((and (consp result)
  14856. (not (consp (cdr result)))
  14857. (stringp (cdr result))) (cdr result))
  14858. ((and (consp result)
  14859. (stringp (car result))) result)
  14860. (result entry))))
  14861. (defun org-diary-to-ical-string (frombuf)
  14862. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14863. This uses the icalendar.el library."
  14864. (let* ((tmpdir (if (featurep 'xemacs)
  14865. (temp-directory)
  14866. temporary-file-directory))
  14867. (tmpfile (make-temp-name
  14868. (expand-file-name "orgics" tmpdir)))
  14869. buf rtn b e)
  14870. (with-current-buffer frombuf
  14871. (icalendar-export-region (point-min) (point-max) tmpfile)
  14872. (setq buf (find-buffer-visiting tmpfile))
  14873. (set-buffer buf)
  14874. (goto-char (point-min))
  14875. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14876. (setq b (match-beginning 0)))
  14877. (goto-char (point-max))
  14878. (if (re-search-backward "^END:VEVENT" nil t)
  14879. (setq e (match-end 0)))
  14880. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14881. (kill-buffer buf)
  14882. (delete-file tmpfile)
  14883. rtn))
  14884. (defun org-closest-date (start current change prefer show-all)
  14885. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14886. When PREFER is `past', return a date that is either CURRENT or past.
  14887. When PREFER is `future', return a date that is either CURRENT or future.
  14888. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14889. ;; Make the proper lists from the dates
  14890. (catch 'exit
  14891. (let ((a1 '(("h" . hour)
  14892. ("d" . day)
  14893. ("w" . week)
  14894. ("m" . month)
  14895. ("y" . year)))
  14896. (shour (nth 2 (org-parse-time-string start)))
  14897. dn dw sday cday n1 n2 n0
  14898. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14899. (setq start (org-date-to-gregorian start)
  14900. current (org-date-to-gregorian
  14901. (if show-all
  14902. current
  14903. (time-to-days (current-time))))
  14904. sday (calendar-absolute-from-gregorian start)
  14905. cday (calendar-absolute-from-gregorian current))
  14906. (if (<= cday sday) (throw 'exit sday))
  14907. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14908. (setq dn (string-to-number (match-string 1 change))
  14909. dw (cdr (assoc (match-string 2 change) a1)))
  14910. (error "Invalid change specifier: %s" change))
  14911. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14912. (cond
  14913. ((eq dw 'hour)
  14914. (let ((missing-hours
  14915. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  14916. dn)))
  14917. (setq n1 (if (zerop missing-hours) cday
  14918. (- cday (1+ (floor (/ missing-hours 24)))))
  14919. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  14920. ((eq dw 'day)
  14921. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14922. n2 (+ n1 dn)))
  14923. ((eq dw 'year)
  14924. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14925. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14926. (setq date1 (list m d y1)
  14927. n1 (calendar-absolute-from-gregorian date1)
  14928. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14929. n2 (calendar-absolute-from-gregorian date2)))
  14930. ((eq dw 'month)
  14931. ;; approx number of month between the two dates
  14932. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14933. ;; How often does dn fit in there?
  14934. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14935. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14936. m (+ m nm)
  14937. ny (floor (/ m 12))
  14938. y (+ y ny)
  14939. m (- m (* ny 12)))
  14940. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14941. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14942. (setq m2 (+ m dn) y2 y)
  14943. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14944. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14945. (while (<= n2 cday)
  14946. (setq n1 n2 m m2 y y2)
  14947. (setq m2 (+ m dn) y2 y)
  14948. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14949. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14950. ;; Make sure n1 is the earlier date
  14951. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14952. (if show-all
  14953. (cond
  14954. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14955. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14956. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14957. (cond
  14958. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14959. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14960. (t (if (= cday n1) n1 n2)))))))
  14961. (defun org-date-to-gregorian (date)
  14962. "Turn any specification of DATE into a Gregorian date for the calendar."
  14963. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14964. ((and (listp date) (= (length date) 3)) date)
  14965. ((stringp date)
  14966. (setq date (org-parse-time-string date))
  14967. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14968. ((listp date)
  14969. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14970. (defun org-parse-time-string (s &optional nodefault)
  14971. "Parse the standard Org-mode time string.
  14972. This should be a lot faster than the normal `parse-time-string'.
  14973. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14974. hour and minute fields will be nil if not given."
  14975. (if (string-match org-ts-regexp0 s)
  14976. (list 0
  14977. (if (or (match-beginning 8) (not nodefault))
  14978. (string-to-number (or (match-string 8 s) "0")))
  14979. (if (or (match-beginning 7) (not nodefault))
  14980. (string-to-number (or (match-string 7 s) "0")))
  14981. (string-to-number (match-string 4 s))
  14982. (string-to-number (match-string 3 s))
  14983. (string-to-number (match-string 2 s))
  14984. nil nil nil)
  14985. (error "Not a standard Org-mode time string: %s" s)))
  14986. (defun org-timestamp-up (&optional arg)
  14987. "Increase the date item at the cursor by one.
  14988. If the cursor is on the year, change the year. If it is on the month,
  14989. the day or the time, change that.
  14990. With prefix ARG, change by that many units."
  14991. (interactive "p")
  14992. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14993. (defun org-timestamp-down (&optional arg)
  14994. "Decrease the date item at the cursor by one.
  14995. If the cursor is on the year, change the year. If it is on the month,
  14996. the day or the time, change that.
  14997. With prefix ARG, change by that many units."
  14998. (interactive "p")
  14999. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15000. (defun org-timestamp-up-day (&optional arg)
  15001. "Increase the date in the time stamp by one day.
  15002. With prefix ARG, change that many days."
  15003. (interactive "p")
  15004. (if (and (not (org-at-timestamp-p t))
  15005. (org-at-heading-p))
  15006. (org-todo 'up)
  15007. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15008. (defun org-timestamp-down-day (&optional arg)
  15009. "Decrease the date in the time stamp by one day.
  15010. With prefix ARG, change that many days."
  15011. (interactive "p")
  15012. (if (and (not (org-at-timestamp-p t))
  15013. (org-at-heading-p))
  15014. (org-todo 'down)
  15015. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15016. (defun org-at-timestamp-p (&optional inactive-ok)
  15017. "Determine if the cursor is in or at a timestamp."
  15018. (interactive)
  15019. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15020. (pos (point))
  15021. (ans (or (looking-at tsr)
  15022. (save-excursion
  15023. (skip-chars-backward "^[<\n\r\t")
  15024. (if (> (point) (point-min)) (backward-char 1))
  15025. (and (looking-at tsr)
  15026. (> (- (match-end 0) pos) -1))))))
  15027. (and ans
  15028. (boundp 'org-ts-what)
  15029. (setq org-ts-what
  15030. (cond
  15031. ((= pos (match-beginning 0)) 'bracket)
  15032. ;; Point is considered to be "on the bracket" whether
  15033. ;; it's really on it or right after it.
  15034. ((= pos (1- (match-end 0))) 'bracket)
  15035. ((= pos (match-end 0)) 'after)
  15036. ((org-pos-in-match-range pos 2) 'year)
  15037. ((org-pos-in-match-range pos 3) 'month)
  15038. ((org-pos-in-match-range pos 7) 'hour)
  15039. ((org-pos-in-match-range pos 8) 'minute)
  15040. ((or (org-pos-in-match-range pos 4)
  15041. (org-pos-in-match-range pos 5)) 'day)
  15042. ((and (> pos (or (match-end 8) (match-end 5)))
  15043. (< pos (match-end 0)))
  15044. (- pos (or (match-end 8) (match-end 5))))
  15045. (t 'day))))
  15046. ans))
  15047. (defun org-toggle-timestamp-type ()
  15048. "Toggle the type (<active> or [inactive]) of a time stamp."
  15049. (interactive)
  15050. (when (org-at-timestamp-p t)
  15051. (let ((beg (match-beginning 0)) (end (match-end 0))
  15052. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15053. (save-excursion
  15054. (goto-char beg)
  15055. (while (re-search-forward "[][<>]" end t)
  15056. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15057. t t)))
  15058. (message "Timestamp is now %sactive"
  15059. (if (equal (char-after beg) ?<) "" "in")))))
  15060. (defvar org-clock-history) ; defined in org-clock.el
  15061. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15062. (defun org-timestamp-change (n &optional what updown)
  15063. "Change the date in the time stamp at point.
  15064. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15065. `year', `minute', `second'. If WHAT is not given, the cursor position
  15066. in the timestamp determines what will be changed."
  15067. (let ((origin (point)) origin-cat
  15068. with-hm inactive
  15069. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15070. org-ts-what
  15071. extra rem
  15072. ts time time0 fixnext clrgx)
  15073. (if (not (org-at-timestamp-p t))
  15074. (error "Not at a timestamp"))
  15075. (if (and (not what) (eq org-ts-what 'bracket))
  15076. (org-toggle-timestamp-type)
  15077. ;; Point isn't on brackets. Remember the part of the time-stamp
  15078. ;; the point was in. Indeed, size of time-stamps may change,
  15079. ;; but point must be kept in the same category nonetheless.
  15080. (setq origin-cat org-ts-what)
  15081. (if (and (not what) (not (eq org-ts-what 'day))
  15082. org-display-custom-times
  15083. (get-text-property (point) 'display)
  15084. (not (get-text-property (1- (point)) 'display)))
  15085. (setq org-ts-what 'day))
  15086. (setq org-ts-what (or what org-ts-what)
  15087. inactive (= (char-after (match-beginning 0)) ?\[)
  15088. ts (match-string 0))
  15089. (replace-match "")
  15090. (if (string-match
  15091. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15092. ts)
  15093. (setq extra (match-string 1 ts)))
  15094. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15095. (setq with-hm t))
  15096. (setq time0 (org-parse-time-string ts))
  15097. (when (and updown
  15098. (eq org-ts-what 'minute)
  15099. (not current-prefix-arg))
  15100. ;; This looks like s-up and s-down. Change by one rounding step.
  15101. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15102. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15103. (setcar (cdr time0) (+ (nth 1 time0)
  15104. (if (> n 0) (- rem) (- dm rem))))))
  15105. (setq time
  15106. (encode-time (or (car time0) 0)
  15107. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15108. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15109. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15110. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15111. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15112. (nthcdr 6 time0)))
  15113. (when (and (member org-ts-what '(hour minute))
  15114. extra
  15115. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15116. (setq extra (org-modify-ts-extra
  15117. extra
  15118. (if (eq org-ts-what 'hour) 2 5)
  15119. n dm)))
  15120. (when (integerp org-ts-what)
  15121. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15122. (if (eq what 'calendar)
  15123. (let ((cal-date (org-get-date-from-calendar)))
  15124. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15125. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15126. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15127. (setcar time0 (or (car time0) 0))
  15128. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15129. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15130. (setq time (apply 'encode-time time0))))
  15131. ;; Insert the new time-stamp, and ensure point stays in the same
  15132. ;; category as before (i.e. not after the last position in that
  15133. ;; category).
  15134. (let ((pos (point)))
  15135. ;; Stay before inserted string. `save-excursion' is of no use.
  15136. (setq org-last-changed-timestamp
  15137. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15138. (goto-char pos))
  15139. (save-match-data
  15140. (looking-at org-ts-regexp3)
  15141. (goto-char (cond
  15142. ;; `day' category ends before `hour' if any, or at
  15143. ;; the end of the day name.
  15144. ((eq origin-cat 'day)
  15145. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15146. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15147. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15148. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15149. ;; `year' and `month' have both fixed size: point
  15150. ;; couldn't have moved into another part.
  15151. (t origin))))
  15152. ;; Update clock if on a CLOCK line.
  15153. (org-clock-update-time-maybe)
  15154. ;; Maybe adjust the closest clock in `org-clock-history'
  15155. (when org-clock-adjust-closest
  15156. (if (not (and (org-at-clock-log-p)
  15157. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15158. org-clock-history))))))
  15159. (message "No clock to adjust")
  15160. (cond ((save-excursion ; fix previous clock?
  15161. (re-search-backward org-ts-regexp0 nil t)
  15162. (org-looking-back (concat org-clock-string " \\[")))
  15163. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15164. ((save-excursion ; fix next clock?
  15165. (re-search-backward org-ts-regexp0 nil t)
  15166. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15167. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15168. (save-window-excursion
  15169. ;; Find closest clock to point, adjust the previous/next one in history
  15170. (let* ((p (save-excursion (org-back-to-heading t)))
  15171. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15172. (clfixnth
  15173. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15174. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15175. (if (not clfixpos)
  15176. (message "No clock to adjust")
  15177. (save-excursion
  15178. (org-goto-marker-or-bmk clfixpos)
  15179. (org-show-subtree)
  15180. (when (re-search-forward clrgx nil t)
  15181. (goto-char (match-beginning 1))
  15182. (let (org-clock-adjust-closest)
  15183. (org-timestamp-change n org-ts-what updown))
  15184. (message "Clock adjusted in %s for heading: %s"
  15185. (file-name-nondirectory (buffer-file-name))
  15186. (org-get-heading t t)))))))))
  15187. ;; Try to recenter the calendar window, if any.
  15188. (if (and org-calendar-follow-timestamp-change
  15189. (get-buffer-window "*Calendar*" t)
  15190. (memq org-ts-what '(day month year)))
  15191. (org-recenter-calendar (time-to-days time))))))
  15192. (defun org-modify-ts-extra (s pos n dm)
  15193. "Change the different parts of the lead-time and repeat fields in timestamp."
  15194. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15195. ng h m new rem)
  15196. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15197. (cond
  15198. ((or (org-pos-in-match-range pos 2)
  15199. (org-pos-in-match-range pos 3))
  15200. (setq m (string-to-number (match-string 3 s))
  15201. h (string-to-number (match-string 2 s)))
  15202. (if (org-pos-in-match-range pos 2)
  15203. (setq h (+ h n))
  15204. (setq n (* dm (org-no-warnings (signum n))))
  15205. (when (not (= 0 (setq rem (% m dm))))
  15206. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15207. (setq m (+ m n)))
  15208. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15209. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15210. (setq h (min 24 (max 0 h)))
  15211. (setq ng 1 new (format "-%02d:%02d" h m)))
  15212. ((org-pos-in-match-range pos 6)
  15213. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15214. ((org-pos-in-match-range pos 5)
  15215. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15216. ((org-pos-in-match-range pos 9)
  15217. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15218. ((org-pos-in-match-range pos 8)
  15219. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15220. (when ng
  15221. (setq s (concat
  15222. (substring s 0 (match-beginning ng))
  15223. new
  15224. (substring s (match-end ng))))))
  15225. s))
  15226. (defun org-recenter-calendar (date)
  15227. "If the calendar is visible, recenter it to DATE."
  15228. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15229. (when cwin
  15230. (let ((calendar-move-hook nil))
  15231. (with-selected-window cwin
  15232. (calendar-goto-date (if (listp date) date
  15233. (calendar-gregorian-from-absolute date))))))))
  15234. (defun org-goto-calendar (&optional arg)
  15235. "Go to the Emacs calendar at the current date.
  15236. If there is a time stamp in the current line, go to that date.
  15237. A prefix ARG can be used to force the current date."
  15238. (interactive "P")
  15239. (let ((tsr org-ts-regexp) diff
  15240. (calendar-move-hook nil)
  15241. (calendar-view-holidays-initially-flag nil)
  15242. (calendar-view-diary-initially-flag nil))
  15243. (if (or (org-at-timestamp-p)
  15244. (save-excursion
  15245. (beginning-of-line 1)
  15246. (looking-at (concat ".*" tsr))))
  15247. (let ((d1 (time-to-days (current-time)))
  15248. (d2 (time-to-days
  15249. (org-time-string-to-time (match-string 1)))))
  15250. (setq diff (- d2 d1))))
  15251. (calendar)
  15252. (calendar-goto-today)
  15253. (if (and diff (not arg)) (calendar-forward-day diff))))
  15254. (defun org-get-date-from-calendar ()
  15255. "Return a list (month day year) of date at point in calendar."
  15256. (with-current-buffer "*Calendar*"
  15257. (save-match-data
  15258. (calendar-cursor-to-date))))
  15259. (defun org-date-from-calendar ()
  15260. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15261. If there is already a time stamp at the cursor position, update it."
  15262. (interactive)
  15263. (if (org-at-timestamp-p t)
  15264. (org-timestamp-change 0 'calendar)
  15265. (let ((cal-date (org-get-date-from-calendar)))
  15266. (org-insert-time-stamp
  15267. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15268. (defun org-minutes-to-hh:mm-string (m)
  15269. "Compute H:MM from a number of minutes."
  15270. (let ((h (/ m 60)))
  15271. (setq m (- m (* 60 h)))
  15272. (format org-time-clocksum-format h m)))
  15273. (defun org-hh:mm-string-to-minutes (s)
  15274. "Convert a string H:MM to a number of minutes.
  15275. If the string is just a number, interpret it as minutes.
  15276. In fact, the first hh:mm or number in the string will be taken,
  15277. there can be extra stuff in the string.
  15278. If no number is found, the return value is 0."
  15279. (cond
  15280. ((integerp s) s)
  15281. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15282. (+ (* (string-to-number (match-string 1 s)) 60)
  15283. (string-to-number (match-string 2 s))))
  15284. ((string-match "\\([0-9]+\\)" s)
  15285. (string-to-number (match-string 1 s)))
  15286. (t 0)))
  15287. (defcustom org-effort-durations
  15288. `(("h" . 60)
  15289. ("d" . ,(* 60 8))
  15290. ("w" . ,(* 60 8 5))
  15291. ("m" . ,(* 60 8 5 4))
  15292. ("y" . ,(* 60 8 5 40)))
  15293. "Conversion factor to minutes for an effort modifier.
  15294. Each entry has the form (MODIFIER . MINUTES).
  15295. In an effort string, a number followed by MODIFIER is multiplied
  15296. by the specified number of MINUTES to obtain an effort in
  15297. minutes.
  15298. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15299. effort string \"2hours\" is equivalent to 120 minutes."
  15300. :group 'org-agenda
  15301. :version "24.1"
  15302. :type '(alist :key-type (string :tag "Modifier")
  15303. :value-type (number :tag "Minutes")))
  15304. (defcustom org-image-actual-width t
  15305. "Should we use the actual width of images when inlining them?
  15306. When set to `t', always use the image width.
  15307. When set to a number, use imagemagick (when available) to set
  15308. the image's width to this value.
  15309. When set to a number in a list, try to get the width from the
  15310. #+ATTR.* keyword if it matches a width specification like
  15311. width=\"[0-9]+\", and fall back on that number if none is found.
  15312. When set to nil, try to get the width from an #+ATTR.* keyword
  15313. and fall back on the original width if none is found.
  15314. This requires Emacs >= 24.1, build with imagemagick support."
  15315. :group 'org-appearance
  15316. :version "24.3"
  15317. :type '(choice
  15318. (const :tag "Use the image width" t)
  15319. (integer :tag "Use a number of pixels")
  15320. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15321. (const :tag "Use #+ATTR* or don't resize" nil)))
  15322. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15323. "Convert a duration string S to minutes.
  15324. A bare number is interpreted as minutes, modifiers can be set by
  15325. customizing `org-effort-durations' (which see).
  15326. Entries containing a colon are interpreted as H:MM by
  15327. `org-hh:mm-string-to-minutes'."
  15328. (let ((result 0)
  15329. (re (concat "\\([0-9.]+\\) *\\("
  15330. (regexp-opt (mapcar 'car org-effort-durations))
  15331. "\\)")))
  15332. (while (string-match re s)
  15333. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15334. (string-to-number (match-string 1 s))))
  15335. (setq s (replace-match "" nil t s)))
  15336. (setq result (floor result))
  15337. (incf result (org-hh:mm-string-to-minutes s))
  15338. (if output-to-string (number-to-string result) result)))
  15339. ;;;; Files
  15340. (defun org-save-all-org-buffers ()
  15341. "Save all Org-mode buffers without user confirmation."
  15342. (interactive)
  15343. (message "Saving all Org-mode buffers...")
  15344. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15345. (when (featurep 'org-id) (org-id-locations-save))
  15346. (message "Saving all Org-mode buffers... done"))
  15347. (defun org-revert-all-org-buffers ()
  15348. "Revert all Org-mode buffers.
  15349. Prompt for confirmation when there are unsaved changes.
  15350. Be sure you know what you are doing before letting this function
  15351. overwrite your changes.
  15352. This function is useful in a setup where one tracks org files
  15353. with a version control system, to revert on one machine after pulling
  15354. changes from another. I believe the procedure must be like this:
  15355. 1. M-x org-save-all-org-buffers
  15356. 2. Pull changes from the other machine, resolve conflicts
  15357. 3. M-x org-revert-all-org-buffers"
  15358. (interactive)
  15359. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15360. (error "Abort"))
  15361. (save-excursion
  15362. (save-window-excursion
  15363. (mapc
  15364. (lambda (b)
  15365. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15366. (with-current-buffer b buffer-file-name))
  15367. (org-pop-to-buffer-same-window b)
  15368. (revert-buffer t 'no-confirm)))
  15369. (buffer-list))
  15370. (when (and (featurep 'org-id) org-id-track-globally)
  15371. (org-id-locations-load)))))
  15372. ;;;; Agenda files
  15373. ;;;###autoload
  15374. (defun org-switchb (&optional arg)
  15375. "Switch between Org buffers.
  15376. With one prefix argument, restrict available buffers to files.
  15377. With two prefix arguments, restrict available buffers to agenda files.
  15378. Defaults to `iswitchb' for buffer name completion.
  15379. Set `org-completion-use-ido' to make it use ido instead."
  15380. (interactive "P")
  15381. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15382. ((equal arg '(16)) (org-buffer-list 'agenda))
  15383. (t (org-buffer-list))))
  15384. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15385. (org-completion-use-ido org-completion-use-ido))
  15386. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15387. (setq org-completion-use-iswitchb t))
  15388. (org-pop-to-buffer-same-window
  15389. (org-icompleting-read "Org buffer: "
  15390. (mapcar 'list (mapcar 'buffer-name blist))
  15391. nil t))))
  15392. ;;; Define some older names previously used for this functionality
  15393. ;;;###autoload
  15394. (defalias 'org-ido-switchb 'org-switchb)
  15395. ;;;###autoload
  15396. (defalias 'org-iswitchb 'org-switchb)
  15397. (defun org-buffer-list (&optional predicate exclude-tmp)
  15398. "Return a list of Org buffers.
  15399. PREDICATE can be `export', `files' or `agenda'.
  15400. export restrict the list to Export buffers.
  15401. files restrict the list to buffers visiting Org files.
  15402. agenda restrict the list to buffers visiting agenda files.
  15403. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15404. (let* ((bfn nil)
  15405. (agenda-files (and (eq predicate 'agenda)
  15406. (mapcar 'file-truename (org-agenda-files t))))
  15407. (filter
  15408. (cond
  15409. ((eq predicate 'files)
  15410. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15411. ((eq predicate 'export)
  15412. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15413. ((eq predicate 'agenda)
  15414. (lambda (b)
  15415. (with-current-buffer b
  15416. (and (derived-mode-p 'org-mode)
  15417. (setq bfn (buffer-file-name b))
  15418. (member (file-truename bfn) agenda-files)))))
  15419. (t (lambda (b) (with-current-buffer b
  15420. (or (derived-mode-p 'org-mode)
  15421. (string-match "\*Org .*Export"
  15422. (buffer-name b)))))))))
  15423. (delq nil
  15424. (mapcar
  15425. (lambda(b)
  15426. (if (and (funcall filter b)
  15427. (or (not exclude-tmp)
  15428. (not (string-match "tmp" (buffer-name b)))))
  15429. b
  15430. nil))
  15431. (buffer-list)))))
  15432. (defun org-agenda-files (&optional unrestricted archives)
  15433. "Get the list of agenda files.
  15434. Optional UNRESTRICTED means return the full list even if a restriction
  15435. is currently in place.
  15436. When ARCHIVES is t, include all archive files that are really being
  15437. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15438. `org-agenda-archives-mode' is t."
  15439. (let ((files
  15440. (cond
  15441. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15442. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15443. ((listp org-agenda-files) org-agenda-files)
  15444. (t (error "Invalid value of `org-agenda-files'")))))
  15445. (setq files (apply 'append
  15446. (mapcar (lambda (f)
  15447. (if (file-directory-p f)
  15448. (directory-files
  15449. f t org-agenda-file-regexp)
  15450. (list f)))
  15451. files)))
  15452. (when org-agenda-skip-unavailable-files
  15453. (setq files (delq nil
  15454. (mapcar (function
  15455. (lambda (file)
  15456. (and (file-readable-p file) file)))
  15457. files))))
  15458. (when (or (eq archives t)
  15459. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15460. (setq files (org-add-archive-files files)))
  15461. files))
  15462. (defun org-agenda-file-p (&optional file)
  15463. "Return non-nil, if FILE is an agenda file.
  15464. If FILE is omitted, use the file associated with the current
  15465. buffer."
  15466. (member (or file (buffer-file-name))
  15467. (org-agenda-files t)))
  15468. (defun org-edit-agenda-file-list ()
  15469. "Edit the list of agenda files.
  15470. Depending on setup, this either uses customize to edit the variable
  15471. `org-agenda-files', or it visits the file that is holding the list. In the
  15472. latter case, the buffer is set up in a way that saving it automatically kills
  15473. the buffer and restores the previous window configuration."
  15474. (interactive)
  15475. (if (stringp org-agenda-files)
  15476. (let ((cw (current-window-configuration)))
  15477. (find-file org-agenda-files)
  15478. (org-set-local 'org-window-configuration cw)
  15479. (org-add-hook 'after-save-hook
  15480. (lambda ()
  15481. (set-window-configuration
  15482. (prog1 org-window-configuration
  15483. (kill-buffer (current-buffer))))
  15484. (org-install-agenda-files-menu)
  15485. (message "New agenda file list installed"))
  15486. nil 'local)
  15487. (message "%s" (substitute-command-keys
  15488. "Edit list and finish with \\[save-buffer]")))
  15489. (customize-variable 'org-agenda-files)))
  15490. (defun org-store-new-agenda-file-list (list)
  15491. "Set new value for the agenda file list and save it correctly."
  15492. (if (stringp org-agenda-files)
  15493. (let ((fe (org-read-agenda-file-list t)) b u)
  15494. (while (setq b (find-buffer-visiting org-agenda-files))
  15495. (kill-buffer b))
  15496. (with-temp-file org-agenda-files
  15497. (insert
  15498. (mapconcat
  15499. (lambda (f) ;; Keep un-expanded entries.
  15500. (if (setq u (assoc f fe))
  15501. (cdr u)
  15502. f))
  15503. list "\n")
  15504. "\n")))
  15505. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15506. (org-insert-mode-line-in-empty-file nil))
  15507. (setq org-agenda-files list)
  15508. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15509. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15510. "Read the list of agenda files from a file.
  15511. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15512. filenames, used by `org-store-new-agenda-file-list' to write back
  15513. un-expanded file names."
  15514. (when (file-directory-p org-agenda-files)
  15515. (error "`org-agenda-files' cannot be a single directory"))
  15516. (when (stringp org-agenda-files)
  15517. (with-temp-buffer
  15518. (insert-file-contents org-agenda-files)
  15519. (mapcar
  15520. (lambda (f)
  15521. (let ((e (expand-file-name (substitute-in-file-name f)
  15522. org-directory)))
  15523. (if pair-with-expansion
  15524. (cons e f)
  15525. e)))
  15526. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15527. ;;;###autoload
  15528. (defun org-cycle-agenda-files ()
  15529. "Cycle through the files in `org-agenda-files'.
  15530. If the current buffer visits an agenda file, find the next one in the list.
  15531. If the current buffer does not, find the first agenda file."
  15532. (interactive)
  15533. (let* ((fs (org-agenda-files t))
  15534. (files (append fs (list (car fs))))
  15535. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15536. file)
  15537. (unless files (error "No agenda files"))
  15538. (catch 'exit
  15539. (while (setq file (pop files))
  15540. (if (equal (file-truename file) tcf)
  15541. (when (car files)
  15542. (find-file (car files))
  15543. (throw 'exit t))))
  15544. (find-file (car fs)))
  15545. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15546. (defun org-agenda-file-to-front (&optional to-end)
  15547. "Move/add the current file to the top of the agenda file list.
  15548. If the file is not present in the list, it is added to the front. If it is
  15549. present, it is moved there. With optional argument TO-END, add/move to the
  15550. end of the list."
  15551. (interactive "P")
  15552. (let ((org-agenda-skip-unavailable-files nil)
  15553. (file-alist (mapcar (lambda (x)
  15554. (cons (file-truename x) x))
  15555. (org-agenda-files t)))
  15556. (ctf (file-truename buffer-file-name))
  15557. x had)
  15558. (setq x (assoc ctf file-alist) had x)
  15559. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15560. (if to-end
  15561. (setq file-alist (append (delq x file-alist) (list x)))
  15562. (setq file-alist (cons x (delq x file-alist))))
  15563. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15564. (org-install-agenda-files-menu)
  15565. (message "File %s to %s of agenda file list"
  15566. (if had "moved" "added") (if to-end "end" "front"))))
  15567. (defun org-remove-file (&optional file)
  15568. "Remove current file from the list of files in variable `org-agenda-files'.
  15569. These are the files which are being checked for agenda entries.
  15570. Optional argument FILE means use this file instead of the current."
  15571. (interactive)
  15572. (let* ((org-agenda-skip-unavailable-files nil)
  15573. (file (or file buffer-file-name))
  15574. (true-file (file-truename file))
  15575. (afile (abbreviate-file-name file))
  15576. (files (delq nil (mapcar
  15577. (lambda (x)
  15578. (if (equal true-file
  15579. (file-truename x))
  15580. nil x))
  15581. (org-agenda-files t)))))
  15582. (if (not (= (length files) (length (org-agenda-files t))))
  15583. (progn
  15584. (org-store-new-agenda-file-list files)
  15585. (org-install-agenda-files-menu)
  15586. (message "Removed file: %s" afile))
  15587. (message "File was not in list: %s (not removed)" afile))))
  15588. (defun org-file-menu-entry (file)
  15589. (vector file (list 'find-file file) t))
  15590. (defun org-check-agenda-file (file)
  15591. "Make sure FILE exists. If not, ask user what to do."
  15592. (when (not (file-exists-p file))
  15593. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15594. (abbreviate-file-name file))
  15595. (let ((r (downcase (read-char-exclusive))))
  15596. (cond
  15597. ((equal r ?r)
  15598. (org-remove-file file)
  15599. (throw 'nextfile t))
  15600. (t (error "Abort"))))))
  15601. (defun org-get-agenda-file-buffer (file)
  15602. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15603. it to the list of buffers which might be released later."
  15604. (let ((buf (org-find-base-buffer-visiting file)))
  15605. (if buf
  15606. buf ; just return it
  15607. ;; Make a new buffer and remember it
  15608. (setq buf (find-file-noselect file))
  15609. (if buf (push buf org-agenda-new-buffers))
  15610. buf)))
  15611. (defun org-release-buffers (blist)
  15612. "Release all buffers in list, asking the user for confirmation when needed.
  15613. When a buffer is unmodified, it is just killed. When modified, it is saved
  15614. \(if the user agrees) and then killed."
  15615. (let (buf file)
  15616. (while (setq buf (pop blist))
  15617. (setq file (buffer-file-name buf))
  15618. (when (and (buffer-modified-p buf)
  15619. file
  15620. (y-or-n-p (format "Save file %s? " file)))
  15621. (with-current-buffer buf (save-buffer)))
  15622. (kill-buffer buf))))
  15623. (defun org-agenda-prepare-buffers (files)
  15624. "Create buffers for all agenda files, protect archived trees and comments."
  15625. (interactive)
  15626. (let ((pa '(:org-archived t))
  15627. (pc '(:org-comment t))
  15628. (pall '(:org-archived t :org-comment t))
  15629. (inhibit-read-only t)
  15630. (rea (concat ":" org-archive-tag ":"))
  15631. bmp file re)
  15632. (save-excursion
  15633. (save-restriction
  15634. (while (setq file (pop files))
  15635. (catch 'nextfile
  15636. (if (bufferp file)
  15637. (set-buffer file)
  15638. (org-check-agenda-file file)
  15639. (set-buffer (org-get-agenda-file-buffer file)))
  15640. (widen)
  15641. (setq bmp (buffer-modified-p))
  15642. (org-refresh-category-properties)
  15643. (setq org-todo-keywords-for-agenda
  15644. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15645. (setq org-done-keywords-for-agenda
  15646. (append org-done-keywords-for-agenda org-done-keywords))
  15647. (setq org-todo-keyword-alist-for-agenda
  15648. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15649. (setq org-drawers-for-agenda
  15650. (append org-drawers-for-agenda org-drawers))
  15651. (setq org-tag-alist-for-agenda
  15652. (append org-tag-alist-for-agenda org-tag-alist))
  15653. (save-excursion
  15654. (remove-text-properties (point-min) (point-max) pall)
  15655. (when org-agenda-skip-archived-trees
  15656. (goto-char (point-min))
  15657. (while (re-search-forward rea nil t)
  15658. (if (org-at-heading-p t)
  15659. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15660. (goto-char (point-min))
  15661. (setq re (format org-heading-keyword-regexp-format
  15662. org-comment-string))
  15663. (while (re-search-forward re nil t)
  15664. (add-text-properties
  15665. (match-beginning 0) (org-end-of-subtree t) pc)))
  15666. (set-buffer-modified-p bmp)))))
  15667. (setq org-todo-keywords-for-agenda
  15668. (org-uniquify org-todo-keywords-for-agenda))
  15669. (setq org-todo-keyword-alist-for-agenda
  15670. (org-uniquify org-todo-keyword-alist-for-agenda)
  15671. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15672. ;;;; Embedded LaTeX
  15673. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15674. "Keymap for the minor `org-cdlatex-mode'.")
  15675. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15676. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15677. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15678. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15679. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15680. (defvar org-cdlatex-texmathp-advice-is-done nil
  15681. "Flag remembering if we have applied the advice to texmathp already.")
  15682. (define-minor-mode org-cdlatex-mode
  15683. "Toggle the minor `org-cdlatex-mode'.
  15684. This mode supports entering LaTeX environment and math in LaTeX fragments
  15685. in Org-mode.
  15686. \\{org-cdlatex-mode-map}"
  15687. nil " OCDL" nil
  15688. (when org-cdlatex-mode
  15689. (require 'cdlatex)
  15690. (run-hooks 'cdlatex-mode-hook)
  15691. (cdlatex-compute-tables))
  15692. (unless org-cdlatex-texmathp-advice-is-done
  15693. (setq org-cdlatex-texmathp-advice-is-done t)
  15694. (defadvice texmathp (around org-math-always-on activate)
  15695. "Always return t in org-mode buffers.
  15696. This is because we want to insert math symbols without dollars even outside
  15697. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15698. an embedded LaTeX fragment, let texmathp do its job.
  15699. \\[org-cdlatex-mode-map]"
  15700. (interactive)
  15701. (let (p)
  15702. (cond
  15703. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15704. ((eq this-command 'cdlatex-math-symbol)
  15705. (setq ad-return-value t
  15706. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15707. (t
  15708. (let ((p (org-inside-LaTeX-fragment-p)))
  15709. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15710. (setq ad-return-value t
  15711. texmathp-why '("Org-mode embedded math" . 0))
  15712. (if p ad-do-it)))))))))
  15713. (defun turn-on-org-cdlatex ()
  15714. "Unconditionally turn on `org-cdlatex-mode'."
  15715. (org-cdlatex-mode 1))
  15716. (defun org-inside-LaTeX-fragment-p ()
  15717. "Test if point is inside a LaTeX fragment.
  15718. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15719. sequence appearing also before point.
  15720. Even though the matchers for math are configurable, this function assumes
  15721. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15722. delimiters are skipped when they have been removed by customization.
  15723. The return value is nil, or a cons cell with the delimiter and the
  15724. position of this delimiter.
  15725. This function does a reasonably good job, but can locally be fooled by
  15726. for example currency specifications. For example it will assume being in
  15727. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15728. fragments that are properly closed, but during editing, we have to live
  15729. with the uncertainty caused by missing closing delimiters. This function
  15730. looks only before point, not after."
  15731. (catch 'exit
  15732. (let ((pos (point))
  15733. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15734. (lim (progn
  15735. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15736. (point)))
  15737. dd-on str (start 0) m re)
  15738. (goto-char pos)
  15739. (when dodollar
  15740. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15741. re (nth 1 (assoc "$" org-latex-regexps)))
  15742. (while (string-match re str start)
  15743. (cond
  15744. ((= (match-end 0) (length str))
  15745. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15746. ((= (match-end 0) (- (length str) 5))
  15747. (throw 'exit nil))
  15748. (t (setq start (match-end 0))))))
  15749. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15750. (goto-char pos)
  15751. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15752. (and (match-beginning 2) (throw 'exit nil))
  15753. ;; count $$
  15754. (while (re-search-backward "\\$\\$" lim t)
  15755. (setq dd-on (not dd-on)))
  15756. (goto-char pos)
  15757. (if dd-on (cons "$$" m))))))
  15758. (defun org-inside-latex-macro-p ()
  15759. "Is point inside a LaTeX macro or its arguments?"
  15760. (save-match-data
  15761. (org-in-regexp
  15762. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15763. (defun org-try-cdlatex-tab ()
  15764. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15765. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15766. - inside a LaTeX fragment, or
  15767. - after the first word in a line, where an abbreviation expansion could
  15768. insert a LaTeX environment."
  15769. (when org-cdlatex-mode
  15770. (cond
  15771. ;; Before any word on the line: No expansion possible.
  15772. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15773. ;; Just after first word on the line: Expand it. Make sure it
  15774. ;; cannot happen on headlines, though.
  15775. ((save-excursion
  15776. (skip-chars-backward "a-zA-Z0-9*")
  15777. (skip-chars-backward " \t")
  15778. (and (bolp) (not (org-at-heading-p))))
  15779. (cdlatex-tab) t)
  15780. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15781. (defun org-cdlatex-underscore-caret (&optional arg)
  15782. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15783. Revert to the normal definition outside of these fragments."
  15784. (interactive "P")
  15785. (if (org-inside-LaTeX-fragment-p)
  15786. (call-interactively 'cdlatex-sub-superscript)
  15787. (let (org-cdlatex-mode)
  15788. (call-interactively (key-binding (vector last-input-event))))))
  15789. (defun org-cdlatex-math-modify (&optional arg)
  15790. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15791. Revert to the normal definition outside of these fragments."
  15792. (interactive "P")
  15793. (if (org-inside-LaTeX-fragment-p)
  15794. (call-interactively 'cdlatex-math-modify)
  15795. (let (org-cdlatex-mode)
  15796. (call-interactively (key-binding (vector last-input-event))))))
  15797. (defvar org-latex-fragment-image-overlays nil
  15798. "List of overlays carrying the images of latex fragments.")
  15799. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15800. (defun org-remove-latex-fragment-image-overlays ()
  15801. "Remove all overlays with LaTeX fragment images in current buffer."
  15802. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15803. (setq org-latex-fragment-image-overlays nil))
  15804. (defun org-preview-latex-fragment (&optional subtree)
  15805. "Preview the LaTeX fragment at point, or all locally or globally.
  15806. If the cursor is in a LaTeX fragment, create the image and overlay
  15807. it over the source code. If there is no fragment at point, display
  15808. all fragments in the current text, from one headline to the next. With
  15809. prefix SUBTREE, display all fragments in the current subtree. With a
  15810. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15811. the cursor is before the first headline,
  15812. display all fragments in the buffer.
  15813. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15814. (interactive "P")
  15815. (unless buffer-file-name
  15816. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15817. (org-remove-latex-fragment-image-overlays)
  15818. (save-excursion
  15819. (save-restriction
  15820. (let (beg end at msg)
  15821. (cond
  15822. ((or (equal subtree '(16))
  15823. (not (save-excursion
  15824. (re-search-backward org-outline-regexp-bol nil t))))
  15825. (setq beg (point-min) end (point-max)
  15826. msg "Creating images for buffer...%s"))
  15827. ((equal subtree '(4))
  15828. (org-back-to-heading)
  15829. (setq beg (point) end (org-end-of-subtree t)
  15830. msg "Creating images for subtree...%s"))
  15831. (t
  15832. (if (setq at (org-inside-LaTeX-fragment-p))
  15833. (goto-char (max (point-min) (- (cdr at) 2)))
  15834. (org-back-to-heading))
  15835. (setq beg (point) end (progn (outline-next-heading) (point))
  15836. msg (if at "Creating image...%s"
  15837. "Creating images for entry...%s"))))
  15838. (message msg "")
  15839. (narrow-to-region beg end)
  15840. (goto-char beg)
  15841. (org-format-latex
  15842. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15843. (file-name-nondirectory
  15844. buffer-file-name)))
  15845. default-directory 'overlays msg at 'forbuffer
  15846. org-latex-create-formula-image-program)
  15847. (message msg "done. Use `C-c C-c' to remove images.")))))
  15848. (defvar org-latex-regexps
  15849. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15850. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15851. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15852. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15853. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15854. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15855. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15856. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15857. "Regular expressions for matching embedded LaTeX.")
  15858. (defvar org-export-have-math nil) ;; dynamic scoping
  15859. (defun org-format-latex (prefix &optional dir overlays msg at
  15860. forbuffer processing-type)
  15861. "Replace LaTeX fragments with links to an image, and produce images.
  15862. Some of the options can be changed using the variable
  15863. `org-format-latex-options'."
  15864. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15865. (let* ((prefixnodir (file-name-nondirectory prefix))
  15866. (absprefix (expand-file-name prefix dir))
  15867. (todir (file-name-directory absprefix))
  15868. (opt org-format-latex-options)
  15869. (optnew org-format-latex-options)
  15870. (matchers (plist-get opt :matchers))
  15871. (re-list org-latex-regexps)
  15872. (org-format-latex-header-extra
  15873. (plist-get (org-infile-export-plist) :latex-header-extra))
  15874. (cnt 0) txt hash link beg end re e checkdir
  15875. string
  15876. m n block-type block linkfile movefile ov)
  15877. ;; Check the different regular expressions
  15878. (while (setq e (pop re-list))
  15879. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15880. block (if block-type "\n\n" ""))
  15881. (when (member m matchers)
  15882. (goto-char (point-min))
  15883. (while (re-search-forward re nil t)
  15884. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15885. (not (get-text-property (match-beginning n)
  15886. 'org-protected))
  15887. (or (not overlays)
  15888. (not (eq (get-char-property (match-beginning n)
  15889. 'org-overlay-type)
  15890. 'org-latex-overlay))))
  15891. (setq org-export-have-math t)
  15892. (cond
  15893. ((eq processing-type 'verbatim)
  15894. ;; Leave the text verbatim, just protect it
  15895. (add-text-properties (match-beginning n) (match-end n)
  15896. '(org-protected t)))
  15897. ((eq processing-type 'mathjax)
  15898. ;; Prepare for MathJax processing
  15899. (setq string (match-string n))
  15900. (if (member m '("$" "$1"))
  15901. (save-excursion
  15902. (delete-region (match-beginning n) (match-end n))
  15903. (goto-char (match-beginning n))
  15904. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15905. "\\)")
  15906. '(org-protected t))))
  15907. (add-text-properties (match-beginning n) (match-end n)
  15908. '(org-protected t))))
  15909. ((or (eq processing-type 'dvipng)
  15910. (eq processing-type 'imagemagick))
  15911. ;; Process to an image
  15912. (setq txt (match-string n)
  15913. beg (match-beginning n) end (match-end n)
  15914. cnt (1+ cnt))
  15915. (let ((face (face-at-point))
  15916. (fg (plist-get opt :foreground))
  15917. (bg (plist-get opt :background))
  15918. print-length print-level) ; make sure full list is printed
  15919. (when forbuffer
  15920. ; Get the colors from the face at point
  15921. (goto-char beg)
  15922. (when (eq fg 'auto)
  15923. (setq fg (face-attribute face :foreground nil 'default)))
  15924. (when (eq bg 'auto)
  15925. (setq bg (face-attribute face :background nil 'default)))
  15926. (setq optnew (copy-sequence opt))
  15927. (plist-put optnew :foreground fg)
  15928. (plist-put optnew :background bg))
  15929. (setq hash (sha1 (prin1-to-string
  15930. (list org-format-latex-header
  15931. org-format-latex-header-extra
  15932. org-export-latex-default-packages-alist
  15933. org-export-latex-packages-alist
  15934. org-format-latex-options
  15935. forbuffer txt fg bg)))
  15936. linkfile (format "%s_%s.png" prefix hash)
  15937. movefile (format "%s_%s.png" absprefix hash)))
  15938. (setq link (concat block "[[file:" linkfile "]]" block))
  15939. (if msg (message msg cnt))
  15940. (goto-char beg)
  15941. (unless checkdir ; make sure the directory exists
  15942. (setq checkdir t)
  15943. (or (file-directory-p todir) (make-directory todir t)))
  15944. (org-create-formula-image
  15945. txt movefile optnew forbuffer processing-type)
  15946. (if overlays
  15947. (progn
  15948. (mapc (lambda (o)
  15949. (if (eq (overlay-get o 'org-overlay-type)
  15950. 'org-latex-overlay)
  15951. (delete-overlay o)))
  15952. (overlays-in beg end))
  15953. (setq ov (make-overlay beg end))
  15954. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15955. (if (featurep 'xemacs)
  15956. (progn
  15957. (overlay-put ov 'invisible t)
  15958. (overlay-put
  15959. ov 'end-glyph
  15960. (make-glyph (vector 'png :file movefile))))
  15961. (overlay-put
  15962. ov 'display
  15963. (list 'image :type 'png :file movefile :ascent 'center)))
  15964. (push ov org-latex-fragment-image-overlays)
  15965. (goto-char end))
  15966. (delete-region beg end)
  15967. (insert (org-add-props link
  15968. (list 'org-latex-src
  15969. (replace-regexp-in-string
  15970. "\"" "" txt)
  15971. 'org-latex-src-embed-type
  15972. (if block-type 'paragraph 'character))))))
  15973. ((eq processing-type 'mathml)
  15974. ;; Process to MathML
  15975. (unless (save-match-data (org-format-latex-mathml-available-p))
  15976. (error "LaTeX to MathML converter not configured"))
  15977. (setq txt (match-string n)
  15978. beg (match-beginning n) end (match-end n)
  15979. cnt (1+ cnt))
  15980. (if msg (message msg cnt))
  15981. (goto-char beg)
  15982. (delete-region beg end)
  15983. (insert (org-format-latex-as-mathml
  15984. txt block-type prefix dir)))
  15985. (t
  15986. (error "Unknown conversion type %s for latex fragments"
  15987. processing-type)))))))))
  15988. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15989. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15990. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15991. sucessful, return the portion between \"<math...> </math>\"
  15992. elements otherwise return nil. When MATHML-FILE is specified,
  15993. write the results in to that file. When invoked as an
  15994. interactive command, prompt for LATEX-FRAG, with initial value
  15995. set to the current active region and echo the results for user
  15996. inspection."
  15997. (interactive (list (let ((frag (when (org-region-active-p)
  15998. (buffer-substring-no-properties
  15999. (region-beginning) (region-end)))))
  16000. (read-string "LaTeX Fragment: " frag nil frag))))
  16001. (unless latex-frag (error "Invalid latex-frag"))
  16002. (let* ((tmp-in-file (file-relative-name
  16003. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16004. (ignore (write-region latex-frag nil tmp-in-file))
  16005. (tmp-out-file (file-relative-name
  16006. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16007. (cmd (format-spec
  16008. org-latex-to-mathml-convert-command
  16009. `((?j . ,(shell-quote-argument
  16010. (expand-file-name org-latex-to-mathml-jar-file)))
  16011. (?I . ,(shell-quote-argument tmp-in-file))
  16012. (?o . ,(shell-quote-argument tmp-out-file)))))
  16013. mathml shell-command-output)
  16014. (when (org-called-interactively-p 'any)
  16015. (unless (org-format-latex-mathml-available-p)
  16016. (error "LaTeX to MathML converter not configured")))
  16017. (message "Running %s" cmd)
  16018. (setq shell-command-output (shell-command-to-string cmd))
  16019. (setq mathml
  16020. (when (file-readable-p tmp-out-file)
  16021. (with-current-buffer (find-file-noselect tmp-out-file t)
  16022. (goto-char (point-min))
  16023. (when (re-search-forward
  16024. (concat
  16025. (regexp-quote
  16026. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16027. "\\(.\\|\n\\)*"
  16028. (regexp-quote "</math>")) nil t)
  16029. (prog1 (match-string 0) (kill-buffer))))))
  16030. (cond
  16031. (mathml
  16032. (setq mathml
  16033. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16034. (when mathml-file
  16035. (write-region mathml nil mathml-file))
  16036. (when (org-called-interactively-p 'any)
  16037. (message mathml)))
  16038. ((message "LaTeX to MathML conversion failed")
  16039. (message shell-command-output)))
  16040. (delete-file tmp-in-file)
  16041. (when (file-exists-p tmp-out-file)
  16042. (delete-file tmp-out-file))
  16043. mathml))
  16044. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16045. prefix &optional dir)
  16046. "Use `org-create-math-formula' but check local cache first."
  16047. (let* ((absprefix (expand-file-name prefix dir))
  16048. (print-length nil) (print-level nil)
  16049. (formula-id (concat
  16050. "formula-"
  16051. (sha1
  16052. (prin1-to-string
  16053. (list latex-frag
  16054. org-latex-to-mathml-convert-command)))))
  16055. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16056. (formula-cache-dir (file-name-directory formula-cache)))
  16057. (unless (file-directory-p formula-cache-dir)
  16058. (make-directory formula-cache-dir t))
  16059. (unless (file-exists-p formula-cache)
  16060. (org-create-math-formula latex-frag formula-cache))
  16061. (if (file-exists-p formula-cache)
  16062. ;; Successful conversion. Return the link to MathML file.
  16063. (org-add-props
  16064. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16065. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16066. 'org-latex-src-embed-type (if latex-frag-type
  16067. 'paragraph 'character)))
  16068. ;; Failed conversion. Return the LaTeX fragment verbatim
  16069. (add-text-properties
  16070. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16071. latex-frag)))
  16072. (defun org-create-formula-image (string tofile options buffer &optional type)
  16073. "Create an image from LaTeX source using dvipng or convert.
  16074. This function calls either `org-create-formula-image-with-dvipng'
  16075. or `org-create-formula-image-with-imagemagick' depending on the
  16076. value of `org-latex-create-formula-image-program' or on the value
  16077. of the optional TYPE variable.
  16078. Note: ultimately these two function should be combined as they
  16079. share a good deal of logic."
  16080. (org-check-external-command
  16081. "latex" "needed to convert LaTeX fragments to images")
  16082. (funcall
  16083. (case (or type org-latex-create-formula-image-program)
  16084. ('dvipng
  16085. (org-check-external-command
  16086. "dvipng" "needed to convert LaTeX fragments to images")
  16087. #'org-create-formula-image-with-dvipng)
  16088. ('imagemagick
  16089. (org-check-external-command
  16090. "convert" "you need to install imagemagick")
  16091. #'org-create-formula-image-with-imagemagick)
  16092. (t (error
  16093. "invalid value of `org-latex-create-formula-image-program'")))
  16094. string tofile options buffer))
  16095. ;; This function borrows from Ganesh Swami's latex2png.el
  16096. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16097. "This calls dvipng."
  16098. (require 'org-latex)
  16099. (let* ((tmpdir (if (featurep 'xemacs)
  16100. (temp-directory)
  16101. temporary-file-directory))
  16102. (texfilebase (make-temp-name
  16103. (expand-file-name "orgtex" tmpdir)))
  16104. (texfile (concat texfilebase ".tex"))
  16105. (dvifile (concat texfilebase ".dvi"))
  16106. (pngfile (concat texfilebase ".png"))
  16107. (fnh (if (featurep 'xemacs)
  16108. (font-height (face-font 'default))
  16109. (face-attribute 'default :height nil)))
  16110. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16111. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16112. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16113. "Black"))
  16114. (bg (or (plist-get options (if buffer :background :html-background))
  16115. "Transparent")))
  16116. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16117. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16118. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16119. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16120. (with-temp-file texfile
  16121. (insert (org-splice-latex-header
  16122. org-format-latex-header
  16123. org-export-latex-default-packages-alist
  16124. org-export-latex-packages-alist t
  16125. org-format-latex-header-extra))
  16126. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16127. (require 'org-latex)
  16128. (org-export-latex-fix-inputenc))
  16129. (let ((dir default-directory))
  16130. (condition-case nil
  16131. (progn
  16132. (cd tmpdir)
  16133. (call-process "latex" nil nil nil texfile))
  16134. (error nil))
  16135. (cd dir))
  16136. (if (not (file-exists-p dvifile))
  16137. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16138. (condition-case nil
  16139. (if (featurep 'xemacs)
  16140. (call-process "dvipng" nil nil nil
  16141. "-fg" fg "-bg" bg
  16142. "-T" "tight"
  16143. "-o" pngfile
  16144. dvifile)
  16145. (call-process "dvipng" nil nil nil
  16146. "-fg" fg "-bg" bg
  16147. "-D" dpi
  16148. ;;"-x" scale "-y" scale
  16149. "-T" "tight"
  16150. "-o" pngfile
  16151. dvifile))
  16152. (error nil))
  16153. (if (not (file-exists-p pngfile))
  16154. (if org-format-latex-signal-error
  16155. (error "Failed to create png file from %s" texfile)
  16156. (message "Failed to create png file from %s" texfile)
  16157. nil)
  16158. ;; Use the requested file name and clean up
  16159. (copy-file pngfile tofile 'replace)
  16160. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16161. (if (file-exists-p (concat texfilebase e))
  16162. (delete-file (concat texfilebase e))))
  16163. pngfile))))
  16164. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16165. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16166. "This calls convert, which is included into imagemagick."
  16167. (require 'org-latex)
  16168. (let* ((tmpdir (if (featurep 'xemacs)
  16169. (temp-directory)
  16170. temporary-file-directory))
  16171. (texfilebase (make-temp-name
  16172. (expand-file-name "orgtex" tmpdir)))
  16173. (texfile (concat texfilebase ".tex"))
  16174. (pdffile (concat texfilebase ".pdf"))
  16175. (pngfile (concat texfilebase ".png"))
  16176. (fnh (if (featurep 'xemacs)
  16177. (font-height (face-font 'default))
  16178. (face-attribute 'default :height nil)))
  16179. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16180. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16181. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16182. "black"))
  16183. (bg (or (plist-get options (if buffer :background :html-background))
  16184. "white")))
  16185. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16186. (setq fg (org-latex-color-format fg)))
  16187. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16188. (setq bg (org-latex-color-format
  16189. (if (string= bg "Transparent") "white" bg))))
  16190. (with-temp-file texfile
  16191. (insert (org-splice-latex-header
  16192. org-format-latex-header
  16193. org-export-latex-default-packages-alist
  16194. org-export-latex-packages-alist t
  16195. org-format-latex-header-extra))
  16196. (insert "\n\\begin{document}\n"
  16197. "\\definecolor{fg}{rgb}{" fg "}\n"
  16198. "\\definecolor{bg}{rgb}{" bg "}\n"
  16199. "\n\\pagecolor{bg}\n"
  16200. "\n{\\color{fg}\n"
  16201. string
  16202. "\n}\n"
  16203. "\n\\end{document}\n" )
  16204. (require 'org-latex)
  16205. (org-export-latex-fix-inputenc))
  16206. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16207. (condition-case nil
  16208. (progn
  16209. (cd tmpdir)
  16210. (setq cmds org-latex-to-pdf-process)
  16211. (while cmds
  16212. (setq latex-frags-cmds (pop cmds))
  16213. (if (listp latex-frags-cmds)
  16214. (setq cmds nil)
  16215. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16216. (while latex-frags-cmds
  16217. (setq cmd (pop latex-frags-cmds))
  16218. (while (string-match "%b" cmd)
  16219. (setq cmd (replace-match
  16220. (save-match-data
  16221. (shell-quote-argument texfile))
  16222. t t cmd)))
  16223. (while (string-match "%f" cmd)
  16224. (setq cmd (replace-match
  16225. (save-match-data
  16226. (shell-quote-argument (file-name-nondirectory texfile)))
  16227. t t cmd)))
  16228. (while (string-match "%o" cmd)
  16229. (setq cmd (replace-match
  16230. (save-match-data
  16231. (shell-quote-argument (file-name-directory texfile)))
  16232. t t cmd)))
  16233. (setq cmd (split-string cmd))
  16234. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16235. (error nil))
  16236. (cd dir))
  16237. (if (not (file-exists-p pdffile))
  16238. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16239. (condition-case nil
  16240. (if (featurep 'xemacs)
  16241. (call-process "convert" nil nil nil
  16242. "-density" "96"
  16243. "-trim"
  16244. "-antialias"
  16245. pdffile
  16246. "-quality" "100"
  16247. ;; "-sharpen" "0x1.0"
  16248. pngfile)
  16249. (call-process "convert" nil nil nil
  16250. "-density" dpi
  16251. "-trim"
  16252. "-antialias"
  16253. pdffile
  16254. "-quality" "100"
  16255. ; "-sharpen" "0x1.0"
  16256. pngfile))
  16257. (error nil))
  16258. (if (not (file-exists-p pngfile))
  16259. (if org-format-latex-signal-error
  16260. (error "Failed to create png file from %s" texfile)
  16261. (message "Failed to create png file from %s" texfile)
  16262. nil)
  16263. ;; Use the requested file name and clean up
  16264. (copy-file pngfile tofile 'replace)
  16265. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16266. (if (file-exists-p (concat texfilebase e))
  16267. (delete-file (concat texfilebase e))))
  16268. pngfile))))
  16269. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16270. "Fill a LaTeX header template TPL.
  16271. In the template, the following place holders will be recognized:
  16272. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16273. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16274. [PACKAGES] \\usepackage statements for PKG
  16275. [NO-PACKAGES] do not include PKG
  16276. [EXTRA] the string EXTRA
  16277. [NO-EXTRA] do not include EXTRA
  16278. For backward compatibility, if both the positive and the negative place
  16279. holder is missing, the positive one (without the \"NO-\") will be
  16280. assumed to be present at the end of the template.
  16281. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16282. EXTRA is a string.
  16283. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16284. (let (rpl (end ""))
  16285. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16286. (setq rpl (if (or (match-end 1) (not def-pkg))
  16287. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16288. tpl (replace-match rpl t t tpl))
  16289. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16290. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16291. (setq rpl (if (or (match-end 1) (not pkg))
  16292. "" (org-latex-packages-to-string pkg snippets-p t))
  16293. tpl (replace-match rpl t t tpl))
  16294. (if pkg (setq end
  16295. (concat end "\n"
  16296. (org-latex-packages-to-string pkg snippets-p)))))
  16297. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16298. (setq rpl (if (or (match-end 1) (not extra))
  16299. "" (concat extra "\n"))
  16300. tpl (replace-match rpl t t tpl))
  16301. (if (and extra (string-match "\\S-" extra))
  16302. (setq end (concat end "\n" extra))))
  16303. (if (string-match "\\S-" end)
  16304. (concat tpl "\n" end)
  16305. tpl)))
  16306. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16307. "Turn an alist of packages into a string with the \\usepackage macros."
  16308. (setq pkg (mapconcat (lambda(p)
  16309. (cond
  16310. ((stringp p) p)
  16311. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16312. (format "%% Package %s omitted" (cadr p)))
  16313. ((equal "" (car p))
  16314. (format "\\usepackage{%s}" (cadr p)))
  16315. (t
  16316. (format "\\usepackage[%s]{%s}"
  16317. (car p) (cadr p)))))
  16318. pkg
  16319. "\n"))
  16320. (if newline (concat pkg "\n") pkg))
  16321. (defun org-dvipng-color (attr)
  16322. "Return a RGB color specification for dvipng."
  16323. (apply 'format "rgb %s %s %s"
  16324. (mapcar 'org-normalize-color
  16325. (if (featurep 'xemacs)
  16326. (color-rgb-components
  16327. (face-property 'default
  16328. (cond ((eq attr :foreground) 'foreground)
  16329. ((eq attr :background) 'background))))
  16330. (color-values (face-attribute 'default attr nil))))))
  16331. (defun org-dvipng-color-format (color-name)
  16332. "Convert COLOR-NAME to a RGB color value for dvipng."
  16333. (apply 'format "rgb %s %s %s"
  16334. (mapcar 'org-normalize-color
  16335. (color-values color-name))))
  16336. (defun org-latex-color (attr)
  16337. "Return a RGB color for the LaTeX color package."
  16338. (apply 'format "%s,%s,%s"
  16339. (mapcar 'org-normalize-color
  16340. (if (featurep 'xemacs)
  16341. (color-rgb-components
  16342. (face-property 'default
  16343. (cond ((eq attr :foreground) 'foreground)
  16344. ((eq attr :background) 'background))))
  16345. (color-values (face-attribute 'default attr nil))))))
  16346. (defun org-latex-color-format (color-name)
  16347. "Convert COLOR-NAME to a RGB color value."
  16348. (apply 'format "%s,%s,%s"
  16349. (mapcar 'org-normalize-color
  16350. (color-values color-name))))
  16351. (defun org-normalize-color (value)
  16352. "Return string to be used as color value for an RGB component."
  16353. (format "%g" (/ value 65535.0)))
  16354. ;; Image display
  16355. (defvar org-inline-image-overlays nil)
  16356. (make-variable-buffer-local 'org-inline-image-overlays)
  16357. (defun org-toggle-inline-images (&optional include-linked)
  16358. "Toggle the display of inline images.
  16359. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16360. (interactive "P")
  16361. (if org-inline-image-overlays
  16362. (progn
  16363. (org-remove-inline-images)
  16364. (message "Inline image display turned off"))
  16365. (org-display-inline-images include-linked)
  16366. (if (and (org-called-interactively-p)
  16367. org-inline-image-overlays)
  16368. (message "%d images displayed inline"
  16369. (length org-inline-image-overlays))
  16370. (message "No images to display inline"))))
  16371. (defun org-redisplay-inline-images ()
  16372. "Refresh the display of inline images."
  16373. (interactive)
  16374. (if (not org-inline-image-overlays)
  16375. (org-toggle-inline-images)
  16376. (org-toggle-inline-images)
  16377. (org-toggle-inline-images)))
  16378. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16379. "Display inline images.
  16380. Normally only links without a description part are inlined, because this
  16381. is how it will work for export. When INCLUDE-LINKED is set, also links
  16382. with a description part will be inlined. This can be nice for a quick
  16383. look at those images, but it does not reflect what exported files will look
  16384. like.
  16385. When REFRESH is set, refresh existing images between BEG and END.
  16386. This will create new image displays only if necessary.
  16387. BEG and END default to the buffer boundaries."
  16388. (interactive "P")
  16389. (unless refresh
  16390. (org-remove-inline-images)
  16391. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16392. (save-excursion
  16393. (save-restriction
  16394. (widen)
  16395. (setq beg (or beg (point-min)) end (or end (point-max)))
  16396. (goto-char beg)
  16397. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16398. (substring (org-image-file-name-regexp) 0 -2)
  16399. "\\)\\]" (if include-linked "" "\\]")))
  16400. old file ov img type attrwidth width)
  16401. (while (re-search-forward re end t)
  16402. (setq old (get-char-property-and-overlay (match-beginning 1)
  16403. 'org-image-overlay)
  16404. file (expand-file-name
  16405. (concat (or (match-string 3) "") (match-string 4))))
  16406. (when (image-type-available-p 'imagemagick)
  16407. (setq attrwidth (if (or (listp org-image-actual-width)
  16408. (null org-image-actual-width))
  16409. (save-excursion
  16410. (save-match-data
  16411. (when (re-search-backward
  16412. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16413. (save-excursion
  16414. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16415. (string-to-number (match-string 1))))))
  16416. width (cond ((eq org-image-actual-width t) nil)
  16417. ((null org-image-actual-width) attrwidth)
  16418. ((numberp org-image-actual-width)
  16419. org-image-actual-width)
  16420. ((listp org-image-actual-width)
  16421. (or attrwidth (car org-image-actual-width))))
  16422. type (if width 'imagemagick)))
  16423. (when (file-exists-p file)
  16424. (if (and (car-safe old) refresh)
  16425. (image-refresh (overlay-get (cdr old) 'display))
  16426. (setq img (save-match-data (create-image file type nil :width width)))
  16427. (when img
  16428. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16429. (overlay-put ov 'display img)
  16430. (overlay-put ov 'face 'default)
  16431. (overlay-put ov 'org-image-overlay t)
  16432. (overlay-put ov 'modification-hooks
  16433. (list 'org-display-inline-remove-overlay))
  16434. (push ov org-inline-image-overlays)))))))))
  16435. (define-obsolete-function-alias
  16436. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16437. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16438. "Remove inline-display overlay if a corresponding region is modified."
  16439. (let ((inhibit-modification-hooks t))
  16440. (when (and ov after)
  16441. (delete ov org-inline-image-overlays)
  16442. (delete-overlay ov))))
  16443. (defun org-remove-inline-images ()
  16444. "Remove inline display of images."
  16445. (interactive)
  16446. (mapc 'delete-overlay org-inline-image-overlays)
  16447. (setq org-inline-image-overlays nil))
  16448. ;;;; Key bindings
  16449. ;; Outline functions from `outline-mode-prefix-map'
  16450. ;; that can be remapped in Org:
  16451. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16452. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16453. (define-key org-mode-map [remap outline-forward-same-level]
  16454. 'org-forward-heading-same-level)
  16455. (define-key org-mode-map [remap outline-backward-same-level]
  16456. 'org-backward-heading-same-level)
  16457. (define-key org-mode-map [remap show-branches]
  16458. 'org-kill-note-or-show-branches)
  16459. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16460. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16461. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16462. ;; Outline functions from `outline-mode-prefix-map' that can not
  16463. ;; be remapped in Org:
  16464. ;;
  16465. ;; - the column "key binding" shows whether the Outline function is still
  16466. ;; available in Org mode on the same key that it has been bound to in
  16467. ;; Outline mode:
  16468. ;; - "overridden": key used for a different functionality in Org mode
  16469. ;; - else: key still bound to the same Outline function in Org mode
  16470. ;;
  16471. ;; | Outline function | key binding | Org replacement |
  16472. ;; |------------------------------------+-------------+-----------------------|
  16473. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16474. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16475. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16476. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16477. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16478. ;; | `show-entry' | overridden | no replacement |
  16479. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16480. ;; | `show-branches' | `C-c C-k' | still same function |
  16481. ;; | `show-subtree' | overridden | visibility cycling |
  16482. ;; | `show-all' | overridden | no replacement |
  16483. ;; | `hide-subtree' | overridden | visibility cycling |
  16484. ;; | `hide-body' | overridden | no replacement |
  16485. ;; | `hide-entry' | overridden | visibility cycling |
  16486. ;; | `hide-leaves' | overridden | no replacement |
  16487. ;; | `hide-sublevels' | overridden | no replacement |
  16488. ;; | `hide-other' | overridden | no replacement |
  16489. ;; Make `C-c C-x' a prefix key
  16490. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16491. ;; TAB key with modifiers
  16492. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16493. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16494. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16495. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16496. ;; The following line is necessary under Suse GNU/Linux
  16497. (unless (featurep 'xemacs)
  16498. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16499. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16500. (define-key org-mode-map [backtab] 'org-shifttab)
  16501. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16502. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16503. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16504. ;; Cursor keys with modifiers
  16505. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16506. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16507. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16508. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16509. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16510. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16511. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16512. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16513. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16514. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16515. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16516. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16517. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16518. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16519. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16520. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16521. ;; Babel keys
  16522. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16523. (mapc (lambda (pair)
  16524. (define-key org-babel-map (car pair) (cdr pair)))
  16525. org-babel-key-bindings)
  16526. ;;; Extra keys for tty access.
  16527. ;; We only set them when really needed because otherwise the
  16528. ;; menus don't show the simple keys
  16529. (when (or org-use-extra-keys
  16530. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16531. (not window-system))
  16532. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16533. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16534. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16535. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16536. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16537. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16538. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16539. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16540. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16541. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16542. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16543. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16544. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16545. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16546. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16547. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16548. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16549. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16550. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16551. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16552. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16553. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16554. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16555. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16556. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16557. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16558. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16559. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16560. ;; All the other keys
  16561. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16562. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16563. (if (boundp 'narrow-map)
  16564. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16565. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16566. (if (boundp 'narrow-map)
  16567. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16568. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16569. (if (boundp 'narrow-map)
  16570. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16571. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16572. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16573. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16574. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16575. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16576. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16577. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16578. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16579. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16580. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16581. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16582. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16583. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16584. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16585. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16586. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16587. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16588. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16589. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16590. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16591. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16592. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16593. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16594. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16595. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16596. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16597. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16598. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16599. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16600. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16601. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16602. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16603. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16604. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16605. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16606. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16607. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16608. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16609. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16610. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16611. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16612. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16613. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16614. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16615. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16616. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16617. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16618. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16619. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16620. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16621. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16622. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16623. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16624. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16625. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16626. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16627. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16628. (org-defkey org-mode-map "\C-m" 'org-return)
  16629. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16630. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16631. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16632. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16633. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16634. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16635. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16636. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16637. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16638. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16639. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16640. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16641. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16642. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16643. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16644. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16645. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16646. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16647. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16648. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16649. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16650. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16651. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16652. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16653. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16654. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16655. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16656. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16657. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16658. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16659. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16660. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16661. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16662. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16663. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16664. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16665. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16666. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16667. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16668. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16669. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16670. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16671. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16672. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16673. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16674. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16675. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16676. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16677. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16678. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16679. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16680. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16681. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16682. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16683. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16684. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16685. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16686. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16687. (when (featurep 'xemacs)
  16688. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16689. (defconst org-speed-commands-default
  16690. '(
  16691. ("Outline Navigation")
  16692. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16693. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16694. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16695. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16696. ("u" . (org-speed-move-safe 'outline-up-heading))
  16697. ("j" . org-goto)
  16698. ("g" . (org-refile t))
  16699. ("Outline Visibility")
  16700. ("c" . org-cycle)
  16701. ("C" . org-shifttab)
  16702. (" " . org-display-outline-path)
  16703. (":" . org-columns)
  16704. ("Outline Structure Editing")
  16705. ("U" . org-shiftmetaup)
  16706. ("D" . org-shiftmetadown)
  16707. ("r" . org-metaright)
  16708. ("l" . org-metaleft)
  16709. ("R" . org-shiftmetaright)
  16710. ("L" . org-shiftmetaleft)
  16711. ("i" . (progn (forward-char 1) (call-interactively
  16712. 'org-insert-heading-respect-content)))
  16713. ("^" . org-sort)
  16714. ("w" . org-refile)
  16715. ("a" . org-archive-subtree-default-with-confirmation)
  16716. ("." . org-mark-subtree)
  16717. ("#" . org-toggle-comment)
  16718. ("Clock Commands")
  16719. ("I" . org-clock-in)
  16720. ("O" . org-clock-out)
  16721. ("Meta Data Editing")
  16722. ("t" . org-todo)
  16723. ("," . (org-priority))
  16724. ("0" . (org-priority ?\ ))
  16725. ("1" . (org-priority ?A))
  16726. ("2" . (org-priority ?B))
  16727. ("3" . (org-priority ?C))
  16728. (";" . org-set-tags-command)
  16729. ("e" . org-set-effort)
  16730. ("E" . org-inc-effort)
  16731. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16732. (org-entry-put (point) "APPT_WARNTIME" m)))
  16733. ("Agenda Views etc")
  16734. ("v" . org-agenda)
  16735. ("/" . org-sparse-tree)
  16736. ("Misc")
  16737. ("o" . org-open-at-point)
  16738. ("?" . org-speed-command-help)
  16739. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16740. (">" . (org-agenda-remove-restriction-lock))
  16741. )
  16742. "The default speed commands.")
  16743. (defun org-print-speed-command (e)
  16744. (if (> (length (car e)) 1)
  16745. (progn
  16746. (princ "\n")
  16747. (princ (car e))
  16748. (princ "\n")
  16749. (princ (make-string (length (car e)) ?-))
  16750. (princ "\n"))
  16751. (princ (car e))
  16752. (princ " ")
  16753. (if (symbolp (cdr e))
  16754. (princ (symbol-name (cdr e)))
  16755. (prin1 (cdr e)))
  16756. (princ "\n")))
  16757. (defun org-speed-command-help ()
  16758. "Show the available speed commands."
  16759. (interactive)
  16760. (if (not org-use-speed-commands)
  16761. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16762. (with-output-to-temp-buffer "*Help*"
  16763. (princ "User-defined Speed commands\n===========================\n")
  16764. (mapc 'org-print-speed-command org-speed-commands-user)
  16765. (princ "\n")
  16766. (princ "Built-in Speed commands\n=======================\n")
  16767. (mapc 'org-print-speed-command org-speed-commands-default))
  16768. (with-current-buffer "*Help*"
  16769. (setq truncate-lines t))))
  16770. (defun org-speed-move-safe (cmd)
  16771. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16772. If not, return to the original position and throw an error."
  16773. (interactive)
  16774. (let ((pos (point)))
  16775. (call-interactively cmd)
  16776. (unless (and (bolp) (org-at-heading-p))
  16777. (goto-char pos)
  16778. (error "Boundary reached while executing %s" cmd))))
  16779. (defvar org-self-insert-command-undo-counter 0)
  16780. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16781. (defvar org-speed-command nil)
  16782. (define-obsolete-function-alias
  16783. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  16784. (defun org-speed-command-activate (keys)
  16785. "Hook for activating single-letter speed commands.
  16786. `org-speed-commands-default' specifies a minimal command set.
  16787. Use `org-speed-commands-user' for further customization."
  16788. (when (or (and (bolp) (looking-at org-outline-regexp))
  16789. (and (functionp org-use-speed-commands)
  16790. (funcall org-use-speed-commands)))
  16791. (cdr (assoc keys (append org-speed-commands-user
  16792. org-speed-commands-default)))))
  16793. (define-obsolete-function-alias
  16794. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  16795. (defun org-babel-speed-command-activate (keys)
  16796. "Hook for activating single-letter code block commands."
  16797. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16798. (cdr (assoc keys org-babel-key-bindings))))
  16799. (defcustom org-speed-command-hook
  16800. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16801. "Hook for activating speed commands at strategic locations.
  16802. Hook functions are called in sequence until a valid handler is
  16803. found.
  16804. Each hook takes a single argument, a user-pressed command key
  16805. which is also a `self-insert-command' from the global map.
  16806. Within the hook, examine the cursor position and the command key
  16807. and return nil or a valid handler as appropriate. Handler could
  16808. be one of an interactive command, a function, or a form.
  16809. Set `org-use-speed-commands' to non-nil value to enable this
  16810. hook. The default setting is `org-speed-command-activate'."
  16811. :group 'org-structure
  16812. :version "24.1"
  16813. :type 'hook)
  16814. (defun org-self-insert-command (N)
  16815. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16816. If the cursor is in a table looking at whitespace, the whitespace is
  16817. overwritten, and the table is not marked as requiring realignment."
  16818. (interactive "p")
  16819. (org-check-before-invisible-edit 'insert)
  16820. (cond
  16821. ((and org-use-speed-commands
  16822. (setq org-speed-command
  16823. (run-hook-with-args-until-success
  16824. 'org-speed-command-hook (this-command-keys))))
  16825. (cond
  16826. ((commandp org-speed-command)
  16827. (setq this-command org-speed-command)
  16828. (call-interactively org-speed-command))
  16829. ((functionp org-speed-command)
  16830. (funcall org-speed-command))
  16831. ((and org-speed-command (listp org-speed-command))
  16832. (eval org-speed-command))
  16833. (t (let (org-use-speed-commands)
  16834. (call-interactively 'org-self-insert-command)))))
  16835. ((and
  16836. (org-table-p)
  16837. (progn
  16838. ;; check if we blank the field, and if that triggers align
  16839. (and (featurep 'org-table) org-table-auto-blank-field
  16840. (member last-command
  16841. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16842. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16843. ;; got extra space, this field does not determine column width
  16844. (let (org-table-may-need-update) (org-table-blank-field))
  16845. ;; no extra space, this field may determine column width
  16846. (org-table-blank-field)))
  16847. t)
  16848. (eq N 1)
  16849. (looking-at "[^|\n]* |"))
  16850. (let (org-table-may-need-update)
  16851. (goto-char (1- (match-end 0)))
  16852. (backward-delete-char 1)
  16853. (goto-char (match-beginning 0))
  16854. (self-insert-command N)))
  16855. (t
  16856. (setq org-table-may-need-update t)
  16857. (self-insert-command N)
  16858. (org-fix-tags-on-the-fly)
  16859. (if org-self-insert-cluster-for-undo
  16860. (if (not (eq last-command 'org-self-insert-command))
  16861. (setq org-self-insert-command-undo-counter 1)
  16862. (if (>= org-self-insert-command-undo-counter 20)
  16863. (setq org-self-insert-command-undo-counter 1)
  16864. (and (> org-self-insert-command-undo-counter 0)
  16865. buffer-undo-list (listp buffer-undo-list)
  16866. (not (cadr buffer-undo-list)) ; remove nil entry
  16867. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16868. (setq org-self-insert-command-undo-counter
  16869. (1+ org-self-insert-command-undo-counter))))))))
  16870. (defun org-check-before-invisible-edit (kind)
  16871. "Check is editing if kind KIND would be dangerous with invisible text around.
  16872. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16873. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16874. (if (and org-catch-invisible-edits
  16875. (or (not (boundp 'visible-mode)) (not visible-mode))
  16876. (or (get-char-property (point) 'invisible)
  16877. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16878. ;; OK, we need to take a closer look
  16879. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16880. (invisible-before-point (if (bobp) nil (get-char-property
  16881. (1- (point)) 'invisible)))
  16882. (border-and-ok-direction
  16883. (or
  16884. ;; Check if we are acting predictably before invisible text
  16885. (and invisible-at-point (not invisible-before-point)
  16886. (memq kind '(insert delete-backward)))
  16887. ;; Check if we are acting predictably after invisible text
  16888. ;; This works not well, and I have turned it off. It seems
  16889. ;; better to always show and stop after invisible text.
  16890. ;; (and (not invisible-at-point) invisible-before-point
  16891. ;; (memq kind '(insert delete)))
  16892. )))
  16893. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16894. (memq invisible-before-point '(outline org-hide-block t)))
  16895. (if (eq org-catch-invisible-edits 'error)
  16896. (error "Editing in invisible areas is prohibited - make visible first"))
  16897. (if (and org-custom-properties-overlays
  16898. (y-or-n-p "Display invisible properties in this buffer? "))
  16899. (org-toggle-custom-properties-visibility)
  16900. ;; Make the area visible
  16901. (save-excursion
  16902. (if invisible-before-point
  16903. (goto-char (previous-single-char-property-change
  16904. (point) 'invisible)))
  16905. (org-cycle))
  16906. (cond
  16907. ((eq org-catch-invisible-edits 'show)
  16908. ;; That's it, we do the edit after showing
  16909. (message
  16910. "Unfolding invisible region around point before editing")
  16911. (sit-for 1))
  16912. ((and (eq org-catch-invisible-edits 'smart)
  16913. border-and-ok-direction)
  16914. (message "Unfolding invisible region around point before editing"))
  16915. (t
  16916. ;; Don't do the edit, make the user repeat it in full visibility
  16917. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  16918. (defun org-fix-tags-on-the-fly ()
  16919. (when (and (equal (char-after (point-at-bol)) ?*)
  16920. (org-at-heading-p))
  16921. (org-align-tags-here org-tags-column)))
  16922. (defun org-delete-backward-char (N)
  16923. "Like `delete-backward-char', insert whitespace at field end in tables.
  16924. When deleting backwards, in tables this function will insert whitespace in
  16925. front of the next \"|\" separator, to keep the table aligned. The table will
  16926. still be marked for re-alignment if the field did fill the entire column,
  16927. because, in this case the deletion might narrow the column."
  16928. (interactive "p")
  16929. (org-check-before-invisible-edit 'delete-backward)
  16930. (if (and (org-table-p)
  16931. (eq N 1)
  16932. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16933. (looking-at ".*?|"))
  16934. (let ((pos (point))
  16935. (noalign (looking-at "[^|\n\r]* |"))
  16936. (c org-table-may-need-update))
  16937. (backward-delete-char N)
  16938. (if (not overwrite-mode)
  16939. (progn
  16940. (skip-chars-forward "^|")
  16941. (insert " ")
  16942. (goto-char (1- pos))))
  16943. ;; noalign: if there were two spaces at the end, this field
  16944. ;; does not determine the width of the column.
  16945. (if noalign (setq org-table-may-need-update c)))
  16946. (backward-delete-char N)
  16947. (org-fix-tags-on-the-fly)))
  16948. (defun org-delete-char (N)
  16949. "Like `delete-char', but insert whitespace at field end in tables.
  16950. When deleting characters, in tables this function will insert whitespace in
  16951. front of the next \"|\" separator, to keep the table aligned. The table will
  16952. still be marked for re-alignment if the field did fill the entire column,
  16953. because, in this case the deletion might narrow the column."
  16954. (interactive "p")
  16955. (org-check-before-invisible-edit 'delete)
  16956. (if (and (org-table-p)
  16957. (not (bolp))
  16958. (not (= (char-after) ?|))
  16959. (eq N 1))
  16960. (if (looking-at ".*?|")
  16961. (let ((pos (point))
  16962. (noalign (looking-at "[^|\n\r]* |"))
  16963. (c org-table-may-need-update))
  16964. (replace-match (concat
  16965. (substring (match-string 0) 1 -1)
  16966. " |"))
  16967. (goto-char pos)
  16968. ;; noalign: if there were two spaces at the end, this field
  16969. ;; does not determine the width of the column.
  16970. (if noalign (setq org-table-may-need-update c)))
  16971. (delete-char N))
  16972. (delete-char N)
  16973. (org-fix-tags-on-the-fly)))
  16974. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16975. (put 'org-self-insert-command 'delete-selection t)
  16976. (put 'orgtbl-self-insert-command 'delete-selection t)
  16977. (put 'org-delete-char 'delete-selection 'supersede)
  16978. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16979. (put 'org-yank 'delete-selection 'yank)
  16980. ;; Make `flyspell-mode' delay after some commands
  16981. (put 'org-self-insert-command 'flyspell-delayed t)
  16982. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16983. (put 'org-delete-char 'flyspell-delayed t)
  16984. (put 'org-delete-backward-char 'flyspell-delayed t)
  16985. ;; Make pabbrev-mode expand after org-mode commands
  16986. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16987. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16988. ;; How to do this: Measure non-white length of current string
  16989. ;; If equal to column width, we should realign.
  16990. (defun org-remap (map &rest commands)
  16991. "In MAP, remap the functions given in COMMANDS.
  16992. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16993. (let (new old)
  16994. (while commands
  16995. (setq old (pop commands) new (pop commands))
  16996. (if (fboundp 'command-remapping)
  16997. (org-defkey map (vector 'remap old) new)
  16998. (substitute-key-definition old new map global-map)))))
  16999. (when (eq org-enable-table-editor 'optimized)
  17000. ;; If the user wants maximum table support, we need to hijack
  17001. ;; some standard editing functions
  17002. (org-remap org-mode-map
  17003. 'self-insert-command 'org-self-insert-command
  17004. 'delete-char 'org-delete-char
  17005. 'delete-backward-char 'org-delete-backward-char)
  17006. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17007. (defvar org-ctrl-c-ctrl-c-hook nil
  17008. "Hook for functions attaching themselves to `C-c C-c'.
  17009. This can be used to add additional functionality to the C-c C-c
  17010. key which executes context-dependent commands. This hook is run
  17011. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17012. run after the last test.
  17013. Each function will be called with no arguments. The function
  17014. must check if the context is appropriate for it to act. If yes,
  17015. it should do its thing and then return a non-nil value. If the
  17016. context is wrong, just do nothing and return nil.")
  17017. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17018. "Hook for functions attaching themselves to `C-c C-c'.
  17019. This can be used to add additional functionality to the C-c C-c
  17020. key which executes context-dependent commands. This hook is run
  17021. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17022. before the first test.
  17023. Each function will be called with no arguments. The function
  17024. must check if the context is appropriate for it to act. If yes,
  17025. it should do its thing and then return a non-nil value. If the
  17026. context is wrong, just do nothing and return nil.")
  17027. (defvar org-tab-first-hook nil
  17028. "Hook for functions to attach themselves to TAB.
  17029. See `org-ctrl-c-ctrl-c-hook' for more information.
  17030. This hook runs as the first action when TAB is pressed, even before
  17031. `org-cycle' messes around with the `outline-regexp' to cater for
  17032. inline tasks and plain list item folding.
  17033. If any function in this hook returns t, any other actions that
  17034. would have been caused by TAB (such as table field motion or visibility
  17035. cycling) will not occur.")
  17036. (defvar org-tab-after-check-for-table-hook nil
  17037. "Hook for functions to attach themselves to TAB.
  17038. See `org-ctrl-c-ctrl-c-hook' for more information.
  17039. This hook runs after it has been established that the cursor is not in a
  17040. table, but before checking if the cursor is in a headline or if global cycling
  17041. should be done.
  17042. If any function in this hook returns t, not other actions like visibility
  17043. cycling will be done.")
  17044. (defvar org-tab-after-check-for-cycling-hook nil
  17045. "Hook for functions to attach themselves to TAB.
  17046. See `org-ctrl-c-ctrl-c-hook' for more information.
  17047. This hook runs after it has been established that not table field motion and
  17048. not visibility should be done because of current context. This is probably
  17049. the place where a package like yasnippets can hook in.")
  17050. (defvar org-tab-before-tab-emulation-hook nil
  17051. "Hook for functions to attach themselves to TAB.
  17052. See `org-ctrl-c-ctrl-c-hook' for more information.
  17053. This hook runs after every other options for TAB have been exhausted, but
  17054. before indentation and \t insertion takes place.")
  17055. (defvar org-metaleft-hook nil
  17056. "Hook for functions attaching themselves to `M-left'.
  17057. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17058. (defvar org-metaright-hook nil
  17059. "Hook for functions attaching themselves to `M-right'.
  17060. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17061. (defvar org-metaup-hook nil
  17062. "Hook for functions attaching themselves to `M-up'.
  17063. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17064. (defvar org-metadown-hook nil
  17065. "Hook for functions attaching themselves to `M-down'.
  17066. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17067. (defvar org-shiftmetaleft-hook nil
  17068. "Hook for functions attaching themselves to `M-S-left'.
  17069. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17070. (defvar org-shiftmetaright-hook nil
  17071. "Hook for functions attaching themselves to `M-S-right'.
  17072. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17073. (defvar org-shiftmetaup-hook nil
  17074. "Hook for functions attaching themselves to `M-S-up'.
  17075. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17076. (defvar org-shiftmetadown-hook nil
  17077. "Hook for functions attaching themselves to `M-S-down'.
  17078. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17079. (defvar org-metareturn-hook nil
  17080. "Hook for functions attaching themselves to `M-RET'.
  17081. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17082. (defvar org-shiftup-hook nil
  17083. "Hook for functions attaching themselves to `S-up'.
  17084. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17085. (defvar org-shiftup-final-hook nil
  17086. "Hook for functions attaching themselves to `S-up'.
  17087. This one runs after all other options except shift-select have been excluded.
  17088. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17089. (defvar org-shiftdown-hook nil
  17090. "Hook for functions attaching themselves to `S-down'.
  17091. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17092. (defvar org-shiftdown-final-hook nil
  17093. "Hook for functions attaching themselves to `S-down'.
  17094. This one runs after all other options except shift-select have been excluded.
  17095. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17096. (defvar org-shiftleft-hook nil
  17097. "Hook for functions attaching themselves to `S-left'.
  17098. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17099. (defvar org-shiftleft-final-hook nil
  17100. "Hook for functions attaching themselves to `S-left'.
  17101. This one runs after all other options except shift-select have been excluded.
  17102. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17103. (defvar org-shiftright-hook nil
  17104. "Hook for functions attaching themselves to `S-right'.
  17105. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17106. (defvar org-shiftright-final-hook nil
  17107. "Hook for functions attaching themselves to `S-right'.
  17108. This one runs after all other options except shift-select have been excluded.
  17109. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17110. (defun org-modifier-cursor-error ()
  17111. "Throw an error, a modified cursor command was applied in wrong context."
  17112. (error "This command is active in special context like tables, headlines or items"))
  17113. (defun org-shiftselect-error ()
  17114. "Throw an error because Shift-Cursor command was applied in wrong context."
  17115. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17116. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17117. (error "This command works only in special context like headlines or timestamps")))
  17118. (defun org-call-for-shift-select (cmd)
  17119. (let ((this-command-keys-shift-translated t))
  17120. (call-interactively cmd)))
  17121. (defun org-shifttab (&optional arg)
  17122. "Global visibility cycling or move to previous table field.
  17123. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17124. on context.
  17125. See the individual commands for more information."
  17126. (interactive "P")
  17127. (cond
  17128. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17129. ((integerp arg)
  17130. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17131. (message "Content view to level: %d" arg)
  17132. (org-content (prefix-numeric-value arg2))
  17133. (setq org-cycle-global-status 'overview)))
  17134. (t (call-interactively 'org-global-cycle))))
  17135. (defun org-shiftmetaleft ()
  17136. "Promote subtree or delete table column.
  17137. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17138. `org-table-delete-column', depending on context. See the
  17139. individual commands for more information."
  17140. (interactive)
  17141. (cond
  17142. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17143. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17144. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17145. ((if (not (org-region-active-p)) (org-at-item-p)
  17146. (save-excursion (goto-char (region-beginning))
  17147. (org-at-item-p)))
  17148. (call-interactively 'org-outdent-item-tree))
  17149. (t (org-modifier-cursor-error))))
  17150. (defun org-shiftmetaright ()
  17151. "Demote subtree or insert table column.
  17152. Calls `org-demote-subtree', `org-indent-item-tree', or
  17153. `org-table-insert-column', depending on context. See the
  17154. individual commands for more information."
  17155. (interactive)
  17156. (cond
  17157. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17158. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17159. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17160. ((if (not (org-region-active-p)) (org-at-item-p)
  17161. (save-excursion (goto-char (region-beginning))
  17162. (org-at-item-p)))
  17163. (call-interactively 'org-indent-item-tree))
  17164. (t (org-modifier-cursor-error))))
  17165. (defun org-shiftmetaup (&optional arg)
  17166. "Move subtree up or kill table row.
  17167. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17168. `org-move-item-up' or `org-timestamp-up', depending on context.
  17169. See the individual commands for more information."
  17170. (interactive "P")
  17171. (cond
  17172. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17173. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17174. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17175. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17176. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17177. (call-interactively 'org-timestamp-up)))
  17178. (t (org-modifier-cursor-error))))
  17179. (defun org-shiftmetadown (&optional arg)
  17180. "Move subtree down or insert table row.
  17181. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17182. `org-move-item-down' or `org-timestamp-up', depending on context.
  17183. See the individual commands for more information."
  17184. (interactive "P")
  17185. (cond
  17186. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17187. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17188. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17189. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17190. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17191. (call-interactively 'org-timestamp-down)))
  17192. (t (org-modifier-cursor-error))))
  17193. (defsubst org-hidden-tree-error ()
  17194. (error
  17195. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17196. (defun org-metaleft (&optional arg)
  17197. "Promote heading or move table column to left.
  17198. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17199. With no specific context, calls the Emacs default `backward-word'.
  17200. See the individual commands for more information."
  17201. (interactive "P")
  17202. (cond
  17203. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17204. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17205. ((org-with-limited-levels
  17206. (or (org-at-heading-p)
  17207. (and (org-region-active-p)
  17208. (save-excursion
  17209. (goto-char (region-beginning))
  17210. (org-at-heading-p)))))
  17211. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17212. (call-interactively 'org-do-promote))
  17213. ;; At an inline task.
  17214. ((org-at-heading-p)
  17215. (call-interactively 'org-inlinetask-promote))
  17216. ((or (org-at-item-p)
  17217. (and (org-region-active-p)
  17218. (save-excursion
  17219. (goto-char (region-beginning))
  17220. (org-at-item-p))))
  17221. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17222. (call-interactively 'org-outdent-item))
  17223. (t (call-interactively 'backward-word))))
  17224. (defun org-metaright (&optional arg)
  17225. "Demote a subtree, a list item or move table column to right.
  17226. In front of a drawer or a block keyword, indent it correctly.
  17227. With no specific context, calls the Emacs default `forward-word'.
  17228. See the individual commands for more information."
  17229. (interactive "P")
  17230. (cond
  17231. ((run-hook-with-args-until-success 'org-metaright-hook))
  17232. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17233. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17234. ((org-at-block-p) (call-interactively 'org-indent-block))
  17235. ((org-with-limited-levels
  17236. (or (org-at-heading-p)
  17237. (and (org-region-active-p)
  17238. (save-excursion
  17239. (goto-char (region-beginning))
  17240. (org-at-heading-p)))))
  17241. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17242. (call-interactively 'org-do-demote))
  17243. ;; At an inline task.
  17244. ((org-at-heading-p)
  17245. (call-interactively 'org-inlinetask-demote))
  17246. ((or (org-at-item-p)
  17247. (and (org-region-active-p)
  17248. (save-excursion
  17249. (goto-char (region-beginning))
  17250. (org-at-item-p))))
  17251. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17252. (call-interactively 'org-indent-item))
  17253. (t (call-interactively 'forward-word))))
  17254. (defun org-check-for-hidden (what)
  17255. "Check if there are hidden headlines/items in the current visual line.
  17256. WHAT can be either `headlines' or `items'. If the current line is
  17257. an outline or item heading and it has a folded subtree below it,
  17258. this function returns t, nil otherwise."
  17259. (let ((re (cond
  17260. ((eq what 'headlines) org-outline-regexp-bol)
  17261. ((eq what 'items) (org-item-beginning-re))
  17262. (t (error "This should not happen"))))
  17263. beg end)
  17264. (save-excursion
  17265. (catch 'exit
  17266. (unless (org-region-active-p)
  17267. (setq beg (point-at-bol))
  17268. (beginning-of-line 2)
  17269. (while (and (not (eobp)) ;; this is like `next-line'
  17270. (get-char-property (1- (point)) 'invisible))
  17271. (beginning-of-line 2))
  17272. (setq end (point))
  17273. (goto-char beg)
  17274. (goto-char (point-at-eol))
  17275. (setq end (max end (point)))
  17276. (while (re-search-forward re end t)
  17277. (if (get-char-property (match-beginning 0) 'invisible)
  17278. (throw 'exit t))))
  17279. nil))))
  17280. (defun org-metaup (&optional arg)
  17281. "Move subtree up or move table row up.
  17282. Calls `org-move-subtree-up' or `org-table-move-row' or
  17283. `org-move-item-up', depending on context. See the individual commands
  17284. for more information."
  17285. (interactive "P")
  17286. (cond
  17287. ((run-hook-with-args-until-success 'org-metaup-hook))
  17288. ((org-region-active-p)
  17289. (let* ((a (min (region-beginning) (region-end)))
  17290. (b (1- (max (region-beginning) (region-end))))
  17291. (c (save-excursion (goto-char a)
  17292. (move-beginning-of-line 0)))
  17293. (d (save-excursion (goto-char a)
  17294. (move-end-of-line 0) (point))))
  17295. (transpose-regions a b c d)
  17296. (goto-char c)))
  17297. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17298. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17299. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17300. (t (org-drag-element-backward))))
  17301. (defun org-metadown (&optional arg)
  17302. "Move subtree down or move table row down.
  17303. Calls `org-move-subtree-down' or `org-table-move-row' or
  17304. `org-move-item-down', depending on context. See the individual
  17305. commands for more information."
  17306. (interactive "P")
  17307. (cond
  17308. ((run-hook-with-args-until-success 'org-metadown-hook))
  17309. ((org-region-active-p)
  17310. (let* ((a (min (region-beginning) (region-end)))
  17311. (b (max (region-beginning) (region-end)))
  17312. (c (save-excursion (goto-char b)
  17313. (move-beginning-of-line 1)))
  17314. (d (save-excursion (goto-char b)
  17315. (move-end-of-line 1) (1+ (point)))))
  17316. (transpose-regions a b c d)
  17317. (goto-char d)))
  17318. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17319. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17320. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17321. (t (org-drag-element-forward))))
  17322. (defun org-shiftup (&optional arg)
  17323. "Increase item in timestamp or increase priority of current headline.
  17324. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17325. depending on context. See the individual commands for more information."
  17326. (interactive "P")
  17327. (cond
  17328. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17329. ((and org-support-shift-select (org-region-active-p))
  17330. (org-call-for-shift-select 'previous-line))
  17331. ((org-at-timestamp-p t)
  17332. (call-interactively (if org-edit-timestamp-down-means-later
  17333. 'org-timestamp-down 'org-timestamp-up)))
  17334. ((and (not (eq org-support-shift-select 'always))
  17335. org-enable-priority-commands
  17336. (org-at-heading-p))
  17337. (call-interactively 'org-priority-up))
  17338. ((and (not org-support-shift-select) (org-at-item-p))
  17339. (call-interactively 'org-previous-item))
  17340. ((org-clocktable-try-shift 'up arg))
  17341. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17342. (org-support-shift-select
  17343. (org-call-for-shift-select 'previous-line))
  17344. (t (org-shiftselect-error))))
  17345. (defun org-shiftdown (&optional arg)
  17346. "Decrease item in timestamp or decrease priority of current headline.
  17347. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17348. depending on context. See the individual commands for more information."
  17349. (interactive "P")
  17350. (cond
  17351. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17352. ((and org-support-shift-select (org-region-active-p))
  17353. (org-call-for-shift-select 'next-line))
  17354. ((org-at-timestamp-p t)
  17355. (call-interactively (if org-edit-timestamp-down-means-later
  17356. 'org-timestamp-up 'org-timestamp-down)))
  17357. ((and (not (eq org-support-shift-select 'always))
  17358. org-enable-priority-commands
  17359. (org-at-heading-p))
  17360. (call-interactively 'org-priority-down))
  17361. ((and (not org-support-shift-select) (org-at-item-p))
  17362. (call-interactively 'org-next-item))
  17363. ((org-clocktable-try-shift 'down arg))
  17364. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17365. (org-support-shift-select
  17366. (org-call-for-shift-select 'next-line))
  17367. (t (org-shiftselect-error))))
  17368. (defun org-shiftright (&optional arg)
  17369. "Cycle the thing at point or in the current line, depending on context.
  17370. Depending on context, this does one of the following:
  17371. - switch a timestamp at point one day into the future
  17372. - on a headline, switch to the next TODO keyword.
  17373. - on an item, switch entire list to the next bullet type
  17374. - on a property line, switch to the next allowed value
  17375. - on a clocktable definition line, move time block into the future"
  17376. (interactive "P")
  17377. (cond
  17378. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17379. ((and org-support-shift-select (org-region-active-p))
  17380. (org-call-for-shift-select 'forward-char))
  17381. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17382. ((and (not (eq org-support-shift-select 'always))
  17383. (org-at-heading-p))
  17384. (let ((org-inhibit-logging
  17385. (not org-treat-S-cursor-todo-selection-as-state-change))
  17386. (org-inhibit-blocking
  17387. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17388. (org-call-with-arg 'org-todo 'right)))
  17389. ((or (and org-support-shift-select
  17390. (not (eq org-support-shift-select 'always))
  17391. (org-at-item-bullet-p))
  17392. (and (not org-support-shift-select) (org-at-item-p)))
  17393. (org-call-with-arg 'org-cycle-list-bullet nil))
  17394. ((and (not (eq org-support-shift-select 'always))
  17395. (org-at-property-p))
  17396. (call-interactively 'org-property-next-allowed-value))
  17397. ((org-clocktable-try-shift 'right arg))
  17398. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17399. (org-support-shift-select
  17400. (org-call-for-shift-select 'forward-char))
  17401. (t (org-shiftselect-error))))
  17402. (defun org-shiftleft (&optional arg)
  17403. "Cycle the thing at point or in the current line, depending on context.
  17404. Depending on context, this does one of the following:
  17405. - switch a timestamp at point one day into the past
  17406. - on a headline, switch to the previous TODO keyword.
  17407. - on an item, switch entire list to the previous bullet type
  17408. - on a property line, switch to the previous allowed value
  17409. - on a clocktable definition line, move time block into the past"
  17410. (interactive "P")
  17411. (cond
  17412. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17413. ((and org-support-shift-select (org-region-active-p))
  17414. (org-call-for-shift-select 'backward-char))
  17415. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17416. ((and (not (eq org-support-shift-select 'always))
  17417. (org-at-heading-p))
  17418. (let ((org-inhibit-logging
  17419. (not org-treat-S-cursor-todo-selection-as-state-change))
  17420. (org-inhibit-blocking
  17421. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17422. (org-call-with-arg 'org-todo 'left)))
  17423. ((or (and org-support-shift-select
  17424. (not (eq org-support-shift-select 'always))
  17425. (org-at-item-bullet-p))
  17426. (and (not org-support-shift-select) (org-at-item-p)))
  17427. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17428. ((and (not (eq org-support-shift-select 'always))
  17429. (org-at-property-p))
  17430. (call-interactively 'org-property-previous-allowed-value))
  17431. ((org-clocktable-try-shift 'left arg))
  17432. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17433. (org-support-shift-select
  17434. (org-call-for-shift-select 'backward-char))
  17435. (t (org-shiftselect-error))))
  17436. (defun org-shiftcontrolright ()
  17437. "Switch to next TODO set."
  17438. (interactive)
  17439. (cond
  17440. ((and org-support-shift-select (org-region-active-p))
  17441. (org-call-for-shift-select 'forward-word))
  17442. ((and (not (eq org-support-shift-select 'always))
  17443. (org-at-heading-p))
  17444. (org-call-with-arg 'org-todo 'nextset))
  17445. (org-support-shift-select
  17446. (org-call-for-shift-select 'forward-word))
  17447. (t (org-shiftselect-error))))
  17448. (defun org-shiftcontrolleft ()
  17449. "Switch to previous TODO set."
  17450. (interactive)
  17451. (cond
  17452. ((and org-support-shift-select (org-region-active-p))
  17453. (org-call-for-shift-select 'backward-word))
  17454. ((and (not (eq org-support-shift-select 'always))
  17455. (org-at-heading-p))
  17456. (org-call-with-arg 'org-todo 'previousset))
  17457. (org-support-shift-select
  17458. (org-call-for-shift-select 'backward-word))
  17459. (t (org-shiftselect-error))))
  17460. (defun org-shiftcontrolup ()
  17461. "Change timestamps synchronously up in CLOCK log lines."
  17462. (interactive)
  17463. (cond ((and (not org-support-shift-select)
  17464. (org-at-clock-log-p)
  17465. (org-at-timestamp-p t))
  17466. (org-clock-timestamps-up))
  17467. (t (org-shiftselect-error))))
  17468. (defun org-shiftcontroldown ()
  17469. "Change timestamps synchronously down in CLOCK log lines."
  17470. (interactive)
  17471. (cond ((and (not org-support-shift-select)
  17472. (org-at-clock-log-p)
  17473. (org-at-timestamp-p t))
  17474. (org-clock-timestamps-down))
  17475. (t (org-shiftselect-error))))
  17476. (defun org-ctrl-c-ret ()
  17477. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17478. (interactive)
  17479. (cond
  17480. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17481. (t (call-interactively 'org-insert-heading))))
  17482. (defun org-find-visible ()
  17483. (let ((s (point)))
  17484. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17485. (get-char-property s 'invisible)))
  17486. s))
  17487. (defun org-find-invisible ()
  17488. (let ((s (point)))
  17489. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17490. (not (get-char-property s 'invisible))))
  17491. s))
  17492. (defun org-copy-visible (beg end)
  17493. "Copy the visible parts of the region."
  17494. (interactive "r")
  17495. (let (snippets s)
  17496. (save-excursion
  17497. (save-restriction
  17498. (narrow-to-region beg end)
  17499. (setq s (goto-char (point-min)))
  17500. (while (not (= (point) (point-max)))
  17501. (goto-char (org-find-invisible))
  17502. (push (buffer-substring s (point)) snippets)
  17503. (setq s (goto-char (org-find-visible))))))
  17504. (kill-new (apply 'concat (nreverse snippets)))))
  17505. (defun org-copy-special ()
  17506. "Copy region in table or copy current subtree.
  17507. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17508. See the individual commands for more information."
  17509. (interactive)
  17510. (call-interactively
  17511. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17512. (defun org-cut-special ()
  17513. "Cut region in table or cut current subtree.
  17514. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17515. See the individual commands for more information."
  17516. (interactive)
  17517. (call-interactively
  17518. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17519. (defun org-paste-special (arg)
  17520. "Paste rectangular region into table, or past subtree relative to level.
  17521. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17522. See the individual commands for more information."
  17523. (interactive "P")
  17524. (if (org-at-table-p)
  17525. (org-table-paste-rectangle)
  17526. (org-paste-subtree arg)))
  17527. (defun org-edit-special (&optional arg)
  17528. "Call a special editor for the stuff at point.
  17529. When at a table, call the formula editor with `org-table-edit-formulas'.
  17530. When at the first line of an src example, call `org-edit-src-code'.
  17531. When in an #+include line, visit the include file. Otherwise call
  17532. `ffap' to visit the file at point."
  17533. (interactive)
  17534. ;; possibly prep session before editing source
  17535. (when arg
  17536. (let* ((info (org-babel-get-src-block-info))
  17537. (lang (nth 0 info))
  17538. (params (nth 2 info))
  17539. (session (cdr (assoc :session params))))
  17540. (when (and info session) ;; we are in a source-code block with a session
  17541. (funcall
  17542. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17543. (cond ;; proceed with `org-edit-special'
  17544. ((save-excursion
  17545. (beginning-of-line 1)
  17546. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17547. (find-file (org-trim (match-string 1))))
  17548. ((org-edit-src-code))
  17549. ((org-edit-fixed-width-region))
  17550. ((org-at-table.el-p)
  17551. (org-edit-src-code))
  17552. ((or (org-at-table-p)
  17553. (save-excursion
  17554. (beginning-of-line 1)
  17555. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17556. (call-interactively 'org-table-edit-formulas))
  17557. (t (call-interactively 'ffap))))
  17558. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17559. (defun org-ctrl-c-ctrl-c (&optional arg)
  17560. "Set tags in headline, or update according to changed information at point.
  17561. This command does many different things, depending on context:
  17562. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17563. this is what we do.
  17564. - If the cursor is on a statistics cookie, update it.
  17565. - If the cursor is in a headline, prompt for tags and insert them
  17566. into the current line, aligned to `org-tags-column'. When called
  17567. with prefix arg, realign all tags in the current buffer.
  17568. - If the cursor is in one of the special #+KEYWORD lines, this
  17569. triggers scanning the buffer for these lines and updating the
  17570. information.
  17571. - If the cursor is inside a table, realign the table. This command
  17572. works even if the automatic table editor has been turned off.
  17573. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17574. the entire table.
  17575. - If the cursor is at a footnote reference or definition, jump to
  17576. the corresponding definition or references, respectively.
  17577. - If the cursor is a the beginning of a dynamic block, update it.
  17578. - If the current buffer is a capture buffer, close note and file it.
  17579. - If the cursor is on a <<<target>>>, update radio targets and
  17580. corresponding links in this buffer.
  17581. - If the cursor is on a numbered item in a plain list, renumber the
  17582. ordered list.
  17583. - If the cursor is on a checkbox, toggle it.
  17584. - If the cursor is on a code block, evaluate it. The variable
  17585. `org-confirm-babel-evaluate' can be used to control prompting
  17586. before code block evaluation, by default every code block
  17587. evaluation requires confirmation. Code block evaluation can be
  17588. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17589. (interactive "P")
  17590. (let ((org-enable-table-editor t))
  17591. (cond
  17592. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17593. org-occur-highlights
  17594. org-latex-fragment-image-overlays)
  17595. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17596. (org-remove-occur-highlights)
  17597. (org-remove-latex-fragment-image-overlays)
  17598. (message "Temporary highlights/overlays removed from current buffer"))
  17599. ((and (local-variable-p 'org-finish-function (current-buffer))
  17600. (fboundp org-finish-function))
  17601. (funcall org-finish-function))
  17602. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17603. ((org-in-regexp org-ts-regexp-both)
  17604. (org-timestamp-change 0 'day))
  17605. ((or (looking-at org-property-start-re)
  17606. (org-at-property-p))
  17607. (call-interactively 'org-property-action))
  17608. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17609. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17610. (or (org-at-heading-p) (org-at-item-p)))
  17611. (call-interactively 'org-update-statistics-cookies))
  17612. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17613. ((org-at-table.el-p)
  17614. (message "Use C-c ' to edit table.el tables"))
  17615. ((org-at-table-p)
  17616. (org-table-maybe-eval-formula)
  17617. (if arg
  17618. (call-interactively 'org-table-recalculate)
  17619. (org-table-maybe-recalculate-line))
  17620. (call-interactively 'org-table-align)
  17621. (orgtbl-send-table 'maybe))
  17622. ((or (org-footnote-at-reference-p)
  17623. (org-footnote-at-definition-p))
  17624. (call-interactively 'org-footnote-action))
  17625. ((org-at-item-checkbox-p)
  17626. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17627. ;; list only if at top item.
  17628. (let* ((cbox (match-string 1))
  17629. (struct (org-list-struct))
  17630. (old-struct (copy-tree struct))
  17631. (parents (org-list-parents-alist struct))
  17632. (orderedp (org-entry-get nil "ORDERED"))
  17633. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17634. block-item)
  17635. ;; Use a light version of `org-toggle-checkbox' to avoid
  17636. ;; computing list structure twice.
  17637. (let ((new-box (cond
  17638. ((equal arg '(16)) "[-]")
  17639. ((equal arg '(4)) nil)
  17640. ((equal "[X]" cbox) "[ ]")
  17641. (t "[X]"))))
  17642. (if (and firstp arg)
  17643. ;; If at first item of sub-list, remove check-box from
  17644. ;; every item at the same level.
  17645. (mapc
  17646. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17647. (org-list-get-all-items
  17648. (point-at-bol) struct (org-list-prevs-alist struct)))
  17649. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17650. ;; Replicate `org-list-write-struct', while grabbing a return
  17651. ;; value from `org-list-struct-fix-box'.
  17652. (org-list-struct-fix-ind struct parents 2)
  17653. (org-list-struct-fix-item-end struct)
  17654. (let ((prevs (org-list-prevs-alist struct)))
  17655. (org-list-struct-fix-bul struct prevs)
  17656. (org-list-struct-fix-ind struct parents)
  17657. (setq block-item
  17658. (org-list-struct-fix-box struct parents prevs orderedp)))
  17659. (org-list-struct-apply-struct struct old-struct)
  17660. (org-update-checkbox-count-maybe)
  17661. (when block-item
  17662. (message
  17663. "Checkboxes were removed due to unchecked box at line %d"
  17664. (org-current-line block-item)))
  17665. (when firstp (org-list-send-list 'maybe))))
  17666. ((org-at-item-p)
  17667. ;; Cursor at an item: repair list. Do checkbox related actions
  17668. ;; only if function was called with an argument. Send list only
  17669. ;; if at top item.
  17670. (let* ((struct (org-list-struct))
  17671. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17672. old-struct)
  17673. (when arg
  17674. (setq old-struct (copy-tree struct))
  17675. (if firstp
  17676. ;; If at first item of sub-list, add check-box to every
  17677. ;; item at the same level.
  17678. (mapc
  17679. (lambda (pos)
  17680. (unless (org-list-get-checkbox pos struct)
  17681. (org-list-set-checkbox pos struct "[ ]")))
  17682. (org-list-get-all-items
  17683. (point-at-bol) struct (org-list-prevs-alist struct)))
  17684. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17685. (org-list-write-struct
  17686. struct (org-list-parents-alist struct) old-struct)
  17687. (when arg (org-update-checkbox-count-maybe))
  17688. (when firstp (org-list-send-list 'maybe))))
  17689. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17690. ;; Dynamic block
  17691. (beginning-of-line 1)
  17692. (save-excursion (org-update-dblock)))
  17693. ((save-excursion
  17694. (let ((case-fold-search t))
  17695. (beginning-of-line 1)
  17696. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17697. (cond
  17698. ((or (equal (match-string 1) "TBLFM")
  17699. (equal (match-string 1) "tblfm"))
  17700. ;; Recalculate the table before this line
  17701. (save-excursion
  17702. (beginning-of-line 1)
  17703. (skip-chars-backward " \r\n\t")
  17704. (if (org-at-table-p)
  17705. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17706. (t
  17707. (let ((org-inhibit-startup-visibility-stuff t)
  17708. (org-startup-align-all-tables nil))
  17709. (when (boundp 'org-table-coordinate-overlays)
  17710. (mapc 'delete-overlay org-table-coordinate-overlays)
  17711. (setq org-table-coordinate-overlays nil))
  17712. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17713. (message "Local setup has been refreshed"))))
  17714. ((org-clock-update-time-maybe))
  17715. (t
  17716. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17717. (error "C-c C-c can do nothing useful at this location"))))))
  17718. (defun org-mode-restart ()
  17719. "Restart Org-mode, to scan again for special lines.
  17720. Also updates the keyword regular expressions."
  17721. (interactive)
  17722. (org-mode)
  17723. (message "Org-mode restarted"))
  17724. (defun org-kill-note-or-show-branches ()
  17725. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17726. (interactive)
  17727. (if (not org-finish-function)
  17728. (progn
  17729. (hide-subtree)
  17730. (call-interactively 'show-branches))
  17731. (let ((org-note-abort t))
  17732. (funcall org-finish-function))))
  17733. (defun org-return (&optional indent)
  17734. "Goto next table row or insert a newline.
  17735. Calls `org-table-next-row' or `newline', depending on context.
  17736. See the individual commands for more information."
  17737. (interactive)
  17738. (let (org-ts-what)
  17739. (cond
  17740. ((or (bobp) (org-in-src-block-p))
  17741. (if indent (newline-and-indent) (newline)))
  17742. ((org-at-table-p)
  17743. (org-table-justify-field-maybe)
  17744. (call-interactively 'org-table-next-row))
  17745. ;; when `newline-and-indent' is called within a list, make sure
  17746. ;; text moved stays inside the item.
  17747. ((and (org-in-item-p) indent)
  17748. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17749. (progn
  17750. (save-match-data (newline))
  17751. (org-indent-line-to (length (match-string 0))))
  17752. (let ((ind (org-get-indentation)))
  17753. (newline)
  17754. (if (org-looking-back org-list-end-re)
  17755. (org-indent-line)
  17756. (org-indent-line-to ind)))))
  17757. ((and org-return-follows-link
  17758. (org-at-timestamp-p t)
  17759. (not (eq org-ts-what 'after)))
  17760. (org-follow-timestamp-link))
  17761. ((and org-return-follows-link
  17762. (let ((tprop (get-text-property (point) 'face)))
  17763. (or (eq tprop 'org-link)
  17764. (and (listp tprop) (memq 'org-link tprop)))))
  17765. (call-interactively 'org-open-at-point))
  17766. ((and (org-at-heading-p)
  17767. (looking-at
  17768. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17769. (org-show-entry)
  17770. (end-of-line 1)
  17771. (newline))
  17772. (t (if indent (newline-and-indent) (newline))))))
  17773. (defun org-return-indent ()
  17774. "Goto next table row or insert a newline and indent.
  17775. Calls `org-table-next-row' or `newline-and-indent', depending on
  17776. context. See the individual commands for more information."
  17777. (interactive)
  17778. (org-return t))
  17779. (defun org-ctrl-c-star ()
  17780. "Compute table, or change heading status of lines.
  17781. Calls `org-table-recalculate' or `org-toggle-heading',
  17782. depending on context."
  17783. (interactive)
  17784. (cond
  17785. ((org-at-table-p)
  17786. (call-interactively 'org-table-recalculate))
  17787. (t
  17788. ;; Convert all lines in region to list items
  17789. (call-interactively 'org-toggle-heading))))
  17790. (defun org-ctrl-c-minus ()
  17791. "Insert separator line in table or modify bullet status of line.
  17792. Also turns a plain line or a region of lines into list items.
  17793. Calls `org-table-insert-hline', `org-toggle-item', or
  17794. `org-cycle-list-bullet', depending on context."
  17795. (interactive)
  17796. (cond
  17797. ((org-at-table-p)
  17798. (call-interactively 'org-table-insert-hline))
  17799. ((org-region-active-p)
  17800. (call-interactively 'org-toggle-item))
  17801. ((org-in-item-p)
  17802. (call-interactively 'org-cycle-list-bullet))
  17803. (t
  17804. (call-interactively 'org-toggle-item))))
  17805. (defun org-toggle-item (arg)
  17806. "Convert headings or normal lines to items, items to normal lines.
  17807. If there is no active region, only the current line is considered.
  17808. If the first non blank line in the region is an headline, convert
  17809. all headlines to items, shifting text accordingly.
  17810. If it is an item, convert all items to normal lines.
  17811. If it is normal text, change region into an item. With a prefix
  17812. argument ARG, change each line in region into an item."
  17813. (interactive "P")
  17814. (let ((shift-text
  17815. (function
  17816. ;; Shift text in current section to IND, from point to END.
  17817. ;; The function leaves point to END line.
  17818. (lambda (ind end)
  17819. (let ((min-i 1000) (end (copy-marker end)))
  17820. ;; First determine the minimum indentation (MIN-I) of
  17821. ;; the text.
  17822. (save-excursion
  17823. (catch 'exit
  17824. (while (< (point) end)
  17825. (let ((i (org-get-indentation)))
  17826. (cond
  17827. ;; Skip blank lines and inline tasks.
  17828. ((looking-at "^[ \t]*$"))
  17829. ((looking-at org-outline-regexp-bol))
  17830. ;; We can't find less than 0 indentation.
  17831. ((zerop i) (throw 'exit (setq min-i 0)))
  17832. ((< i min-i) (setq min-i i))))
  17833. (forward-line))))
  17834. ;; Then indent each line so that a line indented to
  17835. ;; MIN-I becomes indented to IND. Ignore blank lines
  17836. ;; and inline tasks in the process.
  17837. (let ((delta (- ind min-i)))
  17838. (while (< (point) end)
  17839. (unless (or (looking-at "^[ \t]*$")
  17840. (looking-at org-outline-regexp-bol))
  17841. (org-indent-line-to (+ (org-get-indentation) delta)))
  17842. (forward-line)))))))
  17843. (skip-blanks
  17844. (function
  17845. ;; Return beginning of first non-blank line, starting from
  17846. ;; line at POS.
  17847. (lambda (pos)
  17848. (save-excursion
  17849. (goto-char pos)
  17850. (skip-chars-forward " \r\t\n")
  17851. (point-at-bol)))))
  17852. beg end)
  17853. ;; Determine boundaries of changes.
  17854. (if (org-region-active-p)
  17855. (setq beg (funcall skip-blanks (region-beginning))
  17856. end (copy-marker (region-end)))
  17857. (setq beg (funcall skip-blanks (point-at-bol))
  17858. end (copy-marker (point-at-eol))))
  17859. ;; Depending on the starting line, choose an action on the text
  17860. ;; between BEG and END.
  17861. (org-with-limited-levels
  17862. (save-excursion
  17863. (goto-char beg)
  17864. (cond
  17865. ;; Case 1. Start at an item: de-itemize. Note that it only
  17866. ;; happens when a region is active: `org-ctrl-c-minus'
  17867. ;; would call `org-cycle-list-bullet' otherwise.
  17868. ((org-at-item-p)
  17869. (while (< (point) end)
  17870. (when (org-at-item-p)
  17871. (skip-chars-forward " \t")
  17872. (delete-region (point) (match-end 0)))
  17873. (forward-line)))
  17874. ;; Case 2. Start at an heading: convert to items.
  17875. ((org-at-heading-p)
  17876. (let* ((bul (org-list-bullet-string "-"))
  17877. (bul-len (length bul))
  17878. ;; Indentation of the first heading. It should be
  17879. ;; relative to the indentation of its parent, if any.
  17880. (start-ind (save-excursion
  17881. (cond
  17882. ((not org-adapt-indentation) 0)
  17883. ((not (outline-previous-heading)) 0)
  17884. (t (length (match-string 0))))))
  17885. ;; Level of first heading. Further headings will be
  17886. ;; compared to it to determine hierarchy in the list.
  17887. (ref-level (org-reduced-level (org-outline-level))))
  17888. (while (< (point) end)
  17889. (let* ((level (org-reduced-level (org-outline-level)))
  17890. (delta (max 0 (- level ref-level))))
  17891. ;; If current headline is less indented than the first
  17892. ;; one, set it as reference, in order to preserve
  17893. ;; subtrees.
  17894. (when (< level ref-level) (setq ref-level level))
  17895. (replace-match bul t t)
  17896. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17897. ;; Ensure all text down to END (or SECTION-END) belongs
  17898. ;; to the newly created item.
  17899. (let ((section-end (save-excursion
  17900. (or (outline-next-heading) (point)))))
  17901. (forward-line)
  17902. (funcall shift-text
  17903. (+ start-ind (* (1+ delta) bul-len))
  17904. (min end section-end)))))))
  17905. ;; Case 3. Normal line with ARG: turn each non-item line into
  17906. ;; an item.
  17907. (arg
  17908. (while (< (point) end)
  17909. (unless (or (org-at-heading-p) (org-at-item-p))
  17910. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17911. (replace-match
  17912. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17913. (forward-line)))
  17914. ;; Case 4. Normal line without ARG: make the first line of
  17915. ;; region an item, and shift indentation of others
  17916. ;; lines to set them as item's body.
  17917. (t (let* ((bul (org-list-bullet-string "-"))
  17918. (bul-len (length bul))
  17919. (ref-ind (org-get-indentation)))
  17920. (skip-chars-forward " \t")
  17921. (insert bul)
  17922. (forward-line)
  17923. (while (< (point) end)
  17924. ;; Ensure that lines less indented than first one
  17925. ;; still get included in item body.
  17926. (funcall shift-text
  17927. (+ ref-ind bul-len)
  17928. (min end (save-excursion (or (outline-next-heading)
  17929. (point)))))
  17930. (forward-line)))))))))
  17931. (defun org-toggle-heading (&optional nstars)
  17932. "Convert headings to normal text, or items or text to headings.
  17933. If there is no active region, only the current line is considered.
  17934. With a \\[universal-argument] prefix, convert the whole list at
  17935. point into heading.
  17936. In a region:
  17937. - If the first non blank line is an headline, remove the stars
  17938. from all headlines in the region.
  17939. - If it is a normal line turn each and every normal line (i.e. not an
  17940. heading or an item) in the region into a heading.
  17941. - If it is a plain list item, turn all plain list items into headings.
  17942. When converting a line into a heading, the number of stars is chosen
  17943. such that the lines become children of the current entry. However,
  17944. when a prefix argument is given, its value determines the number of
  17945. stars to add."
  17946. (interactive "P")
  17947. (let ((skip-blanks
  17948. (function
  17949. ;; Return beginning of first non-blank line, starting from
  17950. ;; line at POS.
  17951. (lambda (pos)
  17952. (save-excursion
  17953. (goto-char pos)
  17954. (while (org-at-comment-p) (forward-line))
  17955. (skip-chars-forward " \r\t\n")
  17956. (point-at-bol)))))
  17957. beg end toggled)
  17958. ;; Determine boundaries of changes. If a universal prefix has
  17959. ;; been given, put the list in a region. If region ends at a bol,
  17960. ;; do not consider the last line to be in the region.
  17961. (when (and current-prefix-arg (org-at-item-p))
  17962. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17963. (org-mark-element))
  17964. (if (org-region-active-p)
  17965. (setq beg (funcall skip-blanks (region-beginning))
  17966. end (copy-marker (save-excursion
  17967. (goto-char (region-end))
  17968. (if (bolp) (point) (point-at-eol)))))
  17969. (setq beg (funcall skip-blanks (point-at-bol))
  17970. end (copy-marker (point-at-eol))))
  17971. ;; Ensure inline tasks don't count as headings.
  17972. (org-with-limited-levels
  17973. (save-excursion
  17974. (goto-char beg)
  17975. (cond
  17976. ;; Case 1. Started at an heading: de-star headings.
  17977. ((org-at-heading-p)
  17978. (while (< (point) end)
  17979. (when (org-at-heading-p t)
  17980. (looking-at org-outline-regexp) (replace-match "")
  17981. (setq toggled t))
  17982. (forward-line)))
  17983. ;; Case 2. Started at an item: change items into headlines.
  17984. ;; One star will be added by `org-list-to-subtree'.
  17985. ((org-at-item-p)
  17986. (let* ((stars (make-string
  17987. (if nstars
  17988. ;; subtract the star that will be added again by
  17989. ;; `org-list-to-subtree'
  17990. (1- (prefix-numeric-value current-prefix-arg))
  17991. (or (org-current-level) 0))
  17992. ?*))
  17993. (add-stars
  17994. (cond (nstars "") ; stars from prefix only
  17995. ((equal stars "") "") ; before first heading
  17996. (org-odd-levels-only "*") ; inside heading, odd
  17997. (t "")))) ; inside heading, oddeven
  17998. (while (< (point) end)
  17999. (when (org-at-item-p)
  18000. ;; Pay attention to cases when region ends before list.
  18001. (let* ((struct (org-list-struct))
  18002. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18003. (save-restriction
  18004. (narrow-to-region (point) list-end)
  18005. (insert
  18006. (org-list-to-subtree
  18007. (org-list-parse-list t)
  18008. '(:istart (concat stars add-stars (funcall get-stars depth))
  18009. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18010. (setq toggled t))
  18011. (forward-line))))
  18012. ;; Case 3. Started at normal text: make every line an heading,
  18013. ;; skipping headlines and items.
  18014. (t (let* ((stars (make-string
  18015. (if nstars
  18016. (prefix-numeric-value current-prefix-arg)
  18017. (or (org-current-level) 0))
  18018. ?*))
  18019. (add-stars
  18020. (cond (nstars "") ; stars from prefix only
  18021. ((equal stars "") "*") ; before first heading
  18022. (org-odd-levels-only "**") ; inside heading, odd
  18023. (t "*"))) ; inside heading, oddeven
  18024. (rpl (concat stars add-stars " ")))
  18025. (while (< (point) end)
  18026. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18027. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18028. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18029. (forward-line)))))))
  18030. (unless toggled (message "Cannot toggle heading from here"))))
  18031. (defun org-meta-return (&optional arg)
  18032. "Insert a new heading or wrap a region in a table.
  18033. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18034. See the individual commands for more information."
  18035. (interactive "P")
  18036. (cond
  18037. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18038. ((or (org-at-drawer-p) (org-at-property-p))
  18039. (newline-and-indent))
  18040. ((org-at-table-p)
  18041. (call-interactively 'org-table-wrap-region))
  18042. (t (call-interactively 'org-insert-heading))))
  18043. ;;; Menu entries
  18044. (defsubst org-in-subtree-not-table-p ()
  18045. "Are we in a subtree and not in a table?"
  18046. (and (not (org-before-first-heading-p))
  18047. (not (org-at-table-p))))
  18048. ;; Define the Org-mode menus
  18049. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18050. '("Tbl"
  18051. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18052. ["Next Field" org-cycle (org-at-table-p)]
  18053. ["Previous Field" org-shifttab (org-at-table-p)]
  18054. ["Next Row" org-return (org-at-table-p)]
  18055. "--"
  18056. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18057. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18058. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18059. "--"
  18060. ("Column"
  18061. ["Move Column Left" org-metaleft (org-at-table-p)]
  18062. ["Move Column Right" org-metaright (org-at-table-p)]
  18063. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18064. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18065. ("Row"
  18066. ["Move Row Up" org-metaup (org-at-table-p)]
  18067. ["Move Row Down" org-metadown (org-at-table-p)]
  18068. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18069. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18070. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18071. "--"
  18072. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18073. ("Rectangle"
  18074. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18075. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18076. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18077. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18078. "--"
  18079. ("Calculate"
  18080. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18081. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18082. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18083. "--"
  18084. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18085. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18086. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18087. "--"
  18088. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18089. "--"
  18090. ["Sum Column/Rectangle" org-table-sum
  18091. (or (org-at-table-p) (org-region-active-p))]
  18092. ["Which Column?" org-table-current-column (org-at-table-p)])
  18093. ["Debug Formulas"
  18094. org-table-toggle-formula-debugger
  18095. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18096. ["Show Col/Row Numbers"
  18097. org-table-toggle-coordinate-overlays
  18098. :style toggle
  18099. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18100. "--"
  18101. ["Create" org-table-create (and (not (org-at-table-p))
  18102. org-enable-table-editor)]
  18103. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18104. ["Import from File" org-table-import (not (org-at-table-p))]
  18105. ["Export to File" org-table-export (org-at-table-p)]
  18106. "--"
  18107. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18108. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18109. '("Org"
  18110. ("Show/Hide"
  18111. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18112. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18113. ["Sparse Tree..." org-sparse-tree t]
  18114. ["Reveal Context" org-reveal t]
  18115. ["Show All" show-all t]
  18116. "--"
  18117. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18118. "--"
  18119. ["New Heading" org-insert-heading t]
  18120. ("Navigate Headings"
  18121. ["Up" outline-up-heading t]
  18122. ["Next" outline-next-visible-heading t]
  18123. ["Previous" outline-previous-visible-heading t]
  18124. ["Next Same Level" outline-forward-same-level t]
  18125. ["Previous Same Level" outline-backward-same-level t]
  18126. "--"
  18127. ["Jump" org-goto t])
  18128. ("Edit Structure"
  18129. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18130. "--"
  18131. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18132. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18133. "--"
  18134. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18135. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18136. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18137. "--"
  18138. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18139. "--"
  18140. ["Copy visible text" org-copy-visible t]
  18141. "--"
  18142. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18143. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18144. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18145. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18146. "--"
  18147. ["Sort Region/Children" org-sort t]
  18148. "--"
  18149. ["Convert to odd levels" org-convert-to-odd-levels t]
  18150. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18151. ("Editing"
  18152. ["Emphasis..." org-emphasize t]
  18153. ["Edit Source Example" org-edit-special t]
  18154. "--"
  18155. ["Footnote new/jump" org-footnote-action t]
  18156. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18157. ("Archive"
  18158. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18159. "--"
  18160. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18161. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18162. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18163. )
  18164. "--"
  18165. ("Hyperlinks"
  18166. ["Store Link (Global)" org-store-link t]
  18167. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18168. ["Insert Link" org-insert-link t]
  18169. ["Follow Link" org-open-at-point t]
  18170. "--"
  18171. ["Next link" org-next-link t]
  18172. ["Previous link" org-previous-link t]
  18173. "--"
  18174. ["Descriptive Links"
  18175. org-toggle-link-display
  18176. :style radio
  18177. :selected org-descriptive-links
  18178. ]
  18179. ["Literal Links"
  18180. org-toggle-link-display
  18181. :style radio
  18182. :selected (not org-descriptive-links)])
  18183. "--"
  18184. ("TODO Lists"
  18185. ["TODO/DONE/-" org-todo t]
  18186. ("Select keyword"
  18187. ["Next keyword" org-shiftright (org-at-heading-p)]
  18188. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18189. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18190. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18191. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18192. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18193. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18194. "--"
  18195. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18196. :selected org-enforce-todo-dependencies :style toggle :active t]
  18197. "Settings for tree at point"
  18198. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18199. :selected (org-entry-get nil "ORDERED")
  18200. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18201. ["Do Children parallel" org-toggle-ordered-property :style radio
  18202. :selected (not (org-entry-get nil "ORDERED"))
  18203. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18204. "--"
  18205. ["Set Priority" org-priority t]
  18206. ["Priority Up" org-shiftup t]
  18207. ["Priority Down" org-shiftdown t]
  18208. "--"
  18209. ["Get news from all feeds" org-feed-update-all t]
  18210. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18211. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18212. ("TAGS and Properties"
  18213. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18214. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18215. "--"
  18216. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18217. ["Column view of properties" org-columns t]
  18218. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18219. ("Dates and Scheduling"
  18220. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18221. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18222. ("Change Date"
  18223. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18224. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18225. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18226. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18227. ["Compute Time Range" org-evaluate-time-range t]
  18228. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18229. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18230. "--"
  18231. ["Custom time format" org-toggle-time-stamp-overlays
  18232. :style radio :selected org-display-custom-times]
  18233. "--"
  18234. ["Goto Calendar" org-goto-calendar t]
  18235. ["Date from Calendar" org-date-from-calendar t]
  18236. "--"
  18237. ["Start/Restart Timer" org-timer-start t]
  18238. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18239. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18240. ["Insert Timer String" org-timer t]
  18241. ["Insert Timer Item" org-timer-item t])
  18242. ("Logging work"
  18243. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18244. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18245. ["Clock out" org-clock-out t]
  18246. ["Clock cancel" org-clock-cancel t]
  18247. "--"
  18248. ["Mark as default task" org-clock-mark-default-task t]
  18249. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18250. ["Goto running clock" org-clock-goto t]
  18251. "--"
  18252. ["Display times" org-clock-display t]
  18253. ["Create clock table" org-clock-report t]
  18254. "--"
  18255. ["Record DONE time"
  18256. (progn (setq org-log-done (not org-log-done))
  18257. (message "Switching to %s will %s record a timestamp"
  18258. (car org-done-keywords)
  18259. (if org-log-done "automatically" "not")))
  18260. :style toggle :selected org-log-done])
  18261. "--"
  18262. ["Agenda Command..." org-agenda t]
  18263. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18264. ("File List for Agenda")
  18265. ("Special views current file"
  18266. ["TODO Tree" org-show-todo-tree t]
  18267. ["Check Deadlines" org-check-deadlines t]
  18268. ["Timeline" org-timeline t]
  18269. ["Tags/Property tree" org-match-sparse-tree t])
  18270. "--"
  18271. ["Export/Publish..." org-export t]
  18272. ("LaTeX"
  18273. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18274. :selected org-cdlatex-mode]
  18275. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18276. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18277. ["Modify math symbol" org-cdlatex-math-modify
  18278. (org-inside-LaTeX-fragment-p)]
  18279. ["Insert citation" org-reftex-citation t]
  18280. "--"
  18281. ["Template for BEAMER" (progn (require 'org-beamer)
  18282. (org-insert-beamer-options-template)) t])
  18283. "--"
  18284. ("MobileOrg"
  18285. ["Push Files and Views" org-mobile-push t]
  18286. ["Get Captured and Flagged" org-mobile-pull t]
  18287. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18288. "--"
  18289. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18290. "--"
  18291. ("Documentation"
  18292. ["Show Version" org-version t]
  18293. ["Info Documentation" org-info t])
  18294. ("Customize"
  18295. ["Browse Org Group" org-customize t]
  18296. "--"
  18297. ["Expand This Menu" org-create-customize-menu
  18298. (fboundp 'customize-menu-create)])
  18299. ["Send bug report" org-submit-bug-report t]
  18300. "--"
  18301. ("Refresh/Reload"
  18302. ["Refresh setup current buffer" org-mode-restart t]
  18303. ["Reload Org (after update)" org-reload t]
  18304. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18305. ))
  18306. (defun org-info (&optional node)
  18307. "Read documentation for Org-mode in the info system.
  18308. With optional NODE, go directly to that node."
  18309. (interactive)
  18310. (info (format "(org)%s" (or node ""))))
  18311. ;;;###autoload
  18312. (defun org-submit-bug-report ()
  18313. "Submit a bug report on Org-mode via mail.
  18314. Don't hesitate to report any problems or inaccurate documentation.
  18315. If you don't have setup sending mail from (X)Emacs, please copy the
  18316. output buffer into your mail program, as it gives us important
  18317. information about your Org-mode version and configuration."
  18318. (interactive)
  18319. (require 'reporter)
  18320. (org-load-modules-maybe)
  18321. (org-require-autoloaded-modules)
  18322. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18323. (reporter-submit-bug-report
  18324. "emacs-orgmode@gnu.org"
  18325. (org-version nil 'full)
  18326. (let (list)
  18327. (save-window-excursion
  18328. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18329. (delete-other-windows)
  18330. (erase-buffer)
  18331. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18332. We would like to add your full Org-mode and Outline configuration to the
  18333. bug report. This greatly simplifies the work of the maintainer and
  18334. other experts on the mailing list.
  18335. HOWEVER, some variables you have customized may contain private
  18336. information. The names of customers, colleagues, or friends, might
  18337. appear in the form of file names, tags, todo states, or search strings.
  18338. If you answer yes to the prompt, you might want to check and remove
  18339. such private information before sending the email.")
  18340. (add-text-properties (point-min) (point-max) '(face org-warning))
  18341. (when (yes-or-no-p "Include your Org-mode configuration ")
  18342. (mapatoms
  18343. (lambda (v)
  18344. (and (boundp v)
  18345. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18346. (or (and (symbol-value v)
  18347. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18348. (and
  18349. (get v 'custom-type) (get v 'standard-value)
  18350. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18351. (push v list)))))
  18352. (kill-buffer (get-buffer "*Warn about privacy*"))
  18353. list))
  18354. nil nil
  18355. "Remember to cover the basics, that is, what you expected to happen and
  18356. what in fact did happen. You don't know how to make a good report? See
  18357. http://orgmode.org/manual/Feedback.html#Feedback
  18358. Your bug report will be posted to the Org-mode mailing list.
  18359. ------------------------------------------------------------------------")
  18360. (save-excursion
  18361. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18362. (replace-match "\\1Bug: \\3 [\\2]")))))
  18363. (defun org-install-agenda-files-menu ()
  18364. (let ((bl (buffer-list)))
  18365. (save-excursion
  18366. (while bl
  18367. (set-buffer (pop bl))
  18368. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18369. (when (derived-mode-p 'org-mode)
  18370. (easy-menu-change
  18371. '("Org") "File List for Agenda"
  18372. (append
  18373. (list
  18374. ["Edit File List" (org-edit-agenda-file-list) t]
  18375. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18376. ["Remove Current File from List" org-remove-file t]
  18377. ["Cycle through agenda files" org-cycle-agenda-files t]
  18378. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18379. "--")
  18380. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18381. ;;;; Documentation
  18382. (defun org-require-autoloaded-modules ()
  18383. (interactive)
  18384. (mapc 'require
  18385. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18386. org-docbook org-exp org-html org-icalendar
  18387. org-id org-latex
  18388. org-publish org-remember org-table
  18389. org-timer org-xoxo)))
  18390. ;;;###autoload
  18391. (defun org-reload (&optional uncompiled)
  18392. "Reload all org lisp files.
  18393. With prefix arg UNCOMPILED, load the uncompiled versions."
  18394. (interactive "P")
  18395. (require 'loadhist)
  18396. (let* ((org-dir (org-find-library-dir "org"))
  18397. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18398. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18399. (remove-re (mapconcat 'identity
  18400. (mapcar (lambda (f) (concat "^" f "$"))
  18401. (list (if (featurep 'xemacs)
  18402. "org-colview"
  18403. "org-colview-xemacs")
  18404. "org" "org-loaddefs" "org-version"))
  18405. "\\|"))
  18406. (feats (delete-dups
  18407. (mapcar 'file-name-sans-extension
  18408. (mapcar 'file-name-nondirectory
  18409. (delq nil
  18410. (mapcar 'feature-file
  18411. features))))))
  18412. (lfeat (append
  18413. (sort
  18414. (setq feats
  18415. (delq nil (mapcar
  18416. (lambda (f)
  18417. (if (and (string-match feature-re f)
  18418. (not (string-match remove-re f)))
  18419. f nil))
  18420. feats)))
  18421. 'string-lessp)
  18422. (list "org-version" "org")))
  18423. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18424. load-uncore load-misses)
  18425. (setq load-misses
  18426. (delq 't
  18427. (mapcar (lambda (f)
  18428. (or (load (concat org-dir f) 'noerror nil nil 'mustsuffix)
  18429. (and (string= org-dir contrib-dir)
  18430. (load (concat contrib-dir f) 'noerror nil nil 'mustsuffix))
  18431. (and (load (concat (org-find-library-dir f) f) 'noerror nil nil 'mustsuffix)
  18432. (add-to-list 'load-uncore f 'append)
  18433. 't)
  18434. f))
  18435. lfeat)))
  18436. (if load-uncore
  18437. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18438. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18439. (if load-misses
  18440. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18441. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18442. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18443. ;;;###autoload
  18444. (defun org-customize ()
  18445. "Call the customize function with org as argument."
  18446. (interactive)
  18447. (org-load-modules-maybe)
  18448. (org-require-autoloaded-modules)
  18449. (customize-browse 'org))
  18450. (defun org-create-customize-menu ()
  18451. "Create a full customization menu for Org-mode, insert it into the menu."
  18452. (interactive)
  18453. (org-load-modules-maybe)
  18454. (org-require-autoloaded-modules)
  18455. (if (fboundp 'customize-menu-create)
  18456. (progn
  18457. (easy-menu-change
  18458. '("Org") "Customize"
  18459. `(["Browse Org group" org-customize t]
  18460. "--"
  18461. ,(customize-menu-create 'org)
  18462. ["Set" Custom-set t]
  18463. ["Save" Custom-save t]
  18464. ["Reset to Current" Custom-reset-current t]
  18465. ["Reset to Saved" Custom-reset-saved t]
  18466. ["Reset to Standard Settings" Custom-reset-standard t]))
  18467. (message "\"Org\"-menu now contains full customization menu"))
  18468. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18469. ;;;; Miscellaneous stuff
  18470. ;;; Generally useful functions
  18471. (defun org-get-at-bol (property)
  18472. "Get text property PROPERTY at beginning of line."
  18473. (get-text-property (point-at-bol) property))
  18474. (defun org-find-text-property-in-string (prop s)
  18475. "Return the first non-nil value of property PROP in string S."
  18476. (or (get-text-property 0 prop s)
  18477. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18478. prop s)))
  18479. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18480. "Display the given MESSAGE as a warning."
  18481. (if (fboundp 'display-warning)
  18482. (display-warning 'org message
  18483. (if (featurep 'xemacs) 'warning :warning))
  18484. (let ((buf (get-buffer-create "*Org warnings*")))
  18485. (with-current-buffer buf
  18486. (goto-char (point-max))
  18487. (insert "Warning (Org): " message)
  18488. (unless (bolp)
  18489. (newline)))
  18490. (display-buffer buf)
  18491. (sit-for 0))))
  18492. (defun org-eval (form)
  18493. "Eval FORM and return result."
  18494. (condition-case error
  18495. (eval form)
  18496. (error (format "%%![Error: %s]" error))))
  18497. (defun org-in-clocktable-p ()
  18498. "Check if the cursor is in a clocktable."
  18499. (let ((pos (point)) start)
  18500. (save-excursion
  18501. (end-of-line 1)
  18502. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18503. (setq start (match-beginning 0))
  18504. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18505. (>= (match-end 0) pos)
  18506. start))))
  18507. (defun org-in-commented-line ()
  18508. "Is point in a line starting with `#'?"
  18509. (equal (char-after (point-at-bol)) ?#))
  18510. (defun org-in-indented-comment-line ()
  18511. "Is point in a line starting with `#' after some white space?"
  18512. (save-excursion
  18513. (save-match-data
  18514. (goto-char (point-at-bol))
  18515. (looking-at "[ \t]*#"))))
  18516. (defun org-in-verbatim-emphasis ()
  18517. (save-match-data
  18518. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18519. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18520. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18521. (if (and marker (marker-buffer marker)
  18522. (buffer-live-p (marker-buffer marker)))
  18523. (progn
  18524. (org-pop-to-buffer-same-window (marker-buffer marker))
  18525. (if (or (> marker (point-max)) (< marker (point-min)))
  18526. (widen))
  18527. (goto-char marker)
  18528. (org-show-context 'org-goto))
  18529. (if bookmark
  18530. (bookmark-jump bookmark)
  18531. (error "Cannot find location"))))
  18532. (defun org-quote-csv-field (s)
  18533. "Quote field for inclusion in CSV material."
  18534. (if (string-match "[\",]" s)
  18535. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18536. s))
  18537. (defun org-force-self-insert (N)
  18538. "Needed to enforce self-insert under remapping."
  18539. (interactive "p")
  18540. (self-insert-command N))
  18541. (defun org-string-width (s)
  18542. "Compute width of string, ignoring invisible characters.
  18543. This ignores character with invisibility property `org-link', and also
  18544. characters with property `org-cwidth', because these will become invisible
  18545. upon the next fontification round."
  18546. (let (b l)
  18547. (when (or (eq t buffer-invisibility-spec)
  18548. (assq 'org-link buffer-invisibility-spec))
  18549. (while (setq b (text-property-any 0 (length s)
  18550. 'invisible 'org-link s))
  18551. (setq s (concat (substring s 0 b)
  18552. (substring s (or (next-single-property-change
  18553. b 'invisible s) (length s)))))))
  18554. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18555. (setq s (concat (substring s 0 b)
  18556. (substring s (or (next-single-property-change
  18557. b 'org-cwidth s) (length s))))))
  18558. (setq l (string-width s) b -1)
  18559. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18560. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18561. l))
  18562. (defun org-shorten-string (s maxlength)
  18563. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18564. If the string is shorter or has length MAXLENGTH, just return the
  18565. original string. If it is longer, the functions finds a space in the
  18566. string, breaks this string off at that locations and adds three dots
  18567. as ellipsis. Including the ellipsis, the string will not be longer
  18568. than MAXLENGTH. If finding a good breaking point in the string does
  18569. not work, the string is just chopped off in the middle of a word
  18570. if necessary."
  18571. (if (<= (length s) maxlength)
  18572. s
  18573. (let* ((n (max (- maxlength 4) 1))
  18574. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18575. (if (string-match re s)
  18576. (concat (match-string 1 s) "...")
  18577. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18578. (defun org-get-indentation (&optional line)
  18579. "Get the indentation of the current line, interpreting tabs.
  18580. When LINE is given, assume it represents a line and compute its indentation."
  18581. (if line
  18582. (if (string-match "^ *" (org-remove-tabs line))
  18583. (match-end 0))
  18584. (save-excursion
  18585. (beginning-of-line 1)
  18586. (skip-chars-forward " \t")
  18587. (current-column))))
  18588. (defun org-get-string-indentation (s)
  18589. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18590. (let ((n -1) (i 0) (w tab-width) c)
  18591. (catch 'exit
  18592. (while (< (setq n (1+ n)) (length s))
  18593. (setq c (aref s n))
  18594. (cond ((= c ?\ ) (setq i (1+ i)))
  18595. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18596. (t (throw 'exit t)))))
  18597. i))
  18598. (defun org-remove-tabs (s &optional width)
  18599. "Replace tabulators in S with spaces.
  18600. Assumes that s is a single line, starting in column 0."
  18601. (setq width (or width tab-width))
  18602. (while (string-match "\t" s)
  18603. (setq s (replace-match
  18604. (make-string
  18605. (- (* width (/ (+ (match-beginning 0) width) width))
  18606. (match-beginning 0)) ?\ )
  18607. t t s)))
  18608. s)
  18609. (defun org-fix-indentation (line ind)
  18610. "Fix indentation in LINE.
  18611. IND is a cons cell with target and minimum indentation.
  18612. If the current indentation in LINE is smaller than the minimum,
  18613. leave it alone. If it is larger than ind, set it to the target."
  18614. (let* ((l (org-remove-tabs line))
  18615. (i (org-get-indentation l))
  18616. (i1 (car ind)) (i2 (cdr ind)))
  18617. (if (>= i i2) (setq l (substring line i2)))
  18618. (if (> i1 0)
  18619. (concat (make-string i1 ?\ ) l)
  18620. l)))
  18621. (defun org-remove-indentation (code &optional n)
  18622. "Remove the maximum common indentation from the lines in CODE.
  18623. N may optionally be the number of spaces to remove."
  18624. (with-temp-buffer
  18625. (insert code)
  18626. (org-do-remove-indentation n)
  18627. (buffer-string)))
  18628. (defun org-do-remove-indentation (&optional n)
  18629. "Remove the maximum common indentation from the buffer."
  18630. (untabify (point-min) (point-max))
  18631. (let ((min 10000) re)
  18632. (if n
  18633. (setq min n)
  18634. (goto-char (point-min))
  18635. (while (re-search-forward "^ *[^ \n]" nil t)
  18636. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18637. (unless (or (= min 0) (= min 10000))
  18638. (setq re (format "^ \\{%d\\}" min))
  18639. (goto-char (point-min))
  18640. (while (re-search-forward re nil t)
  18641. (replace-match "")
  18642. (end-of-line 1))
  18643. min)))
  18644. (defun org-fill-template (template alist)
  18645. "Find each %key of ALIST in TEMPLATE and replace it."
  18646. (let ((case-fold-search nil)
  18647. entry key value)
  18648. (setq alist (sort (copy-sequence alist)
  18649. (lambda (a b) (< (length (car a)) (length (car b))))))
  18650. (while (setq entry (pop alist))
  18651. (setq template
  18652. (replace-regexp-in-string
  18653. (concat "%" (regexp-quote (car entry)))
  18654. (or (cdr entry) "") template t t)))
  18655. template))
  18656. (defun org-base-buffer (buffer)
  18657. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18658. (if (not buffer)
  18659. buffer
  18660. (or (buffer-base-buffer buffer)
  18661. buffer)))
  18662. (defun org-trim (s)
  18663. "Remove whitespace at beginning and end of string."
  18664. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18665. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18666. s)
  18667. (defun org-wrap (string &optional width lines)
  18668. "Wrap string to either a number of lines, or a width in characters.
  18669. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18670. that costs. If there is a word longer than WIDTH, the text is actually
  18671. wrapped to the length of that word.
  18672. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18673. many lines, whatever width that takes.
  18674. The return value is a list of lines, without newlines at the end."
  18675. (let* ((words (org-split-string string "[ \t\n]+"))
  18676. (maxword (apply 'max (mapcar 'org-string-width words)))
  18677. w ll)
  18678. (cond (width
  18679. (org-do-wrap words (max maxword width)))
  18680. (lines
  18681. (setq w maxword)
  18682. (setq ll (org-do-wrap words maxword))
  18683. (if (<= (length ll) lines)
  18684. ll
  18685. (setq ll words)
  18686. (while (> (length ll) lines)
  18687. (setq w (1+ w))
  18688. (setq ll (org-do-wrap words w)))
  18689. ll))
  18690. (t (error "Cannot wrap this")))))
  18691. (defun org-do-wrap (words width)
  18692. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18693. (let (lines line)
  18694. (while words
  18695. (setq line (pop words))
  18696. (while (and words (< (+ (length line) (length (car words))) width))
  18697. (setq line (concat line " " (pop words))))
  18698. (setq lines (push line lines)))
  18699. (nreverse lines)))
  18700. (defun org-split-string (string &optional separators)
  18701. "Splits STRING into substrings at SEPARATORS.
  18702. No empty strings are returned if there are matches at the beginning
  18703. and end of string."
  18704. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18705. (start 0)
  18706. notfirst
  18707. (list nil))
  18708. (while (and (string-match rexp string
  18709. (if (and notfirst
  18710. (= start (match-beginning 0))
  18711. (< start (length string)))
  18712. (1+ start) start))
  18713. (< (match-beginning 0) (length string)))
  18714. (setq notfirst t)
  18715. (or (eq (match-beginning 0) 0)
  18716. (and (eq (match-beginning 0) (match-end 0))
  18717. (eq (match-beginning 0) start))
  18718. (setq list
  18719. (cons (substring string start (match-beginning 0))
  18720. list)))
  18721. (setq start (match-end 0)))
  18722. (or (eq start (length string))
  18723. (setq list
  18724. (cons (substring string start)
  18725. list)))
  18726. (nreverse list)))
  18727. (defun org-quote-vert (s)
  18728. "Replace \"|\" with \"\\vert\"."
  18729. (while (string-match "|" s)
  18730. (setq s (replace-match "\\vert" t t s)))
  18731. s)
  18732. (defun org-uuidgen-p (s)
  18733. "Is S an ID created by UUIDGEN?"
  18734. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18735. (defun org-in-src-block-p nil
  18736. "Whether point is in a code source block."
  18737. (let (ov)
  18738. (when (setq ov (overlays-at (point)))
  18739. (memq 'org-block-background
  18740. (overlay-properties
  18741. (car ov))))))
  18742. (defun org-context ()
  18743. "Return a list of contexts of the current cursor position.
  18744. If several contexts apply, all are returned.
  18745. Each context entry is a list with a symbol naming the context, and
  18746. two positions indicating start and end of the context. Possible
  18747. contexts are:
  18748. :headline anywhere in a headline
  18749. :headline-stars on the leading stars in a headline
  18750. :todo-keyword on a TODO keyword (including DONE) in a headline
  18751. :tags on the TAGS in a headline
  18752. :priority on the priority cookie in a headline
  18753. :item on the first line of a plain list item
  18754. :item-bullet on the bullet/number of a plain list item
  18755. :checkbox on the checkbox in a plain list item
  18756. :table in an org-mode table
  18757. :table-special on a special filed in a table
  18758. :table-table in a table.el table
  18759. :clocktable in a clocktable
  18760. :src-block in a source block
  18761. :link on a hyperlink
  18762. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18763. :target on a <<target>>
  18764. :radio-target on a <<<radio-target>>>
  18765. :latex-fragment on a LaTeX fragment
  18766. :latex-preview on a LaTeX fragment with overlaid preview image
  18767. This function expects the position to be visible because it uses font-lock
  18768. faces as a help to recognize the following contexts: :table-special, :link,
  18769. and :keyword."
  18770. (let* ((f (get-text-property (point) 'face))
  18771. (faces (if (listp f) f (list f)))
  18772. (case-fold-search t)
  18773. (p (point)) clist o)
  18774. ;; First the large context
  18775. (cond
  18776. ((org-at-heading-p t)
  18777. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18778. (when (progn
  18779. (beginning-of-line 1)
  18780. (looking-at org-todo-line-tags-regexp))
  18781. (push (org-point-in-group p 1 :headline-stars) clist)
  18782. (push (org-point-in-group p 2 :todo-keyword) clist)
  18783. (push (org-point-in-group p 4 :tags) clist))
  18784. (goto-char p)
  18785. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18786. (if (looking-at "\\[#[A-Z0-9]\\]")
  18787. (push (org-point-in-group p 0 :priority) clist)))
  18788. ((org-at-item-p)
  18789. (push (org-point-in-group p 2 :item-bullet) clist)
  18790. (push (list :item (point-at-bol)
  18791. (save-excursion (org-end-of-item) (point)))
  18792. clist)
  18793. (and (org-at-item-checkbox-p)
  18794. (push (org-point-in-group p 0 :checkbox) clist)))
  18795. ((org-at-table-p)
  18796. (push (list :table (org-table-begin) (org-table-end)) clist)
  18797. (if (memq 'org-formula faces)
  18798. (push (list :table-special
  18799. (previous-single-property-change p 'face)
  18800. (next-single-property-change p 'face)) clist)))
  18801. ((org-at-table-p 'any)
  18802. (push (list :table-table) clist)))
  18803. (goto-char p)
  18804. (let ((case-fold-search t))
  18805. ;; New the "medium" contexts: clocktables, source blocks
  18806. (cond ((org-in-clocktable-p)
  18807. (push (list :clocktable
  18808. (and (or (looking-at "#\\+BEGIN: clocktable")
  18809. (search-backward "#+BEGIN: clocktable" nil t))
  18810. (match-beginning 0))
  18811. (and (re-search-forward "#\\+END:?" nil t)
  18812. (match-end 0))) clist))
  18813. ((org-in-src-block-p)
  18814. (push (list :src-block
  18815. (and (or (looking-at "#\\+BEGIN_SRC")
  18816. (search-backward "#+BEGIN_SRC" nil t))
  18817. (match-beginning 0))
  18818. (and (search-forward "#+END_SRC" nil t)
  18819. (match-beginning 0))) clist))))
  18820. (goto-char p)
  18821. ;; Now the small context
  18822. (cond
  18823. ((org-at-timestamp-p)
  18824. (push (org-point-in-group p 0 :timestamp) clist))
  18825. ((memq 'org-link faces)
  18826. (push (list :link
  18827. (previous-single-property-change p 'face)
  18828. (next-single-property-change p 'face)) clist))
  18829. ((memq 'org-special-keyword faces)
  18830. (push (list :keyword
  18831. (previous-single-property-change p 'face)
  18832. (next-single-property-change p 'face)) clist))
  18833. ((org-at-target-p)
  18834. (push (org-point-in-group p 0 :target) clist)
  18835. (goto-char (1- (match-beginning 0)))
  18836. (if (looking-at org-radio-target-regexp)
  18837. (push (org-point-in-group p 0 :radio-target) clist))
  18838. (goto-char p))
  18839. ((setq o (car (delq nil
  18840. (mapcar
  18841. (lambda (x)
  18842. (if (memq x org-latex-fragment-image-overlays) x))
  18843. (overlays-at (point))))))
  18844. (push (list :latex-fragment
  18845. (overlay-start o) (overlay-end o)) clist)
  18846. (push (list :latex-preview
  18847. (overlay-start o) (overlay-end o)) clist))
  18848. ((org-inside-LaTeX-fragment-p)
  18849. ;; FIXME: positions wrong.
  18850. (push (list :latex-fragment (point) (point)) clist)))
  18851. (setq clist (nreverse (delq nil clist)))
  18852. clist))
  18853. ;; FIXME: Compare with at-regexp-p Do we need both?
  18854. (defun org-in-regexp (re &optional nlines visually)
  18855. "Check if point is inside a match of regexp.
  18856. Normally only the current line is checked, but you can include NLINES extra
  18857. lines both before and after point into the search.
  18858. If VISUALLY is set, require that the cursor is not after the match but
  18859. really on, so that the block visually is on the match."
  18860. (catch 'exit
  18861. (let ((pos (point))
  18862. (eol (point-at-eol (+ 1 (or nlines 0))))
  18863. (inc (if visually 1 0)))
  18864. (save-excursion
  18865. (beginning-of-line (- 1 (or nlines 0)))
  18866. (while (re-search-forward re eol t)
  18867. (if (and (<= (match-beginning 0) pos)
  18868. (>= (+ inc (match-end 0)) pos))
  18869. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18870. (defun org-at-regexp-p (regexp)
  18871. "Is point inside a match of REGEXP in the current line?"
  18872. (catch 'exit
  18873. (save-excursion
  18874. (let ((pos (point)) (end (point-at-eol)))
  18875. (beginning-of-line 1)
  18876. (while (re-search-forward regexp end t)
  18877. (if (and (<= (match-beginning 0) pos)
  18878. (>= (match-end 0) pos))
  18879. (throw 'exit t)))
  18880. nil))))
  18881. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18882. "Non-nil when point is between matches of START-RE and END-RE.
  18883. Also return a non-nil value when point is on one of the matches.
  18884. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18885. buffer positions. Default values are the positions of headlines
  18886. surrounding the point.
  18887. The functions returns a cons cell whose car (resp. cdr) is the
  18888. position before START-RE (resp. after END-RE)."
  18889. (save-match-data
  18890. (let ((pos (point))
  18891. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18892. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18893. beg end)
  18894. (save-excursion
  18895. ;; Point is on a block when on START-RE or if START-RE can be
  18896. ;; found before it...
  18897. (and (or (org-at-regexp-p start-re)
  18898. (re-search-backward start-re limit-up t))
  18899. (setq beg (match-beginning 0))
  18900. ;; ... and END-RE after it...
  18901. (goto-char (match-end 0))
  18902. (re-search-forward end-re limit-down t)
  18903. (> (setq end (match-end 0)) pos)
  18904. ;; ... without another START-RE in-between.
  18905. (goto-char (match-beginning 0))
  18906. (not (re-search-backward start-re (1+ beg) t))
  18907. ;; Return value.
  18908. (cons beg end))))))
  18909. (defun org-in-block-p (names)
  18910. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18911. NAMES is a list of strings containing names of blocks.
  18912. Return first block name matched, or nil. Beware that in case of
  18913. nested blocks, the returned name may not belong to the closest
  18914. block from point."
  18915. (save-match-data
  18916. (catch 'exit
  18917. (let ((case-fold-search t)
  18918. (lim-up (save-excursion (outline-previous-heading)))
  18919. (lim-down (save-excursion (outline-next-heading))))
  18920. (mapc (lambda (name)
  18921. (let ((n (regexp-quote name)))
  18922. (when (org-between-regexps-p
  18923. (concat "^[ \t]*#\\+begin_" n)
  18924. (concat "^[ \t]*#\\+end_" n)
  18925. lim-up lim-down)
  18926. (throw 'exit n))))
  18927. names))
  18928. nil)))
  18929. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18930. "Call `multi-occur' with buffers for all agenda files."
  18931. (interactive "sOrg-files matching: \np")
  18932. (let* ((files (org-agenda-files))
  18933. (tnames (mapcar 'file-truename files))
  18934. (extra org-agenda-text-search-extra-files)
  18935. f)
  18936. (when (eq (car extra) 'agenda-archives)
  18937. (setq extra (cdr extra))
  18938. (setq files (org-add-archive-files files)))
  18939. (while (setq f (pop extra))
  18940. (unless (member (file-truename f) tnames)
  18941. (add-to-list 'files f 'append)
  18942. (add-to-list 'tnames (file-truename f) 'append)))
  18943. (multi-occur
  18944. (mapcar (lambda (x)
  18945. (with-current-buffer
  18946. (or (get-file-buffer x) (find-file-noselect x))
  18947. (widen)
  18948. (current-buffer)))
  18949. files)
  18950. regexp)))
  18951. (if (boundp 'occur-mode-find-occurrence-hook)
  18952. ;; Emacs 23
  18953. (add-hook 'occur-mode-find-occurrence-hook
  18954. (lambda ()
  18955. (when (derived-mode-p 'org-mode)
  18956. (org-reveal))))
  18957. ;; Emacs 22
  18958. (defadvice occur-mode-goto-occurrence
  18959. (after org-occur-reveal activate)
  18960. (and (derived-mode-p 'org-mode) (org-reveal)))
  18961. (defadvice occur-mode-goto-occurrence-other-window
  18962. (after org-occur-reveal activate)
  18963. (and (derived-mode-p 'org-mode) (org-reveal)))
  18964. (defadvice occur-mode-display-occurrence
  18965. (after org-occur-reveal activate)
  18966. (when (derived-mode-p 'org-mode)
  18967. (let ((pos (occur-mode-find-occurrence)))
  18968. (with-current-buffer (marker-buffer pos)
  18969. (save-excursion
  18970. (goto-char pos)
  18971. (org-reveal)))))))
  18972. (defun org-occur-link-in-agenda-files ()
  18973. "Create a link and search for it in the agendas.
  18974. The link is not stored in `org-stored-links', it is just created
  18975. for the search purpose."
  18976. (interactive)
  18977. (let ((link (condition-case nil
  18978. (org-store-link nil)
  18979. (error "Unable to create a link to here"))))
  18980. (org-occur-in-agenda-files (regexp-quote link))))
  18981. (defun org-uniquify (list)
  18982. "Remove duplicate elements from LIST."
  18983. (let (res)
  18984. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18985. res))
  18986. (defun org-delete-all (elts list)
  18987. "Remove all elements in ELTS from LIST."
  18988. (while elts
  18989. (setq list (delete (pop elts) list)))
  18990. list)
  18991. (defun org-count (cl-item cl-seq)
  18992. "Count the number of occurrences of ITEM in SEQ.
  18993. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18994. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18995. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18996. (while (< cl-start cl-end)
  18997. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18998. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18999. (setq cl-start (1+ cl-start)))
  19000. cl-count))
  19001. (defun org-remove-if (predicate seq)
  19002. "Remove everything from SEQ that fulfills PREDICATE."
  19003. (let (res e)
  19004. (while seq
  19005. (setq e (pop seq))
  19006. (if (not (funcall predicate e)) (push e res)))
  19007. (nreverse res)))
  19008. (defun org-remove-if-not (predicate seq)
  19009. "Remove everything from SEQ that does not fulfill PREDICATE."
  19010. (let (res e)
  19011. (while seq
  19012. (setq e (pop seq))
  19013. (if (funcall predicate e) (push e res)))
  19014. (nreverse res)))
  19015. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19016. "Reduce two-argument FUNCTION across SEQ.
  19017. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19018. \":initial-value\" removed."
  19019. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19020. (cadr (memq :initial-value cl-keys)))
  19021. (cl-seq (pop cl-seq))
  19022. (t (funcall cl-func)))))
  19023. (while cl-seq
  19024. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19025. cl-accum))
  19026. (defun org-back-over-empty-lines ()
  19027. "Move backwards over whitespace, to the beginning of the first empty line.
  19028. Returns the number of empty lines passed."
  19029. (let ((pos (point)))
  19030. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19031. (skip-chars-backward " \t\n\r")
  19032. (unless (eobp)
  19033. (forward-line -1)))
  19034. (beginning-of-line 2)
  19035. (goto-char (min (point) pos))
  19036. (count-lines (point) pos)))
  19037. (defun org-skip-whitespace ()
  19038. (skip-chars-forward " \t\n\r"))
  19039. (defun org-point-in-group (point group &optional context)
  19040. "Check if POINT is in match-group GROUP.
  19041. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19042. match. If the match group does not exist or point is not inside it,
  19043. return nil."
  19044. (and (match-beginning group)
  19045. (>= point (match-beginning group))
  19046. (<= point (match-end group))
  19047. (if context
  19048. (list context (match-beginning group) (match-end group))
  19049. t)))
  19050. (defun org-switch-to-buffer-other-window (&rest args)
  19051. "Switch to buffer in a second window on the current frame.
  19052. In particular, do not allow pop-up frames.
  19053. Returns the newly created buffer."
  19054. (let (pop-up-frames special-display-buffer-names special-display-regexps
  19055. special-display-function)
  19056. (apply 'switch-to-buffer-other-window args)))
  19057. (defun org-combine-plists (&rest plists)
  19058. "Create a single property list from all plists in PLISTS.
  19059. The process starts by copying the first list, and then setting properties
  19060. from the other lists. Settings in the last list are the most significant
  19061. ones and overrule settings in the other lists."
  19062. (let ((rtn (copy-sequence (pop plists)))
  19063. p v ls)
  19064. (while plists
  19065. (setq ls (pop plists))
  19066. (while ls
  19067. (setq p (pop ls) v (pop ls))
  19068. (setq rtn (plist-put rtn p v))))
  19069. rtn))
  19070. (defun org-replace-escapes (string table)
  19071. "Replace %-escapes in STRING with values in TABLE.
  19072. TABLE is an association list with keys like \"%a\" and string values.
  19073. The sequences in STRING may contain normal field width and padding information,
  19074. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19075. so values can contain further %-escapes if they are define later in TABLE."
  19076. (let ((tbl (copy-alist table))
  19077. (case-fold-search nil)
  19078. (pchg 0)
  19079. e re rpl)
  19080. (while (setq e (pop tbl))
  19081. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19082. (when (and (cdr e) (string-match re (cdr e)))
  19083. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19084. (safe "SREF"))
  19085. (add-text-properties 0 3 (list 'sref sref) safe)
  19086. (setcdr e (replace-match safe t t (cdr e)))))
  19087. (while (string-match re string)
  19088. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19089. (cdr e)))
  19090. (setq string (replace-match rpl t t string))))
  19091. (while (setq pchg (next-property-change pchg string))
  19092. (let ((sref (get-text-property pchg 'sref string)))
  19093. (when (and sref (string-match "SREF" string pchg))
  19094. (setq string (replace-match sref t t string)))))
  19095. string))
  19096. (defun org-sublist (list start end)
  19097. "Return a section of LIST, from START to END.
  19098. Counting starts at 1."
  19099. (let (rtn (c start))
  19100. (setq list (nthcdr (1- start) list))
  19101. (while (and list (<= c end))
  19102. (push (pop list) rtn)
  19103. (setq c (1+ c)))
  19104. (nreverse rtn)))
  19105. (defun org-find-base-buffer-visiting (file)
  19106. "Like `find-buffer-visiting' but always return the base buffer and
  19107. not an indirect buffer."
  19108. (let ((buf (or (get-file-buffer file)
  19109. (find-buffer-visiting file))))
  19110. (if buf
  19111. (or (buffer-base-buffer buf) buf)
  19112. nil)))
  19113. (defun org-image-file-name-regexp (&optional extensions)
  19114. "Return regexp matching the file names of images.
  19115. If EXTENSIONS is given, only match these."
  19116. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19117. (image-file-name-regexp)
  19118. (let ((image-file-name-extensions
  19119. (or extensions
  19120. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19121. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19122. (concat "\\."
  19123. (regexp-opt (nconc (mapcar 'upcase
  19124. image-file-name-extensions)
  19125. image-file-name-extensions)
  19126. t)
  19127. "\\'"))))
  19128. (defun org-file-image-p (file &optional extensions)
  19129. "Return non-nil if FILE is an image."
  19130. (save-match-data
  19131. (string-match (org-image-file-name-regexp extensions) file)))
  19132. (defun org-get-cursor-date ()
  19133. "Return the date at cursor in as a time.
  19134. This works in the calendar and in the agenda, anywhere else it just
  19135. returns the current time."
  19136. (let (date day defd)
  19137. (cond
  19138. ((eq major-mode 'calendar-mode)
  19139. (setq date (calendar-cursor-to-date)
  19140. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19141. ((eq major-mode 'org-agenda-mode)
  19142. (setq day (get-text-property (point) 'day))
  19143. (if day
  19144. (setq date (calendar-gregorian-from-absolute day)
  19145. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19146. (nth 2 date))))))
  19147. (or defd (current-time))))
  19148. (defun org-mark-subtree (&optional up)
  19149. "Mark the current subtree.
  19150. This puts point at the start of the current subtree, and mark at
  19151. the end. If a numeric prefix UP is given, move up into the
  19152. hierarchy of headlines by UP levels before marking the subtree."
  19153. (interactive "P")
  19154. (org-with-limited-levels
  19155. (cond ((org-at-heading-p) (beginning-of-line))
  19156. ((org-before-first-heading-p) (error "Not in a subtree"))
  19157. (t (outline-previous-visible-heading 1))))
  19158. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19159. (if (org-called-interactively-p 'any)
  19160. (call-interactively 'org-mark-element)
  19161. (org-mark-element)))
  19162. ;;; Macros
  19163. ;; Macros are expanded with `org-macro-replace-all', which relies
  19164. ;; internally on `org-macro-expand'.
  19165. ;; Default templates for expansion are stored in the buffer-local
  19166. ;; variable `org-macro-templates'. This variable is updated by
  19167. ;; `org-macro-initialize-templates'.
  19168. ;; Along with macros defined through #+MACRO: keyword, default
  19169. ;; templates include the following hard-coded macros:
  19170. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19171. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19172. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19173. ;; {{{title}}} will also be provided.
  19174. (defvar org-macro-templates nil
  19175. "Alist containing all macro templates in current buffer.
  19176. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19177. stands for macro's name and template for its replacement value,
  19178. both as strings. This is an internal variable. Do not set it
  19179. directly, use instead:
  19180. #+MACRO: name template")
  19181. (make-variable-buffer-local 'org-macro-templates)
  19182. (defun org-macro-expand (macro templates)
  19183. "Return expanded MACRO, as a string.
  19184. MACRO is an object, obtained, for example, with
  19185. `org-element-context'. TEMPLATES is an alist of templates used
  19186. for expansion. See `org-macro-templates' for a buffer-local
  19187. default value. Return nil if no template was found."
  19188. (let ((template
  19189. ;; Macro names are case-insensitive.
  19190. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19191. (when template
  19192. (let ((value (replace-regexp-in-string
  19193. "\\$[0-9]+"
  19194. (lambda (arg)
  19195. (or (nth (1- (string-to-number (substring arg 1)))
  19196. (org-element-property :args macro))
  19197. ;; No argument provided: remove
  19198. ;; place-holder.
  19199. ""))
  19200. template)))
  19201. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19202. (when (string-match "\\`(eval\\>" value)
  19203. (setq value (eval (read value))))
  19204. ;; Return string.
  19205. (format "%s" (or value ""))))))
  19206. (defun org-macro-replace-all (templates)
  19207. "Replace all macros in current buffer by their expansion.
  19208. TEMPLATES is an alist of templates used for expansion. See
  19209. `org-macro-templates' for a buffer-local default value."
  19210. (save-excursion
  19211. (goto-char (point-min))
  19212. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19213. (let ((object (org-element-context)))
  19214. (when (eq (org-element-type object) 'macro)
  19215. (let ((value (org-macro-expand object templates)))
  19216. (when value
  19217. (delete-region
  19218. (org-element-property :begin object)
  19219. ;; Preserve white spaces after the macro.
  19220. (progn (goto-char (org-element-property :end object))
  19221. (skip-chars-backward " \t")
  19222. (point)))
  19223. ;; Leave point before replacement in case of recursive
  19224. ;; expansions.
  19225. (save-excursion (insert value)))))))))
  19226. (defun org-macro-initialize-templates ()
  19227. "Collect macro templates defined in current buffer.
  19228. Templates are stored in buffer-local variable
  19229. `org-macro-templates'. In addition to buffer-defined macros, the
  19230. function installs the following ones: \"property\", \"date\",
  19231. \"time\". and, if appropriate, \"input-file\" and
  19232. \"modification-time\"."
  19233. (let ((case-fold-search t)
  19234. (set-template
  19235. (lambda (cell)
  19236. ;; Add CELL to `org-macro-templates' if there's no
  19237. ;; association matching its name already. Otherwise,
  19238. ;; replace old association with the new one in that
  19239. ;; variable.
  19240. (let ((old-template (assoc (car cell) org-macro-templates)))
  19241. (if old-template (setcdr old-template (cdr cell))
  19242. (push cell org-macro-templates))))))
  19243. ;; Install buffer-local macros.
  19244. (org-with-wide-buffer
  19245. (goto-char (point-min))
  19246. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19247. (let ((element (org-element-at-point)))
  19248. (when (eq (org-element-type element) 'keyword)
  19249. (let ((value (org-element-property :value element)))
  19250. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19251. (funcall set-template
  19252. (cons (match-string 1 value)
  19253. (or (match-string 2 value) "")))))))))
  19254. ;; Install hard-coded macros.
  19255. (mapc (lambda (cell) (funcall set-template cell))
  19256. (list
  19257. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19258. (cons "time" "(eval (format-time-string \"$1\"))")))
  19259. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19260. (when (and visited-file (file-exists-p visited-file))
  19261. (mapc (lambda (cell) (funcall set-template cell))
  19262. (list
  19263. (cons "input-file" (file-name-nondirectory visited-file))
  19264. (cons "modification-time"
  19265. (format "(eval (format-time-string \"$1\" '%s))"
  19266. (prin1-to-string
  19267. (nth 5 (file-attributes visited-file)))))))))))
  19268. ;;; Indentation
  19269. (defun org-indent-line ()
  19270. "Indent line depending on context."
  19271. (interactive)
  19272. (let* ((pos (point))
  19273. (itemp (org-at-item-p))
  19274. (case-fold-search t)
  19275. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19276. (inline-task-p (and (featurep 'org-inlinetask)
  19277. (org-inlinetask-in-task-p)))
  19278. (inline-re (and inline-task-p
  19279. (org-inlinetask-outline-regexp)))
  19280. column)
  19281. (if (and orgstruct-is-++ (eq pos (point)))
  19282. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19283. (indent-according-to-mode))
  19284. (beginning-of-line 1)
  19285. (cond
  19286. ;; Headings
  19287. ((looking-at org-outline-regexp) (setq column 0))
  19288. ;; Included files
  19289. ((looking-at "#\\+include:") (setq column 0))
  19290. ;; Footnote definition
  19291. ((looking-at org-footnote-definition-re) (setq column 0))
  19292. ;; Literal examples
  19293. ((looking-at "[ \t]*:\\( \\|$\\)")
  19294. (setq column (org-get-indentation))) ; do nothing
  19295. ;; Lists
  19296. ((ignore-errors (goto-char (org-in-item-p)))
  19297. (setq column (if itemp
  19298. (org-get-indentation)
  19299. (org-list-item-body-column (point))))
  19300. (goto-char pos))
  19301. ;; Drawers
  19302. ((and (looking-at "[ \t]*:END:")
  19303. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19304. (save-excursion
  19305. (goto-char (1- (match-beginning 1)))
  19306. (setq column (current-column))))
  19307. ;; Special blocks
  19308. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19309. (save-excursion
  19310. (re-search-backward
  19311. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19312. (setq column (org-get-indentation (match-string 0))))
  19313. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19314. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19315. (save-excursion
  19316. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19317. (setq column
  19318. (cond ((equal (downcase (match-string 1)) "src")
  19319. ;; src blocks: let `org-edit-src-exit' handle them
  19320. (org-get-indentation))
  19321. ((equal (downcase (match-string 1)) "example")
  19322. (max (org-get-indentation)
  19323. (org-get-indentation (match-string 0))))
  19324. (t
  19325. (org-get-indentation (match-string 0))))))
  19326. ;; This line has nothing special, look at the previous relevant
  19327. ;; line to compute indentation
  19328. (t
  19329. (beginning-of-line 0)
  19330. (while (and (not (bobp))
  19331. (not (looking-at org-drawer-regexp))
  19332. ;; When point started in an inline task, do not move
  19333. ;; above task starting line.
  19334. (not (and inline-task-p (looking-at inline-re)))
  19335. ;; Skip drawers, blocks, empty lines, verbatim,
  19336. ;; comments, tables, footnotes definitions, lists,
  19337. ;; inline tasks.
  19338. (or (and (looking-at "[ \t]*:END:")
  19339. (re-search-backward org-drawer-regexp nil t))
  19340. (and (looking-at "[ \t]*#\\+end_")
  19341. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19342. (looking-at "[ \t]*[\n:#|]")
  19343. (looking-at org-footnote-definition-re)
  19344. (and (ignore-errors (goto-char (org-in-item-p)))
  19345. (goto-char
  19346. (org-list-get-top-point (org-list-struct))))
  19347. (and (not inline-task-p)
  19348. (featurep 'org-inlinetask)
  19349. (org-inlinetask-in-task-p)
  19350. (or (org-inlinetask-goto-beginning) t))))
  19351. (beginning-of-line 0))
  19352. (cond
  19353. ;; There was an heading above.
  19354. ((looking-at "\\*+[ \t]+")
  19355. (if (not org-adapt-indentation)
  19356. (setq column 0)
  19357. (goto-char (match-end 0))
  19358. (setq column (current-column))))
  19359. ;; A drawer had started and is unfinished
  19360. ((looking-at org-drawer-regexp)
  19361. (goto-char (1- (match-beginning 1)))
  19362. (setq column (current-column)))
  19363. ;; Else, nothing noticeable found: get indentation and go on.
  19364. (t (setq column (org-get-indentation))))))
  19365. ;; Now apply indentation and move cursor accordingly
  19366. (goto-char pos)
  19367. (if (<= (current-column) (current-indentation))
  19368. (org-indent-line-to column)
  19369. (save-excursion (org-indent-line-to column)))
  19370. ;; Special polishing for properties, see `org-property-format'
  19371. (setq column (current-column))
  19372. (beginning-of-line 1)
  19373. (if (looking-at
  19374. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19375. (replace-match (concat (match-string 1)
  19376. (format org-property-format
  19377. (match-string 2) (match-string 3)))
  19378. t t))
  19379. (org-move-to-column column))))
  19380. (defun org-indent-drawer ()
  19381. "Indent the drawer at point."
  19382. (interactive)
  19383. (let ((p (point))
  19384. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19385. (match-end 0)))
  19386. (folded
  19387. (save-excursion
  19388. (end-of-line)
  19389. (when (overlays-at (point))
  19390. (member 'invisible (overlay-properties
  19391. (car (overlays-at (point)))))))))
  19392. (when folded (org-cycle))
  19393. (indent-for-tab-command)
  19394. (while (and (move-beginning-of-line 2) (< (point) e))
  19395. (indent-for-tab-command))
  19396. (goto-char p)
  19397. (when folded (org-cycle)))
  19398. (message "Drawer at point indented"))
  19399. (defun org-indent-block ()
  19400. "Indent the block at point."
  19401. (interactive)
  19402. (let ((p (point))
  19403. (case-fold-search t)
  19404. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19405. (match-end 0)))
  19406. (folded
  19407. (save-excursion
  19408. (end-of-line)
  19409. (when (overlays-at (point))
  19410. (member 'invisible (overlay-properties
  19411. (car (overlays-at (point)))))))))
  19412. (when folded (org-cycle))
  19413. (indent-for-tab-command)
  19414. (while (and (move-beginning-of-line 2) (< (point) e))
  19415. (indent-for-tab-command))
  19416. (goto-char p)
  19417. (when folded (org-cycle)))
  19418. (message "Block at point indented"))
  19419. (defun org-indent-region (start end)
  19420. "Indent region."
  19421. (interactive "r")
  19422. (save-excursion
  19423. (let ((line-end (org-current-line end)))
  19424. (goto-char start)
  19425. (while (< (org-current-line) line-end)
  19426. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19427. (t (call-interactively 'org-indent-line)))
  19428. (move-beginning-of-line 2)))))
  19429. ;;; Filling
  19430. ;; We use our own fill-paragraph and auto-fill functions.
  19431. ;; `org-fill-paragraph' relies on adaptive filling and context
  19432. ;; checking. Appropriate `fill-prefix' is computed with
  19433. ;; `org-adaptive-fill-function'.
  19434. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19435. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19436. ;; `org-adaptive-fill-function' value. Internally,
  19437. ;; `org-comment-line-break-function' breaks the line.
  19438. ;; `org-setup-filling' installs filling and auto-filling related
  19439. ;; variables during `org-mode' initialization.
  19440. (defun org-setup-filling ()
  19441. (interactive)
  19442. ;; Prevent auto-fill from inserting unwanted new items.
  19443. (when (boundp 'fill-nobreak-predicate)
  19444. (org-set-local
  19445. 'fill-nobreak-predicate
  19446. (org-uniquify
  19447. (append fill-nobreak-predicate
  19448. '(org-fill-paragraph-separate-nobreak-p
  19449. org-fill-line-break-nobreak-p)))))
  19450. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19451. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19452. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19453. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19454. (defvar org-element-paragraph-separate) ; org-element.el
  19455. (defun org-fill-paragraph-separate-nobreak-p ()
  19456. "Non-nil when a line break at point would insert a new item."
  19457. (looking-at (substring org-element-paragraph-separate 1)))
  19458. (defun org-fill-line-break-nobreak-p ()
  19459. "Non-nil when a line break at point would create an Org line break."
  19460. (save-excursion
  19461. (skip-chars-backward "[ \t]")
  19462. (skip-chars-backward "\\\\")
  19463. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19464. (declare-function message-in-body-p "message" ())
  19465. (defvar org-element--affiliated-re) ; From org-element.el
  19466. (defun org-adaptive-fill-function ()
  19467. "Compute a fill prefix for the current line.
  19468. Return fill prefix, as a string, or nil if current line isn't
  19469. meant to be filled."
  19470. (org-with-wide-buffer
  19471. (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
  19472. ;; FIXME: This is really the job of orgstruct++-mode
  19473. (let* ((p (line-beginning-position))
  19474. (element (save-excursion (beginning-of-line)
  19475. (org-element-at-point)))
  19476. (type (org-element-type element))
  19477. (post-affiliated
  19478. (save-excursion
  19479. (goto-char (org-element-property :begin element))
  19480. (while (looking-at org-element--affiliated-re) (forward-line))
  19481. (point))))
  19482. (unless (< p post-affiliated)
  19483. (case type
  19484. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19485. (footnote-definition "")
  19486. ((item plain-list)
  19487. (make-string (org-list-item-body-column post-affiliated) ? ))
  19488. (paragraph
  19489. ;; Fill prefix is usually the same as the current line,
  19490. ;; except if the paragraph is at the beginning of an item.
  19491. (let ((parent (org-element-property :parent element)))
  19492. (cond ((eq (org-element-type parent) 'item)
  19493. (make-string (org-list-item-body-column
  19494. (org-element-property :begin parent))
  19495. ? ))
  19496. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19497. (match-string 0))
  19498. (t ""))))
  19499. (comment-block
  19500. ;; Only fill contents if P is within block boundaries.
  19501. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19502. (forward-line)
  19503. (point)))
  19504. (cend (save-excursion
  19505. (goto-char (org-element-property :end element))
  19506. (skip-chars-backward " \r\t\n")
  19507. (line-beginning-position))))
  19508. (when (and (>= p cbeg) (< p cend))
  19509. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19510. (match-string 0)
  19511. ""))))))))))
  19512. (declare-function message-goto-body "message" ())
  19513. (defvar message-cite-prefix-regexp) ; From message.el
  19514. (defvar org-element-all-objects) ; From org-element.el
  19515. (defun org-fill-paragraph (&optional justify)
  19516. "Fill element at point, when applicable.
  19517. This function only applies to comment blocks, comments, example
  19518. blocks and paragraphs. Also, as a special case, re-align table
  19519. when point is at one.
  19520. If JUSTIFY is non-nil (interactively, with prefix argument),
  19521. justify as well. If `sentence-end-double-space' is non-nil, then
  19522. period followed by one space does not end a sentence, so don't
  19523. break a line there. The variable `fill-column' controls the
  19524. width for filling.
  19525. For convenience, when point is at a plain list, an item or
  19526. a footnote definition, try to fill the first paragraph within."
  19527. ;; Falls back on message-fill-paragraph when necessary
  19528. (interactive)
  19529. (if (and (derived-mode-p 'message-mode)
  19530. (or (not (message-in-body-p))
  19531. (save-excursion (move-beginning-of-line 1)
  19532. (looking-at message-cite-prefix-regexp))))
  19533. (let ((fill-paragraph-function
  19534. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19535. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19536. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19537. (paragraph-separate
  19538. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19539. (fill-paragraph nil))
  19540. (save-excursion
  19541. ;; Move to end of line in order to get the first paragraph
  19542. ;; within a plain list or a footnote definition.
  19543. (end-of-line)
  19544. (let ((element (org-element-at-point)))
  19545. ;; First check if point is in a blank line at the beginning of
  19546. ;; the buffer. In that case, ignore filling.
  19547. (if (< (point) (org-element-property :begin element)) t
  19548. (case (org-element-type element)
  19549. ;; Align Org tables, leave table.el tables as-is.
  19550. (table-row (org-table-align) t)
  19551. (table
  19552. (when (eq (org-element-property :type element) 'org)
  19553. (org-table-align))
  19554. t)
  19555. (paragraph
  19556. ;; Paragraphs may contain `line-break' type objects.
  19557. (let ((beg (max (point-min)
  19558. (org-element-property :contents-begin element)))
  19559. (end (min (point-max)
  19560. (org-element-property :contents-end element))))
  19561. ;; Do nothing if point is at an affiliated keyword.
  19562. (if (< (point) beg) t
  19563. (when (derived-mode-p 'message-mode)
  19564. ;; In `message-mode', do not fill following
  19565. ;; citation in current paragraph nor text before
  19566. ;; message body.
  19567. (let ((body-start (save-excursion (message-goto-body))))
  19568. (when body-start (setq beg (max body-start beg))))
  19569. (when (save-excursion
  19570. (re-search-forward
  19571. (concat "^" message-cite-prefix-regexp) end t))
  19572. (setq end (match-beginning 0))))
  19573. ;; Fill paragraph, taking line breaks into
  19574. ;; consideration. For that, slice the paragraph
  19575. ;; using line breaks as separators, and fill the
  19576. ;; parts in reverse order to avoid messing with
  19577. ;; markers.
  19578. (save-excursion
  19579. (goto-char end)
  19580. (mapc
  19581. (lambda (pos)
  19582. (fill-region-as-paragraph pos (point) justify)
  19583. (goto-char pos))
  19584. ;; Find the list of ending positions for line
  19585. ;; breaks in the current paragraph. Add paragraph
  19586. ;; beginning to include first slice.
  19587. (nreverse
  19588. (cons
  19589. beg
  19590. (org-element-map
  19591. (org-element--parse-objects
  19592. beg end nil org-element-all-objects)
  19593. 'line-break
  19594. (lambda (lb) (org-element-property :end lb)))))))
  19595. t)))
  19596. ;; Contents of `comment-block' type elements should be
  19597. ;; filled as plain text, but only if point is within block
  19598. ;; markers.
  19599. (comment-block
  19600. (let* ((case-fold-search t)
  19601. (beg (save-excursion
  19602. (goto-char (org-element-property :begin element))
  19603. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19604. (forward-line)
  19605. (point)))
  19606. (end (save-excursion
  19607. (goto-char (org-element-property :end element))
  19608. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19609. (line-beginning-position))))
  19610. (when (and (>= (point) beg) (< (point) end))
  19611. (fill-region-as-paragraph
  19612. (save-excursion
  19613. (end-of-line)
  19614. (re-search-backward "^[ \t]*$" beg 'move)
  19615. (line-beginning-position))
  19616. (save-excursion
  19617. (beginning-of-line)
  19618. (re-search-forward "^[ \t]*$" end 'move)
  19619. (line-beginning-position))
  19620. justify)))
  19621. t)
  19622. ;; Fill comments.
  19623. (comment (fill-comment-paragraph justify))
  19624. ;; Ignore every other element.
  19625. (otherwise t)))))))
  19626. (defun org-auto-fill-function ()
  19627. "Auto-fill function."
  19628. ;; Check if auto-filling is meaningful.
  19629. (let ((fc (current-fill-column)))
  19630. (when (and fc (> (current-column) fc))
  19631. (let* ((fill-prefix (org-adaptive-fill-function))
  19632. ;; Enforce empty fill prefix, if required. Otherwise, it
  19633. ;; will be computed again.
  19634. (adaptive-fill-mode (not (equal fill-prefix ""))))
  19635. (when fill-prefix (do-auto-fill))))))
  19636. (defun org-comment-line-break-function (&optional soft)
  19637. "Break line at point and indent, continuing comment if within one.
  19638. The inserted newline is marked hard if variable
  19639. `use-hard-newlines' is true, unless optional argument SOFT is
  19640. non-nil."
  19641. (if soft (insert-and-inherit ?\n) (newline 1))
  19642. (save-excursion (forward-char -1) (delete-horizontal-space))
  19643. (delete-horizontal-space)
  19644. (indent-to-left-margin)
  19645. (insert-before-markers-and-inherit fill-prefix))
  19646. ;;; Comments
  19647. ;; Org comments syntax is quite complex. It requires the entire line
  19648. ;; to be just a comment. Also, even with the right syntax at the
  19649. ;; beginning of line, some some elements (i.e. verse-block or
  19650. ;; example-block) don't accept comments. Usual Emacs comment commands
  19651. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19652. ;; Org still relies on `comment-dwim', but cannot trust
  19653. ;; `comment-only-p'. So, `comment-region-function' and
  19654. ;; `uncomment-region-function' both point
  19655. ;; to`org-comment-or-uncomment-region'. Eventually,
  19656. ;; `org-insert-comment' takes care of insertion of comments at the
  19657. ;; beginning of line.
  19658. ;; `org-setup-comments-handling' install comments related variables
  19659. ;; during `org-mode' initialization.
  19660. (defun org-setup-comments-handling ()
  19661. (interactive)
  19662. (org-set-local 'comment-use-syntax nil)
  19663. (org-set-local 'comment-start "# ")
  19664. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19665. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19666. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19667. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19668. (defun org-insert-comment ()
  19669. "Insert an empty comment above current line.
  19670. If the line is empty, insert comment at its beginning."
  19671. (beginning-of-line)
  19672. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19673. (org-indent-line)
  19674. (insert "# "))
  19675. (defvar comment-empty-lines) ; From newcomment.el.
  19676. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19677. "Comment or uncomment each non-blank line in the region.
  19678. Uncomment each non-blank line between BEG and END if it only
  19679. contains commented lines. Otherwise, comment them."
  19680. (save-restriction
  19681. ;; Restrict region
  19682. (narrow-to-region (save-excursion (goto-char beg)
  19683. (skip-chars-forward " \r\t\n" end)
  19684. (line-beginning-position))
  19685. (save-excursion (goto-char end)
  19686. (skip-chars-backward " \r\t\n" beg)
  19687. (line-end-position)))
  19688. (let ((uncommentp
  19689. ;; UNCOMMENTP is non-nil when every non blank line between
  19690. ;; BEG and END is a comment.
  19691. (save-excursion
  19692. (goto-char (point-min))
  19693. (while (and (not (eobp))
  19694. (let ((element (org-element-at-point)))
  19695. (and (eq (org-element-type element) 'comment)
  19696. (goto-char (min (point-max)
  19697. (org-element-property
  19698. :end element)))))))
  19699. (eobp))))
  19700. (if uncommentp
  19701. ;; Only blank lines and comments in region: uncomment it.
  19702. (save-excursion
  19703. (goto-char (point-min))
  19704. (while (not (eobp))
  19705. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19706. (replace-match "" nil nil nil 1))
  19707. (forward-line)))
  19708. ;; Comment each line in region.
  19709. (let ((min-indent (point-max)))
  19710. ;; First find the minimum indentation across all lines.
  19711. (save-excursion
  19712. (goto-char (point-min))
  19713. (while (and (not (eobp)) (not (zerop min-indent)))
  19714. (unless (looking-at "[ \t]*$")
  19715. (setq min-indent (min min-indent (current-indentation))))
  19716. (forward-line)))
  19717. ;; Then loop over all lines.
  19718. (save-excursion
  19719. (goto-char (point-min))
  19720. (while (not (eobp))
  19721. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19722. (org-move-to-column min-indent t)
  19723. (insert comment-start))
  19724. (forward-line))))))))
  19725. ;;; Other stuff.
  19726. (defun org-toggle-fixed-width-section (arg)
  19727. "Toggle the fixed-width export.
  19728. If there is no active region, the QUOTE keyword at the current headline is
  19729. inserted or removed. When present, it causes the text between this headline
  19730. and the next to be exported as fixed-width text, and unmodified.
  19731. If there is an active region, this command adds or removes a colon as the
  19732. first character of this line. If the first character of a line is a colon,
  19733. this line is also exported in fixed-width font."
  19734. (interactive "P")
  19735. (let* ((cc 0)
  19736. (regionp (org-region-active-p))
  19737. (beg (if regionp (region-beginning) (point)))
  19738. (end (if regionp (region-end)))
  19739. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19740. (case-fold-search nil)
  19741. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19742. off)
  19743. (if regionp
  19744. (save-excursion
  19745. (goto-char beg)
  19746. (setq cc (current-column))
  19747. (beginning-of-line 1)
  19748. (setq off (looking-at re))
  19749. (while (> nlines 0)
  19750. (setq nlines (1- nlines))
  19751. (beginning-of-line 1)
  19752. (cond
  19753. (arg
  19754. (org-move-to-column cc t)
  19755. (insert ": \n")
  19756. (forward-line -1))
  19757. ((and off (looking-at re))
  19758. (replace-match "" t t nil 1))
  19759. ((not off) (org-move-to-column cc t) (insert ": ")))
  19760. (forward-line 1)))
  19761. (save-excursion
  19762. (org-back-to-heading)
  19763. (cond
  19764. ((looking-at (format org-heading-keyword-regexp-format
  19765. org-quote-string))
  19766. (goto-char (match-end 1))
  19767. (looking-at (concat " +" org-quote-string))
  19768. (replace-match "" t t)
  19769. (when (eolp) (insert " ")))
  19770. ((looking-at org-outline-regexp)
  19771. (goto-char (match-end 0))
  19772. (insert org-quote-string " ")))))))
  19773. (defun org-reftex-citation ()
  19774. "Use reftex-citation to insert a citation into the buffer.
  19775. This looks for a line like
  19776. #+BIBLIOGRAPHY: foo plain option:-d
  19777. and derives from it that foo.bib is the bibliography file relevant
  19778. for this document. It then installs the necessary environment for RefTeX
  19779. to work in this buffer and calls `reftex-citation' to insert a citation
  19780. into the buffer.
  19781. Export of such citations to both LaTeX and HTML is handled by the contributed
  19782. package org-exp-bibtex by Taru Karttunen."
  19783. (interactive)
  19784. (let ((reftex-docstruct-symbol 'rds)
  19785. (reftex-cite-format "\\cite{%l}")
  19786. rds bib)
  19787. (save-excursion
  19788. (save-restriction
  19789. (widen)
  19790. (let ((case-fold-search t)
  19791. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19792. (if (not (save-excursion
  19793. (or (re-search-forward re nil t)
  19794. (re-search-backward re nil t))))
  19795. (error "No bibliography defined in file")
  19796. (setq bib (concat (match-string 1) ".bib")
  19797. rds (list (list 'bib bib)))))))
  19798. (call-interactively 'reftex-citation)))
  19799. ;;;; Functions extending outline functionality
  19800. (defun org-beginning-of-line (&optional arg)
  19801. "Go to the beginning of the current line. If that is invisible, continue
  19802. to a visible line beginning. This makes the function of C-a more intuitive.
  19803. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19804. first attempt, and only move to after the tags when the cursor is already
  19805. beyond the end of the headline."
  19806. (interactive "P")
  19807. (let ((pos (point))
  19808. (special (if (consp org-special-ctrl-a/e)
  19809. (car org-special-ctrl-a/e)
  19810. org-special-ctrl-a/e))
  19811. refpos)
  19812. (if (org-bound-and-true-p line-move-visual)
  19813. (beginning-of-visual-line 1)
  19814. (beginning-of-line 1))
  19815. (if (and arg (fboundp 'move-beginning-of-line))
  19816. (call-interactively 'move-beginning-of-line)
  19817. (if (bobp)
  19818. nil
  19819. (backward-char 1)
  19820. (if (org-truely-invisible-p)
  19821. (while (and (not (bobp)) (org-truely-invisible-p))
  19822. (backward-char 1)
  19823. (beginning-of-line 1))
  19824. (forward-char 1))))
  19825. (when special
  19826. (cond
  19827. ((and (looking-at org-complex-heading-regexp)
  19828. (= (char-after (match-end 1)) ?\ ))
  19829. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19830. (point-at-eol)))
  19831. (goto-char
  19832. (if (eq special t)
  19833. (cond ((> pos refpos) refpos)
  19834. ((= pos (point)) refpos)
  19835. (t (point)))
  19836. (cond ((> pos (point)) (point))
  19837. ((not (eq last-command this-command)) (point))
  19838. (t refpos)))))
  19839. ((org-at-item-p)
  19840. ;; Being at an item and not looking at an the item means point
  19841. ;; was previously moved to beginning of a visual line, which
  19842. ;; doesn't contain the item. Therefore, do nothing special,
  19843. ;; just stay here.
  19844. (when (looking-at org-list-full-item-re)
  19845. ;; Set special position at first white space character after
  19846. ;; bullet, and check-box, if any.
  19847. (let ((after-bullet
  19848. (let ((box (match-end 3)))
  19849. (if (not box) (match-end 1)
  19850. (let ((after (char-after box)))
  19851. (if (and after (= after ? )) (1+ box) box))))))
  19852. ;; Special case: Move point to special position when
  19853. ;; currently after it or at beginning of line.
  19854. (if (eq special t)
  19855. (when (or (> pos after-bullet) (= (point) pos))
  19856. (goto-char after-bullet))
  19857. ;; Reversed case: Move point to special position when
  19858. ;; point was already at beginning of line and command is
  19859. ;; repeated.
  19860. (when (and (= (point) pos) (eq last-command this-command))
  19861. (goto-char after-bullet))))))))
  19862. (org-no-warnings
  19863. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19864. (defun org-end-of-line (&optional arg)
  19865. "Go to the end of the line.
  19866. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  19867. tags on the first attempt, and only move to after the tags when
  19868. the cursor is already beyond the end of the headline."
  19869. (interactive "P")
  19870. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  19871. org-special-ctrl-a/e))
  19872. (type (org-element-type
  19873. (save-excursion (beginning-of-line) (org-element-at-point)))))
  19874. (cond
  19875. ((or (not special) arg)
  19876. (call-interactively
  19877. (if (fboundp 'move-end-of-line) 'move-end-of-line 'end-of-line)))
  19878. ((memq type '(headline inlinetask))
  19879. (let ((pos (point)))
  19880. (beginning-of-line 1)
  19881. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19882. (if (eq special t)
  19883. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  19884. (goto-char (match-beginning 1))
  19885. (goto-char (match-end 0)))
  19886. (if (or (< pos (match-end 0))
  19887. (not (eq this-command last-command)))
  19888. (goto-char (match-end 0))
  19889. (goto-char (match-beginning 1))))
  19890. (call-interactively
  19891. (if (fboundp 'move-end-of-line) 'move-end-of-line 'end-of-line)))))
  19892. ((memq type
  19893. '(center-block comment-block drawer dynamic-block example-block
  19894. export-block item plain-list property-drawer
  19895. quote-block special-block src-block verse-block))
  19896. ;; Never move past the ellipsis.
  19897. (or (eolp) (move-end-of-line 1))
  19898. (when (org-invisible-p2) (backward-char)))
  19899. (t
  19900. (call-interactively
  19901. (if (fboundp 'move-end-of-line) 'move-end-of-line 'end-of-line))))
  19902. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19903. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19904. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19905. (defun org-backward-sentence (&optional arg)
  19906. "Go to beginning of sentence, or beginning of table field.
  19907. This will call `backward-sentence' or `org-table-beginning-of-field',
  19908. depending on context."
  19909. (interactive "P")
  19910. (cond
  19911. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19912. (t (call-interactively 'backward-sentence))))
  19913. (defun org-forward-sentence (&optional arg)
  19914. "Go to end of sentence, or end of table field.
  19915. This will call `forward-sentence' or `org-table-end-of-field',
  19916. depending on context."
  19917. (interactive "P")
  19918. (cond
  19919. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19920. (t (call-interactively 'forward-sentence))))
  19921. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19922. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19923. (defun org-kill-line (&optional arg)
  19924. "Kill line, to tags or end of line."
  19925. (interactive "P")
  19926. (cond
  19927. ((or (not org-special-ctrl-k)
  19928. (bolp)
  19929. (not (org-at-heading-p)))
  19930. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19931. org-ctrl-k-protect-subtree)
  19932. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19933. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19934. (error "C-k aborted - would kill hidden subtree")))
  19935. (call-interactively
  19936. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19937. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19938. (kill-region (point) (match-beginning 1))
  19939. (org-set-tags nil t))
  19940. (t (kill-region (point) (point-at-eol)))))
  19941. (define-key org-mode-map "\C-k" 'org-kill-line)
  19942. (defun org-yank (&optional arg)
  19943. "Yank. If the kill is a subtree, treat it specially.
  19944. This command will look at the current kill and check if is a single
  19945. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19946. cursor is at the beginning of a line or after the stars of a currently
  19947. empty headline, then the yank is handled specially. How exactly depends
  19948. on the value of the following variables, both set by default.
  19949. org-yank-folded-subtrees
  19950. When set, the subtree(s) will be folded after insertion, but only
  19951. if doing so would now swallow text after the yanked text.
  19952. org-yank-adjusted-subtrees
  19953. When set, the subtree will be promoted or demoted in order to
  19954. fit into the local outline tree structure, which means that the level
  19955. will be adjusted so that it becomes the smaller one of the two
  19956. *visible* surrounding headings.
  19957. Any prefix to this command will cause `yank' to be called directly with
  19958. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19959. will just
  19960. plainly yank the text as it is.
  19961. \[1] The test checks if the first non-white line is a heading
  19962. and if there are no other headings with fewer stars."
  19963. (interactive "P")
  19964. (org-yank-generic 'yank arg))
  19965. (defun org-yank-generic (command arg)
  19966. "Perform some yank-like command.
  19967. This function implements the behavior described in the `org-yank'
  19968. documentation. However, it has been generalized to work for any
  19969. interactive command with similar behavior."
  19970. ;; pretend to be command COMMAND
  19971. (setq this-command command)
  19972. (if arg
  19973. (call-interactively command)
  19974. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19975. (and (org-kill-is-subtree-p)
  19976. (or (bolp)
  19977. (and (looking-at "[ \t]*$")
  19978. (string-match
  19979. "\\`\\*+\\'"
  19980. (buffer-substring (point-at-bol) (point)))))))
  19981. swallowp)
  19982. (cond
  19983. ((and subtreep org-yank-folded-subtrees)
  19984. (let ((beg (point))
  19985. end)
  19986. (if (and subtreep org-yank-adjusted-subtrees)
  19987. (org-paste-subtree nil nil 'for-yank)
  19988. (call-interactively command))
  19989. (setq end (point))
  19990. (goto-char beg)
  19991. (when (and (bolp) subtreep
  19992. (not (setq swallowp
  19993. (org-yank-folding-would-swallow-text beg end))))
  19994. (org-with-limited-levels
  19995. (or (looking-at org-outline-regexp)
  19996. (re-search-forward org-outline-regexp-bol end t))
  19997. (while (and (< (point) end) (looking-at org-outline-regexp))
  19998. (hide-subtree)
  19999. (org-cycle-show-empty-lines 'folded)
  20000. (condition-case nil
  20001. (outline-forward-same-level 1)
  20002. (error (goto-char end))))))
  20003. (when swallowp
  20004. (message
  20005. "Inserted text not folded because that would swallow text"))
  20006. (goto-char end)
  20007. (skip-chars-forward " \t\n\r")
  20008. (beginning-of-line 1)
  20009. (push-mark beg 'nomsg)))
  20010. ((and subtreep org-yank-adjusted-subtrees)
  20011. (let ((beg (point-at-bol)))
  20012. (org-paste-subtree nil nil 'for-yank)
  20013. (push-mark beg 'nomsg)))
  20014. (t
  20015. (call-interactively command))))))
  20016. (defun org-yank-folding-would-swallow-text (beg end)
  20017. "Would hide-subtree at BEG swallow any text after END?"
  20018. (let (level)
  20019. (org-with-limited-levels
  20020. (save-excursion
  20021. (goto-char beg)
  20022. (when (or (looking-at org-outline-regexp)
  20023. (re-search-forward org-outline-regexp-bol end t))
  20024. (setq level (org-outline-level)))
  20025. (goto-char end)
  20026. (skip-chars-forward " \t\r\n\v\f")
  20027. (if (or (eobp)
  20028. (and (bolp) (looking-at org-outline-regexp)
  20029. (<= (org-outline-level) level)))
  20030. nil ; Nothing would be swallowed
  20031. t))))) ; something would swallow
  20032. (define-key org-mode-map "\C-y" 'org-yank)
  20033. (defun org-truely-invisible-p ()
  20034. "Check if point is at a character currently not visible.
  20035. This version does not only check the character property, but also
  20036. `visible-mode'."
  20037. ;; Early versions of noutline don't have `outline-invisible-p'.
  20038. (if (org-bound-and-true-p visible-mode)
  20039. nil
  20040. (outline-invisible-p)))
  20041. (defun org-invisible-p2 ()
  20042. "Check if point is at a character currently not visible."
  20043. (save-excursion
  20044. (if (and (eolp) (not (bobp))) (backward-char 1))
  20045. ;; Early versions of noutline don't have `outline-invisible-p'.
  20046. (outline-invisible-p)))
  20047. (defun org-back-to-heading (&optional invisible-ok)
  20048. "Call `outline-back-to-heading', but provide a better error message."
  20049. (condition-case nil
  20050. (outline-back-to-heading invisible-ok)
  20051. (error (error "Before first headline at position %d in buffer %s"
  20052. (point) (current-buffer)))))
  20053. (defun org-before-first-heading-p ()
  20054. "Before first heading?"
  20055. (save-excursion
  20056. (end-of-line)
  20057. (null (re-search-backward org-outline-regexp-bol nil t))))
  20058. (defun org-at-heading-p (&optional ignored)
  20059. (outline-on-heading-p t))
  20060. ;; Compatibility alias with Org versions < 7.8.03
  20061. (defalias 'org-on-heading-p 'org-at-heading-p)
  20062. (defun org-at-comment-p nil
  20063. "Is cursor in a line starting with a # character?"
  20064. (save-excursion
  20065. (beginning-of-line)
  20066. (looking-at "^#")))
  20067. (defun org-at-drawer-p nil
  20068. "Is cursor at a drawer keyword?"
  20069. (save-excursion
  20070. (move-beginning-of-line 1)
  20071. (looking-at org-drawer-regexp)))
  20072. (defun org-at-block-p nil
  20073. "Is cursor at a block keyword?"
  20074. (save-excursion
  20075. (move-beginning-of-line 1)
  20076. (looking-at org-block-regexp)))
  20077. (defun org-point-at-end-of-empty-headline ()
  20078. "If point is at the end of an empty headline, return t, else nil.
  20079. If the heading only contains a TODO keyword, it is still still considered
  20080. empty."
  20081. (and (looking-at "[ \t]*$")
  20082. (when org-todo-line-regexp
  20083. (save-excursion
  20084. (beginning-of-line 1)
  20085. (let ((case-fold-search nil))
  20086. (looking-at org-todo-line-regexp)
  20087. (string= (match-string 3) ""))))))
  20088. (defun org-at-heading-or-item-p ()
  20089. (or (org-at-heading-p) (org-at-item-p)))
  20090. (defun org-at-target-p ()
  20091. (or (org-in-regexp org-radio-target-regexp)
  20092. (org-in-regexp org-target-regexp)))
  20093. ;; Compatibility alias with Org versions < 7.8.03
  20094. (defalias 'org-on-target-p 'org-at-target-p)
  20095. (defun org-up-heading-all (arg)
  20096. "Move to the heading line of which the present line is a subheading.
  20097. This function considers both visible and invisible heading lines.
  20098. With argument, move up ARG levels."
  20099. (if (fboundp 'outline-up-heading-all)
  20100. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20101. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20102. (defun org-up-heading-safe ()
  20103. "Move to the heading line of which the present line is a subheading.
  20104. This version will not throw an error. It will return the level of the
  20105. headline found, or nil if no higher level is found.
  20106. Also, this function will be a lot faster than `outline-up-heading',
  20107. because it relies on stars being the outline starters. This can really
  20108. make a significant difference in outlines with very many siblings."
  20109. (let (start-level re)
  20110. (org-back-to-heading t)
  20111. (setq start-level (funcall outline-level))
  20112. (if (equal start-level 1)
  20113. nil
  20114. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20115. (if (re-search-backward re nil t)
  20116. (funcall outline-level)))))
  20117. (defun org-first-sibling-p ()
  20118. "Is this heading the first child of its parents?"
  20119. (interactive)
  20120. (let ((re org-outline-regexp-bol)
  20121. level l)
  20122. (unless (org-at-heading-p t)
  20123. (error "Not at a heading"))
  20124. (setq level (funcall outline-level))
  20125. (save-excursion
  20126. (if (not (re-search-backward re nil t))
  20127. t
  20128. (setq l (funcall outline-level))
  20129. (< l level)))))
  20130. (defun org-goto-sibling (&optional previous)
  20131. "Goto the next sibling, even if it is invisible.
  20132. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20133. when a sibling was found. When none is found, return nil and don't
  20134. move point."
  20135. (let ((fun (if previous 're-search-backward 're-search-forward))
  20136. (pos (point))
  20137. (re org-outline-regexp-bol)
  20138. level l)
  20139. (when (condition-case nil (org-back-to-heading t) (error nil))
  20140. (setq level (funcall outline-level))
  20141. (catch 'exit
  20142. (or previous (forward-char 1))
  20143. (while (funcall fun re nil t)
  20144. (setq l (funcall outline-level))
  20145. (when (< l level) (goto-char pos) (throw 'exit nil))
  20146. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20147. (goto-char pos)
  20148. nil))))
  20149. (defun org-show-siblings ()
  20150. "Show all siblings of the current headline."
  20151. (save-excursion
  20152. (while (org-goto-sibling) (org-flag-heading nil)))
  20153. (save-excursion
  20154. (while (org-goto-sibling 'previous)
  20155. (org-flag-heading nil))))
  20156. (defun org-goto-first-child ()
  20157. "Goto the first child, even if it is invisible.
  20158. Return t when a child was found. Otherwise don't move point and
  20159. return nil."
  20160. (let (level (pos (point)) (re org-outline-regexp-bol))
  20161. (when (condition-case nil (org-back-to-heading t) (error nil))
  20162. (setq level (outline-level))
  20163. (forward-char 1)
  20164. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20165. (progn (goto-char (match-beginning 0)) t)
  20166. (goto-char pos) nil))))
  20167. (defun org-show-hidden-entry ()
  20168. "Show an entry where even the heading is hidden."
  20169. (save-excursion
  20170. (org-show-entry)))
  20171. (defun org-flag-heading (flag &optional entry)
  20172. "Flag the current heading. FLAG non-nil means make invisible.
  20173. When ENTRY is non-nil, show the entire entry."
  20174. (save-excursion
  20175. (org-back-to-heading t)
  20176. ;; Check if we should show the entire entry
  20177. (if entry
  20178. (progn
  20179. (org-show-entry)
  20180. (save-excursion
  20181. (and (outline-next-heading)
  20182. (org-flag-heading nil))))
  20183. (outline-flag-region (max (point-min) (1- (point)))
  20184. (save-excursion (outline-end-of-heading) (point))
  20185. flag))))
  20186. (defun org-get-next-sibling ()
  20187. "Move to next heading of the same level, and return point.
  20188. If there is no such heading, return nil.
  20189. This is like outline-next-sibling, but invisible headings are ok."
  20190. (let ((level (funcall outline-level)))
  20191. (outline-next-heading)
  20192. (while (and (not (eobp)) (> (funcall outline-level) level))
  20193. (outline-next-heading))
  20194. (if (or (eobp) (< (funcall outline-level) level))
  20195. nil
  20196. (point))))
  20197. (defun org-get-last-sibling ()
  20198. "Move to previous heading of the same level, and return point.
  20199. If there is no such heading, return nil."
  20200. (let ((opoint (point))
  20201. (level (funcall outline-level)))
  20202. (outline-previous-heading)
  20203. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20204. (while (and (> (funcall outline-level) level)
  20205. (not (bobp)))
  20206. (outline-previous-heading))
  20207. (if (< (funcall outline-level) level)
  20208. nil
  20209. (point)))))
  20210. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20211. "Goto to the end of a subtree."
  20212. ;; This contains an exact copy of the original function, but it uses
  20213. ;; `org-back-to-heading', to make it work also in invisible
  20214. ;; trees. And is uses an invisible-ok argument.
  20215. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20216. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20217. ;; with many children and grandchildren etc, this can be much faster
  20218. ;; than the outline version.
  20219. (org-back-to-heading invisible-ok)
  20220. (let ((first t)
  20221. (level (funcall outline-level)))
  20222. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20223. ;; A true heading (not a plain list item), in Org-mode
  20224. ;; This means we can easily find the end by looking
  20225. ;; only for the right number of stars. Using a regexp to do
  20226. ;; this is so much faster than using a Lisp loop.
  20227. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20228. (forward-char 1)
  20229. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20230. ;; something else, do it the slow way
  20231. (while (and (not (eobp))
  20232. (or first (> (funcall outline-level) level)))
  20233. (setq first nil)
  20234. (outline-next-heading)))
  20235. (unless to-heading
  20236. (if (memq (preceding-char) '(?\n ?\^M))
  20237. (progn
  20238. ;; Go to end of line before heading
  20239. (forward-char -1)
  20240. (if (memq (preceding-char) '(?\n ?\^M))
  20241. ;; leave blank line before heading
  20242. (forward-char -1))))))
  20243. (point))
  20244. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20245. "Use Org version in org-mode, for dramatic speed-up."
  20246. (if (derived-mode-p 'org-mode)
  20247. (progn
  20248. (org-end-of-subtree nil t)
  20249. (unless (eobp) (backward-char 1)))
  20250. ad-do-it))
  20251. (defun org-end-of-meta-data-and-drawers ()
  20252. "Jump to the first text after meta data and drawers in the current entry.
  20253. This will move over empty lines, lines with planning time stamps,
  20254. clocking lines, and drawers."
  20255. (org-back-to-heading t)
  20256. (let ((end (save-excursion (outline-next-heading) (point)))
  20257. (re (concat "\\(" org-drawer-regexp "\\)"
  20258. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20259. (forward-line 1)
  20260. (while (re-search-forward re end t)
  20261. (if (not (match-end 1))
  20262. ;; empty or planning line
  20263. (forward-line 1)
  20264. ;; a drawer, find the end
  20265. (re-search-forward "^[ \t]*:END:" end 'move)
  20266. (forward-line 1)))
  20267. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20268. (point)))
  20269. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20270. "Move forward to the arg'th subheading at same level as this one.
  20271. Stop at the first and last subheadings of a superior heading.
  20272. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20273. non-nil it will also look at invisible ones."
  20274. (interactive "p")
  20275. (org-back-to-heading invisible-ok)
  20276. (org-at-heading-p)
  20277. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20278. (re (format "^\\*\\{1,%d\\} " level))
  20279. l)
  20280. (forward-char 1)
  20281. (while (> arg 0)
  20282. (while (and (re-search-forward re nil 'move)
  20283. (setq l (- (match-end 0) (match-beginning 0) 1))
  20284. (= l level)
  20285. (not invisible-ok)
  20286. (progn (backward-char 1) (outline-invisible-p)))
  20287. (if (< l level) (setq arg 1)))
  20288. (setq arg (1- arg)))
  20289. (beginning-of-line 1)))
  20290. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20291. "Move backward to the arg'th subheading at same level as this one.
  20292. Stop at the first and last subheadings of a superior heading."
  20293. (interactive "p")
  20294. (org-back-to-heading)
  20295. (org-at-heading-p)
  20296. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20297. (re (format "^\\*\\{1,%d\\} " level))
  20298. l)
  20299. (while (> arg 0)
  20300. (while (and (re-search-backward re nil 'move)
  20301. (setq l (- (match-end 0) (match-beginning 0) 1))
  20302. (= l level)
  20303. (not invisible-ok)
  20304. (outline-invisible-p))
  20305. (if (< l level) (setq arg 1)))
  20306. (setq arg (1- arg)))))
  20307. (defun org-forward-element ()
  20308. "Move forward by one element.
  20309. Move to the next element at the same level, when possible."
  20310. (interactive)
  20311. (cond ((eobp) (error "Cannot move further down"))
  20312. ((org-with-limited-levels (org-at-heading-p))
  20313. (let ((origin (point)))
  20314. (org-forward-heading-same-level 1)
  20315. (unless (org-with-limited-levels (org-at-heading-p))
  20316. (goto-char origin)
  20317. (error "Cannot move further down"))))
  20318. (t
  20319. (let* ((elem (org-element-at-point))
  20320. (end (org-element-property :end elem))
  20321. (parent (org-element-property :parent elem)))
  20322. (if (and parent (= (org-element-property :contents-end parent) end))
  20323. (goto-char (org-element-property :end parent))
  20324. (goto-char end))))))
  20325. (defun org-backward-element ()
  20326. "Move backward by one element.
  20327. Move to the previous element at the same level, when possible."
  20328. (interactive)
  20329. (cond ((bobp) (error "Cannot move further up"))
  20330. ((org-with-limited-levels (org-at-heading-p))
  20331. ;; At an headline, move to the previous one, if any, or stay
  20332. ;; here.
  20333. (let ((origin (point)))
  20334. (org-backward-heading-same-level 1)
  20335. (unless (org-with-limited-levels (org-at-heading-p))
  20336. (goto-char origin)
  20337. (error "Cannot move further up"))))
  20338. (t
  20339. (let* ((trail (org-element-at-point 'keep-trail))
  20340. (elem (car trail))
  20341. (prev-elem (nth 1 trail))
  20342. (beg (org-element-property :begin elem)))
  20343. (cond
  20344. ;; Move to beginning of current element if point isn't
  20345. ;; there already.
  20346. ((/= (point) beg) (goto-char beg))
  20347. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20348. ((org-before-first-heading-p) (goto-char (point-min)))
  20349. (t (org-back-to-heading)))))))
  20350. (defun org-up-element ()
  20351. "Move to upper element."
  20352. (interactive)
  20353. (if (org-with-limited-levels (org-at-heading-p))
  20354. (unless (org-up-heading-safe) (error "No surrounding element"))
  20355. (let* ((elem (org-element-at-point))
  20356. (parent (org-element-property :parent elem)))
  20357. (if parent (goto-char (org-element-property :begin parent))
  20358. (if (org-with-limited-levels (org-before-first-heading-p))
  20359. (error "No surrounding element")
  20360. (org-with-limited-levels (org-back-to-heading)))))))
  20361. (defvar org-element-greater-elements)
  20362. (defun org-down-element ()
  20363. "Move to inner element."
  20364. (interactive)
  20365. (let ((element (org-element-at-point)))
  20366. (cond
  20367. ((memq (org-element-type element) '(plain-list table))
  20368. (goto-char (org-element-property :contents-begin element))
  20369. (forward-char))
  20370. ((memq (org-element-type element) org-element-greater-elements)
  20371. ;; If contents are hidden, first disclose them.
  20372. (when (org-element-property :hiddenp element) (org-cycle))
  20373. (goto-char (or (org-element-property :contents-begin element)
  20374. (error "No content for this element"))))
  20375. (t (error "No inner element")))))
  20376. (defun org-drag-element-backward ()
  20377. "Move backward element at point."
  20378. (interactive)
  20379. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20380. (let* ((trail (org-element-at-point 'keep-trail))
  20381. (elem (car trail))
  20382. (prev-elem (nth 1 trail)))
  20383. ;; Error out if no previous element or previous element is
  20384. ;; a parent of the current one.
  20385. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20386. (error "Cannot drag element backward")
  20387. (let ((pos (point)))
  20388. (org-element-swap-A-B prev-elem elem)
  20389. (goto-char (+ (org-element-property :begin prev-elem)
  20390. (- pos (org-element-property :begin elem)))))))))
  20391. (defun org-drag-element-forward ()
  20392. "Move forward element at point."
  20393. (interactive)
  20394. (let* ((pos (point))
  20395. (elem (org-element-at-point)))
  20396. (when (= (point-max) (org-element-property :end elem))
  20397. (error "Cannot drag element forward"))
  20398. (goto-char (org-element-property :end elem))
  20399. (let ((next-elem (org-element-at-point)))
  20400. (when (or (org-element-nested-p elem next-elem)
  20401. (and (eq (org-element-type next-elem) 'headline)
  20402. (not (eq (org-element-type elem) 'headline))))
  20403. (goto-char pos)
  20404. (error "Cannot drag element forward"))
  20405. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20406. ;; body's length (without final blanks) and by the length of
  20407. ;; blanks between ELEM and NEXT-ELEM.
  20408. (let ((size-next (- (save-excursion
  20409. (goto-char (org-element-property :end next-elem))
  20410. (skip-chars-backward " \r\t\n")
  20411. (forward-line)
  20412. ;; Small correction if buffer doesn't end
  20413. ;; with a newline character.
  20414. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20415. (org-element-property :begin next-elem)))
  20416. (size-blank (- (org-element-property :end elem)
  20417. (save-excursion
  20418. (goto-char (org-element-property :end elem))
  20419. (skip-chars-backward " \r\t\n")
  20420. (forward-line)
  20421. (point)))))
  20422. (org-element-swap-A-B elem next-elem)
  20423. (goto-char (+ pos size-next size-blank))))))
  20424. (defun org-mark-element ()
  20425. "Put point at beginning of this element, mark at end.
  20426. Interactively, if this command is repeated or (in Transient Mark
  20427. mode) if the mark is active, it marks the next element after the
  20428. ones already marked."
  20429. (interactive)
  20430. (let (deactivate-mark)
  20431. (if (and (org-called-interactively-p 'any)
  20432. (or (and (eq last-command this-command) (mark t))
  20433. (and transient-mark-mode mark-active)))
  20434. (set-mark
  20435. (save-excursion
  20436. (goto-char (mark))
  20437. (goto-char (org-element-property :end (org-element-at-point)))))
  20438. (let ((element (org-element-at-point)))
  20439. (end-of-line)
  20440. (push-mark (org-element-property :end element) t t)
  20441. (goto-char (org-element-property :begin element))))))
  20442. (defun org-narrow-to-element ()
  20443. "Narrow buffer to current element."
  20444. (interactive)
  20445. (let ((elem (org-element-at-point)))
  20446. (cond
  20447. ((eq (car elem) 'headline)
  20448. (narrow-to-region
  20449. (org-element-property :begin elem)
  20450. (org-element-property :end elem)))
  20451. ((memq (car elem) org-element-greater-elements)
  20452. (narrow-to-region
  20453. (org-element-property :contents-begin elem)
  20454. (org-element-property :contents-end elem)))
  20455. (t
  20456. (narrow-to-region
  20457. (org-element-property :begin elem)
  20458. (org-element-property :end elem))))))
  20459. (defun org-transpose-element ()
  20460. "Transpose current and previous elements, keeping blank lines between.
  20461. Point is moved after both elements."
  20462. (interactive)
  20463. (org-skip-whitespace)
  20464. (let ((end (org-element-property :end (org-element-at-point))))
  20465. (org-drag-element-backward)
  20466. (goto-char end)))
  20467. (defun org-unindent-buffer ()
  20468. "Un-indent the visible part of the buffer.
  20469. Relative indentation (between items, inside blocks, etc.) isn't
  20470. modified."
  20471. (interactive)
  20472. (unless (eq major-mode 'org-mode)
  20473. (error "Cannot un-indent a buffer not in Org mode"))
  20474. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20475. unindent-tree ; For byte-compiler.
  20476. (unindent-tree
  20477. (function
  20478. (lambda (contents)
  20479. (mapc
  20480. (lambda (element)
  20481. (if (memq (org-element-type element) '(headline section))
  20482. (funcall unindent-tree (org-element-contents element))
  20483. (save-excursion
  20484. (save-restriction
  20485. (narrow-to-region
  20486. (org-element-property :begin element)
  20487. (org-element-property :end element))
  20488. (org-do-remove-indentation)))))
  20489. (reverse contents))))))
  20490. (funcall unindent-tree (org-element-contents parse-tree))))
  20491. (defun org-show-subtree ()
  20492. "Show everything after this heading at deeper levels."
  20493. (interactive)
  20494. (outline-flag-region
  20495. (point)
  20496. (save-excursion
  20497. (org-end-of-subtree t t))
  20498. nil))
  20499. (defun org-show-entry ()
  20500. "Show the body directly following this heading.
  20501. Show the heading too, if it is currently invisible."
  20502. (interactive)
  20503. (save-excursion
  20504. (condition-case nil
  20505. (progn
  20506. (org-back-to-heading t)
  20507. (outline-flag-region
  20508. (max (point-min) (1- (point)))
  20509. (save-excursion
  20510. (if (re-search-forward
  20511. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20512. (match-beginning 1)
  20513. (point-max)))
  20514. nil)
  20515. (org-cycle-hide-drawers 'children))
  20516. (error nil))))
  20517. (defun org-make-options-regexp (kwds &optional extra)
  20518. "Make a regular expression for keyword lines."
  20519. (concat
  20520. "^#\\+\\("
  20521. (mapconcat 'regexp-quote kwds "\\|")
  20522. (if extra (concat "\\|" extra))
  20523. "\\):[ \t]*\\(.*\\)"))
  20524. ;; Make isearch reveal the necessary context
  20525. (defun org-isearch-end ()
  20526. "Reveal context after isearch exits."
  20527. (when isearch-success ; only if search was successful
  20528. (if (featurep 'xemacs)
  20529. ;; Under XEmacs, the hook is run in the correct place,
  20530. ;; we directly show the context.
  20531. (org-show-context 'isearch)
  20532. ;; In Emacs the hook runs *before* restoring the overlays.
  20533. ;; So we have to use a one-time post-command-hook to do this.
  20534. ;; (Emacs 22 has a special variable, see function `org-mode')
  20535. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20536. isearch-mode-end-hook-quit)
  20537. ;; Only when the isearch was not quitted.
  20538. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20539. 'append 'local)))))
  20540. (defun org-isearch-post-command ()
  20541. "Remove self from hook, and show context."
  20542. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20543. (org-show-context 'isearch))
  20544. ;;;; Integration with and fixes for other packages
  20545. ;;; Imenu support
  20546. (defvar org-imenu-markers nil
  20547. "All markers currently used by Imenu.")
  20548. (make-variable-buffer-local 'org-imenu-markers)
  20549. (defun org-imenu-new-marker (&optional pos)
  20550. "Return a new marker for use by Imenu, and remember the marker."
  20551. (let ((m (make-marker)))
  20552. (move-marker m (or pos (point)))
  20553. (push m org-imenu-markers)
  20554. m))
  20555. (defun org-imenu-get-tree ()
  20556. "Produce the index for Imenu."
  20557. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20558. (setq org-imenu-markers nil)
  20559. (let* ((n org-imenu-depth)
  20560. (re (concat "^" (org-get-limited-outline-regexp)))
  20561. (subs (make-vector (1+ n) nil))
  20562. (last-level 0)
  20563. m level head)
  20564. (save-excursion
  20565. (save-restriction
  20566. (widen)
  20567. (goto-char (point-max))
  20568. (while (re-search-backward re nil t)
  20569. (setq level (org-reduced-level (funcall outline-level)))
  20570. (when (and (<= level n)
  20571. (looking-at org-complex-heading-regexp))
  20572. (setq head (org-link-display-format
  20573. (org-match-string-no-properties 4))
  20574. m (org-imenu-new-marker))
  20575. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20576. (if (>= level last-level)
  20577. (push (cons head m) (aref subs level))
  20578. (push (cons head (aref subs (1+ level))) (aref subs level))
  20579. (loop for i from (1+ level) to n do (aset subs i nil)))
  20580. (setq last-level level)))))
  20581. (aref subs 1)))
  20582. (eval-after-load "imenu"
  20583. '(progn
  20584. (add-hook 'imenu-after-jump-hook
  20585. (lambda ()
  20586. (if (derived-mode-p 'org-mode)
  20587. (org-show-context 'org-goto))))))
  20588. (defun org-link-display-format (link)
  20589. "Replace a link with either the description, or the link target
  20590. if no description is present"
  20591. (save-match-data
  20592. (if (string-match org-bracket-link-analytic-regexp link)
  20593. (replace-match (if (match-end 5)
  20594. (match-string 5 link)
  20595. (concat (match-string 1 link)
  20596. (match-string 3 link)))
  20597. nil t link)
  20598. link)))
  20599. (defun org-toggle-link-display ()
  20600. "Toggle the literal or descriptive display of links."
  20601. (interactive)
  20602. (if org-descriptive-links
  20603. (progn (org-remove-from-invisibility-spec '(org-link))
  20604. (org-restart-font-lock)
  20605. (setq org-descriptive-links nil))
  20606. (progn (add-to-invisibility-spec '(org-link))
  20607. (org-restart-font-lock)
  20608. (setq org-descriptive-links t))))
  20609. ;; Speedbar support
  20610. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20611. "Overlay marking the agenda restriction line in speedbar.")
  20612. (overlay-put org-speedbar-restriction-lock-overlay
  20613. 'face 'org-agenda-restriction-lock)
  20614. (overlay-put org-speedbar-restriction-lock-overlay
  20615. 'help-echo "Agendas are currently limited to this item.")
  20616. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20617. (defun org-speedbar-set-agenda-restriction ()
  20618. "Restrict future agenda commands to the location at point in speedbar.
  20619. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20620. (interactive)
  20621. (require 'org-agenda)
  20622. (let (p m tp np dir txt)
  20623. (cond
  20624. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20625. 'org-imenu t))
  20626. (setq m (get-text-property p 'org-imenu-marker))
  20627. (with-current-buffer (marker-buffer m)
  20628. (goto-char m)
  20629. (org-agenda-set-restriction-lock 'subtree)))
  20630. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20631. 'speedbar-function 'speedbar-find-file))
  20632. (setq tp (previous-single-property-change
  20633. (1+ p) 'speedbar-function)
  20634. np (next-single-property-change
  20635. tp 'speedbar-function)
  20636. dir (speedbar-line-directory)
  20637. txt (buffer-substring-no-properties (or tp (point-min))
  20638. (or np (point-max))))
  20639. (with-current-buffer (find-file-noselect
  20640. (let ((default-directory dir))
  20641. (expand-file-name txt)))
  20642. (unless (derived-mode-p 'org-mode)
  20643. (error "Cannot restrict to non-Org-mode file"))
  20644. (org-agenda-set-restriction-lock 'file)))
  20645. (t (error "Don't know how to restrict Org-mode's agenda")))
  20646. (move-overlay org-speedbar-restriction-lock-overlay
  20647. (point-at-bol) (point-at-eol))
  20648. (setq current-prefix-arg nil)
  20649. (org-agenda-maybe-redo)))
  20650. (eval-after-load "speedbar"
  20651. '(progn
  20652. (speedbar-add-supported-extension ".org")
  20653. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20654. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20655. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20656. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20657. (add-hook 'speedbar-visiting-tag-hook
  20658. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20659. ;;; Fixes and Hacks for problems with other packages
  20660. ;; Make flyspell not check words in links, to not mess up our keymap
  20661. (defun org-mode-flyspell-verify ()
  20662. "Don't let flyspell put overlays at active buttons, or on
  20663. {todo,all-time,additional-option-like}-keywords."
  20664. (let ((pos (max (1- (point)) (point-min)))
  20665. (word (thing-at-point 'word)))
  20666. (and (not (get-text-property pos 'keymap))
  20667. (not (get-text-property pos 'org-no-flyspell))
  20668. (not (member word org-todo-keywords-1))
  20669. (not (member word org-all-time-keywords))
  20670. (not (member word org-options-keywords))
  20671. (not (member word (mapcar 'car org-startup-options)))
  20672. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20673. (defun org-remove-flyspell-overlays-in (beg end)
  20674. "Remove flyspell overlays in region."
  20675. (and (org-bound-and-true-p flyspell-mode)
  20676. (fboundp 'flyspell-delete-region-overlays)
  20677. (flyspell-delete-region-overlays beg end))
  20678. (add-text-properties beg end '(org-no-flyspell t)))
  20679. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20680. (eval-after-load "bookmark"
  20681. '(if (boundp 'bookmark-after-jump-hook)
  20682. ;; We can use the hook
  20683. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20684. ;; Hook not available, use advice
  20685. (defadvice bookmark-jump (after org-make-visible activate)
  20686. "Make the position visible."
  20687. (org-bookmark-jump-unhide))))
  20688. ;; Make sure saveplace shows the location if it was hidden
  20689. (eval-after-load "saveplace"
  20690. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20691. "Make the position visible."
  20692. (org-bookmark-jump-unhide)))
  20693. ;; Make sure ecb shows the location if it was hidden
  20694. (eval-after-load "ecb"
  20695. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20696. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20697. (if (derived-mode-p 'org-mode)
  20698. (org-show-context))))
  20699. (defun org-bookmark-jump-unhide ()
  20700. "Unhide the current position, to show the bookmark location."
  20701. (and (derived-mode-p 'org-mode)
  20702. (or (outline-invisible-p)
  20703. (save-excursion (goto-char (max (point-min) (1- (point))))
  20704. (outline-invisible-p)))
  20705. (org-show-context 'bookmark-jump)))
  20706. ;; Make session.el ignore our circular variable
  20707. (eval-after-load "session"
  20708. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20709. ;;;; Experimental code
  20710. (defun org-closed-in-range ()
  20711. "Sparse tree of items closed in a certain time range.
  20712. Still experimental, may disappear in the future."
  20713. (interactive)
  20714. ;; Get the time interval from the user.
  20715. (let* ((time1 (org-float-time
  20716. (org-read-date nil 'to-time nil "Starting date: ")))
  20717. (time2 (org-float-time
  20718. (org-read-date nil 'to-time nil "End date:")))
  20719. ;; callback function
  20720. (callback (lambda ()
  20721. (let ((time
  20722. (org-float-time
  20723. (apply 'encode-time
  20724. (org-parse-time-string
  20725. (match-string 1))))))
  20726. ;; check if time in interval
  20727. (and (>= time time1) (<= time time2))))))
  20728. ;; make tree, check each match with the callback
  20729. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20730. ;;;; Finish up
  20731. (provide 'org)
  20732. (run-hooks 'org-load-hook)
  20733. ;;; org.el ends here