org.el 851 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561
  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 "Maxima" maxima)
  190. (const :tag "Matlab" matlab)
  191. (const :tag "Mscgen" mscgen)
  192. (const :tag "Ocaml" ocaml)
  193. (const :tag "Octave" octave)
  194. (const :tag "Org" org)
  195. (const :tag "Perl" perl)
  196. (const :tag "Pico Lisp" picolisp)
  197. (const :tag "PlantUML" plantuml)
  198. (const :tag "Python" python)
  199. (const :tag "Ruby" ruby)
  200. (const :tag "Sass" sass)
  201. (const :tag "Scala" scala)
  202. (const :tag "Scheme" scheme)
  203. (const :tag "Screen" screen)
  204. (const :tag "Shell Script" sh)
  205. (const :tag "Shen" shen)
  206. (const :tag "Sql" sql)
  207. (const :tag "Sqlite" sqlite))
  208. :value-type (boolean :tag "Activate" :value t)))
  209. ;;;; Customization variables
  210. (defcustom org-clone-delete-id nil
  211. "Remove ID property of clones of a subtree.
  212. When non-nil, clones of a subtree don't inherit the ID property.
  213. Otherwise they inherit the ID property with a new unique
  214. identifier."
  215. :type 'boolean
  216. :version "24.1"
  217. :group 'org-id)
  218. ;;; Version
  219. (require 'org-compat)
  220. (org-check-version)
  221. ;;;###autoload
  222. (defun org-version (&optional here full message)
  223. "Show the org-mode version in the echo area.
  224. With prefix argument HERE, insert it at point.
  225. When FULL is non-nil, use a verbose version string.
  226. When MESSAGE is non-nil, display a message with the version."
  227. (interactive "P")
  228. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  229. (save-load-suffixes load-suffixes)
  230. (load-suffixes (list ".el"))
  231. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  232. (org-trash (or
  233. (and (fboundp 'org-release) (fboundp 'org-git-version))
  234. (load (concat org-dir "org-version")
  235. 'noerror 'nomessage nil 'mustsuffix)))
  236. (load-suffixes save-load-suffixes)
  237. (org-version (org-release))
  238. (git-version (org-git-version))
  239. (version (format "Org-mode version %s (%s @ %s)"
  240. org-version
  241. git-version
  242. (if org-install-dir
  243. (if (string= org-dir org-install-dir)
  244. org-install-dir
  245. (concat "mixed installation! " org-install-dir " and " org-dir))
  246. "org-loaddefs.el can not be found!")))
  247. (_version (if full version org-version)))
  248. (if (org-called-interactively-p 'interactive)
  249. (if here
  250. (insert version)
  251. (message version))
  252. (if message (message _version))
  253. _version)))
  254. (defconst org-version (org-version))
  255. ;;; Compatibility constants
  256. ;;; The custom variables
  257. (defgroup org nil
  258. "Outline-based notes management and organizer."
  259. :tag "Org"
  260. :group 'outlines
  261. :group 'calendar)
  262. (defcustom org-mode-hook nil
  263. "Mode hook for Org-mode, run after the mode was turned on."
  264. :group 'org
  265. :type 'hook)
  266. (defcustom org-load-hook nil
  267. "Hook that is run after org.el has been loaded."
  268. :group 'org
  269. :type 'hook)
  270. (defcustom org-log-buffer-setup-hook nil
  271. "Hook that is run after an Org log buffer is created."
  272. :group 'org
  273. :version "24.1"
  274. :type 'hook)
  275. (defvar org-modules) ; defined below
  276. (defvar org-modules-loaded nil
  277. "Have the modules been loaded already?")
  278. (defun org-load-modules-maybe (&optional force)
  279. "Load all extensions listed in `org-modules'."
  280. (when (or force (not org-modules-loaded))
  281. (mapc (lambda (ext)
  282. (condition-case nil (require ext)
  283. (error (message "Problems while trying to load feature `%s'" ext))))
  284. org-modules)
  285. (setq org-modules-loaded t)))
  286. (defun org-set-modules (var value)
  287. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  288. (set var value)
  289. (when (featurep 'org)
  290. (org-load-modules-maybe 'force)))
  291. (when (org-bound-and-true-p org-modules)
  292. (let ((a (member 'org-infojs org-modules)))
  293. (and a (setcar a 'org-jsinfo))))
  294. (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)
  295. "Modules that should always be loaded together with org.el.
  296. If a description starts with <C>, the file is not part of Emacs
  297. and loading it will require that you have downloaded and properly installed
  298. the org-mode distribution.
  299. You can also use this system to load external packages (i.e. neither Org
  300. core modules, nor modules from the CONTRIB directory). Just add symbols
  301. to the end of the list. If the package is called org-xyz.el, then you need
  302. to add the symbol `xyz', and the package must have a call to
  303. (provide 'org-xyz)"
  304. :group 'org
  305. :set 'org-set-modules
  306. :type
  307. '(set :greedy t
  308. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  309. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  310. (const :tag " crypt: Encryption of subtrees" org-crypt)
  311. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  312. (const :tag " docview: Links to doc-view buffers" org-docview)
  313. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  314. (const :tag " id: Global IDs for identifying entries" org-id)
  315. (const :tag " info: Links to Info nodes" org-info)
  316. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  317. (const :tag " habit: Track your consistency with habits" org-habit)
  318. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  319. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  320. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  321. (const :tag " mew Links to Mew folders/messages" org-mew)
  322. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  323. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  324. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  325. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  326. (const :tag " vm: Links to VM folders/messages" org-vm)
  327. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  328. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  329. (const :tag " mouse: Additional mouse support" org-mouse)
  330. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  331. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  332. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  333. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  334. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  335. (const :tag "C collector: Collect properties into tables" org-collector)
  336. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  337. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  338. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  339. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  340. (const :tag "C eval: Include command output as text" org-eval)
  341. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  342. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  343. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  344. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  345. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  346. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  347. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  348. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  349. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  350. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  351. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  352. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  353. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  354. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  355. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  356. (const :tag "C registry: A registry for Org-mode links" org-registry)
  357. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  358. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  359. (const :tag "C secretary: Team management with org-mode" org-secretary)
  360. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  361. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  362. (const :tag "C track: Keep up with Org-mode development" org-track)
  363. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  364. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  365. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  366. (defcustom org-support-shift-select nil
  367. "Non-nil means make shift-cursor commands select text when possible.
  368. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  369. start selecting a region, or enlarge regions started in this way.
  370. In Org-mode, in special contexts, these same keys are used for
  371. other purposes, important enough to compete with shift selection.
  372. Org tries to balance these needs by supporting `shift-select-mode'
  373. outside these special contexts, under control of this variable.
  374. The default of this variable is nil, to avoid confusing behavior. Shifted
  375. cursor keys will then execute Org commands in the following contexts:
  376. - on a headline, changing TODO state (left/right) and priority (up/down)
  377. - on a time stamp, changing the time
  378. - in a plain list item, changing the bullet type
  379. - in a property definition line, switching between allowed values
  380. - in the BEGIN line of a clock table (changing the time block).
  381. Outside these contexts, the commands will throw an error.
  382. When this variable is t and the cursor is not in a special
  383. context, Org-mode will support shift-selection for making and
  384. enlarging regions. To make this more effective, the bullet
  385. cycling will no longer happen anywhere in an item line, but only
  386. if the cursor is exactly on the bullet.
  387. If you set this variable to the symbol `always', then the keys
  388. will not be special in headlines, property lines, and item lines,
  389. to make shift selection work there as well. If this is what you
  390. want, you can use the following alternative commands: `C-c C-t'
  391. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  392. can be used to switch TODO sets, `C-c -' to cycle item bullet
  393. types, and properties can be edited by hand or in column view.
  394. However, when the cursor is on a timestamp, shift-cursor commands
  395. will still edit the time stamp - this is just too good to give up.
  396. XEmacs user should have this variable set to nil, because
  397. `shift-select-mode' is in Emacs 23 or later only."
  398. :group 'org
  399. :type '(choice
  400. (const :tag "Never" nil)
  401. (const :tag "When outside special context" t)
  402. (const :tag "Everywhere except timestamps" always)))
  403. (defcustom org-loop-over-headlines-in-active-region nil
  404. "Shall some commands act upon headlines in the active region?
  405. When set to `t', some commands will be performed in all headlines
  406. within the active region.
  407. When set to `start-level', some commands will be performed in all
  408. headlines within the active region, provided that these headlines
  409. are of the same level than the first one.
  410. When set to a string, those commands will be performed on the
  411. matching headlines within the active region. Such string must be
  412. a tags/property/todo match as it is used in the agenda tags view.
  413. The list of commands is: `org-schedule', `org-deadline',
  414. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  415. `org-archive-to-archive-sibling'. The archiving commands skip
  416. already archived entries."
  417. :type '(choice (const :tag "Don't loop" nil)
  418. (const :tag "All headlines in active region" t)
  419. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  420. (string :tag "Tags/Property/Todo matcher"))
  421. :version "24.1"
  422. :group 'org-todo
  423. :group 'org-archive)
  424. (defgroup org-startup nil
  425. "Options concerning startup of Org-mode."
  426. :tag "Org Startup"
  427. :group 'org)
  428. (defcustom org-startup-folded t
  429. "Non-nil means entering Org-mode will switch to OVERVIEW.
  430. This can also be configured on a per-file basis by adding one of
  431. the following lines anywhere in the buffer:
  432. #+STARTUP: fold (or `overview', this is equivalent)
  433. #+STARTUP: nofold (or `showall', this is equivalent)
  434. #+STARTUP: content
  435. #+STARTUP: showeverything"
  436. :group 'org-startup
  437. :type '(choice
  438. (const :tag "nofold: show all" nil)
  439. (const :tag "fold: overview" t)
  440. (const :tag "content: all headlines" content)
  441. (const :tag "show everything, even drawers" showeverything)))
  442. (defcustom org-startup-truncated t
  443. "Non-nil means entering Org-mode will set `truncate-lines'.
  444. This is useful since some lines containing links can be very long and
  445. uninteresting. Also tables look terrible when wrapped."
  446. :group 'org-startup
  447. :type 'boolean)
  448. (defcustom org-startup-indented nil
  449. "Non-nil means turn on `org-indent-mode' on startup.
  450. This can also be configured on a per-file basis by adding one of
  451. the following lines anywhere in the buffer:
  452. #+STARTUP: indent
  453. #+STARTUP: noindent"
  454. :group 'org-structure
  455. :type '(choice
  456. (const :tag "Not" nil)
  457. (const :tag "Globally (slow on startup in large files)" t)))
  458. (defcustom org-use-sub-superscripts t
  459. "Non-nil means interpret \"_\" and \"^\" for export.
  460. When this option is turned on, you can use TeX-like syntax for sub- and
  461. superscripts. Several characters after \"_\" or \"^\" will be
  462. considered as a single item - so grouping with {} is normally not
  463. needed. For example, the following things will be parsed as single
  464. sub- or superscripts.
  465. 10^24 or 10^tau several digits will be considered 1 item.
  466. 10^-12 or 10^-tau a leading sign with digits or a word
  467. x^2-y^3 will be read as x^2 - y^3, because items are
  468. terminated by almost any nonword/nondigit char.
  469. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  470. Still, ambiguity is possible - so when in doubt use {} to enclose the
  471. sub/superscript. If you set this variable to the symbol `{}',
  472. the braces are *required* in order to trigger interpretations as
  473. sub/superscript. This can be helpful in documents that need \"_\"
  474. frequently in plain text.
  475. Not all export backends support this, but HTML does.
  476. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  477. :group 'org-startup
  478. :group 'org-export-translation
  479. :version "24.1"
  480. :type '(choice
  481. (const :tag "Always interpret" t)
  482. (const :tag "Only with braces" {})
  483. (const :tag "Never interpret" nil)))
  484. (if (fboundp 'defvaralias)
  485. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  486. (defcustom org-startup-with-beamer-mode nil
  487. "Non-nil means turn on `org-beamer-mode' on startup.
  488. This can also be configured on a per-file basis by adding one of
  489. the following lines anywhere in the buffer:
  490. #+STARTUP: beamer"
  491. :group 'org-startup
  492. :version "24.1"
  493. :type 'boolean)
  494. (defcustom org-startup-align-all-tables nil
  495. "Non-nil means align all tables when visiting a file.
  496. This is useful when the column width in tables is forced with <N> cookies
  497. in table fields. Such tables will look correct only after the first re-align.
  498. This can also be configured on a per-file basis by adding one of
  499. the following lines anywhere in the buffer:
  500. #+STARTUP: align
  501. #+STARTUP: noalign"
  502. :group 'org-startup
  503. :type 'boolean)
  504. (defcustom org-startup-with-inline-images nil
  505. "Non-nil means show inline images when loading a new Org file.
  506. This can also be configured on a per-file basis by adding one of
  507. the following lines anywhere in the buffer:
  508. #+STARTUP: inlineimages
  509. #+STARTUP: noinlineimages"
  510. :group 'org-startup
  511. :version "24.1"
  512. :type 'boolean)
  513. (defcustom org-insert-mode-line-in-empty-file nil
  514. "Non-nil means insert the first line setting Org-mode in empty files.
  515. When the function `org-mode' is called interactively in an empty file, this
  516. normally means that the file name does not automatically trigger Org-mode.
  517. To ensure that the file will always be in Org-mode in the future, a
  518. line enforcing Org-mode will be inserted into the buffer, if this option
  519. has been set."
  520. :group 'org-startup
  521. :type 'boolean)
  522. (defcustom org-replace-disputed-keys nil
  523. "Non-nil means use alternative key bindings for some keys.
  524. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  525. These keys are also used by other packages like shift-selection-mode'
  526. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  527. If you want to use Org-mode together with one of these other modes,
  528. or more generally if you would like to move some Org-mode commands to
  529. other keys, set this variable and configure the keys with the variable
  530. `org-disputed-keys'.
  531. This option is only relevant at load-time of Org-mode, and must be set
  532. *before* org.el is loaded. Changing it requires a restart of Emacs to
  533. become effective."
  534. :group 'org-startup
  535. :type 'boolean)
  536. (defcustom org-use-extra-keys nil
  537. "Non-nil means use extra key sequence definitions for certain commands.
  538. This happens automatically if you run XEmacs or if `window-system'
  539. is nil. This variable lets you do the same manually. You must
  540. set it before loading org.
  541. Example: on Carbon Emacs 22 running graphically, with an external
  542. keyboard on a Powerbook, the default way of setting M-left might
  543. not work for either Alt or ESC. Setting this variable will make
  544. it work for ESC."
  545. :group 'org-startup
  546. :type 'boolean)
  547. (if (fboundp 'defvaralias)
  548. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  549. (defcustom org-disputed-keys
  550. '(([(shift up)] . [(meta p)])
  551. ([(shift down)] . [(meta n)])
  552. ([(shift left)] . [(meta -)])
  553. ([(shift right)] . [(meta +)])
  554. ([(control shift right)] . [(meta shift +)])
  555. ([(control shift left)] . [(meta shift -)]))
  556. "Keys for which Org-mode and other modes compete.
  557. This is an alist, cars are the default keys, second element specifies
  558. the alternative to use when `org-replace-disputed-keys' is t.
  559. Keys can be specified in any syntax supported by `define-key'.
  560. The value of this option takes effect only at Org-mode's startup,
  561. therefore you'll have to restart Emacs to apply it after changing."
  562. :group 'org-startup
  563. :type 'alist)
  564. (defun org-key (key)
  565. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  566. Or return the original if not disputed.
  567. Also apply the translations defined in `org-xemacs-key-equivalents'."
  568. (when org-replace-disputed-keys
  569. (let* ((nkey (key-description key))
  570. (x (org-find-if (lambda (x)
  571. (equal (key-description (car x)) nkey))
  572. org-disputed-keys)))
  573. (setq key (if x (cdr x) key))))
  574. (when (featurep 'xemacs)
  575. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  576. key)
  577. (defun org-find-if (predicate seq)
  578. (catch 'exit
  579. (while seq
  580. (if (funcall predicate (car seq))
  581. (throw 'exit (car seq))
  582. (pop seq)))))
  583. (defun org-defkey (keymap key def)
  584. "Define a key, possibly translated, as returned by `org-key'."
  585. (define-key keymap (org-key key) def))
  586. (defcustom org-ellipsis nil
  587. "The ellipsis to use in the Org-mode outline.
  588. When nil, just use the standard three dots. When a string, use that instead,
  589. When a face, use the standard 3 dots, but with the specified face.
  590. The change affects only Org-mode (which will then use its own display table).
  591. Changing this requires executing `M-x org-mode' in a buffer to become
  592. effective."
  593. :group 'org-startup
  594. :type '(choice (const :tag "Default" nil)
  595. (face :tag "Face" :value org-warning)
  596. (string :tag "String" :value "...#")))
  597. (defvar org-display-table nil
  598. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  599. (defgroup org-keywords nil
  600. "Keywords in Org-mode."
  601. :tag "Org Keywords"
  602. :group 'org)
  603. (defcustom org-deadline-string "DEADLINE:"
  604. "String to mark deadline entries.
  605. A deadline is this string, followed by a time stamp. Should be a word,
  606. terminated by a colon. You can insert a schedule keyword and
  607. a timestamp with \\[org-deadline].
  608. Changes become only effective after restarting Emacs."
  609. :group 'org-keywords
  610. :type 'string)
  611. (defcustom org-scheduled-string "SCHEDULED:"
  612. "String to mark scheduled TODO entries.
  613. A schedule is this string, followed by a time stamp. Should be a word,
  614. terminated by a colon. You can insert a schedule keyword and
  615. a timestamp with \\[org-schedule].
  616. Changes become only effective after restarting Emacs."
  617. :group 'org-keywords
  618. :type 'string)
  619. (defcustom org-closed-string "CLOSED:"
  620. "String used as the prefix for timestamps logging closing a TODO entry."
  621. :group 'org-keywords
  622. :type 'string)
  623. (defcustom org-clock-string "CLOCK:"
  624. "String used as prefix for timestamps clocking work hours on an item."
  625. :group 'org-keywords
  626. :type 'string)
  627. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  628. org-scheduled-string "\\|"
  629. org-deadline-string "\\|"
  630. org-closed-string "\\|"
  631. org-clock-string "\\)")
  632. "Matches a line with planning or clock info.")
  633. (defcustom org-comment-string "COMMENT"
  634. "Entries starting with this keyword will never be exported.
  635. An entry can be toggled between COMMENT and normal with
  636. \\[org-toggle-comment].
  637. Changes become only effective after restarting Emacs."
  638. :group 'org-keywords
  639. :type 'string)
  640. (defcustom org-quote-string "QUOTE"
  641. "Entries starting with this keyword will be exported in fixed-width font.
  642. Quoting applies only to the text in the entry following the headline, and does
  643. not extend beyond the next headline, even if that is lower level.
  644. An entry can be toggled between QUOTE and normal with
  645. \\[org-toggle-fixed-width-section]."
  646. :group 'org-keywords
  647. :type 'string)
  648. (defconst org-repeat-re
  649. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  650. "Regular expression for specifying repeated events.
  651. After a match, group 1 contains the repeat expression.")
  652. (defgroup org-structure nil
  653. "Options concerning the general structure of Org-mode files."
  654. :tag "Org Structure"
  655. :group 'org)
  656. (defgroup org-reveal-location nil
  657. "Options about how to make context of a location visible."
  658. :tag "Org Reveal Location"
  659. :group 'org-structure)
  660. (defconst org-context-choice
  661. '(choice
  662. (const :tag "Always" t)
  663. (const :tag "Never" nil)
  664. (repeat :greedy t :tag "Individual contexts"
  665. (cons
  666. (choice :tag "Context"
  667. (const agenda)
  668. (const org-goto)
  669. (const occur-tree)
  670. (const tags-tree)
  671. (const link-search)
  672. (const mark-goto)
  673. (const bookmark-jump)
  674. (const isearch)
  675. (const default))
  676. (boolean))))
  677. "Contexts for the reveal options.")
  678. (defcustom org-show-hierarchy-above '((default . t))
  679. "Non-nil means show full hierarchy when revealing a location.
  680. Org-mode often shows locations in an org-mode file which might have
  681. been invisible before. When this is set, the hierarchy of headings
  682. above the exposed location is shown.
  683. Turning this off for example for sparse trees makes them very compact.
  684. Instead of t, this can also be an alist specifying this option for different
  685. contexts. Valid contexts are
  686. agenda when exposing an entry from the agenda
  687. org-goto when using the command `org-goto' on key C-c C-j
  688. occur-tree when using the command `org-occur' on key C-c /
  689. tags-tree when constructing a sparse tree based on tags matches
  690. link-search when exposing search matches associated with a link
  691. mark-goto when exposing the jump goal of a mark
  692. bookmark-jump when exposing a bookmark location
  693. isearch when exiting from an incremental search
  694. default default for all contexts not set explicitly"
  695. :group 'org-reveal-location
  696. :type org-context-choice)
  697. (defcustom org-show-following-heading '((default . nil))
  698. "Non-nil means show following heading when revealing a location.
  699. Org-mode often shows locations in an org-mode file which might have
  700. been invisible before. When this is set, the heading following the
  701. match is shown.
  702. Turning this off for example for sparse trees makes them very compact,
  703. but makes it harder to edit the location of the match. In such a case,
  704. use the command \\[org-reveal] to show more context.
  705. Instead of t, this can also be an alist specifying this option for different
  706. contexts. See `org-show-hierarchy-above' for valid contexts."
  707. :group 'org-reveal-location
  708. :type org-context-choice)
  709. (defcustom org-show-siblings '((default . nil) (isearch t))
  710. "Non-nil means show all sibling heading when revealing a location.
  711. Org-mode often shows locations in an org-mode file which might have
  712. been invisible before. When this is set, the sibling of the current entry
  713. heading are all made visible. If `org-show-hierarchy-above' is t,
  714. the same happens on each level of the hierarchy above the current entry.
  715. By default this is on for the isearch context, off for all other contexts.
  716. Turning this off for example for sparse trees makes them very compact,
  717. but makes it harder to edit the location of the match. In such a case,
  718. use the command \\[org-reveal] to show more context.
  719. Instead of t, this can also be an alist specifying this option for different
  720. contexts. See `org-show-hierarchy-above' for valid contexts."
  721. :group 'org-reveal-location
  722. :type org-context-choice)
  723. (defcustom org-show-entry-below '((default . nil))
  724. "Non-nil means show the entry below a headline when revealing a location.
  725. Org-mode often shows locations in an org-mode file which might have
  726. been invisible before. When this is set, the text below the headline that is
  727. exposed is also shown.
  728. By default this is off for all contexts.
  729. Instead of t, this can also be an alist specifying this option for different
  730. contexts. See `org-show-hierarchy-above' for valid contexts."
  731. :group 'org-reveal-location
  732. :type org-context-choice)
  733. (defcustom org-indirect-buffer-display 'other-window
  734. "How should indirect tree buffers be displayed?
  735. This applies to indirect buffers created with the commands
  736. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  737. Valid values are:
  738. current-window Display in the current window
  739. other-window Just display in another window.
  740. dedicated-frame Create one new frame, and re-use it each time.
  741. new-frame Make a new frame each time. Note that in this case
  742. previously-made indirect buffers are kept, and you need to
  743. kill these buffers yourself."
  744. :group 'org-structure
  745. :group 'org-agenda-windows
  746. :type '(choice
  747. (const :tag "In current window" current-window)
  748. (const :tag "In current frame, other window" other-window)
  749. (const :tag "Each time a new frame" new-frame)
  750. (const :tag "One dedicated frame" dedicated-frame)))
  751. (defcustom org-use-speed-commands nil
  752. "Non-nil means activate single letter commands at beginning of a headline.
  753. This may also be a function to test for appropriate locations where speed
  754. commands should be active."
  755. :group 'org-structure
  756. :type '(choice
  757. (const :tag "Never" nil)
  758. (const :tag "At beginning of headline stars" t)
  759. (function)))
  760. (defcustom org-speed-commands-user nil
  761. "Alist of additional speed commands.
  762. This list will be checked before `org-speed-commands-default'
  763. when the variable `org-use-speed-commands' is non-nil
  764. and when the cursor is at the beginning of a headline.
  765. The car if each entry is a string with a single letter, which must
  766. be assigned to `self-insert-command' in the global map.
  767. The cdr is either a command to be called interactively, a function
  768. to be called, or a form to be evaluated.
  769. An entry that is just a list with a single string will be interpreted
  770. as a descriptive headline that will be added when listing the speed
  771. commands in the Help buffer using the `?' speed command."
  772. :group 'org-structure
  773. :type '(repeat :value ("k" . ignore)
  774. (choice :value ("k" . ignore)
  775. (list :tag "Descriptive Headline" (string :tag "Headline"))
  776. (cons :tag "Letter and Command"
  777. (string :tag "Command letter")
  778. (choice
  779. (function)
  780. (sexp))))))
  781. (defgroup org-cycle nil
  782. "Options concerning visibility cycling in Org-mode."
  783. :tag "Org Cycle"
  784. :group 'org-structure)
  785. (defcustom org-cycle-skip-children-state-if-no-children t
  786. "Non-nil means skip CHILDREN state in entries that don't have any."
  787. :group 'org-cycle
  788. :type 'boolean)
  789. (defcustom org-cycle-max-level nil
  790. "Maximum level which should still be subject to visibility cycling.
  791. Levels higher than this will, for cycling, be treated as text, not a headline.
  792. When `org-odd-levels-only' is set, a value of N in this variable actually
  793. means 2N-1 stars as the limiting headline.
  794. When nil, cycle all levels.
  795. Note that the limiting level of cycling is also influenced by
  796. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  797. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  798. than its value."
  799. :group 'org-cycle
  800. :type '(choice
  801. (const :tag "No limit" nil)
  802. (integer :tag "Maximum level")))
  803. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  804. "Names of drawers. Drawers are not opened by cycling on the headline above.
  805. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  806. this:
  807. :DRAWERNAME:
  808. .....
  809. :END:
  810. The drawer \"PROPERTIES\" is special for capturing properties through
  811. the property API.
  812. Drawers can be defined on the per-file basis with a line like:
  813. #+DRAWERS: HIDDEN STATE PROPERTIES"
  814. :group 'org-structure
  815. :group 'org-cycle
  816. :type '(repeat (string :tag "Drawer Name")))
  817. (defcustom org-hide-block-startup nil
  818. "Non-nil means entering Org-mode will fold all blocks.
  819. This can also be set in on a per-file basis with
  820. #+STARTUP: hideblocks
  821. #+STARTUP: showblocks"
  822. :group 'org-startup
  823. :group 'org-cycle
  824. :type 'boolean)
  825. (defcustom org-cycle-global-at-bob nil
  826. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  827. This makes it possible to do global cycling without having to use S-TAB or
  828. \\[universal-argument] TAB. For this special case to work, the first line
  829. of the buffer must not be a headline -- it may be empty or some other text.
  830. When used in this way, `org-cycle-hook' is disabled temporarily to make
  831. sure the cursor stays at the beginning of the buffer. When this option is
  832. nil, don't do anything special at the beginning of the buffer."
  833. :group 'org-cycle
  834. :type 'boolean)
  835. (defcustom org-cycle-level-after-item/entry-creation t
  836. "Non-nil means cycle entry level or item indentation in new empty entries.
  837. When the cursor is at the end of an empty headline, i.e with only stars
  838. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  839. and then all possible ancestor states, before returning to the original state.
  840. This makes data entry extremely fast: M-RET to create a new headline,
  841. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  842. When the cursor is at the end of an empty plain list item, one TAB will
  843. make it a subitem, two or more tabs will back up to make this an item
  844. higher up in the item hierarchy."
  845. :group 'org-cycle
  846. :type 'boolean)
  847. (defcustom org-cycle-emulate-tab t
  848. "Where should `org-cycle' emulate TAB.
  849. nil Never
  850. white Only in completely white lines
  851. whitestart Only at the beginning of lines, before the first non-white char
  852. t Everywhere except in headlines
  853. exc-hl-bol Everywhere except at the start of a headline
  854. If TAB is used in a place where it does not emulate TAB, the current subtree
  855. visibility is cycled."
  856. :group 'org-cycle
  857. :type '(choice (const :tag "Never" nil)
  858. (const :tag "Only in completely white lines" white)
  859. (const :tag "Before first char in a line" whitestart)
  860. (const :tag "Everywhere except in headlines" t)
  861. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  862. ))
  863. (defcustom org-cycle-separator-lines 2
  864. "Number of empty lines needed to keep an empty line between collapsed trees.
  865. If you leave an empty line between the end of a subtree and the following
  866. headline, this empty line is hidden when the subtree is folded.
  867. Org-mode will leave (exactly) one empty line visible if the number of
  868. empty lines is equal or larger to the number given in this variable.
  869. So the default 2 means at least 2 empty lines after the end of a subtree
  870. are needed to produce free space between a collapsed subtree and the
  871. following headline.
  872. If the number is negative, and the number of empty lines is at least -N,
  873. all empty lines are shown.
  874. Special case: when 0, never leave empty lines in collapsed view."
  875. :group 'org-cycle
  876. :type 'integer)
  877. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  878. (defcustom org-pre-cycle-hook nil
  879. "Hook that is run before visibility cycling is happening.
  880. The function(s) in this hook must accept a single argument which indicates
  881. the new state that will be set right after running this hook. The
  882. argument is a symbol. Before a global state change, it can have the values
  883. `overview', `content', or `all'. Before a local state change, it can have
  884. the values `folded', `children', or `subtree'."
  885. :group 'org-cycle
  886. :type 'hook)
  887. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  888. org-cycle-hide-drawers
  889. org-cycle-hide-inline-tasks
  890. org-cycle-show-empty-lines
  891. org-optimize-window-after-visibility-change)
  892. "Hook that is run after `org-cycle' has changed the buffer visibility.
  893. The function(s) in this hook must accept a single argument which indicates
  894. the new state that was set by the most recent `org-cycle' command. The
  895. argument is a symbol. After a global state change, it can have the values
  896. `overview', `contents', or `all'. After a local state change, it can have
  897. the values `folded', `children', or `subtree'."
  898. :group 'org-cycle
  899. :type 'hook)
  900. (defgroup org-edit-structure nil
  901. "Options concerning structure editing in Org-mode."
  902. :tag "Org Edit Structure"
  903. :group 'org-structure)
  904. (defcustom org-odd-levels-only nil
  905. "Non-nil means skip even levels and only use odd levels for the outline.
  906. This has the effect that two stars are being added/taken away in
  907. promotion/demotion commands. It also influences how levels are
  908. handled by the exporters.
  909. Changing it requires restart of `font-lock-mode' to become effective
  910. for fontification also in regions already fontified.
  911. You may also set this on a per-file basis by adding one of the following
  912. lines to the buffer:
  913. #+STARTUP: odd
  914. #+STARTUP: oddeven"
  915. :group 'org-edit-structure
  916. :group 'org-appearance
  917. :type 'boolean)
  918. (defcustom org-adapt-indentation t
  919. "Non-nil means adapt indentation to outline node level.
  920. When this variable is set, Org assumes that you write outlines by
  921. indenting text in each node to align with the headline (after the stars).
  922. The following issues are influenced by this variable:
  923. - When this is set and the *entire* text in an entry is indented, the
  924. indentation is increased by one space in a demotion command, and
  925. decreased by one in a promotion command. If any line in the entry
  926. body starts with text at column 0, indentation is not changed at all.
  927. - Property drawers and planning information is inserted indented when
  928. this variable s set. When nil, they will not be indented.
  929. - TAB indents a line relative to context. The lines below a headline
  930. will be indented when this variable is set.
  931. Note that this is all about true indentation, by adding and removing
  932. space characters. See also `org-indent.el' which does level-dependent
  933. indentation in a virtual way, i.e. at display time in Emacs."
  934. :group 'org-edit-structure
  935. :type 'boolean)
  936. (defcustom org-special-ctrl-a/e nil
  937. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  938. When t, `C-a' will bring back the cursor to the beginning of the
  939. headline text, i.e. after the stars and after a possible TODO
  940. keyword. In an item, this will be the position after bullet and
  941. check-box, if any. When the cursor is already at that position,
  942. another `C-a' will bring it to the beginning of the line.
  943. `C-e' will jump to the end of the headline, ignoring the presence
  944. of tags in the headline. A second `C-e' will then jump to the
  945. true end of the line, after any tags. This also means that, when
  946. this variable is non-nil, `C-e' also will never jump beyond the
  947. end of the heading of a folded section, i.e. not after the
  948. ellipses.
  949. When set to the symbol `reversed', the first `C-a' or `C-e' works
  950. normally, going to the true line boundary first. Only a directly
  951. following, identical keypress will bring the cursor to the
  952. special positions.
  953. This may also be a cons cell where the behavior for `C-a' and
  954. `C-e' is set separately."
  955. :group 'org-edit-structure
  956. :type '(choice
  957. (const :tag "off" nil)
  958. (const :tag "on: after stars/bullet and before tags first" t)
  959. (const :tag "reversed: true line boundary first" reversed)
  960. (cons :tag "Set C-a and C-e separately"
  961. (choice :tag "Special C-a"
  962. (const :tag "off" nil)
  963. (const :tag "on: after stars/bullet first" t)
  964. (const :tag "reversed: before stars/bullet first" reversed))
  965. (choice :tag "Special C-e"
  966. (const :tag "off" nil)
  967. (const :tag "on: before tags first" t)
  968. (const :tag "reversed: after tags first" reversed)))))
  969. (if (fboundp 'defvaralias)
  970. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  971. (defcustom org-special-ctrl-k nil
  972. "Non-nil means `C-k' will behave specially in headlines.
  973. When nil, `C-k' will call the default `kill-line' command.
  974. When t, the following will happen while the cursor is in the headline:
  975. - When the cursor is at the beginning of a headline, kill the entire
  976. line and possible the folded subtree below the line.
  977. - When in the middle of the headline text, kill the headline up to the tags.
  978. - When after the headline text, kill the tags."
  979. :group 'org-edit-structure
  980. :type 'boolean)
  981. (defcustom org-ctrl-k-protect-subtree nil
  982. "Non-nil means, do not delete a hidden subtree with C-k.
  983. When set to the symbol `error', simply throw an error when C-k is
  984. used to kill (part-of) a headline that has hidden text behind it.
  985. Any other non-nil value will result in a query to the user, if it is
  986. OK to kill that hidden subtree. When nil, kill without remorse."
  987. :group 'org-edit-structure
  988. :version "24.1"
  989. :type '(choice
  990. (const :tag "Do not protect hidden subtrees" nil)
  991. (const :tag "Protect hidden subtrees with a security query" t)
  992. (const :tag "Never kill a hidden subtree with C-k" error)))
  993. (defcustom org-catch-invisible-edits nil
  994. "Check if in invisible region before inserting or deleting a character.
  995. Valid values are:
  996. nil Do not check, so just do invisible edits.
  997. error Throw an error and do nothing.
  998. show Make point visible, and do the requested edit.
  999. show-and-error Make point visible, then throw an error and abort the edit.
  1000. smart Make point visible, and do insertion/deletion if it is
  1001. adjacent to visible text and the change feels predictable.
  1002. Never delete a previously invisible character or add in the
  1003. middle or right after an invisible region. Basically, this
  1004. allows insertion and backward-delete right before ellipses.
  1005. FIXME: maybe in this case we should not even show?"
  1006. :group 'org-edit-structure
  1007. :version "24.1"
  1008. :type '(choice
  1009. (const :tag "Do not check" nil)
  1010. (const :tag "Throw error when trying to edit" error)
  1011. (const :tag "Unhide, but do not do the edit" show-and-error)
  1012. (const :tag "Show invisible part and do the edit" show)
  1013. (const :tag "Be smart and do the right thing" smart)))
  1014. (defcustom org-yank-folded-subtrees t
  1015. "Non-nil means when yanking subtrees, fold them.
  1016. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1017. it starts with a heading and all other headings in it are either children
  1018. or siblings, then fold all the subtrees. However, do this only if no
  1019. text after the yank would be swallowed into a folded tree by this action."
  1020. :group 'org-edit-structure
  1021. :type 'boolean)
  1022. (defcustom org-yank-adjusted-subtrees nil
  1023. "Non-nil means when yanking subtrees, adjust the level.
  1024. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1025. this function for details."
  1026. :group 'org-edit-structure
  1027. :type 'boolean)
  1028. (defcustom org-M-RET-may-split-line '((default . t))
  1029. "Non-nil means M-RET will split the line at the cursor position.
  1030. When nil, it will go to the end of the line before making a
  1031. new line.
  1032. You may also set this option in a different way for different
  1033. contexts. Valid contexts are:
  1034. headline when creating a new headline
  1035. item when creating a new item
  1036. table in a table field
  1037. default the value to be used for all contexts not explicitly
  1038. customized"
  1039. :group 'org-structure
  1040. :group 'org-table
  1041. :type '(choice
  1042. (const :tag "Always" t)
  1043. (const :tag "Never" nil)
  1044. (repeat :greedy t :tag "Individual contexts"
  1045. (cons
  1046. (choice :tag "Context"
  1047. (const headline)
  1048. (const item)
  1049. (const table)
  1050. (const default))
  1051. (boolean)))))
  1052. (defcustom org-insert-heading-respect-content nil
  1053. "Non-nil means insert new headings after the current subtree.
  1054. When nil, the new heading is created directly after the current line.
  1055. The commands \\[org-insert-heading-respect-content] and
  1056. \\[org-insert-todo-heading-respect-content] turn this variable on
  1057. for the duration of the command."
  1058. :group 'org-structure
  1059. :type 'boolean)
  1060. (defcustom org-blank-before-new-entry '((heading . auto)
  1061. (plain-list-item . auto))
  1062. "Should `org-insert-heading' leave a blank line before new heading/item?
  1063. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1064. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1065. which case Org will look at the surrounding headings/items and try to
  1066. make an intelligent decision whether to insert a blank line or not.
  1067. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1068. set, the setting here is ignored and no empty line is inserted, to avoid
  1069. breaking the list structure."
  1070. :group 'org-edit-structure
  1071. :type '(list
  1072. (cons (const heading)
  1073. (choice (const :tag "Never" nil)
  1074. (const :tag "Always" t)
  1075. (const :tag "Auto" auto)))
  1076. (cons (const plain-list-item)
  1077. (choice (const :tag "Never" nil)
  1078. (const :tag "Always" t)
  1079. (const :tag "Auto" auto)))))
  1080. (defcustom org-insert-heading-hook nil
  1081. "Hook being run after inserting a new heading."
  1082. :group 'org-edit-structure
  1083. :type 'hook)
  1084. (defcustom org-enable-fixed-width-editor t
  1085. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1086. This currently only means they are never auto-wrapped.
  1087. When nil, such lines will be treated like ordinary lines.
  1088. See also the QUOTE keyword."
  1089. :group 'org-edit-structure
  1090. :type 'boolean)
  1091. (defcustom org-goto-auto-isearch t
  1092. "Non-nil means typing characters in `org-goto' starts incremental search."
  1093. :group 'org-edit-structure
  1094. :type 'boolean)
  1095. (defgroup org-sparse-trees nil
  1096. "Options concerning sparse trees in Org-mode."
  1097. :tag "Org Sparse Trees"
  1098. :group 'org-structure)
  1099. (defcustom org-highlight-sparse-tree-matches t
  1100. "Non-nil means highlight all matches that define a sparse tree.
  1101. The highlights will automatically disappear the next time the buffer is
  1102. changed by an edit command."
  1103. :group 'org-sparse-trees
  1104. :type 'boolean)
  1105. (defcustom org-remove-highlights-with-change t
  1106. "Non-nil means any change to the buffer will remove temporary highlights.
  1107. Such highlights are created by `org-occur' and `org-clock-display'.
  1108. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1109. The highlights created by `org-preview-latex-fragment' always need
  1110. `C-c C-c' to be removed."
  1111. :group 'org-sparse-trees
  1112. :group 'org-time
  1113. :type 'boolean)
  1114. (defcustom org-occur-hook '(org-first-headline-recenter)
  1115. "Hook that is run after `org-occur' has constructed a sparse tree.
  1116. This can be used to recenter the window to show as much of the structure
  1117. as possible."
  1118. :group 'org-sparse-trees
  1119. :type 'hook)
  1120. (defgroup org-imenu-and-speedbar nil
  1121. "Options concerning imenu and speedbar in Org-mode."
  1122. :tag "Org Imenu and Speedbar"
  1123. :group 'org-structure)
  1124. (defcustom org-imenu-depth 2
  1125. "The maximum level for Imenu access to Org-mode headlines.
  1126. This also applied for speedbar access."
  1127. :group 'org-imenu-and-speedbar
  1128. :type 'integer)
  1129. (defgroup org-table nil
  1130. "Options concerning tables in Org-mode."
  1131. :tag "Org Table"
  1132. :group 'org)
  1133. (defcustom org-enable-table-editor 'optimized
  1134. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1135. When nil, such lines will be treated like ordinary lines.
  1136. When equal to the symbol `optimized', the table editor will be optimized to
  1137. do the following:
  1138. - Automatic overwrite mode in front of whitespace in table fields.
  1139. This makes the structure of the table stay in tact as long as the edited
  1140. field does not exceed the column width.
  1141. - Minimize the number of realigns. Normally, the table is aligned each time
  1142. TAB or RET are pressed to move to another field. With optimization this
  1143. happens only if changes to a field might have changed the column width.
  1144. Optimization requires replacing the functions `self-insert-command',
  1145. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1146. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1147. very good at guessing when a re-align will be necessary, but you can always
  1148. force one with \\[org-ctrl-c-ctrl-c].
  1149. If you would like to use the optimized version in Org-mode, but the
  1150. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1151. This variable can be used to turn on and off the table editor during a session,
  1152. but in order to toggle optimization, a restart is required.
  1153. See also the variable `org-table-auto-blank-field'."
  1154. :group 'org-table
  1155. :type '(choice
  1156. (const :tag "off" nil)
  1157. (const :tag "on" t)
  1158. (const :tag "on, optimized" optimized)))
  1159. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1160. (version<= emacs-version "24.1"))
  1161. "Non-nil means cluster self-insert commands for undo when possible.
  1162. If this is set, then, like in the Emacs command loop, 20 consecutive
  1163. characters will be undone together.
  1164. This is configurable, because there is some impact on typing performance."
  1165. :group 'org-table
  1166. :type 'boolean)
  1167. (defcustom org-table-tab-recognizes-table.el t
  1168. "Non-nil means TAB will automatically notice a table.el table.
  1169. When it sees such a table, it moves point into it and - if necessary -
  1170. calls `table-recognize-table'."
  1171. :group 'org-table-editing
  1172. :type 'boolean)
  1173. (defgroup org-link nil
  1174. "Options concerning links in Org-mode."
  1175. :tag "Org Link"
  1176. :group 'org)
  1177. (defvar org-link-abbrev-alist-local nil
  1178. "Buffer-local version of `org-link-abbrev-alist', which see.
  1179. The value of this is taken from the #+LINK lines.")
  1180. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1181. (defcustom org-link-abbrev-alist nil
  1182. "Alist of link abbreviations.
  1183. The car of each element is a string, to be replaced at the start of a link.
  1184. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1185. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1186. [[linkkey:tag][description]]
  1187. The 'linkkey' must be a word word, starting with a letter, followed
  1188. by letters, numbers, '-' or '_'.
  1189. If REPLACE is a string, the tag will simply be appended to create the link.
  1190. If the string contains \"%s\", the tag will be inserted there. If the string
  1191. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1192. at that point (see the function `url-hexify-string'). If the string contains
  1193. the specifier \"%(my-function)\", then the custom function `my-function' will
  1194. be invoked: this function takes the tag as its only argument and must return
  1195. a string.
  1196. REPLACE may also be a function that will be called with the tag as the
  1197. only argument to create the link, which should be returned as a string.
  1198. See the manual for examples."
  1199. :group 'org-link
  1200. :type '(repeat
  1201. (cons
  1202. (string :tag "Protocol")
  1203. (choice
  1204. (string :tag "Format")
  1205. (function)))))
  1206. (defcustom org-descriptive-links t
  1207. "Non-nil means Org will display descriptive links.
  1208. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1209. \"Org Website\", hiding the link itself and just displaying its
  1210. description. When set to `nil', Org will display the full links
  1211. literally.
  1212. You can interactively set the value of this variable by calling
  1213. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1214. :group 'org-link
  1215. :type 'boolean)
  1216. (defcustom org-link-file-path-type 'adaptive
  1217. "How the path name in file links should be stored.
  1218. Valid values are:
  1219. relative Relative to the current directory, i.e. the directory of the file
  1220. into which the link is being inserted.
  1221. absolute Absolute path, if possible with ~ for home directory.
  1222. noabbrev Absolute path, no abbreviation of home directory.
  1223. adaptive Use relative path for files in the current directory and sub-
  1224. directories of it. For other files, use an absolute path."
  1225. :group 'org-link
  1226. :type '(choice
  1227. (const relative)
  1228. (const absolute)
  1229. (const noabbrev)
  1230. (const adaptive)))
  1231. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1232. "Types of links that should be activated in Org-mode files.
  1233. This is a list of symbols, each leading to the activation of a certain link
  1234. type. In principle, it does not hurt to turn on most link types - there may
  1235. be a small gain when turning off unused link types. The types are:
  1236. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1237. angle Links in angular brackets that may contain whitespace like
  1238. <bbdb:Carsten Dominik>.
  1239. plain Plain links in normal text, no whitespace, like http://google.com.
  1240. radio Text that is matched by a radio target, see manual for details.
  1241. tag Tag settings in a headline (link to tag search).
  1242. date Time stamps (link to calendar).
  1243. footnote Footnote labels.
  1244. Changing this variable requires a restart of Emacs to become effective."
  1245. :group 'org-link
  1246. :type '(set :greedy t
  1247. (const :tag "Double bracket links" bracket)
  1248. (const :tag "Angular bracket links" angle)
  1249. (const :tag "Plain text links" plain)
  1250. (const :tag "Radio target matches" radio)
  1251. (const :tag "Tags" tag)
  1252. (const :tag "Timestamps" date)
  1253. (const :tag "Footnotes" footnote)))
  1254. (defcustom org-make-link-description-function nil
  1255. "Function to use for generating link descriptions from links.
  1256. When nil, the link location will be used. This function must take
  1257. two parameters: the first one is the link, the second one is the
  1258. description generated by `org-insert-link'. The function should
  1259. return the description to use."
  1260. :group 'org-link
  1261. :type 'function)
  1262. (defgroup org-link-store nil
  1263. "Options concerning storing links in Org-mode."
  1264. :tag "Org Store Link"
  1265. :group 'org-link)
  1266. (defcustom org-url-hexify-p t
  1267. "When non-nil, hexify URL when creating a link."
  1268. :type 'boolean
  1269. :version "24.3"
  1270. :group 'org-link-store)
  1271. (defcustom org-email-link-description-format "Email %c: %.30s"
  1272. "Format of the description part of a link to an email or usenet message.
  1273. The following %-escapes will be replaced by corresponding information:
  1274. %F full \"From\" field
  1275. %f name, taken from \"From\" field, address if no name
  1276. %T full \"To\" field
  1277. %t first name in \"To\" field, address if no name
  1278. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1279. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1280. %s subject
  1281. %d date
  1282. %m message-id.
  1283. You may use normal field width specification between the % and the letter.
  1284. This is for example useful to limit the length of the subject.
  1285. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1286. :group 'org-link-store
  1287. :type 'string)
  1288. (defcustom org-from-is-user-regexp
  1289. (let (r1 r2)
  1290. (when (and user-mail-address (not (string= user-mail-address "")))
  1291. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1292. (when (and user-full-name (not (string= user-full-name "")))
  1293. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1294. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1295. "Regexp matched against the \"From:\" header of an email or usenet message.
  1296. It should match if the message is from the user him/herself."
  1297. :group 'org-link-store
  1298. :type 'regexp)
  1299. (defcustom org-context-in-file-links t
  1300. "Non-nil means file links from `org-store-link' contain context.
  1301. A search string will be added to the file name with :: as separator and
  1302. used to find the context when the link is activated by the command
  1303. `org-open-at-point'. When this option is t, the entire active region
  1304. will be placed in the search string of the file link. If set to a
  1305. positive integer, only the first n lines of context will be stored.
  1306. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1307. negates this setting for the duration of the command."
  1308. :group 'org-link-store
  1309. :type '(choice boolean integer))
  1310. (defcustom org-keep-stored-link-after-insertion nil
  1311. "Non-nil means keep link in list for entire session.
  1312. The command `org-store-link' adds a link pointing to the current
  1313. location to an internal list. These links accumulate during a session.
  1314. The command `org-insert-link' can be used to insert links into any
  1315. Org-mode file (offering completion for all stored links). When this
  1316. option is nil, every link which has been inserted once using \\[org-insert-link]
  1317. will be removed from the list, to make completing the unused links
  1318. more efficient."
  1319. :group 'org-link-store
  1320. :type 'boolean)
  1321. (defgroup org-link-follow nil
  1322. "Options concerning following links in Org-mode."
  1323. :tag "Org Follow Link"
  1324. :group 'org-link)
  1325. (defcustom org-link-translation-function nil
  1326. "Function to translate links with different syntax to Org syntax.
  1327. This can be used to translate links created for example by the Planner
  1328. or emacs-wiki packages to Org syntax.
  1329. The function must accept two parameters, a TYPE containing the link
  1330. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1331. which is everything after the link protocol. It should return a cons
  1332. with possibly modified values of type and path.
  1333. Org contains a function for this, so if you set this variable to
  1334. `org-translate-link-from-planner', you should be able follow many
  1335. links created by planner."
  1336. :group 'org-link-follow
  1337. :type 'function)
  1338. (defcustom org-follow-link-hook nil
  1339. "Hook that is run after a link has been followed."
  1340. :group 'org-link-follow
  1341. :type 'hook)
  1342. (defcustom org-tab-follows-link nil
  1343. "Non-nil means on links TAB will follow the link.
  1344. Needs to be set before org.el is loaded.
  1345. This really should not be used, it does not make sense, and the
  1346. implementation is bad."
  1347. :group 'org-link-follow
  1348. :type 'boolean)
  1349. (defcustom org-return-follows-link nil
  1350. "Non-nil means on links RET will follow the link."
  1351. :group 'org-link-follow
  1352. :type 'boolean)
  1353. (defcustom org-mouse-1-follows-link
  1354. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1355. "Non-nil means mouse-1 on a link will follow the link.
  1356. A longer mouse click will still set point. Does not work on XEmacs.
  1357. Needs to be set before org.el is loaded."
  1358. :group 'org-link-follow
  1359. :type 'boolean)
  1360. (defcustom org-mark-ring-length 4
  1361. "Number of different positions to be recorded in the ring.
  1362. Changing this requires a restart of Emacs to work correctly."
  1363. :group 'org-link-follow
  1364. :type 'integer)
  1365. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1366. "Non-nil means internal links in Org files must exactly match a headline.
  1367. When nil, the link search tries to match a phrase with all words
  1368. in the search text."
  1369. :group 'org-link-follow
  1370. :version "24.1"
  1371. :type '(choice
  1372. (const :tag "Use fuzzy text search" nil)
  1373. (const :tag "Match only exact headline" t)
  1374. (const :tag "Match exact headline or query to create it"
  1375. query-to-create)))
  1376. (defcustom org-link-frame-setup
  1377. '((vm . vm-visit-folder-other-frame)
  1378. (vm-imap . vm-visit-imap-folder-other-frame)
  1379. (gnus . org-gnus-no-new-news)
  1380. (file . find-file-other-window)
  1381. (wl . wl-other-frame))
  1382. "Setup the frame configuration for following links.
  1383. When following a link with Emacs, it may often be useful to display
  1384. this link in another window or frame. This variable can be used to
  1385. set this up for the different types of links.
  1386. For VM, use any of
  1387. `vm-visit-folder'
  1388. `vm-visit-folder-other-window'
  1389. `vm-visit-folder-other-frame'
  1390. For Gnus, use any of
  1391. `gnus'
  1392. `gnus-other-frame'
  1393. `org-gnus-no-new-news'
  1394. For FILE, use any of
  1395. `find-file'
  1396. `find-file-other-window'
  1397. `find-file-other-frame'
  1398. For Wanderlust use any of
  1399. `wl'
  1400. `wl-other-frame'
  1401. For the calendar, use the variable `calendar-setup'.
  1402. For BBDB, it is currently only possible to display the matches in
  1403. another window."
  1404. :group 'org-link-follow
  1405. :type '(list
  1406. (cons (const vm)
  1407. (choice
  1408. (const vm-visit-folder)
  1409. (const vm-visit-folder-other-window)
  1410. (const vm-visit-folder-other-frame)))
  1411. (cons (const gnus)
  1412. (choice
  1413. (const gnus)
  1414. (const gnus-other-frame)
  1415. (const org-gnus-no-new-news)))
  1416. (cons (const file)
  1417. (choice
  1418. (const find-file)
  1419. (const find-file-other-window)
  1420. (const find-file-other-frame)))
  1421. (cons (const wl)
  1422. (choice
  1423. (const wl)
  1424. (const wl-other-frame)))))
  1425. (defcustom org-display-internal-link-with-indirect-buffer nil
  1426. "Non-nil means use indirect buffer to display infile links.
  1427. Activating internal links (from one location in a file to another location
  1428. in the same file) normally just jumps to the location. When the link is
  1429. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1430. is displayed in
  1431. another window. When this option is set, the other window actually displays
  1432. an indirect buffer clone of the current buffer, to avoid any visibility
  1433. changes to the current buffer."
  1434. :group 'org-link-follow
  1435. :type 'boolean)
  1436. (defcustom org-open-non-existing-files nil
  1437. "Non-nil means `org-open-file' will open non-existing files.
  1438. When nil, an error will be generated.
  1439. This variable applies only to external applications because they
  1440. might choke on non-existing files. If the link is to a file that
  1441. will be opened in Emacs, the variable is ignored."
  1442. :group 'org-link-follow
  1443. :type 'boolean)
  1444. (defcustom org-open-directory-means-index-dot-org nil
  1445. "Non-nil means a link to a directory really means to index.org.
  1446. When nil, following a directory link will run dired or open a finder/explorer
  1447. window on that directory."
  1448. :group 'org-link-follow
  1449. :type 'boolean)
  1450. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1451. "Function and arguments to call for following mailto links.
  1452. This is a list with the first element being a Lisp function, and the
  1453. remaining elements being arguments to the function. In string arguments,
  1454. %a will be replaced by the address, and %s will be replaced by the subject
  1455. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1456. :group 'org-link-follow
  1457. :type '(choice
  1458. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1459. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1460. (const :tag "message-mail" (message-mail "%a" "%s"))
  1461. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1462. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1463. "Non-nil means ask for confirmation before executing shell links.
  1464. Shell links can be dangerous: just think about a link
  1465. [[shell:rm -rf ~/*][Google Search]]
  1466. This link would show up in your Org-mode document as \"Google Search\",
  1467. but really it would remove your entire home directory.
  1468. Therefore we advise against setting this variable to nil.
  1469. Just change it to `y-or-n-p' if you want to confirm with a
  1470. single keystroke rather than having to type \"yes\"."
  1471. :group 'org-link-follow
  1472. :type '(choice
  1473. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1474. (const :tag "with y-or-n (faster)" y-or-n-p)
  1475. (const :tag "no confirmation (dangerous)" nil)))
  1476. (put 'org-confirm-shell-link-function
  1477. 'safe-local-variable
  1478. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1479. (defcustom org-confirm-shell-link-not-regexp ""
  1480. "A regexp to skip confirmation for shell links."
  1481. :group 'org-link-follow
  1482. :version "24.1"
  1483. :type 'regexp)
  1484. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1485. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1486. Elisp links can be dangerous: just think about a link
  1487. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1488. This link would show up in your Org-mode document as \"Google Search\",
  1489. but really it would remove your entire home directory.
  1490. Therefore we advise against setting this variable to nil.
  1491. Just change it to `y-or-n-p' if you want to confirm with a
  1492. single keystroke rather than having to type \"yes\"."
  1493. :group 'org-link-follow
  1494. :type '(choice
  1495. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1496. (const :tag "with y-or-n (faster)" y-or-n-p)
  1497. (const :tag "no confirmation (dangerous)" nil)))
  1498. (put 'org-confirm-shell-link-function
  1499. 'safe-local-variable
  1500. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1501. (defcustom org-confirm-elisp-link-not-regexp ""
  1502. "A regexp to skip confirmation for Elisp links."
  1503. :group 'org-link-follow
  1504. :version "24.1"
  1505. :type 'regexp)
  1506. (defconst org-file-apps-defaults-gnu
  1507. '((remote . emacs)
  1508. (system . mailcap)
  1509. (t . mailcap))
  1510. "Default file applications on a UNIX or GNU/Linux system.
  1511. See `org-file-apps'.")
  1512. (defconst org-file-apps-defaults-macosx
  1513. '((remote . emacs)
  1514. (t . "open %s")
  1515. (system . "open %s")
  1516. ("ps.gz" . "gv %s")
  1517. ("eps.gz" . "gv %s")
  1518. ("dvi" . "xdvi %s")
  1519. ("fig" . "xfig %s"))
  1520. "Default file applications on a MacOS X system.
  1521. The system \"open\" is known as a default, but we use X11 applications
  1522. for some files for which the OS does not have a good default.
  1523. See `org-file-apps'.")
  1524. (defconst org-file-apps-defaults-windowsnt
  1525. (list
  1526. '(remote . emacs)
  1527. (cons t
  1528. (list (if (featurep 'xemacs)
  1529. 'mswindows-shell-execute
  1530. 'w32-shell-execute)
  1531. "open" 'file))
  1532. (cons 'system
  1533. (list (if (featurep 'xemacs)
  1534. 'mswindows-shell-execute
  1535. 'w32-shell-execute)
  1536. "open" 'file)))
  1537. "Default file applications on a Windows NT system.
  1538. The system \"open\" is used for most files.
  1539. See `org-file-apps'.")
  1540. (defcustom org-file-apps
  1541. '(
  1542. (auto-mode . emacs)
  1543. ("\\.mm\\'" . default)
  1544. ("\\.x?html?\\'" . default)
  1545. ("\\.pdf\\'" . default)
  1546. )
  1547. "External applications for opening `file:path' items in a document.
  1548. Org-mode uses system defaults for different file types, but
  1549. you can use this variable to set the application for a given file
  1550. extension. The entries in this list are cons cells where the car identifies
  1551. files and the cdr the corresponding command. Possible values for the
  1552. file identifier are
  1553. \"string\" A string as a file identifier can be interpreted in different
  1554. ways, depending on its contents:
  1555. - Alphanumeric characters only:
  1556. Match links with this file extension.
  1557. Example: (\"pdf\" . \"evince %s\")
  1558. to open PDFs with evince.
  1559. - Regular expression: Match links where the
  1560. filename matches the regexp. If you want to
  1561. use groups here, use shy groups.
  1562. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1563. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1564. to open *.html and *.xhtml with firefox.
  1565. - Regular expression which contains (non-shy) groups:
  1566. Match links where the whole link, including \"::\", and
  1567. anything after that, matches the regexp.
  1568. In a custom command string, %1, %2, etc. are replaced with
  1569. the parts of the link that were matched by the groups.
  1570. For backwards compatibility, if a command string is given
  1571. that does not use any of the group matches, this case is
  1572. handled identically to the second one (i.e. match against
  1573. file name only).
  1574. In a custom lisp form, you can access the group matches with
  1575. (match-string n link).
  1576. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1577. to open [[file:document.pdf::5]] with evince at page 5.
  1578. `directory' Matches a directory
  1579. `remote' Matches a remote file, accessible through tramp or efs.
  1580. Remote files most likely should be visited through Emacs
  1581. because external applications cannot handle such paths.
  1582. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1583. so all files Emacs knows how to handle. Using this with
  1584. command `emacs' will open most files in Emacs. Beware that this
  1585. will also open html files inside Emacs, unless you add
  1586. (\"html\" . default) to the list as well.
  1587. t Default for files not matched by any of the other options.
  1588. `system' The system command to open files, like `open' on Windows
  1589. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1590. that will be selected if you call `C-c C-o' with a double
  1591. \\[universal-argument] \\[universal-argument] prefix.
  1592. Possible values for the command are:
  1593. `emacs' The file will be visited by the current Emacs process.
  1594. `default' Use the default application for this file type, which is the
  1595. association for t in the list, most likely in the system-specific
  1596. part.
  1597. This can be used to overrule an unwanted setting in the
  1598. system-specific variable.
  1599. `system' Use the system command for opening files, like \"open\".
  1600. This command is specified by the entry whose car is `system'.
  1601. Most likely, the system-specific version of this variable
  1602. does define this command, but you can overrule/replace it
  1603. here.
  1604. string A command to be executed by a shell; %s will be replaced
  1605. by the path to the file.
  1606. sexp A Lisp form which will be evaluated. The file path will
  1607. be available in the Lisp variable `file'.
  1608. For more examples, see the system specific constants
  1609. `org-file-apps-defaults-macosx'
  1610. `org-file-apps-defaults-windowsnt'
  1611. `org-file-apps-defaults-gnu'."
  1612. :group 'org-link-follow
  1613. :type '(repeat
  1614. (cons (choice :value ""
  1615. (string :tag "Extension")
  1616. (const :tag "System command to open files" system)
  1617. (const :tag "Default for unrecognized files" t)
  1618. (const :tag "Remote file" remote)
  1619. (const :tag "Links to a directory" directory)
  1620. (const :tag "Any files that have Emacs modes"
  1621. auto-mode))
  1622. (choice :value ""
  1623. (const :tag "Visit with Emacs" emacs)
  1624. (const :tag "Use default" default)
  1625. (const :tag "Use the system command" system)
  1626. (string :tag "Command")
  1627. (sexp :tag "Lisp form")))))
  1628. (defcustom org-doi-server-url "http://dx.doi.org/"
  1629. "The URL of the DOI server."
  1630. :type 'string
  1631. :version "24.3"
  1632. :group 'org-link-follow)
  1633. (defgroup org-refile nil
  1634. "Options concerning refiling entries in Org-mode."
  1635. :tag "Org Refile"
  1636. :group 'org)
  1637. (defcustom org-directory "~/org"
  1638. "Directory with org files.
  1639. This is just a default location to look for Org files. There is no need
  1640. at all to put your files into this directory. It is only used in the
  1641. following situations:
  1642. 1. When a capture template specifies a target file that is not an
  1643. absolute path. The path will then be interpreted relative to
  1644. `org-directory'
  1645. 2. When a capture note is filed away in an interactive way (when exiting the
  1646. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1647. with `org-directory' as the default path."
  1648. :group 'org-refile
  1649. :group 'org-remember
  1650. :group 'org-capture
  1651. :type 'directory)
  1652. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1653. "Default target for storing notes.
  1654. Used as a fall back file for org-remember.el and org-capture.el, for
  1655. templates that do not specify a target file."
  1656. :group 'org-refile
  1657. :group 'org-remember
  1658. :group 'org-capture
  1659. :type '(choice
  1660. (const :tag "Default from remember-data-file" nil)
  1661. file))
  1662. (defcustom org-goto-interface 'outline
  1663. "The default interface to be used for `org-goto'.
  1664. Allowed values are:
  1665. outline The interface shows an outline of the relevant file
  1666. and the correct heading is found by moving through
  1667. the outline or by searching with incremental search.
  1668. outline-path-completion Headlines in the current buffer are offered via
  1669. completion. This is the interface also used by
  1670. the refile command."
  1671. :group 'org-refile
  1672. :type '(choice
  1673. (const :tag "Outline" outline)
  1674. (const :tag "Outline-path-completion" outline-path-completion)))
  1675. (defcustom org-goto-max-level 5
  1676. "Maximum target level when running `org-goto' with refile interface."
  1677. :group 'org-refile
  1678. :type 'integer)
  1679. (defcustom org-reverse-note-order nil
  1680. "Non-nil means store new notes at the beginning of a file or entry.
  1681. When nil, new notes will be filed to the end of a file or entry.
  1682. This can also be a list with cons cells of regular expressions that
  1683. are matched against file names, and values."
  1684. :group 'org-remember
  1685. :group 'org-capture
  1686. :group 'org-refile
  1687. :type '(choice
  1688. (const :tag "Reverse always" t)
  1689. (const :tag "Reverse never" nil)
  1690. (repeat :tag "By file name regexp"
  1691. (cons regexp boolean))))
  1692. (defcustom org-log-refile nil
  1693. "Information to record when a task is refiled.
  1694. Possible values are:
  1695. nil Don't add anything
  1696. time Add a time stamp to the task
  1697. note Prompt for a note and add it with template `org-log-note-headings'
  1698. This option can also be set with on a per-file-basis with
  1699. #+STARTUP: nologrefile
  1700. #+STARTUP: logrefile
  1701. #+STARTUP: lognoterefile
  1702. You can have local logging settings for a subtree by setting the LOGGING
  1703. property to one or more of these keywords.
  1704. When bulk-refiling from the agenda, the value `note' is forbidden and
  1705. will temporarily be changed to `time'."
  1706. :group 'org-refile
  1707. :group 'org-progress
  1708. :version "24.1"
  1709. :type '(choice
  1710. (const :tag "No logging" nil)
  1711. (const :tag "Record timestamp" time)
  1712. (const :tag "Record timestamp with note." note)))
  1713. (defcustom org-refile-targets nil
  1714. "Targets for refiling entries with \\[org-refile].
  1715. This is a list of cons cells. Each cell contains:
  1716. - a specification of the files to be considered, either a list of files,
  1717. or a symbol whose function or variable value will be used to retrieve
  1718. a file name or a list of file names. If you use `org-agenda-files' for
  1719. that, all agenda files will be scanned for targets. Nil means consider
  1720. headings in the current buffer.
  1721. - A specification of how to find candidate refile targets. This may be
  1722. any of:
  1723. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1724. This tag has to be present in all target headlines, inheritance will
  1725. not be considered.
  1726. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1727. todo keyword.
  1728. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1729. headlines that are refiling targets.
  1730. - a cons cell (:level . N). Any headline of level N is considered a target.
  1731. Note that, when `org-odd-levels-only' is set, level corresponds to
  1732. order in hierarchy, not to the number of stars.
  1733. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1734. Note that, when `org-odd-levels-only' is set, level corresponds to
  1735. order in hierarchy, not to the number of stars.
  1736. Each element of this list generates a set of possible targets.
  1737. The union of these sets is presented (with completion) to
  1738. the user by `org-refile'.
  1739. You can set the variable `org-refile-target-verify-function' to a function
  1740. to verify each headline found by the simple criteria above.
  1741. When this variable is nil, all top-level headlines in the current buffer
  1742. are used, equivalent to the value `((nil . (:level . 1))'."
  1743. :group 'org-refile
  1744. :type '(repeat
  1745. (cons
  1746. (choice :value org-agenda-files
  1747. (const :tag "All agenda files" org-agenda-files)
  1748. (const :tag "Current buffer" nil)
  1749. (function) (variable) (file))
  1750. (choice :tag "Identify target headline by"
  1751. (cons :tag "Specific tag" (const :value :tag) (string))
  1752. (cons :tag "TODO keyword" (const :value :todo) (string))
  1753. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1754. (cons :tag "Level number" (const :value :level) (integer))
  1755. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1756. (defcustom org-refile-target-verify-function nil
  1757. "Function to verify if the headline at point should be a refile target.
  1758. The function will be called without arguments, with point at the
  1759. beginning of the headline. It should return t and leave point
  1760. where it is if the headline is a valid target for refiling.
  1761. If the target should not be selected, the function must return nil.
  1762. In addition to this, it may move point to a place from where the search
  1763. should be continued. For example, the function may decide that the entire
  1764. subtree of the current entry should be excluded and move point to the end
  1765. of the subtree."
  1766. :group 'org-refile
  1767. :type 'function)
  1768. (defcustom org-refile-use-cache nil
  1769. "Non-nil means cache refile targets to speed up the process.
  1770. The cache for a particular file will be updated automatically when
  1771. the buffer has been killed, or when any of the marker used for flagging
  1772. refile targets no longer points at a live buffer.
  1773. If you have added new entries to a buffer that might themselves be targets,
  1774. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1775. find that easier, `C-u C-u C-u C-c C-w'."
  1776. :group 'org-refile
  1777. :version "24.1"
  1778. :type 'boolean)
  1779. (defcustom org-refile-use-outline-path nil
  1780. "Non-nil means provide refile targets as paths.
  1781. So a level 3 headline will be available as level1/level2/level3.
  1782. When the value is `file', also include the file name (without directory)
  1783. into the path. In this case, you can also stop the completion after
  1784. the file name, to get entries inserted as top level in the file.
  1785. When `full-file-path', include the full file path."
  1786. :group 'org-refile
  1787. :type '(choice
  1788. (const :tag "Not" nil)
  1789. (const :tag "Yes" t)
  1790. (const :tag "Start with file name" file)
  1791. (const :tag "Start with full file path" full-file-path)))
  1792. (defcustom org-outline-path-complete-in-steps t
  1793. "Non-nil means complete the outline path in hierarchical steps.
  1794. When Org-mode uses the refile interface to select an outline path
  1795. \(see variable `org-refile-use-outline-path'), the completion of
  1796. the path can be done is a single go, or if can be done in steps down
  1797. the headline hierarchy. Going in steps is probably the best if you
  1798. do not use a special completion package like `ido' or `icicles'.
  1799. However, when using these packages, going in one step can be very
  1800. fast, while still showing the whole path to the entry."
  1801. :group 'org-refile
  1802. :type 'boolean)
  1803. (defcustom org-refile-allow-creating-parent-nodes nil
  1804. "Non-nil means allow to create new nodes as refile targets.
  1805. New nodes are then created by adding \"/new node name\" to the completion
  1806. of an existing node. When the value of this variable is `confirm',
  1807. new node creation must be confirmed by the user (recommended)
  1808. When nil, the completion must match an existing entry.
  1809. Note that, if the new heading is not seen by the criteria
  1810. listed in `org-refile-targets', multiple instances of the same
  1811. heading would be created by trying again to file under the new
  1812. heading."
  1813. :group 'org-refile
  1814. :type '(choice
  1815. (const :tag "Never" nil)
  1816. (const :tag "Always" t)
  1817. (const :tag "Prompt for confirmation" confirm)))
  1818. (defcustom org-refile-active-region-within-subtree nil
  1819. "Non-nil means also refile active region within a subtree.
  1820. By default `org-refile' doesn't allow refiling regions if they
  1821. don't contain a set of subtrees, but it might be convenient to
  1822. do so sometimes: in that case, the first line of the region is
  1823. converted to a headline before refiling."
  1824. :group 'org-refile
  1825. :version "24.1"
  1826. :type 'boolean)
  1827. (defgroup org-todo nil
  1828. "Options concerning TODO items in Org-mode."
  1829. :tag "Org TODO"
  1830. :group 'org)
  1831. (defgroup org-progress nil
  1832. "Options concerning Progress logging in Org-mode."
  1833. :tag "Org Progress"
  1834. :group 'org-time)
  1835. (defvar org-todo-interpretation-widgets
  1836. '((:tag "Sequence (cycling hits every state)" sequence)
  1837. (:tag "Type (cycling directly to DONE)" type))
  1838. "The available interpretation symbols for customizing `org-todo-keywords'.
  1839. Interested libraries should add to this list.")
  1840. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1841. "List of TODO entry keyword sequences and their interpretation.
  1842. \\<org-mode-map>This is a list of sequences.
  1843. Each sequence starts with a symbol, either `sequence' or `type',
  1844. indicating if the keywords should be interpreted as a sequence of
  1845. action steps, or as different types of TODO items. The first
  1846. keywords are states requiring action - these states will select a headline
  1847. for inclusion into the global TODO list Org-mode produces. If one of
  1848. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1849. signify that no further action is necessary. If \"|\" is not found,
  1850. the last keyword is treated as the only DONE state of the sequence.
  1851. The command \\[org-todo] cycles an entry through these states, and one
  1852. additional state where no keyword is present. For details about this
  1853. cycling, see the manual.
  1854. TODO keywords and interpretation can also be set on a per-file basis with
  1855. the special #+SEQ_TODO and #+TYP_TODO lines.
  1856. Each keyword can optionally specify a character for fast state selection
  1857. \(in combination with the variable `org-use-fast-todo-selection')
  1858. and specifiers for state change logging, using the same syntax that
  1859. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1860. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1861. indicates to record a time stamp each time this state is selected.
  1862. Each keyword may also specify if a timestamp or a note should be
  1863. recorded when entering or leaving the state, by adding additional
  1864. characters in the parenthesis after the keyword. This looks like this:
  1865. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1866. record only the time of the state change. With X and Y being either
  1867. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1868. Y when leaving the state if and only if the *target* state does not
  1869. define X. You may omit any of the fast-selection key or X or /Y,
  1870. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1871. For backward compatibility, this variable may also be just a list
  1872. of keywords. In this case the interpretation (sequence or type) will be
  1873. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1874. :group 'org-todo
  1875. :group 'org-keywords
  1876. :type '(choice
  1877. (repeat :tag "Old syntax, just keywords"
  1878. (string :tag "Keyword"))
  1879. (repeat :tag "New syntax"
  1880. (cons
  1881. (choice
  1882. :tag "Interpretation"
  1883. ;;Quick and dirty way to see
  1884. ;;`org-todo-interpretations'. This takes the
  1885. ;;place of item arguments
  1886. :convert-widget
  1887. (lambda (widget)
  1888. (widget-put widget
  1889. :args (mapcar
  1890. #'(lambda (x)
  1891. (widget-convert
  1892. (cons 'const x)))
  1893. org-todo-interpretation-widgets))
  1894. widget))
  1895. (repeat
  1896. (string :tag "Keyword"))))))
  1897. (defvar org-todo-keywords-1 nil
  1898. "All TODO and DONE keywords active in a buffer.")
  1899. (make-variable-buffer-local 'org-todo-keywords-1)
  1900. (defvar org-todo-keywords-for-agenda nil)
  1901. (defvar org-done-keywords-for-agenda nil)
  1902. (defvar org-drawers-for-agenda nil)
  1903. (defvar org-todo-keyword-alist-for-agenda nil)
  1904. (defvar org-tag-alist-for-agenda nil)
  1905. (defvar org-agenda-contributing-files nil)
  1906. (defvar org-not-done-keywords nil)
  1907. (make-variable-buffer-local 'org-not-done-keywords)
  1908. (defvar org-done-keywords nil)
  1909. (make-variable-buffer-local 'org-done-keywords)
  1910. (defvar org-todo-heads nil)
  1911. (make-variable-buffer-local 'org-todo-heads)
  1912. (defvar org-todo-sets nil)
  1913. (make-variable-buffer-local 'org-todo-sets)
  1914. (defvar org-todo-log-states nil)
  1915. (make-variable-buffer-local 'org-todo-log-states)
  1916. (defvar org-todo-kwd-alist nil)
  1917. (make-variable-buffer-local 'org-todo-kwd-alist)
  1918. (defvar org-todo-key-alist nil)
  1919. (make-variable-buffer-local 'org-todo-key-alist)
  1920. (defvar org-todo-key-trigger nil)
  1921. (make-variable-buffer-local 'org-todo-key-trigger)
  1922. (defcustom org-todo-interpretation 'sequence
  1923. "Controls how TODO keywords are interpreted.
  1924. This variable is in principle obsolete and is only used for
  1925. backward compatibility, if the interpretation of todo keywords is
  1926. not given already in `org-todo-keywords'. See that variable for
  1927. more information."
  1928. :group 'org-todo
  1929. :group 'org-keywords
  1930. :type '(choice (const sequence)
  1931. (const type)))
  1932. (defcustom org-use-fast-todo-selection t
  1933. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1934. This variable describes if and under what circumstances the cycling
  1935. mechanism for TODO keywords will be replaced by a single-key, direct
  1936. selection scheme.
  1937. When nil, fast selection is never used.
  1938. When the symbol `prefix', it will be used when `org-todo' is called
  1939. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1940. `C-u t' in an agenda buffer.
  1941. When t, fast selection is used by default. In this case, the prefix
  1942. argument forces cycling instead.
  1943. In all cases, the special interface is only used if access keys have
  1944. actually been assigned by the user, i.e. if keywords in the configuration
  1945. are followed by a letter in parenthesis, like TODO(t)."
  1946. :group 'org-todo
  1947. :type '(choice
  1948. (const :tag "Never" nil)
  1949. (const :tag "By default" t)
  1950. (const :tag "Only with C-u C-c C-t" prefix)))
  1951. (defcustom org-provide-todo-statistics t
  1952. "Non-nil means update todo statistics after insert and toggle.
  1953. ALL-HEADLINES means update todo statistics by including headlines
  1954. with no TODO keyword as well, counting them as not done.
  1955. A list of TODO keywords means the same, but skip keywords that are
  1956. not in this list.
  1957. When this is set, todo statistics is updated in the parent of the
  1958. current entry each time a todo state is changed."
  1959. :group 'org-todo
  1960. :type '(choice
  1961. (const :tag "Yes, only for TODO entries" t)
  1962. (const :tag "Yes, including all entries" 'all-headlines)
  1963. (repeat :tag "Yes, for TODOs in this list"
  1964. (string :tag "TODO keyword"))
  1965. (other :tag "No TODO statistics" nil)))
  1966. (defcustom org-hierarchical-todo-statistics t
  1967. "Non-nil means TODO statistics covers just direct children.
  1968. When nil, all entries in the subtree are considered.
  1969. This has only an effect if `org-provide-todo-statistics' is set.
  1970. To set this to nil for only a single subtree, use a COOKIE_DATA
  1971. property and include the word \"recursive\" into the value."
  1972. :group 'org-todo
  1973. :type 'boolean)
  1974. (defcustom org-after-todo-state-change-hook nil
  1975. "Hook which is run after the state of a TODO item was changed.
  1976. The new state (a string with a TODO keyword, or nil) is available in the
  1977. Lisp variable `org-state'."
  1978. :group 'org-todo
  1979. :type 'hook)
  1980. (defvar org-blocker-hook nil
  1981. "Hook for functions that are allowed to block a state change.
  1982. Each function gets as its single argument a property list, see
  1983. `org-trigger-hook' for more information about this list.
  1984. If any of the functions in this hook returns nil, the state change
  1985. is blocked.")
  1986. (defvar org-trigger-hook nil
  1987. "Hook for functions that are triggered by a state change.
  1988. Each function gets as its single argument a property list with at least
  1989. the following elements:
  1990. (:type type-of-change :position pos-at-entry-start
  1991. :from old-state :to new-state)
  1992. Depending on the type, more properties may be present.
  1993. This mechanism is currently implemented for:
  1994. TODO state changes
  1995. ------------------
  1996. :type todo-state-change
  1997. :from previous state (keyword as a string), or nil, or a symbol
  1998. 'todo' or 'done', to indicate the general type of state.
  1999. :to new state, like in :from")
  2000. (defcustom org-enforce-todo-dependencies nil
  2001. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2002. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2003. be blocked if any prior sibling is not yet done.
  2004. Finally, if the parent is blocked because of ordered siblings of its own,
  2005. the child will also be blocked."
  2006. :set (lambda (var val)
  2007. (set var val)
  2008. (if val
  2009. (add-hook 'org-blocker-hook
  2010. 'org-block-todo-from-children-or-siblings-or-parent)
  2011. (remove-hook 'org-blocker-hook
  2012. 'org-block-todo-from-children-or-siblings-or-parent)))
  2013. :group 'org-todo
  2014. :type 'boolean)
  2015. (defcustom org-enforce-todo-checkbox-dependencies nil
  2016. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2017. When this is nil, checkboxes have no influence on switching TODO states.
  2018. When non-nil, you first need to check off all check boxes before the TODO
  2019. entry can be switched to DONE.
  2020. This variable needs to be set before org.el is loaded, and you need to
  2021. restart Emacs after a change to make the change effective. The only way
  2022. to change is while Emacs is running is through the customize interface."
  2023. :set (lambda (var val)
  2024. (set var val)
  2025. (if val
  2026. (add-hook 'org-blocker-hook
  2027. 'org-block-todo-from-checkboxes)
  2028. (remove-hook 'org-blocker-hook
  2029. 'org-block-todo-from-checkboxes)))
  2030. :group 'org-todo
  2031. :type 'boolean)
  2032. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2033. "Non-nil means inserting a TODO heading is treated as state change.
  2034. So when the command \\[org-insert-todo-heading] is used, state change
  2035. logging will apply if appropriate. When nil, the new TODO item will
  2036. be inserted directly, and no logging will take place."
  2037. :group 'org-todo
  2038. :type 'boolean)
  2039. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2040. "Non-nil means switching TODO states with S-cursor counts as state change.
  2041. This is the default behavior. However, setting this to nil allows a
  2042. convenient way to select a TODO state and bypass any logging associated
  2043. with that."
  2044. :group 'org-todo
  2045. :type 'boolean)
  2046. (defcustom org-todo-state-tags-triggers nil
  2047. "Tag changes that should be triggered by TODO state changes.
  2048. This is a list. Each entry is
  2049. (state-change (tag . flag) .......)
  2050. State-change can be a string with a state, and empty string to indicate the
  2051. state that has no TODO keyword, or it can be one of the symbols `todo'
  2052. or `done', meaning any not-done or done state, respectively."
  2053. :group 'org-todo
  2054. :group 'org-tags
  2055. :type '(repeat
  2056. (cons (choice :tag "When changing to"
  2057. (const :tag "Not-done state" todo)
  2058. (const :tag "Done state" done)
  2059. (string :tag "State"))
  2060. (repeat
  2061. (cons :tag "Tag action"
  2062. (string :tag "Tag")
  2063. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2064. (defcustom org-log-done nil
  2065. "Information to record when a task moves to the DONE state.
  2066. Possible values are:
  2067. nil Don't add anything, just change the keyword
  2068. time Add a time stamp to the task
  2069. note Prompt for a note and add it with template `org-log-note-headings'
  2070. This option can also be set with on a per-file-basis with
  2071. #+STARTUP: nologdone
  2072. #+STARTUP: logdone
  2073. #+STARTUP: lognotedone
  2074. You can have local logging settings for a subtree by setting the LOGGING
  2075. property to one or more of these keywords."
  2076. :group 'org-todo
  2077. :group 'org-progress
  2078. :type '(choice
  2079. (const :tag "No logging" nil)
  2080. (const :tag "Record CLOSED timestamp" time)
  2081. (const :tag "Record CLOSED timestamp with note." note)))
  2082. ;; Normalize old uses of org-log-done.
  2083. (cond
  2084. ((eq org-log-done t) (setq org-log-done 'time))
  2085. ((and (listp org-log-done) (memq 'done org-log-done))
  2086. (setq org-log-done 'note)))
  2087. (defcustom org-log-reschedule nil
  2088. "Information to record when the scheduling date of a tasks is modified.
  2089. Possible values are:
  2090. nil Don't add anything, just change the date
  2091. time Add a time stamp to the task
  2092. note Prompt for a note and add it with template `org-log-note-headings'
  2093. This option can also be set with on a per-file-basis with
  2094. #+STARTUP: nologreschedule
  2095. #+STARTUP: logreschedule
  2096. #+STARTUP: lognotereschedule"
  2097. :group 'org-todo
  2098. :group 'org-progress
  2099. :type '(choice
  2100. (const :tag "No logging" nil)
  2101. (const :tag "Record timestamp" time)
  2102. (const :tag "Record timestamp with note." note)))
  2103. (defcustom org-log-redeadline nil
  2104. "Information to record when the deadline date of a tasks is modified.
  2105. Possible values are:
  2106. nil Don't add anything, just change the date
  2107. time Add a time stamp to the task
  2108. note Prompt for a note and add it with template `org-log-note-headings'
  2109. This option can also be set with on a per-file-basis with
  2110. #+STARTUP: nologredeadline
  2111. #+STARTUP: logredeadline
  2112. #+STARTUP: lognoteredeadline
  2113. You can have local logging settings for a subtree by setting the LOGGING
  2114. property to one or more of these keywords."
  2115. :group 'org-todo
  2116. :group 'org-progress
  2117. :type '(choice
  2118. (const :tag "No logging" nil)
  2119. (const :tag "Record timestamp" time)
  2120. (const :tag "Record timestamp with note." note)))
  2121. (defcustom org-log-note-clock-out nil
  2122. "Non-nil means record a note when clocking out of an item.
  2123. This can also be configured on a per-file basis by adding one of
  2124. the following lines anywhere in the buffer:
  2125. #+STARTUP: lognoteclock-out
  2126. #+STARTUP: nolognoteclock-out"
  2127. :group 'org-todo
  2128. :group 'org-progress
  2129. :type 'boolean)
  2130. (defcustom org-log-done-with-time t
  2131. "Non-nil means the CLOSED time stamp will contain date and time.
  2132. When nil, only the date will be recorded."
  2133. :group 'org-progress
  2134. :type 'boolean)
  2135. (defcustom org-log-note-headings
  2136. '((done . "CLOSING NOTE %t")
  2137. (state . "State %-12s from %-12S %t")
  2138. (note . "Note taken on %t")
  2139. (reschedule . "Rescheduled from %S on %t")
  2140. (delschedule . "Not scheduled, was %S on %t")
  2141. (redeadline . "New deadline from %S on %t")
  2142. (deldeadline . "Removed deadline, was %S on %t")
  2143. (refile . "Refiled on %t")
  2144. (clock-out . ""))
  2145. "Headings for notes added to entries.
  2146. The value is an alist, with the car being a symbol indicating the note
  2147. context, and the cdr is the heading to be used. The heading may also be the
  2148. empty string.
  2149. %t in the heading will be replaced by a time stamp.
  2150. %T will be an active time stamp instead the default inactive one
  2151. %d will be replaced by a short-format time stamp.
  2152. %D will be replaced by an active short-format time stamp.
  2153. %s will be replaced by the new TODO state, in double quotes.
  2154. %S will be replaced by the old TODO state, in double quotes.
  2155. %u will be replaced by the user name.
  2156. %U will be replaced by the full user name.
  2157. In fact, it is not a good idea to change the `state' entry, because
  2158. agenda log mode depends on the format of these entries."
  2159. :group 'org-todo
  2160. :group 'org-progress
  2161. :type '(list :greedy t
  2162. (cons (const :tag "Heading when closing an item" done) string)
  2163. (cons (const :tag
  2164. "Heading when changing todo state (todo sequence only)"
  2165. state) string)
  2166. (cons (const :tag "Heading when just taking a note" note) string)
  2167. (cons (const :tag "Heading when clocking out" clock-out) string)
  2168. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2169. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2170. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2171. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2172. (cons (const :tag "Heading when refiling" refile) string)))
  2173. (unless (assq 'note org-log-note-headings)
  2174. (push '(note . "%t") org-log-note-headings))
  2175. (defcustom org-log-into-drawer nil
  2176. "Non-nil means insert state change notes and time stamps into a drawer.
  2177. When nil, state changes notes will be inserted after the headline and
  2178. any scheduling and clock lines, but not inside a drawer.
  2179. The value of this variable should be the name of the drawer to use.
  2180. LOGBOOK is proposed as the default drawer for this purpose, you can
  2181. also set this to a string to define the drawer of your choice.
  2182. A value of t is also allowed, representing \"LOGBOOK\".
  2183. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2184. will be ignored.
  2185. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2186. a subtree."
  2187. :group 'org-todo
  2188. :group 'org-progress
  2189. :type '(choice
  2190. (const :tag "Not into a drawer" nil)
  2191. (const :tag "LOGBOOK" t)
  2192. (string :tag "Other")))
  2193. (if (fboundp 'defvaralias)
  2194. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2195. (defun org-log-into-drawer ()
  2196. "Return the value of `org-log-into-drawer', but let properties overrule.
  2197. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2198. used instead of the default value."
  2199. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2200. (cond
  2201. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2202. ((equal p "t") "LOGBOOK")
  2203. (t p))))
  2204. (defcustom org-log-state-notes-insert-after-drawers nil
  2205. "Non-nil means insert state change notes after any drawers in entry.
  2206. Only the drawers that *immediately* follow the headline and the
  2207. deadline/scheduled line are skipped.
  2208. When nil, insert notes right after the heading and perhaps the line
  2209. with deadline/scheduling if present.
  2210. This variable will have no effect if `org-log-into-drawer' is
  2211. set."
  2212. :group 'org-todo
  2213. :group 'org-progress
  2214. :type 'boolean)
  2215. (defcustom org-log-states-order-reversed t
  2216. "Non-nil means the latest state note will be directly after heading.
  2217. When nil, the state change notes will be ordered according to time."
  2218. :group 'org-todo
  2219. :group 'org-progress
  2220. :type 'boolean)
  2221. (defcustom org-todo-repeat-to-state nil
  2222. "The TODO state to which a repeater should return the repeating task.
  2223. By default this is the first task in a TODO sequence, or the previous state
  2224. in a TODO_TYP set. But you can specify another task here.
  2225. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2226. :group 'org-todo
  2227. :version "24.1"
  2228. :type '(choice (const :tag "Head of sequence" nil)
  2229. (string :tag "Specific state")))
  2230. (defcustom org-log-repeat 'time
  2231. "Non-nil means record moving through the DONE state when triggering repeat.
  2232. An auto-repeating task is immediately switched back to TODO when
  2233. marked DONE. If you are not logging state changes (by adding \"@\"
  2234. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2235. record a closing note, there will be no record of the task moving
  2236. through DONE. This variable forces taking a note anyway.
  2237. nil Don't force a record
  2238. time Record a time stamp
  2239. note Prompt for a note and add it with template `org-log-note-headings'
  2240. This option can also be set with on a per-file-basis with
  2241. #+STARTUP: nologrepeat
  2242. #+STARTUP: logrepeat
  2243. #+STARTUP: lognoterepeat
  2244. You can have local logging settings for a subtree by setting the LOGGING
  2245. property to one or more of these keywords."
  2246. :group 'org-todo
  2247. :group 'org-progress
  2248. :type '(choice
  2249. (const :tag "Don't force a record" nil)
  2250. (const :tag "Force recording the DONE state" time)
  2251. (const :tag "Force recording a note with the DONE state" note)))
  2252. (defgroup org-priorities nil
  2253. "Priorities in Org-mode."
  2254. :tag "Org Priorities"
  2255. :group 'org-todo)
  2256. (defcustom org-enable-priority-commands t
  2257. "Non-nil means priority commands are active.
  2258. When nil, these commands will be disabled, so that you never accidentally
  2259. set a priority."
  2260. :group 'org-priorities
  2261. :type 'boolean)
  2262. (defcustom org-highest-priority ?A
  2263. "The highest priority of TODO items. A character like ?A, ?B etc.
  2264. Must have a smaller ASCII number than `org-lowest-priority'."
  2265. :group 'org-priorities
  2266. :type 'character)
  2267. (defcustom org-lowest-priority ?C
  2268. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2269. Must have a larger ASCII number than `org-highest-priority'."
  2270. :group 'org-priorities
  2271. :type 'character)
  2272. (defcustom org-default-priority ?B
  2273. "The default priority of TODO items.
  2274. This is the priority an item gets if no explicit priority is given.
  2275. When starting to cycle on an empty priority the first step in the cycle
  2276. depends on `org-priority-start-cycle-with-default'. The resulting first
  2277. step priority must not exceed the range from `org-highest-priority' to
  2278. `org-lowest-priority' which means that `org-default-priority' has to be
  2279. in this range exclusive or inclusive the range boundaries. Else the
  2280. first step refuses to set the default and the second will fall back
  2281. to (depending on the command used) the highest or lowest priority."
  2282. :group 'org-priorities
  2283. :type 'character)
  2284. (defcustom org-priority-start-cycle-with-default t
  2285. "Non-nil means start with default priority when starting to cycle.
  2286. When this is nil, the first step in the cycle will be (depending on the
  2287. command used) one higher or lower than the default priority.
  2288. See also `org-default-priority'."
  2289. :group 'org-priorities
  2290. :type 'boolean)
  2291. (defcustom org-get-priority-function nil
  2292. "Function to extract the priority from a string.
  2293. The string is normally the headline. If this is nil Org computes the
  2294. priority from the priority cookie like [#A] in the headline. It returns
  2295. an integer, increasing by 1000 for each priority level.
  2296. The user can set a different function here, which should take a string
  2297. as an argument and return the numeric priority."
  2298. :group 'org-priorities
  2299. :version "24.1"
  2300. :type 'function)
  2301. (defgroup org-time nil
  2302. "Options concerning time stamps and deadlines in Org-mode."
  2303. :tag "Org Time"
  2304. :group 'org)
  2305. (defcustom org-insert-labeled-timestamps-at-point nil
  2306. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2307. When nil, these labeled time stamps are forces into the second line of an
  2308. entry, just after the headline. When scheduling from the global TODO list,
  2309. the time stamp will always be forced into the second line."
  2310. :group 'org-time
  2311. :type 'boolean)
  2312. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2313. "Formats for `format-time-string' which are used for time stamps.
  2314. It is not recommended to change this constant.")
  2315. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2316. "Number of minutes to round time stamps to.
  2317. These are two values, the first applies when first creating a time stamp.
  2318. The second applies when changing it with the commands `S-up' and `S-down'.
  2319. When changing the time stamp, this means that it will change in steps
  2320. of N minutes, as given by the second value.
  2321. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2322. numbers should be factors of 60, so for example 5, 10, 15.
  2323. When this is larger than 1, you can still force an exact time stamp by using
  2324. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2325. and by using a prefix arg to `S-up/down' to specify the exact number
  2326. of minutes to shift."
  2327. :group 'org-time
  2328. :get #'(lambda (var) ; Make sure both elements are there
  2329. (if (integerp (default-value var))
  2330. (list (default-value var) 5)
  2331. (default-value var)))
  2332. :type '(list
  2333. (integer :tag "when inserting times")
  2334. (integer :tag "when modifying times")))
  2335. ;; Normalize old customizations of this variable.
  2336. (when (integerp org-time-stamp-rounding-minutes)
  2337. (setq org-time-stamp-rounding-minutes
  2338. (list org-time-stamp-rounding-minutes
  2339. org-time-stamp-rounding-minutes)))
  2340. (defcustom org-display-custom-times nil
  2341. "Non-nil means overlay custom formats over all time stamps.
  2342. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2343. To turn this on on a per-file basis, insert anywhere in the file:
  2344. #+STARTUP: customtime"
  2345. :group 'org-time
  2346. :set 'set-default
  2347. :type 'sexp)
  2348. (make-variable-buffer-local 'org-display-custom-times)
  2349. (defcustom org-time-stamp-custom-formats
  2350. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2351. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2352. These are overlaid over the default ISO format if the variable
  2353. `org-display-custom-times' is set. Time like %H:%M should be at the
  2354. end of the second format. The custom formats are also honored by export
  2355. commands, if custom time display is turned on at the time of export."
  2356. :group 'org-time
  2357. :type 'sexp)
  2358. (defun org-time-stamp-format (&optional long inactive)
  2359. "Get the right format for a time string."
  2360. (let ((f (if long (cdr org-time-stamp-formats)
  2361. (car org-time-stamp-formats))))
  2362. (if inactive
  2363. (concat "[" (substring f 1 -1) "]")
  2364. f)))
  2365. (defcustom org-time-clocksum-format "%d:%02d"
  2366. "The format string used when creating CLOCKSUM lines.
  2367. This is also used when org-mode generates a time duration."
  2368. :group 'org-time
  2369. :type 'string)
  2370. (defcustom org-time-clocksum-use-fractional nil
  2371. "If non-nil, \\[org-clock-display] uses fractional times.
  2372. org-mode generates a time duration."
  2373. :group 'org-time
  2374. :type 'boolean)
  2375. (defcustom org-time-clocksum-fractional-format "%.2f"
  2376. "The format string used when creating CLOCKSUM lines, or when
  2377. org-mode generates a time duration."
  2378. :group 'org-time
  2379. :type 'string)
  2380. (defcustom org-deadline-warning-days 14
  2381. "No. of days before expiration during which a deadline becomes active.
  2382. This variable governs the display in sparse trees and in the agenda.
  2383. When 0 or negative, it means use this number (the absolute value of it)
  2384. even if a deadline has a different individual lead time specified.
  2385. Custom commands can set this variable in the options section."
  2386. :group 'org-time
  2387. :group 'org-agenda-daily/weekly
  2388. :type 'integer)
  2389. (defcustom org-read-date-prefer-future t
  2390. "Non-nil means assume future for incomplete date input from user.
  2391. This affects the following situations:
  2392. 1. The user gives a month but not a year.
  2393. For example, if it is April and you enter \"feb 2\", this will be read
  2394. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2395. 2. The user gives a day, but no month.
  2396. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2397. read this as the third of *next* month. However, if you enter \"17\",
  2398. it will be considered as *this* month.
  2399. If you set this variable to the symbol `time', then also the following
  2400. will work:
  2401. 3. If the user gives a time.
  2402. If the time is before now, it will be interpreted as tomorrow.
  2403. Currently none of this works for ISO week specifications.
  2404. When this option is nil, the current day, month and year will always be
  2405. used as defaults.
  2406. See also `org-agenda-jump-prefer-future'."
  2407. :group 'org-time
  2408. :type '(choice
  2409. (const :tag "Never" nil)
  2410. (const :tag "Check month and day" t)
  2411. (const :tag "Check month, day, and time" time)))
  2412. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2413. "Should the agenda jump command prefer the future for incomplete dates?
  2414. The default is to do the same as configured in `org-read-date-prefer-future'.
  2415. But you can also set a deviating value here.
  2416. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2417. :group 'org-agenda
  2418. :group 'org-time
  2419. :version "24.1"
  2420. :type '(choice
  2421. (const :tag "Use org-read-date-prefer-future"
  2422. org-read-date-prefer-future)
  2423. (const :tag "Never" nil)
  2424. (const :tag "Always" t)))
  2425. (defcustom org-read-date-force-compatible-dates t
  2426. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2427. Depending on the system Emacs is running on, certain dates cannot
  2428. be represented with the type used internally to represent time.
  2429. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2430. correctly. Some systems allow for earlier dates, some for later,
  2431. some for both. One way to find out it to insert any date into an
  2432. Org buffer, putting the cursor on the year and hitting S-up and
  2433. S-down to test the range.
  2434. When this variable is set to t, the date/time prompt will not let
  2435. you specify dates outside the 1970-2037 range, so it is certain that
  2436. these dates will work in whatever version of Emacs you are
  2437. running, and also that you can move a file from one Emacs implementation
  2438. to another. WHenever Org is forcing the year for you, it will display
  2439. a message and beep.
  2440. When this variable is nil, Org will check if the date is
  2441. representable in the specific Emacs implementation you are using.
  2442. If not, it will force a year, usually the current year, and beep
  2443. to remind you. Currently this setting is not recommended because
  2444. the likelihood that you will open your Org files in an Emacs that
  2445. has limited date range is not negligible.
  2446. A workaround for this problem is to use diary sexp dates for time
  2447. stamps outside of this range."
  2448. :group 'org-time
  2449. :version "24.1"
  2450. :type 'boolean)
  2451. (defcustom org-read-date-display-live t
  2452. "Non-nil means display current interpretation of date prompt live.
  2453. This display will be in an overlay, in the minibuffer."
  2454. :group 'org-time
  2455. :type 'boolean)
  2456. (defcustom org-read-date-popup-calendar t
  2457. "Non-nil means pop up a calendar when prompting for a date.
  2458. In the calendar, the date can be selected with mouse-1. However, the
  2459. minibuffer will also be active, and you can simply enter the date as well.
  2460. When nil, only the minibuffer will be available."
  2461. :group 'org-time
  2462. :type 'boolean)
  2463. (if (fboundp 'defvaralias)
  2464. (defvaralias 'org-popup-calendar-for-date-prompt
  2465. 'org-read-date-popup-calendar))
  2466. (defcustom org-read-date-minibuffer-setup-hook nil
  2467. "Hook to be used to set up keys for the date/time interface.
  2468. Add key definitions to `minibuffer-local-map', which will be a temporary
  2469. copy."
  2470. :group 'org-time
  2471. :type 'hook)
  2472. (defcustom org-extend-today-until 0
  2473. "The hour when your day really ends. Must be an integer.
  2474. This has influence for the following applications:
  2475. - When switching the agenda to \"today\". It it is still earlier than
  2476. the time given here, the day recognized as TODAY is actually yesterday.
  2477. - When a date is read from the user and it is still before the time given
  2478. here, the current date and time will be assumed to be yesterday, 23:59.
  2479. Also, timestamps inserted in capture templates follow this rule.
  2480. IMPORTANT: This is a feature whose implementation is and likely will
  2481. remain incomplete. Really, it is only here because past midnight seems to
  2482. be the favorite working time of John Wiegley :-)"
  2483. :group 'org-time
  2484. :type 'integer)
  2485. (defcustom org-use-effective-time nil
  2486. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2487. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2488. \"effective time\" of any timestamps between midnight and 8am will be
  2489. 23:59 of the previous day."
  2490. :group 'org-time
  2491. :version "24.1"
  2492. :type 'boolean)
  2493. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2494. "When non-nil, use the last clock out time for `org-todo'.
  2495. Note that this option has precedence over the combined use of
  2496. `org-use-effective-time' and `org-extend-today-until'."
  2497. :group 'org-time
  2498. ;; :version "24.3"
  2499. :type 'boolean)
  2500. (defcustom org-edit-timestamp-down-means-later nil
  2501. "Non-nil means S-down will increase the time in a time stamp.
  2502. When nil, S-up will increase."
  2503. :group 'org-time
  2504. :type 'boolean)
  2505. (defcustom org-calendar-follow-timestamp-change t
  2506. "Non-nil means make the calendar window follow timestamp changes.
  2507. When a timestamp is modified and the calendar window is visible, it will be
  2508. moved to the new date."
  2509. :group 'org-time
  2510. :type 'boolean)
  2511. (defgroup org-tags nil
  2512. "Options concerning tags in Org-mode."
  2513. :tag "Org Tags"
  2514. :group 'org)
  2515. (defcustom org-tag-alist nil
  2516. "List of tags allowed in Org-mode files.
  2517. When this list is nil, Org-mode will base TAG input on what is already in the
  2518. buffer.
  2519. The value of this variable is an alist, the car of each entry must be a
  2520. keyword as a string, the cdr may be a character that is used to select
  2521. that tag through the fast-tag-selection interface.
  2522. See the manual for details."
  2523. :group 'org-tags
  2524. :type '(repeat
  2525. (choice
  2526. (cons (string :tag "Tag name")
  2527. (character :tag "Access char"))
  2528. (list :tag "Start radio group"
  2529. (const :startgroup)
  2530. (option (string :tag "Group description")))
  2531. (list :tag "End radio group"
  2532. (const :endgroup)
  2533. (option (string :tag "Group description")))
  2534. (const :tag "New line" (:newline)))))
  2535. (defcustom org-tag-persistent-alist nil
  2536. "List of tags that will always appear in all Org-mode files.
  2537. This is in addition to any in buffer settings or customizations
  2538. of `org-tag-alist'.
  2539. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2540. The value of this variable is an alist, the car of each entry must be a
  2541. keyword as a string, the cdr may be a character that is used to select
  2542. that tag through the fast-tag-selection interface.
  2543. See the manual for details.
  2544. To disable these tags on a per-file basis, insert anywhere in the file:
  2545. #+STARTUP: noptag"
  2546. :group 'org-tags
  2547. :type '(repeat
  2548. (choice
  2549. (cons (string :tag "Tag name")
  2550. (character :tag "Access char"))
  2551. (const :tag "Start radio group" (:startgroup))
  2552. (const :tag "End radio group" (:endgroup))
  2553. (const :tag "New line" (:newline)))))
  2554. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2555. "If non-nil, always offer completion for all tags of all agenda files.
  2556. Instead of customizing this variable directly, you might want to
  2557. set it locally for capture buffers, because there no list of
  2558. tags in that file can be created dynamically (there are none).
  2559. (add-hook 'org-capture-mode-hook
  2560. (lambda ()
  2561. (set (make-local-variable
  2562. 'org-complete-tags-always-offer-all-agenda-tags)
  2563. t)))"
  2564. :group 'org-tags
  2565. :version "24.1"
  2566. :type 'boolean)
  2567. (defvar org-file-tags nil
  2568. "List of tags that can be inherited by all entries in the file.
  2569. The tags will be inherited if the variable `org-use-tag-inheritance'
  2570. says they should be.
  2571. This variable is populated from #+FILETAGS lines.")
  2572. (defcustom org-use-fast-tag-selection 'auto
  2573. "Non-nil means use fast tag selection scheme.
  2574. This is a special interface to select and deselect tags with single keys.
  2575. When nil, fast selection is never used.
  2576. When the symbol `auto', fast selection is used if and only if selection
  2577. characters for tags have been configured, either through the variable
  2578. `org-tag-alist' or through a #+TAGS line in the buffer.
  2579. When t, fast selection is always used and selection keys are assigned
  2580. automatically if necessary."
  2581. :group 'org-tags
  2582. :type '(choice
  2583. (const :tag "Always" t)
  2584. (const :tag "Never" nil)
  2585. (const :tag "When selection characters are configured" 'auto)))
  2586. (defcustom org-fast-tag-selection-single-key nil
  2587. "Non-nil means fast tag selection exits after first change.
  2588. When nil, you have to press RET to exit it.
  2589. During fast tag selection, you can toggle this flag with `C-c'.
  2590. This variable can also have the value `expert'. In this case, the window
  2591. displaying the tags menu is not even shown, until you press C-c again."
  2592. :group 'org-tags
  2593. :type '(choice
  2594. (const :tag "No" nil)
  2595. (const :tag "Yes" t)
  2596. (const :tag "Expert" expert)))
  2597. (defvar org-fast-tag-selection-include-todo nil
  2598. "Non-nil means fast tags selection interface will also offer TODO states.
  2599. This is an undocumented feature, you should not rely on it.")
  2600. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2601. "The column to which tags should be indented in a headline.
  2602. If this number is positive, it specifies the column. If it is negative,
  2603. it means that the tags should be flushright to that column. For example,
  2604. -80 works well for a normal 80 character screen.
  2605. When 0, place tags directly after headline text, with only one space in
  2606. between."
  2607. :group 'org-tags
  2608. :type 'integer)
  2609. (defcustom org-auto-align-tags t
  2610. "Non-nil keeps tags aligned when modifying headlines.
  2611. Some operations (i.e. demoting) change the length of a headline and
  2612. therefore shift the tags around. With this option turned on, after
  2613. each such operation the tags are again aligned to `org-tags-column'."
  2614. :group 'org-tags
  2615. :type 'boolean)
  2616. (defcustom org-use-tag-inheritance t
  2617. "Non-nil means tags in levels apply also for sublevels.
  2618. When nil, only the tags directly given in a specific line apply there.
  2619. This may also be a list of tags that should be inherited, or a regexp that
  2620. matches tags that should be inherited. Additional control is possible
  2621. with the variable `org-tags-exclude-from-inheritance' which gives an
  2622. explicit list of tags to be excluded from inheritance., even if the value of
  2623. `org-use-tag-inheritance' would select it for inheritance.
  2624. If this option is t, a match early-on in a tree can lead to a large
  2625. number of matches in the subtree when constructing the agenda or creating
  2626. a sparse tree. If you only want to see the first match in a tree during
  2627. a search, check out the variable `org-tags-match-list-sublevels'."
  2628. :group 'org-tags
  2629. :type '(choice
  2630. (const :tag "Not" nil)
  2631. (const :tag "Always" t)
  2632. (repeat :tag "Specific tags" (string :tag "Tag"))
  2633. (regexp :tag "Tags matched by regexp")))
  2634. (defcustom org-tags-exclude-from-inheritance nil
  2635. "List of tags that should never be inherited.
  2636. This is a way to exclude a few tags from inheritance. For way to do
  2637. the opposite, to actively allow inheritance for selected tags,
  2638. see the variable `org-use-tag-inheritance'."
  2639. :group 'org-tags
  2640. :type '(repeat (string :tag "Tag")))
  2641. (defun org-tag-inherit-p (tag)
  2642. "Check if TAG is one that should be inherited."
  2643. (cond
  2644. ((member tag org-tags-exclude-from-inheritance) nil)
  2645. ((eq org-use-tag-inheritance t) t)
  2646. ((not org-use-tag-inheritance) nil)
  2647. ((stringp org-use-tag-inheritance)
  2648. (string-match org-use-tag-inheritance tag))
  2649. ((listp org-use-tag-inheritance)
  2650. (member tag org-use-tag-inheritance))
  2651. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2652. (defcustom org-tags-match-list-sublevels t
  2653. "Non-nil means list also sublevels of headlines matching a search.
  2654. This variable applies to tags/property searches, and also to stuck
  2655. projects because this search is based on a tags match as well.
  2656. When set to the symbol `indented', sublevels are indented with
  2657. leading dots.
  2658. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2659. the sublevels of a headline matching a tag search often also match
  2660. the same search. Listing all of them can create very long lists.
  2661. Setting this variable to nil causes subtrees of a match to be skipped.
  2662. This variable is semi-obsolete and probably should always be true. It
  2663. is better to limit inheritance to certain tags using the variables
  2664. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2665. :group 'org-tags
  2666. :type '(choice
  2667. (const :tag "No, don't list them" nil)
  2668. (const :tag "Yes, do list them" t)
  2669. (const :tag "List them, indented with leading dots" indented)))
  2670. (defcustom org-tags-sort-function nil
  2671. "When set, tags are sorted using this function as a comparator."
  2672. :group 'org-tags
  2673. :type '(choice
  2674. (const :tag "No sorting" nil)
  2675. (const :tag "Alphabetical" string<)
  2676. (const :tag "Reverse alphabetical" string>)
  2677. (function :tag "Custom function" nil)))
  2678. (defvar org-tags-history nil
  2679. "History of minibuffer reads for tags.")
  2680. (defvar org-last-tags-completion-table nil
  2681. "The last used completion table for tags.")
  2682. (defvar org-after-tags-change-hook nil
  2683. "Hook that is run after the tags in a line have changed.")
  2684. (defgroup org-properties nil
  2685. "Options concerning properties in Org-mode."
  2686. :tag "Org Properties"
  2687. :group 'org)
  2688. (defcustom org-property-format "%-10s %s"
  2689. "How property key/value pairs should be formatted by `indent-line'.
  2690. When `indent-line' hits a property definition, it will format the line
  2691. according to this format, mainly to make sure that the values are
  2692. lined-up with respect to each other."
  2693. :group 'org-properties
  2694. :type 'string)
  2695. (defcustom org-properties-postprocess-alist nil
  2696. "Alist of properties and functions to adjust inserted values.
  2697. Elements of this alist must be of the form
  2698. ([string] [function])
  2699. where [string] must be a property name and [function] must be a
  2700. lambda expression: this lambda expression must take one argument,
  2701. the value to adjust, and return the new value as a string.
  2702. For example, this element will allow the property \"Remaining\"
  2703. to be updated wrt the relation between the \"Effort\" property
  2704. and the clock summary:
  2705. ((\"Remaining\" (lambda(value)
  2706. (let ((clocksum (org-clock-sum-current-item))
  2707. (effort (org-duration-string-to-minutes
  2708. (org-entry-get (point) \"Effort\"))))
  2709. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2710. :group 'org-properties
  2711. :version "24.1"
  2712. :type '(alist :key-type (string :tag "Property")
  2713. :value-type (function :tag "Function")))
  2714. (defcustom org-use-property-inheritance nil
  2715. "Non-nil means properties apply also for sublevels.
  2716. This setting is chiefly used during property searches. Turning it on can
  2717. cause significant overhead when doing a search, which is why it is not
  2718. on by default.
  2719. When nil, only the properties directly given in the current entry count.
  2720. When t, every property is inherited. The value may also be a list of
  2721. properties that should have inheritance, or a regular expression matching
  2722. properties that should be inherited.
  2723. However, note that some special properties use inheritance under special
  2724. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2725. and the properties ending in \"_ALL\" when they are used as descriptor
  2726. for valid values of a property.
  2727. Note for programmers:
  2728. When querying an entry with `org-entry-get', you can control if inheritance
  2729. should be used. By default, `org-entry-get' looks only at the local
  2730. properties. You can request inheritance by setting the inherit argument
  2731. to t (to force inheritance) or to `selective' (to respect the setting
  2732. in this variable)."
  2733. :group 'org-properties
  2734. :type '(choice
  2735. (const :tag "Not" nil)
  2736. (const :tag "Always" t)
  2737. (repeat :tag "Specific properties" (string :tag "Property"))
  2738. (regexp :tag "Properties matched by regexp")))
  2739. (defun org-property-inherit-p (property)
  2740. "Check if PROPERTY is one that should be inherited."
  2741. (cond
  2742. ((eq org-use-property-inheritance t) t)
  2743. ((not org-use-property-inheritance) nil)
  2744. ((stringp org-use-property-inheritance)
  2745. (string-match org-use-property-inheritance property))
  2746. ((listp org-use-property-inheritance)
  2747. (member property org-use-property-inheritance))
  2748. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2749. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2750. "The default column format, if no other format has been defined.
  2751. This variable can be set on the per-file basis by inserting a line
  2752. #+COLUMNS: %25ITEM ....."
  2753. :group 'org-properties
  2754. :type 'string)
  2755. (defcustom org-columns-ellipses ".."
  2756. "The ellipses to be used when a field in column view is truncated.
  2757. When this is the empty string, as many characters as possible are shown,
  2758. but then there will be no visual indication that the field has been truncated.
  2759. When this is a string of length N, the last N characters of a truncated
  2760. field are replaced by this string. If the column is narrower than the
  2761. ellipses string, only part of the ellipses string will be shown."
  2762. :group 'org-properties
  2763. :type 'string)
  2764. (defcustom org-columns-modify-value-for-display-function nil
  2765. "Function that modifies values for display in column view.
  2766. For example, it can be used to cut out a certain part from a time stamp.
  2767. The function must take 2 arguments:
  2768. column-title The title of the column (*not* the property name)
  2769. value The value that should be modified.
  2770. The function should return the value that should be displayed,
  2771. or nil if the normal value should be used."
  2772. :group 'org-properties
  2773. :type 'function)
  2774. (defcustom org-effort-property "Effort"
  2775. "The property that is being used to keep track of effort estimates.
  2776. Effort estimates given in this property need to have the format H:MM."
  2777. :group 'org-properties
  2778. :group 'org-progress
  2779. :type '(string :tag "Property"))
  2780. (defconst org-global-properties-fixed
  2781. '(("VISIBILITY_ALL" . "folded children content all")
  2782. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2783. "List of property/value pairs that can be inherited by any entry.
  2784. These are fixed values, for the preset properties. The user variable
  2785. that can be used to add to this list is `org-global-properties'.
  2786. The entries in this list are cons cells where the car is a property
  2787. name and cdr is a string with the value. If the value represents
  2788. multiple items like an \"_ALL\" property, separate the items by
  2789. spaces.")
  2790. (defcustom org-global-properties nil
  2791. "List of property/value pairs that can be inherited by any entry.
  2792. This list will be combined with the constant `org-global-properties-fixed'.
  2793. The entries in this list are cons cells where the car is a property
  2794. name and cdr is a string with the value.
  2795. You can set buffer-local values for the same purpose in the variable
  2796. `org-file-properties' this by adding lines like
  2797. #+PROPERTY: NAME VALUE"
  2798. :group 'org-properties
  2799. :type '(repeat
  2800. (cons (string :tag "Property")
  2801. (string :tag "Value"))))
  2802. (defvar org-file-properties nil
  2803. "List of property/value pairs that can be inherited by any entry.
  2804. Valid for the current buffer.
  2805. This variable is populated from #+PROPERTY lines.")
  2806. (make-variable-buffer-local 'org-file-properties)
  2807. (defgroup org-agenda nil
  2808. "Options concerning agenda views in Org-mode."
  2809. :tag "Org Agenda"
  2810. :group 'org)
  2811. (defvar org-category nil
  2812. "Variable used by org files to set a category for agenda display.
  2813. Such files should use a file variable to set it, for example
  2814. # -*- mode: org; org-category: \"ELisp\"
  2815. or contain a special line
  2816. #+CATEGORY: ELisp
  2817. If the file does not specify a category, then file's base name
  2818. is used instead.")
  2819. (make-variable-buffer-local 'org-category)
  2820. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2821. (defcustom org-agenda-files nil
  2822. "The files to be used for agenda display.
  2823. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2824. \\[org-remove-file]. You can also use customize to edit the list.
  2825. If an entry is a directory, all files in that directory that are matched by
  2826. `org-agenda-file-regexp' will be part of the file list.
  2827. If the value of the variable is not a list but a single file name, then
  2828. the list of agenda files is actually stored and maintained in that file, one
  2829. agenda file per line. In this file paths can be given relative to
  2830. `org-directory'. Tilde expansion and environment variable substitution
  2831. are also made."
  2832. :group 'org-agenda
  2833. :type '(choice
  2834. (repeat :tag "List of files and directories" file)
  2835. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2836. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2837. "Regular expression to match files for `org-agenda-files'.
  2838. If any element in the list in that variable contains a directory instead
  2839. of a normal file, all files in that directory that are matched by this
  2840. regular expression will be included."
  2841. :group 'org-agenda
  2842. :type 'regexp)
  2843. (defcustom org-agenda-text-search-extra-files nil
  2844. "List of extra files to be searched by text search commands.
  2845. These files will be search in addition to the agenda files by the
  2846. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2847. Note that these files will only be searched for text search commands,
  2848. not for the other agenda views like todo lists, tag searches or the weekly
  2849. agenda. This variable is intended to list notes and possibly archive files
  2850. that should also be searched by these two commands.
  2851. In fact, if the first element in the list is the symbol `agenda-archives',
  2852. than all archive files of all agenda files will be added to the search
  2853. scope."
  2854. :group 'org-agenda
  2855. :type '(set :greedy t
  2856. (const :tag "Agenda Archives" agenda-archives)
  2857. (repeat :inline t (file))))
  2858. (if (fboundp 'defvaralias)
  2859. (defvaralias 'org-agenda-multi-occur-extra-files
  2860. 'org-agenda-text-search-extra-files))
  2861. (defcustom org-agenda-skip-unavailable-files nil
  2862. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2863. A nil value means to remove them, after a query, from the list."
  2864. :group 'org-agenda
  2865. :type 'boolean)
  2866. (defcustom org-calendar-to-agenda-key [?c]
  2867. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2868. The command `org-calendar-goto-agenda' will be bound to this key. The
  2869. default is the character `c' because then `c' can be used to switch back and
  2870. forth between agenda and calendar."
  2871. :group 'org-agenda
  2872. :type 'sexp)
  2873. (defcustom org-calendar-insert-diary-entry-key [?i]
  2874. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2875. This option is irrelevant until `org-agenda-diary-file' has been configured
  2876. to point to an Org-mode file. When that is the case, the command
  2877. `org-agenda-diary-entry' will be bound to the key given here, by default
  2878. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2879. if you want to continue doing this, you need to change this to a different
  2880. key."
  2881. :group 'org-agenda
  2882. :type 'sexp)
  2883. (defcustom org-agenda-diary-file 'diary-file
  2884. "File to which to add new entries with the `i' key in agenda and calendar.
  2885. When this is the symbol `diary-file', the functionality in the Emacs
  2886. calendar will be used to add entries to the `diary-file'. But when this
  2887. points to a file, `org-agenda-diary-entry' will be used instead."
  2888. :group 'org-agenda
  2889. :type '(choice
  2890. (const :tag "The standard Emacs diary file" diary-file)
  2891. (file :tag "Special Org file diary entries")))
  2892. (eval-after-load "calendar"
  2893. '(progn
  2894. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2895. 'org-calendar-goto-agenda)
  2896. (add-hook 'calendar-mode-hook
  2897. (lambda ()
  2898. (unless (eq org-agenda-diary-file 'diary-file)
  2899. (define-key calendar-mode-map
  2900. org-calendar-insert-diary-entry-key
  2901. 'org-agenda-diary-entry))))))
  2902. (defgroup org-latex nil
  2903. "Options for embedding LaTeX code into Org-mode."
  2904. :tag "Org LaTeX"
  2905. :group 'org)
  2906. (defcustom org-format-latex-options
  2907. '(:foreground default :background default :scale 1.0
  2908. :html-foreground "Black" :html-background "Transparent"
  2909. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2910. "Options for creating images from LaTeX fragments.
  2911. This is a property list with the following properties:
  2912. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2913. `default' means use the foreground of the default face.
  2914. `auto' means use the foreground from the text face.
  2915. :background the background color, or \"Transparent\".
  2916. `default' means use the background of the default face.
  2917. `auto' means use the background from the text face.
  2918. :scale a scaling factor for the size of the images, to get more pixels
  2919. :html-foreground, :html-background, :html-scale
  2920. the same numbers for HTML export.
  2921. :matchers a list indicating which matchers should be used to
  2922. find LaTeX fragments. Valid members of this list are:
  2923. \"begin\" find environments
  2924. \"$1\" find single characters surrounded by $.$
  2925. \"$\" find math expressions surrounded by $...$
  2926. \"$$\" find math expressions surrounded by $$....$$
  2927. \"\\(\" find math expressions surrounded by \\(...\\)
  2928. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2929. :group 'org-latex
  2930. :type 'plist)
  2931. (defcustom org-format-latex-signal-error t
  2932. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2933. When nil, just push out a message."
  2934. :group 'org-latex
  2935. :version "24.1"
  2936. :type 'boolean)
  2937. (defcustom org-latex-to-mathml-jar-file nil
  2938. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2939. Use this to specify additional executable file say a jar file.
  2940. When using MathToWeb as the converter, specify the full-path to
  2941. your mathtoweb.jar file."
  2942. :group 'org-latex
  2943. :version "24.1"
  2944. :type '(choice
  2945. (const :tag "None" nil)
  2946. (file :tag "JAR file" :must-match t)))
  2947. (defcustom org-latex-to-mathml-convert-command nil
  2948. "Command to convert LaTeX fragments to MathML.
  2949. Replace format-specifiers in the command as noted below and use
  2950. `shell-command' to convert LaTeX to MathML.
  2951. %j: Executable file in fully expanded form as specified by
  2952. `org-latex-to-mathml-jar-file'.
  2953. %I: Input LaTeX file in fully expanded form
  2954. %o: Output MathML file
  2955. This command is used by `org-create-math-formula'.
  2956. When using MathToWeb as the converter, set this to
  2957. \"java -jar %j -unicode -force -df %o %I\"."
  2958. :group 'org-latex
  2959. :version "24.1"
  2960. :type '(choice
  2961. (const :tag "None" nil)
  2962. (string :tag "\nShell command")))
  2963. (defcustom org-latex-create-formula-image-program 'dvipng
  2964. "Program to convert LaTeX fragments with.
  2965. dvipng Process the LaTeX fragments to dvi file, then convert
  2966. dvi files to png files using dvipng.
  2967. This will also include processing of non-math environments.
  2968. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2969. to convert pdf files to png files"
  2970. :group 'org-latex
  2971. :version "24.1"
  2972. :type '(choice
  2973. (const :tag "dvipng" dvipng)
  2974. (const :tag "imagemagick" imagemagick)))
  2975. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  2976. "Path to store latex preview images. A relative path here creates many
  2977. directories relative to the processed org files paths. An absolute path
  2978. puts all preview images at the same place."
  2979. :group 'org-latex
  2980. :version "24.3"
  2981. :type 'string)
  2982. (defun org-format-latex-mathml-available-p ()
  2983. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2984. (save-match-data
  2985. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2986. org-latex-to-mathml-convert-command)
  2987. (let ((executable (car (split-string
  2988. org-latex-to-mathml-convert-command))))
  2989. (when (executable-find executable)
  2990. (if (string-match
  2991. "%j" org-latex-to-mathml-convert-command)
  2992. (file-readable-p org-latex-to-mathml-jar-file)
  2993. t))))))
  2994. (defcustom org-format-latex-header "\\documentclass{article}
  2995. \\usepackage[usenames]{color}
  2996. \\usepackage{amsmath}
  2997. \\usepackage[mathscr]{eucal}
  2998. \\pagestyle{empty} % do not remove
  2999. \[PACKAGES]
  3000. \[DEFAULT-PACKAGES]
  3001. % The settings below are copied from fullpage.sty
  3002. \\setlength{\\textwidth}{\\paperwidth}
  3003. \\addtolength{\\textwidth}{-3cm}
  3004. \\setlength{\\oddsidemargin}{1.5cm}
  3005. \\addtolength{\\oddsidemargin}{-2.54cm}
  3006. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3007. \\setlength{\\textheight}{\\paperheight}
  3008. \\addtolength{\\textheight}{-\\headheight}
  3009. \\addtolength{\\textheight}{-\\headsep}
  3010. \\addtolength{\\textheight}{-\\footskip}
  3011. \\addtolength{\\textheight}{-3cm}
  3012. \\setlength{\\topmargin}{1.5cm}
  3013. \\addtolength{\\topmargin}{-2.54cm}"
  3014. "The document header used for processing LaTeX fragments.
  3015. It is imperative that this header make sure that no page number
  3016. appears on the page. The package defined in the variables
  3017. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3018. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3019. will be appended."
  3020. :group 'org-latex
  3021. :type 'string)
  3022. (defvar org-format-latex-header-extra nil)
  3023. (defun org-set-packages-alist (var val)
  3024. "Set the packages alist and make sure it has 3 elements per entry."
  3025. (set var (mapcar (lambda (x)
  3026. (if (and (consp x) (= (length x) 2))
  3027. (list (car x) (nth 1 x) t)
  3028. x))
  3029. val)))
  3030. (defun org-get-packages-alist (var)
  3031. "Get the packages alist and make sure it has 3 elements per entry."
  3032. (mapcar (lambda (x)
  3033. (if (and (consp x) (= (length x) 2))
  3034. (list (car x) (nth 1 x) t)
  3035. x))
  3036. (default-value var)))
  3037. ;; The following variables are defined here because is it also used
  3038. ;; when formatting latex fragments. Originally it was part of the
  3039. ;; LaTeX exporter, which is why the name includes "export".
  3040. (defcustom org-export-latex-default-packages-alist
  3041. '(("AUTO" "inputenc" t)
  3042. ("T1" "fontenc" t)
  3043. ("" "fixltx2e" nil)
  3044. ("" "graphicx" t)
  3045. ("" "longtable" nil)
  3046. ("" "float" nil)
  3047. ("" "wrapfig" nil)
  3048. ("" "soul" t)
  3049. ("" "textcomp" t)
  3050. ("" "marvosym" t)
  3051. ("" "wasysym" t)
  3052. ("" "latexsym" t)
  3053. ("" "amssymb" t)
  3054. ("" "hyperref" nil)
  3055. "\\tolerance=1000"
  3056. )
  3057. "Alist of default packages to be inserted in the header.
  3058. Change this only if one of the packages here causes an incompatibility
  3059. with another package you are using.
  3060. The packages in this list are needed by one part or another of Org-mode
  3061. to function properly.
  3062. - inputenc, fontenc: for basic font and character selection
  3063. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3064. for interpreting the entities in `org-entities'. You can skip some of these
  3065. packages if you don't use any of the symbols in it.
  3066. - graphicx: for including images
  3067. - float, wrapfig: for figure placement
  3068. - longtable: for long tables
  3069. - hyperref: for cross references
  3070. Therefore you should not modify this variable unless you know what you
  3071. are doing. The one reason to change it anyway is that you might be loading
  3072. some other package that conflicts with one of the default packages.
  3073. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3074. If SNIPPET-FLAG is t, the package also needs to be included when
  3075. compiling LaTeX snippets into images for inclusion into HTML."
  3076. :group 'org-export-latex
  3077. :set 'org-set-packages-alist
  3078. :get 'org-get-packages-alist
  3079. :version "24.1"
  3080. :type '(repeat
  3081. (choice
  3082. (list :tag "options/package pair"
  3083. (string :tag "options")
  3084. (string :tag "package")
  3085. (boolean :tag "Snippet"))
  3086. (string :tag "A line of LaTeX"))))
  3087. (defcustom org-export-latex-packages-alist nil
  3088. "Alist of packages to be inserted in every LaTeX header.
  3089. These will be inserted after `org-export-latex-default-packages-alist'.
  3090. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3091. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3092. turning LaTeX snippets into images for inclusion into HTML.
  3093. Make sure that you only list packages here which:
  3094. - you want in every file
  3095. - do not conflict with the default packages in
  3096. `org-export-latex-default-packages-alist'
  3097. - do not conflict with the setup in `org-format-latex-header'."
  3098. :group 'org-export-latex
  3099. :set 'org-set-packages-alist
  3100. :get 'org-get-packages-alist
  3101. :type '(repeat
  3102. (choice
  3103. (list :tag "options/package pair"
  3104. (string :tag "options")
  3105. (string :tag "package")
  3106. (boolean :tag "Snippet"))
  3107. (string :tag "A line of LaTeX"))))
  3108. (defgroup org-appearance nil
  3109. "Settings for Org-mode appearance."
  3110. :tag "Org Appearance"
  3111. :group 'org)
  3112. (defcustom org-level-color-stars-only nil
  3113. "Non-nil means fontify only the stars in each headline.
  3114. When nil, the entire headline is fontified.
  3115. Changing it requires restart of `font-lock-mode' to become effective
  3116. also in regions already fontified."
  3117. :group 'org-appearance
  3118. :type 'boolean)
  3119. (defcustom org-hide-leading-stars nil
  3120. "Non-nil means hide the first N-1 stars in a headline.
  3121. This works by using the face `org-hide' for these stars. This
  3122. face is white for a light background, and black for a dark
  3123. background. You may have to customize the face `org-hide' to
  3124. make this work.
  3125. Changing it requires restart of `font-lock-mode' to become effective
  3126. also in regions already fontified.
  3127. You may also set this on a per-file basis by adding one of the following
  3128. lines to the buffer:
  3129. #+STARTUP: hidestars
  3130. #+STARTUP: showstars"
  3131. :group 'org-appearance
  3132. :type 'boolean)
  3133. (defcustom org-hidden-keywords nil
  3134. "List of symbols corresponding to keywords to be hidden the org buffer.
  3135. For example, a value '(title) for this list will make the document's title
  3136. appear in the buffer without the initial #+TITLE: keyword."
  3137. :group 'org-appearance
  3138. :version "24.1"
  3139. :type '(set (const :tag "#+AUTHOR" author)
  3140. (const :tag "#+DATE" date)
  3141. (const :tag "#+EMAIL" email)
  3142. (const :tag "#+TITLE" title)))
  3143. (defcustom org-custom-properties nil
  3144. "List of properties (as strings) with a special meaning.
  3145. The default use of these custom properties is to let the user
  3146. hide them with `org-toggle-custom-properties-visibility'."
  3147. :group 'org-properties
  3148. :group 'org-appearance
  3149. :version "24.3"
  3150. :type '(repeat (string :tag "Property Name")))
  3151. (defcustom org-fontify-done-headline nil
  3152. "Non-nil means change the face of a headline if it is marked DONE.
  3153. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3154. When this is non-nil, the headline after the keyword is set to the
  3155. `org-headline-done' as an additional indication."
  3156. :group 'org-appearance
  3157. :type 'boolean)
  3158. (defcustom org-fontify-emphasized-text t
  3159. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3160. Changing this variable requires a restart of Emacs to take effect."
  3161. :group 'org-appearance
  3162. :type 'boolean)
  3163. (defcustom org-fontify-whole-heading-line nil
  3164. "Non-nil means fontify the whole line for headings.
  3165. This is useful when setting a background color for the
  3166. org-level-* faces."
  3167. :group 'org-appearance
  3168. :type 'boolean)
  3169. (defcustom org-highlight-latex-fragments-and-specials nil
  3170. "Non-nil means fontify what is treated specially by the exporters."
  3171. :group 'org-appearance
  3172. :type 'boolean)
  3173. (defcustom org-hide-emphasis-markers nil
  3174. "Non-nil mean font-lock should hide the emphasis marker characters."
  3175. :group 'org-appearance
  3176. :type 'boolean)
  3177. (defcustom org-pretty-entities nil
  3178. "Non-nil means show entities as UTF8 characters.
  3179. When nil, the \\name form remains in the buffer."
  3180. :group 'org-appearance
  3181. :version "24.1"
  3182. :type 'boolean)
  3183. (defcustom org-pretty-entities-include-sub-superscripts t
  3184. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3185. :group 'org-appearance
  3186. :version "24.1"
  3187. :type 'boolean)
  3188. (defvar org-emph-re nil
  3189. "Regular expression for matching emphasis.
  3190. After a match, the match groups contain these elements:
  3191. 0 The match of the full regular expression, including the characters
  3192. before and after the proper match
  3193. 1 The character before the proper match, or empty at beginning of line
  3194. 2 The proper match, including the leading and trailing markers
  3195. 3 The leading marker like * or /, indicating the type of highlighting
  3196. 4 The text between the emphasis markers, not including the markers
  3197. 5 The character after the match, empty at the end of a line")
  3198. (defvar org-verbatim-re nil
  3199. "Regular expression for matching verbatim text.")
  3200. (defvar org-emphasis-regexp-components) ; defined just below
  3201. (defvar org-emphasis-alist) ; defined just below
  3202. (defun org-set-emph-re (var val)
  3203. "Set variable and compute the emphasis regular expression."
  3204. (set var val)
  3205. (when (and (boundp 'org-emphasis-alist)
  3206. (boundp 'org-emphasis-regexp-components)
  3207. org-emphasis-alist org-emphasis-regexp-components)
  3208. (let* ((e org-emphasis-regexp-components)
  3209. (pre (car e))
  3210. (post (nth 1 e))
  3211. (border (nth 2 e))
  3212. (body (nth 3 e))
  3213. (nl (nth 4 e))
  3214. (body1 (concat body "*?"))
  3215. (markers (mapconcat 'car org-emphasis-alist ""))
  3216. (vmarkers (mapconcat
  3217. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3218. org-emphasis-alist "")))
  3219. ;; make sure special characters appear at the right position in the class
  3220. (if (string-match "\\^" markers)
  3221. (setq markers (concat (replace-match "" t t markers) "^")))
  3222. (if (string-match "-" markers)
  3223. (setq markers (concat (replace-match "" t t markers) "-")))
  3224. (if (string-match "\\^" vmarkers)
  3225. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3226. (if (string-match "-" vmarkers)
  3227. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3228. (if (> nl 0)
  3229. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3230. (int-to-string nl) "\\}")))
  3231. ;; Make the regexp
  3232. (setq org-emph-re
  3233. (concat "\\([" pre "]\\|^\\)"
  3234. "\\("
  3235. "\\([" markers "]\\)"
  3236. "\\("
  3237. "[^" border "]\\|"
  3238. "[^" border "]"
  3239. body1
  3240. "[^" border "]"
  3241. "\\)"
  3242. "\\3\\)"
  3243. "\\([" post "]\\|$\\)"))
  3244. (setq org-verbatim-re
  3245. (concat "\\([" pre "]\\|^\\)"
  3246. "\\("
  3247. "\\([" vmarkers "]\\)"
  3248. "\\("
  3249. "[^" border "]\\|"
  3250. "[^" border "]"
  3251. body1
  3252. "[^" border "]"
  3253. "\\)"
  3254. "\\3\\)"
  3255. "\\([" post "]\\|$\\)")))))
  3256. (defcustom org-emphasis-regexp-components
  3257. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3258. "Components used to build the regular expression for emphasis.
  3259. This is a list with five entries. Terminology: In an emphasis string
  3260. like \" *strong word* \", we call the initial space PREMATCH, the final
  3261. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3262. and \"trong wor\" is the body. The different components in this variable
  3263. specify what is allowed/forbidden in each part:
  3264. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3265. post Chars allowed as postmatch. End of line will be allowed too.
  3266. border The chars *forbidden* as border characters.
  3267. body-regexp A regexp like \".\" to match a body character. Don't use
  3268. non-shy groups here, and don't allow newline here.
  3269. newline The maximum number of newlines allowed in an emphasis exp.
  3270. Use customize to modify this, or restart Emacs after changing it."
  3271. :group 'org-appearance
  3272. :set 'org-set-emph-re
  3273. :type '(list
  3274. (sexp :tag "Allowed chars in pre ")
  3275. (sexp :tag "Allowed chars in post ")
  3276. (sexp :tag "Forbidden chars in border ")
  3277. (sexp :tag "Regexp for body ")
  3278. (integer :tag "number of newlines allowed")
  3279. (option (boolean :tag "Please ignore this button"))))
  3280. (defcustom org-emphasis-alist
  3281. `(("*" bold "<b>" "</b>")
  3282. ("/" italic "<i>" "</i>")
  3283. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3284. ("=" org-code "<code>" "</code>" verbatim)
  3285. ("~" org-verbatim "<code>" "</code>" verbatim)
  3286. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3287. "<del>" "</del>")
  3288. )
  3289. "Special syntax for emphasized text.
  3290. Text starting and ending with a special character will be emphasized, for
  3291. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3292. characters, the face to be used by font-lock for highlighting in Org-mode
  3293. Emacs buffers, and the HTML tags to be used for this.
  3294. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3295. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3296. Use customize to modify this, or restart Emacs after changing it."
  3297. :group 'org-appearance
  3298. :set 'org-set-emph-re
  3299. :type '(repeat
  3300. (list
  3301. (string :tag "Marker character")
  3302. (choice
  3303. (face :tag "Font-lock-face")
  3304. (plist :tag "Face property list"))
  3305. (string :tag "HTML start tag")
  3306. (string :tag "HTML end tag")
  3307. (option (const verbatim)))))
  3308. (defvar org-protecting-blocks
  3309. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3310. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3311. This is needed for font-lock setup.")
  3312. ;;; Miscellaneous options
  3313. (defgroup org-completion nil
  3314. "Completion in Org-mode."
  3315. :tag "Org Completion"
  3316. :group 'org)
  3317. (defcustom org-completion-use-ido nil
  3318. "Non-nil means use ido completion wherever possible.
  3319. Note that `ido-mode' must be active for this variable to be relevant.
  3320. If you decide to turn this variable on, you might well want to turn off
  3321. `org-outline-path-complete-in-steps'.
  3322. See also `org-completion-use-iswitchb'."
  3323. :group 'org-completion
  3324. :type 'boolean)
  3325. (defcustom org-completion-use-iswitchb nil
  3326. "Non-nil means use iswitchb completion wherever possible.
  3327. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3328. If you decide to turn this variable on, you might well want to turn off
  3329. `org-outline-path-complete-in-steps'.
  3330. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3331. :group 'org-completion
  3332. :type 'boolean)
  3333. (defcustom org-completion-fallback-command 'hippie-expand
  3334. "The expansion command called by \\[pcomplete] in normal context.
  3335. Normal means, no org-mode-specific context."
  3336. :group 'org-completion
  3337. :type 'function)
  3338. ;;; Functions and variables from their packages
  3339. ;; Declared here to avoid compiler warnings
  3340. ;; XEmacs only
  3341. (defvar outline-mode-menu-heading)
  3342. (defvar outline-mode-menu-show)
  3343. (defvar outline-mode-menu-hide)
  3344. (defvar zmacs-regions) ; XEmacs regions
  3345. ;; Emacs only
  3346. (defvar mark-active)
  3347. ;; Various packages
  3348. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3349. (declare-function calendar-forward-day "cal-move" (arg))
  3350. (declare-function calendar-goto-date "cal-move" (date))
  3351. (declare-function calendar-goto-today "cal-move" ())
  3352. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3353. (defvar calc-embedded-close-formula)
  3354. (defvar calc-embedded-open-formula)
  3355. (declare-function cdlatex-tab "ext:cdlatex" ())
  3356. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3357. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3358. (defvar font-lock-unfontify-region-function)
  3359. (declare-function iswitchb-read-buffer "iswitchb"
  3360. (prompt &optional default require-match start matches-set))
  3361. (defvar iswitchb-temp-buflist)
  3362. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3363. (defvar org-agenda-tags-todo-honor-ignore-options)
  3364. (declare-function org-agenda-skip "org-agenda" ())
  3365. (declare-function
  3366. org-agenda-format-item "org-agenda"
  3367. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3368. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3369. (declare-function org-agenda-change-all-lines "org-agenda"
  3370. (newhead hdmarker &optional fixface just-this))
  3371. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3372. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3373. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3374. (beg end))
  3375. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3376. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3377. "org-agenda" (&optional end))
  3378. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3379. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3380. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3381. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3382. (declare-function org-indent-mode "org-indent" (&optional arg))
  3383. (declare-function parse-time-string "parse-time" (string))
  3384. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3385. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3386. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3387. (defvar remember-data-file)
  3388. (defvar texmathp-why)
  3389. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3390. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3391. (defvar w3m-current-url)
  3392. (defvar w3m-current-title)
  3393. (defvar org-latex-regexps)
  3394. ;;; Autoload and prepare some org modules
  3395. ;; Some table stuff that needs to be defined here, because it is used
  3396. ;; by the functions setting up org-mode or checking for table context.
  3397. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3398. "Detect an org-type or table-type table.")
  3399. (defconst org-table-line-regexp "^[ \t]*|"
  3400. "Detect an org-type table line.")
  3401. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3402. "Detect an org-type table line.")
  3403. (defconst org-table-hline-regexp "^[ \t]*|-"
  3404. "Detect an org-type table hline.")
  3405. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3406. "Detect a table-type table hline.")
  3407. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3408. "Detect the first line outside a table when searching from within it.
  3409. This works for both table types.")
  3410. ;; Autoload the functions in org-table.el that are needed by functions here.
  3411. (eval-and-compile
  3412. (org-autoload "org-table"
  3413. '(org-table-begin org-table-blank-field org-table-end)))
  3414. ;;;###autoload
  3415. (defun turn-on-orgtbl ()
  3416. "Unconditionally turn on `orgtbl-mode'."
  3417. (require 'org-table)
  3418. (orgtbl-mode 1))
  3419. (defun org-at-table-p (&optional table-type)
  3420. "Return t if the cursor is inside an org-type table.
  3421. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3422. (if org-enable-table-editor
  3423. (save-excursion
  3424. (beginning-of-line 1)
  3425. (looking-at (if table-type org-table-any-line-regexp
  3426. org-table-line-regexp)))
  3427. nil))
  3428. (defsubst org-table-p () (org-at-table-p))
  3429. (defun org-at-table.el-p ()
  3430. "Return t if and only if we are at a table.el table."
  3431. (and (org-at-table-p 'any)
  3432. (save-excursion
  3433. (goto-char (org-table-begin 'any))
  3434. (looking-at org-table1-hline-regexp))))
  3435. (defun org-table-recognize-table.el ()
  3436. "If there is a table.el table nearby, recognize it and move into it."
  3437. (if org-table-tab-recognizes-table.el
  3438. (if (org-at-table.el-p)
  3439. (progn
  3440. (beginning-of-line 1)
  3441. (if (looking-at org-table-dataline-regexp)
  3442. nil
  3443. (if (looking-at org-table1-hline-regexp)
  3444. (progn
  3445. (beginning-of-line 2)
  3446. (if (looking-at org-table-any-border-regexp)
  3447. (beginning-of-line -1)))))
  3448. (if (re-search-forward "|" (org-table-end t) t)
  3449. (progn
  3450. (require 'table)
  3451. (if (table--at-cell-p (point))
  3452. t
  3453. (message "recognizing table.el table...")
  3454. (table-recognize-table)
  3455. (message "recognizing table.el table...done")))
  3456. (error "This should not happen"))
  3457. t)
  3458. nil)
  3459. nil))
  3460. (defun org-at-table-hline-p ()
  3461. "Return t if the cursor is inside a hline in a table."
  3462. (if org-enable-table-editor
  3463. (save-excursion
  3464. (beginning-of-line 1)
  3465. (looking-at org-table-hline-regexp))
  3466. nil))
  3467. (defvar org-table-clean-did-remove-column nil)
  3468. (defun org-table-map-tables (function &optional quietly)
  3469. "Apply FUNCTION to the start of all tables in the buffer."
  3470. (save-excursion
  3471. (save-restriction
  3472. (widen)
  3473. (goto-char (point-min))
  3474. (while (re-search-forward org-table-any-line-regexp nil t)
  3475. (unless quietly
  3476. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3477. (beginning-of-line 1)
  3478. (when (and (looking-at org-table-line-regexp)
  3479. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3480. (not (org-in-block-p '("src" "example"))))
  3481. (save-excursion (funcall function))
  3482. (or (looking-at org-table-line-regexp)
  3483. (forward-char 1)))
  3484. (re-search-forward org-table-any-border-regexp nil 1))))
  3485. (unless quietly (message "Mapping tables: done")))
  3486. ;; Declare and autoload functions from org-exp.el & Co
  3487. (declare-function org-default-export-plist "org-exp")
  3488. (declare-function org-infile-export-plist "org-exp")
  3489. (declare-function org-get-current-options "org-exp")
  3490. ;; Declare and autoload functions from org-agenda.el
  3491. (eval-and-compile
  3492. (org-autoload "org-agenda"
  3493. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3494. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3495. (declare-function org-clock-update-mode-line "org-clock" ())
  3496. (declare-function org-resolve-clocks "org-clock"
  3497. (&optional also-non-dangling-p prompt last-valid))
  3498. (defvar org-clock-start-time)
  3499. (defvar org-clock-marker (make-marker)
  3500. "Marker recording the last clock-in.")
  3501. (defvar org-clock-hd-marker (make-marker)
  3502. "Marker recording the last clock-in, but the headline position.")
  3503. (defvar org-clock-heading ""
  3504. "The heading of the current clock entry.")
  3505. (defun org-clock-is-active ()
  3506. "Return non-nil if clock is currently running.
  3507. The return value is actually the clock marker."
  3508. (marker-buffer org-clock-marker))
  3509. (eval-and-compile
  3510. (org-autoload "org-clock" '(org-clock-remove-overlays
  3511. org-clock-update-time-maybe
  3512. org-clocktable-shift)))
  3513. (defun org-check-running-clock ()
  3514. "Check if the current buffer contains the running clock.
  3515. If yes, offer to stop it and to save the buffer with the changes."
  3516. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3517. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3518. (buffer-name))))
  3519. (org-clock-out)
  3520. (when (y-or-n-p "Save changed buffer?")
  3521. (save-buffer))))
  3522. (defun org-clocktable-try-shift (dir n)
  3523. "Check if this line starts a clock table, if yes, shift the time block."
  3524. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3525. (org-clocktable-shift dir n)))
  3526. ;;;###autoload
  3527. (defun org-clock-persistence-insinuate ()
  3528. "Set up hooks for clock persistence."
  3529. (require 'org-clock)
  3530. (add-hook 'org-mode-hook 'org-clock-load)
  3531. (add-hook 'kill-emacs-hook 'org-clock-save))
  3532. ;; Define the variable already here, to make sure we have it.
  3533. (defvar org-indent-mode nil
  3534. "Non-nil if Org-Indent mode is enabled.
  3535. Use the command `org-indent-mode' to change this variable.")
  3536. ;; Autoload archiving code
  3537. ;; The stuff that is needed for cycling and tags has to be defined here.
  3538. (defgroup org-archive nil
  3539. "Options concerning archiving in Org-mode."
  3540. :tag "Org Archive"
  3541. :group 'org-structure)
  3542. (defcustom org-archive-location "%s_archive::"
  3543. "The location where subtrees should be archived.
  3544. The value of this variable is a string, consisting of two parts,
  3545. separated by a double-colon. The first part is a filename and
  3546. the second part is a headline.
  3547. When the filename is omitted, archiving happens in the same file.
  3548. %s in the filename will be replaced by the current file
  3549. name (without the directory part). Archiving to a different file
  3550. is useful to keep archived entries from contributing to the
  3551. Org-mode Agenda.
  3552. The archived entries will be filed as subtrees of the specified
  3553. headline. When the headline is omitted, the subtrees are simply
  3554. filed away at the end of the file, as top-level entries. Also in
  3555. the heading you can use %s to represent the file name, this can be
  3556. useful when using the same archive for a number of different files.
  3557. Here are a few examples:
  3558. \"%s_archive::\"
  3559. If the current file is Projects.org, archive in file
  3560. Projects.org_archive, as top-level trees. This is the default.
  3561. \"::* Archived Tasks\"
  3562. Archive in the current file, under the top-level headline
  3563. \"* Archived Tasks\".
  3564. \"~/org/archive.org::\"
  3565. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3566. \"~/org/archive.org::* From %s\"
  3567. Archive in file ~/org/archive.org (absolute path), under headlines
  3568. \"From FILENAME\" where file name is the current file name.
  3569. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3570. The \"datetree/\" string is special, signifying to archive
  3571. items to the datetree. Items are placed in either the CLOSED
  3572. date of the item, or the current date if there is no CLOSED date.
  3573. The heading will be a subentry to the current date. There doesn't
  3574. need to be a heading, but there always needs to be a slash after
  3575. datetree. For example, to store archived items directly in the
  3576. datetree, use \"~/org/datetree.org::datetree/\".
  3577. \"basement::** Finished Tasks\"
  3578. Archive in file ./basement (relative path), as level 3 trees
  3579. below the level 2 heading \"** Finished Tasks\".
  3580. You may set this option on a per-file basis by adding to the buffer a
  3581. line like
  3582. #+ARCHIVE: basement::** Finished Tasks
  3583. You may also define it locally for a subtree by setting an ARCHIVE property
  3584. in the entry. If such a property is found in an entry, or anywhere up
  3585. the hierarchy, it will be used."
  3586. :group 'org-archive
  3587. :type 'string)
  3588. (defcustom org-archive-tag "ARCHIVE"
  3589. "The tag that marks a subtree as archived.
  3590. An archived subtree does not open during visibility cycling, and does
  3591. not contribute to the agenda listings.
  3592. After changing this, font-lock must be restarted in the relevant buffers to
  3593. get the proper fontification."
  3594. :group 'org-archive
  3595. :group 'org-keywords
  3596. :type 'string)
  3597. (defcustom org-agenda-skip-archived-trees t
  3598. "Non-nil means the agenda will skip any items located in archived trees.
  3599. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3600. variable is no longer recommended, you should leave it at the value t.
  3601. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3602. :group 'org-archive
  3603. :group 'org-agenda-skip
  3604. :type 'boolean)
  3605. (defcustom org-columns-skip-archived-trees t
  3606. "Non-nil means ignore archived trees when creating column view."
  3607. :group 'org-archive
  3608. :group 'org-properties
  3609. :type 'boolean)
  3610. (defcustom org-cycle-open-archived-trees nil
  3611. "Non-nil means `org-cycle' will open archived trees.
  3612. An archived tree is a tree marked with the tag ARCHIVE.
  3613. When nil, archived trees will stay folded. You can still open them with
  3614. normal outline commands like `show-all', but not with the cycling commands."
  3615. :group 'org-archive
  3616. :group 'org-cycle
  3617. :type 'boolean)
  3618. (defcustom org-sparse-tree-open-archived-trees nil
  3619. "Non-nil means sparse tree construction shows matches in archived trees.
  3620. When nil, matches in these trees are highlighted, but the trees are kept in
  3621. collapsed state."
  3622. :group 'org-archive
  3623. :group 'org-sparse-trees
  3624. :type 'boolean)
  3625. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3626. "The default date type when building a sparse tree.
  3627. When this is nil, a date is a scheduled or a deadline timestamp.
  3628. Otherwise, these types are allowed:
  3629. all: all timestamps
  3630. active: only active timestamps (<...>)
  3631. inactive: only inactive timestamps (<...)
  3632. scheduled: only scheduled timestamps
  3633. deadline: only deadline timestamps"
  3634. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3635. (const :tag "All timestamps" all)
  3636. (const :tag "Only active timestamps" active)
  3637. (const :tag "Only inactive timestamps" inactive)
  3638. (const :tag "Only scheduled timestamps" scheduled)
  3639. (const :tag "Only deadline timestamps" deadline))
  3640. :version "24.3"
  3641. :group 'org-sparse-trees)
  3642. (defun org-cycle-hide-archived-subtrees (state)
  3643. "Re-hide all archived subtrees after a visibility state change."
  3644. (when (and (not org-cycle-open-archived-trees)
  3645. (not (memq state '(overview folded))))
  3646. (save-excursion
  3647. (let* ((globalp (memq state '(contents all)))
  3648. (beg (if globalp (point-min) (point)))
  3649. (end (if globalp (point-max) (org-end-of-subtree t))))
  3650. (org-hide-archived-subtrees beg end)
  3651. (goto-char beg)
  3652. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3653. (message "%s" (substitute-command-keys
  3654. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3655. (defun org-force-cycle-archived ()
  3656. "Cycle subtree even if it is archived."
  3657. (interactive)
  3658. (setq this-command 'org-cycle)
  3659. (let ((org-cycle-open-archived-trees t))
  3660. (call-interactively 'org-cycle)))
  3661. (defun org-hide-archived-subtrees (beg end)
  3662. "Re-hide all archived subtrees after a visibility state change."
  3663. (save-excursion
  3664. (let* ((re (concat ":" org-archive-tag ":")))
  3665. (goto-char beg)
  3666. (while (re-search-forward re end t)
  3667. (when (org-at-heading-p)
  3668. (org-flag-subtree t)
  3669. (org-end-of-subtree t))))))
  3670. (declare-function outline-end-of-heading "outline" ())
  3671. (declare-function outline-flag-region "outline" (from to flag))
  3672. (defun org-flag-subtree (flag)
  3673. (save-excursion
  3674. (org-back-to-heading t)
  3675. (outline-end-of-heading)
  3676. (outline-flag-region (point)
  3677. (progn (org-end-of-subtree t) (point))
  3678. flag)))
  3679. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3680. (eval-and-compile
  3681. (org-autoload "org-archive"
  3682. '(org-add-archive-files)))
  3683. ;; Autoload Column View Code
  3684. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3685. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3686. (declare-function org-columns-compute "org-colview" (property))
  3687. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3688. '(org-columns-number-to-string
  3689. org-columns-get-format-and-top-level
  3690. org-columns-compute
  3691. org-columns-remove-overlays))
  3692. ;; Autoload ID code
  3693. (declare-function org-id-store-link "org-id")
  3694. (declare-function org-id-locations-load "org-id")
  3695. (declare-function org-id-locations-save "org-id")
  3696. (defvar org-id-track-globally)
  3697. (org-autoload "org-id"
  3698. '(org-id-new
  3699. org-id-copy
  3700. org-id-get-with-outline-path-completion
  3701. org-id-get-with-outline-drilling))
  3702. ;;; Variables for pre-computed regular expressions, all buffer local
  3703. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3704. "Matches first line of a hidden block.")
  3705. (make-variable-buffer-local 'org-drawer-regexp)
  3706. (defvar org-todo-regexp nil
  3707. "Matches any of the TODO state keywords.")
  3708. (make-variable-buffer-local 'org-todo-regexp)
  3709. (defvar org-not-done-regexp nil
  3710. "Matches any of the TODO state keywords except the last one.")
  3711. (make-variable-buffer-local 'org-not-done-regexp)
  3712. (defvar org-not-done-heading-regexp nil
  3713. "Matches a TODO headline that is not done.")
  3714. (make-variable-buffer-local 'org-not-done-regexp)
  3715. (defvar org-todo-line-regexp nil
  3716. "Matches a headline and puts TODO state into group 2 if present.")
  3717. (make-variable-buffer-local 'org-todo-line-regexp)
  3718. (defvar org-complex-heading-regexp nil
  3719. "Matches a headline and puts everything into groups:
  3720. group 1: the stars
  3721. group 2: The todo keyword, maybe
  3722. group 3: Priority cookie
  3723. group 4: True headline
  3724. group 5: Tags")
  3725. (make-variable-buffer-local 'org-complex-heading-regexp)
  3726. (defvar org-complex-heading-regexp-format nil
  3727. "Printf format to make regexp to match an exact headline.
  3728. This regexp will match the headline of any node which has the
  3729. exact headline text that is put into the format, but may have any
  3730. TODO state, priority and tags.")
  3731. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3732. (defvar org-todo-line-tags-regexp nil
  3733. "Matches a headline and puts TODO state into group 2 if present.
  3734. Also put tags into group 4 if tags are present.")
  3735. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3736. (defvar org-ds-keyword-length 12
  3737. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3738. (make-variable-buffer-local 'org-ds-keyword-length)
  3739. (defvar org-deadline-regexp nil
  3740. "Matches the DEADLINE keyword.")
  3741. (make-variable-buffer-local 'org-deadline-regexp)
  3742. (defvar org-deadline-time-regexp nil
  3743. "Matches the DEADLINE keyword together with a time stamp.")
  3744. (make-variable-buffer-local 'org-deadline-time-regexp)
  3745. (defvar org-deadline-line-regexp nil
  3746. "Matches the DEADLINE keyword and the rest of the line.")
  3747. (make-variable-buffer-local 'org-deadline-line-regexp)
  3748. (defvar org-scheduled-regexp nil
  3749. "Matches the SCHEDULED keyword.")
  3750. (make-variable-buffer-local 'org-scheduled-regexp)
  3751. (defvar org-scheduled-time-regexp nil
  3752. "Matches the SCHEDULED keyword together with a time stamp.")
  3753. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3754. (defvar org-closed-time-regexp nil
  3755. "Matches the CLOSED keyword together with a time stamp.")
  3756. (make-variable-buffer-local 'org-closed-time-regexp)
  3757. (defvar org-keyword-time-regexp nil
  3758. "Matches any of the 4 keywords, together with the time stamp.")
  3759. (make-variable-buffer-local 'org-keyword-time-regexp)
  3760. (defvar org-keyword-time-not-clock-regexp nil
  3761. "Matches any of the 3 keywords, together with the time stamp.")
  3762. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3763. (defvar org-maybe-keyword-time-regexp nil
  3764. "Matches a timestamp, possibly preceded by a keyword.")
  3765. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3766. (defvar org-all-time-keywords nil
  3767. "List of time keywords.")
  3768. (make-variable-buffer-local 'org-all-time-keywords)
  3769. (defconst org-plain-time-of-day-regexp
  3770. (concat
  3771. "\\(\\<[012]?[0-9]"
  3772. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3773. "\\(--?"
  3774. "\\(\\<[012]?[0-9]"
  3775. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3776. "\\)?")
  3777. "Regular expression to match a plain time or time range.
  3778. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3779. groups carry important information:
  3780. 0 the full match
  3781. 1 the first time, range or not
  3782. 8 the second time, if it is a range.")
  3783. (defconst org-plain-time-extension-regexp
  3784. (concat
  3785. "\\(\\<[012]?[0-9]"
  3786. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3787. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3788. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3789. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3790. groups carry important information:
  3791. 0 the full match
  3792. 7 hours of duration
  3793. 9 minutes of duration")
  3794. (defconst org-stamp-time-of-day-regexp
  3795. (concat
  3796. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3797. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3798. "\\(--?"
  3799. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3800. "Regular expression to match a timestamp time or time range.
  3801. After a match, the following groups carry important information:
  3802. 0 the full match
  3803. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3804. 2 the first time, range or not
  3805. 4 the second time, if it is a range.")
  3806. (defconst org-startup-options
  3807. '(("fold" org-startup-folded t)
  3808. ("overview" org-startup-folded t)
  3809. ("nofold" org-startup-folded nil)
  3810. ("showall" org-startup-folded nil)
  3811. ("showeverything" org-startup-folded showeverything)
  3812. ("content" org-startup-folded content)
  3813. ("indent" org-startup-indented t)
  3814. ("noindent" org-startup-indented nil)
  3815. ("hidestars" org-hide-leading-stars t)
  3816. ("showstars" org-hide-leading-stars nil)
  3817. ("odd" org-odd-levels-only t)
  3818. ("oddeven" org-odd-levels-only nil)
  3819. ("align" org-startup-align-all-tables t)
  3820. ("noalign" org-startup-align-all-tables nil)
  3821. ("inlineimages" org-startup-with-inline-images t)
  3822. ("noinlineimages" org-startup-with-inline-images nil)
  3823. ("customtime" org-display-custom-times t)
  3824. ("logdone" org-log-done time)
  3825. ("lognotedone" org-log-done note)
  3826. ("nologdone" org-log-done nil)
  3827. ("lognoteclock-out" org-log-note-clock-out t)
  3828. ("nolognoteclock-out" org-log-note-clock-out nil)
  3829. ("logrepeat" org-log-repeat state)
  3830. ("lognoterepeat" org-log-repeat note)
  3831. ("nologrepeat" org-log-repeat nil)
  3832. ("logreschedule" org-log-reschedule time)
  3833. ("lognotereschedule" org-log-reschedule note)
  3834. ("nologreschedule" org-log-reschedule nil)
  3835. ("logredeadline" org-log-redeadline time)
  3836. ("lognoteredeadline" org-log-redeadline note)
  3837. ("nologredeadline" org-log-redeadline nil)
  3838. ("logrefile" org-log-refile time)
  3839. ("lognoterefile" org-log-refile note)
  3840. ("nologrefile" org-log-refile nil)
  3841. ("fninline" org-footnote-define-inline t)
  3842. ("nofninline" org-footnote-define-inline nil)
  3843. ("fnlocal" org-footnote-section nil)
  3844. ("fnauto" org-footnote-auto-label t)
  3845. ("fnprompt" org-footnote-auto-label nil)
  3846. ("fnconfirm" org-footnote-auto-label confirm)
  3847. ("fnplain" org-footnote-auto-label plain)
  3848. ("fnadjust" org-footnote-auto-adjust t)
  3849. ("nofnadjust" org-footnote-auto-adjust nil)
  3850. ("constcgs" constants-unit-system cgs)
  3851. ("constSI" constants-unit-system SI)
  3852. ("noptag" org-tag-persistent-alist nil)
  3853. ("hideblocks" org-hide-block-startup t)
  3854. ("nohideblocks" org-hide-block-startup nil)
  3855. ("beamer" org-startup-with-beamer-mode t)
  3856. ("entitiespretty" org-pretty-entities t)
  3857. ("entitiesplain" org-pretty-entities nil))
  3858. "Variable associated with STARTUP options for org-mode.
  3859. Each element is a list of three items: the startup options (as written
  3860. in the #+STARTUP line), the corresponding variable, and the value to set
  3861. this variable to if the option is found. An optional forth element PUSH
  3862. means to push this value onto the list in the variable.")
  3863. (defun org-update-property-plist (key val props)
  3864. "Update PROPS with KEY and VAL."
  3865. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3866. (key (if appending (substring key 0 (- (length key) 1)) key))
  3867. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3868. (previous (cdr (assoc key props))))
  3869. (if appending
  3870. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3871. (cons (cons key val) remainder))))
  3872. (defconst org-block-regexp
  3873. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3874. "Regular expression for hiding blocks.")
  3875. (defconst org-heading-keyword-regexp-format
  3876. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3877. "Printf format for a regexp matching an headline with some keyword.
  3878. This regexp will match the headline of any node which has the
  3879. exact keyword that is put into the format. The keyword isn't in
  3880. any group by default, but the stars and the body are.")
  3881. (defconst org-heading-keyword-maybe-regexp-format
  3882. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3883. "Printf format for a regexp matching an headline, possibly with some keyword.
  3884. This regexp can match any headline with the specified keyword, or
  3885. without a keyword. The keyword isn't in any group by default,
  3886. but the stars and the body are.")
  3887. (defun org-set-regexps-and-options ()
  3888. "Precompute regular expressions for current buffer."
  3889. (when (derived-mode-p 'org-mode)
  3890. (org-set-local 'org-todo-kwd-alist nil)
  3891. (org-set-local 'org-todo-key-alist nil)
  3892. (org-set-local 'org-todo-key-trigger nil)
  3893. (org-set-local 'org-todo-keywords-1 nil)
  3894. (org-set-local 'org-done-keywords nil)
  3895. (org-set-local 'org-todo-heads nil)
  3896. (org-set-local 'org-todo-sets nil)
  3897. (org-set-local 'org-todo-log-states nil)
  3898. (org-set-local 'org-file-properties nil)
  3899. (org-set-local 'org-file-tags nil)
  3900. (let ((re (org-make-options-regexp
  3901. '("CATEGORY" "TODO" "COLUMNS"
  3902. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3903. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3904. "OPTIONS")
  3905. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3906. (splitre "[ \t]+")
  3907. (scripts org-use-sub-superscripts)
  3908. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3909. tail sep kws1 prio props ftags drawers beamer-p
  3910. ext-setup-or-nil setup-contents (start 0))
  3911. (save-excursion
  3912. (save-restriction
  3913. (widen)
  3914. (goto-char (point-min))
  3915. (while (or (and ext-setup-or-nil
  3916. (string-match re ext-setup-or-nil start)
  3917. (setq start (match-end 0)))
  3918. (and (setq ext-setup-or-nil nil start 0)
  3919. (re-search-forward re nil t)))
  3920. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3921. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3922. (if (stringp value) (setq value (org-trim value)))
  3923. (cond
  3924. ((equal key "CATEGORY")
  3925. (setq cat value))
  3926. ((member key '("SEQ_TODO" "TODO"))
  3927. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3928. ((equal key "TYP_TODO")
  3929. (push (cons 'type (org-split-string value splitre)) kwds))
  3930. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3931. ;; general TODO-like setup
  3932. (push (cons (intern (downcase (match-string 1 key)))
  3933. (org-split-string value splitre)) kwds))
  3934. ((equal key "TAGS")
  3935. (setq tags (append tags (if tags '("\\n") nil)
  3936. (org-split-string value splitre))))
  3937. ((equal key "COLUMNS")
  3938. (org-set-local 'org-columns-default-format value))
  3939. ((equal key "LINK")
  3940. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3941. (push (cons (match-string 1 value)
  3942. (org-trim (match-string 2 value)))
  3943. links)))
  3944. ((equal key "PRIORITIES")
  3945. (setq prio (org-split-string value " +")))
  3946. ((equal key "PROPERTY")
  3947. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3948. (setq props (org-update-property-plist (match-string 1 value)
  3949. (match-string 2 value)
  3950. props))))
  3951. ((equal key "FILETAGS")
  3952. (when (string-match "\\S-" value)
  3953. (setq ftags
  3954. (append
  3955. ftags
  3956. (apply 'append
  3957. (mapcar (lambda (x) (org-split-string x ":"))
  3958. (org-split-string value)))))))
  3959. ((equal key "DRAWERS")
  3960. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  3961. ((equal key "CONSTANTS")
  3962. (setq const (append const (org-split-string value splitre))))
  3963. ((equal key "STARTUP")
  3964. (let ((opts (org-split-string value splitre))
  3965. l var val)
  3966. (while (setq l (pop opts))
  3967. (when (setq l (assoc l org-startup-options))
  3968. (setq var (nth 1 l) val (nth 2 l))
  3969. (if (not (nth 3 l))
  3970. (set (make-local-variable var) val)
  3971. (if (not (listp (symbol-value var)))
  3972. (set (make-local-variable var) nil))
  3973. (set (make-local-variable var) (symbol-value var))
  3974. (add-to-list var val))))))
  3975. ((equal key "ARCHIVE")
  3976. (setq arch value)
  3977. (remove-text-properties 0 (length arch)
  3978. '(face t fontified t) arch))
  3979. ((equal key "LATEX_CLASS")
  3980. (setq beamer-p (equal value "beamer")))
  3981. ((equal key "OPTIONS")
  3982. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3983. (setq scripts (read (match-string 2 value)))))
  3984. ((equal key "SETUPFILE")
  3985. (setq setup-contents (org-file-contents
  3986. (expand-file-name
  3987. (org-remove-double-quotes value))
  3988. 'noerror))
  3989. (if (not ext-setup-or-nil)
  3990. (setq ext-setup-or-nil setup-contents start 0)
  3991. (setq ext-setup-or-nil
  3992. (concat (substring ext-setup-or-nil 0 start)
  3993. "\n" setup-contents "\n"
  3994. (substring ext-setup-or-nil start)))))))
  3995. ;; search for property blocks
  3996. (goto-char (point-min))
  3997. (while (re-search-forward org-block-regexp nil t)
  3998. (when (equal "PROPERTY" (upcase (match-string 1)))
  3999. (setq value (replace-regexp-in-string
  4000. "[\n\r]" " " (match-string 4)))
  4001. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4002. (setq props (org-update-property-plist (match-string 1 value)
  4003. (match-string 2 value)
  4004. props)))))))
  4005. (org-set-local 'org-use-sub-superscripts scripts)
  4006. (when cat
  4007. (org-set-local 'org-category (intern cat))
  4008. (push (cons "CATEGORY" cat) props))
  4009. (when prio
  4010. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4011. (setq prio (mapcar 'string-to-char prio))
  4012. (org-set-local 'org-highest-priority (nth 0 prio))
  4013. (org-set-local 'org-lowest-priority (nth 1 prio))
  4014. (org-set-local 'org-default-priority (nth 2 prio)))
  4015. (and props (org-set-local 'org-file-properties (nreverse props)))
  4016. (and ftags (org-set-local 'org-file-tags
  4017. (mapcar 'org-add-prop-inherited ftags)))
  4018. (and drawers (org-set-local 'org-drawers drawers))
  4019. (and arch (org-set-local 'org-archive-location arch))
  4020. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4021. ;; Process the TODO keywords
  4022. (unless kwds
  4023. ;; Use the global values as if they had been given locally.
  4024. (setq kwds (default-value 'org-todo-keywords))
  4025. (if (stringp (car kwds))
  4026. (setq kwds (list (cons org-todo-interpretation
  4027. (default-value 'org-todo-keywords)))))
  4028. (setq kwds (reverse kwds)))
  4029. (setq kwds (nreverse kwds))
  4030. (let (inter kws kw)
  4031. (while (setq kws (pop kwds))
  4032. (let ((kws (or
  4033. (run-hook-with-args-until-success
  4034. 'org-todo-setup-filter-hook kws)
  4035. kws)))
  4036. (setq inter (pop kws) sep (member "|" kws)
  4037. kws0 (delete "|" (copy-sequence kws))
  4038. kwsa nil
  4039. kws1 (mapcar
  4040. (lambda (x)
  4041. ;; 1 2
  4042. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4043. (progn
  4044. (setq kw (match-string 1 x)
  4045. key (and (match-end 2) (match-string 2 x))
  4046. log (org-extract-log-state-settings x))
  4047. (push (cons kw (and key (string-to-char key))) kwsa)
  4048. (and log (push log org-todo-log-states))
  4049. kw)
  4050. (error "Invalid TODO keyword %s" x)))
  4051. kws0)
  4052. kwsa (if kwsa (append '((:startgroup))
  4053. (nreverse kwsa)
  4054. '((:endgroup))))
  4055. hw (car kws1)
  4056. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4057. tail (list inter hw (car dws) (org-last dws))))
  4058. (add-to-list 'org-todo-heads hw 'append)
  4059. (push kws1 org-todo-sets)
  4060. (setq org-done-keywords (append org-done-keywords dws nil))
  4061. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4062. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4063. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4064. (setq org-todo-sets (nreverse org-todo-sets)
  4065. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4066. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4067. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4068. ;; Process the constants
  4069. (when const
  4070. (let (e cst)
  4071. (while (setq e (pop const))
  4072. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4073. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4074. (setq org-table-formula-constants-local cst)))
  4075. ;; Process the tags.
  4076. (when tags
  4077. (let (e tgs)
  4078. (while (setq e (pop tags))
  4079. (cond
  4080. ((equal e "{") (push '(:startgroup) tgs))
  4081. ((equal e "}") (push '(:endgroup) tgs))
  4082. ((equal e "\\n") (push '(:newline) tgs))
  4083. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4084. (push (cons (match-string 1 e)
  4085. (string-to-char (match-string 2 e)))
  4086. tgs))
  4087. (t (push (list e) tgs))))
  4088. (org-set-local 'org-tag-alist nil)
  4089. (while (setq e (pop tgs))
  4090. (or (and (stringp (car e))
  4091. (assoc (car e) org-tag-alist))
  4092. (push e org-tag-alist)))))
  4093. ;; Compute the regular expressions and other local variables.
  4094. ;; Using `org-outline-regexp-bol' would complicate them much,
  4095. ;; because of the fixed white space at the end of that string.
  4096. (if (not org-done-keywords)
  4097. (setq org-done-keywords (and org-todo-keywords-1
  4098. (list (org-last org-todo-keywords-1)))))
  4099. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4100. (length org-scheduled-string)
  4101. (length org-clock-string)
  4102. (length org-closed-string)))
  4103. org-drawer-regexp
  4104. (concat "^[ \t]*:\\("
  4105. (mapconcat 'regexp-quote org-drawers "\\|")
  4106. "\\):[ \t]*$")
  4107. org-not-done-keywords
  4108. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4109. org-todo-regexp
  4110. (concat "\\("
  4111. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4112. "\\)")
  4113. org-not-done-regexp
  4114. (concat "\\("
  4115. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4116. "\\)")
  4117. org-not-done-heading-regexp
  4118. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4119. org-todo-line-regexp
  4120. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4121. org-complex-heading-regexp
  4122. (concat "^\\(\\*+\\)"
  4123. "\\(?: +" org-todo-regexp "\\)?"
  4124. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4125. "\\(?: +\\(.*?\\)\\)??"
  4126. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4127. "[ \t]*$")
  4128. org-complex-heading-regexp-format
  4129. (concat "^\\(\\*+\\)"
  4130. "\\(?: +" org-todo-regexp "\\)?"
  4131. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4132. "\\(?: +"
  4133. ;; Stats cookies can be stuck to body.
  4134. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4135. "\\(%s\\)"
  4136. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4137. "\\)"
  4138. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4139. "[ \t]*$")
  4140. org-todo-line-tags-regexp
  4141. (concat "^\\(\\*+\\)"
  4142. "\\(?: +" org-todo-regexp "\\)?"
  4143. "\\(?: +\\(.*?\\)\\)??"
  4144. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4145. "[ \t]*$")
  4146. org-deadline-regexp (concat "\\<" org-deadline-string)
  4147. org-deadline-time-regexp
  4148. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4149. org-deadline-line-regexp
  4150. (concat "\\<\\(" org-deadline-string "\\).*")
  4151. org-scheduled-regexp
  4152. (concat "\\<" org-scheduled-string)
  4153. org-scheduled-time-regexp
  4154. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4155. org-closed-time-regexp
  4156. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4157. org-keyword-time-regexp
  4158. (concat "\\<\\(" org-scheduled-string
  4159. "\\|" org-deadline-string
  4160. "\\|" org-closed-string
  4161. "\\|" org-clock-string "\\)"
  4162. " *[[<]\\([^]>]+\\)[]>]")
  4163. org-keyword-time-not-clock-regexp
  4164. (concat "\\<\\(" org-scheduled-string
  4165. "\\|" org-deadline-string
  4166. "\\|" org-closed-string
  4167. "\\)"
  4168. " *[[<]\\([^]>]+\\)[]>]")
  4169. org-maybe-keyword-time-regexp
  4170. (concat "\\(\\<\\(" org-scheduled-string
  4171. "\\|" org-deadline-string
  4172. "\\|" org-closed-string
  4173. "\\|" org-clock-string "\\)\\)?"
  4174. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4175. org-all-time-keywords
  4176. (mapcar (lambda (w) (substring w 0 -1))
  4177. (list org-scheduled-string org-deadline-string
  4178. org-clock-string org-closed-string))
  4179. )
  4180. (org-compute-latex-and-specials-regexp)
  4181. (org-set-font-lock-defaults))))
  4182. (defun org-file-contents (file &optional noerror)
  4183. "Return the contents of FILE, as a string."
  4184. (if (or (not file)
  4185. (not (file-readable-p file)))
  4186. (if noerror
  4187. (progn
  4188. (message "Cannot read file \"%s\"" file)
  4189. (ding) (sit-for 2)
  4190. "")
  4191. (error "Cannot read file \"%s\"" file))
  4192. (with-temp-buffer
  4193. (insert-file-contents file)
  4194. (buffer-string))))
  4195. (defun org-extract-log-state-settings (x)
  4196. "Extract the log state setting from a TODO keyword string.
  4197. This will extract info from a string like \"WAIT(w@/!)\"."
  4198. (let (kw key log1 log2)
  4199. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4200. (setq kw (match-string 1 x)
  4201. key (and (match-end 2) (match-string 2 x))
  4202. log1 (and (match-end 3) (match-string 3 x))
  4203. log2 (and (match-end 4) (match-string 4 x)))
  4204. (and (or log1 log2)
  4205. (list kw
  4206. (and log1 (if (equal log1 "!") 'time 'note))
  4207. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4208. (defun org-remove-keyword-keys (list)
  4209. "Remove a pair of parenthesis at the end of each string in LIST."
  4210. (mapcar (lambda (x)
  4211. (if (string-match "(.*)$" x)
  4212. (substring x 0 (match-beginning 0))
  4213. x))
  4214. list))
  4215. (defun org-assign-fast-keys (alist)
  4216. "Assign fast keys to a keyword-key alist.
  4217. Respect keys that are already there."
  4218. (let (new e (alt ?0))
  4219. (while (setq e (pop alist))
  4220. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4221. (cdr e)) ;; Key already assigned.
  4222. (push e new)
  4223. (let ((clist (string-to-list (downcase (car e))))
  4224. (used (append new alist)))
  4225. (when (= (car clist) ?@)
  4226. (pop clist))
  4227. (while (and clist (rassoc (car clist) used))
  4228. (pop clist))
  4229. (unless clist
  4230. (while (rassoc alt used)
  4231. (incf alt)))
  4232. (push (cons (car e) (or (car clist) alt)) new))))
  4233. (nreverse new)))
  4234. ;;; Some variables used in various places
  4235. (defvar org-window-configuration nil
  4236. "Used in various places to store a window configuration.")
  4237. (defvar org-selected-window nil
  4238. "Used in various places to store a window configuration.")
  4239. (defvar org-finish-function nil
  4240. "Function to be called when `C-c C-c' is used.
  4241. This is for getting out of special buffers like capture.")
  4242. ;; FIXME: Occasionally check by commenting these, to make sure
  4243. ;; no other functions uses these, forgetting to let-bind them.
  4244. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4245. (defvar org-last-state)
  4246. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4247. ;; Defined somewhere in this file, but used before definition.
  4248. (defvar org-entities) ;; defined in org-entities.el
  4249. (defvar org-struct-menu)
  4250. (defvar org-org-menu)
  4251. (defvar org-tbl-menu)
  4252. ;;;; Define the Org-mode
  4253. ;; We use a before-change function to check if a table might need
  4254. ;; an update.
  4255. (defvar org-table-may-need-update t
  4256. "Indicates that a table might need an update.
  4257. This variable is set by `org-before-change-function'.
  4258. `org-table-align' sets it back to nil.")
  4259. (defun org-before-change-function (beg end)
  4260. "Every change indicates that a table might need an update."
  4261. (setq org-table-may-need-update t))
  4262. (defvar org-mode-map)
  4263. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4264. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4265. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4266. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4267. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4268. (defvar org-table-buffer-is-an nil)
  4269. (defvar bidi-paragraph-direction)
  4270. (defvar buffer-face-mode-face)
  4271. (require 'outline)
  4272. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4273. (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"))
  4274. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4275. ;; Other stuff we need.
  4276. (require 'time-date)
  4277. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4278. (require 'easymenu)
  4279. (require 'overlay)
  4280. (require 'org-macs)
  4281. (require 'org-entities)
  4282. ;; (require 'org-compat) moved higher up in the file before it is first used
  4283. (require 'org-faces)
  4284. (require 'org-list)
  4285. (require 'org-pcomplete)
  4286. (require 'org-src)
  4287. (require 'org-footnote)
  4288. ;; babel
  4289. (require 'ob)
  4290. (require 'ob-table)
  4291. (require 'ob-lob)
  4292. (require 'ob-ref)
  4293. (require 'ob-tangle)
  4294. (require 'ob-comint)
  4295. (require 'ob-keys)
  4296. ;;;###autoload
  4297. (define-derived-mode org-mode outline-mode "Org"
  4298. "Outline-based notes management and organizer, alias
  4299. \"Carsten's outline-mode for keeping track of everything.\"
  4300. Org-mode develops organizational tasks around a NOTES file which
  4301. contains information about projects as plain text. Org-mode is
  4302. implemented on top of outline-mode, which is ideal to keep the content
  4303. of large files well structured. It supports ToDo items, deadlines and
  4304. time stamps, which magically appear in the diary listing of the Emacs
  4305. calendar. Tables are easily created with a built-in table editor.
  4306. Plain text URL-like links connect to websites, emails (VM), Usenet
  4307. messages (Gnus), BBDB entries, and any files related to the project.
  4308. For printing and sharing of notes, an Org-mode file (or a part of it)
  4309. can be exported as a structured ASCII or HTML file.
  4310. The following commands are available:
  4311. \\{org-mode-map}"
  4312. ;; Get rid of Outline menus, they are not needed
  4313. ;; Need to do this here because define-derived-mode sets up
  4314. ;; the keymap so late. Still, it is a waste to call this each time
  4315. ;; we switch another buffer into org-mode.
  4316. (if (featurep 'xemacs)
  4317. (when (boundp 'outline-mode-menu-heading)
  4318. ;; Assume this is Greg's port, it uses easymenu
  4319. (easy-menu-remove outline-mode-menu-heading)
  4320. (easy-menu-remove outline-mode-menu-show)
  4321. (easy-menu-remove outline-mode-menu-hide))
  4322. (define-key org-mode-map [menu-bar headings] 'undefined)
  4323. (define-key org-mode-map [menu-bar hide] 'undefined)
  4324. (define-key org-mode-map [menu-bar show] 'undefined))
  4325. (org-load-modules-maybe)
  4326. (easy-menu-add org-org-menu)
  4327. (easy-menu-add org-tbl-menu)
  4328. (org-install-agenda-files-menu)
  4329. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4330. (add-to-invisibility-spec '(org-cwidth))
  4331. (add-to-invisibility-spec '(org-hide-block . t))
  4332. (when (featurep 'xemacs)
  4333. (org-set-local 'line-move-ignore-invisible t))
  4334. (org-set-local 'outline-regexp org-outline-regexp)
  4335. (org-set-local 'outline-level 'org-outline-level)
  4336. (setq bidi-paragraph-direction 'left-to-right)
  4337. (when (and org-ellipsis
  4338. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4339. (fboundp 'make-glyph-code))
  4340. (unless org-display-table
  4341. (setq org-display-table (make-display-table)))
  4342. (set-display-table-slot
  4343. org-display-table 4
  4344. (vconcat (mapcar
  4345. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4346. org-ellipsis)))
  4347. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4348. (setq buffer-display-table org-display-table))
  4349. (org-set-regexps-and-options)
  4350. (when (and org-tag-faces (not org-tags-special-faces-re))
  4351. ;; tag faces set outside customize.... force initialization.
  4352. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4353. ;; Calc embedded
  4354. (org-set-local 'calc-embedded-open-mode "# ")
  4355. (modify-syntax-entry ?@ "w")
  4356. (modify-syntax-entry ?\" "\"")
  4357. (if org-startup-truncated (setq truncate-lines t))
  4358. (org-set-local 'font-lock-unfontify-region-function
  4359. 'org-unfontify-region)
  4360. ;; Activate before-change-function
  4361. (org-set-local 'org-table-may-need-update t)
  4362. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4363. 'local)
  4364. ;; Check for running clock before killing a buffer
  4365. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4366. ;; Initialize macros templates.
  4367. (org-macro-initialize-templates)
  4368. ;; Initialize radio targets.
  4369. (org-update-radio-target-regexp)
  4370. ;; Indentation.
  4371. (org-set-local 'indent-line-function 'org-indent-line)
  4372. (org-set-local 'indent-region-function 'org-indent-region)
  4373. ;; Filling and auto-filling.
  4374. (org-setup-filling)
  4375. ;; Comments.
  4376. (org-setup-comments-handling)
  4377. ;; Beginning/end of defun
  4378. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4379. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4380. ;; Next error for sparse trees
  4381. (org-set-local 'next-error-function 'org-occur-next-match)
  4382. ;; Make sure dependence stuff works reliably, even for users who set it
  4383. ;; too late :-(
  4384. (if org-enforce-todo-dependencies
  4385. (add-hook 'org-blocker-hook
  4386. 'org-block-todo-from-children-or-siblings-or-parent)
  4387. (remove-hook 'org-blocker-hook
  4388. 'org-block-todo-from-children-or-siblings-or-parent))
  4389. (if org-enforce-todo-checkbox-dependencies
  4390. (add-hook 'org-blocker-hook
  4391. 'org-block-todo-from-checkboxes)
  4392. (remove-hook 'org-blocker-hook
  4393. 'org-block-todo-from-checkboxes))
  4394. ;; Align options lines
  4395. (org-set-local
  4396. 'align-mode-rules-list
  4397. '((org-in-buffer-settings
  4398. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4399. (modes . '(org-mode)))))
  4400. ;; Imenu
  4401. (org-set-local 'imenu-create-index-function
  4402. 'org-imenu-get-tree)
  4403. ;; Make isearch reveal context
  4404. (if (or (featurep 'xemacs)
  4405. (not (boundp 'outline-isearch-open-invisible-function)))
  4406. ;; Emacs 21 and XEmacs make use of the hook
  4407. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4408. ;; Emacs 22 deals with this through a special variable
  4409. (org-set-local 'outline-isearch-open-invisible-function
  4410. (lambda (&rest ignore) (org-show-context 'isearch))))
  4411. ;; Turn on org-beamer-mode?
  4412. (and org-startup-with-beamer-mode (org-beamer-mode))
  4413. ;; Setup the pcomplete hooks
  4414. (set (make-local-variable 'pcomplete-command-completion-function)
  4415. 'org-pcomplete-initial)
  4416. (set (make-local-variable 'pcomplete-command-name-function)
  4417. 'org-command-at-point)
  4418. (set (make-local-variable 'pcomplete-default-completion-function)
  4419. 'ignore)
  4420. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4421. 'org-parse-arguments)
  4422. (set (make-local-variable 'pcomplete-termination-string) "")
  4423. (when (>= emacs-major-version 23)
  4424. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4425. ;; If empty file that did not turn on org-mode automatically, make it to.
  4426. (if (and org-insert-mode-line-in-empty-file
  4427. (org-called-interactively-p 'any)
  4428. (= (point-min) (point-max)))
  4429. (insert "# -*- mode: org -*-\n\n"))
  4430. (unless org-inhibit-startup
  4431. (when org-startup-align-all-tables
  4432. (let ((bmp (buffer-modified-p)))
  4433. (org-table-map-tables 'org-table-align 'quietly)
  4434. (set-buffer-modified-p bmp)))
  4435. (when org-startup-with-inline-images
  4436. (org-display-inline-images))
  4437. (when org-startup-indented
  4438. (require 'org-indent)
  4439. (org-indent-mode 1))
  4440. (unless org-inhibit-startup-visibility-stuff
  4441. (org-set-startup-visibility)))
  4442. ;; Try to set org-hide correctly
  4443. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4444. (when (fboundp 'abbrev-table-put)
  4445. (abbrev-table-put org-mode-abbrev-table
  4446. :parents (list text-mode-abbrev-table)))
  4447. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4448. (defun org-find-invisible-foreground ()
  4449. (let ((candidates (remove
  4450. "unspecified-bg"
  4451. (list
  4452. (face-background 'default)
  4453. (face-background 'org-default)
  4454. (cdr (assoc 'background-color default-frame-alist))
  4455. (cdr (assoc 'background-color initial-frame-alist))
  4456. (cdr (assoc 'background-color window-system-default-frame-alist))
  4457. (face-foreground 'org-hide)))))
  4458. (car (remove nil candidates))))
  4459. (defun org-current-time ()
  4460. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4461. (if (> (car org-time-stamp-rounding-minutes) 1)
  4462. (let ((r (car org-time-stamp-rounding-minutes))
  4463. (time (decode-time)))
  4464. (apply 'encode-time
  4465. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4466. (nthcdr 2 time))))
  4467. (current-time)))
  4468. (defun org-today ()
  4469. "Return today date, considering `org-extend-today-until'."
  4470. (time-to-days
  4471. (time-subtract (current-time)
  4472. (list 0 (* 3600 org-extend-today-until) 0))))
  4473. ;;;; Font-Lock stuff, including the activators
  4474. (defvar org-mouse-map (make-sparse-keymap))
  4475. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4476. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4477. (when org-mouse-1-follows-link
  4478. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4479. (when org-tab-follows-link
  4480. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4481. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4482. (require 'font-lock)
  4483. (defconst org-non-link-chars "]\t\n\r<>")
  4484. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4485. "shell" "elisp" "doi" "message"))
  4486. (defvar org-link-types-re nil
  4487. "Matches a link that has a url-like prefix like \"http:\"")
  4488. (defvar org-link-re-with-space nil
  4489. "Matches a link with spaces, optional angular brackets around it.")
  4490. (defvar org-link-re-with-space2 nil
  4491. "Matches a link with spaces, optional angular brackets around it.")
  4492. (defvar org-link-re-with-space3 nil
  4493. "Matches a link with spaces, only for internal part in bracket links.")
  4494. (defvar org-angle-link-re nil
  4495. "Matches link with angular brackets, spaces are allowed.")
  4496. (defvar org-plain-link-re nil
  4497. "Matches plain link, without spaces.")
  4498. (defvar org-bracket-link-regexp nil
  4499. "Matches a link in double brackets.")
  4500. (defvar org-bracket-link-analytic-regexp nil
  4501. "Regular expression used to analyze links.
  4502. Here is what the match groups contain after a match:
  4503. 1: http:
  4504. 2: http
  4505. 3: path
  4506. 4: [desc]
  4507. 5: desc")
  4508. (defvar org-bracket-link-analytic-regexp++ nil
  4509. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4510. (defvar org-any-link-re nil
  4511. "Regular expression matching any link.")
  4512. (defcustom org-match-sexp-depth 3
  4513. "Number of stacked braces for sub/superscript matching.
  4514. This has to be set before loading org.el to be effective."
  4515. :group 'org-export-translation ; ??????????????????????????/
  4516. :type 'integer)
  4517. (defun org-create-multibrace-regexp (left right n)
  4518. "Create a regular expression which will match a balanced sexp.
  4519. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4520. as single character strings.
  4521. The regexp returned will match the entire expression including the
  4522. delimiters. It will also define a single group which contains the
  4523. match except for the outermost delimiters. The maximum depth of
  4524. stacked delimiters is N. Escaping delimiters is not possible."
  4525. (let* ((nothing (concat "[^" left right "]*?"))
  4526. (or "\\|")
  4527. (re nothing)
  4528. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4529. (while (> n 1)
  4530. (setq n (1- n)
  4531. re (concat re or next)
  4532. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4533. (concat left "\\(" re "\\)" right)))
  4534. (defvar org-match-substring-regexp
  4535. (concat
  4536. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4537. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4538. "\\|"
  4539. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4540. "\\|"
  4541. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4542. "The regular expression matching a sub- or superscript.")
  4543. (defvar org-match-substring-with-braces-regexp
  4544. (concat
  4545. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4546. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4547. "\\)")
  4548. "The regular expression matching a sub- or superscript, forcing braces.")
  4549. (defun org-make-link-regexps ()
  4550. "Update the link regular expressions.
  4551. This should be called after the variable `org-link-types' has changed."
  4552. (setq org-link-types-re
  4553. (concat
  4554. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4555. org-link-re-with-space
  4556. (concat
  4557. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4558. "\\([^" org-non-link-chars " ]"
  4559. "[^" org-non-link-chars "]*"
  4560. "[^" org-non-link-chars " ]\\)>?")
  4561. org-link-re-with-space2
  4562. (concat
  4563. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4564. "\\([^" org-non-link-chars " ]"
  4565. "[^\t\n\r]*"
  4566. "[^" org-non-link-chars " ]\\)>?")
  4567. org-link-re-with-space3
  4568. (concat
  4569. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4570. "\\([^" org-non-link-chars " ]"
  4571. "[^\t\n\r]*\\)")
  4572. org-angle-link-re
  4573. (concat
  4574. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4575. "\\([^" org-non-link-chars " ]"
  4576. "[^" org-non-link-chars "]*"
  4577. "\\)>")
  4578. org-plain-link-re
  4579. (concat
  4580. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4581. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4582. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4583. org-bracket-link-regexp
  4584. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4585. org-bracket-link-analytic-regexp
  4586. (concat
  4587. "\\[\\["
  4588. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4589. "\\([^]]+\\)"
  4590. "\\]"
  4591. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4592. "\\]")
  4593. org-bracket-link-analytic-regexp++
  4594. (concat
  4595. "\\[\\["
  4596. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4597. "\\([^]]+\\)"
  4598. "\\]"
  4599. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4600. "\\]")
  4601. org-any-link-re
  4602. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4603. org-angle-link-re "\\)\\|\\("
  4604. org-plain-link-re "\\)")))
  4605. (org-make-link-regexps)
  4606. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4607. "Regular expression for fast time stamp matching.")
  4608. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4609. "Regular expression for fast time stamp matching.")
  4610. (defconst org-ts-regexp0
  4611. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4612. "Regular expression matching time strings for analysis.
  4613. This one does not require the space after the date, so it can be used
  4614. on a string that terminates immediately after the date.")
  4615. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4616. "Regular expression matching time strings for analysis.")
  4617. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4618. "Regular expression matching time stamps, with groups.")
  4619. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4620. "Regular expression matching time stamps (also [..]), with groups.")
  4621. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4622. "Regular expression matching a time stamp range.")
  4623. (defconst org-tr-regexp-both
  4624. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4625. "Regular expression matching a time stamp range.")
  4626. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4627. org-ts-regexp "\\)?")
  4628. "Regular expression matching a time stamp or time stamp range.")
  4629. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4630. org-ts-regexp-both "\\)?")
  4631. "Regular expression matching a time stamp or time stamp range.
  4632. The time stamps may be either active or inactive.")
  4633. (defvar org-emph-face nil)
  4634. (defun org-do-emphasis-faces (limit)
  4635. "Run through the buffer and add overlays to emphasized strings."
  4636. (let (rtn a)
  4637. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4638. (if (not (= (char-after (match-beginning 3))
  4639. (char-after (match-beginning 4))))
  4640. (progn
  4641. (setq rtn t)
  4642. (setq a (assoc (match-string 3) org-emphasis-alist))
  4643. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4644. 'face
  4645. (nth 1 a))
  4646. (and (nth 4 a)
  4647. (org-remove-flyspell-overlays-in
  4648. (match-beginning 0) (match-end 0)))
  4649. (add-text-properties (match-beginning 2) (match-end 2)
  4650. '(font-lock-multiline t org-emphasis t))
  4651. (when org-hide-emphasis-markers
  4652. (add-text-properties (match-end 4) (match-beginning 5)
  4653. '(invisible org-link))
  4654. (add-text-properties (match-beginning 3) (match-end 3)
  4655. '(invisible org-link)))))
  4656. (backward-char 1))
  4657. rtn))
  4658. (defun org-emphasize (&optional char)
  4659. "Insert or change an emphasis, i.e. a font like bold or italic.
  4660. If there is an active region, change that region to a new emphasis.
  4661. If there is no region, just insert the marker characters and position
  4662. the cursor between them.
  4663. CHAR should be either the marker character, or the first character of the
  4664. HTML tag associated with that emphasis. If CHAR is a space, the means
  4665. to remove the emphasis of the selected region.
  4666. If char is not given (for example in an interactive call) it
  4667. will be prompted for."
  4668. (interactive)
  4669. (let ((eal org-emphasis-alist) e det
  4670. (erc org-emphasis-regexp-components)
  4671. (prompt "")
  4672. (string "") beg end move tag c s)
  4673. (if (org-region-active-p)
  4674. (setq beg (region-beginning) end (region-end)
  4675. string (buffer-substring beg end))
  4676. (setq move t))
  4677. (while (setq e (pop eal))
  4678. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4679. c (aref tag 0))
  4680. (push (cons c (string-to-char (car e))) det)
  4681. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4682. (substring tag 1)))))
  4683. (setq det (nreverse det))
  4684. (unless char
  4685. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4686. (setq char (read-char-exclusive)))
  4687. (setq char (or (cdr (assoc char det)) char))
  4688. (if (equal char ?\ )
  4689. (setq s "" move nil)
  4690. (unless (assoc (char-to-string char) org-emphasis-alist)
  4691. (error "No such emphasis marker: \"%c\"" char))
  4692. (setq s (char-to-string char)))
  4693. (while (and (> (length string) 1)
  4694. (equal (substring string 0 1) (substring string -1))
  4695. (assoc (substring string 0 1) org-emphasis-alist))
  4696. (setq string (substring string 1 -1)))
  4697. (setq string (concat s string s))
  4698. (if beg (delete-region beg end))
  4699. (unless (or (bolp)
  4700. (string-match (concat "[" (nth 0 erc) "\n]")
  4701. (char-to-string (char-before (point)))))
  4702. (insert " "))
  4703. (unless (or (eobp)
  4704. (string-match (concat "[" (nth 1 erc) "\n]")
  4705. (char-to-string (char-after (point)))))
  4706. (insert " ") (backward-char 1))
  4707. (insert string)
  4708. (and move (backward-char 1))))
  4709. (defconst org-nonsticky-props
  4710. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4711. (defsubst org-rear-nonsticky-at (pos)
  4712. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4713. (defun org-activate-plain-links (limit)
  4714. "Run through the buffer and add overlays to links."
  4715. (catch 'exit
  4716. (let (f hl)
  4717. (when (re-search-forward (concat org-plain-link-re) limit t)
  4718. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4719. (setq f (get-text-property (match-beginning 0) 'face))
  4720. (setq hl (org-match-string-no-properties 0))
  4721. (if (or (eq f 'org-tag)
  4722. (and (listp f) (memq 'org-tag f)))
  4723. nil
  4724. (add-text-properties (match-beginning 0) (match-end 0)
  4725. (list 'mouse-face 'highlight
  4726. 'face 'org-link
  4727. 'htmlize-link `(:uri ,hl)
  4728. 'keymap org-mouse-map))
  4729. (org-rear-nonsticky-at (match-end 0)))
  4730. t))))
  4731. (defun org-activate-code (limit)
  4732. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4733. (progn
  4734. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4735. (remove-text-properties (match-beginning 0) (match-end 0)
  4736. '(display t invisible t intangible t))
  4737. t)))
  4738. (defcustom org-src-fontify-natively nil
  4739. "When non-nil, fontify code in code blocks."
  4740. :type 'boolean
  4741. :version "24.1"
  4742. :group 'org-appearance
  4743. :group 'org-babel)
  4744. (defcustom org-allow-promoting-top-level-subtree nil
  4745. "When non-nil, allow promoting a top level subtree.
  4746. The leading star of the top level headline will be replaced
  4747. by a #."
  4748. :type 'boolean
  4749. :version "24.1"
  4750. :group 'org-appearance)
  4751. (defun org-fontify-meta-lines-and-blocks (limit)
  4752. (condition-case nil
  4753. (org-fontify-meta-lines-and-blocks-1 limit)
  4754. (error (message "org-mode fontification error"))))
  4755. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4756. "Fontify #+ lines and blocks, in the correct ways."
  4757. (let ((case-fold-search t))
  4758. (if (re-search-forward
  4759. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4760. limit t)
  4761. (let ((beg (match-beginning 0))
  4762. (block-start (match-end 0))
  4763. (block-end nil)
  4764. (lang (match-string 7))
  4765. (beg1 (line-beginning-position 2))
  4766. (dc1 (downcase (match-string 2)))
  4767. (dc3 (downcase (match-string 3)))
  4768. end end1 quoting block-type ovl)
  4769. (cond
  4770. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4771. ;; a single line of backend-specific content
  4772. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4773. (remove-text-properties (match-beginning 0) (match-end 0)
  4774. '(display t invisible t intangible t))
  4775. (add-text-properties (match-beginning 1) (match-end 3)
  4776. '(font-lock-fontified t face org-meta-line))
  4777. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4778. '(font-lock-fontified t face org-block))
  4779. ; for backend-specific code
  4780. t)
  4781. ((and (match-end 4) (equal dc3 "+begin"))
  4782. ;; Truly a block
  4783. (setq block-type (downcase (match-string 5))
  4784. quoting (member block-type org-protecting-blocks))
  4785. (when (re-search-forward
  4786. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4787. nil t) ;; on purpose, we look further than LIMIT
  4788. (setq end (min (point-max) (match-end 0))
  4789. end1 (min (point-max) (1- (match-beginning 0))))
  4790. (setq block-end (match-beginning 0))
  4791. (when quoting
  4792. (remove-text-properties beg end
  4793. '(display t invisible t intangible t)))
  4794. (add-text-properties
  4795. beg end
  4796. '(font-lock-fontified t font-lock-multiline t))
  4797. (add-text-properties beg beg1 '(face org-meta-line))
  4798. (add-text-properties end1 (min (point-max) (1+ end))
  4799. '(face org-meta-line)) ; for end_src
  4800. (cond
  4801. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4802. (org-src-font-lock-fontify-block lang block-start block-end)
  4803. ;; remove old background overlays
  4804. (mapc (lambda (ov)
  4805. (if (eq (overlay-get ov 'face) 'org-block-background)
  4806. (delete-overlay ov)))
  4807. (overlays-at (/ (+ beg1 block-end) 2)))
  4808. ;; add a background overlay
  4809. (setq ovl (make-overlay beg1 block-end))
  4810. (overlay-put ovl 'face 'org-block-background)
  4811. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4812. (quoting
  4813. (add-text-properties beg1 (min (point-max) (1+ end1))
  4814. '(face org-block))) ; end of source block
  4815. ((not org-fontify-quote-and-verse-blocks))
  4816. ((string= block-type "quote")
  4817. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4818. ((string= block-type "verse")
  4819. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4820. (add-text-properties beg beg1 '(face org-block-begin-line))
  4821. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4822. '(face org-block-end-line))
  4823. t))
  4824. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4825. (add-text-properties
  4826. beg (match-end 3)
  4827. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4828. '(font-lock-fontified t invisible t)
  4829. '(font-lock-fontified t face org-document-info-keyword)))
  4830. (add-text-properties
  4831. (match-beginning 6) (match-end 6)
  4832. (if (string-equal dc1 "+title:")
  4833. '(font-lock-fontified t face org-document-title)
  4834. '(font-lock-fontified t face org-document-info))))
  4835. ((or (equal dc1 "+results")
  4836. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4837. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4838. "+call:" "+header:" "+headers:" "+name:"))
  4839. (and (match-end 4) (equal dc3 "+attr")))
  4840. (add-text-properties
  4841. beg (match-end 0)
  4842. '(font-lock-fontified t face org-meta-line))
  4843. t)
  4844. ((member dc3 '(" " ""))
  4845. (add-text-properties
  4846. beg (match-end 0)
  4847. '(font-lock-fontified t face font-lock-comment-face)))
  4848. ((not (member (char-after beg) '(?\ ?\t)))
  4849. ;; just any other in-buffer setting, but not indented
  4850. (add-text-properties
  4851. beg (match-end 0)
  4852. '(font-lock-fontified t face org-meta-line))
  4853. t)
  4854. (t nil))))))
  4855. (defun org-activate-angle-links (limit)
  4856. "Run through the buffer and add overlays to links."
  4857. (if (re-search-forward org-angle-link-re limit t)
  4858. (progn
  4859. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4860. (add-text-properties (match-beginning 0) (match-end 0)
  4861. (list 'mouse-face 'highlight
  4862. 'keymap org-mouse-map))
  4863. (org-rear-nonsticky-at (match-end 0))
  4864. t)))
  4865. (defun org-activate-footnote-links (limit)
  4866. "Run through the buffer and add overlays to footnotes."
  4867. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4868. (when fn
  4869. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4870. (org-remove-flyspell-overlays-in beg end)
  4871. (add-text-properties beg end
  4872. (list 'mouse-face 'highlight
  4873. 'keymap org-mouse-map
  4874. 'help-echo
  4875. (if (= (point-at-bol) beg)
  4876. "Footnote definition"
  4877. "Footnote reference")
  4878. 'font-lock-fontified t
  4879. 'font-lock-multiline t
  4880. 'face 'org-footnote))))))
  4881. (defun org-activate-bracket-links (limit)
  4882. "Run through the buffer and add overlays to bracketed links."
  4883. (if (re-search-forward org-bracket-link-regexp limit t)
  4884. (let* ((hl (org-match-string-no-properties 1))
  4885. (help (concat "LINK: " hl))
  4886. ;; FIXME: Above we should remove the escapes. But that
  4887. ;; requires another match, protecting match data, a lot
  4888. ;; of overhead for font-lock.
  4889. (ip (org-maybe-intangible
  4890. (list 'invisible 'org-link
  4891. 'keymap org-mouse-map 'mouse-face 'highlight
  4892. 'font-lock-multiline t 'help-echo help
  4893. 'htmlize-link `(:uri ,hl))))
  4894. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4895. 'font-lock-multiline t 'help-echo help
  4896. 'htmlize-link `(:uri ,hl))))
  4897. ;; We need to remove the invisible property here. Table narrowing
  4898. ;; may have made some of this invisible.
  4899. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4900. (remove-text-properties (match-beginning 0) (match-end 0)
  4901. '(invisible nil))
  4902. (if (match-end 3)
  4903. (progn
  4904. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4905. (org-rear-nonsticky-at (match-beginning 3))
  4906. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4907. (org-rear-nonsticky-at (match-end 3))
  4908. (add-text-properties (match-end 3) (match-end 0) ip)
  4909. (org-rear-nonsticky-at (match-end 0)))
  4910. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4911. (org-rear-nonsticky-at (match-beginning 1))
  4912. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4913. (org-rear-nonsticky-at (match-end 1))
  4914. (add-text-properties (match-end 1) (match-end 0) ip)
  4915. (org-rear-nonsticky-at (match-end 0)))
  4916. t)))
  4917. (defun org-activate-dates (limit)
  4918. "Run through the buffer and add overlays to dates."
  4919. (if (re-search-forward org-tsr-regexp-both limit t)
  4920. (progn
  4921. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4922. (add-text-properties (match-beginning 0) (match-end 0)
  4923. (list 'mouse-face 'highlight
  4924. 'keymap org-mouse-map))
  4925. (org-rear-nonsticky-at (match-end 0))
  4926. (when org-display-custom-times
  4927. (if (match-end 3)
  4928. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4929. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4930. t)))
  4931. (defvar org-target-link-regexp nil
  4932. "Regular expression matching radio targets in plain text.")
  4933. (make-variable-buffer-local 'org-target-link-regexp)
  4934. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4935. "Regular expression matching a link target.")
  4936. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4937. "Regular expression matching a radio target.")
  4938. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4939. "Regular expression matching any target.")
  4940. (defun org-activate-target-links (limit)
  4941. "Run through the buffer and add overlays to target matches."
  4942. (when org-target-link-regexp
  4943. (let ((case-fold-search t))
  4944. (if (re-search-forward org-target-link-regexp limit t)
  4945. (progn
  4946. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4947. (add-text-properties (match-beginning 0) (match-end 0)
  4948. (list 'mouse-face 'highlight
  4949. 'keymap org-mouse-map
  4950. 'help-echo "Radio target link"
  4951. 'org-linked-text t))
  4952. (org-rear-nonsticky-at (match-end 0))
  4953. t)))))
  4954. (defun org-update-radio-target-regexp ()
  4955. "Find all radio targets in this file and update the regular expression."
  4956. (interactive)
  4957. (when (memq 'radio org-activate-links)
  4958. (setq org-target-link-regexp
  4959. (org-make-target-link-regexp (org-all-targets 'radio)))
  4960. (org-restart-font-lock)))
  4961. (defun org-hide-wide-columns (limit)
  4962. (let (s e)
  4963. (setq s (text-property-any (point) (or limit (point-max))
  4964. 'org-cwidth t))
  4965. (when s
  4966. (setq e (next-single-property-change s 'org-cwidth))
  4967. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4968. (goto-char e)
  4969. t)))
  4970. (defvar org-latex-and-specials-regexp nil
  4971. "Regular expression for highlighting export special stuff.")
  4972. (defvar org-match-substring-regexp)
  4973. (defvar org-match-substring-with-braces-regexp)
  4974. ;; This should be with the exporter code, but we also use if for font-locking
  4975. (defconst org-export-html-special-string-regexps
  4976. '(("\\\\-" . "&shy;")
  4977. ("---\\([^-]\\)" . "&mdash;\\1")
  4978. ("--\\([^-]\\)" . "&ndash;\\1")
  4979. ("\\.\\.\\." . "&hellip;"))
  4980. "Regular expressions for special string conversion.")
  4981. (defun org-compute-latex-and-specials-regexp ()
  4982. "Compute regular expression for stuff treated specially by exporters."
  4983. (if (not org-highlight-latex-fragments-and-specials)
  4984. (org-set-local 'org-latex-and-specials-regexp nil)
  4985. (require 'org-exp)
  4986. (let*
  4987. ((matchers (plist-get org-format-latex-options :matchers))
  4988. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4989. org-latex-regexps)))
  4990. (org-export-allow-BIND nil)
  4991. (options (org-combine-plists (org-default-export-plist)
  4992. (org-infile-export-plist)))
  4993. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4994. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4995. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4996. (org-export-html-expand (plist-get options :expand-quoted-html))
  4997. (org-export-with-special-strings (plist-get options :special-strings))
  4998. (re-sub
  4999. (cond
  5000. ((equal org-export-with-sub-superscripts '{})
  5001. (list org-match-substring-with-braces-regexp))
  5002. (org-export-with-sub-superscripts
  5003. (list org-match-substring-regexp))))
  5004. (re-latex
  5005. (if org-export-with-LaTeX-fragments
  5006. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5007. (re-macros
  5008. (if org-export-with-TeX-macros
  5009. (list (concat "\\\\"
  5010. (regexp-opt
  5011. (append
  5012. (delq nil
  5013. (mapcar 'car-safe
  5014. (append org-entities-user
  5015. org-entities)))
  5016. (if (boundp 'org-latex-entities)
  5017. (mapcar (lambda (x)
  5018. (or (car-safe x) x))
  5019. org-latex-entities)
  5020. nil))
  5021. 'words))) ; FIXME
  5022. ))
  5023. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5024. (re-special (if org-export-with-special-strings
  5025. (mapcar (lambda (x) (car x))
  5026. org-export-html-special-string-regexps)))
  5027. (re-rest
  5028. (delq nil
  5029. (list
  5030. (if org-export-html-expand "@<[^>\n]+>")
  5031. ))))
  5032. (org-set-local
  5033. 'org-latex-and-specials-regexp
  5034. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5035. re-rest) "\\|")))))
  5036. (defun org-do-latex-and-special-faces (limit)
  5037. "Run through the buffer and add overlays to links."
  5038. (when org-latex-and-specials-regexp
  5039. (let (rtn d)
  5040. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5041. limit t))
  5042. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5043. 'face))
  5044. '(org-code org-verbatim underline)))
  5045. (progn
  5046. (setq rtn t
  5047. d (cond ((member (char-after (1+ (match-beginning 0)))
  5048. '(?_ ?^)) 1)
  5049. (t 0)))
  5050. (font-lock-prepend-text-property
  5051. (+ d (match-beginning 0)) (match-end 0)
  5052. 'face 'org-latex-and-export-specials)
  5053. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5054. '(font-lock-multiline t)))))
  5055. rtn)))
  5056. (defun org-restart-font-lock ()
  5057. "Restart `font-lock-mode', to force refontification."
  5058. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5059. (font-lock-mode -1)
  5060. (font-lock-mode 1)))
  5061. (defun org-all-targets (&optional radio)
  5062. "Return a list of all targets in this file.
  5063. When optional argument RADIO is non-nil, only find radio
  5064. targets."
  5065. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5066. (save-excursion
  5067. (goto-char (point-min))
  5068. (while (re-search-forward re nil t)
  5069. (let ((obj (org-element-context)))
  5070. (when (memq (org-element-type obj) '(radio-target target))
  5071. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5072. rtn)))
  5073. (defun org-make-target-link-regexp (targets)
  5074. "Make regular expression matching all strings in TARGETS.
  5075. The regular expression finds the targets also if there is a line break
  5076. between words."
  5077. (and targets
  5078. (concat
  5079. "\\<\\("
  5080. (mapconcat
  5081. (lambda (x)
  5082. (setq x (regexp-quote x))
  5083. (while (string-match " +" x)
  5084. (setq x (replace-match "\\s-+" t t x)))
  5085. x)
  5086. targets
  5087. "\\|")
  5088. "\\)\\>")))
  5089. (defun org-activate-tags (limit)
  5090. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5091. (progn
  5092. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5093. (add-text-properties (match-beginning 1) (match-end 1)
  5094. (list 'mouse-face 'highlight
  5095. 'keymap org-mouse-map))
  5096. (org-rear-nonsticky-at (match-end 1))
  5097. t)))
  5098. (defun org-outline-level ()
  5099. "Compute the outline level of the heading at point.
  5100. If this is called at a normal headline, the level is the number of stars.
  5101. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5102. (save-excursion
  5103. (if (not (condition-case nil
  5104. (org-back-to-heading t)
  5105. (error nil)))
  5106. 0
  5107. (looking-at org-outline-regexp)
  5108. (1- (- (match-end 0) (match-beginning 0))))))
  5109. (defvar org-font-lock-keywords nil)
  5110. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5111. "Regular expression matching a property line.")
  5112. (defvar org-font-lock-hook nil
  5113. "Functions to be called for special font lock stuff.")
  5114. (defvar org-font-lock-set-keywords-hook nil
  5115. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5116. This is called after `org-font-lock-extra-keywords' is defined, but before
  5117. it is installed to be used by font lock. This can be useful if something
  5118. needs to be inserted at a specific position in the font-lock sequence.")
  5119. (defun org-font-lock-hook (limit)
  5120. "Run `org-font-lock-hook' within LIMIT."
  5121. (run-hook-with-args 'org-font-lock-hook limit))
  5122. (defun org-set-font-lock-defaults ()
  5123. "Set font lock defaults for the current buffer."
  5124. (let* ((em org-fontify-emphasized-text)
  5125. (lk org-activate-links)
  5126. (org-font-lock-extra-keywords
  5127. (list
  5128. ;; Call the hook
  5129. '(org-font-lock-hook)
  5130. ;; Headlines
  5131. `(,(if org-fontify-whole-heading-line
  5132. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5133. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5134. (1 (org-get-level-face 1))
  5135. (2 (org-get-level-face 2))
  5136. (3 (org-get-level-face 3)))
  5137. ;; Table lines
  5138. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5139. (1 'org-table t))
  5140. ;; Table internals
  5141. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5142. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5143. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5144. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5145. ;; Drawers
  5146. (list org-drawer-regexp '(0 'org-special-keyword t))
  5147. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5148. ;; Properties
  5149. (list org-property-re
  5150. '(1 'org-special-keyword t)
  5151. '(3 'org-property-value t))
  5152. ;; Links
  5153. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5154. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5155. (if (memq 'plain lk) '(org-activate-plain-links))
  5156. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5157. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5158. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5159. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5160. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5161. '(org-hide-wide-columns (0 nil append))
  5162. ;; TODO keyword
  5163. (list (format org-heading-keyword-regexp-format
  5164. org-todo-regexp)
  5165. '(2 (org-get-todo-face 2) t))
  5166. ;; DONE
  5167. (if org-fontify-done-headline
  5168. (list (format org-heading-keyword-regexp-format
  5169. (concat
  5170. "\\(?:"
  5171. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5172. "\\)"))
  5173. '(2 'org-headline-done t))
  5174. nil)
  5175. ;; Priorities
  5176. '(org-font-lock-add-priority-faces)
  5177. ;; Tags
  5178. '(org-font-lock-add-tag-faces)
  5179. ;; Special keywords
  5180. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5181. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5182. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5183. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5184. ;; Emphasis
  5185. (if em
  5186. (if (featurep 'xemacs)
  5187. '(org-do-emphasis-faces (0 nil append))
  5188. '(org-do-emphasis-faces)))
  5189. ;; Checkboxes
  5190. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5191. 1 'org-checkbox prepend)
  5192. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5193. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5194. (0 (org-get-checkbox-statistics-face) t)))
  5195. ;; Description list items
  5196. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5197. 1 'org-list-dt prepend)
  5198. ;; ARCHIVEd headings
  5199. (list (concat
  5200. org-outline-regexp-bol
  5201. "\\(.*:" org-archive-tag ":.*\\)")
  5202. '(1 'org-archived prepend))
  5203. ;; Specials
  5204. '(org-do-latex-and-special-faces)
  5205. '(org-fontify-entities)
  5206. '(org-raise-scripts)
  5207. ;; Code
  5208. '(org-activate-code (1 'org-code t))
  5209. ;; COMMENT
  5210. (list (format org-heading-keyword-regexp-format
  5211. (concat "\\("
  5212. org-comment-string "\\|" org-quote-string
  5213. "\\)"))
  5214. '(2 'org-special-keyword t))
  5215. ;; Blocks and meta lines
  5216. '(org-fontify-meta-lines-and-blocks)
  5217. )))
  5218. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5219. (run-hooks 'org-font-lock-set-keywords-hook)
  5220. ;; Now set the full font-lock-keywords
  5221. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5222. (org-set-local 'font-lock-defaults
  5223. '(org-font-lock-keywords t nil nil backward-paragraph))
  5224. (kill-local-variable 'font-lock-keywords) nil))
  5225. (defun org-toggle-pretty-entities ()
  5226. "Toggle the composition display of entities as UTF8 characters."
  5227. (interactive)
  5228. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5229. (org-restart-font-lock)
  5230. (if org-pretty-entities
  5231. (message "Entities are displayed as UTF8 characters")
  5232. (save-restriction
  5233. (widen)
  5234. (org-decompose-region (point-min) (point-max))
  5235. (message "Entities are displayed plain"))))
  5236. (defvar org-custom-properties-overlays nil
  5237. "List of overlays used for custom properties.")
  5238. (make-variable-buffer-local 'org-custom-properties-overlays)
  5239. (defun org-toggle-custom-properties-visibility ()
  5240. "Display or hide properties in `org-custom-properties'."
  5241. (interactive)
  5242. (if org-custom-properties-overlays
  5243. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5244. (setq org-custom-properties-overlays nil))
  5245. (unless (not org-custom-properties)
  5246. (save-excursion
  5247. (save-restriction
  5248. (widen)
  5249. (goto-char (point-min))
  5250. (while (re-search-forward org-property-re nil t)
  5251. (mapc (lambda(p)
  5252. (when (equal p (substring (match-string 1) 1 -1))
  5253. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5254. (overlay-put o 'invisible t)
  5255. (overlay-put o 'org-custom-property t)
  5256. (push o org-custom-properties-overlays))))
  5257. org-custom-properties)))))))
  5258. (defun org-fontify-entities (limit)
  5259. "Find an entity to fontify."
  5260. (let (ee)
  5261. (when org-pretty-entities
  5262. (catch 'match
  5263. (while (re-search-forward
  5264. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5265. limit t)
  5266. (if (and (not (org-in-indented-comment-line))
  5267. (setq ee (org-entity-get (match-string 1)))
  5268. (= (length (nth 6 ee)) 1))
  5269. (let*
  5270. ((end (if (equal (match-string 2) "{}")
  5271. (match-end 2)
  5272. (match-end 1))))
  5273. (add-text-properties
  5274. (match-beginning 0) end
  5275. (list 'font-lock-fontified t))
  5276. (compose-region (match-beginning 0) end
  5277. (nth 6 ee) nil)
  5278. (backward-char 1)
  5279. (throw 'match t))))
  5280. nil))))
  5281. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5282. "Fontify string S like in Org-mode."
  5283. (with-temp-buffer
  5284. (insert s)
  5285. (let ((org-odd-levels-only odd-levels))
  5286. (org-mode)
  5287. (font-lock-fontify-buffer)
  5288. (buffer-string))))
  5289. (defvar org-m nil)
  5290. (defvar org-l nil)
  5291. (defvar org-f nil)
  5292. (defun org-get-level-face (n)
  5293. "Get the right face for match N in font-lock matching of headlines."
  5294. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5295. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5296. (if org-cycle-level-faces
  5297. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5298. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5299. (cond
  5300. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5301. ((eq n 2) org-f)
  5302. (t (if org-level-color-stars-only nil org-f))))
  5303. (defun org-get-todo-face (kwd)
  5304. "Get the right face for a TODO keyword KWD.
  5305. If KWD is a number, get the corresponding match group."
  5306. (if (numberp kwd) (setq kwd (match-string kwd)))
  5307. (or (org-face-from-face-or-color
  5308. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5309. (and (member kwd org-done-keywords) 'org-done)
  5310. 'org-todo))
  5311. (defun org-face-from-face-or-color (context inherit face-or-color)
  5312. "Create a face list that inherits INHERIT, but sets the foreground color.
  5313. When FACE-OR-COLOR is not a string, just return it."
  5314. (if (stringp face-or-color)
  5315. (list :inherit inherit
  5316. (cdr (assoc context org-faces-easy-properties))
  5317. face-or-color)
  5318. face-or-color))
  5319. (defun org-font-lock-add-tag-faces (limit)
  5320. "Add the special tag faces."
  5321. (when (and org-tag-faces org-tags-special-faces-re)
  5322. (while (re-search-forward org-tags-special-faces-re limit t)
  5323. (add-text-properties (match-beginning 1) (match-end 1)
  5324. (list 'face (org-get-tag-face 1)
  5325. 'font-lock-fontified t))
  5326. (backward-char 1))))
  5327. (defun org-font-lock-add-priority-faces (limit)
  5328. "Add the special priority faces."
  5329. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5330. (when (save-match-data (org-at-heading-p))
  5331. (add-text-properties
  5332. (match-beginning 0) (match-end 0)
  5333. (list 'face (or (org-face-from-face-or-color
  5334. 'priority 'org-special-keyword
  5335. (cdr (assoc (char-after (match-beginning 1))
  5336. org-priority-faces)))
  5337. 'org-special-keyword)
  5338. 'font-lock-fontified t)))))
  5339. (defun org-get-tag-face (kwd)
  5340. "Get the right face for a TODO keyword KWD.
  5341. If KWD is a number, get the corresponding match group."
  5342. (if (numberp kwd) (setq kwd (match-string kwd)))
  5343. (or (org-face-from-face-or-color
  5344. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5345. 'org-tag))
  5346. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5347. "Remove fontification and activation overlays from links."
  5348. (font-lock-default-unfontify-region beg end)
  5349. (let* ((buffer-undo-list t)
  5350. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5351. (inhibit-modification-hooks t)
  5352. deactivate-mark buffer-file-name buffer-file-truename)
  5353. (org-decompose-region beg end)
  5354. (remove-text-properties beg end
  5355. '(mouse-face t keymap t org-linked-text t
  5356. invisible t intangible t
  5357. org-no-flyspell t org-emphasis t))
  5358. (org-remove-font-lock-display-properties beg end)))
  5359. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5360. ((raise 0.3) (height 0.7))
  5361. ((raise -0.5))
  5362. ((raise 0.5)))
  5363. "Display properties for showing superscripts and subscripts.")
  5364. (defun org-remove-font-lock-display-properties (beg end)
  5365. "Remove specific display properties that have been added by font lock.
  5366. The will remove the raise properties that are used to show superscripts
  5367. and subscripts."
  5368. (let (next prop)
  5369. (while (< beg end)
  5370. (setq next (next-single-property-change beg 'display nil end)
  5371. prop (get-text-property beg 'display))
  5372. (if (member prop org-script-display)
  5373. (put-text-property beg next 'display nil))
  5374. (setq beg next))))
  5375. (defun org-raise-scripts (limit)
  5376. "Add raise properties to sub/superscripts."
  5377. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5378. (if (re-search-forward
  5379. (if (eq org-use-sub-superscripts t)
  5380. org-match-substring-regexp
  5381. org-match-substring-with-braces-regexp)
  5382. limit t)
  5383. (let* ((pos (point)) table-p comment-p
  5384. (mpos (match-beginning 3))
  5385. (emph-p (get-text-property mpos 'org-emphasis))
  5386. (link-p (get-text-property mpos 'mouse-face))
  5387. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5388. (goto-char (point-at-bol))
  5389. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5390. comment-p (org-looking-at-p "[ \t]*#"))
  5391. (goto-char pos)
  5392. ;; FIXME: Should we go back one character here, for a_b^c
  5393. ;; (goto-char (1- pos)) ;????????????????????
  5394. (if (or comment-p emph-p link-p keyw-p)
  5395. t
  5396. (put-text-property (match-beginning 3) (match-end 0)
  5397. 'display
  5398. (if (equal (char-after (match-beginning 2)) ?^)
  5399. (nth (if table-p 3 1) org-script-display)
  5400. (nth (if table-p 2 0) org-script-display)))
  5401. (add-text-properties (match-beginning 2) (match-end 2)
  5402. (list 'invisible t
  5403. 'org-dwidth t 'org-dwidth-n 1))
  5404. (if (and (eq (char-after (match-beginning 3)) ?{)
  5405. (eq (char-before (match-end 3)) ?}))
  5406. (progn
  5407. (add-text-properties
  5408. (match-beginning 3) (1+ (match-beginning 3))
  5409. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5410. (add-text-properties
  5411. (1- (match-end 3)) (match-end 3)
  5412. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5413. t)))))
  5414. ;;;; Visibility cycling, including org-goto and indirect buffer
  5415. ;;; Cycling
  5416. (defvar org-cycle-global-status nil)
  5417. (make-variable-buffer-local 'org-cycle-global-status)
  5418. (defvar org-cycle-subtree-status nil)
  5419. (make-variable-buffer-local 'org-cycle-subtree-status)
  5420. (defvar org-inlinetask-min-level)
  5421. ;;;###autoload
  5422. (defun org-cycle (&optional arg)
  5423. "TAB-action and visibility cycling for Org-mode.
  5424. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5425. is outline visibility cycling, but it also invokes other actions
  5426. in special contexts.
  5427. - When this function is called with a prefix argument, rotate the entire
  5428. buffer through 3 states (global cycling)
  5429. 1. OVERVIEW: Show only top-level headlines.
  5430. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5431. 3. SHOW ALL: Show everything.
  5432. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5433. determined by the variable `org-startup-folded', and by any VISIBILITY
  5434. properties in the buffer.
  5435. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5436. including any drawers.
  5437. - When inside a table, re-align the table and move to the next field.
  5438. - When point is at the beginning of a headline, rotate the subtree started
  5439. by this line through 3 different states (local cycling)
  5440. 1. FOLDED: Only the main headline is shown.
  5441. 2. CHILDREN: The main headline and the direct children are shown.
  5442. From this state, you can move to one of the children
  5443. and zoom in further.
  5444. 3. SUBTREE: Show the entire subtree, including body text.
  5445. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5446. - When point is at the beginning of an empty headline and the variable
  5447. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5448. of the headline by demoting and promoting it to likely levels. This
  5449. speeds up creation document structure by pressing TAB once or several
  5450. times right after creating a new headline.
  5451. - When there is a numeric prefix, go up to a heading with level ARG, do
  5452. a `show-subtree' and return to the previous cursor position. If ARG
  5453. is negative, go up that many levels.
  5454. - When point is not at the beginning of a headline, execute the global
  5455. binding for TAB, which is re-indenting the line. See the option
  5456. `org-cycle-emulate-tab' for details.
  5457. - Special case: if point is at the beginning of the buffer and there is
  5458. no headline in line 1, this function will act as if called with prefix arg
  5459. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5460. But only if also the variable `org-cycle-global-at-bob' is t."
  5461. (interactive "P")
  5462. (org-load-modules-maybe)
  5463. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5464. (and org-cycle-level-after-item/entry-creation
  5465. (or (org-cycle-level)
  5466. (org-cycle-item-indentation))))
  5467. (let* ((limit-level
  5468. (or org-cycle-max-level
  5469. (and (boundp 'org-inlinetask-min-level)
  5470. org-inlinetask-min-level
  5471. (1- org-inlinetask-min-level))))
  5472. (nstars (and limit-level
  5473. (if org-odd-levels-only
  5474. (and limit-level (1- (* limit-level 2)))
  5475. limit-level)))
  5476. (org-outline-regexp
  5477. (if (not (derived-mode-p 'org-mode))
  5478. outline-regexp
  5479. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5480. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5481. (not (looking-at org-outline-regexp))))
  5482. (org-cycle-hook
  5483. (if bob-special
  5484. (delq 'org-optimize-window-after-visibility-change
  5485. (copy-sequence org-cycle-hook))
  5486. org-cycle-hook))
  5487. (pos (point)))
  5488. (if (or bob-special (equal arg '(4)))
  5489. ;; special case: use global cycling
  5490. (setq arg t))
  5491. (cond
  5492. ((equal arg '(16))
  5493. (setq last-command 'dummy)
  5494. (org-set-startup-visibility)
  5495. (message "Startup visibility, plus VISIBILITY properties"))
  5496. ((equal arg '(64))
  5497. (show-all)
  5498. (message "Entire buffer visible, including drawers"))
  5499. ;; Table: enter it or move to the next field.
  5500. ((org-at-table-p 'any)
  5501. (if (org-at-table.el-p)
  5502. (message "Use C-c ' to edit table.el tables")
  5503. (if arg (org-table-edit-field t)
  5504. (org-table-justify-field-maybe)
  5505. (call-interactively 'org-table-next-field))))
  5506. ((run-hook-with-args-until-success
  5507. 'org-tab-after-check-for-table-hook))
  5508. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5509. ((eq arg t) (org-cycle-internal-global))
  5510. ;; Drawers: delegate to `org-flag-drawer'.
  5511. ((and org-drawers org-drawer-regexp
  5512. (save-excursion
  5513. (beginning-of-line 1)
  5514. (looking-at org-drawer-regexp)))
  5515. (org-flag-drawer ; toggle block visibility
  5516. (not (get-char-property (match-end 0) 'invisible))))
  5517. ;; Show-subtree, ARG levels up from here.
  5518. ((integerp arg)
  5519. (save-excursion
  5520. (org-back-to-heading)
  5521. (outline-up-heading (if (< arg 0) (- arg)
  5522. (- (funcall outline-level) arg)))
  5523. (org-show-subtree)))
  5524. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5525. ((and (featurep 'org-inlinetask)
  5526. (org-inlinetask-at-task-p)
  5527. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5528. (org-inlinetask-toggle-visibility))
  5529. ((org-try-cdlatex-tab))
  5530. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5531. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5532. (save-excursion (beginning-of-line 1)
  5533. (looking-at org-outline-regexp)))
  5534. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5535. (org-cycle-internal-local))
  5536. ;; From there: TAB emulation and template completion.
  5537. (buffer-read-only (org-back-to-heading))
  5538. ((run-hook-with-args-until-success
  5539. 'org-tab-after-check-for-cycling-hook))
  5540. ((org-try-structure-completion))
  5541. ((run-hook-with-args-until-success
  5542. 'org-tab-before-tab-emulation-hook))
  5543. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5544. (or (not (bolp))
  5545. (not (looking-at org-outline-regexp))))
  5546. (call-interactively (global-key-binding "\t")))
  5547. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5548. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5549. (or (and (eq org-cycle-emulate-tab 'white)
  5550. (= (match-end 0) (point-at-eol)))
  5551. (and (eq org-cycle-emulate-tab 'whitestart)
  5552. (>= (match-end 0) pos))))
  5553. t
  5554. (eq org-cycle-emulate-tab t))
  5555. (call-interactively (global-key-binding "\t")))
  5556. (t (save-excursion
  5557. (org-back-to-heading)
  5558. (org-cycle)))))))
  5559. (defun org-cycle-internal-global ()
  5560. "Do the global cycling action."
  5561. ;; Hack to avoid display of messages for .org attachments in Gnus
  5562. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5563. (cond
  5564. ((and (eq last-command this-command)
  5565. (eq org-cycle-global-status 'overview))
  5566. ;; We just created the overview - now do table of contents
  5567. ;; This can be slow in very large buffers, so indicate action
  5568. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5569. (unless ga (message "CONTENTS..."))
  5570. (org-content)
  5571. (unless ga (message "CONTENTS...done"))
  5572. (setq org-cycle-global-status 'contents)
  5573. (run-hook-with-args 'org-cycle-hook 'contents))
  5574. ((and (eq last-command this-command)
  5575. (eq org-cycle-global-status 'contents))
  5576. ;; We just showed the table of contents - now show everything
  5577. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5578. (show-all)
  5579. (unless ga (message "SHOW ALL"))
  5580. (setq org-cycle-global-status 'all)
  5581. (run-hook-with-args 'org-cycle-hook 'all))
  5582. (t
  5583. ;; Default action: go to overview
  5584. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5585. (org-overview)
  5586. (unless ga (message "OVERVIEW"))
  5587. (setq org-cycle-global-status 'overview)
  5588. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5589. (defun org-cycle-internal-local ()
  5590. "Do the local cycling action."
  5591. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5592. ;; First, determine end of headline (EOH), end of subtree or item
  5593. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5594. (save-excursion
  5595. (if (org-at-item-p)
  5596. (progn
  5597. (beginning-of-line)
  5598. (setq struct (org-list-struct))
  5599. (setq eoh (point-at-eol))
  5600. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5601. (setq has-children (org-list-has-child-p (point) struct)))
  5602. (org-back-to-heading)
  5603. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5604. (setq eos (save-excursion
  5605. (org-end-of-subtree t)
  5606. (unless (eobp)
  5607. (skip-chars-forward " \t\n"))
  5608. (if (eobp) (point) (1- (point)))))
  5609. (setq has-children
  5610. (or (save-excursion
  5611. (let ((level (funcall outline-level)))
  5612. (outline-next-heading)
  5613. (and (org-at-heading-p t)
  5614. (> (funcall outline-level) level))))
  5615. (save-excursion
  5616. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5617. ;; Determine end invisible part of buffer (EOL)
  5618. (beginning-of-line 2)
  5619. ;; XEmacs doesn't have `next-single-char-property-change'
  5620. (if (featurep 'xemacs)
  5621. (while (and (not (eobp)) ;; this is like `next-line'
  5622. (get-char-property (1- (point)) 'invisible))
  5623. (beginning-of-line 2))
  5624. (while (and (not (eobp)) ;; this is like `next-line'
  5625. (get-char-property (1- (point)) 'invisible))
  5626. (goto-char (next-single-char-property-change (point) 'invisible))
  5627. (and (eolp) (beginning-of-line 2))))
  5628. (setq eol (point)))
  5629. ;; Find out what to do next and set `this-command'
  5630. (cond
  5631. ((= eos eoh)
  5632. ;; Nothing is hidden behind this heading
  5633. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5634. (message "EMPTY ENTRY")
  5635. (setq org-cycle-subtree-status nil)
  5636. (save-excursion
  5637. (goto-char eos)
  5638. (outline-next-heading)
  5639. (if (outline-invisible-p) (org-flag-heading nil))))
  5640. ((and (or (>= eol eos)
  5641. (not (string-match "\\S-" (buffer-substring eol eos))))
  5642. (or has-children
  5643. (not (setq children-skipped
  5644. org-cycle-skip-children-state-if-no-children))))
  5645. ;; Entire subtree is hidden in one line: children view
  5646. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5647. (if (org-at-item-p)
  5648. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5649. (org-show-entry)
  5650. (org-with-limited-levels (show-children))
  5651. ;; FIXME: This slows down the func way too much.
  5652. ;; How keep drawers hidden in subtree anyway?
  5653. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5654. ;; (org-cycle-hide-drawers 'subtree))
  5655. ;; Fold every list in subtree to top-level items.
  5656. (when (eq org-cycle-include-plain-lists 'integrate)
  5657. (save-excursion
  5658. (org-back-to-heading)
  5659. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5660. (beginning-of-line 1)
  5661. (let* ((struct (org-list-struct))
  5662. (prevs (org-list-prevs-alist struct))
  5663. (end (org-list-get-bottom-point struct)))
  5664. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5665. (org-list-get-all-items (point) struct prevs))
  5666. (goto-char end))))))
  5667. (message "CHILDREN")
  5668. (save-excursion
  5669. (goto-char eos)
  5670. (outline-next-heading)
  5671. (if (outline-invisible-p) (org-flag-heading nil)))
  5672. (setq org-cycle-subtree-status 'children)
  5673. (run-hook-with-args 'org-cycle-hook 'children))
  5674. ((or children-skipped
  5675. (and (eq last-command this-command)
  5676. (eq org-cycle-subtree-status 'children)))
  5677. ;; We just showed the children, or no children are there,
  5678. ;; now show everything.
  5679. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5680. (outline-flag-region eoh eos nil)
  5681. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5682. (setq org-cycle-subtree-status 'subtree)
  5683. (run-hook-with-args 'org-cycle-hook 'subtree))
  5684. (t
  5685. ;; Default action: hide the subtree.
  5686. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5687. (outline-flag-region eoh eos t)
  5688. (message "FOLDED")
  5689. (setq org-cycle-subtree-status 'folded)
  5690. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5691. ;;;###autoload
  5692. (defun org-global-cycle (&optional arg)
  5693. "Cycle the global visibility. For details see `org-cycle'.
  5694. With \\[universal-argument] prefix arg, switch to startup visibility.
  5695. With a numeric prefix, show all headlines up to that level."
  5696. (interactive "P")
  5697. (let ((org-cycle-include-plain-lists
  5698. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5699. (cond
  5700. ((integerp arg)
  5701. (show-all)
  5702. (hide-sublevels arg)
  5703. (setq org-cycle-global-status 'contents))
  5704. ((equal arg '(4))
  5705. (org-set-startup-visibility)
  5706. (message "Startup visibility, plus VISIBILITY properties."))
  5707. (t
  5708. (org-cycle '(4))))))
  5709. (defun org-set-startup-visibility ()
  5710. "Set the visibility required by startup options and properties."
  5711. (cond
  5712. ((eq org-startup-folded t)
  5713. (org-cycle '(4)))
  5714. ((eq org-startup-folded 'content)
  5715. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5716. (org-cycle '(4)) (org-cycle '(4)))))
  5717. (unless (eq org-startup-folded 'showeverything)
  5718. (if org-hide-block-startup (org-hide-block-all))
  5719. (org-set-visibility-according-to-property 'no-cleanup)
  5720. (org-cycle-hide-archived-subtrees 'all)
  5721. (org-cycle-hide-drawers 'all)
  5722. (org-cycle-show-empty-lines t)))
  5723. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5724. "Switch subtree visibilities according to :VISIBILITY: property."
  5725. (interactive)
  5726. (let (org-show-entry-below state)
  5727. (save-excursion
  5728. (goto-char (point-min))
  5729. (while (re-search-forward
  5730. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5731. nil t)
  5732. (setq state (match-string 1))
  5733. (save-excursion
  5734. (org-back-to-heading t)
  5735. (hide-subtree)
  5736. (org-reveal)
  5737. (cond
  5738. ((equal state '("fold" "folded"))
  5739. (hide-subtree))
  5740. ((equal state "children")
  5741. (org-show-hidden-entry)
  5742. (show-children))
  5743. ((equal state "content")
  5744. (save-excursion
  5745. (save-restriction
  5746. (org-narrow-to-subtree)
  5747. (org-content))))
  5748. ((member state '("all" "showall"))
  5749. (show-subtree)))))
  5750. (unless no-cleanup
  5751. (org-cycle-hide-archived-subtrees 'all)
  5752. (org-cycle-hide-drawers 'all)
  5753. (org-cycle-show-empty-lines 'all)))))
  5754. ;; This function uses outline-regexp instead of the more fundamental
  5755. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5756. ;; buffers, where outline-regexp is needed.
  5757. (defun org-overview ()
  5758. "Switch to overview mode, showing only top-level headlines.
  5759. Really, this shows all headlines with level equal or greater than the level
  5760. of the first headline in the buffer. This is important, because if the
  5761. first headline is not level one, then (hide-sublevels 1) gives confusing
  5762. results."
  5763. (interactive)
  5764. (let ((level (save-excursion
  5765. (goto-char (point-min))
  5766. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5767. (progn
  5768. (goto-char (match-beginning 0))
  5769. (funcall outline-level))))))
  5770. (and level (hide-sublevels level))))
  5771. (defun org-content (&optional arg)
  5772. "Show all headlines in the buffer, like a table of contents.
  5773. With numerical argument N, show content up to level N."
  5774. (interactive "P")
  5775. (save-excursion
  5776. ;; Visit all headings and show their offspring
  5777. (and (integerp arg) (org-overview))
  5778. (goto-char (point-max))
  5779. (catch 'exit
  5780. (while (and (progn (condition-case nil
  5781. (outline-previous-visible-heading 1)
  5782. (error (goto-char (point-min))))
  5783. t)
  5784. (looking-at org-outline-regexp))
  5785. (if (integerp arg)
  5786. (show-children (1- arg))
  5787. (show-branches))
  5788. (if (bobp) (throw 'exit nil))))))
  5789. (defun org-optimize-window-after-visibility-change (state)
  5790. "Adjust the window after a change in outline visibility.
  5791. This function is the default value of the hook `org-cycle-hook'."
  5792. (when (get-buffer-window (current-buffer))
  5793. (cond
  5794. ((eq state 'content) nil)
  5795. ((eq state 'all) nil)
  5796. ((eq state 'folded) nil)
  5797. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5798. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5799. (defun org-remove-empty-overlays-at (pos)
  5800. "Remove outline overlays that do not contain non-white stuff."
  5801. (mapc
  5802. (lambda (o)
  5803. (and (eq 'outline (overlay-get o 'invisible))
  5804. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5805. (overlay-end o))))
  5806. (delete-overlay o)))
  5807. (overlays-at pos)))
  5808. (defun org-clean-visibility-after-subtree-move ()
  5809. "Fix visibility issues after moving a subtree."
  5810. ;; First, find a reasonable region to look at:
  5811. ;; Start two siblings above, end three below
  5812. (let* ((beg (save-excursion
  5813. (and (org-get-last-sibling)
  5814. (org-get-last-sibling))
  5815. (point)))
  5816. (end (save-excursion
  5817. (and (org-get-next-sibling)
  5818. (org-get-next-sibling)
  5819. (org-get-next-sibling))
  5820. (if (org-at-heading-p)
  5821. (point-at-eol)
  5822. (point))))
  5823. (level (looking-at "\\*+"))
  5824. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5825. (save-excursion
  5826. (save-restriction
  5827. (narrow-to-region beg end)
  5828. (when re
  5829. ;; Properly fold already folded siblings
  5830. (goto-char (point-min))
  5831. (while (re-search-forward re nil t)
  5832. (if (and (not (outline-invisible-p))
  5833. (save-excursion
  5834. (goto-char (point-at-eol)) (outline-invisible-p)))
  5835. (hide-entry))))
  5836. (org-cycle-show-empty-lines 'overview)
  5837. (org-cycle-hide-drawers 'overview)))))
  5838. (defun org-cycle-show-empty-lines (state)
  5839. "Show empty lines above all visible headlines.
  5840. The region to be covered depends on STATE when called through
  5841. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5842. entire buffer covered. Note that an empty line is only shown if there
  5843. are at least `org-cycle-separator-lines' empty lines before the headline."
  5844. (when (not (= org-cycle-separator-lines 0))
  5845. (save-excursion
  5846. (let* ((n (abs org-cycle-separator-lines))
  5847. (re (cond
  5848. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5849. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5850. (t (let ((ns (number-to-string (- n 2))))
  5851. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5852. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5853. beg end b e)
  5854. (cond
  5855. ((memq state '(overview contents t))
  5856. (setq beg (point-min) end (point-max)))
  5857. ((memq state '(children folded))
  5858. (setq beg (point) end (progn (org-end-of-subtree t t)
  5859. (beginning-of-line 2)
  5860. (point)))))
  5861. (when beg
  5862. (goto-char beg)
  5863. (while (re-search-forward re end t)
  5864. (unless (get-char-property (match-end 1) 'invisible)
  5865. (setq e (match-end 1))
  5866. (if (< org-cycle-separator-lines 0)
  5867. (setq b (save-excursion
  5868. (goto-char (match-beginning 0))
  5869. (org-back-over-empty-lines)
  5870. (if (save-excursion
  5871. (goto-char (max (point-min) (1- (point))))
  5872. (org-at-heading-p))
  5873. (1- (point))
  5874. (point))))
  5875. (setq b (match-beginning 1)))
  5876. (outline-flag-region b e nil)))))))
  5877. ;; Never hide empty lines at the end of the file.
  5878. (save-excursion
  5879. (goto-char (point-max))
  5880. (outline-previous-heading)
  5881. (outline-end-of-heading)
  5882. (if (and (looking-at "[ \t\n]+")
  5883. (= (match-end 0) (point-max)))
  5884. (outline-flag-region (point) (match-end 0) nil))))
  5885. (defun org-show-empty-lines-in-parent ()
  5886. "Move to the parent and re-show empty lines before visible headlines."
  5887. (save-excursion
  5888. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5889. (org-cycle-show-empty-lines context))))
  5890. (defun org-files-list ()
  5891. "Return `org-agenda-files' list, plus all open org-mode files.
  5892. This is useful for operations that need to scan all of a user's
  5893. open and agenda-wise Org files."
  5894. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5895. (dolist (buf (buffer-list))
  5896. (with-current-buffer buf
  5897. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5898. (let ((file (expand-file-name (buffer-file-name))))
  5899. (unless (member file files)
  5900. (push file files))))))
  5901. files))
  5902. (defsubst org-entry-beginning-position ()
  5903. "Return the beginning position of the current entry."
  5904. (save-excursion (outline-back-to-heading t) (point)))
  5905. (defsubst org-entry-end-position ()
  5906. "Return the end position of the current entry."
  5907. (save-excursion (outline-next-heading) (point)))
  5908. (defun org-cycle-hide-drawers (state)
  5909. "Re-hide all drawers after a visibility state change."
  5910. (when (and (derived-mode-p 'org-mode)
  5911. (not (memq state '(overview folded contents))))
  5912. (save-excursion
  5913. (let* ((globalp (memq state '(contents all)))
  5914. (beg (if globalp (point-min) (point)))
  5915. (end (if globalp (point-max)
  5916. (if (eq state 'children)
  5917. (save-excursion (outline-next-heading) (point))
  5918. (org-end-of-subtree t)))))
  5919. (goto-char beg)
  5920. (while (re-search-forward org-drawer-regexp end t)
  5921. (org-flag-drawer t))))))
  5922. (defun org-cycle-hide-inline-tasks (state)
  5923. "Re-hide inline task when switching to 'contents visibility state."
  5924. (when (and (eq state 'contents)
  5925. (boundp 'org-inlinetask-min-level)
  5926. org-inlinetask-min-level)
  5927. (hide-sublevels (1- org-inlinetask-min-level))))
  5928. (defun org-flag-drawer (flag)
  5929. "When FLAG is non-nil, hide the drawer we are within.
  5930. Otherwise make it visible."
  5931. (save-excursion
  5932. (beginning-of-line 1)
  5933. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5934. (let ((b (match-end 0)))
  5935. (if (re-search-forward
  5936. "^[ \t]*:END:"
  5937. (save-excursion (outline-next-heading) (point)) t)
  5938. (outline-flag-region b (point-at-eol) flag)
  5939. (error ":END: line missing at position %s" b))))))
  5940. (defun org-subtree-end-visible-p ()
  5941. "Is the end of the current subtree visible?"
  5942. (pos-visible-in-window-p
  5943. (save-excursion (org-end-of-subtree t) (point))))
  5944. (defun org-first-headline-recenter (&optional N)
  5945. "Move cursor to the first headline and recenter the headline.
  5946. Optional argument N means put the headline into the Nth line of the window."
  5947. (goto-char (point-min))
  5948. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5949. (beginning-of-line)
  5950. (recenter (prefix-numeric-value N))))
  5951. ;;; Saving and restoring visibility
  5952. (defun org-outline-overlay-data (&optional use-markers)
  5953. "Return a list of the locations of all outline overlays.
  5954. These are overlays with the `invisible' property value `outline'.
  5955. The return value is a list of cons cells, with start and stop
  5956. positions for each overlay.
  5957. If USE-MARKERS is set, return the positions as markers."
  5958. (let (beg end)
  5959. (save-excursion
  5960. (save-restriction
  5961. (widen)
  5962. (delq nil
  5963. (mapcar (lambda (o)
  5964. (when (eq (overlay-get o 'invisible) 'outline)
  5965. (setq beg (overlay-start o)
  5966. end (overlay-end o))
  5967. (and beg end (> end beg)
  5968. (if use-markers
  5969. (cons (move-marker (make-marker) beg)
  5970. (move-marker (make-marker) end))
  5971. (cons beg end)))))
  5972. (overlays-in (point-min) (point-max))))))))
  5973. (defun org-set-outline-overlay-data (data)
  5974. "Create visibility overlays for all positions in DATA.
  5975. DATA should have been made by `org-outline-overlay-data'."
  5976. (let (o)
  5977. (save-excursion
  5978. (save-restriction
  5979. (widen)
  5980. (show-all)
  5981. (mapc (lambda (c)
  5982. (outline-flag-region (car c) (cdr c) t))
  5983. data)))))
  5984. ;;; Folding of blocks
  5985. (defvar org-hide-block-overlays nil
  5986. "Overlays hiding blocks.")
  5987. (make-variable-buffer-local 'org-hide-block-overlays)
  5988. (defun org-block-map (function &optional start end)
  5989. "Call FUNCTION at the head of all source blocks in the current buffer.
  5990. Optional arguments START and END can be used to limit the range."
  5991. (let ((start (or start (point-min)))
  5992. (end (or end (point-max))))
  5993. (save-excursion
  5994. (goto-char start)
  5995. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5996. (save-excursion
  5997. (save-match-data
  5998. (goto-char (match-beginning 0))
  5999. (funcall function)))))))
  6000. (defun org-hide-block-toggle-all ()
  6001. "Toggle the visibility of all blocks in the current buffer."
  6002. (org-block-map #'org-hide-block-toggle))
  6003. (defun org-hide-block-all ()
  6004. "Fold all blocks in the current buffer."
  6005. (interactive)
  6006. (org-show-block-all)
  6007. (org-block-map #'org-hide-block-toggle-maybe))
  6008. (defun org-show-block-all ()
  6009. "Unfold all blocks in the current buffer."
  6010. (interactive)
  6011. (mapc 'delete-overlay org-hide-block-overlays)
  6012. (setq org-hide-block-overlays nil))
  6013. (defun org-hide-block-toggle-maybe ()
  6014. "Toggle visibility of block at point."
  6015. (interactive)
  6016. (let ((case-fold-search t))
  6017. (if (save-excursion
  6018. (beginning-of-line 1)
  6019. (looking-at org-block-regexp))
  6020. (progn (org-hide-block-toggle)
  6021. t) ;; to signal that we took action
  6022. nil))) ;; to signal that we did not
  6023. (defun org-hide-block-toggle (&optional force)
  6024. "Toggle the visibility of the current block."
  6025. (interactive)
  6026. (save-excursion
  6027. (beginning-of-line)
  6028. (if (re-search-forward org-block-regexp nil t)
  6029. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6030. (end (match-end 0)) ;; end of entire body
  6031. ov)
  6032. (if (memq t (mapcar (lambda (overlay)
  6033. (eq (overlay-get overlay 'invisible)
  6034. 'org-hide-block))
  6035. (overlays-at start)))
  6036. (if (or (not force) (eq force 'off))
  6037. (mapc (lambda (ov)
  6038. (when (member ov org-hide-block-overlays)
  6039. (setq org-hide-block-overlays
  6040. (delq ov org-hide-block-overlays)))
  6041. (when (eq (overlay-get ov 'invisible)
  6042. 'org-hide-block)
  6043. (delete-overlay ov)))
  6044. (overlays-at start)))
  6045. (setq ov (make-overlay start end))
  6046. (overlay-put ov 'invisible 'org-hide-block)
  6047. ;; make the block accessible to isearch
  6048. (overlay-put
  6049. ov 'isearch-open-invisible
  6050. (lambda (ov)
  6051. (when (member ov org-hide-block-overlays)
  6052. (setq org-hide-block-overlays
  6053. (delq ov org-hide-block-overlays)))
  6054. (when (eq (overlay-get ov 'invisible)
  6055. 'org-hide-block)
  6056. (delete-overlay ov))))
  6057. (push ov org-hide-block-overlays)))
  6058. (error "Not looking at a source block"))))
  6059. ;; org-tab-after-check-for-cycling-hook
  6060. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6061. ;; Remove overlays when changing major mode
  6062. (add-hook 'org-mode-hook
  6063. (lambda () (org-add-hook 'change-major-mode-hook
  6064. 'org-show-block-all 'append 'local)))
  6065. ;;; Org-goto
  6066. (defvar org-goto-window-configuration nil)
  6067. (defvar org-goto-marker nil)
  6068. (defvar org-goto-map
  6069. (let ((map (make-sparse-keymap)))
  6070. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6071. (while (setq cmd (pop cmds))
  6072. (substitute-key-definition cmd cmd map global-map)))
  6073. (suppress-keymap map)
  6074. (org-defkey map "\C-m" 'org-goto-ret)
  6075. (org-defkey map [(return)] 'org-goto-ret)
  6076. (org-defkey map [(left)] 'org-goto-left)
  6077. (org-defkey map [(right)] 'org-goto-right)
  6078. (org-defkey map [(control ?g)] 'org-goto-quit)
  6079. (org-defkey map "\C-i" 'org-cycle)
  6080. (org-defkey map [(tab)] 'org-cycle)
  6081. (org-defkey map [(down)] 'outline-next-visible-heading)
  6082. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6083. (if org-goto-auto-isearch
  6084. (if (fboundp 'define-key-after)
  6085. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6086. nil)
  6087. (org-defkey map "q" 'org-goto-quit)
  6088. (org-defkey map "n" 'outline-next-visible-heading)
  6089. (org-defkey map "p" 'outline-previous-visible-heading)
  6090. (org-defkey map "f" 'outline-forward-same-level)
  6091. (org-defkey map "b" 'outline-backward-same-level)
  6092. (org-defkey map "u" 'outline-up-heading))
  6093. (org-defkey map "/" 'org-occur)
  6094. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6095. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6096. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6097. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6098. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6099. map))
  6100. (defconst org-goto-help
  6101. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6102. RET=jump to location [Q]uit and return to previous location
  6103. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6104. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6105. ;; FIXME: Docstring does not mention both interfaces
  6106. (defun org-goto (&optional alternative-interface)
  6107. "Look up a different location in the current file, keeping current visibility.
  6108. When you want look-up or go to a different location in a
  6109. document, the fastest way is often to fold the entire buffer and
  6110. then dive into the tree. This method has the disadvantage, that
  6111. the previous location will be folded, which may not be what you
  6112. want.
  6113. This command works around this by showing a copy of the current
  6114. buffer in an indirect buffer, in overview mode. You can dive
  6115. into the tree in that copy, use org-occur and incremental search
  6116. to find a location. When pressing RET or `Q', the command
  6117. returns to the original buffer in which the visibility is still
  6118. unchanged. After RET it will also jump to the location selected
  6119. in the indirect buffer and expose the headline hierarchy above.
  6120. With a prefix argument, use the alternative interface: e.g. if
  6121. `org-goto-interface' is 'outline use 'outline-path-completion."
  6122. (interactive "P")
  6123. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6124. (org-refile-use-outline-path t)
  6125. (org-refile-target-verify-function nil)
  6126. (interface
  6127. (if (not alternative-interface)
  6128. org-goto-interface
  6129. (if (eq org-goto-interface 'outline)
  6130. 'outline-path-completion
  6131. 'outline)))
  6132. (org-goto-start-pos (point))
  6133. (selected-point
  6134. (if (eq interface 'outline)
  6135. (car (org-get-location (current-buffer) org-goto-help))
  6136. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6137. (org-refile-check-position pa)
  6138. (nth 3 pa)))))
  6139. (if selected-point
  6140. (progn
  6141. (org-mark-ring-push org-goto-start-pos)
  6142. (goto-char selected-point)
  6143. (if (or (outline-invisible-p) (org-invisible-p2))
  6144. (org-show-context 'org-goto)))
  6145. (message "Quit"))))
  6146. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6147. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6148. (defvar org-goto-local-auto-isearch-map) ; defined below
  6149. (defun org-get-location (buf help)
  6150. "Let the user select a location in the Org-mode buffer BUF.
  6151. This function uses a recursive edit. It returns the selected position
  6152. or nil."
  6153. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6154. (isearch-hide-immediately nil)
  6155. (isearch-search-fun-function
  6156. (lambda () 'org-goto-local-search-headings))
  6157. (org-goto-selected-point org-goto-exit-command)
  6158. (pop-up-frames nil)
  6159. (special-display-buffer-names nil)
  6160. (special-display-regexps nil)
  6161. (special-display-function nil))
  6162. (save-excursion
  6163. (save-window-excursion
  6164. (delete-other-windows)
  6165. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6166. (org-pop-to-buffer-same-window
  6167. (condition-case nil
  6168. (make-indirect-buffer (current-buffer) "*org-goto*")
  6169. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6170. (with-output-to-temp-buffer "*Help*"
  6171. (princ help))
  6172. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6173. (setq buffer-read-only nil)
  6174. (let ((org-startup-truncated t)
  6175. (org-startup-folded nil)
  6176. (org-startup-align-all-tables nil))
  6177. (org-mode)
  6178. (org-overview))
  6179. (setq buffer-read-only t)
  6180. (if (and (boundp 'org-goto-start-pos)
  6181. (integer-or-marker-p org-goto-start-pos))
  6182. (let ((org-show-hierarchy-above t)
  6183. (org-show-siblings t)
  6184. (org-show-following-heading t))
  6185. (goto-char org-goto-start-pos)
  6186. (and (outline-invisible-p) (org-show-context)))
  6187. (goto-char (point-min)))
  6188. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6189. (message "Select location and press RET")
  6190. (use-local-map org-goto-map)
  6191. (recursive-edit)
  6192. ))
  6193. (kill-buffer "*org-goto*")
  6194. (cons org-goto-selected-point org-goto-exit-command)))
  6195. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6196. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6197. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6198. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6199. (defun org-goto-local-search-headings (string bound noerror)
  6200. "Search and make sure that any matches are in headlines."
  6201. (catch 'return
  6202. (while (if isearch-forward
  6203. (search-forward string bound noerror)
  6204. (search-backward string bound noerror))
  6205. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6206. (and (member :headline context)
  6207. (not (member :tags context))))
  6208. (throw 'return (point))))))
  6209. (defun org-goto-local-auto-isearch ()
  6210. "Start isearch."
  6211. (interactive)
  6212. (goto-char (point-min))
  6213. (let ((keys (this-command-keys)))
  6214. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6215. (isearch-mode t)
  6216. (isearch-process-search-char (string-to-char keys)))))
  6217. (defun org-goto-ret (&optional arg)
  6218. "Finish `org-goto' by going to the new location."
  6219. (interactive "P")
  6220. (setq org-goto-selected-point (point)
  6221. org-goto-exit-command 'return)
  6222. (throw 'exit nil))
  6223. (defun org-goto-left ()
  6224. "Finish `org-goto' by going to the new location."
  6225. (interactive)
  6226. (if (org-at-heading-p)
  6227. (progn
  6228. (beginning-of-line 1)
  6229. (setq org-goto-selected-point (point)
  6230. org-goto-exit-command 'left)
  6231. (throw 'exit nil))
  6232. (error "Not on a heading")))
  6233. (defun org-goto-right ()
  6234. "Finish `org-goto' by going to the new location."
  6235. (interactive)
  6236. (if (org-at-heading-p)
  6237. (progn
  6238. (setq org-goto-selected-point (point)
  6239. org-goto-exit-command 'right)
  6240. (throw 'exit nil))
  6241. (error "Not on a heading")))
  6242. (defun org-goto-quit ()
  6243. "Finish `org-goto' without cursor motion."
  6244. (interactive)
  6245. (setq org-goto-selected-point nil)
  6246. (setq org-goto-exit-command 'quit)
  6247. (throw 'exit nil))
  6248. ;;; Indirect buffer display of subtrees
  6249. (defvar org-indirect-dedicated-frame nil
  6250. "This is the frame being used for indirect tree display.")
  6251. (defvar org-last-indirect-buffer nil)
  6252. (defun org-tree-to-indirect-buffer (&optional arg)
  6253. "Create indirect buffer and narrow it to current subtree.
  6254. With a numerical prefix ARG, go up to this level and then take that tree.
  6255. If ARG is negative, go up that many levels.
  6256. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6257. indirect buffer previously made with this command, to avoid proliferation of
  6258. indirect buffers. However, when you call the command with a \
  6259. \\[universal-argument] prefix, or
  6260. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6261. is kept so that you can work with several indirect buffers at the same time.
  6262. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6263. \\[universal-argument] prefix also
  6264. requests that a new frame be made for the new buffer, so that the dedicated
  6265. frame is not changed."
  6266. (interactive "P")
  6267. (let ((cbuf (current-buffer))
  6268. (cwin (selected-window))
  6269. (pos (point))
  6270. beg end level heading ibuf)
  6271. (save-excursion
  6272. (org-back-to-heading t)
  6273. (when (numberp arg)
  6274. (setq level (org-outline-level))
  6275. (if (< arg 0) (setq arg (+ level arg)))
  6276. (while (> (setq level (org-outline-level)) arg)
  6277. (org-up-heading-safe)))
  6278. (setq beg (point)
  6279. heading (org-get-heading))
  6280. (org-end-of-subtree t t)
  6281. (if (org-at-heading-p) (backward-char 1))
  6282. (setq end (point)))
  6283. (if (and (buffer-live-p org-last-indirect-buffer)
  6284. (not (eq org-indirect-buffer-display 'new-frame))
  6285. (not arg))
  6286. (kill-buffer org-last-indirect-buffer))
  6287. (setq ibuf (org-get-indirect-buffer cbuf)
  6288. org-last-indirect-buffer ibuf)
  6289. (cond
  6290. ((or (eq org-indirect-buffer-display 'new-frame)
  6291. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6292. (select-frame (make-frame))
  6293. (delete-other-windows)
  6294. (org-pop-to-buffer-same-window ibuf)
  6295. (org-set-frame-title heading))
  6296. ((eq org-indirect-buffer-display 'dedicated-frame)
  6297. (raise-frame
  6298. (select-frame (or (and org-indirect-dedicated-frame
  6299. (frame-live-p org-indirect-dedicated-frame)
  6300. org-indirect-dedicated-frame)
  6301. (setq org-indirect-dedicated-frame (make-frame)))))
  6302. (delete-other-windows)
  6303. (org-pop-to-buffer-same-window ibuf)
  6304. (org-set-frame-title (concat "Indirect: " heading)))
  6305. ((eq org-indirect-buffer-display 'current-window)
  6306. (org-pop-to-buffer-same-window ibuf))
  6307. ((eq org-indirect-buffer-display 'other-window)
  6308. (pop-to-buffer ibuf))
  6309. (t (error "Invalid value")))
  6310. (if (featurep 'xemacs)
  6311. (save-excursion (org-mode) (turn-on-font-lock)))
  6312. (narrow-to-region beg end)
  6313. (show-all)
  6314. (goto-char pos)
  6315. (run-hook-with-args 'org-cycle-hook 'all)
  6316. (and (window-live-p cwin) (select-window cwin))))
  6317. (defun org-get-indirect-buffer (&optional buffer)
  6318. (setq buffer (or buffer (current-buffer)))
  6319. (let ((n 1) (base (buffer-name buffer)) bname)
  6320. (while (buffer-live-p
  6321. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6322. (setq n (1+ n)))
  6323. (condition-case nil
  6324. (make-indirect-buffer buffer bname 'clone)
  6325. (error (make-indirect-buffer buffer bname)))))
  6326. (defun org-set-frame-title (title)
  6327. "Set the title of the current frame to the string TITLE."
  6328. ;; FIXME: how to name a single frame in XEmacs???
  6329. (unless (featurep 'xemacs)
  6330. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6331. ;;;; Structure editing
  6332. ;;; Inserting headlines
  6333. (defun org-previous-line-empty-p ()
  6334. (save-excursion
  6335. (and (not (bobp))
  6336. (or (beginning-of-line 0) t)
  6337. (save-match-data
  6338. (looking-at "[ \t]*$")))))
  6339. (defun org-insert-heading (&optional force-heading invisible-ok)
  6340. "Insert a new heading or item with same depth at point.
  6341. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6342. If point is at the beginning of a headline, insert a sibling before the
  6343. current headline. If point is not at the beginning, split the line,
  6344. create the new headline with the text in the current line after point
  6345. \(but see also the variable `org-M-RET-may-split-line').
  6346. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6347. This is important for non-interactive uses of the command."
  6348. (interactive "P")
  6349. (if (or (= (buffer-size) 0)
  6350. (and (not (save-excursion
  6351. (and (ignore-errors (org-back-to-heading invisible-ok))
  6352. (org-at-heading-p))))
  6353. (or force-heading (not (org-in-item-p)))))
  6354. (progn
  6355. (insert "\n* ")
  6356. (run-hooks 'org-insert-heading-hook))
  6357. (when (or force-heading (not (org-insert-item)))
  6358. (let* ((empty-line-p nil)
  6359. (level nil)
  6360. (on-heading (org-at-heading-p))
  6361. (head (save-excursion
  6362. (condition-case nil
  6363. (progn
  6364. (org-back-to-heading invisible-ok)
  6365. (when (and (not on-heading)
  6366. (featurep 'org-inlinetask)
  6367. (integerp org-inlinetask-min-level)
  6368. (>= (length (match-string 0))
  6369. org-inlinetask-min-level))
  6370. ;; Find a heading level before the inline task
  6371. (while (and (setq level (org-up-heading-safe))
  6372. (>= level org-inlinetask-min-level)))
  6373. (if (org-at-heading-p)
  6374. (org-back-to-heading invisible-ok)
  6375. (error "This should not happen")))
  6376. (setq empty-line-p (org-previous-line-empty-p))
  6377. (match-string 0))
  6378. (error "*"))))
  6379. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6380. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6381. pos hide-previous previous-pos)
  6382. (cond
  6383. ((and (org-at-heading-p) (bolp)
  6384. (or (bobp)
  6385. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6386. ;; insert before the current line
  6387. (open-line (if blank 2 1)))
  6388. ((and (bolp)
  6389. (not org-insert-heading-respect-content)
  6390. (or (bobp)
  6391. (save-excursion
  6392. (backward-char 1) (not (outline-invisible-p)))))
  6393. ;; insert right here
  6394. nil)
  6395. (t
  6396. ;; somewhere in the line
  6397. (save-excursion
  6398. (setq previous-pos (point-at-bol))
  6399. (end-of-line)
  6400. (setq hide-previous (outline-invisible-p)))
  6401. (and org-insert-heading-respect-content (org-show-subtree))
  6402. (let ((split
  6403. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6404. (save-excursion
  6405. (let ((p (point)))
  6406. (goto-char (point-at-bol))
  6407. (and (looking-at org-complex-heading-regexp)
  6408. (match-beginning 4)
  6409. (> p (match-beginning 4)))))))
  6410. tags pos)
  6411. (cond
  6412. (org-insert-heading-respect-content
  6413. (org-end-of-subtree nil t)
  6414. (when (featurep 'org-inlinetask)
  6415. (while (and (not (eobp))
  6416. (looking-at "\\(\\*+\\)[ \t]+")
  6417. (>= (length (match-string 1))
  6418. org-inlinetask-min-level))
  6419. (org-end-of-subtree nil t)))
  6420. (or (bolp) (newline))
  6421. (or (org-previous-line-empty-p)
  6422. (and blank (newline)))
  6423. (open-line 1))
  6424. ((org-at-heading-p)
  6425. (when hide-previous
  6426. (show-children)
  6427. (org-show-entry))
  6428. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6429. (setq tags (and (match-end 2) (match-string 2)))
  6430. (and (match-end 1)
  6431. (delete-region (match-beginning 1) (match-end 1)))
  6432. (setq pos (point-at-bol))
  6433. (or split (end-of-line 1))
  6434. (delete-horizontal-space)
  6435. (if (string-match "\\`\\*+\\'"
  6436. (buffer-substring (point-at-bol) (point)))
  6437. (insert " "))
  6438. (newline (if blank 2 1))
  6439. (when tags
  6440. (save-excursion
  6441. (goto-char pos)
  6442. (end-of-line 1)
  6443. (insert " " tags)
  6444. (org-set-tags nil 'align))))
  6445. (t
  6446. (or split (end-of-line 1))
  6447. (newline (if blank 2 1)))))))
  6448. (insert head) (just-one-space)
  6449. (setq pos (point))
  6450. (end-of-line 1)
  6451. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6452. (when (and org-insert-heading-respect-content hide-previous)
  6453. (save-excursion
  6454. (goto-char previous-pos)
  6455. (hide-subtree)))
  6456. (run-hooks 'org-insert-heading-hook)))))
  6457. (defun org-get-heading (&optional no-tags no-todo)
  6458. "Return the heading of the current entry, without the stars.
  6459. When NO-TAGS is non-nil, don't include tags.
  6460. When NO-TODO is non-nil, don't include TODO keywords."
  6461. (save-excursion
  6462. (org-back-to-heading t)
  6463. (cond
  6464. ((and no-tags no-todo)
  6465. (looking-at org-complex-heading-regexp)
  6466. (match-string 4))
  6467. (no-tags
  6468. (looking-at (concat org-outline-regexp
  6469. "\\(.*?\\)"
  6470. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6471. (match-string 1))
  6472. (no-todo
  6473. (looking-at org-todo-line-regexp)
  6474. (match-string 3))
  6475. (t (looking-at org-heading-regexp)
  6476. (match-string 2)))))
  6477. (defun org-heading-components ()
  6478. "Return the components of the current heading.
  6479. This is a list with the following elements:
  6480. - the level as an integer
  6481. - the reduced level, different if `org-odd-levels-only' is set.
  6482. - the TODO keyword, or nil
  6483. - the priority character, like ?A, or nil if no priority is given
  6484. - the headline text itself, or the tags string if no headline text
  6485. - the tags string, or nil."
  6486. (save-excursion
  6487. (org-back-to-heading t)
  6488. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6489. (list (length (match-string 1))
  6490. (org-reduced-level (length (match-string 1)))
  6491. (org-match-string-no-properties 2)
  6492. (and (match-end 3) (aref (match-string 3) 2))
  6493. (org-match-string-no-properties 4)
  6494. (org-match-string-no-properties 5)))))
  6495. (defun org-get-entry ()
  6496. "Get the entry text, after heading, entire subtree."
  6497. (save-excursion
  6498. (org-back-to-heading t)
  6499. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6500. (defun org-insert-heading-after-current ()
  6501. "Insert a new heading with same level as current, after current subtree."
  6502. (interactive)
  6503. (org-back-to-heading)
  6504. (org-insert-heading)
  6505. (org-move-subtree-down)
  6506. (end-of-line 1))
  6507. (defun org-insert-heading-respect-content ()
  6508. (interactive)
  6509. (let ((org-insert-heading-respect-content t))
  6510. (org-insert-heading t)))
  6511. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6512. (interactive "P")
  6513. (let ((org-insert-heading-respect-content t))
  6514. (org-insert-todo-heading force-state t)))
  6515. (defun org-insert-todo-heading (arg &optional force-heading)
  6516. "Insert a new heading with the same level and TODO state as current heading.
  6517. If the heading has no TODO state, or if the state is DONE, use the first
  6518. state (TODO by default). Also with prefix arg, force first state."
  6519. (interactive "P")
  6520. (when (or force-heading (not (org-insert-item 'checkbox)))
  6521. (org-insert-heading force-heading)
  6522. (save-excursion
  6523. (org-back-to-heading)
  6524. (outline-previous-heading)
  6525. (looking-at org-todo-line-regexp))
  6526. (let*
  6527. ((new-mark-x
  6528. (if (or arg
  6529. (not (match-beginning 2))
  6530. (member (match-string 2) org-done-keywords))
  6531. (car org-todo-keywords-1)
  6532. (match-string 2)))
  6533. (new-mark
  6534. (or
  6535. (run-hook-with-args-until-success
  6536. 'org-todo-get-default-hook new-mark-x nil)
  6537. new-mark-x)))
  6538. (beginning-of-line 1)
  6539. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6540. (if org-treat-insert-todo-heading-as-state-change
  6541. (org-todo new-mark)
  6542. (insert new-mark " "))))
  6543. (when org-provide-todo-statistics
  6544. (org-update-parent-todo-statistics))))
  6545. (defun org-insert-subheading (arg)
  6546. "Insert a new subheading and demote it.
  6547. Works for outline headings and for plain lists alike."
  6548. (interactive "P")
  6549. (org-insert-heading arg)
  6550. (cond
  6551. ((org-at-heading-p) (org-do-demote))
  6552. ((org-at-item-p) (org-indent-item))))
  6553. (defun org-insert-todo-subheading (arg)
  6554. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6555. Works for outline headings and for plain lists alike."
  6556. (interactive "P")
  6557. (org-insert-todo-heading arg)
  6558. (cond
  6559. ((org-at-heading-p) (org-do-demote))
  6560. ((org-at-item-p) (org-indent-item))))
  6561. ;;; Promotion and Demotion
  6562. (defvar org-after-demote-entry-hook nil
  6563. "Hook run after an entry has been demoted.
  6564. The cursor will be at the beginning of the entry.
  6565. When a subtree is being demoted, the hook will be called for each node.")
  6566. (defvar org-after-promote-entry-hook nil
  6567. "Hook run after an entry has been promoted.
  6568. The cursor will be at the beginning of the entry.
  6569. When a subtree is being promoted, the hook will be called for each node.")
  6570. (defun org-promote-subtree ()
  6571. "Promote the entire subtree.
  6572. See also `org-promote'."
  6573. (interactive)
  6574. (save-excursion
  6575. (org-with-limited-levels (org-map-tree 'org-promote)))
  6576. (org-fix-position-after-promote))
  6577. (defun org-demote-subtree ()
  6578. "Demote the entire subtree. See `org-demote'.
  6579. See also `org-promote'."
  6580. (interactive)
  6581. (save-excursion
  6582. (org-with-limited-levels (org-map-tree 'org-demote)))
  6583. (org-fix-position-after-promote))
  6584. (defun org-do-promote ()
  6585. "Promote the current heading higher up the tree.
  6586. If the region is active in `transient-mark-mode', promote all headings
  6587. in the region."
  6588. (interactive)
  6589. (save-excursion
  6590. (if (org-region-active-p)
  6591. (org-map-region 'org-promote (region-beginning) (region-end))
  6592. (org-promote)))
  6593. (org-fix-position-after-promote))
  6594. (defun org-do-demote ()
  6595. "Demote the current heading lower down the tree.
  6596. If the region is active in `transient-mark-mode', demote all headings
  6597. in the region."
  6598. (interactive)
  6599. (save-excursion
  6600. (if (org-region-active-p)
  6601. (org-map-region 'org-demote (region-beginning) (region-end))
  6602. (org-demote)))
  6603. (org-fix-position-after-promote))
  6604. (defun org-fix-position-after-promote ()
  6605. "Make sure that after pro/demotion cursor position is right."
  6606. (let ((pos (point)))
  6607. (when (save-excursion
  6608. (beginning-of-line 1)
  6609. (looking-at org-todo-line-regexp)
  6610. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6611. (cond ((eobp) (insert " "))
  6612. ((eolp) (insert " "))
  6613. ((equal (char-after) ?\ ) (forward-char 1))))))
  6614. (defun org-current-level ()
  6615. "Return the level of the current entry, or nil if before the first headline.
  6616. The level is the number of stars at the beginning of the headline."
  6617. (save-excursion
  6618. (org-with-limited-levels
  6619. (if (ignore-errors (org-back-to-heading t))
  6620. (funcall outline-level)))))
  6621. (defun org-get-previous-line-level ()
  6622. "Return the outline depth of the last headline before the current line.
  6623. Returns 0 for the first headline in the buffer, and nil if before the
  6624. first headline."
  6625. (let ((current-level (org-current-level))
  6626. (prev-level (when (> (line-number-at-pos) 1)
  6627. (save-excursion
  6628. (beginning-of-line 0)
  6629. (org-current-level)))))
  6630. (cond ((null current-level) nil) ; Before first headline
  6631. ((null prev-level) 0) ; At first headline
  6632. (prev-level))))
  6633. (defun org-reduced-level (l)
  6634. "Compute the effective level of a heading.
  6635. This takes into account the setting of `org-odd-levels-only'."
  6636. (cond
  6637. ((zerop l) 0)
  6638. (org-odd-levels-only (1+ (floor (/ l 2))))
  6639. (t l)))
  6640. (defun org-level-increment ()
  6641. "Return the number of stars that will be added or removed at a
  6642. time to headlines when structure editing, based on the value of
  6643. `org-odd-levels-only'."
  6644. (if org-odd-levels-only 2 1))
  6645. (defun org-get-valid-level (level &optional change)
  6646. "Rectify a level change under the influence of `org-odd-levels-only'
  6647. LEVEL is a current level, CHANGE is by how much the level should be
  6648. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6649. even level numbers will become the next higher odd number."
  6650. (if org-odd-levels-only
  6651. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6652. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6653. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6654. (max 1 (+ level (or change 0)))))
  6655. (if (boundp 'define-obsolete-function-alias)
  6656. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6657. (define-obsolete-function-alias 'org-get-legal-level
  6658. 'org-get-valid-level)
  6659. (define-obsolete-function-alias 'org-get-legal-level
  6660. 'org-get-valid-level "23.1")))
  6661. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6662. ;; ̀org-with-limited-levels'
  6663. (defun org-promote ()
  6664. "Promote the current heading higher up the tree.
  6665. If the region is active in `transient-mark-mode', promote all headings
  6666. in the region."
  6667. (org-back-to-heading t)
  6668. (let* ((level (save-match-data (funcall outline-level)))
  6669. (after-change-functions (remove 'flyspell-after-change-function
  6670. after-change-functions))
  6671. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6672. (diff (abs (- level (length up-head) -1))))
  6673. (cond ((and (= level 1) org-called-with-limited-levels
  6674. org-allow-promoting-top-level-subtree)
  6675. (replace-match "# " nil t))
  6676. ((= level 1)
  6677. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6678. (t (replace-match up-head nil t)))
  6679. ;; Fixup tag positioning
  6680. (unless (= level 1)
  6681. (and org-auto-align-tags (org-set-tags nil t))
  6682. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6683. (run-hooks 'org-after-promote-entry-hook)))
  6684. (defun org-demote ()
  6685. "Demote the current heading lower down the tree.
  6686. If the region is active in `transient-mark-mode', demote all headings
  6687. in the region."
  6688. (org-back-to-heading t)
  6689. (let* ((level (save-match-data (funcall outline-level)))
  6690. (after-change-functions (remove 'flyspell-after-change-function
  6691. after-change-functions))
  6692. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6693. (diff (abs (- level (length down-head) -1))))
  6694. (replace-match down-head nil t)
  6695. ;; Fixup tag positioning
  6696. (and org-auto-align-tags (org-set-tags nil t))
  6697. (if org-adapt-indentation (org-fixup-indentation diff))
  6698. (run-hooks 'org-after-demote-entry-hook)))
  6699. (defun org-cycle-level ()
  6700. "Cycle the level of an empty headline through possible states.
  6701. This goes first to child, then to parent, level, then up the hierarchy.
  6702. After top level, it switches back to sibling level."
  6703. (interactive)
  6704. (let ((org-adapt-indentation nil))
  6705. (when (org-point-at-end-of-empty-headline)
  6706. (setq this-command 'org-cycle-level) ; Only needed for caching
  6707. (let ((cur-level (org-current-level))
  6708. (prev-level (org-get-previous-line-level)))
  6709. (cond
  6710. ;; If first headline in file, promote to top-level.
  6711. ((= prev-level 0)
  6712. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6713. do (org-do-promote)))
  6714. ;; If same level as prev, demote one.
  6715. ((= prev-level cur-level)
  6716. (org-do-demote))
  6717. ;; If parent is top-level, promote to top level if not already.
  6718. ((= prev-level 1)
  6719. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6720. do (org-do-promote)))
  6721. ;; If top-level, return to prev-level.
  6722. ((= cur-level 1)
  6723. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6724. do (org-do-demote)))
  6725. ;; If less than prev-level, promote one.
  6726. ((< cur-level prev-level)
  6727. (org-do-promote))
  6728. ;; If deeper than prev-level, promote until higher than
  6729. ;; prev-level.
  6730. ((> cur-level prev-level)
  6731. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6732. do (org-do-promote))))
  6733. t))))
  6734. (defun org-map-tree (fun)
  6735. "Call FUN for every heading underneath the current one."
  6736. (org-back-to-heading)
  6737. (let ((level (funcall outline-level)))
  6738. (save-excursion
  6739. (funcall fun)
  6740. (while (and (progn
  6741. (outline-next-heading)
  6742. (> (funcall outline-level) level))
  6743. (not (eobp)))
  6744. (funcall fun)))))
  6745. (defun org-map-region (fun beg end)
  6746. "Call FUN for every heading between BEG and END."
  6747. (let ((org-ignore-region t))
  6748. (save-excursion
  6749. (setq end (copy-marker end))
  6750. (goto-char beg)
  6751. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6752. (< (point) end))
  6753. (funcall fun))
  6754. (while (and (progn
  6755. (outline-next-heading)
  6756. (< (point) end))
  6757. (not (eobp)))
  6758. (funcall fun)))))
  6759. (defvar org-property-end-re) ; silence byte-compiler
  6760. (defun org-fixup-indentation (diff)
  6761. "Change the indentation in the current entry by DIFF.
  6762. However, if any line in the current entry has no indentation, or if it
  6763. would end up with no indentation after the change, nothing at all is done."
  6764. (save-excursion
  6765. (let ((end (save-excursion (outline-next-heading)
  6766. (point-marker)))
  6767. (prohibit (if (> diff 0)
  6768. "^\\S-"
  6769. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6770. col)
  6771. (unless (save-excursion (end-of-line 1)
  6772. (re-search-forward prohibit end t))
  6773. (while (and (< (point) end)
  6774. (re-search-forward "^[ \t]+" end t))
  6775. (goto-char (match-end 0))
  6776. (setq col (current-column))
  6777. (if (< diff 0) (replace-match ""))
  6778. (org-indent-to-column (+ diff col))))
  6779. (move-marker end nil))))
  6780. (defun org-convert-to-odd-levels ()
  6781. "Convert an org-mode file with all levels allowed to one with odd levels.
  6782. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6783. level 5 etc."
  6784. (interactive)
  6785. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6786. (let ((outline-level 'org-outline-level)
  6787. (org-odd-levels-only nil) n)
  6788. (save-excursion
  6789. (goto-char (point-min))
  6790. (while (re-search-forward "^\\*\\*+ " nil t)
  6791. (setq n (- (length (match-string 0)) 2))
  6792. (while (>= (setq n (1- n)) 0)
  6793. (org-demote))
  6794. (end-of-line 1))))))
  6795. (defun org-convert-to-oddeven-levels ()
  6796. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6797. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6798. file contains a section with an even level, conversion would
  6799. destroy the structure of the file. An error is signaled in this
  6800. case."
  6801. (interactive)
  6802. (goto-char (point-min))
  6803. ;; First check if there are no even levels
  6804. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6805. (org-show-context t)
  6806. (error "Not all levels are odd in this file. Conversion not possible"))
  6807. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6808. (let ((outline-regexp org-outline-regexp)
  6809. (outline-level 'org-outline-level)
  6810. (org-odd-levels-only nil) n)
  6811. (save-excursion
  6812. (goto-char (point-min))
  6813. (while (re-search-forward "^\\*\\*+ " nil t)
  6814. (setq n (/ (1- (length (match-string 0))) 2))
  6815. (while (>= (setq n (1- n)) 0)
  6816. (org-promote))
  6817. (end-of-line 1))))))
  6818. (defun org-tr-level (n)
  6819. "Make N odd if required."
  6820. (if org-odd-levels-only (1+ (/ n 2)) n))
  6821. ;;; Vertical tree motion, cutting and pasting of subtrees
  6822. (defun org-move-subtree-up (&optional arg)
  6823. "Move the current subtree up past ARG headlines of the same level."
  6824. (interactive "p")
  6825. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6826. (defun org-move-subtree-down (&optional arg)
  6827. "Move the current subtree down past ARG headlines of the same level."
  6828. (interactive "p")
  6829. (setq arg (prefix-numeric-value arg))
  6830. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6831. 'org-get-last-sibling))
  6832. (ins-point (make-marker))
  6833. (cnt (abs arg))
  6834. (col (current-column))
  6835. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6836. ;; Select the tree
  6837. (org-back-to-heading)
  6838. (setq beg0 (point))
  6839. (save-excursion
  6840. (setq ne-beg (org-back-over-empty-lines))
  6841. (setq beg (point)))
  6842. (save-match-data
  6843. (save-excursion (outline-end-of-heading)
  6844. (setq folded (outline-invisible-p)))
  6845. (outline-end-of-subtree))
  6846. (outline-next-heading)
  6847. (setq ne-end (org-back-over-empty-lines))
  6848. (setq end (point))
  6849. (goto-char beg0)
  6850. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6851. ;; include less whitespace
  6852. (save-excursion
  6853. (goto-char beg)
  6854. (forward-line (- ne-beg ne-end))
  6855. (setq beg (point))))
  6856. ;; Find insertion point, with error handling
  6857. (while (> cnt 0)
  6858. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6859. (progn (goto-char beg0)
  6860. (error "Cannot move past superior level or buffer limit")))
  6861. (setq cnt (1- cnt)))
  6862. (if (> arg 0)
  6863. ;; Moving forward - still need to move over subtree
  6864. (progn (org-end-of-subtree t t)
  6865. (save-excursion
  6866. (org-back-over-empty-lines)
  6867. (or (bolp) (newline)))))
  6868. (setq ne-ins (org-back-over-empty-lines))
  6869. (move-marker ins-point (point))
  6870. (setq txt (buffer-substring beg end))
  6871. (org-save-markers-in-region beg end)
  6872. (delete-region beg end)
  6873. (org-remove-empty-overlays-at beg)
  6874. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6875. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6876. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6877. (let ((bbb (point)))
  6878. (insert-before-markers txt)
  6879. (org-reinstall-markers-in-region bbb)
  6880. (move-marker ins-point bbb))
  6881. (or (bolp) (insert "\n"))
  6882. (setq ins-end (point))
  6883. (goto-char ins-point)
  6884. (org-skip-whitespace)
  6885. (when (and (< arg 0)
  6886. (org-first-sibling-p)
  6887. (> ne-ins ne-beg))
  6888. ;; Move whitespace back to beginning
  6889. (save-excursion
  6890. (goto-char ins-end)
  6891. (let ((kill-whole-line t))
  6892. (kill-line (- ne-ins ne-beg)) (point)))
  6893. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6894. (move-marker ins-point nil)
  6895. (if folded
  6896. (hide-subtree)
  6897. (org-show-entry)
  6898. (show-children)
  6899. (org-cycle-hide-drawers 'children))
  6900. (org-clean-visibility-after-subtree-move)
  6901. ;; move back to the initial column we were at
  6902. (move-to-column col)))
  6903. (defvar org-subtree-clip ""
  6904. "Clipboard for cut and paste of subtrees.
  6905. This is actually only a copy of the kill, because we use the normal kill
  6906. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6907. (defvar org-subtree-clip-folded nil
  6908. "Was the last copied subtree folded?
  6909. This is used to fold the tree back after pasting.")
  6910. (defun org-cut-subtree (&optional n)
  6911. "Cut the current subtree into the clipboard.
  6912. With prefix arg N, cut this many sequential subtrees.
  6913. This is a short-hand for marking the subtree and then cutting it."
  6914. (interactive "p")
  6915. (org-copy-subtree n 'cut))
  6916. (defun org-copy-subtree (&optional n cut force-store-markers)
  6917. "Cut the current subtree into the clipboard.
  6918. With prefix arg N, cut this many sequential subtrees.
  6919. This is a short-hand for marking the subtree and then copying it.
  6920. If CUT is non-nil, actually cut the subtree.
  6921. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6922. of some markers in the region, even if CUT is non-nil. This is
  6923. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6924. (interactive "p")
  6925. (let (beg end folded (beg0 (point)))
  6926. (if (org-called-interactively-p 'any)
  6927. (org-back-to-heading nil) ; take what looks like a subtree
  6928. (org-back-to-heading t)) ; take what is really there
  6929. (org-back-over-empty-lines)
  6930. (setq beg (point))
  6931. (skip-chars-forward " \t\r\n")
  6932. (save-match-data
  6933. (save-excursion (outline-end-of-heading)
  6934. (setq folded (outline-invisible-p)))
  6935. (condition-case nil
  6936. (org-forward-heading-same-level (1- n) t)
  6937. (error nil))
  6938. (org-end-of-subtree t t))
  6939. (org-back-over-empty-lines)
  6940. (setq end (point))
  6941. (goto-char beg0)
  6942. (when (> end beg)
  6943. (setq org-subtree-clip-folded folded)
  6944. (when (or cut force-store-markers)
  6945. (org-save-markers-in-region beg end))
  6946. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6947. (setq org-subtree-clip (current-kill 0))
  6948. (message "%s: Subtree(s) with %d characters"
  6949. (if cut "Cut" "Copied")
  6950. (length org-subtree-clip)))))
  6951. (defun org-paste-subtree (&optional level tree for-yank)
  6952. "Paste the clipboard as a subtree, with modification of headline level.
  6953. The entire subtree is promoted or demoted in order to match a new headline
  6954. level.
  6955. If the cursor is at the beginning of a headline, the same level as
  6956. that headline is used to paste the tree
  6957. If not, the new level is derived from the *visible* headings
  6958. before and after the insertion point, and taken to be the inferior headline
  6959. level of the two. So if the previous visible heading is level 3 and the
  6960. next is level 4 (or vice versa), level 4 will be used for insertion.
  6961. This makes sure that the subtree remains an independent subtree and does
  6962. not swallow low level entries.
  6963. You can also force a different level, either by using a numeric prefix
  6964. argument, or by inserting the heading marker by hand. For example, if the
  6965. cursor is after \"*****\", then the tree will be shifted to level 5.
  6966. If optional TREE is given, use this text instead of the kill ring.
  6967. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6968. move back over whitespace before inserting, and move point to the end of
  6969. the inserted text when done."
  6970. (interactive "P")
  6971. (setq tree (or tree (and kill-ring (current-kill 0))))
  6972. (unless (org-kill-is-subtree-p tree)
  6973. (error "%s"
  6974. (substitute-command-keys
  6975. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6976. (org-with-limited-levels
  6977. (let* ((visp (not (outline-invisible-p)))
  6978. (txt tree)
  6979. (^re_ "\\(\\*+\\)[ \t]*")
  6980. (old-level (if (string-match org-outline-regexp-bol txt)
  6981. (- (match-end 0) (match-beginning 0) 1)
  6982. -1))
  6983. (force-level (cond (level (prefix-numeric-value level))
  6984. ((and (looking-at "[ \t]*$")
  6985. (string-match
  6986. "^\\*+$" (buffer-substring
  6987. (point-at-bol) (point))))
  6988. (- (match-end 1) (match-beginning 1)))
  6989. ((and (bolp)
  6990. (looking-at org-outline-regexp))
  6991. (- (match-end 0) (point) 1))))
  6992. (previous-level (save-excursion
  6993. (condition-case nil
  6994. (progn
  6995. (outline-previous-visible-heading 1)
  6996. (if (looking-at ^re_)
  6997. (- (match-end 0) (match-beginning 0) 1)
  6998. 1))
  6999. (error 1))))
  7000. (next-level (save-excursion
  7001. (condition-case nil
  7002. (progn
  7003. (or (looking-at org-outline-regexp)
  7004. (outline-next-visible-heading 1))
  7005. (if (looking-at ^re_)
  7006. (- (match-end 0) (match-beginning 0) 1)
  7007. 1))
  7008. (error 1))))
  7009. (new-level (or force-level (max previous-level next-level)))
  7010. (shift (if (or (= old-level -1)
  7011. (= new-level -1)
  7012. (= old-level new-level))
  7013. 0
  7014. (- new-level old-level)))
  7015. (delta (if (> shift 0) -1 1))
  7016. (func (if (> shift 0) 'org-demote 'org-promote))
  7017. (org-odd-levels-only nil)
  7018. beg end newend)
  7019. ;; Remove the forced level indicator
  7020. (if force-level
  7021. (delete-region (point-at-bol) (point)))
  7022. ;; Paste
  7023. (beginning-of-line (if (bolp) 1 2))
  7024. (unless for-yank (org-back-over-empty-lines))
  7025. (setq beg (point))
  7026. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7027. (insert-before-markers txt)
  7028. (unless (string-match "\n\\'" txt) (insert "\n"))
  7029. (setq newend (point))
  7030. (org-reinstall-markers-in-region beg)
  7031. (setq end (point))
  7032. (goto-char beg)
  7033. (skip-chars-forward " \t\n\r")
  7034. (setq beg (point))
  7035. (if (and (outline-invisible-p) visp)
  7036. (save-excursion (outline-show-heading)))
  7037. ;; Shift if necessary
  7038. (unless (= shift 0)
  7039. (save-restriction
  7040. (narrow-to-region beg end)
  7041. (while (not (= shift 0))
  7042. (org-map-region func (point-min) (point-max))
  7043. (setq shift (+ delta shift)))
  7044. (goto-char (point-min))
  7045. (setq newend (point-max))))
  7046. (when (or (org-called-interactively-p 'interactive) for-yank)
  7047. (message "Clipboard pasted as level %d subtree" new-level))
  7048. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7049. kill-ring
  7050. (eq org-subtree-clip (current-kill 0))
  7051. org-subtree-clip-folded)
  7052. ;; The tree was folded before it was killed/copied
  7053. (hide-subtree))
  7054. (and for-yank (goto-char newend)))))
  7055. (defun org-kill-is-subtree-p (&optional txt)
  7056. "Check if the current kill is an outline subtree, or a set of trees.
  7057. Returns nil if kill does not start with a headline, or if the first
  7058. headline level is not the largest headline level in the tree.
  7059. So this will actually accept several entries of equal levels as well,
  7060. which is OK for `org-paste-subtree'.
  7061. If optional TXT is given, check this string instead of the current kill."
  7062. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7063. (re (org-get-limited-outline-regexp))
  7064. (^re (concat "^" re))
  7065. (start-level (and kill
  7066. (string-match
  7067. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7068. kill)
  7069. (- (match-end 2) (match-beginning 2) 1)))
  7070. (start (1+ (or (match-beginning 2) -1))))
  7071. (if (not start-level)
  7072. (progn
  7073. nil) ;; does not even start with a heading
  7074. (catch 'exit
  7075. (while (setq start (string-match ^re kill (1+ start)))
  7076. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7077. (throw 'exit nil)))
  7078. t))))
  7079. (defvar org-markers-to-move nil
  7080. "Markers that should be moved with a cut-and-paste operation.
  7081. Those markers are stored together with their positions relative to
  7082. the start of the region.")
  7083. (defun org-save-markers-in-region (beg end)
  7084. "Check markers in region.
  7085. If these markers are between BEG and END, record their position relative
  7086. to BEG, so that after moving the block of text, we can put the markers back
  7087. into place.
  7088. This function gets called just before an entry or tree gets cut from the
  7089. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7090. called immediately, to move the markers with the entries."
  7091. (setq org-markers-to-move nil)
  7092. (when (featurep 'org-clock)
  7093. (org-clock-save-markers-for-cut-and-paste beg end))
  7094. (when (featurep 'org-agenda)
  7095. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7096. (defun org-check-and-save-marker (marker beg end)
  7097. "Check if MARKER is between BEG and END.
  7098. If yes, remember the marker and the distance to BEG."
  7099. (when (and (marker-buffer marker)
  7100. (equal (marker-buffer marker) (current-buffer)))
  7101. (if (and (>= marker beg) (< marker end))
  7102. (push (cons marker (- marker beg)) org-markers-to-move))))
  7103. (defun org-reinstall-markers-in-region (beg)
  7104. "Move all remembered markers to their position relative to BEG."
  7105. (mapc (lambda (x)
  7106. (move-marker (car x) (+ beg (cdr x))))
  7107. org-markers-to-move)
  7108. (setq org-markers-to-move nil))
  7109. (defun org-narrow-to-subtree ()
  7110. "Narrow buffer to the current subtree."
  7111. (interactive)
  7112. (save-excursion
  7113. (save-match-data
  7114. (org-with-limited-levels
  7115. (narrow-to-region
  7116. (progn (org-back-to-heading t) (point))
  7117. (progn (org-end-of-subtree t t)
  7118. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7119. (point)))))))
  7120. (defun org-narrow-to-block ()
  7121. "Narrow buffer to the current block."
  7122. (interactive)
  7123. (let* ((case-fold-search t)
  7124. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7125. "^[ \t]*#\\+end_.*")))
  7126. (if blockp
  7127. (narrow-to-region (car blockp) (cdr blockp))
  7128. (error "Not in a block"))))
  7129. (eval-when-compile
  7130. (defvar org-property-drawer-re))
  7131. (defvar org-property-start-re) ;; defined below
  7132. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7133. "Clone the task (subtree) at point N times.
  7134. The clones will be inserted as siblings.
  7135. In interactive use, the user will be prompted for the number of
  7136. clones to be produced, and for a time SHIFT, which may be a
  7137. repeater as used in time stamps, for example `+3d'.
  7138. When a valid repeater is given and the entry contains any time
  7139. stamps, the clones will become a sequence in time, with time
  7140. stamps in the subtree shifted for each clone produced. If SHIFT
  7141. is nil or the empty string, time stamps will be left alone. The
  7142. ID property of the original subtree is removed.
  7143. If the original subtree did contain time stamps with a repeater,
  7144. the following will happen:
  7145. - the repeater will be removed in each clone
  7146. - an additional clone will be produced, with the current, unshifted
  7147. date(s) in the entry.
  7148. - the original entry will be placed *after* all the clones, with
  7149. repeater intact.
  7150. - the start days in the repeater in the original entry will be shifted
  7151. to past the last clone.
  7152. In this way you can spell out a number of instances of a repeating task,
  7153. and still retain the repeater to cover future instances of the task."
  7154. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7155. (let (beg end template task idprop
  7156. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7157. (drawer-re org-drawer-regexp))
  7158. (if (not (and (integerp n) (> n 0)))
  7159. (error "Invalid number of replications %s" n))
  7160. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7161. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7162. shift)))
  7163. (error "Invalid shift specification %s" shift))
  7164. (when doshift
  7165. (setq shift-n (string-to-number (match-string 1 shift))
  7166. shift-what (cdr (assoc (match-string 2 shift)
  7167. '(("d" . day) ("w" . week)
  7168. ("m" . month) ("y" . year))))))
  7169. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7170. (setq nmin 1 nmax n)
  7171. (org-back-to-heading t)
  7172. (setq beg (point))
  7173. (setq idprop (org-entry-get nil "ID"))
  7174. (org-end-of-subtree t t)
  7175. (or (bolp) (insert "\n"))
  7176. (setq end (point))
  7177. (setq template (buffer-substring beg end))
  7178. (when (and doshift
  7179. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7180. (delete-region beg end)
  7181. (setq end beg)
  7182. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7183. (goto-char end)
  7184. (loop for n from nmin to nmax do
  7185. ;; prepare clone
  7186. (with-temp-buffer
  7187. (insert template)
  7188. (org-mode)
  7189. (goto-char (point-min))
  7190. (org-show-subtree)
  7191. (and idprop (if org-clone-delete-id
  7192. (org-entry-delete nil "ID")
  7193. (org-id-get-create t)))
  7194. (unless (= n 0)
  7195. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7196. (kill-whole-line))
  7197. (goto-char (point-min))
  7198. (while (re-search-forward drawer-re nil t)
  7199. (mapc (lambda (d)
  7200. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7201. (goto-char (point-min))
  7202. (when doshift
  7203. (while (re-search-forward org-ts-regexp-both nil t)
  7204. (org-timestamp-change (* n shift-n) shift-what))
  7205. (unless (= n n-no-remove)
  7206. (goto-char (point-min))
  7207. (while (re-search-forward org-ts-regexp nil t)
  7208. (save-excursion
  7209. (goto-char (match-beginning 0))
  7210. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7211. (delete-region (match-beginning 1) (match-end 1)))))))
  7212. (setq task (buffer-string)))
  7213. (insert task))
  7214. (goto-char beg)))
  7215. ;;; Outline Sorting
  7216. (defun org-sort (with-case)
  7217. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7218. Optional argument WITH-CASE means sort case-sensitively."
  7219. (interactive "P")
  7220. (cond
  7221. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7222. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7223. (t
  7224. (org-call-with-arg 'org-sort-entries with-case))))
  7225. (defun org-sort-remove-invisible (s)
  7226. (remove-text-properties 0 (length s) org-rm-props s)
  7227. (while (string-match org-bracket-link-regexp s)
  7228. (setq s (replace-match (if (match-end 2)
  7229. (match-string 3 s)
  7230. (match-string 1 s)) t t s)))
  7231. s)
  7232. (defvar org-priority-regexp) ; defined later in the file
  7233. (defvar org-after-sorting-entries-or-items-hook nil
  7234. "Hook that is run after a bunch of entries or items have been sorted.
  7235. When children are sorted, the cursor is in the parent line when this
  7236. hook gets called. When a region or a plain list is sorted, the cursor
  7237. will be in the first entry of the sorted region/list.")
  7238. (defun org-sort-entries
  7239. (&optional with-case sorting-type getkey-func compare-func property)
  7240. "Sort entries on a certain level of an outline tree.
  7241. If there is an active region, the entries in the region are sorted.
  7242. Else, if the cursor is before the first entry, sort the top-level items.
  7243. Else, the children of the entry at point are sorted.
  7244. Sorting can be alphabetically, numerically, by date/time as given by
  7245. a time stamp, by a property or by priority.
  7246. The command prompts for the sorting type unless it has been given to the
  7247. function through the SORTING-TYPE argument, which needs to be a character,
  7248. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7249. precise meaning of each character:
  7250. n Numerically, by converting the beginning of the entry/item to a number.
  7251. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7252. t By date/time, either the first active time stamp in the entry, or, if
  7253. none exist, by the first inactive one.
  7254. s By the scheduled date/time.
  7255. d By deadline date/time.
  7256. c By creation time, which is assumed to be the first inactive time stamp
  7257. at the beginning of a line.
  7258. p By priority according to the cookie.
  7259. r By the value of a property.
  7260. Capital letters will reverse the sort order.
  7261. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7262. called with point at the beginning of the record. It must return either
  7263. a string or a number that should serve as the sorting key for that record.
  7264. Comparing entries ignores case by default. However, with an optional argument
  7265. WITH-CASE, the sorting considers case as well."
  7266. (interactive "P")
  7267. (let ((case-func (if with-case 'identity 'downcase))
  7268. start beg end stars re re2
  7269. txt what tmp)
  7270. ;; Find beginning and end of region to sort
  7271. (cond
  7272. ((org-region-active-p)
  7273. ;; we will sort the region
  7274. (setq end (region-end)
  7275. what "region")
  7276. (goto-char (region-beginning))
  7277. (if (not (org-at-heading-p)) (outline-next-heading))
  7278. (setq start (point)))
  7279. ((or (org-at-heading-p)
  7280. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7281. ;; we will sort the children of the current headline
  7282. (org-back-to-heading)
  7283. (setq start (point)
  7284. end (progn (org-end-of-subtree t t)
  7285. (or (bolp) (insert "\n"))
  7286. (org-back-over-empty-lines)
  7287. (point))
  7288. what "children")
  7289. (goto-char start)
  7290. (show-subtree)
  7291. (outline-next-heading))
  7292. (t
  7293. ;; we will sort the top-level entries in this file
  7294. (goto-char (point-min))
  7295. (or (org-at-heading-p) (outline-next-heading))
  7296. (setq start (point))
  7297. (goto-char (point-max))
  7298. (beginning-of-line 1)
  7299. (when (looking-at ".*?\\S-")
  7300. ;; File ends in a non-white line
  7301. (end-of-line 1)
  7302. (insert "\n"))
  7303. (setq end (point-max))
  7304. (setq what "top-level")
  7305. (goto-char start)
  7306. (show-all)))
  7307. (setq beg (point))
  7308. (if (>= beg end) (error "Nothing to sort"))
  7309. (looking-at "\\(\\*+\\)")
  7310. (setq stars (match-string 1)
  7311. re (concat "^" (regexp-quote stars) " +")
  7312. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7313. txt (buffer-substring beg end))
  7314. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7315. (if (and (not (equal stars "*")) (string-match re2 txt))
  7316. (error "Region to sort contains a level above the first entry"))
  7317. (unless sorting-type
  7318. (message
  7319. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7320. [t]ime [s]cheduled [d]eadline [c]reated
  7321. A/N/T/S/D/C/P/O/F means reversed:"
  7322. what)
  7323. (setq sorting-type (read-char-exclusive))
  7324. (unless getkey-func
  7325. (and (= (downcase sorting-type) ?f)
  7326. (setq getkey-func
  7327. (org-icompleting-read "Sort using function: "
  7328. obarray 'fboundp t nil nil))
  7329. (setq getkey-func (intern getkey-func))))
  7330. (and (= (downcase sorting-type) ?r)
  7331. (not property)
  7332. (setq property
  7333. (org-icompleting-read "Property: "
  7334. (mapcar 'list (org-buffer-property-keys t))
  7335. nil t))))
  7336. (message "Sorting entries...")
  7337. (save-restriction
  7338. (narrow-to-region start end)
  7339. (let ((dcst (downcase sorting-type))
  7340. (case-fold-search nil)
  7341. (now (current-time)))
  7342. (sort-subr
  7343. (/= dcst sorting-type)
  7344. ;; This function moves to the beginning character of the "record" to
  7345. ;; be sorted.
  7346. (lambda nil
  7347. (if (re-search-forward re nil t)
  7348. (goto-char (match-beginning 0))
  7349. (goto-char (point-max))))
  7350. ;; This function moves to the last character of the "record" being
  7351. ;; sorted.
  7352. (lambda nil
  7353. (save-match-data
  7354. (condition-case nil
  7355. (outline-forward-same-level 1)
  7356. (error
  7357. (goto-char (point-max))))))
  7358. ;; This function returns the value that gets sorted against.
  7359. (lambda nil
  7360. (cond
  7361. ((= dcst ?n)
  7362. (if (looking-at org-complex-heading-regexp)
  7363. (string-to-number (match-string 4))
  7364. nil))
  7365. ((= dcst ?a)
  7366. (if (looking-at org-complex-heading-regexp)
  7367. (funcall case-func (match-string 4))
  7368. nil))
  7369. ((= dcst ?t)
  7370. (let ((end (save-excursion (outline-next-heading) (point))))
  7371. (if (or (re-search-forward org-ts-regexp end t)
  7372. (re-search-forward org-ts-regexp-both end t))
  7373. (org-time-string-to-seconds (match-string 0))
  7374. (org-float-time now))))
  7375. ((= dcst ?c)
  7376. (let ((end (save-excursion (outline-next-heading) (point))))
  7377. (if (re-search-forward
  7378. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7379. end t)
  7380. (org-time-string-to-seconds (match-string 0))
  7381. (org-float-time now))))
  7382. ((= dcst ?s)
  7383. (let ((end (save-excursion (outline-next-heading) (point))))
  7384. (if (re-search-forward org-scheduled-time-regexp end t)
  7385. (org-time-string-to-seconds (match-string 1))
  7386. (org-float-time now))))
  7387. ((= dcst ?d)
  7388. (let ((end (save-excursion (outline-next-heading) (point))))
  7389. (if (re-search-forward org-deadline-time-regexp end t)
  7390. (org-time-string-to-seconds (match-string 1))
  7391. (org-float-time now))))
  7392. ((= dcst ?p)
  7393. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7394. (string-to-char (match-string 2))
  7395. org-default-priority))
  7396. ((= dcst ?r)
  7397. (or (org-entry-get nil property) ""))
  7398. ((= dcst ?o)
  7399. (if (looking-at org-complex-heading-regexp)
  7400. (- 9999 (length (member (match-string 2)
  7401. org-todo-keywords-1)))))
  7402. ((= dcst ?f)
  7403. (if getkey-func
  7404. (progn
  7405. (setq tmp (funcall getkey-func))
  7406. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7407. tmp)
  7408. (error "Invalid key function `%s'" getkey-func)))
  7409. (t (error "Invalid sorting type `%c'" sorting-type))))
  7410. nil
  7411. (cond
  7412. ((= dcst ?a) 'string<)
  7413. ((= dcst ?f) compare-func)
  7414. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7415. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7416. (message "Sorting entries...done")))
  7417. (defun org-do-sort (table what &optional with-case sorting-type)
  7418. "Sort TABLE of WHAT according to SORTING-TYPE.
  7419. The user will be prompted for the SORTING-TYPE if the call to this
  7420. function does not specify it. WHAT is only for the prompt, to indicate
  7421. what is being sorted. The sorting key will be extracted from
  7422. the car of the elements of the table.
  7423. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7424. (unless sorting-type
  7425. (message
  7426. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7427. what)
  7428. (setq sorting-type (read-char-exclusive)))
  7429. (let ((dcst (downcase sorting-type))
  7430. extractfun comparefun)
  7431. ;; Define the appropriate functions
  7432. (cond
  7433. ((= dcst ?n)
  7434. (setq extractfun 'string-to-number
  7435. comparefun (if (= dcst sorting-type) '< '>)))
  7436. ((= dcst ?a)
  7437. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7438. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7439. comparefun (if (= dcst sorting-type)
  7440. 'string<
  7441. (lambda (a b) (and (not (string< a b))
  7442. (not (string= a b)))))))
  7443. ((= dcst ?t)
  7444. (setq extractfun
  7445. (lambda (x)
  7446. (if (or (string-match org-ts-regexp x)
  7447. (string-match org-ts-regexp-both x))
  7448. (org-float-time
  7449. (org-time-string-to-time (match-string 0 x)))
  7450. 0))
  7451. comparefun (if (= dcst sorting-type) '< '>)))
  7452. (t (error "Invalid sorting type `%c'" sorting-type)))
  7453. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7454. table)
  7455. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7456. ;;; The orgstruct minor mode
  7457. ;; Define a minor mode which can be used in other modes in order to
  7458. ;; integrate the org-mode structure editing commands.
  7459. ;; This is really a hack, because the org-mode structure commands use
  7460. ;; keys which normally belong to the major mode. Here is how it
  7461. ;; works: The minor mode defines all the keys necessary to operate the
  7462. ;; structure commands, but wraps the commands into a function which
  7463. ;; tests if the cursor is currently at a headline or a plain list
  7464. ;; item. If that is the case, the structure command is used,
  7465. ;; temporarily setting many Org-mode variables like regular
  7466. ;; expressions for filling etc. However, when any of those keys is
  7467. ;; used at a different location, function uses `key-binding' to look
  7468. ;; up if the key has an associated command in another currently active
  7469. ;; keymap (minor modes, major mode, global), and executes that
  7470. ;; command. There might be problems if any of the keys is otherwise
  7471. ;; used as a prefix key.
  7472. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7473. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7474. ;; addresses this by checking explicitly for both bindings.
  7475. (defvar orgstruct-mode-map (make-sparse-keymap)
  7476. "Keymap for the minor `orgstruct-mode'.")
  7477. (defvar org-local-vars nil
  7478. "List of local variables, for use by `orgstruct-mode'.")
  7479. ;;;###autoload
  7480. (define-minor-mode orgstruct-mode
  7481. "Toggle the minor mode `orgstruct-mode'.
  7482. This mode is for using Org-mode structure commands in other
  7483. modes. The following keys behave as if Org-mode were active, if
  7484. the cursor is on a headline, or on a plain list item (both as
  7485. defined by Org-mode).
  7486. M-up Move entry/item up
  7487. M-down Move entry/item down
  7488. M-left Promote
  7489. M-right Demote
  7490. M-S-up Move entry/item up
  7491. M-S-down Move entry/item down
  7492. M-S-left Promote subtree
  7493. M-S-right Demote subtree
  7494. M-q Fill paragraph and items like in Org-mode
  7495. C-c ^ Sort entries
  7496. C-c - Cycle list bullet
  7497. TAB Cycle item visibility
  7498. M-RET Insert new heading/item
  7499. S-M-RET Insert new TODO heading / Checkbox item
  7500. C-c C-c Set tags / toggle checkbox"
  7501. nil " OrgStruct" nil
  7502. (org-load-modules-maybe)
  7503. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7504. ;;;###autoload
  7505. (defun turn-on-orgstruct ()
  7506. "Unconditionally turn on `orgstruct-mode'."
  7507. (orgstruct-mode 1))
  7508. (defvar org-fb-vars nil)
  7509. (make-variable-buffer-local 'org-fb-vars)
  7510. (defun orgstruct++-mode (&optional arg)
  7511. "Toggle `orgstruct-mode', the enhanced version of it.
  7512. In addition to setting orgstruct-mode, this also exports all
  7513. indentation and autofilling variables from org-mode into the
  7514. buffer. It will also recognize item context in multiline items."
  7515. (interactive "P")
  7516. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7517. (if (< arg 1)
  7518. (progn (orgstruct-mode -1)
  7519. (mapc (lambda(v)
  7520. (org-set-local (car v)
  7521. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7522. org-fb-vars))
  7523. (orgstruct-mode 1)
  7524. (setq org-fb-vars nil)
  7525. (let (var val)
  7526. (mapc
  7527. (lambda (x)
  7528. (when (string-match
  7529. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7530. (symbol-name (car x)))
  7531. (setq var (car x) val (nth 1 x))
  7532. (push (list var `(quote ,(eval var))) org-fb-vars)
  7533. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7534. org-local-vars)
  7535. (org-set-local 'orgstruct-is-++ t))))
  7536. (defvar orgstruct-is-++ nil
  7537. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7538. (make-variable-buffer-local 'orgstruct-is-++)
  7539. ;;;###autoload
  7540. (defun turn-on-orgstruct++ ()
  7541. "Unconditionally turn on `orgstruct++-mode'."
  7542. (orgstruct++-mode 1))
  7543. (defun orgstruct-error ()
  7544. "Error when there is no default binding for a structure key."
  7545. (interactive)
  7546. (error "This key has no function outside structure elements"))
  7547. (defun orgstruct-setup ()
  7548. "Setup orgstruct keymaps."
  7549. (let ((nfunc 0)
  7550. (bindings
  7551. (list
  7552. '([(meta up)] org-metaup)
  7553. '([(meta down)] org-metadown)
  7554. '([(meta left)] org-metaleft)
  7555. '([(meta right)] org-metaright)
  7556. '([(meta shift up)] org-shiftmetaup)
  7557. '([(meta shift down)] org-shiftmetadown)
  7558. '([(meta shift left)] org-shiftmetaleft)
  7559. '([(meta shift right)] org-shiftmetaright)
  7560. '([?\e (up)] org-metaup)
  7561. '([?\e (down)] org-metadown)
  7562. '([?\e (left)] org-metaleft)
  7563. '([?\e (right)] org-metaright)
  7564. '([?\e (shift up)] org-shiftmetaup)
  7565. '([?\e (shift down)] org-shiftmetadown)
  7566. '([?\e (shift left)] org-shiftmetaleft)
  7567. '([?\e (shift right)] org-shiftmetaright)
  7568. '([(shift up)] org-shiftup)
  7569. '([(shift down)] org-shiftdown)
  7570. '([(shift left)] org-shiftleft)
  7571. '([(shift right)] org-shiftright)
  7572. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7573. '("\M-q" fill-paragraph)
  7574. '("\C-c^" org-sort)
  7575. '("\C-c-" org-cycle-list-bullet)))
  7576. elt key fun cmd)
  7577. (while (setq elt (pop bindings))
  7578. (setq nfunc (1+ nfunc))
  7579. (setq key (org-key (car elt))
  7580. fun (nth 1 elt)
  7581. cmd (orgstruct-make-binding fun nfunc key))
  7582. (org-defkey orgstruct-mode-map key cmd))
  7583. ;; Prevent an error for users who forgot to make autoloads
  7584. (require 'org-element)
  7585. ;; Special treatment needed for TAB and RET
  7586. (org-defkey orgstruct-mode-map [(tab)]
  7587. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7588. (org-defkey orgstruct-mode-map "\C-i"
  7589. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7590. (org-defkey orgstruct-mode-map "\M-\C-m"
  7591. (orgstruct-make-binding 'org-insert-heading 105
  7592. "\M-\C-m" [(meta return)]))
  7593. (org-defkey orgstruct-mode-map [(meta return)]
  7594. (orgstruct-make-binding 'org-insert-heading 106
  7595. [(meta return)] "\M-\C-m"))
  7596. (org-defkey orgstruct-mode-map [(shift meta return)]
  7597. (orgstruct-make-binding 'org-insert-todo-heading 107
  7598. [(meta return)] "\M-\C-m"))
  7599. (org-defkey orgstruct-mode-map "\e\C-m"
  7600. (orgstruct-make-binding 'org-insert-heading 108
  7601. "\e\C-m" [?\e (return)]))
  7602. (org-defkey orgstruct-mode-map [?\e (return)]
  7603. (orgstruct-make-binding 'org-insert-heading 109
  7604. [?\e (return)] "\e\C-m"))
  7605. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7606. (orgstruct-make-binding 'org-insert-todo-heading 110
  7607. [?\e (return)] "\e\C-m"))
  7608. (unless org-local-vars
  7609. (setq org-local-vars (org-get-local-variables)))
  7610. t))
  7611. (defun orgstruct-make-binding (fun n &rest keys)
  7612. "Create a function for binding in the structure minor mode.
  7613. FUN is the command to call inside a table. N is used to create a unique
  7614. command name. KEYS are keys that should be checked in for a command
  7615. to execute outside of tables."
  7616. (eval
  7617. (list 'defun
  7618. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7619. '(arg)
  7620. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7621. "Outside of structure, run the binding of `"
  7622. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7623. "'.")
  7624. '(interactive "p")
  7625. (list 'if
  7626. `(org-context-p 'headline 'item
  7627. (and orgstruct-is-++
  7628. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7629. 'item-body))
  7630. (list 'org-run-like-in-org-mode (list 'quote fun))
  7631. (list 'let '(orgstruct-mode)
  7632. (list 'call-interactively
  7633. (append '(or)
  7634. (mapcar (lambda (k)
  7635. (list 'key-binding k))
  7636. keys)
  7637. '('orgstruct-error))))))))
  7638. (defun org-contextualize-keys (alist contexts)
  7639. "Return valid elements in ALIST depending on CONTEXTS.
  7640. `org-agenda-custom-commands' or `org-capture-templates' are the
  7641. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7642. or `org-capture-templates-contexts' are the associated contexts
  7643. definitions."
  7644. (let ((contexts
  7645. ;; normalize contexts
  7646. (mapcar
  7647. (lambda(c) (cond ((listp (cadr c))
  7648. (list (car c) (car c) (cadr c)))
  7649. ((string= "" (cadr c))
  7650. (list (car c) (car c) (caddr c)))
  7651. (t c))) contexts))
  7652. (a alist) c r s)
  7653. ;; loop over all commands or templates
  7654. (while (setq c (pop a))
  7655. (let (vrules repl)
  7656. (cond
  7657. ((not (assoc (car c) contexts))
  7658. (push c r))
  7659. ((and (assoc (car c) contexts)
  7660. (setq vrules (org-contextualize-validate-key
  7661. (car c) contexts)))
  7662. (mapc (lambda (vr)
  7663. (when (not (equal (car vr) (cadr vr)))
  7664. (setq repl vr))) vrules)
  7665. (if (not repl) (push c r)
  7666. (push (cadr repl) s)
  7667. (push
  7668. (cons (car c)
  7669. (cdr (or (assoc (cadr repl) alist)
  7670. (error "Undefined key `%s' as contextual replacement for `%s'"
  7671. (cadr repl) (car c)))))
  7672. r))))))
  7673. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7674. (delq
  7675. nil
  7676. (delete-dups
  7677. (mapcar (lambda (x)
  7678. (let ((tpl (car x)))
  7679. (when (not (delq
  7680. nil
  7681. (mapcar (lambda(y)
  7682. (equal y tpl)) s))) x)))
  7683. (reverse r))))))
  7684. (defun org-contextualize-validate-key (key contexts)
  7685. "Check CONTEXTS for agenda or capture KEY."
  7686. (let (r rr res)
  7687. (while (setq r (pop contexts))
  7688. (mapc
  7689. (lambda (rr)
  7690. (when
  7691. (and (equal key (car r))
  7692. (if (functionp rr) (funcall rr)
  7693. (or (and (eq (car rr) 'in-file)
  7694. (buffer-file-name)
  7695. (string-match (cdr rr) (buffer-file-name)))
  7696. (and (eq (car rr) 'in-mode)
  7697. (string-match (cdr rr) (symbol-name major-mode)))
  7698. (when (and (eq (car rr) 'not-in-file)
  7699. (buffer-file-name))
  7700. (not (string-match (cdr rr) (buffer-file-name))))
  7701. (when (eq (car rr) 'not-in-mode)
  7702. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7703. (push r res)))
  7704. (car (last r))))
  7705. (delete-dups (delq nil res))))
  7706. (defun org-context-p (&rest contexts)
  7707. "Check if local context is any of CONTEXTS.
  7708. Possible values in the list of contexts are `table', `headline', and `item'."
  7709. (let ((pos (point)))
  7710. (goto-char (point-at-bol))
  7711. (prog1 (or (and (memq 'table contexts)
  7712. (looking-at "[ \t]*|"))
  7713. (and (memq 'headline contexts)
  7714. (looking-at org-outline-regexp))
  7715. (and (memq 'item contexts)
  7716. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7717. (and (memq 'item-body contexts)
  7718. (org-in-item-p)))
  7719. (goto-char pos))))
  7720. (defun org-get-local-variables ()
  7721. "Return a list of all local variables in an Org mode buffer."
  7722. (let (varlist)
  7723. (with-current-buffer (get-buffer-create "*Org tmp*")
  7724. (erase-buffer)
  7725. (org-mode)
  7726. (setq varlist (buffer-local-variables)))
  7727. (kill-buffer "*Org tmp*")
  7728. (delq nil
  7729. (mapcar
  7730. (lambda (x)
  7731. (setq x
  7732. (if (symbolp x)
  7733. (list x)
  7734. (list (car x) (list 'quote (cdr x)))))
  7735. (if (string-match
  7736. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7737. (symbol-name (car x)))
  7738. x nil))
  7739. varlist))))
  7740. (defun org-clone-local-variables (from-buffer &optional regexp)
  7741. "Clone local variables from FROM-BUFFER.
  7742. Optional argument REGEXP selects variables to clone."
  7743. (mapc
  7744. (lambda (pair)
  7745. (and (symbolp (car pair))
  7746. (or (null regexp)
  7747. (string-match regexp (symbol-name (car pair))))
  7748. (set (make-local-variable (car pair))
  7749. (cdr pair))))
  7750. (buffer-local-variables from-buffer)))
  7751. ;;;###autoload
  7752. (defun org-run-like-in-org-mode (cmd)
  7753. "Run a command, pretending that the current buffer is in Org-mode.
  7754. This will temporarily bind local variables that are typically bound in
  7755. Org-mode to the values they have in Org-mode, and then interactively
  7756. call CMD."
  7757. (org-load-modules-maybe)
  7758. (unless org-local-vars
  7759. (setq org-local-vars (org-get-local-variables)))
  7760. (eval (list 'let org-local-vars
  7761. (list 'call-interactively (list 'quote cmd)))))
  7762. ;;;; Archiving
  7763. (defun org-get-category (&optional pos force-refresh)
  7764. "Get the category applying to position POS."
  7765. (save-match-data
  7766. (if force-refresh (org-refresh-category-properties))
  7767. (let ((pos (or pos (point))))
  7768. (or (get-text-property pos 'org-category)
  7769. (progn (org-refresh-category-properties)
  7770. (get-text-property pos 'org-category))))))
  7771. (defun org-refresh-category-properties ()
  7772. "Refresh category text properties in the buffer."
  7773. (let ((case-fold-search t)
  7774. (inhibit-read-only t)
  7775. (def-cat (cond
  7776. ((null org-category)
  7777. (if buffer-file-name
  7778. (file-name-sans-extension
  7779. (file-name-nondirectory buffer-file-name))
  7780. "???"))
  7781. ((symbolp org-category) (symbol-name org-category))
  7782. (t org-category)))
  7783. beg end cat pos optionp)
  7784. (org-unmodified
  7785. (save-excursion
  7786. (save-restriction
  7787. (widen)
  7788. (goto-char (point-min))
  7789. (put-text-property (point) (point-max) 'org-category def-cat)
  7790. (while (re-search-forward
  7791. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7792. (setq pos (match-end 0)
  7793. optionp (equal (char-after (match-beginning 0)) ?#)
  7794. cat (org-trim (match-string 2)))
  7795. (if optionp
  7796. (setq beg (point-at-bol) end (point-max))
  7797. (org-back-to-heading t)
  7798. (setq beg (point) end (org-end-of-subtree t t)))
  7799. (put-text-property beg end 'org-category cat)
  7800. (put-text-property beg end 'org-category-position beg)
  7801. (goto-char pos)))))))
  7802. ;;;; Link Stuff
  7803. ;;; Link abbreviations
  7804. (defun org-link-expand-abbrev (link)
  7805. "Apply replacements as defined in `org-link-abbrev-alist'."
  7806. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7807. (let* ((key (match-string 1 link))
  7808. (as (or (assoc key org-link-abbrev-alist-local)
  7809. (assoc key org-link-abbrev-alist)))
  7810. (tag (and (match-end 2) (match-string 3 link)))
  7811. rpl)
  7812. (if (not as)
  7813. link
  7814. (setq rpl (cdr as))
  7815. (cond
  7816. ((symbolp rpl) (funcall rpl tag))
  7817. ((string-match "%(\\([^)]+\\))" rpl)
  7818. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7819. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7820. ((string-match "%h" rpl)
  7821. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7822. (t (concat rpl tag)))))
  7823. link))
  7824. ;;; Storing and inserting links
  7825. (defvar org-insert-link-history nil
  7826. "Minibuffer history for links inserted with `org-insert-link'.")
  7827. (defvar org-stored-links nil
  7828. "Contains the links stored with `org-store-link'.")
  7829. (defvar org-store-link-plist nil
  7830. "Plist with info about the most recently link created with `org-store-link'.")
  7831. (defvar org-link-protocols nil
  7832. "Link protocols added to Org-mode using `org-add-link-type'.")
  7833. (defvar org-store-link-functions nil
  7834. "List of functions that are called to create and store a link.
  7835. Each function will be called in turn until one returns a non-nil
  7836. value. Each function should check if it is responsible for creating
  7837. this link (for example by looking at the major mode).
  7838. If not, it must exit and return nil.
  7839. If yes, it should return a non-nil value after a calling
  7840. `org-store-link-props' with a list of properties and values.
  7841. Special properties are:
  7842. :type The link prefix, like \"http\". This must be given.
  7843. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7844. This is obligatory as well.
  7845. :description Optional default description for the second pair
  7846. of brackets in an Org-mode link. The user can still change
  7847. this when inserting this link into an Org-mode buffer.
  7848. In addition to these, any additional properties can be specified
  7849. and then used in capture templates.")
  7850. (defun org-add-link-type (type &optional follow export)
  7851. "Add TYPE to the list of `org-link-types'.
  7852. Re-compute all regular expressions depending on `org-link-types'
  7853. FOLLOW and EXPORT are two functions.
  7854. FOLLOW should take the link path as the single argument and do whatever
  7855. is necessary to follow the link, for example find a file or display
  7856. a mail message.
  7857. EXPORT should format the link path for export to one of the export formats.
  7858. It should be a function accepting three arguments:
  7859. path the path of the link, the text after the prefix (like \"http:\")
  7860. desc the description of the link, if any, or a description added by
  7861. org-export-normalize-links if there is none
  7862. format the export format, a symbol like `html' or `latex' or `ascii'..
  7863. The function may use the FORMAT information to return different values
  7864. depending on the format. The return value will be put literally into
  7865. the exported file. If the return value is nil, this means Org should
  7866. do what it normally does with links which do not have EXPORT defined.
  7867. Org-mode has a built-in default for exporting links. If you are happy with
  7868. this default, there is no need to define an export function for the link
  7869. type. For a simple example of an export function, see `org-bbdb.el'."
  7870. (add-to-list 'org-link-types type t)
  7871. (org-make-link-regexps)
  7872. (if (assoc type org-link-protocols)
  7873. (setcdr (assoc type org-link-protocols) (list follow export))
  7874. (push (list type follow export) org-link-protocols)))
  7875. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7876. (defvar org-link-to-org-use-id) ; Defined in org-id.el
  7877. ;;;###autoload
  7878. (defun org-store-link (arg)
  7879. "\\<org-mode-map>Store an org-link to the current location.
  7880. This link is added to `org-stored-links' and can later be inserted
  7881. into an org-buffer with \\[org-insert-link].
  7882. For some link types, a prefix arg is interpreted:
  7883. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7884. For file links, arg negates `org-context-in-file-links'."
  7885. (interactive "P")
  7886. (org-load-modules-maybe)
  7887. (setq org-store-link-plist nil) ; reset
  7888. (org-with-limited-levels
  7889. (let (link cpltxt desc description search txt custom-id agenda-link)
  7890. (cond
  7891. ((run-hook-with-args-until-success 'org-store-link-functions)
  7892. (setq link (plist-get org-store-link-plist :link)
  7893. desc (or (plist-get org-store-link-plist :description) link)))
  7894. ((org-src-edit-buffer-p)
  7895. (let (label gc)
  7896. (while (or (not label)
  7897. (save-excursion
  7898. (save-restriction
  7899. (widen)
  7900. (goto-char (point-min))
  7901. (re-search-forward
  7902. (regexp-quote (format org-coderef-label-format label))
  7903. nil t))))
  7904. (when label (message "Label exists already") (sit-for 2))
  7905. (setq label (read-string "Code line label: " label)))
  7906. (end-of-line 1)
  7907. (setq link (format org-coderef-label-format label))
  7908. (setq gc (- 79 (length link)))
  7909. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7910. (insert link)
  7911. (setq link (concat "(" label ")") desc nil)))
  7912. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7913. ;; We are in the agenda, link to referenced location
  7914. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7915. (get-text-property (point) 'org-marker))))
  7916. (when m
  7917. (org-with-point-at m
  7918. (setq agenda-link
  7919. (if (org-called-interactively-p 'any)
  7920. (call-interactively 'org-store-link)
  7921. (org-store-link nil)))))))
  7922. ((eq major-mode 'calendar-mode)
  7923. (let ((cd (calendar-cursor-to-date)))
  7924. (setq link
  7925. (format-time-string
  7926. (car org-time-stamp-formats)
  7927. (apply 'encode-time
  7928. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7929. nil nil nil))))
  7930. (org-store-link-props :type "calendar" :date cd)))
  7931. ((eq major-mode 'help-mode)
  7932. (setq link (concat "help:" (save-excursion
  7933. (goto-char (point-min))
  7934. (looking-at "^[^ ]+")
  7935. (match-string 0))))
  7936. (org-store-link-props :type "help"))
  7937. ((eq major-mode 'w3-mode)
  7938. (setq cpltxt (if (and (buffer-name)
  7939. (not (string-match "Untitled" (buffer-name))))
  7940. (buffer-name)
  7941. (url-view-url t))
  7942. link (url-view-url t))
  7943. (org-store-link-props :type "w3" :url (url-view-url t)))
  7944. ((eq major-mode 'w3m-mode)
  7945. (setq cpltxt (or w3m-current-title w3m-current-url)
  7946. link w3m-current-url)
  7947. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7948. ((setq search (run-hook-with-args-until-success
  7949. 'org-create-file-search-functions))
  7950. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7951. "::" search))
  7952. (setq cpltxt (or description link)))
  7953. ((eq major-mode 'image-mode)
  7954. (setq cpltxt (concat "file:"
  7955. (abbreviate-file-name buffer-file-name))
  7956. link cpltxt)
  7957. (org-store-link-props :type "image" :file buffer-file-name))
  7958. ((eq major-mode 'dired-mode)
  7959. ;; link to the file in the current line
  7960. (let ((file (dired-get-filename nil t)))
  7961. (setq file (if file
  7962. (abbreviate-file-name
  7963. (expand-file-name (dired-get-filename nil t)))
  7964. ;; otherwise, no file so use current directory.
  7965. default-directory))
  7966. (setq cpltxt (concat "file:" file)
  7967. link cpltxt)))
  7968. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7969. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7970. (cond
  7971. ((org-in-regexp "<<\\(.*?\\)>>")
  7972. (setq cpltxt
  7973. (concat "file:"
  7974. (abbreviate-file-name
  7975. (buffer-file-name (buffer-base-buffer)))
  7976. "::" (match-string 1))
  7977. link cpltxt))
  7978. ((and (featurep 'org-id)
  7979. (or (eq org-link-to-org-use-id t)
  7980. (and (org-called-interactively-p 'any)
  7981. (or (eq org-link-to-org-use-id 'create-if-interactive)
  7982. (and (eq org-link-to-org-use-id
  7983. 'create-if-interactive-and-no-custom-id)
  7984. (not custom-id))))
  7985. (and org-link-to-org-use-id (org-entry-get nil "ID"))))
  7986. ;; We can make a link using the ID.
  7987. (setq link (condition-case nil
  7988. (prog1 (org-id-store-link)
  7989. (setq desc (plist-get org-store-link-plist :description)))
  7990. (error
  7991. ;; probably before first headline, link to file only
  7992. (concat "file:"
  7993. (abbreviate-file-name
  7994. (buffer-file-name (buffer-base-buffer))))))))
  7995. (t
  7996. ;; Just link to current headline
  7997. (setq cpltxt (concat "file:"
  7998. (abbreviate-file-name
  7999. (buffer-file-name (buffer-base-buffer)))))
  8000. ;; Add a context search string
  8001. (when (org-xor org-context-in-file-links arg)
  8002. (setq txt (cond
  8003. ((org-at-heading-p) nil)
  8004. ((org-region-active-p)
  8005. (buffer-substring (region-beginning) (region-end)))))
  8006. (when (or (null txt) (string-match "\\S-" txt))
  8007. (setq cpltxt
  8008. (concat cpltxt "::"
  8009. (condition-case nil
  8010. (org-make-org-heading-search-string txt)
  8011. (error "")))
  8012. desc (or (nth 4 (ignore-errors
  8013. (org-heading-components))) "NONE"))))
  8014. (if (string-match "::\\'" cpltxt)
  8015. (setq cpltxt (substring cpltxt 0 -2)))
  8016. (setq link cpltxt))))
  8017. ((buffer-file-name (buffer-base-buffer))
  8018. ;; Just link to this file here.
  8019. (setq cpltxt (concat "file:"
  8020. (abbreviate-file-name
  8021. (buffer-file-name (buffer-base-buffer)))))
  8022. ;; Add a context string
  8023. (when (org-xor org-context-in-file-links arg)
  8024. (setq txt (if (org-region-active-p)
  8025. (buffer-substring (region-beginning) (region-end))
  8026. (buffer-substring (point-at-bol) (point-at-eol))))
  8027. ;; Only use search option if there is some text.
  8028. (when (string-match "\\S-" txt)
  8029. (setq cpltxt
  8030. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8031. desc "NONE")))
  8032. (setq link cpltxt))
  8033. ((org-called-interactively-p 'interactive)
  8034. (error "Cannot link to a buffer which is not visiting a file"))
  8035. (t (setq link nil)))
  8036. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8037. (setq link (or link cpltxt)
  8038. desc (or desc cpltxt))
  8039. (if (equal desc "NONE") (setq desc nil))
  8040. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8041. (progn
  8042. (setq org-stored-links
  8043. (cons (list link desc) org-stored-links))
  8044. (message "Stored: %s" (or desc link))
  8045. (when custom-id
  8046. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8047. "::#" custom-id))
  8048. (setq org-stored-links
  8049. (cons (list link desc) org-stored-links))))
  8050. (or agenda-link (and link (org-make-link-string link desc)))))))
  8051. (defun org-store-link-props (&rest plist)
  8052. "Store link properties, extract names and addresses."
  8053. (let (x adr)
  8054. (when (setq x (plist-get plist :from))
  8055. (setq adr (mail-extract-address-components x))
  8056. (setq plist (plist-put plist :fromname (car adr)))
  8057. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8058. (when (setq x (plist-get plist :to))
  8059. (setq adr (mail-extract-address-components x))
  8060. (setq plist (plist-put plist :toname (car adr)))
  8061. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8062. (let ((from (plist-get plist :from))
  8063. (to (plist-get plist :to)))
  8064. (when (and from to org-from-is-user-regexp)
  8065. (setq plist
  8066. (plist-put plist :fromto
  8067. (if (string-match org-from-is-user-regexp from)
  8068. (concat "to %t")
  8069. (concat "from %f"))))))
  8070. (setq org-store-link-plist plist))
  8071. (defun org-add-link-props (&rest plist)
  8072. "Add these properties to the link property list."
  8073. (let (key value)
  8074. (while plist
  8075. (setq key (pop plist) value (pop plist))
  8076. (setq org-store-link-plist
  8077. (plist-put org-store-link-plist key value)))))
  8078. (defun org-email-link-description (&optional fmt)
  8079. "Return the description part of an email link.
  8080. This takes information from `org-store-link-plist' and formats it
  8081. according to FMT (default from `org-email-link-description-format')."
  8082. (setq fmt (or fmt org-email-link-description-format))
  8083. (let* ((p org-store-link-plist)
  8084. (to (plist-get p :toaddress))
  8085. (from (plist-get p :fromaddress))
  8086. (table
  8087. (list
  8088. (cons "%c" (plist-get p :fromto))
  8089. (cons "%F" (plist-get p :from))
  8090. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8091. (cons "%T" (plist-get p :to))
  8092. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8093. (cons "%s" (plist-get p :subject))
  8094. (cons "%d" (plist-get p :date))
  8095. (cons "%m" (plist-get p :message-id)))))
  8096. (when (string-match "%c" fmt)
  8097. ;; Check if the user wrote this message
  8098. (if (and org-from-is-user-regexp from to
  8099. (save-match-data (string-match org-from-is-user-regexp from)))
  8100. (setq fmt (replace-match "to %t" t t fmt))
  8101. (setq fmt (replace-match "from %f" t t fmt))))
  8102. (org-replace-escapes fmt table)))
  8103. (defun org-make-org-heading-search-string (&optional string heading)
  8104. "Make search string for STRING or current headline."
  8105. (interactive)
  8106. (let ((s (or string (org-get-heading)))
  8107. (lines org-context-in-file-links))
  8108. (unless (and string (not heading))
  8109. ;; We are using a headline, clean up garbage in there.
  8110. (if (string-match org-todo-regexp s)
  8111. (setq s (replace-match "" t t s)))
  8112. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8113. (setq s (replace-match "" t t s)))
  8114. (setq s (org-trim s))
  8115. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8116. org-comment-string "\\)") s)
  8117. (setq s (replace-match "" t t s)))
  8118. (while (string-match org-ts-regexp s)
  8119. (setq s (replace-match "" t t s))))
  8120. (or string (setq s (concat "*" s))) ; Add * for headlines
  8121. (when (and string (integerp lines) (> lines 0))
  8122. (let ((slines (org-split-string s "\n")))
  8123. (when (< lines (length slines))
  8124. (setq s (mapconcat
  8125. 'identity
  8126. (reverse (nthcdr (- (length slines) lines)
  8127. (reverse slines))) "\n")))))
  8128. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8129. (defun org-make-link-string (link &optional description)
  8130. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8131. (unless (string-match "\\S-" link)
  8132. (error "Empty link"))
  8133. (when (and description
  8134. (stringp description)
  8135. (not (string-match "\\S-" description)))
  8136. (setq description nil))
  8137. (when (stringp description)
  8138. ;; Remove brackets from the description, they are fatal.
  8139. (while (string-match "\\[" description)
  8140. (setq description (replace-match "{" t t description)))
  8141. (while (string-match "\\]" description)
  8142. (setq description (replace-match "}" t t description))))
  8143. (when (equal link description)
  8144. ;; No description needed, it is identical
  8145. (setq description nil))
  8146. (when (and (not description)
  8147. (not (string-match (org-image-file-name-regexp) link))
  8148. (not (equal link (org-link-escape link))))
  8149. (setq description (org-extract-attributes link)))
  8150. (setq link
  8151. (cond ((string-match (org-image-file-name-regexp) link) link)
  8152. ((string-match org-link-types-re link)
  8153. (concat (match-string 1 link)
  8154. (org-link-escape (substring link (match-end 1)))))
  8155. (t (org-link-escape link))))
  8156. (concat "[[" link "]"
  8157. (if description (concat "[" description "]") "")
  8158. "]"))
  8159. (defconst org-link-escape-chars
  8160. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8161. "List of characters that should be escaped in link.
  8162. This is the list that is used for internal purposes.")
  8163. (defconst org-link-escape-chars-browser
  8164. '(?\ )
  8165. "List of escapes for characters that are problematic in links.
  8166. This is the list that is used before handing over to the browser.")
  8167. (defun org-link-escape (text &optional table merge)
  8168. "Return percent escaped representation of TEXT.
  8169. TEXT is a string with the text to escape.
  8170. Optional argument TABLE is a list with characters that should be
  8171. escaped. When nil, `org-link-escape-chars' is used.
  8172. If optional argument MERGE is set, merge TABLE into
  8173. `org-link-escape-chars'."
  8174. (cond
  8175. ((and table merge)
  8176. (mapc (lambda (defchr)
  8177. (unless (member defchr table)
  8178. (setq table (cons defchr table)))) org-link-escape-chars))
  8179. ((null table)
  8180. (setq table org-link-escape-chars)))
  8181. (mapconcat
  8182. (lambda (char)
  8183. (if (or (member char table)
  8184. (and (or (< char 32) (= char 37) (> char 126))
  8185. org-url-hexify-p))
  8186. (mapconcat (lambda (sequence-element)
  8187. (format "%%%.2X" sequence-element))
  8188. (or (encode-coding-char char 'utf-8)
  8189. (error "Unable to percent escape character: %s"
  8190. (char-to-string char))) "")
  8191. (char-to-string char))) text ""))
  8192. (defun org-link-unescape (str)
  8193. "Unhex hexified Unicode strings as returned from the JavaScript function
  8194. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8195. (unless (and (null str) (string= "" str))
  8196. (let ((pos 0) (case-fold-search t) unhexed)
  8197. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8198. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8199. (setq str (replace-match unhexed t t str))
  8200. (setq pos (+ pos (length unhexed))))))
  8201. str)
  8202. (defun org-link-unescape-compound (hex)
  8203. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8204. Note: this function also decodes single byte encodings like
  8205. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8206. (save-match-data
  8207. (let* ((bytes (cdr (split-string hex "%")))
  8208. (ret "")
  8209. (eat 0)
  8210. (sum 0))
  8211. (while bytes
  8212. (let* ((val (string-to-number (pop bytes) 16))
  8213. (shift-xor
  8214. (if (= 0 eat)
  8215. (cond
  8216. ((>= val 252) (cons 6 252))
  8217. ((>= val 248) (cons 5 248))
  8218. ((>= val 240) (cons 4 240))
  8219. ((>= val 224) (cons 3 224))
  8220. ((>= val 192) (cons 2 192))
  8221. (t (cons 0 0)))
  8222. (cons 6 128))))
  8223. (if (>= val 192) (setq eat (car shift-xor)))
  8224. (setq val (logxor val (cdr shift-xor)))
  8225. (setq sum (+ (lsh sum (car shift-xor)) val))
  8226. (if (> eat 0) (setq eat (- eat 1)))
  8227. (cond
  8228. ((= 0 eat) ;multi byte
  8229. (setq ret (concat ret (org-char-to-string sum)))
  8230. (setq sum 0))
  8231. ((not bytes) ; single byte(s)
  8232. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8233. )) ;; end (while bytes
  8234. ret )))
  8235. (defun org-link-unescape-single-byte-sequence (hex)
  8236. "Unhexify hex-encoded single byte character sequences."
  8237. (mapconcat (lambda (byte)
  8238. (char-to-string (string-to-number byte 16)))
  8239. (cdr (split-string hex "%")) ""))
  8240. (defun org-xor (a b)
  8241. "Exclusive or."
  8242. (if a (not b) b))
  8243. (defun org-fixup-message-id-for-http (s)
  8244. "Replace special characters in a message id, so it can be used in an http query."
  8245. (when (string-match "%" s)
  8246. (setq s (mapconcat (lambda (c)
  8247. (if (eq c ?%)
  8248. "%25"
  8249. (char-to-string c)))
  8250. s "")))
  8251. (while (string-match "<" s)
  8252. (setq s (replace-match "%3C" t t s)))
  8253. (while (string-match ">" s)
  8254. (setq s (replace-match "%3E" t t s)))
  8255. (while (string-match "@" s)
  8256. (setq s (replace-match "%40" t t s)))
  8257. s)
  8258. (defun org-link-prettify (link)
  8259. "Return a human-readable representation of LINK.
  8260. The car of LINK must be a raw link the cdr of LINK must be either
  8261. a link description or nil."
  8262. (let ((desc (or (cadr link) "<no description>")))
  8263. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8264. "<" (car link) ">")))
  8265. ;;;###autoload
  8266. (defun org-insert-link-global ()
  8267. "Insert a link like Org-mode does.
  8268. This command can be called in any mode to insert a link in Org-mode syntax."
  8269. (interactive)
  8270. (org-load-modules-maybe)
  8271. (org-run-like-in-org-mode 'org-insert-link))
  8272. (defun org-insert-all-links (&optional keep)
  8273. "Insert all links in `org-stored-links'."
  8274. (interactive "P")
  8275. (let ((links (copy-sequence org-stored-links)) l)
  8276. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8277. (insert "- ")
  8278. (org-insert-link nil (car l) (cadr l))
  8279. (insert "\n"))))
  8280. (defun org-link-fontify-links-to-this-file ()
  8281. "Fontify links to the current file in `org-stored-links'."
  8282. (let ((f (buffer-file-name)) a b)
  8283. (setq a (mapcar (lambda(l)
  8284. (let ((ll (car l)))
  8285. (when (and (string-match "^file:\\(.+\\)::" ll)
  8286. (equal f (expand-file-name (match-string 1 ll))))
  8287. ll)))
  8288. org-stored-links))
  8289. (when (featurep 'org-id)
  8290. (setq b (mapcar (lambda(l)
  8291. (let ((ll (car l)))
  8292. (when (and (string-match "^id:\\(.+\\)$" ll)
  8293. (equal f (expand-file-name
  8294. (or (org-id-find-id-file
  8295. (match-string 1 ll)) ""))))
  8296. ll)))
  8297. org-stored-links)))
  8298. (mapcar (lambda(l)
  8299. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8300. (delq nil (append a b)))))
  8301. (defvar org-link-links-in-this-file nil)
  8302. (defun org-insert-link (&optional complete-file link-location default-description)
  8303. "Insert a link. At the prompt, enter the link.
  8304. Completion can be used to insert any of the link protocol prefixes like
  8305. http or ftp in use.
  8306. The history can be used to select a link previously stored with
  8307. `org-store-link'. When the empty string is entered (i.e. if you just
  8308. press RET at the prompt), the link defaults to the most recently
  8309. stored link. As SPC triggers completion in the minibuffer, you need to
  8310. use M-SPC or C-q SPC to force the insertion of a space character.
  8311. You will also be prompted for a description, and if one is given, it will
  8312. be displayed in the buffer instead of the link.
  8313. If there is already a link at point, this command will allow you to edit link
  8314. and description parts.
  8315. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8316. be selected using completion. The path to the file will be relative to the
  8317. current directory if the file is in the current directory or a subdirectory.
  8318. Otherwise, the link will be the absolute path as completed in the minibuffer
  8319. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8320. option `org-link-file-path-type'.
  8321. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8322. the current directory or below.
  8323. With three \\[universal-argument] prefixes, negate the meaning of
  8324. `org-keep-stored-link-after-insertion'.
  8325. If `org-make-link-description-function' is non-nil, this function will be
  8326. called with the link target, and the result will be the default
  8327. link description.
  8328. If the LINK-LOCATION parameter is non-nil, this value will be
  8329. used as the link location instead of reading one interactively.
  8330. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8331. be used as the default description."
  8332. (interactive "P")
  8333. (let* ((wcf (current-window-configuration))
  8334. (origbuf (current-buffer))
  8335. (region (if (org-region-active-p)
  8336. (buffer-substring (region-beginning) (region-end))))
  8337. (remove (and region (list (region-beginning) (region-end))))
  8338. (desc region)
  8339. tmphist ; byte-compile incorrectly complains about this
  8340. (link link-location)
  8341. (abbrevs org-link-abbrev-alist-local)
  8342. entry file all-prefixes auto-desc)
  8343. (cond
  8344. (link-location) ; specified by arg, just use it.
  8345. ((org-in-regexp org-bracket-link-regexp 1)
  8346. ;; We do have a link at point, and we are going to edit it.
  8347. (setq remove (list (match-beginning 0) (match-end 0)))
  8348. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8349. (setq link (read-string "Link: "
  8350. (org-link-unescape
  8351. (org-match-string-no-properties 1)))))
  8352. ((or (org-in-regexp org-angle-link-re)
  8353. (org-in-regexp org-plain-link-re))
  8354. ;; Convert to bracket link
  8355. (setq remove (list (match-beginning 0) (match-end 0))
  8356. link (read-string "Link: "
  8357. (org-remove-angle-brackets (match-string 0)))))
  8358. ((member complete-file '((4) (16)))
  8359. ;; Completing read for file names.
  8360. (setq link (org-file-complete-link complete-file)))
  8361. (t
  8362. ;; Read link, with completion for stored links.
  8363. (org-link-fontify-links-to-this-file)
  8364. (org-switch-to-buffer-other-window "*Org Links*")
  8365. (with-current-buffer "*Org Links*"
  8366. (erase-buffer)
  8367. (insert "Insert a link.
  8368. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8369. (when org-stored-links
  8370. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8371. (insert (mapconcat 'org-link-prettify
  8372. (reverse org-stored-links) "\n")))
  8373. (goto-char (point-min)))
  8374. (let ((cw (selected-window)))
  8375. (select-window (get-buffer-window "*Org Links*" 'visible))
  8376. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8377. (unless (pos-visible-in-window-p (point-max))
  8378. (org-fit-window-to-buffer))
  8379. (and (window-live-p cw) (select-window cw)))
  8380. ;; Fake a link history, containing the stored links.
  8381. (setq tmphist (append (mapcar 'car org-stored-links)
  8382. org-insert-link-history))
  8383. (setq all-prefixes (append (mapcar 'car abbrevs)
  8384. (mapcar 'car org-link-abbrev-alist)
  8385. org-link-types))
  8386. (unwind-protect
  8387. (progn
  8388. (setq link
  8389. (org-completing-read
  8390. "Link: "
  8391. (append
  8392. (mapcar (lambda (x) (concat x ":"))
  8393. all-prefixes)
  8394. (mapcar 'car org-stored-links))
  8395. nil nil nil
  8396. 'tmphist
  8397. (caar org-stored-links)))
  8398. (if (not (string-match "\\S-" link))
  8399. (error "No link selected"))
  8400. (mapc (lambda(l)
  8401. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8402. org-stored-links)
  8403. (if (or (member link all-prefixes)
  8404. (and (equal ":" (substring link -1))
  8405. (member (substring link 0 -1) all-prefixes)
  8406. (setq link (substring link 0 -1))))
  8407. (setq link (with-current-buffer origbuf
  8408. (org-link-try-special-completion link)))))
  8409. (set-window-configuration wcf)
  8410. (kill-buffer "*Org Links*"))
  8411. (setq entry (assoc link org-stored-links))
  8412. (or entry (push link org-insert-link-history))
  8413. (setq desc (or desc (nth 1 entry)))))
  8414. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8415. (not org-keep-stored-link-after-insertion))
  8416. (setq org-stored-links (delq (assoc link org-stored-links)
  8417. org-stored-links)))
  8418. (if (string-match org-plain-link-re link)
  8419. ;; URL-like link, normalize the use of angular brackets.
  8420. (setq link (org-remove-angle-brackets link)))
  8421. ;; Check if we are linking to the current file with a search option
  8422. ;; If yes, simplify the link by using only the search option.
  8423. (when (and buffer-file-name
  8424. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8425. (let* ((path (match-string 1 link))
  8426. (case-fold-search nil)
  8427. (search (match-string 2 link)))
  8428. (save-match-data
  8429. (if (equal (file-truename buffer-file-name) (file-truename path))
  8430. ;; We are linking to this same file, with a search option
  8431. (setq link search)))))
  8432. ;; Check if we can/should use a relative path. If yes, simplify the link
  8433. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8434. (let* ((type (match-string 1 link))
  8435. (path (match-string 2 link))
  8436. (origpath path)
  8437. (case-fold-search nil))
  8438. (cond
  8439. ((or (eq org-link-file-path-type 'absolute)
  8440. (equal complete-file '(16)))
  8441. (setq path (abbreviate-file-name (expand-file-name path))))
  8442. ((eq org-link-file-path-type 'noabbrev)
  8443. (setq path (expand-file-name path)))
  8444. ((eq org-link-file-path-type 'relative)
  8445. (setq path (file-relative-name path)))
  8446. (t
  8447. (save-match-data
  8448. (if (string-match (concat "^" (regexp-quote
  8449. (expand-file-name
  8450. (file-name-as-directory
  8451. default-directory))))
  8452. (expand-file-name path))
  8453. ;; We are linking a file with relative path name.
  8454. (setq path (substring (expand-file-name path)
  8455. (match-end 0)))
  8456. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8457. (setq link (concat type path))
  8458. (if (equal desc origpath)
  8459. (setq desc path))))
  8460. (if org-make-link-description-function
  8461. (setq desc
  8462. (or (condition-case nil
  8463. (funcall org-make-link-description-function link desc)
  8464. (error (progn (message "Can't get link description from `%s'"
  8465. (symbol-name org-make-link-description-function))
  8466. (sit-for 2) nil)))
  8467. (read-string "Description: " default-description)))
  8468. (if default-description (setq desc default-description)
  8469. (setq desc (or (and auto-desc desc)
  8470. (read-string "Description: " desc)))))
  8471. (unless (string-match "\\S-" desc) (setq desc nil))
  8472. (if remove (apply 'delete-region remove))
  8473. (insert (org-make-link-string link desc))))
  8474. (defun org-link-try-special-completion (type)
  8475. "If there is completion support for link type TYPE, offer it."
  8476. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8477. (if (functionp fun)
  8478. (funcall fun)
  8479. (read-string "Link (no completion support): " (concat type ":")))))
  8480. (defun org-file-complete-link (&optional arg)
  8481. "Create a file link using completion."
  8482. (let (file link)
  8483. (setq file (org-iread-file-name "File: "))
  8484. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8485. (pwd1 (file-name-as-directory (abbreviate-file-name
  8486. (expand-file-name ".")))))
  8487. (cond
  8488. ((equal arg '(16))
  8489. (setq link (concat
  8490. "file:"
  8491. (abbreviate-file-name (expand-file-name file)))))
  8492. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8493. (setq link (concat "file:" (match-string 1 file))))
  8494. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8495. (expand-file-name file))
  8496. (setq link (concat
  8497. "file:" (match-string 1 (expand-file-name file)))))
  8498. (t (setq link (concat "file:" file)))))
  8499. link))
  8500. (defun org-iread-file-name (&rest args)
  8501. "Read-file-name using `ido-mode' speedup if available.
  8502. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8503. See `read-file-name' for a description of parameters."
  8504. (org-without-partial-completion
  8505. (if (and org-completion-use-ido
  8506. (fboundp 'ido-read-file-name)
  8507. (boundp 'ido-mode) ido-mode
  8508. (listp (second args)))
  8509. (let ((ido-enter-matching-directory nil))
  8510. (apply 'ido-read-file-name args))
  8511. (apply 'read-file-name args))))
  8512. (defun org-completing-read (&rest args)
  8513. "Completing-read with SPACE being a normal character."
  8514. (let ((enable-recursive-minibuffers t)
  8515. (minibuffer-local-completion-map
  8516. (copy-keymap minibuffer-local-completion-map)))
  8517. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8518. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8519. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8520. (apply 'org-icompleting-read args)))
  8521. (defun org-completing-read-no-i (&rest args)
  8522. (let (org-completion-use-ido org-completion-use-iswitchb)
  8523. (apply 'org-completing-read args)))
  8524. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8525. "Use iswitch as a completing-read replacement to choose from choices.
  8526. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8527. from."
  8528. (let* ((iswitchb-use-virtual-buffers nil)
  8529. (iswitchb-make-buflist-hook
  8530. (lambda ()
  8531. (setq iswitchb-temp-buflist choices))))
  8532. (iswitchb-read-buffer prompt)))
  8533. (defun org-icompleting-read (&rest args)
  8534. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8535. (org-without-partial-completion
  8536. (if (and org-completion-use-ido
  8537. (fboundp 'ido-completing-read)
  8538. (boundp 'ido-mode) ido-mode
  8539. (listp (second args)))
  8540. (let ((ido-enter-matching-directory nil))
  8541. (apply 'ido-completing-read (concat (car args))
  8542. (if (consp (car (nth 1 args)))
  8543. (mapcar 'car (nth 1 args))
  8544. (nth 1 args))
  8545. (cddr args)))
  8546. (if (and org-completion-use-iswitchb
  8547. (boundp 'iswitchb-mode) iswitchb-mode
  8548. (listp (second args)))
  8549. (apply 'org-iswitchb-completing-read (concat (car args))
  8550. (if (consp (car (nth 1 args)))
  8551. (mapcar 'car (nth 1 args))
  8552. (nth 1 args))
  8553. (cddr args))
  8554. (apply 'completing-read args)))))
  8555. (defun org-extract-attributes (s)
  8556. "Extract the attributes cookie from a string and set as text property."
  8557. (let (a attr (start 0) key value)
  8558. (save-match-data
  8559. (when (string-match "{{\\([^}]+\\)}}$" s)
  8560. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8561. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8562. (setq key (match-string 1 a) value (match-string 2 a)
  8563. start (match-end 0)
  8564. attr (plist-put attr (intern key) value))))
  8565. (org-add-props s nil 'org-attr attr))
  8566. s))
  8567. (defun org-extract-attributes-from-string (tag)
  8568. (let (key value attr)
  8569. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8570. (setq key (match-string 1 tag) value (match-string 2 tag)
  8571. tag (replace-match "" t t tag)
  8572. attr (plist-put attr (intern key) value)))
  8573. (cons tag attr)))
  8574. (defun org-attributes-to-string (plist)
  8575. "Format a property list into an HTML attribute list."
  8576. (let ((s "") key value)
  8577. (while plist
  8578. (setq key (pop plist) value (pop plist))
  8579. (and value
  8580. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8581. s))
  8582. ;;; Opening/following a link
  8583. (defvar org-link-search-failed nil)
  8584. (defvar org-open-link-functions nil
  8585. "Hook for functions finding a plain text link.
  8586. These functions must take a single argument, the link content.
  8587. They will be called for links that look like [[link text][description]]
  8588. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8589. like a filename (e.g. \"./blue.png\").
  8590. These functions will be called *before* Org attempts to resolve the
  8591. link by doing text searches in the current buffer - so if you want a
  8592. link \"[[target]]\" to still find \"<<target>>\", your function should
  8593. handle this as a special case.
  8594. When the function does handle the link, it must return a non-nil value.
  8595. If it decides that it is not responsible for this link, it must return
  8596. nil to indicate that that Org-mode can continue with other options
  8597. like exact and fuzzy text search.")
  8598. (defun org-next-link ()
  8599. "Move forward to the next link.
  8600. If the link is in hidden text, expose it."
  8601. (interactive)
  8602. (when (and org-link-search-failed (eq this-command last-command))
  8603. (goto-char (point-min))
  8604. (message "Link search wrapped back to beginning of buffer"))
  8605. (setq org-link-search-failed nil)
  8606. (let* ((pos (point))
  8607. (ct (org-context))
  8608. (a (assoc :link ct)))
  8609. (if a (goto-char (nth 2 a)))
  8610. (if (re-search-forward org-any-link-re nil t)
  8611. (progn
  8612. (goto-char (match-beginning 0))
  8613. (if (outline-invisible-p) (org-show-context)))
  8614. (goto-char pos)
  8615. (setq org-link-search-failed t)
  8616. (error "No further link found"))))
  8617. (defun org-previous-link ()
  8618. "Move backward to the previous link.
  8619. If the link is in hidden text, expose it."
  8620. (interactive)
  8621. (when (and org-link-search-failed (eq this-command last-command))
  8622. (goto-char (point-max))
  8623. (message "Link search wrapped back to end of buffer"))
  8624. (setq org-link-search-failed nil)
  8625. (let* ((pos (point))
  8626. (ct (org-context))
  8627. (a (assoc :link ct)))
  8628. (if a (goto-char (nth 1 a)))
  8629. (if (re-search-backward org-any-link-re nil t)
  8630. (progn
  8631. (goto-char (match-beginning 0))
  8632. (if (outline-invisible-p) (org-show-context)))
  8633. (goto-char pos)
  8634. (setq org-link-search-failed t)
  8635. (error "No further link found"))))
  8636. (defun org-translate-link (s)
  8637. "Translate a link string if a translation function has been defined."
  8638. (if (and org-link-translation-function
  8639. (fboundp org-link-translation-function)
  8640. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8641. (progn
  8642. (setq s (funcall org-link-translation-function
  8643. (match-string 1 s) (match-string 2 s)))
  8644. (concat (car s) ":" (cdr s)))
  8645. s))
  8646. (defun org-translate-link-from-planner (type path)
  8647. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8648. This is still an experimental function, your mileage may vary."
  8649. (cond
  8650. ((member type '("http" "https" "news" "ftp"))
  8651. ;; standard Internet links are the same.
  8652. nil)
  8653. ((and (equal type "irc") (string-match "^//" path))
  8654. ;; Planner has two / at the beginning of an irc link, we have 1.
  8655. ;; We should have zero, actually....
  8656. (setq path (substring path 1)))
  8657. ((and (equal type "lisp") (string-match "^/" path))
  8658. ;; Planner has a slash, we do not.
  8659. (setq type "elisp" path (substring path 1)))
  8660. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8661. ;; A typical message link. Planner has the id after the final slash,
  8662. ;; we separate it with a hash mark
  8663. (setq path (concat (match-string 1 path) "#"
  8664. (org-remove-angle-brackets (match-string 2 path)))))
  8665. )
  8666. (cons type path))
  8667. (defun org-find-file-at-mouse (ev)
  8668. "Open file link or URL at mouse."
  8669. (interactive "e")
  8670. (mouse-set-point ev)
  8671. (org-open-at-point 'in-emacs))
  8672. (defun org-open-at-mouse (ev)
  8673. "Open file link or URL at mouse.
  8674. See the docstring of `org-open-file' for details."
  8675. (interactive "e")
  8676. (mouse-set-point ev)
  8677. (if (eq major-mode 'org-agenda-mode)
  8678. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8679. (org-open-at-point))
  8680. (defvar org-window-config-before-follow-link nil
  8681. "The window configuration before following a link.
  8682. This is saved in case the need arises to restore it.")
  8683. (defvar org-open-link-marker (make-marker)
  8684. "Marker pointing to the location where `org-open-at-point; was called.")
  8685. ;;;###autoload
  8686. (defun org-open-at-point-global ()
  8687. "Follow a link like Org-mode does.
  8688. This command can be called in any mode to follow a link that has
  8689. Org-mode syntax."
  8690. (interactive)
  8691. (org-run-like-in-org-mode 'org-open-at-point))
  8692. ;;;###autoload
  8693. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8694. "Open a link in the string S, as if it was in Org-mode."
  8695. (interactive "sLink: \nP")
  8696. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8697. (with-temp-buffer
  8698. (let ((org-inhibit-startup (not reference-buffer)))
  8699. (org-mode)
  8700. (insert s)
  8701. (goto-char (point-min))
  8702. (when reference-buffer
  8703. (setq org-link-abbrev-alist-local
  8704. (with-current-buffer reference-buffer
  8705. org-link-abbrev-alist-local)))
  8706. (org-open-at-point arg reference-buffer)))))
  8707. (defvar org-open-at-point-functions nil
  8708. "Hook that is run when following a link at point.
  8709. Functions in this hook must return t if they identify and follow
  8710. a link at point. If they don't find anything interesting at point,
  8711. they must return nil.")
  8712. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8713. (defun org-open-at-point (&optional arg reference-buffer)
  8714. "Open link at or after point.
  8715. If there is no link at point, this function will search forward up to
  8716. the end of the current line.
  8717. Normally, files will be opened by an appropriate application. If the
  8718. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8719. With a double prefix argument, try to open outside of Emacs, in the
  8720. application the system uses for this file type."
  8721. (interactive "P")
  8722. ;; if in a code block, then open the block's results
  8723. (unless (call-interactively #'org-babel-open-src-block-result)
  8724. (org-load-modules-maybe)
  8725. (move-marker org-open-link-marker (point))
  8726. (setq org-window-config-before-follow-link (current-window-configuration))
  8727. (org-remove-occur-highlights nil nil t)
  8728. (cond
  8729. ((and (org-at-heading-p)
  8730. (not (org-at-timestamp-p t))
  8731. (not (org-in-regexp
  8732. (concat org-plain-link-re "\\|"
  8733. org-bracket-link-regexp "\\|"
  8734. org-angle-link-re "\\|"
  8735. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8736. (not (get-text-property (point) 'org-linked-text)))
  8737. (or (org-offer-links-in-entry arg)
  8738. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8739. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8740. ((and (org-at-timestamp-p t)
  8741. (not (org-in-regexp org-bracket-link-regexp)))
  8742. (org-follow-timestamp-link))
  8743. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8744. (not (org-in-regexp org-bracket-link-regexp)))
  8745. (org-footnote-action))
  8746. (t
  8747. (let (type path link line search (pos (point)))
  8748. (catch 'match
  8749. (save-excursion
  8750. (skip-chars-forward "^]\n\r")
  8751. (when (org-in-regexp org-bracket-link-regexp 1)
  8752. (setq link (org-extract-attributes
  8753. (org-link-unescape (org-match-string-no-properties 1))))
  8754. (while (string-match " *\n *" link)
  8755. (setq link (replace-match " " t t link)))
  8756. (setq link (org-link-expand-abbrev link))
  8757. (cond
  8758. ((or (file-name-absolute-p link)
  8759. (string-match "^\\.\\.?/" link))
  8760. (setq type "file" path link))
  8761. ((string-match org-link-re-with-space3 link)
  8762. (setq type (match-string 1 link) path (match-string 2 link)))
  8763. ((string-match "^help:+\\(.+\\)" link)
  8764. (setq type "help" path (match-string 1 link)))
  8765. (t (setq type "thisfile" path link)))
  8766. (throw 'match t)))
  8767. (when (get-text-property (point) 'org-linked-text)
  8768. (setq type "thisfile"
  8769. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8770. (1+ (point)) (point))
  8771. path (buffer-substring
  8772. (or (previous-single-property-change pos 'org-linked-text)
  8773. (point-min))
  8774. (or (next-single-property-change pos 'org-linked-text)
  8775. (point-max))))
  8776. (throw 'match t))
  8777. (save-excursion
  8778. (when (or (org-in-regexp org-angle-link-re)
  8779. (and (goto-char (car (org-in-regexp org-plain-link-re)))
  8780. (save-match-data (not (looking-back "\\[\\[")))))
  8781. (setq type (match-string 1)
  8782. path (org-link-unescape (match-string 2)))
  8783. (throw 'match t)))
  8784. (save-excursion
  8785. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8786. (setq type "tags"
  8787. path (match-string 1))
  8788. (while (string-match ":" path)
  8789. (setq path (replace-match "+" t t path)))
  8790. (throw 'match t)))
  8791. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8792. (setq type "tree-match"
  8793. path (match-string 1))
  8794. (throw 'match t)))
  8795. (unless path
  8796. (error "No link found"))
  8797. ;; switch back to reference buffer
  8798. ;; needed when if called in a temporary buffer through
  8799. ;; org-open-link-from-string
  8800. (with-current-buffer (or reference-buffer (current-buffer))
  8801. ;; Remove any trailing spaces in path
  8802. (if (string-match " +\\'" path)
  8803. (setq path (replace-match "" t t path)))
  8804. (if (and org-link-translation-function
  8805. (fboundp org-link-translation-function))
  8806. ;; Check if we need to translate the link
  8807. (let ((tmp (funcall org-link-translation-function type path)))
  8808. (setq type (car tmp) path (cdr tmp))))
  8809. (cond
  8810. ((assoc type org-link-protocols)
  8811. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8812. ((equal type "help")
  8813. (let ((f-or-v (intern path)))
  8814. (cond ((fboundp f-or-v)
  8815. (describe-function f-or-v))
  8816. ((boundp f-or-v)
  8817. (describe-variable f-or-v))
  8818. (t (error "Not a known function or variable")))))
  8819. ((equal type "mailto")
  8820. (let ((cmd (car org-link-mailto-program))
  8821. (args (cdr org-link-mailto-program)) args1
  8822. (address path) (subject "") a)
  8823. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8824. (setq address (match-string 1 path)
  8825. subject (org-link-escape (match-string 2 path))))
  8826. (while args
  8827. (cond
  8828. ((not (stringp (car args))) (push (pop args) args1))
  8829. (t (setq a (pop args))
  8830. (if (string-match "%a" a)
  8831. (setq a (replace-match address t t a)))
  8832. (if (string-match "%s" a)
  8833. (setq a (replace-match subject t t a)))
  8834. (push a args1))))
  8835. (apply cmd (nreverse args1))))
  8836. ((member type '("http" "https" "ftp" "news"))
  8837. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8838. (org-link-escape
  8839. path org-link-escape-chars-browser)
  8840. path))))
  8841. ((string= type "doi")
  8842. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8843. (org-link-escape
  8844. path org-link-escape-chars-browser)
  8845. path))))
  8846. ((member type '("message"))
  8847. (browse-url (concat type ":" path)))
  8848. ((string= type "tags")
  8849. (org-tags-view arg path))
  8850. ((string= type "tree-match")
  8851. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8852. ((string= type "file")
  8853. (if (string-match "::\\([0-9]+\\)\\'" path)
  8854. (setq line (string-to-number (match-string 1 path))
  8855. path (substring path 0 (match-beginning 0)))
  8856. (if (string-match "::\\(.+\\)\\'" path)
  8857. (setq search (match-string 1 path)
  8858. path (substring path 0 (match-beginning 0)))))
  8859. (if (string-match "[*?{]" (file-name-nondirectory path))
  8860. (dired path)
  8861. (org-open-file path arg line search)))
  8862. ((string= type "shell")
  8863. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8864. (cmd path))
  8865. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8866. (string-match org-confirm-shell-link-not-regexp cmd))
  8867. (not org-confirm-shell-link-function)
  8868. (funcall org-confirm-shell-link-function
  8869. (format "Execute \"%s\" in shell? "
  8870. (org-add-props cmd nil
  8871. 'face 'org-warning))))
  8872. (progn
  8873. (message "Executing %s" cmd)
  8874. (shell-command cmd buf)
  8875. (if (featurep 'midnight)
  8876. (setq clean-buffer-list-kill-buffer-names
  8877. (cons buf clean-buffer-list-kill-buffer-names))))
  8878. (error "Abort"))))
  8879. ((string= type "elisp")
  8880. (let ((cmd path))
  8881. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8882. (string-match org-confirm-elisp-link-not-regexp cmd))
  8883. (not org-confirm-elisp-link-function)
  8884. (funcall org-confirm-elisp-link-function
  8885. (format "Execute \"%s\" as elisp? "
  8886. (org-add-props cmd nil
  8887. 'face 'org-warning))))
  8888. (message "%s => %s" cmd
  8889. (if (equal (string-to-char cmd) ?\()
  8890. (eval (read cmd))
  8891. (call-interactively (read cmd))))
  8892. (error "Abort"))))
  8893. ((and (string= type "thisfile")
  8894. (run-hook-with-args-until-success
  8895. 'org-open-link-functions path)))
  8896. ((string= type "thisfile")
  8897. (if arg
  8898. (switch-to-buffer-other-window
  8899. (org-get-buffer-for-internal-link (current-buffer)))
  8900. (org-mark-ring-push))
  8901. (let ((cmd `(org-link-search
  8902. ,path
  8903. ,(cond ((equal arg '(4)) ''occur)
  8904. ((equal arg '(16)) ''org-occur))
  8905. ,pos)))
  8906. (condition-case nil (let ((org-link-search-inhibit-query t))
  8907. (eval cmd))
  8908. (error (progn (widen) (eval cmd))))))
  8909. (t (browse-url-at-point)))))))
  8910. (move-marker org-open-link-marker nil)
  8911. (run-hook-with-args 'org-follow-link-hook)))
  8912. (defun org-offer-links-in-entry (&optional nth zero)
  8913. "Offer links in the current entry and follow the selected link.
  8914. If there is only one link, follow it immediately as well.
  8915. If NTH is an integer, immediately pick the NTH link found.
  8916. If ZERO is a string, check also this string for a link, and if
  8917. there is one, offer it as link number zero."
  8918. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8919. "\\(" org-angle-link-re "\\)\\|"
  8920. "\\(" org-plain-link-re "\\)"))
  8921. (cnt ?0)
  8922. (in-emacs (if (integerp nth) nil nth))
  8923. have-zero end links link c)
  8924. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8925. (push (match-string 0 zero) links)
  8926. (setq cnt (1- cnt) have-zero t))
  8927. (save-excursion
  8928. (org-back-to-heading t)
  8929. (setq end (save-excursion (outline-next-heading) (point)))
  8930. (while (re-search-forward re end t)
  8931. (push (match-string 0) links))
  8932. (setq links (org-uniquify (reverse links))))
  8933. (cond
  8934. ((null links)
  8935. (message "No links"))
  8936. ((equal (length links) 1)
  8937. (setq link (list (car links))))
  8938. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8939. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8940. (t ; we have to select a link
  8941. (save-excursion
  8942. (save-window-excursion
  8943. (delete-other-windows)
  8944. (with-output-to-temp-buffer "*Select Link*"
  8945. (mapc (lambda (l)
  8946. (if (not (string-match org-bracket-link-regexp l))
  8947. (princ (format "[%c] %s\n" (incf cnt)
  8948. (org-remove-angle-brackets l)))
  8949. (if (match-end 3)
  8950. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8951. (match-string 3 l) (match-string 1 l)))
  8952. (princ (format "[%c] %s\n" (incf cnt)
  8953. (match-string 1 l))))))
  8954. links))
  8955. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8956. (message "Select link to open, RET to open all:")
  8957. (setq c (read-char-exclusive))
  8958. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8959. (when (equal c ?q) (error "Abort"))
  8960. (if (equal c ?\C-m)
  8961. (setq link links)
  8962. (setq nth (- c ?0))
  8963. (if have-zero (setq nth (1+ nth)))
  8964. (unless (and (integerp nth) (>= (length links) nth))
  8965. (error "Invalid link selection"))
  8966. (setq link (list (nth (1- nth) links))))))
  8967. (if link
  8968. (let ((buf (current-buffer)))
  8969. (dolist (l link)
  8970. (org-open-link-from-string l in-emacs buf))
  8971. t)
  8972. nil)))
  8973. ;; Add special file links that specify the way of opening
  8974. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8975. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8976. (defun org-open-file-with-system (path)
  8977. "Open file at PATH using the system way of opening it."
  8978. (org-open-file path 'system))
  8979. (defun org-open-file-with-emacs (path)
  8980. "Open file at PATH in Emacs."
  8981. (org-open-file path 'emacs))
  8982. (defun org-remove-file-link-modifiers ()
  8983. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8984. (goto-char (point-min))
  8985. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8986. (org-if-unprotected
  8987. (replace-match "file:" t t))))
  8988. (eval-after-load "org-exp"
  8989. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8990. 'org-remove-file-link-modifiers))
  8991. ;;;; Time estimates
  8992. (defun org-get-effort (&optional pom)
  8993. "Get the effort estimate for the current entry."
  8994. (org-entry-get pom org-effort-property))
  8995. ;;; File search
  8996. (defvar org-create-file-search-functions nil
  8997. "List of functions to construct the right search string for a file link.
  8998. These functions are called in turn with point at the location to
  8999. which the link should point.
  9000. A function in the hook should first test if it would like to
  9001. handle this file type, for example by checking the `major-mode'
  9002. or the file extension. If it decides not to handle this file, it
  9003. should just return nil to give other functions a chance. If it
  9004. does handle the file, it must return the search string to be used
  9005. when following the link. The search string will be part of the
  9006. file link, given after a double colon, and `org-open-at-point'
  9007. will automatically search for it. If special measures must be
  9008. taken to make the search successful, another function should be
  9009. added to the companion hook `org-execute-file-search-functions',
  9010. which see.
  9011. A function in this hook may also use `setq' to set the variable
  9012. `description' to provide a suggestion for the descriptive text to
  9013. be used for this link when it gets inserted into an Org-mode
  9014. buffer with \\[org-insert-link].")
  9015. (defvar org-execute-file-search-functions nil
  9016. "List of functions to execute a file search triggered by a link.
  9017. Functions added to this hook must accept a single argument, the
  9018. search string that was part of the file link, the part after the
  9019. double colon. The function must first check if it would like to
  9020. handle this search, for example by checking the `major-mode' or
  9021. the file extension. If it decides not to handle this search, it
  9022. should just return nil to give other functions a chance. If it
  9023. does handle the search, it must return a non-nil value to keep
  9024. other functions from trying.
  9025. Each function can access the current prefix argument through the
  9026. variable `current-prefix-argument'. Note that a single prefix is
  9027. used to force opening a link in Emacs, so it may be good to only
  9028. use a numeric or double prefix to guide the search function.
  9029. In case this is needed, a function in this hook can also restore
  9030. the window configuration before `org-open-at-point' was called using:
  9031. (set-window-configuration org-window-config-before-follow-link)")
  9032. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9033. (defun org-link-search (s &optional type avoid-pos stealth)
  9034. "Search for a link search option.
  9035. If S is surrounded by forward slashes, it is interpreted as a
  9036. regular expression. In org-mode files, this will create an `org-occur'
  9037. sparse tree. In ordinary files, `occur' will be used to list matches.
  9038. If the current buffer is in `dired-mode', grep will be used to search
  9039. in all files. If AVOID-POS is given, ignore matches near that position.
  9040. When optional argument STEALTH is non-nil, do not modify
  9041. visibility around point, thus ignoring
  9042. `org-show-hierarchy-above', `org-show-following-heading' and
  9043. `org-show-siblings' variables."
  9044. (let ((case-fold-search t)
  9045. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9046. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9047. (append '(("") (" ") ("\t") ("\n"))
  9048. org-emphasis-alist)
  9049. "\\|") "\\)"))
  9050. (pos (point))
  9051. (pre nil) (post nil)
  9052. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9053. (cond
  9054. ;; First check if there are any special search functions
  9055. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9056. ;; Now try the builtin stuff
  9057. ((and (equal (string-to-char s0) ?#)
  9058. (> (length s0) 1)
  9059. (save-excursion
  9060. (goto-char (point-min))
  9061. (and
  9062. (re-search-forward
  9063. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9064. (setq type 'dedicated
  9065. pos (match-beginning 0))))
  9066. ;; There is an exact target for this
  9067. (goto-char pos)
  9068. (org-back-to-heading t)))
  9069. ((save-excursion
  9070. (goto-char (point-min))
  9071. (and
  9072. (re-search-forward
  9073. (concat "<<" (regexp-quote s0) ">>") nil t)
  9074. (setq type 'dedicated
  9075. pos (match-beginning 0))))
  9076. ;; There is an exact target for this
  9077. (goto-char pos))
  9078. ((save-excursion
  9079. (goto-char (point-min))
  9080. (and
  9081. (re-search-forward
  9082. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9083. (setq type 'dedicated pos (match-beginning 0))))
  9084. ;; Found an invisible target.
  9085. (goto-char pos))
  9086. ((save-excursion
  9087. (goto-char (point-min))
  9088. (and
  9089. (re-search-forward
  9090. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9091. (setq type 'dedicated pos (match-beginning 0))))
  9092. ;; Found an element with a matching #+name affiliated keyword.
  9093. (goto-char pos))
  9094. ((and (string-match "^(\\(.*\\))$" s0)
  9095. (save-excursion
  9096. (goto-char (point-min))
  9097. (and
  9098. (re-search-forward
  9099. (concat "[^[]" (regexp-quote
  9100. (format org-coderef-label-format
  9101. (match-string 1 s0))))
  9102. nil t)
  9103. (setq type 'dedicated
  9104. pos (1+ (match-beginning 0))))))
  9105. ;; There is a coderef target for this
  9106. (goto-char pos))
  9107. ((string-match "^/\\(.*\\)/$" s)
  9108. ;; A regular expression
  9109. (cond
  9110. ((derived-mode-p 'org-mode)
  9111. (org-occur (match-string 1 s)))
  9112. ;;((eq major-mode 'dired-mode)
  9113. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9114. (t (org-do-occur (match-string 1 s)))))
  9115. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9116. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9117. (goto-char (point-min))
  9118. (cond
  9119. ((let (case-fold-search)
  9120. (re-search-forward (format org-complex-heading-regexp-format
  9121. (regexp-quote s))
  9122. nil t))
  9123. ;; OK, found a match
  9124. (setq type 'dedicated)
  9125. (goto-char (match-beginning 0)))
  9126. ((and (not org-link-search-inhibit-query)
  9127. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9128. (y-or-n-p "No match - create this as a new heading? "))
  9129. (goto-char (point-max))
  9130. (or (bolp) (newline))
  9131. (insert "* " s "\n")
  9132. (beginning-of-line 0))
  9133. (t
  9134. (goto-char pos)
  9135. (error "No match"))))
  9136. (t
  9137. ;; A normal search string
  9138. (when (equal (string-to-char s) ?*)
  9139. ;; Anchor on headlines, post may include tags.
  9140. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9141. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9142. s (substring s 1)))
  9143. (remove-text-properties
  9144. 0 (length s)
  9145. '(face nil mouse-face nil keymap nil fontified nil) s)
  9146. ;; Make a series of regular expressions to find a match
  9147. (setq words (org-split-string s "[ \n\r\t]+")
  9148. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9149. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9150. "\\)" markers)
  9151. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9152. re2a (concat "[ \t\r\n]" re2a_)
  9153. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9154. re4 (concat "[^a-zA-Z_]" re4_)
  9155. re1 (concat pre re2 post)
  9156. re3 (concat pre (if pre re4_ re4) post)
  9157. re5 (concat pre ".*" re4)
  9158. re2 (concat pre re2)
  9159. re2a (concat pre (if pre re2a_ re2a))
  9160. re4 (concat pre (if pre re4_ re4))
  9161. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9162. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9163. re5 "\\)"
  9164. ))
  9165. (cond
  9166. ((eq type 'org-occur) (org-occur reall))
  9167. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9168. (t (goto-char (point-min))
  9169. (setq type 'fuzzy)
  9170. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9171. (org-search-not-self 1 re1 nil t)
  9172. (org-search-not-self 1 re2 nil t)
  9173. (org-search-not-self 1 re2a nil t)
  9174. (org-search-not-self 1 re3 nil t)
  9175. (org-search-not-self 1 re4 nil t)
  9176. (org-search-not-self 1 re5 nil t)
  9177. )
  9178. (goto-char (match-beginning 1))
  9179. (goto-char pos)
  9180. (error "No match"))))))
  9181. (and (derived-mode-p 'org-mode)
  9182. (not stealth)
  9183. (org-show-context 'link-search))
  9184. type))
  9185. (defun org-search-not-self (group &rest args)
  9186. "Execute `re-search-forward', but only accept matches that do not
  9187. enclose the position of `org-open-link-marker'."
  9188. (let ((m org-open-link-marker))
  9189. (catch 'exit
  9190. (while (apply 're-search-forward args)
  9191. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9192. (goto-char (match-end group))
  9193. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9194. (> (match-beginning 0) (marker-position m))
  9195. (< (match-end 0) (marker-position m)))
  9196. (save-match-data
  9197. (or (not (org-in-regexp
  9198. org-bracket-link-analytic-regexp 1))
  9199. (not (match-end 4)) ; no description
  9200. (and (<= (match-beginning 4) (point))
  9201. (>= (match-end 4) (point))))))
  9202. (throw 'exit (point))))))))
  9203. (defun org-get-buffer-for-internal-link (buffer)
  9204. "Return a buffer to be used for displaying the link target of internal links."
  9205. (cond
  9206. ((not org-display-internal-link-with-indirect-buffer)
  9207. buffer)
  9208. ((string-match "(Clone)$" (buffer-name buffer))
  9209. (message "Buffer is already a clone, not making another one")
  9210. ;; we also do not modify visibility in this case
  9211. buffer)
  9212. (t ; make a new indirect buffer for displaying the link
  9213. (let* ((bn (buffer-name buffer))
  9214. (ibn (concat bn "(Clone)"))
  9215. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9216. (with-current-buffer ib (org-overview))
  9217. ib))))
  9218. (defun org-do-occur (regexp &optional cleanup)
  9219. "Call the Emacs command `occur'.
  9220. If CLEANUP is non-nil, remove the printout of the regular expression
  9221. in the *Occur* buffer. This is useful if the regex is long and not useful
  9222. to read."
  9223. (occur regexp)
  9224. (when cleanup
  9225. (let ((cwin (selected-window)) win beg end)
  9226. (when (setq win (get-buffer-window "*Occur*"))
  9227. (select-window win))
  9228. (goto-char (point-min))
  9229. (when (re-search-forward "match[a-z]+" nil t)
  9230. (setq beg (match-end 0))
  9231. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9232. (setq end (1- (match-beginning 0)))))
  9233. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9234. (goto-char (point-min))
  9235. (select-window cwin))))
  9236. ;;; The mark ring for links jumps
  9237. (defvar org-mark-ring nil
  9238. "Mark ring for positions before jumps in Org-mode.")
  9239. (defvar org-mark-ring-last-goto nil
  9240. "Last position in the mark ring used to go back.")
  9241. ;; Fill and close the ring
  9242. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9243. (loop for i from 1 to org-mark-ring-length do
  9244. (push (make-marker) org-mark-ring))
  9245. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9246. org-mark-ring)
  9247. (defun org-mark-ring-push (&optional pos buffer)
  9248. "Put the current position or POS into the mark ring and rotate it."
  9249. (interactive)
  9250. (setq pos (or pos (point)))
  9251. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9252. (move-marker (car org-mark-ring)
  9253. (or pos (point))
  9254. (or buffer (current-buffer)))
  9255. (message "%s"
  9256. (substitute-command-keys
  9257. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9258. (defun org-mark-ring-goto (&optional n)
  9259. "Jump to the previous position in the mark ring.
  9260. With prefix arg N, jump back that many stored positions. When
  9261. called several times in succession, walk through the entire ring.
  9262. Org-mode commands jumping to a different position in the current file,
  9263. or to another Org-mode file, automatically push the old position
  9264. onto the ring."
  9265. (interactive "p")
  9266. (let (p m)
  9267. (if (eq last-command this-command)
  9268. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9269. (setq p org-mark-ring))
  9270. (setq org-mark-ring-last-goto p)
  9271. (setq m (car p))
  9272. (org-pop-to-buffer-same-window (marker-buffer m))
  9273. (goto-char m)
  9274. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9275. (defun org-remove-angle-brackets (s)
  9276. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9277. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9278. s)
  9279. (defun org-add-angle-brackets (s)
  9280. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9281. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9282. s)
  9283. (defun org-remove-double-quotes (s)
  9284. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9285. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9286. s)
  9287. ;;; Following specific links
  9288. (defun org-follow-timestamp-link ()
  9289. "Open an agenda view for the time-stamp date/range at point."
  9290. (cond
  9291. ((org-at-date-range-p t)
  9292. (let ((org-agenda-start-on-weekday)
  9293. (t1 (match-string 1))
  9294. (t2 (match-string 2)) tt1 tt2)
  9295. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9296. tt2 (time-to-days (org-time-string-to-time t2)))
  9297. (let ((org-agenda-buffer-tmp-name
  9298. (format "*Org Agenda(a:%s)"
  9299. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9300. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9301. ((org-at-timestamp-p t)
  9302. (let ((org-agenda-buffer-tmp-name
  9303. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9304. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9305. (substring (match-string 1) 0 10)))
  9306. 1)))
  9307. (t (error "This should not happen"))))
  9308. ;;; Following file links
  9309. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9310. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9311. (declare-function mailcap-mime-info
  9312. "mailcap" (string &optional request no-decode))
  9313. (defvar org-wait nil)
  9314. (defun org-open-file (path &optional in-emacs line search)
  9315. "Open the file at PATH.
  9316. First, this expands any special file name abbreviations. Then the
  9317. configuration variable `org-file-apps' is checked if it contains an
  9318. entry for this file type, and if yes, the corresponding command is launched.
  9319. If no application is found, Emacs simply visits the file.
  9320. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9321. With a double \\[universal-argument] \\[universal-argument] \
  9322. prefix arg, Org tries to avoid opening in Emacs
  9323. and to use an external application to visit the file.
  9324. Optional LINE specifies a line to go to, optional SEARCH a string
  9325. to search for. If LINE or SEARCH is given, the file will be
  9326. opened in Emacs, unless an entry from org-file-apps that makes
  9327. use of groups in a regexp matches.
  9328. If you want to change the way frames are used when following a
  9329. link, please customize `org-link-frame-setup'.
  9330. If the file does not exist, an error is thrown."
  9331. (let* ((file (if (equal path "")
  9332. buffer-file-name
  9333. (substitute-in-file-name (expand-file-name path))))
  9334. (file-apps (append org-file-apps (org-default-apps)))
  9335. (apps (org-remove-if
  9336. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9337. (apps-dlink (org-remove-if-not
  9338. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9339. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9340. (dirp (if remp nil (file-directory-p file)))
  9341. (file (if (and dirp org-open-directory-means-index-dot-org)
  9342. (concat (file-name-as-directory file) "index.org")
  9343. file))
  9344. (a-m-a-p (assq 'auto-mode apps))
  9345. (dfile (downcase file))
  9346. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9347. (link (cond ((and (eq line nil)
  9348. (eq search nil))
  9349. file)
  9350. (line
  9351. (concat file "::" (number-to-string line)))
  9352. (search
  9353. (concat file "::" search))))
  9354. (dlink (downcase link))
  9355. (old-buffer (current-buffer))
  9356. (old-pos (point))
  9357. (old-mode major-mode)
  9358. ext cmd link-match-data)
  9359. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9360. (setq ext (match-string 1 dfile))
  9361. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9362. (setq ext (match-string 1 dfile))))
  9363. (cond
  9364. ((member in-emacs '((16) system))
  9365. (setq cmd (cdr (assoc 'system apps))))
  9366. (in-emacs (setq cmd 'emacs))
  9367. (t
  9368. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9369. (and dirp (cdr (assoc 'directory apps)))
  9370. ; first, try matching against apps-dlink
  9371. ; if we get a match here, store the match data for later
  9372. (let ((match (assoc-default dlink apps-dlink
  9373. 'string-match)))
  9374. (if match
  9375. (progn (setq link-match-data (match-data))
  9376. match)
  9377. (progn (setq in-emacs (or in-emacs line search))
  9378. nil))) ; if we have no match in apps-dlink,
  9379. ; always open the file in emacs if line or search
  9380. ; is given (for backwards compatibility)
  9381. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9382. 'string-match)
  9383. (cdr (assoc ext apps))
  9384. (cdr (assoc t apps))))))
  9385. (when (eq cmd 'system)
  9386. (setq cmd (cdr (assoc 'system apps))))
  9387. (when (eq cmd 'default)
  9388. (setq cmd (cdr (assoc t apps))))
  9389. (when (eq cmd 'mailcap)
  9390. (require 'mailcap)
  9391. (mailcap-parse-mailcaps)
  9392. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9393. (command (mailcap-mime-info mime-type)))
  9394. (if (stringp command)
  9395. (setq cmd command)
  9396. (setq cmd 'emacs))))
  9397. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9398. (not (file-exists-p file))
  9399. (not org-open-non-existing-files))
  9400. (error "No such file: %s" file))
  9401. (cond
  9402. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9403. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9404. (while (string-match "['\"]%s['\"]" cmd)
  9405. (setq cmd (replace-match "%s" t t cmd)))
  9406. (while (string-match "%s" cmd)
  9407. (setq cmd (replace-match
  9408. (save-match-data
  9409. (shell-quote-argument
  9410. (convert-standard-filename file)))
  9411. t t cmd)))
  9412. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9413. (save-match-data
  9414. (let ((match-index 1)
  9415. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9416. (set-match-data link-match-data)
  9417. (while (<= match-index number-of-groups)
  9418. (let ((regex (concat "%" (number-to-string match-index)))
  9419. (replace-with (match-string match-index dlink)))
  9420. (while (string-match regex cmd)
  9421. (setq cmd (replace-match replace-with t t cmd))))
  9422. (setq match-index (+ match-index 1)))))
  9423. (save-window-excursion
  9424. (message "Running %s...done" cmd)
  9425. (start-process-shell-command cmd nil cmd)
  9426. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9427. ((or (stringp cmd)
  9428. (eq cmd 'emacs))
  9429. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9430. (widen)
  9431. (if line (org-goto-line line)
  9432. (if search (org-link-search search))))
  9433. ((consp cmd)
  9434. (let ((file (convert-standard-filename file)))
  9435. (save-match-data
  9436. (set-match-data link-match-data)
  9437. (eval cmd))))
  9438. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9439. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9440. (or (not (equal old-buffer (current-buffer)))
  9441. (not (equal old-pos (point))))
  9442. (org-mark-ring-push old-pos old-buffer))))
  9443. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9444. "This function returns non-nil if `entry' uses a regular
  9445. expression which should be matched against the whole link by
  9446. org-open-file.
  9447. It assumes that is the case when the entry uses a regular
  9448. expression which has at least one grouping construct and the
  9449. action is either a lisp form or a command string containing
  9450. '%1', i.e. using at least one subexpression match as a
  9451. parameter."
  9452. (let ((selector (car entry))
  9453. (action (cdr entry)))
  9454. (if (stringp selector)
  9455. (and (> (regexp-opt-depth selector) 0)
  9456. (or (and (stringp action)
  9457. (string-match "%[0-9]" action))
  9458. (consp action)))
  9459. nil)))
  9460. (defun org-default-apps ()
  9461. "Return the default applications for this operating system."
  9462. (cond
  9463. ((eq system-type 'darwin)
  9464. org-file-apps-defaults-macosx)
  9465. ((eq system-type 'windows-nt)
  9466. org-file-apps-defaults-windowsnt)
  9467. (t org-file-apps-defaults-gnu)))
  9468. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9469. "Convert extensions to regular expressions in the cars of LIST.
  9470. Also, weed out any non-string entries, because the return value is used
  9471. only for regexp matching.
  9472. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9473. point to the symbol `emacs', indicating that the file should
  9474. be opened in Emacs."
  9475. (append
  9476. (delq nil
  9477. (mapcar (lambda (x)
  9478. (if (not (stringp (car x)))
  9479. nil
  9480. (if (string-match "\\W" (car x))
  9481. x
  9482. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9483. list))
  9484. (if add-auto-mode
  9485. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9486. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9487. (defun org-file-remote-p (file)
  9488. "Test whether FILE specifies a location on a remote system.
  9489. Return non-nil if the location is indeed remote.
  9490. For example, the filename \"/user@host:/foo\" specifies a location
  9491. on the system \"/user@host:\"."
  9492. (cond ((fboundp 'file-remote-p)
  9493. (file-remote-p file))
  9494. ((fboundp 'tramp-handle-file-remote-p)
  9495. (tramp-handle-file-remote-p file))
  9496. ((and (boundp 'ange-ftp-name-format)
  9497. (string-match (car ange-ftp-name-format) file))
  9498. t)))
  9499. ;;;; Refiling
  9500. (defun org-get-org-file ()
  9501. "Read a filename, with default directory `org-directory'."
  9502. (let ((default (or org-default-notes-file remember-data-file)))
  9503. (read-file-name (format "File name [%s]: " default)
  9504. (file-name-as-directory org-directory)
  9505. default)))
  9506. (defun org-notes-order-reversed-p ()
  9507. "Check if the current file should receive notes in reversed order."
  9508. (cond
  9509. ((not org-reverse-note-order) nil)
  9510. ((eq t org-reverse-note-order) t)
  9511. ((not (listp org-reverse-note-order)) nil)
  9512. (t (catch 'exit
  9513. (let ((all org-reverse-note-order)
  9514. entry)
  9515. (while (setq entry (pop all))
  9516. (if (string-match (car entry) buffer-file-name)
  9517. (throw 'exit (cdr entry))))
  9518. nil)))))
  9519. (defvar org-refile-target-table nil
  9520. "The list of refile targets, created by `org-refile'.")
  9521. (defvar org-agenda-new-buffers nil
  9522. "Buffers created to visit agenda files.")
  9523. (defvar org-refile-cache nil
  9524. "Cache for refile targets.")
  9525. (defvar org-refile-markers nil
  9526. "All the markers used for caching refile locations.")
  9527. (defun org-refile-marker (pos)
  9528. "Get a new refile marker, but only if caching is in use."
  9529. (if (not org-refile-use-cache)
  9530. pos
  9531. (let ((m (make-marker)))
  9532. (move-marker m pos)
  9533. (push m org-refile-markers)
  9534. m)))
  9535. (defun org-refile-cache-clear ()
  9536. "Clear the refile cache and disable all the markers."
  9537. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9538. (setq org-refile-markers nil)
  9539. (setq org-refile-cache nil)
  9540. (message "Refile cache has been cleared"))
  9541. (defun org-refile-cache-check-set (set)
  9542. "Check if all the markers in the cache still have live buffers."
  9543. (let (marker)
  9544. (catch 'exit
  9545. (while (and set (setq marker (nth 3 (pop set))))
  9546. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9547. (when (and marker (null (marker-buffer marker)))
  9548. (message "not found") (sit-for 3)
  9549. (throw 'exit nil)))
  9550. t)))
  9551. (defun org-refile-cache-put (set &rest identifiers)
  9552. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9553. (let* ((key (sha1 (prin1-to-string identifiers)))
  9554. (entry (assoc key org-refile-cache)))
  9555. (if entry
  9556. (setcdr entry set)
  9557. (push (cons key set) org-refile-cache))))
  9558. (defun org-refile-cache-get (&rest identifiers)
  9559. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9560. (cond
  9561. ((not org-refile-cache) nil)
  9562. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9563. (t
  9564. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9565. org-refile-cache))))
  9566. (and set (org-refile-cache-check-set set) set)))))
  9567. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9568. "Produce a table with refile targets."
  9569. (let ((case-fold-search nil)
  9570. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9571. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9572. targets tgs txt re files f desc descre fast-path-p level pos0)
  9573. (message "Getting targets...")
  9574. (with-current-buffer (or default-buffer (current-buffer))
  9575. (while (setq entry (pop entries))
  9576. (setq files (car entry) desc (cdr entry))
  9577. (setq fast-path-p nil)
  9578. (cond
  9579. ((null files) (setq files (list (current-buffer))))
  9580. ((eq files 'org-agenda-files)
  9581. (setq files (org-agenda-files 'unrestricted)))
  9582. ((and (symbolp files) (fboundp files))
  9583. (setq files (funcall files)))
  9584. ((and (symbolp files) (boundp files))
  9585. (setq files (symbol-value files))))
  9586. (if (stringp files) (setq files (list files)))
  9587. (cond
  9588. ((eq (car desc) :tag)
  9589. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9590. ((eq (car desc) :todo)
  9591. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9592. ((eq (car desc) :regexp)
  9593. (setq descre (cdr desc)))
  9594. ((eq (car desc) :level)
  9595. (setq descre (concat "^\\*\\{" (number-to-string
  9596. (if org-odd-levels-only
  9597. (1- (* 2 (cdr desc)))
  9598. (cdr desc)))
  9599. "\\}[ \t]")))
  9600. ((eq (car desc) :maxlevel)
  9601. (setq fast-path-p t)
  9602. (setq descre (concat "^\\*\\{1," (number-to-string
  9603. (if org-odd-levels-only
  9604. (1- (* 2 (cdr desc)))
  9605. (cdr desc)))
  9606. "\\}[ \t]")))
  9607. (t (error "Bad refiling target description %s" desc)))
  9608. (while (setq f (pop files))
  9609. (with-current-buffer
  9610. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9611. (or
  9612. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9613. (progn
  9614. (if (bufferp f) (setq f (buffer-file-name
  9615. (buffer-base-buffer f))))
  9616. (setq f (and f (expand-file-name f)))
  9617. (if (eq org-refile-use-outline-path 'file)
  9618. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9619. (save-excursion
  9620. (save-restriction
  9621. (widen)
  9622. (goto-char (point-min))
  9623. (while (re-search-forward descre nil t)
  9624. (goto-char (setq pos0 (point-at-bol)))
  9625. (catch 'next
  9626. (when org-refile-target-verify-function
  9627. (save-match-data
  9628. (or (funcall org-refile-target-verify-function)
  9629. (throw 'next t))))
  9630. (when (and (looking-at org-complex-heading-regexp)
  9631. (not (member (match-string 4) excluded-entries))
  9632. (match-string 4))
  9633. (setq level (org-reduced-level
  9634. (- (match-end 1) (match-beginning 1)))
  9635. txt (org-link-display-format (match-string 4))
  9636. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9637. re (format org-complex-heading-regexp-format
  9638. (regexp-quote (match-string 4))))
  9639. (when org-refile-use-outline-path
  9640. (setq txt (mapconcat
  9641. 'org-protect-slash
  9642. (append
  9643. (if (eq org-refile-use-outline-path
  9644. 'file)
  9645. (list (file-name-nondirectory
  9646. (buffer-file-name
  9647. (buffer-base-buffer))))
  9648. (if (eq org-refile-use-outline-path
  9649. 'full-file-path)
  9650. (list (buffer-file-name
  9651. (buffer-base-buffer)))))
  9652. (org-get-outline-path fast-path-p
  9653. level txt)
  9654. (list txt))
  9655. "/")))
  9656. (push (list txt f re (org-refile-marker (point)))
  9657. tgs)))
  9658. (when (= (point) pos0)
  9659. ;; verification function has not moved point
  9660. (goto-char (point-at-eol))))))))
  9661. (when org-refile-use-cache
  9662. (org-refile-cache-put tgs (buffer-file-name) descre))
  9663. (setq targets (append tgs targets))
  9664. ))))
  9665. (message "Getting targets...done")
  9666. (nreverse targets)))
  9667. (defun org-protect-slash (s)
  9668. (while (string-match "/" s)
  9669. (setq s (replace-match "\\" t t s)))
  9670. s)
  9671. (defvar org-olpa (make-vector 20 nil))
  9672. (defun org-get-outline-path (&optional fastp level heading)
  9673. "Return the outline path to the current entry, as a list.
  9674. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9675. routine which makes outline path derivations for an entire file,
  9676. avoiding backtracing. Refile target collection makes use of that."
  9677. (if fastp
  9678. (progn
  9679. (if (> level 19)
  9680. (error "Outline path failure, more than 19 levels"))
  9681. (loop for i from level upto 19 do
  9682. (aset org-olpa i nil))
  9683. (prog1
  9684. (delq nil (append org-olpa nil))
  9685. (aset org-olpa level heading)))
  9686. (let (rtn case-fold-search)
  9687. (save-excursion
  9688. (save-restriction
  9689. (widen)
  9690. (while (org-up-heading-safe)
  9691. (when (looking-at org-complex-heading-regexp)
  9692. (push (org-match-string-no-properties 4) rtn)))
  9693. rtn)))))
  9694. (defun org-format-outline-path (path &optional width prefix separator)
  9695. "Format the outline path PATH for display.
  9696. WIDTH is the maximum number of characters that is available.
  9697. PREFIX is a prefix to be included in the returned string,
  9698. such as the file name.
  9699. SEPARATOR is inserted between the different parts of the path,
  9700. the default is \"/\"."
  9701. (setq width (or width 79))
  9702. (if prefix (setq width (- width (length prefix))))
  9703. (if (not path)
  9704. (or prefix "")
  9705. (let* ((nsteps (length path))
  9706. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9707. (maxwidth (if (<= total-width width)
  9708. 10000 ;; everything fits
  9709. ;; we need to shorten the level headings
  9710. (/ (- width nsteps) nsteps)))
  9711. (org-odd-levels-only nil)
  9712. (n 0)
  9713. (total (1+ (length prefix))))
  9714. (setq maxwidth (max maxwidth 10))
  9715. (concat prefix
  9716. (if prefix (or separator "/"))
  9717. (mapconcat
  9718. (lambda (h)
  9719. (setq n (1+ n))
  9720. (if (and (= n nsteps) (< maxwidth 10000))
  9721. (setq maxwidth (- total-width total)))
  9722. (if (< (length h) maxwidth)
  9723. (progn (setq total (+ total (length h) 1)) h)
  9724. (setq h (substring h 0 (- maxwidth 2))
  9725. total (+ total maxwidth 1))
  9726. (if (string-match "[ \t]+\\'" h)
  9727. (setq h (substring h 0 (match-beginning 0))))
  9728. (setq h (concat h "..")))
  9729. (org-add-props h nil 'face
  9730. (nth (% (1- n) org-n-level-faces)
  9731. org-level-faces))
  9732. h)
  9733. path (or separator "/"))))))
  9734. (defun org-display-outline-path (&optional file current separator just-return-string)
  9735. "Display the current outline path in the echo area.
  9736. If FILE is non-nil, prepend the output with the file name.
  9737. If CURRENT is non-nil, append the current heading to the output.
  9738. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9739. the different parts of the path and defaults to \"/\".
  9740. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9741. (interactive "P")
  9742. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9743. (case-fold-search nil)
  9744. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9745. res)
  9746. (if current (setq path (append path
  9747. (save-excursion
  9748. (org-back-to-heading t)
  9749. (if (looking-at org-complex-heading-regexp)
  9750. (list (match-string 4)))))))
  9751. (setq res
  9752. (org-format-outline-path
  9753. path
  9754. (1- (frame-width))
  9755. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9756. separator))
  9757. (if just-return-string
  9758. (org-no-properties res)
  9759. (message "%s" res))))
  9760. (defvar org-refile-history nil
  9761. "History for refiling operations.")
  9762. (defvar org-after-refile-insert-hook nil
  9763. "Hook run after `org-refile' has inserted its stuff at the new location.
  9764. Note that this is still *before* the stuff will be removed from
  9765. the *old* location.")
  9766. (defvar org-capture-last-stored-marker)
  9767. (defvar org-refile-keep nil
  9768. "Non-nil means `org-refile' will copy instead of refile.")
  9769. (defun org-copy ()
  9770. "Like `org-refile', but copy."
  9771. (interactive)
  9772. (let ((org-refile-keep t))
  9773. (funcall 'org-refile nil nil nil "Copy")))
  9774. (defun org-refile (&optional goto default-buffer rfloc msg)
  9775. "Move the entry or entries at point to another heading.
  9776. The list of target headings is compiled using the information in
  9777. `org-refile-targets', which see.
  9778. At the target location, the entry is filed as a subitem of the target
  9779. heading. Depending on `org-reverse-note-order', the new subitem will
  9780. either be the first or the last subitem.
  9781. If there is an active region, all entries in that region will be moved.
  9782. However, the region must fulfill the requirement that the first heading
  9783. is the first one sets the top-level of the moved text - at most siblings
  9784. below it are allowed.
  9785. With prefix arg GOTO, the command will only visit the target location
  9786. and not actually move anything.
  9787. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9788. go to the location where the last refiling operation has put the subtree.
  9789. With a prefix argument of `2', refile to the running clock.
  9790. RFLOC can be a refile location obtained in a different way.
  9791. MSG is a string to replace \"Refile\" in the default prompt with
  9792. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9793. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9794. If you are using target caching (see `org-refile-use-cache'),
  9795. you have to clear the target cache in order to find new targets.
  9796. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9797. prefix argument (`C-u C-u C-u C-c C-w')."
  9798. (interactive "P")
  9799. (if (member goto '(0 (64)))
  9800. (org-refile-cache-clear)
  9801. (let* ((actionmsg (or msg "Refile"))
  9802. (cbuf (current-buffer))
  9803. (regionp (org-region-active-p))
  9804. (region-start (and regionp (region-beginning)))
  9805. (region-end (and regionp (region-end)))
  9806. (region-length (and regionp (- region-end region-start)))
  9807. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9808. pos it nbuf file re level reversed)
  9809. (setq last-command nil)
  9810. (when regionp
  9811. (goto-char region-start)
  9812. (or (bolp) (goto-char (point-at-bol)))
  9813. (setq region-start (point))
  9814. (unless (or (org-kill-is-subtree-p
  9815. (buffer-substring region-start region-end))
  9816. (prog1 org-refile-active-region-within-subtree
  9817. (org-toggle-heading)))
  9818. (error "The region is not a (sequence of) subtree(s)")))
  9819. (if (equal goto '(16))
  9820. (org-refile-goto-last-stored)
  9821. (when (or
  9822. (and (equal goto 2)
  9823. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9824. (prog1
  9825. (setq it (list (or org-clock-heading "running clock")
  9826. (buffer-file-name
  9827. (marker-buffer org-clock-hd-marker))
  9828. ""
  9829. (marker-position org-clock-hd-marker)))
  9830. (setq goto nil)))
  9831. (setq it (or rfloc
  9832. (let (heading-text)
  9833. (save-excursion
  9834. (unless goto
  9835. (org-back-to-heading t)
  9836. (setq heading-text
  9837. (nth 4 (org-heading-components))))
  9838. (org-refile-get-location
  9839. (cond (goto "Goto")
  9840. (regionp (concat actionmsg " region to"))
  9841. (t (concat actionmsg " subtree \""
  9842. heading-text "\" to")))
  9843. default-buffer
  9844. (and (not (equal '(4) goto))
  9845. org-refile-allow-creating-parent-nodes)
  9846. goto))))))
  9847. (setq file (nth 1 it)
  9848. re (nth 2 it)
  9849. pos (nth 3 it))
  9850. (if (and (not goto)
  9851. pos
  9852. (equal (buffer-file-name) file)
  9853. (if regionp
  9854. (and (>= pos region-start)
  9855. (<= pos region-end))
  9856. (and (>= pos (point))
  9857. (< pos (save-excursion
  9858. (org-end-of-subtree t t))))))
  9859. (error "Cannot refile to position inside the tree or region"))
  9860. (setq nbuf (or (find-buffer-visiting file)
  9861. (find-file-noselect file)))
  9862. (if goto
  9863. (progn
  9864. (org-pop-to-buffer-same-window nbuf)
  9865. (goto-char pos)
  9866. (org-show-context 'org-goto))
  9867. (if regionp
  9868. (progn
  9869. (org-kill-new (buffer-substring region-start region-end))
  9870. (org-save-markers-in-region region-start region-end))
  9871. (org-copy-subtree 1 nil t))
  9872. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9873. (find-file-noselect file)))
  9874. (setq reversed (org-notes-order-reversed-p))
  9875. (save-excursion
  9876. (save-restriction
  9877. (widen)
  9878. (if pos
  9879. (progn
  9880. (goto-char pos)
  9881. (looking-at org-outline-regexp)
  9882. (setq level (org-get-valid-level (funcall outline-level) 1))
  9883. (goto-char
  9884. (if reversed
  9885. (or (outline-next-heading) (point-max))
  9886. (or (save-excursion (org-get-next-sibling))
  9887. (org-end-of-subtree t t)
  9888. (point-max)))))
  9889. (setq level 1)
  9890. (if (not reversed)
  9891. (goto-char (point-max))
  9892. (goto-char (point-min))
  9893. (or (outline-next-heading) (goto-char (point-max)))))
  9894. (if (not (bolp)) (newline))
  9895. (org-paste-subtree level)
  9896. (when org-log-refile
  9897. (org-add-log-setup 'refile nil nil 'findpos
  9898. org-log-refile)
  9899. (unless (eq org-log-refile 'note)
  9900. (save-excursion (org-add-log-note))))
  9901. (and org-auto-align-tags
  9902. (let ((org-loop-over-headlines-in-active-region nil))
  9903. (org-set-tags nil t)))
  9904. (bookmark-set "org-refile-last-stored")
  9905. ;; If we are refiling for capture, make sure that the
  9906. ;; last-capture pointers point here
  9907. (when (org-bound-and-true-p org-refile-for-capture)
  9908. (bookmark-set "org-capture-last-stored-marker")
  9909. (move-marker org-capture-last-stored-marker (point)))
  9910. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9911. (run-hooks 'org-after-refile-insert-hook))))
  9912. (unless org-refile-keep
  9913. (if regionp
  9914. (delete-region (point) (+ (point) region-length))
  9915. (org-cut-subtree)))
  9916. (when (featurep 'org-inlinetask)
  9917. (org-inlinetask-remove-END-maybe))
  9918. (setq org-markers-to-move nil)
  9919. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  9920. (defun org-refile-goto-last-stored ()
  9921. "Go to the location where the last refile was stored."
  9922. (interactive)
  9923. (bookmark-jump "org-refile-last-stored")
  9924. (message "This is the location of the last refile"))
  9925. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9926. no-exclude)
  9927. "Prompt the user for a refile location, using PROMPT.
  9928. PROMPT should not be suffixed with a colon and a space, because
  9929. this function appends the default value from
  9930. `org-refile-history' automatically, if that is not empty.
  9931. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9932. this is used for the GOTO interface."
  9933. (let ((org-refile-targets org-refile-targets)
  9934. (org-refile-use-outline-path org-refile-use-outline-path)
  9935. excluded-entries)
  9936. (when (and (derived-mode-p 'org-mode)
  9937. (not org-refile-use-cache)
  9938. (not no-exclude))
  9939. (org-map-tree
  9940. (lambda()
  9941. (setq excluded-entries
  9942. (append excluded-entries (list (org-get-heading t t)))))))
  9943. (setq org-refile-target-table
  9944. (org-refile-get-targets default-buffer excluded-entries)))
  9945. (unless org-refile-target-table
  9946. (error "No refile targets"))
  9947. (let* ((cbuf (current-buffer))
  9948. (partial-completion-mode nil)
  9949. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9950. (cfunc (if (and org-refile-use-outline-path
  9951. org-outline-path-complete-in-steps)
  9952. 'org-olpath-completing-read
  9953. 'org-icompleting-read))
  9954. (extra (if org-refile-use-outline-path "/" ""))
  9955. (cbnex (concat (buffer-name) extra))
  9956. (filename (and cfn (expand-file-name cfn)))
  9957. (tbl (mapcar
  9958. (lambda (x)
  9959. (if (and (not (member org-refile-use-outline-path
  9960. '(file full-file-path)))
  9961. (not (equal filename (nth 1 x))))
  9962. (cons (concat (car x) extra " ("
  9963. (file-name-nondirectory (nth 1 x)) ")")
  9964. (cdr x))
  9965. (cons (concat (car x) extra) (cdr x))))
  9966. org-refile-target-table))
  9967. (completion-ignore-case t)
  9968. cdef
  9969. (prompt (concat prompt
  9970. (or (and (car org-refile-history)
  9971. (concat " (default " (car org-refile-history) ")"))
  9972. (and (assoc cbnex tbl) (setq cdef cbnex)
  9973. (concat " (default " cbnex ")"))) ": "))
  9974. pa answ parent-target child parent old-hist)
  9975. (setq old-hist org-refile-history)
  9976. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9977. nil 'org-refile-history (or cdef (car org-refile-history))))
  9978. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9979. (org-refile-check-position pa)
  9980. (if pa
  9981. (progn
  9982. (when (or (not org-refile-history)
  9983. (not (eq old-hist org-refile-history))
  9984. (not (equal (car pa) (car org-refile-history))))
  9985. (setq org-refile-history
  9986. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9987. org-refile-history
  9988. (cdr org-refile-history))))
  9989. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9990. (pop org-refile-history)))
  9991. pa)
  9992. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9993. (progn
  9994. (setq parent (match-string 1 answ)
  9995. child (match-string 2 answ))
  9996. (setq parent-target (or (assoc parent tbl)
  9997. (assoc (concat parent "/") tbl)))
  9998. (when (and parent-target
  9999. (or (eq new-nodes t)
  10000. (and (eq new-nodes 'confirm)
  10001. (y-or-n-p (format "Create new node \"%s\"? "
  10002. child)))))
  10003. (org-refile-new-child parent-target child)))
  10004. (error "Invalid target location")))))
  10005. (declare-function org-string-nw-p "org-macs" (s))
  10006. (defun org-refile-check-position (refile-pointer)
  10007. "Check if the refile pointer matches the readline to which it points."
  10008. (let* ((file (nth 1 refile-pointer))
  10009. (re (nth 2 refile-pointer))
  10010. (pos (nth 3 refile-pointer))
  10011. buffer)
  10012. (when (org-string-nw-p re)
  10013. (setq buffer (if (markerp pos)
  10014. (marker-buffer pos)
  10015. (or (find-buffer-visiting file)
  10016. (find-file-noselect file))))
  10017. (with-current-buffer buffer
  10018. (save-excursion
  10019. (save-restriction
  10020. (widen)
  10021. (goto-char pos)
  10022. (beginning-of-line 1)
  10023. (unless (org-looking-at-p re)
  10024. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  10025. (defun org-refile-new-child (parent-target child)
  10026. "Use refile target PARENT-TARGET to add new CHILD below it."
  10027. (unless parent-target
  10028. (error "Cannot find parent for new node"))
  10029. (let ((file (nth 1 parent-target))
  10030. (pos (nth 3 parent-target))
  10031. level)
  10032. (with-current-buffer (or (find-buffer-visiting file)
  10033. (find-file-noselect file))
  10034. (save-excursion
  10035. (save-restriction
  10036. (widen)
  10037. (if pos
  10038. (goto-char pos)
  10039. (goto-char (point-max))
  10040. (if (not (bolp)) (newline)))
  10041. (when (looking-at org-outline-regexp)
  10042. (setq level (funcall outline-level))
  10043. (org-end-of-subtree t t))
  10044. (org-back-over-empty-lines)
  10045. (insert "\n" (make-string
  10046. (if pos (org-get-valid-level level 1) 1) ?*)
  10047. " " child "\n")
  10048. (beginning-of-line 0)
  10049. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10050. (defun org-olpath-completing-read (prompt collection &rest args)
  10051. "Read an outline path like a file name."
  10052. (let ((thetable collection)
  10053. (org-completion-use-ido nil) ; does not work with ido.
  10054. (org-completion-use-iswitchb nil)) ; or iswitchb
  10055. (apply
  10056. 'org-icompleting-read prompt
  10057. (lambda (string predicate &optional flag)
  10058. (let (rtn r f (l (length string)))
  10059. (cond
  10060. ((eq flag nil)
  10061. ;; try completion
  10062. (try-completion string thetable))
  10063. ((eq flag t)
  10064. ;; all-completions
  10065. (setq rtn (all-completions string thetable predicate))
  10066. (mapcar
  10067. (lambda (x)
  10068. (setq r (substring x l))
  10069. (if (string-match " ([^)]*)$" x)
  10070. (setq f (match-string 0 x))
  10071. (setq f ""))
  10072. (if (string-match "/" r)
  10073. (concat string (substring r 0 (match-end 0)) f)
  10074. x))
  10075. rtn))
  10076. ((eq flag 'lambda)
  10077. ;; exact match?
  10078. (assoc string thetable)))))
  10079. args)))
  10080. ;;;; Dynamic blocks
  10081. (defun org-find-dblock (name)
  10082. "Find the first dynamic block with name NAME in the buffer.
  10083. If not found, stay at current position and return nil."
  10084. (let ((case-fold-search t) pos)
  10085. (save-excursion
  10086. (goto-char (point-min))
  10087. (setq pos (and (re-search-forward
  10088. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10089. (match-beginning 0))))
  10090. (if pos (goto-char pos))
  10091. pos))
  10092. (defconst org-dblock-start-re
  10093. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10094. "Matches the start line of a dynamic block, with parameters.")
  10095. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10096. "Matches the end of a dynamic block.")
  10097. (defun org-create-dblock (plist)
  10098. "Create a dynamic block section, with parameters taken from PLIST.
  10099. PLIST must contain a :name entry which is used as name of the block."
  10100. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10101. (end-of-line 1)
  10102. (newline))
  10103. (let ((col (current-column))
  10104. (name (plist-get plist :name)))
  10105. (insert "#+BEGIN: " name)
  10106. (while plist
  10107. (if (eq (car plist) :name)
  10108. (setq plist (cddr plist))
  10109. (insert " " (prin1-to-string (pop plist)))))
  10110. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10111. (beginning-of-line -2)))
  10112. (defun org-prepare-dblock ()
  10113. "Prepare dynamic block for refresh.
  10114. This empties the block, puts the cursor at the insert position and returns
  10115. the property list including an extra property :name with the block name."
  10116. (unless (looking-at org-dblock-start-re)
  10117. (error "Not at a dynamic block"))
  10118. (let* ((begdel (1+ (match-end 0)))
  10119. (name (org-no-properties (match-string 1)))
  10120. (params (append (list :name name)
  10121. (read (concat "(" (match-string 3) ")")))))
  10122. (save-excursion
  10123. (beginning-of-line 1)
  10124. (skip-chars-forward " \t")
  10125. (setq params (plist-put params :indentation-column (current-column))))
  10126. (unless (re-search-forward org-dblock-end-re nil t)
  10127. (error "Dynamic block not terminated"))
  10128. (setq params
  10129. (append params
  10130. (list :content (buffer-substring
  10131. begdel (match-beginning 0)))))
  10132. (delete-region begdel (match-beginning 0))
  10133. (goto-char begdel)
  10134. (open-line 1)
  10135. params))
  10136. (defun org-map-dblocks (&optional command)
  10137. "Apply COMMAND to all dynamic blocks in the current buffer.
  10138. If COMMAND is not given, use `org-update-dblock'."
  10139. (let ((cmd (or command 'org-update-dblock)))
  10140. (save-excursion
  10141. (goto-char (point-min))
  10142. (while (re-search-forward org-dblock-start-re nil t)
  10143. (goto-char (match-beginning 0))
  10144. (save-excursion
  10145. (condition-case nil
  10146. (funcall cmd)
  10147. (error (message "Error during update of dynamic block"))))
  10148. (unless (re-search-forward org-dblock-end-re nil t)
  10149. (error "Dynamic block not terminated"))))))
  10150. (defun org-dblock-update (&optional arg)
  10151. "User command for updating dynamic blocks.
  10152. Update the dynamic block at point. With prefix ARG, update all dynamic
  10153. blocks in the buffer."
  10154. (interactive "P")
  10155. (if arg
  10156. (org-update-all-dblocks)
  10157. (or (looking-at org-dblock-start-re)
  10158. (org-beginning-of-dblock))
  10159. (org-update-dblock)))
  10160. (defun org-update-dblock ()
  10161. "Update the dynamic block at point.
  10162. This means to empty the block, parse for parameters and then call
  10163. the correct writing function."
  10164. (interactive)
  10165. (save-window-excursion
  10166. (let* ((pos (point))
  10167. (line (org-current-line))
  10168. (params (org-prepare-dblock))
  10169. (name (plist-get params :name))
  10170. (indent (plist-get params :indentation-column))
  10171. (cmd (intern (concat "org-dblock-write:" name))))
  10172. (message "Updating dynamic block `%s' at line %d..." name line)
  10173. (funcall cmd params)
  10174. (message "Updating dynamic block `%s' at line %d...done" name line)
  10175. (goto-char pos)
  10176. (when (and indent (> indent 0))
  10177. (setq indent (make-string indent ?\ ))
  10178. (save-excursion
  10179. (org-beginning-of-dblock)
  10180. (forward-line 1)
  10181. (while (not (looking-at org-dblock-end-re))
  10182. (insert indent)
  10183. (beginning-of-line 2))
  10184. (when (looking-at org-dblock-end-re)
  10185. (and (looking-at "[ \t]+")
  10186. (replace-match ""))
  10187. (insert indent)))))))
  10188. (defun org-beginning-of-dblock ()
  10189. "Find the beginning of the dynamic block at point.
  10190. Error if there is no such block at point."
  10191. (let ((pos (point))
  10192. beg)
  10193. (end-of-line 1)
  10194. (if (and (re-search-backward org-dblock-start-re nil t)
  10195. (setq beg (match-beginning 0))
  10196. (re-search-forward org-dblock-end-re nil t)
  10197. (> (match-end 0) pos))
  10198. (goto-char beg)
  10199. (goto-char pos)
  10200. (error "Not in a dynamic block"))))
  10201. (defun org-update-all-dblocks ()
  10202. "Update all dynamic blocks in the buffer.
  10203. This function can be used in a hook."
  10204. (interactive)
  10205. (when (derived-mode-p 'org-mode)
  10206. (org-map-dblocks 'org-update-dblock)))
  10207. ;;;; Completion
  10208. (defconst org-additional-option-like-keywords
  10209. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10210. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10211. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10212. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10213. "BEGIN:" "END:"
  10214. "ORGTBL" "TBLFM:" "TBLNAME:"
  10215. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10216. "BEGIN_VERBATIM" "END_VERBATIM"
  10217. "BEGIN_QUOTE" "END_QUOTE"
  10218. "BEGIN_VERSE" "END_VERSE"
  10219. "BEGIN_CENTER" "END_CENTER"
  10220. "BEGIN_SRC" "END_SRC"
  10221. "BEGIN_RESULT" "END_RESULT"
  10222. "BEGIN_lstlisting" "END_lstlisting"
  10223. "NAME:" "RESULTS:"
  10224. "HEADER:" "HEADERS:"
  10225. "COLUMNS:" "PROPERTY:"
  10226. "CAPTION:" "LABEL:"
  10227. "SETUPFILE:"
  10228. "INCLUDE:"
  10229. "BIND:"
  10230. "MACRO:"))
  10231. (defconst org-options-keywords
  10232. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10233. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10234. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10235. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10236. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10237. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10238. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10239. (defconst org-additional-option-like-keywords-for-flyspell
  10240. (delete-dups
  10241. (split-string
  10242. (mapconcat (lambda(k)
  10243. (replace-regexp-in-string
  10244. "_\\|:" " "
  10245. (concat k " " (downcase k) " " (upcase k))))
  10246. (append org-options-keywords org-additional-option-like-keywords)
  10247. " ")
  10248. " +" t)))
  10249. (defcustom org-structure-template-alist
  10250. '(
  10251. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10252. "<src lang=\"?\">\n\n</src>")
  10253. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10254. "<example>\n?\n</example>")
  10255. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10256. "<quote>\n?\n</quote>")
  10257. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10258. "<verse>\n?\n</verse>")
  10259. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10260. "<center>\n?\n</center>")
  10261. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10262. "<literal style=\"latex\">\n?\n</literal>")
  10263. ("L" "#+LaTeX: "
  10264. "<literal style=\"latex\">?</literal>")
  10265. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10266. "<literal style=\"html\">\n?\n</literal>")
  10267. ("H" "#+HTML: "
  10268. "<literal style=\"html\">?</literal>")
  10269. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10270. ("A" "#+ASCII: ")
  10271. ("i" "#+INDEX: ?"
  10272. "#+INDEX: ?")
  10273. ("I" "#+INCLUDE: %file ?"
  10274. "<include file=%file markup=\"?\">")
  10275. )
  10276. "Structure completion elements.
  10277. This is a list of abbreviation keys and values. The value gets inserted
  10278. if you type `<' followed by the key and then press the completion key,
  10279. usually `M-TAB'. %file will be replaced by a file name after prompting
  10280. for the file using completion. The cursor will be placed at the position
  10281. of the `?` in the template.
  10282. There are two templates for each key, the first uses the original Org syntax,
  10283. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10284. the default when the /org-mtags.el/ module has been loaded. See also the
  10285. variable `org-mtags-prefer-muse-templates'."
  10286. :group 'org-completion
  10287. :type '(repeat
  10288. (string :tag "Key")
  10289. (string :tag "Template")
  10290. (string :tag "Muse Template")))
  10291. (defun org-try-structure-completion ()
  10292. "Try to complete a structure template before point.
  10293. This looks for strings like \"<e\" on an otherwise empty line and
  10294. expands them."
  10295. (let ((l (buffer-substring (point-at-bol) (point)))
  10296. a)
  10297. (when (and (looking-at "[ \t]*$")
  10298. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10299. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10300. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10301. (match-beginning 1)) a)
  10302. t)))
  10303. (defun org-complete-expand-structure-template (start cell)
  10304. "Expand a structure template."
  10305. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10306. (rpl (nth (if musep 2 1) cell))
  10307. (ind ""))
  10308. (delete-region start (point))
  10309. (when (string-match "\\`#\\+" rpl)
  10310. (cond
  10311. ((bolp))
  10312. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10313. (setq ind (buffer-substring (point-at-bol) (point))))
  10314. (t (newline))))
  10315. (setq start (point))
  10316. (if (string-match "%file" rpl)
  10317. (setq rpl (replace-match
  10318. (concat
  10319. "\""
  10320. (save-match-data
  10321. (abbreviate-file-name (read-file-name "Include file: ")))
  10322. "\"")
  10323. t t rpl)))
  10324. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10325. (concat "\n" ind)))
  10326. (insert rpl)
  10327. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10328. ;;;; TODO, DEADLINE, Comments
  10329. (defun org-toggle-comment ()
  10330. "Change the COMMENT state of an entry."
  10331. (interactive)
  10332. (save-excursion
  10333. (org-back-to-heading)
  10334. (let (case-fold-search)
  10335. (cond
  10336. ((looking-at (format org-heading-keyword-regexp-format
  10337. org-comment-string))
  10338. (goto-char (match-end 1))
  10339. (looking-at (concat " +" org-comment-string))
  10340. (replace-match "" t t)
  10341. (when (eolp) (insert " ")))
  10342. ((looking-at org-outline-regexp)
  10343. (goto-char (match-end 0))
  10344. (insert org-comment-string " "))))))
  10345. (defvar org-last-todo-state-is-todo nil
  10346. "This is non-nil when the last TODO state change led to a TODO state.
  10347. If the last change removed the TODO tag or switched to DONE, then
  10348. this is nil.")
  10349. (defvar org-setting-tags nil) ; dynamically skipped
  10350. (defvar org-todo-setup-filter-hook nil
  10351. "Hook for functions that pre-filter todo specs.
  10352. Each function takes a todo spec and returns either nil or the spec
  10353. transformed into canonical form." )
  10354. (defvar org-todo-get-default-hook nil
  10355. "Hook for functions that get a default item for todo.
  10356. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10357. nil or a string to be used for the todo mark." )
  10358. (defvar org-agenda-headline-snapshot-before-repeat)
  10359. (defun org-current-effective-time ()
  10360. "Return current time adjusted for `org-extend-today-until' variable."
  10361. (let* ((ct (org-current-time))
  10362. (dct (decode-time ct))
  10363. (ct1
  10364. (cond
  10365. (org-use-last-clock-out-time-as-effective-time
  10366. (or (org-clock-get-last-clock-out-time) ct))
  10367. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10368. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10369. (t ct))))
  10370. ct1))
  10371. (defun org-todo-yesterday (&optional arg)
  10372. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10373. (interactive "P")
  10374. (if (eq major-mode 'org-agenda-mode)
  10375. (apply 'org-agenda-todo-yesterday arg)
  10376. (let* ((hour (third (decode-time
  10377. (org-current-time))))
  10378. (org-extend-today-until (1+ hour)))
  10379. (org-todo arg))))
  10380. (defun org-todo (&optional arg)
  10381. "Change the TODO state of an item.
  10382. The state of an item is given by a keyword at the start of the heading,
  10383. like
  10384. *** TODO Write paper
  10385. *** DONE Call mom
  10386. The different keywords are specified in the variable `org-todo-keywords'.
  10387. By default the available states are \"TODO\" and \"DONE\".
  10388. So for this example: when the item starts with TODO, it is changed to DONE.
  10389. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10390. DONE are present, add TODO at the beginning of the heading.
  10391. With \\[universal-argument] prefix arg, use completion to determine the new \
  10392. state.
  10393. With numeric prefix arg, switch to that state.
  10394. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10395. keywords (nextset).
  10396. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10397. With a numeric prefix arg of 0, inhibit note taking for the change.
  10398. For calling through lisp, arg is also interpreted in the following way:
  10399. 'none -> empty state
  10400. \"\"(empty string) -> switch to empty state
  10401. 'done -> switch to DONE
  10402. 'nextset -> switch to the next set of keywords
  10403. 'previousset -> switch to the previous set of keywords
  10404. \"WAITING\" -> switch to the specified keyword, but only if it
  10405. really is a member of `org-todo-keywords'."
  10406. (interactive "P")
  10407. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10408. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10409. 'region-start-level 'region))
  10410. org-loop-over-headlines-in-active-region)
  10411. (org-map-entries
  10412. `(org-todo ,arg)
  10413. org-loop-over-headlines-in-active-region
  10414. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10415. (if (equal arg '(16)) (setq arg 'nextset))
  10416. (let ((org-blocker-hook org-blocker-hook)
  10417. (case-fold-search nil))
  10418. (when (equal arg '(64))
  10419. (setq arg nil org-blocker-hook nil))
  10420. (when (and org-blocker-hook
  10421. (or org-inhibit-blocking
  10422. (org-entry-get nil "NOBLOCKING")))
  10423. (setq org-blocker-hook nil))
  10424. (save-excursion
  10425. (catch 'exit
  10426. (org-back-to-heading t)
  10427. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10428. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10429. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10430. (let* ((match-data (match-data))
  10431. (startpos (point-at-bol))
  10432. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10433. (org-log-done org-log-done)
  10434. (org-log-repeat org-log-repeat)
  10435. (org-todo-log-states org-todo-log-states)
  10436. (org-inhibit-logging
  10437. (if (equal arg 0)
  10438. (progn (setq arg nil) 'note) org-inhibit-logging))
  10439. (this (match-string 1))
  10440. (hl-pos (match-beginning 0))
  10441. (head (org-get-todo-sequence-head this))
  10442. (ass (assoc head org-todo-kwd-alist))
  10443. (interpret (nth 1 ass))
  10444. (done-word (nth 3 ass))
  10445. (final-done-word (nth 4 ass))
  10446. (org-last-state (or this ""))
  10447. (completion-ignore-case t)
  10448. (member (member this org-todo-keywords-1))
  10449. (tail (cdr member))
  10450. (org-state (cond
  10451. ((and org-todo-key-trigger
  10452. (or (and (equal arg '(4))
  10453. (eq org-use-fast-todo-selection 'prefix))
  10454. (and (not arg) org-use-fast-todo-selection
  10455. (not (eq org-use-fast-todo-selection
  10456. 'prefix)))))
  10457. ;; Use fast selection
  10458. (org-fast-todo-selection))
  10459. ((and (equal arg '(4))
  10460. (or (not org-use-fast-todo-selection)
  10461. (not org-todo-key-trigger)))
  10462. ;; Read a state with completion
  10463. (org-icompleting-read
  10464. "State: " (mapcar (lambda(x) (list x))
  10465. org-todo-keywords-1)
  10466. nil t))
  10467. ((eq arg 'right)
  10468. (if this
  10469. (if tail (car tail) nil)
  10470. (car org-todo-keywords-1)))
  10471. ((eq arg 'left)
  10472. (if (equal member org-todo-keywords-1)
  10473. nil
  10474. (if this
  10475. (nth (- (length org-todo-keywords-1)
  10476. (length tail) 2)
  10477. org-todo-keywords-1)
  10478. (org-last org-todo-keywords-1))))
  10479. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10480. (setq arg nil))) ; hack to fall back to cycling
  10481. (arg
  10482. ;; user or caller requests a specific state
  10483. (cond
  10484. ((equal arg "") nil)
  10485. ((eq arg 'none) nil)
  10486. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10487. ((eq arg 'nextset)
  10488. (or (car (cdr (member head org-todo-heads)))
  10489. (car org-todo-heads)))
  10490. ((eq arg 'previousset)
  10491. (let ((org-todo-heads (reverse org-todo-heads)))
  10492. (or (car (cdr (member head org-todo-heads)))
  10493. (car org-todo-heads))))
  10494. ((car (member arg org-todo-keywords-1)))
  10495. ((stringp arg)
  10496. (error "State `%s' not valid in this file" arg))
  10497. ((nth (1- (prefix-numeric-value arg))
  10498. org-todo-keywords-1))))
  10499. ((null member) (or head (car org-todo-keywords-1)))
  10500. ((equal this final-done-word) nil) ;; -> make empty
  10501. ((null tail) nil) ;; -> first entry
  10502. ((memq interpret '(type priority))
  10503. (if (eq this-command last-command)
  10504. (car tail)
  10505. (if (> (length tail) 0)
  10506. (or done-word (car org-done-keywords))
  10507. nil)))
  10508. (t
  10509. (car tail))))
  10510. (org-state (or
  10511. (run-hook-with-args-until-success
  10512. 'org-todo-get-default-hook org-state org-last-state)
  10513. org-state))
  10514. (next (if org-state (concat " " org-state " ") " "))
  10515. (change-plist (list :type 'todo-state-change :from this :to org-state
  10516. :position startpos))
  10517. dolog now-done-p)
  10518. (when org-blocker-hook
  10519. (setq org-last-todo-state-is-todo
  10520. (not (member this org-done-keywords)))
  10521. (unless (save-excursion
  10522. (save-match-data
  10523. (org-with-wide-buffer
  10524. (run-hook-with-args-until-failure
  10525. 'org-blocker-hook change-plist))))
  10526. (if (org-called-interactively-p 'interactive)
  10527. (error "TODO state change from %s to %s blocked" this org-state)
  10528. ;; fail silently
  10529. (message "TODO state change from %s to %s blocked" this org-state)
  10530. (throw 'exit nil))))
  10531. (store-match-data match-data)
  10532. (replace-match next t t)
  10533. (unless (pos-visible-in-window-p hl-pos)
  10534. (message "TODO state changed to %s" (org-trim next)))
  10535. (unless head
  10536. (setq head (org-get-todo-sequence-head org-state)
  10537. ass (assoc head org-todo-kwd-alist)
  10538. interpret (nth 1 ass)
  10539. done-word (nth 3 ass)
  10540. final-done-word (nth 4 ass)))
  10541. (when (memq arg '(nextset previousset))
  10542. (message "Keyword-Set %d/%d: %s"
  10543. (- (length org-todo-sets) -1
  10544. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10545. (length org-todo-sets)
  10546. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10547. (setq org-last-todo-state-is-todo
  10548. (not (member org-state org-done-keywords)))
  10549. (setq now-done-p (and (member org-state org-done-keywords)
  10550. (not (member this org-done-keywords))))
  10551. (and logging (org-local-logging logging))
  10552. (when (and (or org-todo-log-states org-log-done)
  10553. (not (eq org-inhibit-logging t))
  10554. (not (memq arg '(nextset previousset))))
  10555. ;; we need to look at recording a time and note
  10556. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10557. (nth 2 (assoc this org-todo-log-states))))
  10558. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10559. (setq dolog 'time))
  10560. (when (and org-state
  10561. (member org-state org-not-done-keywords)
  10562. (not (member this org-not-done-keywords)))
  10563. ;; This is now a todo state and was not one before
  10564. ;; If there was a CLOSED time stamp, get rid of it.
  10565. (org-add-planning-info nil nil 'closed))
  10566. (when (and now-done-p org-log-done)
  10567. ;; It is now done, and it was not done before
  10568. (org-add-planning-info 'closed (org-current-effective-time))
  10569. (if (and (not dolog) (eq 'note org-log-done))
  10570. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10571. (when (and org-state dolog)
  10572. ;; This is a non-nil state, and we need to log it
  10573. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10574. ;; Fixup tag positioning
  10575. (org-todo-trigger-tag-changes org-state)
  10576. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10577. (when org-provide-todo-statistics
  10578. (org-update-parent-todo-statistics))
  10579. (run-hooks 'org-after-todo-state-change-hook)
  10580. (if (and arg (not (member org-state org-done-keywords)))
  10581. (setq head (org-get-todo-sequence-head org-state)))
  10582. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10583. ;; Do we need to trigger a repeat?
  10584. (when now-done-p
  10585. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10586. ;; This is for the agenda, take a snapshot of the headline.
  10587. (save-match-data
  10588. (setq org-agenda-headline-snapshot-before-repeat
  10589. (org-get-heading))))
  10590. (org-auto-repeat-maybe org-state))
  10591. ;; Fixup cursor location if close to the keyword
  10592. (if (and (outline-on-heading-p)
  10593. (not (bolp))
  10594. (save-excursion (beginning-of-line 1)
  10595. (looking-at org-todo-line-regexp))
  10596. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10597. (progn
  10598. (goto-char (or (match-end 2) (match-end 1)))
  10599. (and (looking-at " ") (just-one-space))))
  10600. (when org-trigger-hook
  10601. (save-excursion
  10602. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10603. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10604. "Block turning an entry into a TODO, using the hierarchy.
  10605. This checks whether the current task should be blocked from state
  10606. changes. Such blocking occurs when:
  10607. 1. The task has children which are not all in a completed state.
  10608. 2. A task has a parent with the property :ORDERED:, and there
  10609. are siblings prior to the current task with incomplete
  10610. status.
  10611. 3. The parent of the task is blocked because it has siblings that should
  10612. be done first, or is child of a block grandparent TODO entry."
  10613. (if (not org-enforce-todo-dependencies)
  10614. t ; if locally turned off don't block
  10615. (catch 'dont-block
  10616. ;; If this is not a todo state change, or if this entry is already DONE,
  10617. ;; do not block
  10618. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10619. (member (plist-get change-plist :from)
  10620. (cons 'done org-done-keywords))
  10621. (member (plist-get change-plist :to)
  10622. (cons 'todo org-not-done-keywords))
  10623. (not (plist-get change-plist :to)))
  10624. (throw 'dont-block t))
  10625. ;; If this task has children, and any are undone, it's blocked
  10626. (save-excursion
  10627. (org-back-to-heading t)
  10628. (let ((this-level (funcall outline-level)))
  10629. (outline-next-heading)
  10630. (let ((child-level (funcall outline-level)))
  10631. (while (and (not (eobp))
  10632. (> child-level this-level))
  10633. ;; this todo has children, check whether they are all
  10634. ;; completed
  10635. (if (and (not (org-entry-is-done-p))
  10636. (org-entry-is-todo-p))
  10637. (throw 'dont-block nil))
  10638. (outline-next-heading)
  10639. (setq child-level (funcall outline-level))))))
  10640. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10641. ;; any previous siblings are undone, it's blocked
  10642. (save-excursion
  10643. (org-back-to-heading t)
  10644. (let* ((pos (point))
  10645. (parent-pos (and (org-up-heading-safe) (point))))
  10646. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10647. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10648. (forward-line 1)
  10649. (re-search-forward org-not-done-heading-regexp pos t))
  10650. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10651. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10652. (while t
  10653. (goto-char parent-pos)
  10654. (if (not (looking-at org-not-done-heading-regexp))
  10655. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10656. (setq pos (point))
  10657. (setq parent-pos (and (org-up-heading-safe) (point)))
  10658. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10659. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10660. (forward-line 1)
  10661. (re-search-forward org-not-done-heading-regexp pos t))
  10662. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10663. (defcustom org-track-ordered-property-with-tag nil
  10664. "Should the ORDERED property also be shown as a tag?
  10665. The ORDERED property decides if an entry should require subtasks to be
  10666. completed in sequence. Since a property is not very visible, setting
  10667. this option means that toggling the ORDERED property with the command
  10668. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10669. not relevant for the behavior, but it makes things more visible.
  10670. Note that toggling the tag with tags commands will not change the property
  10671. and therefore not influence behavior!
  10672. This can be t, meaning the tag ORDERED should be used, It can also be a
  10673. string to select a different tag for this task."
  10674. :group 'org-todo
  10675. :type '(choice
  10676. (const :tag "No tracking" nil)
  10677. (const :tag "Track with ORDERED tag" t)
  10678. (string :tag "Use other tag")))
  10679. (defun org-toggle-ordered-property ()
  10680. "Toggle the ORDERED property of the current entry.
  10681. For better visibility, you can track the value of this property with a tag.
  10682. See variable `org-track-ordered-property-with-tag'."
  10683. (interactive)
  10684. (let* ((t1 org-track-ordered-property-with-tag)
  10685. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10686. (save-excursion
  10687. (org-back-to-heading)
  10688. (if (org-entry-get nil "ORDERED")
  10689. (progn
  10690. (org-delete-property "ORDERED")
  10691. (and tag (org-toggle-tag tag 'off))
  10692. (message "Subtasks can be completed in arbitrary order"))
  10693. (org-entry-put nil "ORDERED" "t")
  10694. (and tag (org-toggle-tag tag 'on))
  10695. (message "Subtasks must be completed in sequence")))))
  10696. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10697. (defun org-block-todo-from-checkboxes (change-plist)
  10698. "Block turning an entry into a TODO, using checkboxes.
  10699. This checks whether the current task should be blocked from state
  10700. changes because there are unchecked boxes in this entry."
  10701. (if (not org-enforce-todo-checkbox-dependencies)
  10702. t ; if locally turned off don't block
  10703. (catch 'dont-block
  10704. ;; If this is not a todo state change, or if this entry is already DONE,
  10705. ;; do not block
  10706. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10707. (member (plist-get change-plist :from)
  10708. (cons 'done org-done-keywords))
  10709. (member (plist-get change-plist :to)
  10710. (cons 'todo org-not-done-keywords))
  10711. (not (plist-get change-plist :to)))
  10712. (throw 'dont-block t))
  10713. ;; If this task has checkboxes that are not checked, it's blocked
  10714. (save-excursion
  10715. (org-back-to-heading t)
  10716. (let ((beg (point)) end)
  10717. (outline-next-heading)
  10718. (setq end (point))
  10719. (goto-char beg)
  10720. (if (org-list-search-forward
  10721. (concat (org-item-beginning-re)
  10722. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10723. "\\[[- ]\\]")
  10724. end t)
  10725. (progn
  10726. (if (boundp 'org-blocked-by-checkboxes)
  10727. (setq org-blocked-by-checkboxes t))
  10728. (throw 'dont-block nil)))))
  10729. t))) ; do not block
  10730. (defun org-entry-blocked-p ()
  10731. "Is the current entry blocked?"
  10732. (if (org-entry-get nil "NOBLOCKING")
  10733. nil ;; Never block this entry
  10734. (not
  10735. (run-hook-with-args-until-failure
  10736. 'org-blocker-hook
  10737. (list :type 'todo-state-change
  10738. :position (point)
  10739. :from 'todo
  10740. :to 'done)))))
  10741. (defun org-update-statistics-cookies (all)
  10742. "Update the statistics cookie, either from TODO or from checkboxes.
  10743. This should be called with the cursor in a line with a statistics cookie."
  10744. (interactive "P")
  10745. (if all
  10746. (progn
  10747. (org-update-checkbox-count 'all)
  10748. (org-map-entries 'org-update-parent-todo-statistics))
  10749. (if (not (org-at-heading-p))
  10750. (org-update-checkbox-count)
  10751. (let ((pos (move-marker (make-marker) (point)))
  10752. end l1 l2)
  10753. (ignore-errors (org-back-to-heading t))
  10754. (if (not (org-at-heading-p))
  10755. (org-update-checkbox-count)
  10756. (setq l1 (org-outline-level))
  10757. (setq end (save-excursion
  10758. (outline-next-heading)
  10759. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10760. (point)))
  10761. (if (and (save-excursion
  10762. (re-search-forward
  10763. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10764. (not (save-excursion (re-search-forward
  10765. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10766. (org-update-checkbox-count)
  10767. (if (and l2 (> l2 l1))
  10768. (progn
  10769. (goto-char end)
  10770. (org-update-parent-todo-statistics))
  10771. (goto-char pos)
  10772. (beginning-of-line 1)
  10773. (while (re-search-forward
  10774. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10775. (point-at-eol) t)
  10776. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10777. (goto-char pos)
  10778. (move-marker pos nil)))))
  10779. (defvar org-entry-property-inherited-from) ;; defined below
  10780. (defun org-update-parent-todo-statistics ()
  10781. "Update any statistics cookie in the parent of the current headline.
  10782. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10783. the entire subtree and this will travel up the hierarchy and update
  10784. statistics everywhere."
  10785. (let* ((prop (save-excursion (org-up-heading-safe)
  10786. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10787. (recursive (or (not org-hierarchical-todo-statistics)
  10788. (and prop (string-match "\\<recursive\\>" prop))))
  10789. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10790. 0))
  10791. (first t)
  10792. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10793. level ltoggle l1 new ndel
  10794. (cnt-all 0) (cnt-done 0) is-percent kwd
  10795. checkbox-beg ov ovs ove cookie-present)
  10796. (catch 'exit
  10797. (save-excursion
  10798. (beginning-of-line 1)
  10799. (setq ltoggle (funcall outline-level))
  10800. ;; Three situations are to consider:
  10801. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10802. ;; to the top-level ancestor on the headline;
  10803. ;; 2. If parent has "recursive" property, repeat up to the
  10804. ;; headline setting that property, taking inheritance into
  10805. ;; account;
  10806. ;; 3. Else, move up to direct parent and proceed only once.
  10807. (while (and (setq level (org-up-heading-safe))
  10808. (or recursive first)
  10809. (>= (point) lim))
  10810. (setq first nil cookie-present nil)
  10811. (unless (and level
  10812. (not (string-match
  10813. "\\<checkbox\\>"
  10814. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10815. "")))))
  10816. (throw 'exit nil))
  10817. (while (re-search-forward box-re (point-at-eol) t)
  10818. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10819. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10820. (save-match-data
  10821. (unless (outline-next-heading) (throw 'exit nil))
  10822. (while (and (looking-at org-complex-heading-regexp)
  10823. (> (setq l1 (length (match-string 1))) level))
  10824. (setq kwd (and (or recursive (= l1 ltoggle))
  10825. (match-string 2)))
  10826. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10827. (and (listp org-provide-todo-statistics)
  10828. (or (member kwd org-provide-todo-statistics)
  10829. (member kwd org-done-keywords))))
  10830. (setq cnt-all (1+ cnt-all))
  10831. (if (eq org-provide-todo-statistics t)
  10832. (and kwd (setq cnt-all (1+ cnt-all)))))
  10833. (and (member kwd org-done-keywords)
  10834. (setq cnt-done (1+ cnt-done)))
  10835. (outline-next-heading)))
  10836. (setq new
  10837. (if is-percent
  10838. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10839. (format "[%d/%d]" cnt-done cnt-all))
  10840. ndel (- (match-end 0) checkbox-beg))
  10841. ;; handle overlays when updating cookie from column view
  10842. (when (setq ov (car (overlays-at checkbox-beg)))
  10843. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10844. (delete-overlay ov))
  10845. (goto-char checkbox-beg)
  10846. (insert new)
  10847. (delete-region (point) (+ (point) ndel))
  10848. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10849. (when ov (move-overlay ov ovs ove)))
  10850. (when cookie-present
  10851. (run-hook-with-args 'org-after-todo-statistics-hook
  10852. cnt-done (- cnt-all cnt-done))))))
  10853. (run-hooks 'org-todo-statistics-hook)))
  10854. (defvar org-after-todo-statistics-hook nil
  10855. "Hook that is called after a TODO statistics cookie has been updated.
  10856. Each function is called with two arguments: the number of not-done entries
  10857. and the number of done entries.
  10858. For example, the following function, when added to this hook, will switch
  10859. an entry to DONE when all children are done, and back to TODO when new
  10860. entries are set to a TODO status. Note that this hook is only called
  10861. when there is a statistics cookie in the headline!
  10862. (defun org-summary-todo (n-done n-not-done)
  10863. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10864. (let (org-log-done org-log-states) ; turn off logging
  10865. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10866. ")
  10867. (defvar org-todo-statistics-hook nil
  10868. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10869. This hook runs even if there is no statistics cookie present, in which case
  10870. `org-after-todo-statistics-hook' would not run.")
  10871. (defun org-todo-trigger-tag-changes (state)
  10872. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10873. (let ((l org-todo-state-tags-triggers)
  10874. changes)
  10875. (when (or (not state) (equal state ""))
  10876. (setq changes (append changes (cdr (assoc "" l)))))
  10877. (when (and (stringp state) (> (length state) 0))
  10878. (setq changes (append changes (cdr (assoc state l)))))
  10879. (when (member state org-not-done-keywords)
  10880. (setq changes (append changes (cdr (assoc 'todo l)))))
  10881. (when (member state org-done-keywords)
  10882. (setq changes (append changes (cdr (assoc 'done l)))))
  10883. (dolist (c changes)
  10884. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10885. (defun org-local-logging (value)
  10886. "Get logging settings from a property VALUE."
  10887. (let* (words w a)
  10888. ;; directly set the variables, they are already local.
  10889. (setq org-log-done nil
  10890. org-log-repeat nil
  10891. org-todo-log-states nil)
  10892. (setq words (org-split-string value))
  10893. (while (setq w (pop words))
  10894. (cond
  10895. ((setq a (assoc w org-startup-options))
  10896. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10897. (set (nth 1 a) (nth 2 a))))
  10898. ((setq a (org-extract-log-state-settings w))
  10899. (and (member (car a) org-todo-keywords-1)
  10900. (push a org-todo-log-states)))))))
  10901. (defun org-get-todo-sequence-head (kwd)
  10902. "Return the head of the TODO sequence to which KWD belongs.
  10903. If KWD is not set, check if there is a text property remembering the
  10904. right sequence."
  10905. (let (p)
  10906. (cond
  10907. ((not kwd)
  10908. (or (get-text-property (point-at-bol) 'org-todo-head)
  10909. (progn
  10910. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10911. nil (point-at-eol)))
  10912. (get-text-property p 'org-todo-head))))
  10913. ((not (member kwd org-todo-keywords-1))
  10914. (car org-todo-keywords-1))
  10915. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10916. (defun org-fast-todo-selection ()
  10917. "Fast TODO keyword selection with single keys.
  10918. Returns the new TODO keyword, or nil if no state change should occur."
  10919. (let* ((fulltable org-todo-key-alist)
  10920. (done-keywords org-done-keywords) ;; needed for the faces.
  10921. (maxlen (apply 'max (mapcar
  10922. (lambda (x)
  10923. (if (stringp (car x)) (string-width (car x)) 0))
  10924. fulltable)))
  10925. (expert nil)
  10926. (fwidth (+ maxlen 3 1 3))
  10927. (ncol (/ (- (window-width) 4) fwidth))
  10928. tg cnt e c tbl
  10929. groups ingroup)
  10930. (save-excursion
  10931. (save-window-excursion
  10932. (if expert
  10933. (set-buffer (get-buffer-create " *Org todo*"))
  10934. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10935. (erase-buffer)
  10936. (org-set-local 'org-done-keywords done-keywords)
  10937. (setq tbl fulltable cnt 0)
  10938. (while (setq e (pop tbl))
  10939. (cond
  10940. ((equal e '(:startgroup))
  10941. (push '() groups) (setq ingroup t)
  10942. (when (not (= cnt 0))
  10943. (setq cnt 0)
  10944. (insert "\n"))
  10945. (insert "{ "))
  10946. ((equal e '(:endgroup))
  10947. (setq ingroup nil cnt 0)
  10948. (insert "}\n"))
  10949. ((equal e '(:newline))
  10950. (when (not (= cnt 0))
  10951. (setq cnt 0)
  10952. (insert "\n")
  10953. (setq e (car tbl))
  10954. (while (equal (car tbl) '(:newline))
  10955. (insert "\n")
  10956. (setq tbl (cdr tbl)))))
  10957. (t
  10958. (setq tg (car e) c (cdr e))
  10959. (if ingroup (push tg (car groups)))
  10960. (setq tg (org-add-props tg nil 'face
  10961. (org-get-todo-face tg)))
  10962. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10963. (insert "[" c "] " tg (make-string
  10964. (- fwidth 4 (length tg)) ?\ ))
  10965. (when (= (setq cnt (1+ cnt)) ncol)
  10966. (insert "\n")
  10967. (if ingroup (insert " "))
  10968. (setq cnt 0)))))
  10969. (insert "\n")
  10970. (goto-char (point-min))
  10971. (if (not expert) (org-fit-window-to-buffer))
  10972. (message "[a-z..]:Set [SPC]:clear")
  10973. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10974. (cond
  10975. ((or (= c ?\C-g)
  10976. (and (= c ?q) (not (rassoc c fulltable))))
  10977. (setq quit-flag t))
  10978. ((= c ?\ ) nil)
  10979. ((setq e (rassoc c fulltable) tg (car e))
  10980. tg)
  10981. (t (setq quit-flag t)))))))
  10982. (defun org-entry-is-todo-p ()
  10983. (member (org-get-todo-state) org-not-done-keywords))
  10984. (defun org-entry-is-done-p ()
  10985. (member (org-get-todo-state) org-done-keywords))
  10986. (defun org-get-todo-state ()
  10987. (save-excursion
  10988. (org-back-to-heading t)
  10989. (and (looking-at org-todo-line-regexp)
  10990. (match-end 2)
  10991. (match-string 2))))
  10992. (defun org-at-date-range-p (&optional inactive-ok)
  10993. "Is the cursor inside a date range?"
  10994. (interactive)
  10995. (save-excursion
  10996. (catch 'exit
  10997. (let ((pos (point)))
  10998. (skip-chars-backward "^[<\r\n")
  10999. (skip-chars-backward "<[")
  11000. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11001. (>= (match-end 0) pos)
  11002. (throw 'exit t))
  11003. (skip-chars-backward "^<[\r\n")
  11004. (skip-chars-backward "<[")
  11005. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11006. (>= (match-end 0) pos)
  11007. (throw 'exit t)))
  11008. nil)))
  11009. (defun org-get-repeat (&optional tagline)
  11010. "Check if there is a deadline/schedule with repeater in this entry."
  11011. (save-match-data
  11012. (save-excursion
  11013. (org-back-to-heading t)
  11014. (and (re-search-forward (if tagline
  11015. (concat tagline "\\s-*" org-repeat-re)
  11016. org-repeat-re)
  11017. (org-entry-end-position) t)
  11018. (match-string-no-properties 1)))))
  11019. (defvar org-last-changed-timestamp)
  11020. (defvar org-last-inserted-timestamp)
  11021. (defvar org-log-post-message)
  11022. (defvar org-log-note-purpose)
  11023. (defvar org-log-note-how)
  11024. (defvar org-log-note-extra)
  11025. (defun org-auto-repeat-maybe (done-word)
  11026. "Check if the current headline contains a repeated deadline/schedule.
  11027. If yes, set TODO state back to what it was and change the base date
  11028. of repeating deadline/scheduled time stamps to new date.
  11029. This function is run automatically after each state change to a DONE state."
  11030. ;; last-state is dynamically scoped into this function
  11031. (let* ((repeat (org-get-repeat))
  11032. (aa (assoc org-last-state org-todo-kwd-alist))
  11033. (interpret (nth 1 aa))
  11034. (head (nth 2 aa))
  11035. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11036. (msg "Entry repeats: ")
  11037. (org-log-done nil)
  11038. (org-todo-log-states nil)
  11039. re type n what ts time to-state)
  11040. (when repeat
  11041. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11042. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11043. org-todo-repeat-to-state))
  11044. (unless (and to-state (member to-state org-todo-keywords-1))
  11045. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11046. (org-todo to-state)
  11047. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11048. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11049. (org-time-stamp-format t t))))
  11050. (when org-log-repeat
  11051. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11052. (memq 'org-add-log-note post-command-hook))
  11053. ;; OK, we are already setup for some record
  11054. (if (eq org-log-repeat 'note)
  11055. ;; make sure we take a note, not only a time stamp
  11056. (setq org-log-note-how 'note))
  11057. ;; Set up for taking a record
  11058. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11059. org-last-state
  11060. 'findpos org-log-repeat)))
  11061. (org-back-to-heading t)
  11062. (org-add-planning-info nil nil 'closed)
  11063. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11064. org-deadline-time-regexp "\\)\\|\\("
  11065. org-ts-regexp "\\)"))
  11066. (while (re-search-forward
  11067. re (save-excursion (outline-next-heading) (point)) t)
  11068. (setq type (if (match-end 1) org-scheduled-string
  11069. (if (match-end 3) org-deadline-string "Plain:"))
  11070. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11071. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11072. (setq n (string-to-number (match-string 2 ts))
  11073. what (match-string 3 ts))
  11074. (if (equal what "w") (setq n (* n 7) what "d"))
  11075. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11076. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11077. ;; Preparation, see if we need to modify the start date for the change
  11078. (when (match-end 1)
  11079. (setq time (save-match-data (org-time-string-to-time ts)))
  11080. (cond
  11081. ((equal (match-string 1 ts) ".")
  11082. ;; Shift starting date to today
  11083. (org-timestamp-change
  11084. (- (org-today) (time-to-days time))
  11085. 'day))
  11086. ((equal (match-string 1 ts) "+")
  11087. (let ((nshiftmax 10) (nshift 0))
  11088. (while (or (= nshift 0)
  11089. (<= (time-to-days time)
  11090. (time-to-days (current-time))))
  11091. (when (= (incf nshift) nshiftmax)
  11092. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11093. (error "Abort")))
  11094. (org-timestamp-change n (cdr (assoc what whata)))
  11095. (org-at-timestamp-p t)
  11096. (setq ts (match-string 1))
  11097. (setq time (save-match-data (org-time-string-to-time ts)))))
  11098. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11099. ;; rematch, so that we have everything in place for the real shift
  11100. (org-at-timestamp-p t)
  11101. (setq ts (match-string 1))
  11102. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11103. (org-timestamp-change n (cdr (assoc what whata)))
  11104. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11105. (setq org-log-post-message msg)
  11106. (message "%s" msg))))
  11107. (defun org-show-todo-tree (arg)
  11108. "Make a compact tree which shows all headlines marked with TODO.
  11109. The tree will show the lines where the regexp matches, and all higher
  11110. headlines above the match.
  11111. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11112. With a numeric prefix N, construct a sparse tree for the Nth element
  11113. of `org-todo-keywords-1'."
  11114. (interactive "P")
  11115. (let ((case-fold-search nil)
  11116. (kwd-re
  11117. (cond ((null arg) org-not-done-regexp)
  11118. ((equal arg '(4))
  11119. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11120. (mapcar 'list org-todo-keywords-1))))
  11121. (concat "\\("
  11122. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11123. "\\)\\>")))
  11124. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11125. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11126. org-todo-keywords-1)))
  11127. (t (error "Invalid prefix argument: %s" arg)))))
  11128. (message "%d TODO entries found"
  11129. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11130. (defun org-deadline (&optional remove time)
  11131. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11132. With argument REMOVE, remove any deadline from the item.
  11133. With argument TIME, set the deadline at the corresponding date. TIME
  11134. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11135. (interactive "P")
  11136. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11137. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11138. 'region-start-level 'region))
  11139. org-loop-over-headlines-in-active-region)
  11140. (org-map-entries
  11141. `(org-deadline ',remove ,time)
  11142. org-loop-over-headlines-in-active-region
  11143. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11144. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11145. (repeater (and old-date
  11146. (string-match
  11147. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11148. old-date)
  11149. (match-string 1 old-date))))
  11150. (if remove
  11151. (progn
  11152. (when (and old-date org-log-redeadline)
  11153. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11154. org-log-redeadline))
  11155. (org-remove-timestamp-with-keyword org-deadline-string)
  11156. (message "Item no longer has a deadline."))
  11157. (org-add-planning-info 'deadline time 'closed)
  11158. (when (and old-date org-log-redeadline
  11159. (not (equal old-date
  11160. (substring org-last-inserted-timestamp 1 -1))))
  11161. (org-add-log-setup 'redeadline nil old-date 'findpos
  11162. org-log-redeadline))
  11163. (when repeater
  11164. (save-excursion
  11165. (org-back-to-heading t)
  11166. (when (re-search-forward (concat org-deadline-string " "
  11167. org-last-inserted-timestamp)
  11168. (save-excursion
  11169. (outline-next-heading) (point)) t)
  11170. (goto-char (1- (match-end 0)))
  11171. (insert " " repeater)
  11172. (setq org-last-inserted-timestamp
  11173. (concat (substring org-last-inserted-timestamp 0 -1)
  11174. " " repeater
  11175. (substring org-last-inserted-timestamp -1))))))
  11176. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11177. (defun org-schedule (&optional remove time)
  11178. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11179. With argument REMOVE, remove any scheduling date from the item.
  11180. With argument TIME, scheduled at the corresponding date. TIME can
  11181. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11182. (interactive "P")
  11183. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11184. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11185. 'region-start-level 'region))
  11186. org-loop-over-headlines-in-active-region)
  11187. (org-map-entries
  11188. `(org-schedule ',remove ,time)
  11189. org-loop-over-headlines-in-active-region
  11190. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11191. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11192. (repeater (and old-date
  11193. (string-match
  11194. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11195. old-date)
  11196. (match-string 1 old-date))))
  11197. (if remove
  11198. (progn
  11199. (when (and old-date org-log-reschedule)
  11200. (org-add-log-setup 'delschedule nil old-date 'findpos
  11201. org-log-reschedule))
  11202. (org-remove-timestamp-with-keyword org-scheduled-string)
  11203. (message "Item is no longer scheduled."))
  11204. (org-add-planning-info 'scheduled time 'closed)
  11205. (when (and old-date org-log-reschedule
  11206. (not (equal old-date
  11207. (substring org-last-inserted-timestamp 1 -1))))
  11208. (org-add-log-setup 'reschedule nil old-date 'findpos
  11209. org-log-reschedule))
  11210. (when repeater
  11211. (save-excursion
  11212. (org-back-to-heading t)
  11213. (when (re-search-forward (concat org-scheduled-string " "
  11214. org-last-inserted-timestamp)
  11215. (save-excursion
  11216. (outline-next-heading) (point)) t)
  11217. (goto-char (1- (match-end 0)))
  11218. (insert " " repeater)
  11219. (setq org-last-inserted-timestamp
  11220. (concat (substring org-last-inserted-timestamp 0 -1)
  11221. " " repeater
  11222. (substring org-last-inserted-timestamp -1))))))
  11223. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11224. (defun org-get-scheduled-time (pom &optional inherit)
  11225. "Get the scheduled time as a time tuple, of a format suitable
  11226. for calling org-schedule with, or if there is no scheduling,
  11227. returns nil."
  11228. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11229. (when time
  11230. (apply 'encode-time (org-parse-time-string time)))))
  11231. (defun org-get-deadline-time (pom &optional inherit)
  11232. "Get the deadline as a time tuple, of a format suitable for
  11233. calling org-deadline with, or if there is no scheduling, returns
  11234. nil."
  11235. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11236. (when time
  11237. (apply 'encode-time (org-parse-time-string time)))))
  11238. (defun org-remove-timestamp-with-keyword (keyword)
  11239. "Remove all time stamps with KEYWORD in the current entry."
  11240. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11241. beg)
  11242. (save-excursion
  11243. (org-back-to-heading t)
  11244. (setq beg (point))
  11245. (outline-next-heading)
  11246. (while (re-search-backward re beg t)
  11247. (replace-match "")
  11248. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11249. (equal (char-before) ?\ ))
  11250. (backward-delete-char 1)
  11251. (if (string-match "^[ \t]*$" (buffer-substring
  11252. (point-at-bol) (point-at-eol)))
  11253. (delete-region (point-at-bol)
  11254. (min (point-max) (1+ (point-at-eol))))))))))
  11255. (defun org-add-planning-info (what &optional time &rest remove)
  11256. "Insert new timestamp with keyword in the line directly after the headline.
  11257. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11258. If non is given, the user is prompted for a date.
  11259. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11260. be removed."
  11261. (interactive)
  11262. (let (org-time-was-given org-end-time-was-given ts
  11263. end default-time default-input)
  11264. (catch 'exit
  11265. (when (and (memq what '(scheduled deadline))
  11266. (or (not time)
  11267. (and (stringp time)
  11268. (string-match "^[-+]+[0-9]" time))))
  11269. ;; Try to get a default date/time from existing timestamp
  11270. (save-excursion
  11271. (org-back-to-heading t)
  11272. (setq end (save-excursion (outline-next-heading) (point)))
  11273. (when (re-search-forward (if (eq what 'scheduled)
  11274. org-scheduled-time-regexp
  11275. org-deadline-time-regexp)
  11276. end t)
  11277. (setq ts (match-string 1)
  11278. default-time
  11279. (apply 'encode-time (org-parse-time-string ts))
  11280. default-input (and ts (org-get-compact-tod ts))))))
  11281. (when what
  11282. (setq time
  11283. (if (stringp time)
  11284. ;; This is a string (relative or absolute), set proper date
  11285. (apply 'encode-time
  11286. (org-read-date-analyze
  11287. time default-time (decode-time default-time)))
  11288. ;; If necessary, get the time from the user
  11289. (or time (org-read-date nil 'to-time nil nil
  11290. default-time default-input)))))
  11291. (when (and org-insert-labeled-timestamps-at-point
  11292. (member what '(scheduled deadline)))
  11293. (insert
  11294. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11295. (org-insert-time-stamp time org-time-was-given
  11296. nil nil nil (list org-end-time-was-given))
  11297. (setq what nil))
  11298. (save-excursion
  11299. (save-restriction
  11300. (let (col list elt ts buffer-invisibility-spec)
  11301. (org-back-to-heading t)
  11302. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11303. (goto-char (match-end 1))
  11304. (setq col (current-column))
  11305. (goto-char (match-end 0))
  11306. (if (eobp) (insert "\n") (forward-char 1))
  11307. (when (and (not what)
  11308. (not (looking-at
  11309. (concat "[ \t]*"
  11310. org-keyword-time-not-clock-regexp))))
  11311. ;; Nothing to add, nothing to remove...... :-)
  11312. (throw 'exit nil))
  11313. (if (and (not (looking-at org-outline-regexp))
  11314. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11315. "[^\r\n]*"))
  11316. (not (equal (match-string 1) org-clock-string)))
  11317. (narrow-to-region (match-beginning 0) (match-end 0))
  11318. (insert-before-markers "\n")
  11319. (backward-char 1)
  11320. (narrow-to-region (point) (point))
  11321. (and org-adapt-indentation (org-indent-to-column col)))
  11322. ;; Check if we have to remove something.
  11323. (setq list (cons what remove))
  11324. (while list
  11325. (setq elt (pop list))
  11326. (when (or (and (eq elt 'scheduled)
  11327. (re-search-forward org-scheduled-time-regexp nil t))
  11328. (and (eq elt 'deadline)
  11329. (re-search-forward org-deadline-time-regexp nil t))
  11330. (and (eq elt 'closed)
  11331. (re-search-forward org-closed-time-regexp nil t)))
  11332. (replace-match "")
  11333. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11334. (and (looking-at "[ \t]+") (replace-match ""))
  11335. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11336. (when what
  11337. (insert
  11338. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11339. (cond ((eq what 'scheduled) org-scheduled-string)
  11340. ((eq what 'deadline) org-deadline-string)
  11341. ((eq what 'closed) org-closed-string))
  11342. " ")
  11343. (setq ts (org-insert-time-stamp
  11344. time
  11345. (or org-time-was-given
  11346. (and (eq what 'closed) org-log-done-with-time))
  11347. (eq what 'closed)
  11348. nil nil (list org-end-time-was-given)))
  11349. (insert
  11350. (if (not (or (bolp) (eq (char-before) ?\ )
  11351. (memq (char-after) '(32 10))
  11352. (eobp))) " " ""))
  11353. (end-of-line 1))
  11354. (goto-char (point-min))
  11355. (widen)
  11356. (if (and (looking-at "[ \t]*\n")
  11357. (equal (char-before) ?\n))
  11358. (delete-region (1- (point)) (point-at-eol)))
  11359. ts))))))
  11360. (defvar org-log-note-marker (make-marker))
  11361. (defvar org-log-note-purpose nil)
  11362. (defvar org-log-note-state nil)
  11363. (defvar org-log-note-previous-state nil)
  11364. (defvar org-log-note-how nil)
  11365. (defvar org-log-note-extra nil)
  11366. (defvar org-log-note-window-configuration nil)
  11367. (defvar org-log-note-return-to (make-marker))
  11368. (defvar org-log-note-effective-time nil
  11369. "Remembered current time so that dynamically scoped
  11370. `org-extend-today-until' affects tha timestamps in state change
  11371. log")
  11372. (defvar org-log-post-message nil
  11373. "Message to be displayed after a log note has been stored.
  11374. The auto-repeater uses this.")
  11375. (defun org-add-note ()
  11376. "Add a note to the current entry.
  11377. This is done in the same way as adding a state change note."
  11378. (interactive)
  11379. (org-add-log-setup 'note nil nil 'findpos nil))
  11380. (defvar org-property-end-re)
  11381. (defun org-add-log-setup (&optional purpose state prev-state
  11382. findpos how extra)
  11383. "Set up the post command hook to take a note.
  11384. If this is about to TODO state change, the new state is expected in STATE.
  11385. When FINDPOS is non-nil, find the correct position for the note in
  11386. the current entry. If not, assume that it can be inserted at point.
  11387. HOW is an indicator what kind of note should be created.
  11388. EXTRA is additional text that will be inserted into the notes buffer."
  11389. (let* ((org-log-into-drawer (org-log-into-drawer))
  11390. (drawer (cond ((stringp org-log-into-drawer)
  11391. org-log-into-drawer)
  11392. (org-log-into-drawer "LOGBOOK"))))
  11393. (save-restriction
  11394. (save-excursion
  11395. (when findpos
  11396. (org-back-to-heading t)
  11397. (narrow-to-region (point) (save-excursion
  11398. (outline-next-heading) (point)))
  11399. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11400. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11401. "[^\r\n]*\\)?"))
  11402. (goto-char (match-end 0))
  11403. (cond
  11404. (drawer
  11405. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11406. nil t)
  11407. (progn
  11408. (goto-char (match-end 0))
  11409. (or org-log-states-order-reversed
  11410. (and (re-search-forward org-property-end-re nil t)
  11411. (goto-char (1- (match-beginning 0))))))
  11412. (insert "\n:" drawer ":\n:END:")
  11413. (beginning-of-line 0)
  11414. (org-indent-line)
  11415. (beginning-of-line 2)
  11416. (org-indent-line)
  11417. (end-of-line 0)))
  11418. ((and org-log-state-notes-insert-after-drawers
  11419. (save-excursion
  11420. (forward-line) (looking-at org-drawer-regexp)))
  11421. (forward-line)
  11422. (while (looking-at org-drawer-regexp)
  11423. (goto-char (match-end 0))
  11424. (re-search-forward org-property-end-re (point-max) t)
  11425. (forward-line))
  11426. (forward-line -1)))
  11427. (unless org-log-states-order-reversed
  11428. (and (= (char-after) ?\n) (forward-char 1))
  11429. (org-skip-over-state-notes)
  11430. (skip-chars-backward " \t\n\r")))
  11431. (move-marker org-log-note-marker (point))
  11432. (setq org-log-note-purpose purpose
  11433. org-log-note-state state
  11434. org-log-note-previous-state prev-state
  11435. org-log-note-how how
  11436. org-log-note-extra extra
  11437. org-log-note-effective-time (org-current-effective-time))
  11438. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11439. (defun org-skip-over-state-notes ()
  11440. "Skip past the list of State notes in an entry."
  11441. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11442. (when (ignore-errors (goto-char (org-in-item-p)))
  11443. (let* ((struct (org-list-struct))
  11444. (prevs (org-list-prevs-alist struct)))
  11445. (while (looking-at "[ \t]*- State")
  11446. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11447. (org-list-get-item-end (point) struct)))))))
  11448. (defun org-add-log-note (&optional purpose)
  11449. "Pop up a window for taking a note, and add this note later at point."
  11450. (remove-hook 'post-command-hook 'org-add-log-note)
  11451. (setq org-log-note-window-configuration (current-window-configuration))
  11452. (delete-other-windows)
  11453. (move-marker org-log-note-return-to (point))
  11454. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11455. (goto-char org-log-note-marker)
  11456. (org-switch-to-buffer-other-window "*Org Note*")
  11457. (erase-buffer)
  11458. (if (memq org-log-note-how '(time state))
  11459. (let (current-prefix-arg) (org-store-log-note))
  11460. (let ((org-inhibit-startup t)) (org-mode))
  11461. (insert (format "# Insert note for %s.
  11462. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11463. (cond
  11464. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11465. ((eq org-log-note-purpose 'done) "closed todo item")
  11466. ((eq org-log-note-purpose 'state)
  11467. (format "state change from \"%s\" to \"%s\""
  11468. (or org-log-note-previous-state "")
  11469. (or org-log-note-state "")))
  11470. ((eq org-log-note-purpose 'reschedule)
  11471. "rescheduling")
  11472. ((eq org-log-note-purpose 'delschedule)
  11473. "no longer scheduled")
  11474. ((eq org-log-note-purpose 'redeadline)
  11475. "changing deadline")
  11476. ((eq org-log-note-purpose 'deldeadline)
  11477. "removing deadline")
  11478. ((eq org-log-note-purpose 'refile)
  11479. "refiling")
  11480. ((eq org-log-note-purpose 'note)
  11481. "this entry")
  11482. (t (error "This should not happen")))))
  11483. (if org-log-note-extra (insert org-log-note-extra))
  11484. (org-set-local 'org-finish-function 'org-store-log-note)
  11485. (run-hooks 'org-log-buffer-setup-hook)))
  11486. (defvar org-note-abort nil) ; dynamically scoped
  11487. (defun org-store-log-note ()
  11488. "Finish taking a log note, and insert it to where it belongs."
  11489. (let ((txt (buffer-string))
  11490. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11491. lines ind bul)
  11492. (kill-buffer (current-buffer))
  11493. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11494. (setq txt (replace-match "" t t txt)))
  11495. (if (string-match "\\s-+\\'" txt)
  11496. (setq txt (replace-match "" t t txt)))
  11497. (setq lines (org-split-string txt "\n"))
  11498. (when (and note (string-match "\\S-" note))
  11499. (setq note
  11500. (org-replace-escapes
  11501. note
  11502. (list (cons "%u" (user-login-name))
  11503. (cons "%U" user-full-name)
  11504. (cons "%t" (format-time-string
  11505. (org-time-stamp-format 'long 'inactive)
  11506. org-log-note-effective-time))
  11507. (cons "%T" (format-time-string
  11508. (org-time-stamp-format 'long nil)
  11509. org-log-note-effective-time))
  11510. (cons "%d" (format-time-string
  11511. (org-time-stamp-format nil 'inactive)
  11512. org-log-note-effective-time))
  11513. (cons "%D" (format-time-string
  11514. (org-time-stamp-format nil nil)
  11515. org-log-note-effective-time))
  11516. (cons "%s" (if org-log-note-state
  11517. (concat "\"" org-log-note-state "\"")
  11518. ""))
  11519. (cons "%S" (if org-log-note-previous-state
  11520. (concat "\"" org-log-note-previous-state "\"")
  11521. "\"\"")))))
  11522. (if lines (setq note (concat note " \\\\")))
  11523. (push note lines))
  11524. (when (or current-prefix-arg org-note-abort)
  11525. (when org-log-into-drawer
  11526. (org-remove-empty-drawer-at
  11527. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11528. org-log-note-marker))
  11529. (setq lines nil))
  11530. (when lines
  11531. (with-current-buffer (marker-buffer org-log-note-marker)
  11532. (save-excursion
  11533. (goto-char org-log-note-marker)
  11534. (move-marker org-log-note-marker nil)
  11535. (end-of-line 1)
  11536. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11537. (setq ind (save-excursion
  11538. (if (ignore-errors (goto-char (org-in-item-p)))
  11539. (let ((struct (org-list-struct)))
  11540. (org-list-get-ind
  11541. (org-list-get-top-point struct) struct))
  11542. (skip-chars-backward " \r\t\n")
  11543. (cond
  11544. ((and (org-at-heading-p)
  11545. org-adapt-indentation)
  11546. (1+ (org-current-level)))
  11547. ((org-at-heading-p) 0)
  11548. (t (org-get-indentation))))))
  11549. (setq bul (org-list-bullet-string "-"))
  11550. (org-indent-line-to ind)
  11551. (insert bul (pop lines))
  11552. (let ((ind-body (+ (length bul) ind)))
  11553. (while lines
  11554. (insert "\n")
  11555. (org-indent-line-to ind-body)
  11556. (insert (pop lines))))
  11557. (message "Note stored")
  11558. (org-back-to-heading t)
  11559. (org-cycle-hide-drawers 'children)))))
  11560. (set-window-configuration org-log-note-window-configuration)
  11561. (with-current-buffer (marker-buffer org-log-note-return-to)
  11562. (goto-char org-log-note-return-to))
  11563. (move-marker org-log-note-return-to nil)
  11564. (and org-log-post-message (message "%s" org-log-post-message)))
  11565. (defun org-remove-empty-drawer-at (drawer pos)
  11566. "Remove an empty drawer DRAWER at position POS.
  11567. POS may also be a marker."
  11568. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11569. (save-excursion
  11570. (save-restriction
  11571. (widen)
  11572. (goto-char pos)
  11573. (if (org-in-regexp
  11574. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11575. (replace-match ""))))))
  11576. (defvar org-ts-type nil)
  11577. (defun org-sparse-tree (&optional arg type)
  11578. "Create a sparse tree, prompt for the details.
  11579. This command can create sparse trees. You first need to select the type
  11580. of match used to create the tree:
  11581. t Show all TODO entries.
  11582. T Show entries with a specific TODO keyword.
  11583. m Show entries selected by a tags/property match.
  11584. p Enter a property name and its value (both with completion on existing
  11585. names/values) and show entries with that property.
  11586. r Show entries matching a regular expression (`/' can be used as well).
  11587. b Show deadlines and scheduled items before a date.
  11588. a Show deadlines and scheduled items after a date.
  11589. d Show deadlines due within `org-deadline-warning-days'.
  11590. D Show deadlines and scheduled items between a date range."
  11591. (interactive "P")
  11592. (let (ans kwd value ts-type)
  11593. (setq type (or type org-sparse-tree-default-date-type))
  11594. (setq org-ts-type type)
  11595. (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"
  11596. (cond ((eq type 'all) "all timestamps")
  11597. ((eq type 'scheduled) "only scheduled")
  11598. ((eq type 'deadline) "only deadline")
  11599. ((eq type 'active) "only active timestamps")
  11600. ((eq type 'inactive) "only inactive timestamps")
  11601. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11602. (t "scheduled/deadline")))
  11603. (setq ans (read-char-exclusive))
  11604. (cond
  11605. ((equal ans ?c)
  11606. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11607. ((equal ans ?d)
  11608. (call-interactively 'org-check-deadlines))
  11609. ((equal ans ?b)
  11610. (call-interactively 'org-check-before-date))
  11611. ((equal ans ?a)
  11612. (call-interactively 'org-check-after-date))
  11613. ((equal ans ?D)
  11614. (call-interactively 'org-check-dates-range))
  11615. ((equal ans ?t)
  11616. (org-show-todo-tree nil))
  11617. ((equal ans ?T)
  11618. (org-show-todo-tree '(4)))
  11619. ((member ans '(?T ?m))
  11620. (call-interactively 'org-match-sparse-tree))
  11621. ((member ans '(?p ?P))
  11622. (setq kwd (org-icompleting-read "Property: "
  11623. (mapcar 'list (org-buffer-property-keys))))
  11624. (setq value (org-icompleting-read "Value: "
  11625. (mapcar 'list (org-property-values kwd))))
  11626. (unless (string-match "\\`{.*}\\'" value)
  11627. (setq value (concat "\"" value "\"")))
  11628. (org-match-sparse-tree arg (concat kwd "=" value)))
  11629. ((member ans '(?r ?R ?/))
  11630. (call-interactively 'org-occur))
  11631. (t (error "No such sparse tree command \"%c\"" ans)))))
  11632. (defvar org-occur-highlights nil
  11633. "List of overlays used for occur matches.")
  11634. (make-variable-buffer-local 'org-occur-highlights)
  11635. (defvar org-occur-parameters nil
  11636. "Parameters of the active org-occur calls.
  11637. This is a list, each call to org-occur pushes as cons cell,
  11638. containing the regular expression and the callback, onto the list.
  11639. The list can contain several entries if `org-occur' has been called
  11640. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11641. will only contain one set of parameters. When the highlights are
  11642. removed (for example with `C-c C-c', or with the next edit (depending
  11643. on `org-remove-highlights-with-change'), this variable is emptied
  11644. as well.")
  11645. (make-variable-buffer-local 'org-occur-parameters)
  11646. (defun org-occur (regexp &optional keep-previous callback)
  11647. "Make a compact tree which shows all matches of REGEXP.
  11648. The tree will show the lines where the regexp matches, and all higher
  11649. headlines above the match. It will also show the heading after the match,
  11650. to make sure editing the matching entry is easy.
  11651. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11652. call to `org-occur' will be kept, to allow stacking of calls to this
  11653. command.
  11654. If CALLBACK is non-nil, it is a function which is called to confirm
  11655. that the match should indeed be shown."
  11656. (interactive "sRegexp: \nP")
  11657. (when (equal regexp "")
  11658. (error "Regexp cannot be empty"))
  11659. (unless keep-previous
  11660. (org-remove-occur-highlights nil nil t))
  11661. (push (cons regexp callback) org-occur-parameters)
  11662. (let ((cnt 0))
  11663. (save-excursion
  11664. (goto-char (point-min))
  11665. (if (or (not keep-previous) ; do not want to keep
  11666. (not org-occur-highlights)) ; no previous matches
  11667. ;; hide everything
  11668. (org-overview))
  11669. (while (re-search-forward regexp nil t)
  11670. (when (or (not callback)
  11671. (save-match-data (funcall callback)))
  11672. (setq cnt (1+ cnt))
  11673. (when org-highlight-sparse-tree-matches
  11674. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11675. (org-show-context 'occur-tree))))
  11676. (when org-remove-highlights-with-change
  11677. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11678. nil 'local))
  11679. (unless org-sparse-tree-open-archived-trees
  11680. (org-hide-archived-subtrees (point-min) (point-max)))
  11681. (run-hooks 'org-occur-hook)
  11682. (if (org-called-interactively-p 'interactive)
  11683. (message "%d match(es) for regexp %s" cnt regexp))
  11684. cnt))
  11685. (defun org-occur-next-match (&optional n reset)
  11686. "Function for `next-error-function' to find sparse tree matches.
  11687. N is the number of matches to move, when negative move backwards.
  11688. RESET is entirely ignored - this function always goes back to the
  11689. starting point when no match is found."
  11690. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11691. (search-func (if (< n 0)
  11692. 'previous-single-char-property-change
  11693. 'next-single-char-property-change))
  11694. (n (abs n))
  11695. (pos (point))
  11696. p1)
  11697. (catch 'exit
  11698. (while (setq p1 (funcall search-func (point) 'org-type))
  11699. (when (equal p1 limit)
  11700. (goto-char pos)
  11701. (error "No more matches"))
  11702. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11703. (setq n (1- n))
  11704. (when (= n 0)
  11705. (goto-char p1)
  11706. (throw 'exit (point))))
  11707. (goto-char p1))
  11708. (goto-char p1)
  11709. (error "No more matches"))))
  11710. (defun org-show-context (&optional key)
  11711. "Make sure point and context are visible.
  11712. How much context is shown depends upon the variables
  11713. `org-show-hierarchy-above', `org-show-following-heading',
  11714. `org-show-entry-below' and `org-show-siblings'."
  11715. (let ((heading-p (org-at-heading-p t))
  11716. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11717. (following-p (org-get-alist-option org-show-following-heading key))
  11718. (entry-p (org-get-alist-option org-show-entry-below key))
  11719. (siblings-p (org-get-alist-option org-show-siblings key)))
  11720. (catch 'exit
  11721. ;; Show heading or entry text
  11722. (if (and heading-p (not entry-p))
  11723. (org-flag-heading nil) ; only show the heading
  11724. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11725. (org-show-hidden-entry))) ; show entire entry
  11726. (when following-p
  11727. ;; Show next sibling, or heading below text
  11728. (save-excursion
  11729. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11730. (org-flag-heading nil))))
  11731. (when siblings-p (org-show-siblings))
  11732. (when hierarchy-p
  11733. ;; show all higher headings, possibly with siblings
  11734. (save-excursion
  11735. (while (and (condition-case nil
  11736. (progn (org-up-heading-all 1) t)
  11737. (error nil))
  11738. (not (bobp)))
  11739. (org-flag-heading nil)
  11740. (when siblings-p (org-show-siblings))))))))
  11741. (defvar org-reveal-start-hook nil
  11742. "Hook run before revealing a location.")
  11743. (defun org-reveal (&optional siblings)
  11744. "Show current entry, hierarchy above it, and the following headline.
  11745. This can be used to show a consistent set of context around locations
  11746. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11747. not t for the search context.
  11748. With optional argument SIBLINGS, on each level of the hierarchy all
  11749. siblings are shown. This repairs the tree structure to what it would
  11750. look like when opened with hierarchical calls to `org-cycle'.
  11751. With double optional argument \\[universal-argument] \\[universal-argument], \
  11752. go to the parent and show the
  11753. entire tree."
  11754. (interactive "P")
  11755. (run-hooks 'org-reveal-start-hook)
  11756. (let ((org-show-hierarchy-above t)
  11757. (org-show-following-heading t)
  11758. (org-show-siblings (if siblings t org-show-siblings)))
  11759. (org-show-context nil))
  11760. (when (equal siblings '(16))
  11761. (save-excursion
  11762. (when (org-up-heading-safe)
  11763. (org-show-subtree)
  11764. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11765. (defun org-highlight-new-match (beg end)
  11766. "Highlight from BEG to END and mark the highlight is an occur headline."
  11767. (let ((ov (make-overlay beg end)))
  11768. (overlay-put ov 'face 'secondary-selection)
  11769. (overlay-put ov 'org-type 'org-occur)
  11770. (push ov org-occur-highlights)))
  11771. (defun org-remove-occur-highlights (&optional beg end noremove)
  11772. "Remove the occur highlights from the buffer.
  11773. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11774. from the `before-change-functions' in the current buffer."
  11775. (interactive)
  11776. (unless org-inhibit-highlight-removal
  11777. (mapc 'delete-overlay org-occur-highlights)
  11778. (setq org-occur-highlights nil)
  11779. (setq org-occur-parameters nil)
  11780. (unless noremove
  11781. (remove-hook 'before-change-functions
  11782. 'org-remove-occur-highlights 'local))))
  11783. ;;;; Priorities
  11784. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11785. "Regular expression matching the priority indicator.")
  11786. (defvar org-remove-priority-next-time nil)
  11787. (defun org-priority-up ()
  11788. "Increase the priority of the current item."
  11789. (interactive)
  11790. (org-priority 'up))
  11791. (defun org-priority-down ()
  11792. "Decrease the priority of the current item."
  11793. (interactive)
  11794. (org-priority 'down))
  11795. (defun org-priority (&optional action show)
  11796. "Change the priority of an item.
  11797. ACTION can be `set', `up', `down', or a character."
  11798. (interactive "P")
  11799. (if (equal action '(4))
  11800. (org-show-priority)
  11801. (unless org-enable-priority-commands
  11802. (error "Priority commands are disabled"))
  11803. (setq action (or action 'set))
  11804. (let (current new news have remove)
  11805. (save-excursion
  11806. (org-back-to-heading t)
  11807. (if (looking-at org-priority-regexp)
  11808. (setq current (string-to-char (match-string 2))
  11809. have t))
  11810. (cond
  11811. ((eq action 'remove)
  11812. (setq remove t new ?\ ))
  11813. ((or (eq action 'set)
  11814. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11815. (if (not (eq action 'set))
  11816. (setq new action)
  11817. (message "Priority %c-%c, SPC to remove: "
  11818. org-highest-priority org-lowest-priority)
  11819. (save-match-data
  11820. (setq new (read-char-exclusive))))
  11821. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11822. (= (upcase org-lowest-priority) org-lowest-priority))
  11823. (setq new (upcase new)))
  11824. (cond ((equal new ?\ ) (setq remove t))
  11825. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11826. (error "Priority must be between `%c' and `%c'"
  11827. org-highest-priority org-lowest-priority))))
  11828. ((eq action 'up)
  11829. (setq new (if have
  11830. (1- current) ; normal cycling
  11831. ;; last priority was empty
  11832. (if (eq last-command this-command)
  11833. org-lowest-priority ; wrap around empty to lowest
  11834. ;; default
  11835. (if org-priority-start-cycle-with-default
  11836. org-default-priority
  11837. (1- org-default-priority))))))
  11838. ((eq action 'down)
  11839. (setq new (if have
  11840. (1+ current) ; normal cycling
  11841. ;; last priority was empty
  11842. (if (eq last-command this-command)
  11843. org-highest-priority ; wrap around empty to highest
  11844. ;; default
  11845. (if org-priority-start-cycle-with-default
  11846. org-default-priority
  11847. (1+ org-default-priority))))))
  11848. (t (error "Invalid action")))
  11849. (if (or (< (upcase new) org-highest-priority)
  11850. (> (upcase new) org-lowest-priority))
  11851. (if (and (memq action '(up down))
  11852. (not have) (not (eq last-command this-command)))
  11853. ;; `new' is from default priority
  11854. (error
  11855. "The default can not be set, see `org-default-priority' why")
  11856. ;; normal cycling: `new' is beyond highest/lowest priority
  11857. ;; and is wrapped around to the empty priority
  11858. (setq remove t)))
  11859. (setq news (format "%c" new))
  11860. (if have
  11861. (if remove
  11862. (replace-match "" t t nil 1)
  11863. (replace-match news t t nil 2))
  11864. (if remove
  11865. (error "No priority cookie found in line")
  11866. (let ((case-fold-search nil))
  11867. (looking-at org-todo-line-regexp))
  11868. (if (match-end 2)
  11869. (progn
  11870. (goto-char (match-end 2))
  11871. (insert " [#" news "]"))
  11872. (goto-char (match-beginning 3))
  11873. (insert "[#" news "] "))))
  11874. (org-preserve-lc (org-set-tags nil 'align)))
  11875. (if remove
  11876. (message "Priority removed")
  11877. (message "Priority of current item set to %s" news)))))
  11878. (defun org-show-priority ()
  11879. "Show the priority of the current item.
  11880. This priority is composed of the main priority given with the [#A] cookies,
  11881. and by additional input from the age of a schedules or deadline entry."
  11882. (interactive)
  11883. (let ((pri (if (eq major-mode 'org-agenda-mode)
  11884. (org-get-at-bol 'priority)
  11885. (save-excursion
  11886. (save-match-data
  11887. (beginning-of-line)
  11888. (and (looking-at org-heading-regexp)
  11889. (org-get-priority (match-string 0))))))))
  11890. (message "Priority is %d" (if pri pri -1000))))
  11891. (defun org-get-priority (s)
  11892. "Find priority cookie and return priority."
  11893. (if (functionp org-get-priority-function)
  11894. (funcall org-get-priority-function)
  11895. (save-match-data
  11896. (if (not (string-match org-priority-regexp s))
  11897. (* 1000 (- org-lowest-priority org-default-priority))
  11898. (* 1000 (- org-lowest-priority
  11899. (string-to-char (match-string 2 s))))))))
  11900. ;;;; Tags
  11901. (defvar org-agenda-archives-mode)
  11902. (defvar org-map-continue-from nil
  11903. "Position from where mapping should continue.
  11904. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11905. (defvar org-scanner-tags nil
  11906. "The current tag list while the tags scanner is running.")
  11907. (defvar org-trust-scanner-tags nil
  11908. "Should `org-get-tags-at' use the tags for the scanner.
  11909. This is for internal dynamical scoping only.
  11910. When this is non-nil, the function `org-get-tags-at' will return the value
  11911. of `org-scanner-tags' instead of building the list by itself. This
  11912. can lead to large speed-ups when the tags scanner is used in a file with
  11913. many entries, and when the list of tags is retrieved, for example to
  11914. obtain a list of properties. Building the tags list for each entry in such
  11915. a file becomes an N^2 operation - but with this variable set, it scales
  11916. as N.")
  11917. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11918. "Scan headline tags with inheritance and produce output ACTION.
  11919. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11920. or `agenda' to produce an entry list for an agenda view. It can also be
  11921. a Lisp form or a function that should be called at each matched headline, in
  11922. this case the return value is a list of all return values from these calls.
  11923. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11924. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11925. only lines with a not-done TODO keyword are included in the output.
  11926. This should be the same variable that was scoped into
  11927. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11928. START-LEVEL can be a string with asterisks, reducing the scope to
  11929. headlines matching this string."
  11930. (require 'org-agenda)
  11931. (let* ((re (concat "^"
  11932. (if start-level
  11933. ;; Get the correct level to match
  11934. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11935. org-outline-regexp)
  11936. " *\\(\\<\\("
  11937. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11938. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11939. (props (list 'face 'default
  11940. 'done-face 'org-agenda-done
  11941. 'undone-face 'default
  11942. 'mouse-face 'highlight
  11943. 'org-not-done-regexp org-not-done-regexp
  11944. 'org-todo-regexp org-todo-regexp
  11945. 'org-complex-heading-regexp org-complex-heading-regexp
  11946. 'help-echo
  11947. (format "mouse-2 or RET jump to org file %s"
  11948. (abbreviate-file-name
  11949. (or (buffer-file-name (buffer-base-buffer))
  11950. (buffer-name (buffer-base-buffer)))))))
  11951. (case-fold-search nil)
  11952. (org-map-continue-from nil)
  11953. lspos tags tags-list
  11954. (tags-alist (list (cons 0 org-file-tags)))
  11955. (llast 0) rtn rtn1 level category i txt
  11956. todo marker entry priority)
  11957. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11958. (setq action (list 'lambda nil action)))
  11959. (save-excursion
  11960. (goto-char (point-min))
  11961. (when (eq action 'sparse-tree)
  11962. (org-overview)
  11963. (org-remove-occur-highlights))
  11964. (while (re-search-forward re nil t)
  11965. (setq org-map-continue-from nil)
  11966. (catch :skip
  11967. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11968. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11969. (goto-char (setq lspos (match-beginning 0)))
  11970. (setq level (org-reduced-level (org-outline-level))
  11971. category (org-get-category))
  11972. (setq i llast llast level)
  11973. ;; remove tag lists from same and sublevels
  11974. (while (>= i level)
  11975. (when (setq entry (assoc i tags-alist))
  11976. (setq tags-alist (delete entry tags-alist)))
  11977. (setq i (1- i)))
  11978. ;; add the next tags
  11979. (when tags
  11980. (setq tags (org-split-string tags ":")
  11981. tags-alist
  11982. (cons (cons level tags) tags-alist)))
  11983. ;; compile tags for current headline
  11984. (setq tags-list
  11985. (if org-use-tag-inheritance
  11986. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11987. tags)
  11988. org-scanner-tags tags-list)
  11989. (when org-use-tag-inheritance
  11990. (setcdr (car tags-alist)
  11991. (mapcar (lambda (x)
  11992. (setq x (copy-sequence x))
  11993. (org-add-prop-inherited x))
  11994. (cdar tags-alist))))
  11995. (when (and tags org-use-tag-inheritance
  11996. (or (not (eq t org-use-tag-inheritance))
  11997. org-tags-exclude-from-inheritance))
  11998. ;; selective inheritance, remove uninherited ones
  11999. (setcdr (car tags-alist)
  12000. (org-remove-uninherited-tags (cdar tags-alist))))
  12001. (when (and
  12002. ;; eval matcher only when the todo condition is OK
  12003. (and (or (not todo-only) (member todo org-not-done-keywords))
  12004. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12005. (eval matcher)))
  12006. ;; Call the skipper, but return t if it does not skip,
  12007. ;; so that the `and' form continues evaluating
  12008. (progn
  12009. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12010. t)
  12011. ;; Check if timestamps are deselecting this entry
  12012. (or (not todo-only)
  12013. (and (member todo org-not-done-keywords)
  12014. (or (not org-agenda-tags-todo-honor-ignore-options)
  12015. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  12016. ;; Extra check for the archive tag
  12017. ;; FIXME: Does the skipper already do this????
  12018. (or
  12019. (not (member org-archive-tag tags-list))
  12020. ;; we have an archive tag, should we use this anyway?
  12021. (or (not org-agenda-skip-archived-trees)
  12022. (and (eq action 'agenda) org-agenda-archives-mode))))
  12023. ;; select this headline
  12024. (cond
  12025. ((eq action 'sparse-tree)
  12026. (and org-highlight-sparse-tree-matches
  12027. (org-get-heading) (match-end 0)
  12028. (org-highlight-new-match
  12029. (match-beginning 1) (match-end 1)))
  12030. (org-show-context 'tags-tree))
  12031. ((eq action 'agenda)
  12032. (setq txt (org-agenda-format-item
  12033. ""
  12034. (concat
  12035. (if (eq org-tags-match-list-sublevels 'indented)
  12036. (make-string (1- level) ?.) "")
  12037. (org-get-heading))
  12038. level category
  12039. tags-list)
  12040. priority (org-get-priority txt))
  12041. (goto-char lspos)
  12042. (setq marker (org-agenda-new-marker))
  12043. (org-add-props txt props
  12044. 'org-marker marker 'org-hd-marker marker 'org-category category
  12045. 'todo-state todo
  12046. 'priority priority 'type "tagsmatch")
  12047. (push txt rtn))
  12048. ((functionp action)
  12049. (setq org-map-continue-from nil)
  12050. (save-excursion
  12051. (setq rtn1 (funcall action))
  12052. (push rtn1 rtn)))
  12053. (t (error "Invalid action")))
  12054. ;; if we are to skip sublevels, jump to end of subtree
  12055. (unless org-tags-match-list-sublevels
  12056. (org-end-of-subtree t)
  12057. (backward-char 1))))
  12058. ;; Get the correct position from where to continue
  12059. (if org-map-continue-from
  12060. (goto-char org-map-continue-from)
  12061. (and (= (point) lspos) (end-of-line 1)))))
  12062. (when (and (eq action 'sparse-tree)
  12063. (not org-sparse-tree-open-archived-trees))
  12064. (org-hide-archived-subtrees (point-min) (point-max)))
  12065. (nreverse rtn)))
  12066. (defun org-remove-uninherited-tags (tags)
  12067. "Remove all tags that are not inherited from the list TAGS."
  12068. (cond
  12069. ((eq org-use-tag-inheritance t)
  12070. (if org-tags-exclude-from-inheritance
  12071. (org-delete-all org-tags-exclude-from-inheritance tags)
  12072. tags))
  12073. ((not org-use-tag-inheritance) nil)
  12074. ((stringp org-use-tag-inheritance)
  12075. (delq nil (mapcar
  12076. (lambda (x)
  12077. (if (and (string-match org-use-tag-inheritance x)
  12078. (not (member x org-tags-exclude-from-inheritance)))
  12079. x nil))
  12080. tags)))
  12081. ((listp org-use-tag-inheritance)
  12082. (delq nil (mapcar
  12083. (lambda (x)
  12084. (if (member x org-use-tag-inheritance) x nil))
  12085. tags)))))
  12086. (defun org-match-sparse-tree (&optional todo-only match)
  12087. "Create a sparse tree according to tags string MATCH.
  12088. MATCH can contain positive and negative selection of tags, like
  12089. \"+WORK+URGENT-WITHBOSS\".
  12090. If optional argument TODO-ONLY is non-nil, only select lines that are
  12091. also TODO lines."
  12092. (interactive "P")
  12093. (org-agenda-prepare-buffers (list (current-buffer)))
  12094. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12095. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12096. (defvar org-cached-props nil)
  12097. (defun org-cached-entry-get (pom property)
  12098. (if (or (eq t org-use-property-inheritance)
  12099. (and (stringp org-use-property-inheritance)
  12100. (string-match org-use-property-inheritance property))
  12101. (and (listp org-use-property-inheritance)
  12102. (member property org-use-property-inheritance)))
  12103. ;; Caching is not possible, check it directly
  12104. (org-entry-get pom property 'inherit)
  12105. ;; Get all properties, so that we can do complicated checks easily
  12106. (cdr (assoc property (or org-cached-props
  12107. (setq org-cached-props
  12108. (org-entry-properties pom)))))))
  12109. (defun org-global-tags-completion-table (&optional files)
  12110. "Return the list of all tags in all agenda buffer/files.
  12111. Optional FILES argument is a list of files which can be used
  12112. instead of the agenda files."
  12113. (save-excursion
  12114. (org-uniquify
  12115. (delq nil
  12116. (apply 'append
  12117. (mapcar
  12118. (lambda (file)
  12119. (set-buffer (find-file-noselect file))
  12120. (append (org-get-buffer-tags)
  12121. (mapcar (lambda (x) (if (stringp (car-safe x))
  12122. (list (car-safe x)) nil))
  12123. org-tag-alist)))
  12124. (if (and files (car files))
  12125. files
  12126. (org-agenda-files))))))))
  12127. (defun org-make-tags-matcher (match)
  12128. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12129. The variable `todo-only' is scoped dynamically into this function.
  12130. It will be set to t if the matcher restricts matching to TODO entries,
  12131. otherwise will not be touched.
  12132. Returns a cons of the selection string MATCH and the constructed
  12133. lisp form implementing the matcher. The matcher is to be evaluated
  12134. at an Org entry, with point on the headline, and returns t if the
  12135. entry matches the selection string MATCH. The returned lisp form
  12136. references two variables with information about the entry, which
  12137. must be bound around the form's evaluation: todo, the TODO keyword
  12138. at the entry (or nil of none); and tags-list, the list of all tags
  12139. at the entry including inherited ones. Additionally, the category
  12140. of the entry (if any) must be specified as the text property
  12141. 'org-category on the headline.
  12142. See also `org-scan-tags'.
  12143. "
  12144. (declare (special todo-only))
  12145. (unless (boundp 'todo-only)
  12146. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12147. (unless match
  12148. ;; Get a new match request, with completion
  12149. (let ((org-last-tags-completion-table
  12150. (org-global-tags-completion-table)))
  12151. (setq match (org-completing-read-no-i
  12152. "Match: " 'org-tags-completion-function nil nil nil
  12153. 'org-tags-history))))
  12154. ;; Parse the string and create a lisp form
  12155. (let ((match0 match)
  12156. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12157. minus tag mm
  12158. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12159. orterms term orlist re-p str-p level-p level-op time-p
  12160. prop-p pn pv po gv rest)
  12161. (if (string-match "/+" match)
  12162. ;; match contains also a todo-matching request
  12163. (progn
  12164. (setq tagsmatch (substring match 0 (match-beginning 0))
  12165. todomatch (substring match (match-end 0)))
  12166. (if (string-match "^!" todomatch)
  12167. (setq todo-only t todomatch (substring todomatch 1)))
  12168. (if (string-match "^\\s-*$" todomatch)
  12169. (setq todomatch nil)))
  12170. ;; only matching tags
  12171. (setq tagsmatch match todomatch nil))
  12172. ;; Make the tags matcher
  12173. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12174. (setq tagsmatcher t)
  12175. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12176. (while (setq term (pop orterms))
  12177. (while (and (equal (substring term -1) "\\") orterms)
  12178. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12179. (while (string-match re term)
  12180. (setq rest (substring term (match-end 0))
  12181. minus (and (match-end 1)
  12182. (equal (match-string 1 term) "-"))
  12183. tag (save-match-data (replace-regexp-in-string
  12184. "\\\\-" "-"
  12185. (match-string 2 term)))
  12186. re-p (equal (string-to-char tag) ?{)
  12187. level-p (match-end 4)
  12188. prop-p (match-end 5)
  12189. mm (cond
  12190. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12191. (level-p
  12192. (setq level-op (org-op-to-function (match-string 3 term)))
  12193. `(,level-op level ,(string-to-number
  12194. (match-string 4 term))))
  12195. (prop-p
  12196. (setq pn (match-string 5 term)
  12197. po (match-string 6 term)
  12198. pv (match-string 7 term)
  12199. re-p (equal (string-to-char pv) ?{)
  12200. str-p (equal (string-to-char pv) ?\")
  12201. time-p (save-match-data
  12202. (string-match "^\"[[<].*[]>]\"$" pv))
  12203. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12204. (if time-p (setq pv (org-matcher-time pv)))
  12205. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12206. (cond
  12207. ((equal pn "CATEGORY")
  12208. (setq gv '(get-text-property (point) 'org-category)))
  12209. ((equal pn "TODO")
  12210. (setq gv 'todo))
  12211. (t
  12212. (setq gv `(org-cached-entry-get nil ,pn))))
  12213. (if re-p
  12214. (if (eq po 'org<>)
  12215. `(not (string-match ,pv (or ,gv "")))
  12216. `(string-match ,pv (or ,gv "")))
  12217. (if str-p
  12218. `(,po (or ,gv "") ,pv)
  12219. `(,po (string-to-number (or ,gv ""))
  12220. ,(string-to-number pv) ))))
  12221. (t `(member ,tag tags-list)))
  12222. mm (if minus (list 'not mm) mm)
  12223. term rest)
  12224. (push mm tagsmatcher))
  12225. (push (if (> (length tagsmatcher) 1)
  12226. (cons 'and tagsmatcher)
  12227. (car tagsmatcher))
  12228. orlist)
  12229. (setq tagsmatcher nil))
  12230. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12231. (setq tagsmatcher
  12232. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12233. ;; Make the todo matcher
  12234. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12235. (setq todomatcher t)
  12236. (setq orterms (org-split-string todomatch "|") orlist nil)
  12237. (while (setq term (pop orterms))
  12238. (while (string-match re term)
  12239. (setq minus (and (match-end 1)
  12240. (equal (match-string 1 term) "-"))
  12241. kwd (match-string 2 term)
  12242. re-p (equal (string-to-char kwd) ?{)
  12243. term (substring term (match-end 0))
  12244. mm (if re-p
  12245. `(string-match ,(substring kwd 1 -1) todo)
  12246. (list 'equal 'todo kwd))
  12247. mm (if minus (list 'not mm) mm))
  12248. (push mm todomatcher))
  12249. (push (if (> (length todomatcher) 1)
  12250. (cons 'and todomatcher)
  12251. (car todomatcher))
  12252. orlist)
  12253. (setq todomatcher nil))
  12254. (setq todomatcher (if (> (length orlist) 1)
  12255. (cons 'or orlist) (car orlist))))
  12256. ;; Return the string and lisp forms of the matcher
  12257. (setq matcher (if todomatcher
  12258. (list 'and tagsmatcher todomatcher)
  12259. tagsmatcher))
  12260. (when todo-only
  12261. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12262. matcher)))
  12263. (cons match0 matcher)))
  12264. (defun org-op-to-function (op &optional stringp)
  12265. "Turn an operator into the appropriate function."
  12266. (setq op
  12267. (cond
  12268. ((equal op "<" ) '(< string< org-time<))
  12269. ((equal op ">" ) '(> org-string> org-time>))
  12270. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12271. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12272. ((member op '("=" "==")) '(= string= org-time=))
  12273. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12274. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12275. (defun org<> (a b) (not (= a b)))
  12276. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12277. (defun org-string>= (a b) (not (string< a b)))
  12278. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12279. (defun org-string<> (a b) (not (string= a b)))
  12280. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= 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) (org<> a b)))
  12286. (defun org-2ft (s)
  12287. "Convert S to a floating point time.
  12288. If S is already a number, just return it. If it is a string, parse
  12289. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12290. (cond
  12291. ((numberp s) s)
  12292. ((stringp s)
  12293. (condition-case nil
  12294. (float-time (apply 'encode-time (org-parse-time-string s)))
  12295. (error 0.)))
  12296. (t 0.)))
  12297. (defun org-time-today ()
  12298. "Time in seconds today at 0:00.
  12299. Returns the float number of seconds since the beginning of the
  12300. epoch to the beginning of today (00:00)."
  12301. (float-time (apply 'encode-time
  12302. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12303. (defun org-matcher-time (s)
  12304. "Interpret a time comparison value."
  12305. (save-match-data
  12306. (cond
  12307. ((string= s "<now>") (float-time))
  12308. ((string= s "<today>") (org-time-today))
  12309. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12310. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12311. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12312. (+ (org-time-today)
  12313. (* (string-to-number (match-string 1 s))
  12314. (cdr (assoc (match-string 2 s)
  12315. '(("d" . 86400.0) ("w" . 604800.0)
  12316. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12317. (t (org-2ft s)))))
  12318. (defun org-match-any-p (re list)
  12319. "Does re match any element of list?"
  12320. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12321. (delq nil list))
  12322. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12323. (defvar org-tags-overlay (make-overlay 1 1))
  12324. (org-detach-overlay org-tags-overlay)
  12325. (defun org-get-local-tags-at (&optional pos)
  12326. "Get a list of tags defined in the current headline."
  12327. (org-get-tags-at pos 'local))
  12328. (defun org-get-local-tags ()
  12329. "Get a list of tags defined in the current headline."
  12330. (org-get-tags-at nil 'local))
  12331. (defun org-get-tags-at (&optional pos local)
  12332. "Get a list of all headline tags applicable at POS.
  12333. POS defaults to point. If tags are inherited, the list contains
  12334. the targets in the same sequence as the headlines appear, i.e.
  12335. the tags of the current headline come last.
  12336. When LOCAL is non-nil, only return tags from the current headline,
  12337. ignore inherited ones."
  12338. (interactive)
  12339. (if (and org-trust-scanner-tags
  12340. (or (not pos) (equal pos (point)))
  12341. (not local))
  12342. org-scanner-tags
  12343. (let (tags ltags lastpos parent)
  12344. (save-excursion
  12345. (save-restriction
  12346. (widen)
  12347. (goto-char (or pos (point)))
  12348. (save-match-data
  12349. (catch 'done
  12350. (condition-case nil
  12351. (progn
  12352. (org-back-to-heading t)
  12353. (while (not (equal lastpos (point)))
  12354. (setq lastpos (point))
  12355. (when (looking-at
  12356. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12357. (setq ltags (org-split-string
  12358. (org-match-string-no-properties 1) ":"))
  12359. (when parent
  12360. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12361. (setq tags (append
  12362. (if parent
  12363. (org-remove-uninherited-tags ltags)
  12364. ltags)
  12365. tags)))
  12366. (or org-use-tag-inheritance (throw 'done t))
  12367. (if local (throw 'done t))
  12368. (or (org-up-heading-safe) (error nil))
  12369. (setq parent t)))
  12370. (error nil)))))
  12371. (if local
  12372. tags
  12373. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12374. (defun org-add-prop-inherited (s)
  12375. (add-text-properties 0 (length s) '(inherited t) s)
  12376. s)
  12377. (defun org-toggle-tag (tag &optional onoff)
  12378. "Toggle the tag TAG for the current line.
  12379. If ONOFF is `on' or `off', don't toggle but set to this state."
  12380. (let (res current)
  12381. (save-excursion
  12382. (org-back-to-heading t)
  12383. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12384. (point-at-eol) t)
  12385. (progn
  12386. (setq current (match-string 1))
  12387. (replace-match ""))
  12388. (setq current ""))
  12389. (setq current (nreverse (org-split-string current ":")))
  12390. (cond
  12391. ((eq onoff 'on)
  12392. (setq res t)
  12393. (or (member tag current) (push tag current)))
  12394. ((eq onoff 'off)
  12395. (or (not (member tag current)) (setq current (delete tag current))))
  12396. (t (if (member tag current)
  12397. (setq current (delete tag current))
  12398. (setq res t)
  12399. (push tag current))))
  12400. (end-of-line 1)
  12401. (if current
  12402. (progn
  12403. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12404. (org-set-tags nil t))
  12405. (delete-horizontal-space))
  12406. (run-hooks 'org-after-tags-change-hook))
  12407. res))
  12408. (defun org-align-tags-here (to-col)
  12409. ;; Assumes that this is a headline
  12410. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12411. (beginning-of-line 1)
  12412. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12413. (< pos (match-beginning 2)))
  12414. (progn
  12415. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12416. (goto-char (match-beginning 1))
  12417. (insert " ")
  12418. (delete-region (point) (1+ (match-beginning 2)))
  12419. (setq ncol (max (current-column)
  12420. (1+ col)
  12421. (if (> to-col 0)
  12422. to-col
  12423. (- (abs to-col) tags-l))))
  12424. (setq p (point))
  12425. (insert (make-string (- ncol (current-column)) ?\ ))
  12426. (setq ncol (current-column))
  12427. (when indent-tabs-mode (tabify p (point-at-eol)))
  12428. (org-move-to-column (min ncol col) t))
  12429. (goto-char pos))))
  12430. (defun org-set-tags-command (&optional arg just-align)
  12431. "Call the set-tags command for the current entry."
  12432. (interactive "P")
  12433. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12434. (org-set-tags arg just-align)
  12435. (save-excursion
  12436. (org-back-to-heading t)
  12437. (org-set-tags arg just-align))))
  12438. (defun org-set-tags-to (data)
  12439. "Set the tags of the current entry to DATA, replacing the current tags.
  12440. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12441. If DATA is nil or the empty string, any tags will be removed."
  12442. (interactive "sTags: ")
  12443. (setq data
  12444. (cond
  12445. ((eq data nil) "")
  12446. ((equal data "") "")
  12447. ((stringp data)
  12448. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12449. ":"))
  12450. ((listp data)
  12451. (concat ":" (mapconcat 'identity data ":") ":"))))
  12452. (when data
  12453. (save-excursion
  12454. (org-back-to-heading t)
  12455. (when (looking-at org-complex-heading-regexp)
  12456. (if (match-end 5)
  12457. (progn
  12458. (goto-char (match-beginning 5))
  12459. (insert data)
  12460. (delete-region (point) (point-at-eol))
  12461. (org-set-tags nil 'align))
  12462. (goto-char (point-at-eol))
  12463. (insert " " data)
  12464. (org-set-tags nil 'align)))
  12465. (beginning-of-line 1)
  12466. (if (looking-at ".*?\\([ \t]+\\)$")
  12467. (delete-region (match-beginning 1) (match-end 1))))))
  12468. (defun org-align-all-tags ()
  12469. "Align the tags i all headings."
  12470. (interactive)
  12471. (save-excursion
  12472. (or (ignore-errors (org-back-to-heading t))
  12473. (outline-next-heading))
  12474. (if (org-at-heading-p)
  12475. (org-set-tags t)
  12476. (message "No headings"))))
  12477. (defvar org-indent-indentation-per-level)
  12478. (defun org-set-tags (&optional arg just-align)
  12479. "Set the tags for the current headline.
  12480. With prefix ARG, realign all tags in headings in the current buffer."
  12481. (interactive "P")
  12482. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12483. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12484. 'region-start-level 'region))
  12485. org-loop-over-headlines-in-active-region)
  12486. (org-map-entries
  12487. ;; We don't use ARG and JUST-ALIGN here these args are not
  12488. ;; useful when looping over headlines
  12489. `(org-set-tags)
  12490. org-loop-over-headlines-in-active-region
  12491. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12492. (let* ((re org-outline-regexp-bol)
  12493. (current (unless arg (org-get-tags-string)))
  12494. (col (current-column))
  12495. (org-setting-tags t)
  12496. table current-tags inherited-tags ; computed below when needed
  12497. tags p0 c0 c1 rpl di tc level)
  12498. (if arg
  12499. (save-excursion
  12500. (goto-char (point-min))
  12501. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12502. (while (re-search-forward re nil t)
  12503. (org-set-tags nil t)
  12504. (end-of-line 1)))
  12505. (message "All tags realigned to column %d" org-tags-column))
  12506. (if just-align
  12507. (setq tags current)
  12508. ;; Get a new set of tags from the user
  12509. (save-excursion
  12510. (setq table (append org-tag-persistent-alist
  12511. (or org-tag-alist (org-get-buffer-tags))
  12512. (and
  12513. org-complete-tags-always-offer-all-agenda-tags
  12514. (org-global-tags-completion-table
  12515. (org-agenda-files))))
  12516. org-last-tags-completion-table table
  12517. current-tags (org-split-string current ":")
  12518. inherited-tags (nreverse
  12519. (nthcdr (length current-tags)
  12520. (nreverse (org-get-tags-at))))
  12521. tags
  12522. (if (or (eq t org-use-fast-tag-selection)
  12523. (and org-use-fast-tag-selection
  12524. (delq nil (mapcar 'cdr table))))
  12525. (org-fast-tag-selection
  12526. current-tags inherited-tags table
  12527. (if org-fast-tag-selection-include-todo
  12528. org-todo-key-alist))
  12529. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12530. (org-trim
  12531. (org-icompleting-read "Tags: "
  12532. 'org-tags-completion-function
  12533. nil nil current 'org-tags-history))))))
  12534. (while (string-match "[-+&]+" tags)
  12535. ;; No boolean logic, just a list
  12536. (setq tags (replace-match ":" t t tags))))
  12537. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12538. (if org-tags-sort-function
  12539. (setq tags (mapconcat 'identity
  12540. (sort (org-split-string
  12541. tags (org-re "[^[:alnum:]_@#%]+"))
  12542. org-tags-sort-function) ":")))
  12543. (if (string-match "\\`[\t ]*\\'" tags)
  12544. (setq tags "")
  12545. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12546. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12547. ;; Insert new tags at the correct column
  12548. (beginning-of-line 1)
  12549. (setq level (or (and (looking-at org-outline-regexp)
  12550. (- (match-end 0) (point) 1))
  12551. 1))
  12552. (cond
  12553. ((and (equal current "") (equal tags "")))
  12554. ((re-search-forward
  12555. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12556. (point-at-eol) t)
  12557. (if (equal tags "")
  12558. (setq rpl "")
  12559. (goto-char (match-beginning 0))
  12560. (setq c0 (current-column)
  12561. ;; compute offset for the case of org-indent-mode active
  12562. di (if org-indent-mode
  12563. (* (1- org-indent-indentation-per-level) (1- level))
  12564. 0)
  12565. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12566. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12567. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12568. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12569. (replace-match rpl t t)
  12570. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12571. tags)
  12572. (t (error "Tags alignment failed")))
  12573. (org-move-to-column col)
  12574. (unless just-align
  12575. (run-hooks 'org-after-tags-change-hook))))))
  12576. (defun org-change-tag-in-region (beg end tag off)
  12577. "Add or remove TAG for each entry in the region.
  12578. This works in the agenda, and also in an org-mode buffer."
  12579. (interactive
  12580. (list (region-beginning) (region-end)
  12581. (let ((org-last-tags-completion-table
  12582. (if (derived-mode-p 'org-mode)
  12583. (org-get-buffer-tags)
  12584. (org-global-tags-completion-table))))
  12585. (org-icompleting-read
  12586. "Tag: " 'org-tags-completion-function nil nil nil
  12587. 'org-tags-history))
  12588. (progn
  12589. (message "[s]et or [r]emove? ")
  12590. (equal (read-char-exclusive) ?r))))
  12591. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12592. (let ((agendap (equal major-mode 'org-agenda-mode))
  12593. l1 l2 m buf pos newhead (cnt 0))
  12594. (goto-char end)
  12595. (setq l2 (1- (org-current-line)))
  12596. (goto-char beg)
  12597. (setq l1 (org-current-line))
  12598. (loop for l from l1 to l2 do
  12599. (org-goto-line l)
  12600. (setq m (get-text-property (point) 'org-hd-marker))
  12601. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12602. (and agendap m))
  12603. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12604. pos (if agendap m (point)))
  12605. (with-current-buffer buf
  12606. (save-excursion
  12607. (save-restriction
  12608. (goto-char pos)
  12609. (setq cnt (1+ cnt))
  12610. (org-toggle-tag tag (if off 'off 'on))
  12611. (setq newhead (org-get-heading)))))
  12612. (and agendap (org-agenda-change-all-lines newhead m))))
  12613. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12614. (defun org-tags-completion-function (string predicate &optional flag)
  12615. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12616. (confirm (lambda (x) (stringp (car x)))))
  12617. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12618. (setq s1 (match-string 1 string)
  12619. s2 (match-string 2 string))
  12620. (setq s1 "" s2 string))
  12621. (cond
  12622. ((eq flag nil)
  12623. ;; try completion
  12624. (setq rtn (try-completion s2 ctable confirm))
  12625. (if (stringp rtn)
  12626. (setq rtn
  12627. (concat s1 s2 (substring rtn (length s2))
  12628. (if (and org-add-colon-after-tag-completion
  12629. (assoc rtn ctable))
  12630. ":" ""))))
  12631. rtn)
  12632. ((eq flag t)
  12633. ;; all-completions
  12634. (all-completions s2 ctable confirm)
  12635. )
  12636. ((eq flag 'lambda)
  12637. ;; exact match?
  12638. (assoc s2 ctable)))
  12639. ))
  12640. (defun org-fast-tag-insert (kwd tags face &optional end)
  12641. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12642. (insert (format "%-12s" (concat kwd ":"))
  12643. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12644. (or end "")))
  12645. (defun org-fast-tag-show-exit (flag)
  12646. (save-excursion
  12647. (org-goto-line 3)
  12648. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12649. (replace-match ""))
  12650. (when flag
  12651. (end-of-line 1)
  12652. (org-move-to-column (- (window-width) 19) t)
  12653. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12654. (defun org-set-current-tags-overlay (current prefix)
  12655. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12656. (if (featurep 'xemacs)
  12657. (org-overlay-display org-tags-overlay (concat prefix s)
  12658. 'secondary-selection)
  12659. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12660. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12661. (defvar org-last-tag-selection-key nil)
  12662. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12663. "Fast tag selection with single keys.
  12664. CURRENT is the current list of tags in the headline, INHERITED is the
  12665. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12666. possibly with grouping information. TODO-TABLE is a similar table with
  12667. TODO keywords, should these have keys assigned to them.
  12668. If the keys are nil, a-z are automatically assigned.
  12669. Returns the new tags string, or nil to not change the current settings."
  12670. (let* ((fulltable (append table todo-table))
  12671. (maxlen (apply 'max (mapcar
  12672. (lambda (x)
  12673. (if (stringp (car x)) (string-width (car x)) 0))
  12674. fulltable)))
  12675. (buf (current-buffer))
  12676. (expert (eq org-fast-tag-selection-single-key 'expert))
  12677. (buffer-tags nil)
  12678. (fwidth (+ maxlen 3 1 3))
  12679. (ncol (/ (- (window-width) 4) fwidth))
  12680. (i-face 'org-done)
  12681. (c-face 'org-todo)
  12682. tg cnt e c char c1 c2 ntable tbl rtn
  12683. ov-start ov-end ov-prefix
  12684. (exit-after-next org-fast-tag-selection-single-key)
  12685. (done-keywords org-done-keywords)
  12686. groups ingroup)
  12687. (save-excursion
  12688. (beginning-of-line 1)
  12689. (if (looking-at
  12690. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12691. (setq ov-start (match-beginning 1)
  12692. ov-end (match-end 1)
  12693. ov-prefix "")
  12694. (setq ov-start (1- (point-at-eol))
  12695. ov-end (1+ ov-start))
  12696. (skip-chars-forward "^\n\r")
  12697. (setq ov-prefix
  12698. (concat
  12699. (buffer-substring (1- (point)) (point))
  12700. (if (> (current-column) org-tags-column)
  12701. " "
  12702. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12703. (move-overlay org-tags-overlay ov-start ov-end)
  12704. (save-window-excursion
  12705. (if expert
  12706. (set-buffer (get-buffer-create " *Org tags*"))
  12707. (delete-other-windows)
  12708. (split-window-vertically)
  12709. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12710. (erase-buffer)
  12711. (org-set-local 'org-done-keywords done-keywords)
  12712. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12713. (org-fast-tag-insert "Current" current c-face "\n\n")
  12714. (org-fast-tag-show-exit exit-after-next)
  12715. (org-set-current-tags-overlay current ov-prefix)
  12716. (setq tbl fulltable char ?a cnt 0)
  12717. (while (setq e (pop tbl))
  12718. (cond
  12719. ((equal (car e) :startgroup)
  12720. (push '() groups) (setq ingroup t)
  12721. (when (not (= cnt 0))
  12722. (setq cnt 0)
  12723. (insert "\n"))
  12724. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12725. ((equal (car e) :endgroup)
  12726. (setq ingroup nil cnt 0)
  12727. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12728. ((equal e '(:newline))
  12729. (when (not (= cnt 0))
  12730. (setq cnt 0)
  12731. (insert "\n")
  12732. (setq e (car tbl))
  12733. (while (equal (car tbl) '(:newline))
  12734. (insert "\n")
  12735. (setq tbl (cdr tbl)))))
  12736. (t
  12737. (setq tg (copy-sequence (car e)) c2 nil)
  12738. (if (cdr e)
  12739. (setq c (cdr e))
  12740. ;; automatically assign a character.
  12741. (setq c1 (string-to-char
  12742. (downcase (substring
  12743. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12744. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12745. (while (or (rassoc char ntable) (rassoc char table))
  12746. (setq char (1+ char)))
  12747. (setq c2 c1))
  12748. (setq c (or c2 char)))
  12749. (if ingroup (push tg (car groups)))
  12750. (setq tg (org-add-props tg nil 'face
  12751. (cond
  12752. ((not (assoc tg table))
  12753. (org-get-todo-face tg))
  12754. ((member tg current) c-face)
  12755. ((member tg inherited) i-face))))
  12756. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12757. (insert "[" c "] " tg (make-string
  12758. (- fwidth 4 (length tg)) ?\ ))
  12759. (push (cons tg c) ntable)
  12760. (when (= (setq cnt (1+ cnt)) ncol)
  12761. (insert "\n")
  12762. (if ingroup (insert " "))
  12763. (setq cnt 0)))))
  12764. (setq ntable (nreverse ntable))
  12765. (insert "\n")
  12766. (goto-char (point-min))
  12767. (if (not expert) (org-fit-window-to-buffer))
  12768. (setq rtn
  12769. (catch 'exit
  12770. (while t
  12771. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12772. (if (not groups) "no " "")
  12773. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12774. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12775. (setq org-last-tag-selection-key c)
  12776. (cond
  12777. ((= c ?\r) (throw 'exit t))
  12778. ((= c ?!)
  12779. (setq groups (not groups))
  12780. (goto-char (point-min))
  12781. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12782. ((= c ?\C-c)
  12783. (if (not expert)
  12784. (org-fast-tag-show-exit
  12785. (setq exit-after-next (not exit-after-next)))
  12786. (setq expert nil)
  12787. (delete-other-windows)
  12788. (set-window-buffer (split-window-vertically) " *Org tags*")
  12789. (org-switch-to-buffer-other-window " *Org tags*")
  12790. (org-fit-window-to-buffer)))
  12791. ((or (= c ?\C-g)
  12792. (and (= c ?q) (not (rassoc c ntable))))
  12793. (org-detach-overlay org-tags-overlay)
  12794. (setq quit-flag t))
  12795. ((= c ?\ )
  12796. (setq current nil)
  12797. (if exit-after-next (setq exit-after-next 'now)))
  12798. ((= c ?\t)
  12799. (condition-case nil
  12800. (setq tg (org-icompleting-read
  12801. "Tag: "
  12802. (or buffer-tags
  12803. (with-current-buffer buf
  12804. (org-get-buffer-tags)))))
  12805. (quit (setq tg "")))
  12806. (when (string-match "\\S-" tg)
  12807. (add-to-list 'buffer-tags (list tg))
  12808. (if (member tg current)
  12809. (setq current (delete tg current))
  12810. (push tg current)))
  12811. (if exit-after-next (setq exit-after-next 'now)))
  12812. ((setq e (rassoc c todo-table) tg (car e))
  12813. (with-current-buffer buf
  12814. (save-excursion (org-todo tg)))
  12815. (if exit-after-next (setq exit-after-next 'now)))
  12816. ((setq e (rassoc c ntable) tg (car e))
  12817. (if (member tg current)
  12818. (setq current (delete tg current))
  12819. (loop for g in groups do
  12820. (if (member tg g)
  12821. (mapc (lambda (x)
  12822. (setq current (delete x current)))
  12823. g)))
  12824. (push tg current))
  12825. (if exit-after-next (setq exit-after-next 'now))))
  12826. ;; Create a sorted list
  12827. (setq current
  12828. (sort current
  12829. (lambda (a b)
  12830. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12831. (if (eq exit-after-next 'now) (throw 'exit t))
  12832. (goto-char (point-min))
  12833. (beginning-of-line 2)
  12834. (delete-region (point) (point-at-eol))
  12835. (org-fast-tag-insert "Current" current c-face)
  12836. (org-set-current-tags-overlay current ov-prefix)
  12837. (while (re-search-forward
  12838. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12839. (setq tg (match-string 1))
  12840. (add-text-properties
  12841. (match-beginning 1) (match-end 1)
  12842. (list 'face
  12843. (cond
  12844. ((member tg current) c-face)
  12845. ((member tg inherited) i-face)
  12846. (t (get-text-property (match-beginning 1) 'face))))))
  12847. (goto-char (point-min)))))
  12848. (org-detach-overlay org-tags-overlay)
  12849. (if rtn
  12850. (mapconcat 'identity current ":")
  12851. nil))))
  12852. (defun org-get-tags-string ()
  12853. "Get the TAGS string in the current headline."
  12854. (unless (org-at-heading-p t)
  12855. (error "Not on a heading"))
  12856. (save-excursion
  12857. (beginning-of-line 1)
  12858. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12859. (org-match-string-no-properties 1)
  12860. "")))
  12861. (defun org-get-tags ()
  12862. "Get the list of tags specified in the current headline."
  12863. (org-split-string (org-get-tags-string) ":"))
  12864. (defun org-get-buffer-tags ()
  12865. "Get a table of all tags used in the buffer, for completion."
  12866. (let (tags)
  12867. (save-excursion
  12868. (goto-char (point-min))
  12869. (while (re-search-forward
  12870. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12871. (when (equal (char-after (point-at-bol 0)) ?*)
  12872. (mapc (lambda (x) (add-to-list 'tags x))
  12873. (org-split-string (org-match-string-no-properties 1) ":")))))
  12874. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12875. (mapcar 'list tags)))
  12876. ;;;; The mapping API
  12877. (defun org-map-entries (func &optional match scope &rest skip)
  12878. "Call FUNC at each headline selected by MATCH in SCOPE.
  12879. FUNC is a function or a lisp form. The function will be called without
  12880. arguments, with the cursor positioned at the beginning of the headline.
  12881. The return values of all calls to the function will be collected and
  12882. returned as a list.
  12883. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12884. does not need to preserve point. After evaluation, the cursor will be
  12885. moved to the end of the line (presumably of the headline of the
  12886. processed entry) and search continues from there. Under some
  12887. circumstances, this may not produce the wanted results. For example,
  12888. if you have removed (e.g. archived) the current (sub)tree it could
  12889. mean that the next entry will be skipped entirely. In such cases, you
  12890. can specify the position from where search should continue by making
  12891. FUNC set the variable `org-map-continue-from' to the desired buffer
  12892. position.
  12893. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12894. Only headlines that are matched by this query will be considered during
  12895. the iteration. When MATCH is nil or t, all headlines will be
  12896. visited by the iteration.
  12897. SCOPE determines the scope of this command. It can be any of:
  12898. nil The current buffer, respecting the restriction if any
  12899. tree The subtree started with the entry at point
  12900. region The entries within the active region, if any
  12901. region-start-level
  12902. The entries within the active region, but only those at
  12903. the same level than the first one.
  12904. file The current buffer, without restriction
  12905. file-with-archives
  12906. The current buffer, and any archives associated with it
  12907. agenda All agenda files
  12908. agenda-with-archives
  12909. All agenda files with any archive files associated with them
  12910. \(file1 file2 ...)
  12911. If this is a list, all files in the list will be scanned
  12912. The remaining args are treated as settings for the skipping facilities of
  12913. the scanner. The following items can be given here:
  12914. archive skip trees with the archive tag.
  12915. comment skip trees with the COMMENT keyword
  12916. function or Emacs Lisp form:
  12917. will be used as value for `org-agenda-skip-function', so whenever
  12918. the function returns t, FUNC will not be called for that
  12919. entry and search will continue from the point where the
  12920. function leaves it.
  12921. If your function needs to retrieve the tags including inherited tags
  12922. at the *current* entry, you can use the value of the variable
  12923. `org-scanner-tags' which will be much faster than getting the value
  12924. with `org-get-tags-at'. If your function gets properties with
  12925. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12926. to t around the call to `org-entry-properties' to get the same speedup.
  12927. Note that if your function moves around to retrieve tags and properties at
  12928. a *different* entry, you cannot use these techniques."
  12929. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12930. (not (org-region-active-p)))
  12931. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12932. (org-agenda-skip-archived-trees (memq 'archive skip))
  12933. (org-agenda-skip-comment-trees (memq 'comment skip))
  12934. (org-agenda-skip-function
  12935. (car (org-delete-all '(comment archive) skip)))
  12936. (org-tags-match-list-sublevels t)
  12937. (start-level (eq scope 'region-start-level))
  12938. matcher file res
  12939. org-todo-keywords-for-agenda
  12940. org-done-keywords-for-agenda
  12941. org-todo-keyword-alist-for-agenda
  12942. org-drawers-for-agenda
  12943. org-tag-alist-for-agenda
  12944. todo-only)
  12945. (cond
  12946. ((eq match t) (setq matcher t))
  12947. ((eq match nil) (setq matcher t))
  12948. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12949. (save-excursion
  12950. (save-restriction
  12951. (cond ((eq scope 'tree)
  12952. (org-back-to-heading t)
  12953. (org-narrow-to-subtree)
  12954. (setq scope nil))
  12955. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12956. (org-region-active-p))
  12957. ;; If needed, set start-level to a string like "2"
  12958. (when start-level
  12959. (save-excursion
  12960. (goto-char (region-beginning))
  12961. (unless (org-at-heading-p) (outline-next-heading))
  12962. (setq start-level (org-current-level))))
  12963. (narrow-to-region (region-beginning)
  12964. (save-excursion
  12965. (goto-char (region-end))
  12966. (unless (and (bolp) (org-at-heading-p))
  12967. (outline-next-heading))
  12968. (point)))
  12969. (setq scope nil)))
  12970. (if (not scope)
  12971. (progn
  12972. (org-agenda-prepare-buffers
  12973. (list (buffer-file-name (current-buffer))))
  12974. (setq res (org-scan-tags func matcher todo-only start-level)))
  12975. ;; Get the right scope
  12976. (cond
  12977. ((and scope (listp scope) (symbolp (car scope)))
  12978. (setq scope (eval scope)))
  12979. ((eq scope 'agenda)
  12980. (setq scope (org-agenda-files t)))
  12981. ((eq scope 'agenda-with-archives)
  12982. (setq scope (org-agenda-files t))
  12983. (setq scope (org-add-archive-files scope)))
  12984. ((eq scope 'file)
  12985. (setq scope (list (buffer-file-name))))
  12986. ((eq scope 'file-with-archives)
  12987. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12988. (org-agenda-prepare-buffers scope)
  12989. (while (setq file (pop scope))
  12990. (with-current-buffer (org-find-base-buffer-visiting file)
  12991. (save-excursion
  12992. (save-restriction
  12993. (widen)
  12994. (goto-char (point-min))
  12995. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12996. res)))
  12997. ;;;; Properties
  12998. ;;; Setting and retrieving properties
  12999. (defconst org-special-properties
  13000. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13001. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13002. "The special properties valid in Org-mode.
  13003. These are properties that are not defined in the property drawer,
  13004. but in some other way.")
  13005. (defconst org-default-properties
  13006. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13007. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13008. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13009. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13010. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13011. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13012. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13013. "Some properties that are used by Org-mode for various purposes.
  13014. Being in this list makes sure that they are offered for completion.")
  13015. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13016. "Regular expression matching the first line of a property drawer.")
  13017. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13018. "Regular expression matching the last line of a property drawer.")
  13019. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13020. "Regular expression matching the first line of a property drawer.")
  13021. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13022. "Regular expression matching the first line of a property drawer.")
  13023. (defconst org-property-drawer-re
  13024. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13025. org-property-end-re "\\)\n?")
  13026. "Matches an entire property drawer.")
  13027. (defconst org-clock-drawer-re
  13028. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13029. org-property-end-re "\\)\n?")
  13030. "Matches an entire clock drawer.")
  13031. (defsubst org-re-property (property)
  13032. "Return a regexp matching a PROPERTY line.
  13033. Match group 1 will be set to the value."
  13034. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13035. (defsubst org-re-property-keyword (property)
  13036. "Return a regexp matching a PROPERTY line, possibly with no
  13037. value for the property."
  13038. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13039. (defun org-property-action ()
  13040. "Do an action on properties."
  13041. (interactive)
  13042. (let (c)
  13043. (org-at-property-p)
  13044. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13045. (setq c (read-char-exclusive))
  13046. (cond
  13047. ((equal c ?s)
  13048. (call-interactively 'org-set-property))
  13049. ((equal c ?d)
  13050. (call-interactively 'org-delete-property))
  13051. ((equal c ?D)
  13052. (call-interactively 'org-delete-property-globally))
  13053. ((equal c ?c)
  13054. (call-interactively 'org-compute-property-at-point))
  13055. (t (error "No such property action %c" c)))))
  13056. (defun org-inc-effort ()
  13057. "Increment the value of the effort property in the current entry."
  13058. (interactive)
  13059. (org-set-effort nil t))
  13060. (defun org-set-effort (&optional value increment)
  13061. "Set the effort property of the current entry.
  13062. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13063. 10th allowed value.
  13064. When INCREMENT is non-nil, set the property to the next allowed value."
  13065. (interactive "P")
  13066. (if (equal value 0) (setq value 10))
  13067. (let* ((completion-ignore-case t)
  13068. (prop org-effort-property)
  13069. (cur (org-entry-get nil prop))
  13070. (allowed (org-property-get-allowed-values nil prop 'table))
  13071. (existing (mapcar 'list (org-property-values prop)))
  13072. rpl
  13073. (val (cond
  13074. ((stringp value) value)
  13075. ((and allowed (integerp value))
  13076. (or (car (nth (1- value) allowed))
  13077. (car (org-last allowed))))
  13078. ((and allowed increment)
  13079. (or (caadr (member (list cur) allowed))
  13080. (error "Allowed effort values are not set")))
  13081. (allowed
  13082. (message "Select 1-9,0, [RET%s]: %s"
  13083. (if cur (concat "=" cur) "")
  13084. (mapconcat 'car allowed " "))
  13085. (setq rpl (read-char-exclusive))
  13086. (if (equal rpl ?\r)
  13087. cur
  13088. (setq rpl (- rpl ?0))
  13089. (if (equal rpl 0) (setq rpl 10))
  13090. (if (and (> rpl 0) (<= rpl (length allowed)))
  13091. (car (nth (1- rpl) allowed))
  13092. (org-completing-read "Effort: " allowed nil))))
  13093. (t
  13094. (let (org-completion-use-ido org-completion-use-iswitchb)
  13095. (org-completing-read
  13096. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13097. (concat "[" cur "]") "")
  13098. ": ")
  13099. existing nil nil "" nil cur))))))
  13100. (unless (equal (org-entry-get nil prop) val)
  13101. (org-entry-put nil prop val))
  13102. (message "%s is now %s" prop val)))
  13103. (defun org-at-property-p ()
  13104. "Is cursor inside a property drawer?"
  13105. (save-excursion
  13106. (beginning-of-line 1)
  13107. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13108. (save-match-data ;; Used by calling procedures
  13109. (let ((p (point))
  13110. (range (unless (org-before-first-heading-p)
  13111. (org-get-property-block))))
  13112. (and range (<= (car range) p) (< p (cdr range))))))))
  13113. (defun org-get-property-block (&optional beg end force)
  13114. "Return the (beg . end) range of the body of the property drawer.
  13115. BEG and END are the beginning and end of the current subtree, or of
  13116. the part before the first headline. If they are not given, they will
  13117. be found. If the drawer does not exist and FORCE is non-nil, create
  13118. the drawer."
  13119. (catch 'exit
  13120. (save-excursion
  13121. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13122. (progn (org-back-to-heading t) (point))))
  13123. (end (or end (and (not (outline-next-heading)) (point-max))
  13124. (point))))
  13125. (goto-char beg)
  13126. (if (re-search-forward org-property-start-re end t)
  13127. (setq beg (1+ (match-end 0)))
  13128. (if force
  13129. (save-excursion
  13130. (org-insert-property-drawer)
  13131. (setq end (progn (outline-next-heading) (point))))
  13132. (throw 'exit nil))
  13133. (goto-char beg)
  13134. (if (re-search-forward org-property-start-re end t)
  13135. (setq beg (1+ (match-end 0)))))
  13136. (if (re-search-forward org-property-end-re end t)
  13137. (setq end (match-beginning 0))
  13138. (or force (throw 'exit nil))
  13139. (goto-char beg)
  13140. (setq end beg)
  13141. (org-indent-line)
  13142. (insert ":END:\n"))
  13143. (cons beg end)))))
  13144. (defun org-entry-properties (&optional pom which specific)
  13145. "Get all properties of the entry at point-or-marker POM.
  13146. This includes the TODO keyword, the tags, time strings for deadline,
  13147. scheduled, and clocking, and any additional properties defined in the
  13148. entry. The return value is an alist, keys may occur multiple times
  13149. if the property key was used several times.
  13150. POM may also be nil, in which case the current entry is used.
  13151. If WHICH is nil or `all', get all properties. If WHICH is
  13152. `special' or `standard', only get that subclass. If WHICH
  13153. is a string only get exactly this property. SPECIFIC can be a string, the
  13154. specific property we are interested in. Specifying it can speed
  13155. things up because then unnecessary parsing is avoided."
  13156. (setq which (or which 'all))
  13157. (org-with-point-at pom
  13158. (let ((clockstr (substring org-clock-string 0 -1))
  13159. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13160. (case-fold-search nil)
  13161. beg end range props sum-props key key1 value string clocksum clocksumt)
  13162. (save-excursion
  13163. (when (condition-case nil
  13164. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13165. (error nil))
  13166. (setq beg (point))
  13167. (setq sum-props (get-text-property (point) 'org-summaries))
  13168. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13169. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13170. (outline-next-heading)
  13171. (setq end (point))
  13172. (when (memq which '(all special))
  13173. ;; Get the special properties, like TODO and tags
  13174. (goto-char beg)
  13175. (when (and (or (not specific) (string= specific "TODO"))
  13176. (looking-at org-todo-line-regexp) (match-end 2))
  13177. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13178. (when (and (or (not specific) (string= specific "PRIORITY"))
  13179. (looking-at org-priority-regexp))
  13180. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13181. (when (or (not specific) (string= specific "FILE"))
  13182. (push (cons "FILE" buffer-file-name) props))
  13183. (when (and (or (not specific) (string= specific "TAGS"))
  13184. (setq value (org-get-tags-string))
  13185. (string-match "\\S-" value))
  13186. (push (cons "TAGS" value) props))
  13187. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13188. (setq value (org-get-tags-at)))
  13189. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13190. ":"))
  13191. props))
  13192. (when (or (not specific) (string= specific "BLOCKED"))
  13193. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13194. (when (or (not specific)
  13195. (member specific
  13196. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13197. "TIMESTAMP" "TIMESTAMP_IA")))
  13198. (catch 'match
  13199. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13200. (setq key (if (match-end 1)
  13201. (substring (org-match-string-no-properties 1)
  13202. 0 -1))
  13203. string (if (equal key clockstr)
  13204. (org-trim
  13205. (buffer-substring-no-properties
  13206. (match-beginning 3) (goto-char
  13207. (point-at-eol))))
  13208. (substring (org-match-string-no-properties 3)
  13209. 1 -1)))
  13210. ;; Get the correct property name from the key. This is
  13211. ;; necessary if the user has configured time keywords.
  13212. (setq key1 (concat key ":"))
  13213. (cond
  13214. ((not key)
  13215. (setq key
  13216. (if (= (char-after (match-beginning 3)) ?\[)
  13217. "TIMESTAMP_IA" "TIMESTAMP")))
  13218. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13219. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13220. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13221. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13222. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13223. (progn
  13224. (push (cons key string) props)
  13225. ;; no need to search further if match is found
  13226. (throw 'match t))
  13227. (when (or (equal key "CLOCK") (not (assoc key props)))
  13228. (push (cons key string) props)))))))
  13229. (when (memq which '(all standard))
  13230. ;; Get the standard properties, like :PROP: ...
  13231. (setq range (org-get-property-block beg end))
  13232. (when range
  13233. (goto-char (car range))
  13234. (while (re-search-forward
  13235. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13236. (cdr range) t)
  13237. (setq key (org-match-string-no-properties 1)
  13238. value (org-trim (or (org-match-string-no-properties 2) "")))
  13239. (unless (member key excluded)
  13240. (push (cons key (or value "")) props)))))
  13241. (if clocksum
  13242. (push (cons "CLOCKSUM"
  13243. (org-columns-number-to-string (/ (float clocksum) 60.)
  13244. 'add_times))
  13245. props))
  13246. (if clocksumt
  13247. (push (cons "CLOCKSUM_T"
  13248. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13249. 'add_times))
  13250. props))
  13251. (unless (assoc "CATEGORY" props)
  13252. (push (cons "CATEGORY" (org-get-category)) props))
  13253. (append sum-props (nreverse props)))))))
  13254. (defun org-entry-get (pom property &optional inherit literal-nil)
  13255. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13256. If INHERIT is non-nil and the entry does not have the property,
  13257. then also check higher levels of the hierarchy.
  13258. If INHERIT is the symbol `selective', use inheritance only if the setting
  13259. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13260. If the property is present but empty, the return value is the empty string.
  13261. If the property is not present at all, nil is returned.
  13262. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13263. do not interpret it as the list atom nil. This is used for inheritance
  13264. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13265. (org-with-point-at pom
  13266. (if (and inherit (if (eq inherit 'selective)
  13267. (org-property-inherit-p property)
  13268. t))
  13269. (org-entry-get-with-inheritance property literal-nil)
  13270. (if (member property org-special-properties)
  13271. ;; We need a special property. Use `org-entry-properties' to
  13272. ;; retrieve it, but specify the wanted property
  13273. (cdr (assoc property (org-entry-properties nil 'special property)))
  13274. (let* ((range (org-get-property-block))
  13275. (props (list (or (assoc property org-file-properties)
  13276. (assoc property org-global-properties)
  13277. (assoc property org-global-properties-fixed))))
  13278. (ap (lambda (key)
  13279. (when (re-search-forward
  13280. (org-re-property key) (cdr range) t)
  13281. (setq props
  13282. (org-update-property-plist
  13283. key
  13284. (if (match-end 1)
  13285. (org-match-string-no-properties 1) "")
  13286. props)))))
  13287. val)
  13288. (when (and range (goto-char (car range)))
  13289. (funcall ap property)
  13290. (goto-char (car range))
  13291. (while (funcall ap (concat property "+")))
  13292. (setq val (cdr (assoc property props)))
  13293. (when val (if literal-nil val (org-not-nil val)))))))))
  13294. (defun org-property-or-variable-value (var &optional inherit)
  13295. "Check if there is a property fixing the value of VAR.
  13296. If yes, return this value. If not, return the current value of the variable."
  13297. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13298. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13299. (read prop)
  13300. (symbol-value var))))
  13301. (defun org-entry-delete (pom property)
  13302. "Delete the property PROPERTY from entry at point-or-marker POM."
  13303. (org-with-point-at pom
  13304. (if (member property org-special-properties)
  13305. nil ; cannot delete these properties.
  13306. (let ((range (org-get-property-block)))
  13307. (if (and range
  13308. (goto-char (car range))
  13309. (re-search-forward
  13310. (org-re-property property)
  13311. (cdr range) t))
  13312. (progn
  13313. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13314. t)
  13315. nil)))))
  13316. ;; Multi-values properties are properties that contain multiple values
  13317. ;; These values are assumed to be single words, separated by whitespace.
  13318. (defun org-entry-add-to-multivalued-property (pom property value)
  13319. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13320. (let* ((old (org-entry-get pom property))
  13321. (values (and old (org-split-string old "[ \t]"))))
  13322. (setq value (org-entry-protect-space value))
  13323. (unless (member value values)
  13324. (setq values (cons value values))
  13325. (org-entry-put pom property
  13326. (mapconcat 'identity values " ")))))
  13327. (defun org-entry-remove-from-multivalued-property (pom property value)
  13328. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13329. (let* ((old (org-entry-get pom property))
  13330. (values (and old (org-split-string old "[ \t]"))))
  13331. (setq value (org-entry-protect-space value))
  13332. (when (member value values)
  13333. (setq values (delete value values))
  13334. (org-entry-put pom property
  13335. (mapconcat 'identity values " ")))))
  13336. (defun org-entry-member-in-multivalued-property (pom property value)
  13337. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13338. (let* ((old (org-entry-get pom property))
  13339. (values (and old (org-split-string old "[ \t]"))))
  13340. (setq value (org-entry-protect-space value))
  13341. (member value values)))
  13342. (defun org-entry-get-multivalued-property (pom property)
  13343. "Return a list of values in a multivalued property."
  13344. (let* ((value (org-entry-get pom property))
  13345. (values (and value (org-split-string value "[ \t]"))))
  13346. (mapcar 'org-entry-restore-space values)))
  13347. (defun org-entry-put-multivalued-property (pom property &rest values)
  13348. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13349. VALUES should be a list of strings. Spaces will be protected."
  13350. (org-entry-put pom property
  13351. (mapconcat 'org-entry-protect-space values " "))
  13352. (let* ((value (org-entry-get pom property))
  13353. (values (and value (org-split-string value "[ \t]"))))
  13354. (mapcar 'org-entry-restore-space values)))
  13355. (defun org-entry-protect-space (s)
  13356. "Protect spaces and newline in string S."
  13357. (while (string-match " " s)
  13358. (setq s (replace-match "%20" t t s)))
  13359. (while (string-match "\n" s)
  13360. (setq s (replace-match "%0A" t t s)))
  13361. s)
  13362. (defun org-entry-restore-space (s)
  13363. "Restore spaces and newline in string S."
  13364. (while (string-match "%20" s)
  13365. (setq s (replace-match " " t t s)))
  13366. (while (string-match "%0A" s)
  13367. (setq s (replace-match "\n" t t s)))
  13368. s)
  13369. (defvar org-entry-property-inherited-from (make-marker)
  13370. "Marker pointing to the entry from where a property was inherited.
  13371. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13372. location of the entry where the inheritance search matched. If there was
  13373. no match, the marker will point nowhere.
  13374. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13375. is set.")
  13376. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13377. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13378. The search will stop at the first ancestor which has the property defined.
  13379. If the value found is \"nil\", return nil to show that the property
  13380. should be considered as undefined (this is the meaning of nil here).
  13381. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13382. (move-marker org-entry-property-inherited-from nil)
  13383. (let (tmp)
  13384. (save-excursion
  13385. (save-restriction
  13386. (widen)
  13387. (catch 'ex
  13388. (while t
  13389. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13390. (or (ignore-errors (org-back-to-heading t))
  13391. (goto-char (point-min)))
  13392. (move-marker org-entry-property-inherited-from (point))
  13393. (throw 'ex tmp))
  13394. (or (ignore-errors (org-up-heading-safe))
  13395. (throw 'ex nil))))))
  13396. (setq tmp (or tmp
  13397. (cdr (assoc property org-file-properties))
  13398. (cdr (assoc property org-global-properties))
  13399. (cdr (assoc property org-global-properties-fixed))))
  13400. (if literal-nil tmp (org-not-nil tmp))))
  13401. (defvar org-property-changed-functions nil
  13402. "Hook called when the value of a property has changed.
  13403. Each hook function should accept two arguments, the name of the property
  13404. and the new value.")
  13405. (defun org-entry-put (pom property value)
  13406. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13407. (org-with-point-at pom
  13408. (org-back-to-heading t)
  13409. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13410. range)
  13411. (cond
  13412. ((equal property "TODO")
  13413. (when (and (stringp value) (string-match "\\S-" value)
  13414. (not (member value org-todo-keywords-1)))
  13415. (error "\"%s\" is not a valid TODO state" value))
  13416. (if (or (not value)
  13417. (not (string-match "\\S-" value)))
  13418. (setq value 'none))
  13419. (org-todo value)
  13420. (org-set-tags nil 'align))
  13421. ((equal property "PRIORITY")
  13422. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13423. (string-to-char value) ?\ ))
  13424. (org-set-tags nil 'align))
  13425. ((equal property "CLOCKSUM")
  13426. (if (not (re-search-forward
  13427. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13428. (error "Cannot find a clock log")
  13429. (goto-char (- (match-end 1) 2))
  13430. (cond
  13431. ((eq value 'earlier) (org-timestamp-down))
  13432. ((eq value 'later) (org-timestamp-up)))
  13433. (org-clock-sum-current-item)))
  13434. ((equal property "SCHEDULED")
  13435. (if (re-search-forward org-scheduled-time-regexp end t)
  13436. (cond
  13437. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13438. ((eq value 'later) (org-timestamp-change 1 'day))
  13439. (t (call-interactively 'org-schedule)))
  13440. (call-interactively 'org-schedule)))
  13441. ((equal property "DEADLINE")
  13442. (if (re-search-forward org-deadline-time-regexp end t)
  13443. (cond
  13444. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13445. ((eq value 'later) (org-timestamp-change 1 'day))
  13446. (t (call-interactively 'org-deadline)))
  13447. (call-interactively 'org-deadline)))
  13448. ((member property org-special-properties)
  13449. (error "The %s property can not yet be set with `org-entry-put'"
  13450. property))
  13451. (t ; a non-special property
  13452. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13453. (setq range (org-get-property-block beg end 'force))
  13454. (goto-char (car range))
  13455. (if (re-search-forward
  13456. (org-re-property-keyword property) (cdr range) t)
  13457. (progn
  13458. (delete-region (match-beginning 0) (match-end 0))
  13459. (goto-char (match-beginning 0)))
  13460. (goto-char (cdr range))
  13461. (insert "\n")
  13462. (backward-char 1)
  13463. (org-indent-line))
  13464. (insert ":" property ":")
  13465. (and value (insert " " value))
  13466. (org-indent-line)))))
  13467. (run-hook-with-args 'org-property-changed-functions property value)))
  13468. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13469. "Get all property keys in the current buffer.
  13470. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13471. like tags and TODO state.
  13472. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13473. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13474. and others.
  13475. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13476. formats in the current buffer."
  13477. (let (rtn range cfmt s p)
  13478. (save-excursion
  13479. (save-restriction
  13480. (widen)
  13481. (goto-char (point-min))
  13482. (while (re-search-forward org-property-start-re nil t)
  13483. (setq range (org-get-property-block))
  13484. (goto-char (car range))
  13485. (while (re-search-forward
  13486. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13487. (cdr range) t)
  13488. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13489. (outline-next-heading))))
  13490. (when include-specials
  13491. (setq rtn (append org-special-properties rtn)))
  13492. (when include-defaults
  13493. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13494. (add-to-list 'rtn org-effort-property))
  13495. (when include-columns
  13496. (save-excursion
  13497. (save-restriction
  13498. (widen)
  13499. (goto-char (point-min))
  13500. (while (re-search-forward
  13501. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13502. nil t)
  13503. (setq cfmt (match-string 2) s 0)
  13504. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13505. cfmt s)
  13506. (setq s (match-end 0)
  13507. p (match-string 1 cfmt))
  13508. (unless (or (equal p "ITEM")
  13509. (member p org-special-properties))
  13510. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13511. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13512. (defun org-property-values (key)
  13513. "Return a list of all values of property KEY in the current buffer."
  13514. (save-excursion
  13515. (save-restriction
  13516. (widen)
  13517. (goto-char (point-min))
  13518. (let ((re (org-re-property key))
  13519. values)
  13520. (while (re-search-forward re nil t)
  13521. (add-to-list 'values (org-trim (match-string 1))))
  13522. (delete "" values)))))
  13523. (defun org-insert-property-drawer ()
  13524. "Insert a property drawer into the current entry."
  13525. (org-back-to-heading t)
  13526. (looking-at org-outline-regexp)
  13527. (let ((indent (if org-adapt-indentation
  13528. (- (match-end 0) (match-beginning 0))
  13529. 0))
  13530. (beg (point))
  13531. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13532. end hiddenp)
  13533. (outline-next-heading)
  13534. (setq end (point))
  13535. (goto-char beg)
  13536. (while (re-search-forward re end t))
  13537. (setq hiddenp (outline-invisible-p))
  13538. (end-of-line 1)
  13539. (and (equal (char-after) ?\n) (forward-char 1))
  13540. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13541. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13542. ;; just skip this line
  13543. (beginning-of-line 2)
  13544. ;; Drawer start, find the end
  13545. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13546. (beginning-of-line 1)))
  13547. (org-skip-over-state-notes)
  13548. (skip-chars-backward " \t\n\r")
  13549. (if (eq (char-before) ?*) (forward-char 1))
  13550. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13551. (beginning-of-line 0)
  13552. (org-indent-to-column indent)
  13553. (beginning-of-line 2)
  13554. (org-indent-to-column indent)
  13555. (beginning-of-line 0)
  13556. (if hiddenp
  13557. (save-excursion
  13558. (org-back-to-heading t)
  13559. (hide-entry))
  13560. (org-flag-drawer t))))
  13561. (defun org-insert-drawer (&optional arg drawer)
  13562. "Insert a drawer at point.
  13563. Optional argument DRAWER, when non-nil, is a string representing
  13564. drawer's name. Otherwise, the user is prompted for a name.
  13565. If a region is active, insert the drawer around that region
  13566. instead.
  13567. Point is left between drawer's boundaries."
  13568. (interactive "P")
  13569. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13570. "LOGBOOK"))
  13571. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13572. ;; internally by Org. They are meant to be inserted
  13573. ;; automatically.
  13574. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13575. ;; Remove system drawers from list. Note: For some reason,
  13576. ;; `org-completing-read' ignores the predicate while
  13577. ;; `completing-read' handles it fine.
  13578. (drawer (if arg "PROPERTIES"
  13579. (or drawer
  13580. (completing-read
  13581. "Drawer: " org-drawers
  13582. (lambda (d) (not (member d system-drawers))))))))
  13583. (cond
  13584. ;; With C-u, fall back on `org-insert-property-drawer'
  13585. (arg (org-insert-property-drawer))
  13586. ;; With an active region, insert a drawer at point.
  13587. ((not (org-region-active-p))
  13588. (progn
  13589. (unless (bolp) (insert "\n"))
  13590. (insert (format ":%s:\n\n:END:\n" drawer))
  13591. (forward-line -2)))
  13592. ;; Otherwise, insert the drawer at point
  13593. (t
  13594. (let ((rbeg (region-beginning))
  13595. (rend (copy-marker (region-end))))
  13596. (unwind-protect
  13597. (progn
  13598. (goto-char rbeg)
  13599. (beginning-of-line)
  13600. (when (save-excursion
  13601. (re-search-forward org-outline-regexp-bol rend t))
  13602. (error "Drawers cannot contain headlines"))
  13603. ;; Position point at the beginning of the first
  13604. ;; non-blank line in region. Insert drawer's opening
  13605. ;; there, then indent it.
  13606. (org-skip-whitespace)
  13607. (beginning-of-line)
  13608. (insert ":" drawer ":\n")
  13609. (forward-line -1)
  13610. (indent-for-tab-command)
  13611. ;; Move point to the beginning of the first blank line
  13612. ;; after the last non-blank line in region. Insert
  13613. ;; drawer's closing, then indent it.
  13614. (goto-char rend)
  13615. (skip-chars-backward " \r\t\n")
  13616. (insert "\n:END:")
  13617. (deactivate-mark t)
  13618. (indent-for-tab-command)
  13619. (unless (eolp) (insert "\n")))
  13620. ;; Clear marker, whatever the outcome of insertion is.
  13621. (set-marker rend nil)))))))
  13622. (defvar org-property-set-functions-alist nil
  13623. "Property set function alist.
  13624. Each entry should have the following format:
  13625. (PROPERTY . READ-FUNCTION)
  13626. The read function will be called with the same argument as
  13627. `org-completing-read'.")
  13628. (defun org-set-property-function (property)
  13629. "Get the function that should be used to set PROPERTY.
  13630. This is computed according to `org-property-set-functions-alist'."
  13631. (or (cdr (assoc property org-property-set-functions-alist))
  13632. 'org-completing-read))
  13633. (defun org-read-property-value (property)
  13634. "Read PROPERTY value from user."
  13635. (let* ((completion-ignore-case t)
  13636. (allowed (org-property-get-allowed-values nil property 'table))
  13637. (cur (org-entry-get nil property))
  13638. (prompt (concat property " value"
  13639. (if (and cur (string-match "\\S-" cur))
  13640. (concat " [" cur "]") "") ": "))
  13641. (set-function (org-set-property-function property))
  13642. (val (if allowed
  13643. (funcall set-function prompt allowed nil
  13644. (not (get-text-property 0 'org-unrestricted
  13645. (caar allowed))))
  13646. (let (org-completion-use-ido org-completion-use-iswitchb)
  13647. (funcall set-function prompt
  13648. (mapcar 'list (org-property-values property))
  13649. nil nil "" nil cur)))))
  13650. (if (equal val "")
  13651. cur
  13652. val)))
  13653. (defvar org-last-set-property nil)
  13654. (defun org-read-property-name ()
  13655. "Read a property name."
  13656. (let* ((completion-ignore-case t)
  13657. (keys (org-buffer-property-keys nil t t))
  13658. (default-prop (or (save-excursion
  13659. (save-match-data
  13660. (beginning-of-line)
  13661. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13662. (null (string= (match-string 1) "END"))
  13663. (match-string 1))))
  13664. org-last-set-property))
  13665. (property (org-icompleting-read
  13666. (concat "Property"
  13667. (if default-prop (concat " [" default-prop "]") "")
  13668. ": ")
  13669. (mapcar 'list keys)
  13670. nil nil nil nil
  13671. default-prop
  13672. )))
  13673. (if (member property keys)
  13674. property
  13675. (or (cdr (assoc (downcase property)
  13676. (mapcar (lambda (x) (cons (downcase x) x))
  13677. keys)))
  13678. property))))
  13679. (defun org-set-property (property value)
  13680. "In the current entry, set PROPERTY to VALUE.
  13681. When called interactively, this will prompt for a property name, offering
  13682. completion on existing and default properties. And then it will prompt
  13683. for a value, offering completion either on allowed values (via an inherited
  13684. xxx_ALL property) or on existing values in other instances of this property
  13685. in the current file."
  13686. (interactive (list nil nil))
  13687. (let* ((property (or property (org-read-property-name)))
  13688. (value (or value (org-read-property-value property)))
  13689. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13690. (setq org-last-set-property property)
  13691. ;; Possibly postprocess the inserted value:
  13692. (when fn (setq value (funcall fn value)))
  13693. (unless (equal (org-entry-get nil property) value)
  13694. (org-entry-put nil property value))))
  13695. (defun org-delete-property (property)
  13696. "In the current entry, delete PROPERTY."
  13697. (interactive
  13698. (let* ((completion-ignore-case t)
  13699. (prop (org-icompleting-read "Property: "
  13700. (org-entry-properties nil 'standard))))
  13701. (list prop)))
  13702. (message "Property %s %s" property
  13703. (if (org-entry-delete nil property)
  13704. "deleted"
  13705. "was not present in the entry")))
  13706. (defun org-delete-property-globally (property)
  13707. "Remove PROPERTY globally, from all entries."
  13708. (interactive
  13709. (let* ((completion-ignore-case t)
  13710. (prop (org-icompleting-read
  13711. "Globally remove property: "
  13712. (mapcar 'list (org-buffer-property-keys)))))
  13713. (list prop)))
  13714. (save-excursion
  13715. (save-restriction
  13716. (widen)
  13717. (goto-char (point-min))
  13718. (let ((cnt 0))
  13719. (while (re-search-forward
  13720. (org-re-property property)
  13721. nil t)
  13722. (setq cnt (1+ cnt))
  13723. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13724. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13725. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13726. (defun org-compute-property-at-point ()
  13727. "Compute the property at point.
  13728. This looks for an enclosing column format, extracts the operator and
  13729. then applies it to the property in the column format's scope."
  13730. (interactive)
  13731. (unless (org-at-property-p)
  13732. (error "Not at a property"))
  13733. (let ((prop (org-match-string-no-properties 2)))
  13734. (org-columns-get-format-and-top-level)
  13735. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13736. (error "No operator defined for property %s" prop))
  13737. (org-columns-compute prop)))
  13738. (defvar org-property-allowed-value-functions nil
  13739. "Hook for functions supplying allowed values for a specific property.
  13740. The functions must take a single argument, the name of the property, and
  13741. return a flat list of allowed values. If \":ETC\" is one of
  13742. the values, this means that these values are intended as defaults for
  13743. completion, but that other values should be allowed too.
  13744. The functions must return nil if they are not responsible for this
  13745. property.")
  13746. (defun org-property-get-allowed-values (pom property &optional table)
  13747. "Get allowed values for the property PROPERTY.
  13748. When TABLE is non-nil, return an alist that can directly be used for
  13749. completion."
  13750. (let (vals)
  13751. (cond
  13752. ((equal property "TODO")
  13753. (setq vals (org-with-point-at pom
  13754. (append org-todo-keywords-1 '("")))))
  13755. ((equal property "PRIORITY")
  13756. (let ((n org-lowest-priority))
  13757. (while (>= n org-highest-priority)
  13758. (push (char-to-string n) vals)
  13759. (setq n (1- n)))))
  13760. ((member property org-special-properties))
  13761. ((setq vals (run-hook-with-args-until-success
  13762. 'org-property-allowed-value-functions property)))
  13763. (t
  13764. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13765. (when (and vals (string-match "\\S-" vals))
  13766. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13767. (setq vals (mapcar (lambda (x)
  13768. (cond ((stringp x) x)
  13769. ((numberp x) (number-to-string x))
  13770. ((symbolp x) (symbol-name x))
  13771. (t "???")))
  13772. vals)))))
  13773. (when (member ":ETC" vals)
  13774. (setq vals (remove ":ETC" vals))
  13775. (org-add-props (car vals) '(org-unrestricted t)))
  13776. (if table (mapcar 'list vals) vals)))
  13777. (defun org-property-previous-allowed-value (&optional previous)
  13778. "Switch to the next allowed value for this property."
  13779. (interactive)
  13780. (org-property-next-allowed-value t))
  13781. (defun org-property-next-allowed-value (&optional previous)
  13782. "Switch to the next allowed value for this property."
  13783. (interactive)
  13784. (unless (org-at-property-p)
  13785. (error "Not at a property"))
  13786. (let* ((key (match-string 2))
  13787. (value (match-string 3))
  13788. (allowed (or (org-property-get-allowed-values (point) key)
  13789. (and (member value '("[ ]" "[-]" "[X]"))
  13790. '("[ ]" "[X]"))))
  13791. nval)
  13792. (unless allowed
  13793. (error "Allowed values for this property have not been defined"))
  13794. (if previous (setq allowed (reverse allowed)))
  13795. (if (member value allowed)
  13796. (setq nval (car (cdr (member value allowed)))))
  13797. (setq nval (or nval (car allowed)))
  13798. (if (equal nval value)
  13799. (error "Only one allowed value for this property"))
  13800. (org-at-property-p)
  13801. (replace-match (concat " :" key ": " nval) t t)
  13802. (org-indent-line)
  13803. (beginning-of-line 1)
  13804. (skip-chars-forward " \t")
  13805. (run-hook-with-args 'org-property-changed-functions key nval)))
  13806. (defun org-find-olp (path &optional this-buffer)
  13807. "Return a marker pointing to the entry at outline path OLP.
  13808. If anything goes wrong, throw an error.
  13809. You can wrap this call to catch the error like this:
  13810. (condition-case msg
  13811. (org-mobile-locate-entry (match-string 4))
  13812. (error (nth 1 msg)))
  13813. The return value will then be either a string with the error message,
  13814. or a marker if everything is OK.
  13815. If THIS-BUFFER is set, the outline path does not contain a file,
  13816. only headings."
  13817. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13818. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13819. (level 1)
  13820. (lmin 1)
  13821. (lmax 1)
  13822. limit re end found pos heading cnt flevel)
  13823. (unless buffer (error "File not found :%s" file))
  13824. (with-current-buffer buffer
  13825. (save-excursion
  13826. (save-restriction
  13827. (widen)
  13828. (setq limit (point-max))
  13829. (goto-char (point-min))
  13830. (while (setq heading (pop path))
  13831. (setq re (format org-complex-heading-regexp-format
  13832. (regexp-quote heading)))
  13833. (setq cnt 0 pos (point))
  13834. (while (re-search-forward re end t)
  13835. (setq level (- (match-end 1) (match-beginning 1)))
  13836. (if (and (>= level lmin) (<= level lmax))
  13837. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13838. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13839. lmax heading))
  13840. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13841. lmax heading))
  13842. (goto-char found)
  13843. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13844. (setq end (save-excursion (org-end-of-subtree t t))))
  13845. (when (org-at-heading-p)
  13846. (move-marker (make-marker) (point))))))))
  13847. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13848. "Find node HEADING in BUFFER.
  13849. Return a marker to the heading if it was found, or nil if not.
  13850. If POS-ONLY is set, return just the position instead of a marker.
  13851. The heading text must match exact, but it may have a TODO keyword,
  13852. a priority cookie and tags in the standard locations."
  13853. (with-current-buffer (or buffer (current-buffer))
  13854. (save-excursion
  13855. (save-restriction
  13856. (widen)
  13857. (goto-char (point-min))
  13858. (let (case-fold-search)
  13859. (if (re-search-forward
  13860. (format org-complex-heading-regexp-format
  13861. (regexp-quote heading)) nil t)
  13862. (if pos-only
  13863. (match-beginning 0)
  13864. (move-marker (make-marker) (match-beginning 0)))))))))
  13865. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13866. "Find Org node headline HEADING in all .org files in directory DIR.
  13867. When the target headline is found, return a marker to this location."
  13868. (let ((files (directory-files (or dir default-directory)
  13869. nil "\\`[^.#].*\\.org\\'"))
  13870. file visiting m buffer)
  13871. (catch 'found
  13872. (while (setq file (pop files))
  13873. (message "trying %s" file)
  13874. (setq visiting (org-find-base-buffer-visiting file))
  13875. (setq buffer (or visiting (find-file-noselect file)))
  13876. (setq m (org-find-exact-headline-in-buffer
  13877. heading buffer))
  13878. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13879. (and m (throw 'found m))))))
  13880. (defun org-find-entry-with-id (ident)
  13881. "Locate the entry that contains the ID property with exact value IDENT.
  13882. IDENT can be a string, a symbol or a number, this function will search for
  13883. the string representation of it.
  13884. Return the position where this entry starts, or nil if there is no such entry."
  13885. (interactive "sID: ")
  13886. (let ((id (cond
  13887. ((stringp ident) ident)
  13888. ((symbol-name ident) (symbol-name ident))
  13889. ((numberp ident) (number-to-string ident))
  13890. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13891. (case-fold-search nil))
  13892. (save-excursion
  13893. (save-restriction
  13894. (widen)
  13895. (goto-char (point-min))
  13896. (when (re-search-forward
  13897. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13898. nil t)
  13899. (org-back-to-heading t)
  13900. (point))))))
  13901. ;;;; Timestamps
  13902. (defvar org-last-changed-timestamp nil)
  13903. (defvar org-last-inserted-timestamp nil
  13904. "The last time stamp inserted with `org-insert-time-stamp'.")
  13905. (defvar org-time-was-given) ; dynamically scoped parameter
  13906. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13907. (defvar org-ts-what) ; dynamically scoped parameter
  13908. (defun org-time-stamp (arg &optional inactive)
  13909. "Prompt for a date/time and insert a time stamp.
  13910. If the user specifies a time like HH:MM or if this command is
  13911. called with at least one prefix argument, the time stamp contains
  13912. the date and the time. Otherwise, only the date is be included.
  13913. All parts of a date not specified by the user is filled in from
  13914. the current date/time. So if you just press return without
  13915. typing anything, the time stamp will represent the current
  13916. date/time.
  13917. If there is already a timestamp at the cursor, it will be
  13918. modified.
  13919. With two universal prefix arguments, insert an active timestamp
  13920. with the current time without prompting the user."
  13921. (interactive "P")
  13922. (let* ((ts nil)
  13923. (default-time
  13924. ;; Default time is either today, or, when entering a range,
  13925. ;; the range start.
  13926. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13927. (save-excursion
  13928. (re-search-backward
  13929. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13930. (- (point) 20) t)))
  13931. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13932. (current-time)))
  13933. (default-input (and ts (org-get-compact-tod ts)))
  13934. (repeater (save-excursion
  13935. (save-match-data
  13936. (beginning-of-line)
  13937. (when (re-search-forward
  13938. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13939. (save-excursion (progn (end-of-line) (point))) t)
  13940. (match-string 0)))))
  13941. org-time-was-given org-end-time-was-given time)
  13942. (cond
  13943. ((and (org-at-timestamp-p t)
  13944. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13945. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13946. (insert "--")
  13947. (setq time (let ((this-command this-command))
  13948. (org-read-date arg 'totime nil nil
  13949. default-time default-input inactive)))
  13950. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13951. ((org-at-timestamp-p t)
  13952. (setq time (let ((this-command this-command))
  13953. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13954. (when (org-at-timestamp-p t) ; just to get the match data
  13955. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13956. (replace-match "")
  13957. (setq org-last-changed-timestamp
  13958. (org-insert-time-stamp
  13959. time (or org-time-was-given arg)
  13960. inactive nil nil (list org-end-time-was-given)))
  13961. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13962. (setq org-last-changed-timestamp
  13963. (concat (substring org-last-inserted-timestamp 0 -1)
  13964. " " repeater ">"))))
  13965. (message "Timestamp updated"))
  13966. ((equal arg '(16))
  13967. (org-insert-time-stamp (current-time) t))
  13968. (t
  13969. (setq time (let ((this-command this-command))
  13970. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13971. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13972. nil nil (list org-end-time-was-given))))))
  13973. ;; FIXME: can we use this for something else, like computing time differences?
  13974. (defun org-get-compact-tod (s)
  13975. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13976. (let* ((t1 (match-string 1 s))
  13977. (h1 (string-to-number (match-string 2 s)))
  13978. (m1 (string-to-number (match-string 3 s)))
  13979. (t2 (and (match-end 4) (match-string 5 s)))
  13980. (h2 (and t2 (string-to-number (match-string 6 s))))
  13981. (m2 (and t2 (string-to-number (match-string 7 s))))
  13982. dh dm)
  13983. (if (not t2)
  13984. t1
  13985. (setq dh (- h2 h1) dm (- m2 m1))
  13986. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13987. (concat t1 "+" (number-to-string dh)
  13988. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13989. (defun org-time-stamp-inactive (&optional arg)
  13990. "Insert an inactive time stamp.
  13991. An inactive time stamp is enclosed in square brackets instead of angle
  13992. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13993. does not link to the calendar and cannot be changed with the S-cursor keys.
  13994. So these are more for recording a certain time/date."
  13995. (interactive "P")
  13996. (org-time-stamp arg 'inactive))
  13997. (defvar org-date-ovl (make-overlay 1 1))
  13998. (overlay-put org-date-ovl 'face 'org-date-selected)
  13999. (org-detach-overlay org-date-ovl)
  14000. (defvar org-ans1) ; dynamically scoped parameter
  14001. (defvar org-ans2) ; dynamically scoped parameter
  14002. (defvar org-plain-time-of-day-regexp) ; defined below
  14003. (defvar org-overriding-default-time nil) ; dynamically scoped
  14004. (defvar org-read-date-overlay nil)
  14005. (defvar org-dcst nil) ; dynamically scoped
  14006. (defvar org-read-date-history nil)
  14007. (defvar org-read-date-final-answer nil)
  14008. (defvar org-read-date-analyze-futurep nil)
  14009. (defvar org-read-date-analyze-forced-year nil)
  14010. (defvar org-read-date-inactive)
  14011. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14012. default-time default-input inactive)
  14013. "Read a date, possibly a time, and make things smooth for the user.
  14014. The prompt will suggest to enter an ISO date, but you can also enter anything
  14015. which will at least partially be understood by `parse-time-string'.
  14016. Unrecognized parts of the date will default to the current day, month, year,
  14017. hour and minute. If this command is called to replace a timestamp at point,
  14018. or to enter the second timestamp of a range, the default time is taken
  14019. from the existing stamp. Furthermore, the command prefers the future,
  14020. so if you are giving a date where the year is not given, and the day-month
  14021. combination is already past in the current year, it will assume you
  14022. mean next year. For details, see the manual. A few examples:
  14023. 3-2-5 --> 2003-02-05
  14024. feb 15 --> currentyear-02-15
  14025. 2/15 --> currentyear-02-15
  14026. sep 12 9 --> 2009-09-12
  14027. 12:45 --> today 12:45
  14028. 22 sept 0:34 --> currentyear-09-22 0:34
  14029. 12 --> currentyear-currentmonth-12
  14030. Fri --> nearest Friday (today or later)
  14031. etc.
  14032. Furthermore you can specify a relative date by giving, as the *first* thing
  14033. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14034. change in days weeks, months, years.
  14035. With a single plus or minus, the date is relative to today. With a double
  14036. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14037. +4d --> four days from today
  14038. +4 --> same as above
  14039. +2w --> two weeks from today
  14040. ++5 --> five days from default date
  14041. The function understands only English month and weekday abbreviations.
  14042. While prompting, a calendar is popped up - you can also select the
  14043. date with the mouse (button 1). The calendar shows a period of three
  14044. months. To scroll it to other months, use the keys `>' and `<'.
  14045. If you don't like the calendar, turn it off with
  14046. \(setq org-read-date-popup-calendar nil)
  14047. With optional argument TO-TIME, the date will immediately be converted
  14048. to an internal time.
  14049. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14050. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14051. still enter a time, and this function will inform the calling routine
  14052. about this change. The calling routine may then choose to change the
  14053. format used to insert the time stamp into the buffer to include the time.
  14054. With optional argument FROM-STRING, read from this string instead from
  14055. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14056. the time/date that is used for everything that is not specified by the
  14057. user."
  14058. (require 'parse-time)
  14059. (let* ((org-time-stamp-rounding-minutes
  14060. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14061. (org-dcst org-display-custom-times)
  14062. (ct (org-current-time))
  14063. (org-def (or org-overriding-default-time default-time ct))
  14064. (org-defdecode (decode-time org-def))
  14065. (dummy (progn
  14066. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14067. (setcar (nthcdr 2 org-defdecode) -1)
  14068. (setcar (nthcdr 1 org-defdecode) 59)
  14069. (setq org-def (apply 'encode-time org-defdecode)
  14070. org-defdecode (decode-time org-def)))))
  14071. (calendar-frame-setup nil)
  14072. (calendar-setup nil)
  14073. (calendar-move-hook nil)
  14074. (calendar-view-diary-initially-flag nil)
  14075. (calendar-view-holidays-initially-flag nil)
  14076. (timestr (format-time-string
  14077. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14078. (prompt (concat (if prompt (concat prompt " ") "")
  14079. (format "Date+time [%s]: " timestr)))
  14080. ans (org-ans0 "") org-ans1 org-ans2 final)
  14081. (cond
  14082. (from-string (setq ans from-string))
  14083. (org-read-date-popup-calendar
  14084. (save-excursion
  14085. (save-window-excursion
  14086. (calendar)
  14087. (org-eval-in-calendar '(setq cursor-type nil) t)
  14088. (unwind-protect
  14089. (progn
  14090. (calendar-forward-day (- (time-to-days org-def)
  14091. (calendar-absolute-from-gregorian
  14092. (calendar-current-date))))
  14093. (org-eval-in-calendar nil t)
  14094. (let* ((old-map (current-local-map))
  14095. (map (copy-keymap calendar-mode-map))
  14096. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14097. (org-defkey map (kbd "RET") 'org-calendar-select)
  14098. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14099. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14100. (org-defkey minibuffer-local-map [(meta shift left)]
  14101. (lambda () (interactive)
  14102. (org-eval-in-calendar '(calendar-backward-month 1))))
  14103. (org-defkey minibuffer-local-map [(meta shift right)]
  14104. (lambda () (interactive)
  14105. (org-eval-in-calendar '(calendar-forward-month 1))))
  14106. (org-defkey minibuffer-local-map [(meta shift up)]
  14107. (lambda () (interactive)
  14108. (org-eval-in-calendar '(calendar-backward-year 1))))
  14109. (org-defkey minibuffer-local-map [(meta shift down)]
  14110. (lambda () (interactive)
  14111. (org-eval-in-calendar '(calendar-forward-year 1))))
  14112. (org-defkey minibuffer-local-map [?\e (shift left)]
  14113. (lambda () (interactive)
  14114. (org-eval-in-calendar '(calendar-backward-month 1))))
  14115. (org-defkey minibuffer-local-map [?\e (shift right)]
  14116. (lambda () (interactive)
  14117. (org-eval-in-calendar '(calendar-forward-month 1))))
  14118. (org-defkey minibuffer-local-map [?\e (shift up)]
  14119. (lambda () (interactive)
  14120. (org-eval-in-calendar '(calendar-backward-year 1))))
  14121. (org-defkey minibuffer-local-map [?\e (shift down)]
  14122. (lambda () (interactive)
  14123. (org-eval-in-calendar '(calendar-forward-year 1))))
  14124. (org-defkey minibuffer-local-map [(shift up)]
  14125. (lambda () (interactive)
  14126. (org-eval-in-calendar '(calendar-backward-week 1))))
  14127. (org-defkey minibuffer-local-map [(shift down)]
  14128. (lambda () (interactive)
  14129. (org-eval-in-calendar '(calendar-forward-week 1))))
  14130. (org-defkey minibuffer-local-map [(shift left)]
  14131. (lambda () (interactive)
  14132. (org-eval-in-calendar '(calendar-backward-day 1))))
  14133. (org-defkey minibuffer-local-map [(shift right)]
  14134. (lambda () (interactive)
  14135. (org-eval-in-calendar '(calendar-forward-day 1))))
  14136. (org-defkey minibuffer-local-map ">"
  14137. (lambda () (interactive)
  14138. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14139. (org-defkey minibuffer-local-map "<"
  14140. (lambda () (interactive)
  14141. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14142. (org-defkey minibuffer-local-map "\C-v"
  14143. (lambda () (interactive)
  14144. (org-eval-in-calendar
  14145. '(calendar-scroll-left-three-months 1))))
  14146. (org-defkey minibuffer-local-map "\M-v"
  14147. (lambda () (interactive)
  14148. (org-eval-in-calendar
  14149. '(calendar-scroll-right-three-months 1))))
  14150. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14151. (unwind-protect
  14152. (progn
  14153. (use-local-map map)
  14154. (setq org-read-date-inactive inactive)
  14155. (add-hook 'post-command-hook 'org-read-date-display)
  14156. (setq org-ans0 (read-string prompt default-input
  14157. 'org-read-date-history nil))
  14158. ;; org-ans0: from prompt
  14159. ;; org-ans1: from mouse click
  14160. ;; org-ans2: from calendar motion
  14161. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14162. (remove-hook 'post-command-hook 'org-read-date-display)
  14163. (use-local-map old-map)
  14164. (when org-read-date-overlay
  14165. (delete-overlay org-read-date-overlay)
  14166. (setq org-read-date-overlay nil)))))
  14167. (bury-buffer "*Calendar*")))))
  14168. (t ; Naked prompt only
  14169. (unwind-protect
  14170. (setq ans (read-string prompt default-input
  14171. 'org-read-date-history timestr))
  14172. (when org-read-date-overlay
  14173. (delete-overlay org-read-date-overlay)
  14174. (setq org-read-date-overlay nil)))))
  14175. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14176. (when org-read-date-analyze-forced-year
  14177. (message "Year was forced into %s"
  14178. (if org-read-date-force-compatible-dates
  14179. "compatible range (1970-2037)"
  14180. "range representable on this machine"))
  14181. (ding))
  14182. ;; One round trip to get rid of 34th of August and stuff like that....
  14183. (setq final (decode-time (apply 'encode-time final)))
  14184. (setq org-read-date-final-answer ans)
  14185. (if to-time
  14186. (apply 'encode-time final)
  14187. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14188. (format "%04d-%02d-%02d %02d:%02d"
  14189. (nth 5 final) (nth 4 final) (nth 3 final)
  14190. (nth 2 final) (nth 1 final))
  14191. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14192. (defvar org-def)
  14193. (defvar org-defdecode)
  14194. (defvar org-with-time)
  14195. (defun org-read-date-display ()
  14196. "Display the current date prompt interpretation in the minibuffer."
  14197. (when org-read-date-display-live
  14198. (when org-read-date-overlay
  14199. (delete-overlay org-read-date-overlay))
  14200. (when (minibufferp (current-buffer))
  14201. (save-excursion
  14202. (end-of-line 1)
  14203. (while (not (equal (buffer-substring
  14204. (max (point-min) (- (point) 4)) (point))
  14205. " "))
  14206. (insert " ")))
  14207. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14208. " " (or org-ans1 org-ans2)))
  14209. (org-end-time-was-given nil)
  14210. (f (org-read-date-analyze ans org-def org-defdecode))
  14211. (fmts (if org-dcst
  14212. org-time-stamp-custom-formats
  14213. org-time-stamp-formats))
  14214. (fmt (if (or org-with-time
  14215. (and (boundp 'org-time-was-given) org-time-was-given))
  14216. (cdr fmts)
  14217. (car fmts)))
  14218. (txt (format-time-string fmt (apply 'encode-time f)))
  14219. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14220. (txt (concat "=> " txt)))
  14221. (when (and org-end-time-was-given
  14222. (string-match org-plain-time-of-day-regexp txt))
  14223. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14224. org-end-time-was-given
  14225. (substring txt (match-end 0)))))
  14226. (when org-read-date-analyze-futurep
  14227. (setq txt (concat txt " (=>F)")))
  14228. (setq org-read-date-overlay
  14229. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14230. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14231. (defun org-read-date-analyze (ans org-def org-defdecode)
  14232. "Analyze the combined answer of the date prompt."
  14233. ;; FIXME: cleanup and comment
  14234. (let ((nowdecode (decode-time (current-time)))
  14235. delta deltan deltaw deltadef year month day
  14236. hour minute second wday pm h2 m2 tl wday1
  14237. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14238. (setq org-read-date-analyze-futurep nil
  14239. org-read-date-analyze-forced-year nil)
  14240. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14241. (setq ans "+0"))
  14242. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14243. (setq ans (replace-match "" t t ans)
  14244. deltan (car delta)
  14245. deltaw (nth 1 delta)
  14246. deltadef (nth 2 delta)))
  14247. ;; Check if there is an iso week date in there. If yes, store the
  14248. ;; info and postpone interpreting it until the rest of the parsing
  14249. ;; is done.
  14250. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14251. (setq iso-year (if (match-end 1)
  14252. (org-small-year-to-year
  14253. (string-to-number (match-string 1 ans))))
  14254. iso-weekday (if (match-end 3)
  14255. (string-to-number (match-string 3 ans)))
  14256. iso-week (string-to-number (match-string 2 ans)))
  14257. (setq ans (replace-match "" t t ans)))
  14258. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14259. (when (string-match
  14260. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14261. (setq year (if (match-end 2)
  14262. (string-to-number (match-string 2 ans))
  14263. (progn (setq kill-year t)
  14264. (string-to-number (format-time-string "%Y"))))
  14265. month (string-to-number (match-string 3 ans))
  14266. day (string-to-number (match-string 4 ans)))
  14267. (if (< year 100) (setq year (+ 2000 year)))
  14268. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14269. t nil ans)))
  14270. ;; Help matching dotted european dates
  14271. (when (string-match
  14272. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14273. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14274. (setq kill-year t)
  14275. (string-to-number (format-time-string "%Y")))
  14276. day (string-to-number (match-string 1 ans))
  14277. month (string-to-number (match-string 2 ans))
  14278. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14279. t nil ans)))
  14280. ;; Help matching american dates, like 5/30 or 5/30/7
  14281. (when (string-match
  14282. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14283. (setq year (if (match-end 4)
  14284. (string-to-number (match-string 4 ans))
  14285. (progn (setq kill-year t)
  14286. (string-to-number (format-time-string "%Y"))))
  14287. month (string-to-number (match-string 1 ans))
  14288. day (string-to-number (match-string 2 ans)))
  14289. (if (< year 100) (setq year (+ 2000 year)))
  14290. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14291. t nil ans)))
  14292. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14293. ;; If there is a time with am/pm, and *no* time without it, we convert
  14294. ;; so that matching will be successful.
  14295. (loop for i from 1 to 2 do ; twice, for end time as well
  14296. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14297. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14298. (setq hour (string-to-number (match-string 1 ans))
  14299. minute (if (match-end 3)
  14300. (string-to-number (match-string 3 ans))
  14301. 0)
  14302. pm (equal ?p
  14303. (string-to-char (downcase (match-string 4 ans)))))
  14304. (if (and (= hour 12) (not pm))
  14305. (setq hour 0)
  14306. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14307. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14308. t t ans))))
  14309. ;; Check if a time range is given as a duration
  14310. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14311. (setq hour (string-to-number (match-string 1 ans))
  14312. h2 (+ hour (string-to-number (match-string 3 ans)))
  14313. minute (string-to-number (match-string 2 ans))
  14314. m2 (+ minute (if (match-end 5) (string-to-number
  14315. (match-string 5 ans))0)))
  14316. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14317. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14318. t t ans)))
  14319. ;; Check if there is a time range
  14320. (when (boundp 'org-end-time-was-given)
  14321. (setq org-time-was-given nil)
  14322. (when (and (string-match org-plain-time-of-day-regexp ans)
  14323. (match-end 8))
  14324. (setq org-end-time-was-given (match-string 8 ans))
  14325. (setq ans (concat (substring ans 0 (match-beginning 7))
  14326. (substring ans (match-end 7))))))
  14327. (setq tl (parse-time-string ans)
  14328. day (or (nth 3 tl) (nth 3 org-defdecode))
  14329. month (or (nth 4 tl)
  14330. (if (and org-read-date-prefer-future
  14331. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14332. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14333. (nth 4 org-defdecode)))
  14334. year (or (and (not kill-year) (nth 5 tl))
  14335. (if (and org-read-date-prefer-future
  14336. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14337. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14338. (nth 5 org-defdecode)))
  14339. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14340. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14341. second (or (nth 0 tl) 0)
  14342. wday (nth 6 tl))
  14343. (when (and (eq org-read-date-prefer-future 'time)
  14344. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14345. (equal day (nth 3 nowdecode))
  14346. (equal month (nth 4 nowdecode))
  14347. (equal year (nth 5 nowdecode))
  14348. (nth 2 tl)
  14349. (or (< (nth 2 tl) (nth 2 nowdecode))
  14350. (and (= (nth 2 tl) (nth 2 nowdecode))
  14351. (nth 1 tl)
  14352. (< (nth 1 tl) (nth 1 nowdecode)))))
  14353. (setq day (1+ day)
  14354. futurep t))
  14355. ;; Special date definitions below
  14356. (cond
  14357. (iso-week
  14358. ;; There was an iso week
  14359. (require 'cal-iso)
  14360. (setq futurep nil)
  14361. (setq year (or iso-year year)
  14362. day (or iso-weekday wday 1)
  14363. wday nil ; to make sure that the trigger below does not match
  14364. iso-date (calendar-gregorian-from-absolute
  14365. (calendar-absolute-from-iso
  14366. (list iso-week day year))))
  14367. ; FIXME: Should we also push ISO weeks into the future?
  14368. ; (when (and org-read-date-prefer-future
  14369. ; (not iso-year)
  14370. ; (< (calendar-absolute-from-gregorian iso-date)
  14371. ; (time-to-days (current-time))))
  14372. ; (setq year (1+ year)
  14373. ; iso-date (calendar-gregorian-from-absolute
  14374. ; (calendar-absolute-from-iso
  14375. ; (list iso-week day year)))))
  14376. (setq month (car iso-date)
  14377. year (nth 2 iso-date)
  14378. day (nth 1 iso-date)))
  14379. (deltan
  14380. (setq futurep nil)
  14381. (unless deltadef
  14382. (let ((now (decode-time (current-time))))
  14383. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14384. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14385. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14386. ((equal deltaw "m") (setq month (+ month deltan)))
  14387. ((equal deltaw "y") (setq year (+ year deltan)))))
  14388. ((and wday (not (nth 3 tl)))
  14389. ;; Weekday was given, but no day, so pick that day in the week
  14390. ;; on or after the derived date.
  14391. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14392. (unless (equal wday wday1)
  14393. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14394. (if (and (boundp 'org-time-was-given)
  14395. (nth 2 tl))
  14396. (setq org-time-was-given t))
  14397. (if (< year 100) (setq year (+ 2000 year)))
  14398. ;; Check of the date is representable
  14399. (if org-read-date-force-compatible-dates
  14400. (progn
  14401. (if (< year 1970)
  14402. (setq year 1970 org-read-date-analyze-forced-year t))
  14403. (if (> year 2037)
  14404. (setq year 2037 org-read-date-analyze-forced-year t)))
  14405. (condition-case nil
  14406. (ignore (encode-time second minute hour day month year))
  14407. (error
  14408. (setq year (nth 5 org-defdecode))
  14409. (setq org-read-date-analyze-forced-year t))))
  14410. (setq org-read-date-analyze-futurep futurep)
  14411. (list second minute hour day month year)))
  14412. (defvar parse-time-weekdays)
  14413. (defun org-read-date-get-relative (s today default)
  14414. "Check string S for special relative date string.
  14415. TODAY and DEFAULT are internal times, for today and for a default.
  14416. Return shift list (N what def-flag)
  14417. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14418. N is the number of WHATs to shift.
  14419. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14420. the DEFAULT date rather than TODAY."
  14421. (require 'parse-time)
  14422. (when (and
  14423. (string-match
  14424. (concat
  14425. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14426. "\\([0-9]+\\)?"
  14427. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14428. "\\([ \t]\\|$\\)") s)
  14429. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14430. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14431. (string-to-char (substring (match-string 1 s) -1))
  14432. ?+))
  14433. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14434. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14435. (what (if (match-end 3) (match-string 3 s) "d"))
  14436. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14437. (date (if rel default today))
  14438. (wday (nth 6 (decode-time date)))
  14439. delta)
  14440. (if wday1
  14441. (progn
  14442. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14443. (if (= dir ?-) (setq delta (- delta 7)))
  14444. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14445. (list delta "d" rel))
  14446. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14447. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14448. "Turn a user-specified date into the internal representation.
  14449. The internal representation needed by the calendar is (month day year).
  14450. This is a wrapper to handle the brain-dead convention in calendar that
  14451. user function argument order change dependent on argument order."
  14452. (if (boundp 'calendar-date-style)
  14453. (cond
  14454. ((eq calendar-date-style 'american)
  14455. (list arg1 arg2 arg3))
  14456. ((eq calendar-date-style 'european)
  14457. (list arg2 arg1 arg3))
  14458. ((eq calendar-date-style 'iso)
  14459. (list arg2 arg3 arg1)))
  14460. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14461. (if (org-bound-and-true-p european-calendar-style)
  14462. (list arg2 arg1 arg3)
  14463. (list arg1 arg2 arg3)))))
  14464. (defun org-eval-in-calendar (form &optional keepdate)
  14465. "Eval FORM in the calendar window and return to current window.
  14466. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14467. otherwise stick to the current value of `org-ans2'."
  14468. (let ((sf (selected-frame))
  14469. (sw (selected-window)))
  14470. (select-window (get-buffer-window "*Calendar*" t))
  14471. (eval form)
  14472. (when (and (not keepdate) (calendar-cursor-to-date))
  14473. (let* ((date (calendar-cursor-to-date))
  14474. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14475. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14476. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14477. (select-window sw)
  14478. (org-select-frame-set-input-focus sf)))
  14479. (defun org-calendar-select ()
  14480. "Return to `org-read-date' with the date currently selected.
  14481. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14482. (interactive)
  14483. (when (calendar-cursor-to-date)
  14484. (let* ((date (calendar-cursor-to-date))
  14485. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14486. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14487. (if (active-minibuffer-window) (exit-minibuffer))))
  14488. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14489. "Insert a date stamp for the date given by the internal TIME.
  14490. WITH-HM means use the stamp format that includes the time of the day.
  14491. INACTIVE means use square brackets instead of angular ones, so that the
  14492. stamp will not contribute to the agenda.
  14493. PRE and POST are optional strings to be inserted before and after the
  14494. stamp.
  14495. The command returns the inserted time stamp."
  14496. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14497. stamp)
  14498. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14499. (insert-before-markers (or pre ""))
  14500. (when (listp extra)
  14501. (setq extra (car extra))
  14502. (if (and (stringp extra)
  14503. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14504. (setq extra (format "-%02d:%02d"
  14505. (string-to-number (match-string 1 extra))
  14506. (string-to-number (match-string 2 extra))))
  14507. (setq extra nil)))
  14508. (when extra
  14509. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14510. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14511. (insert-before-markers (or post ""))
  14512. (setq org-last-inserted-timestamp stamp)))
  14513. (defun org-toggle-time-stamp-overlays ()
  14514. "Toggle the use of custom time stamp formats."
  14515. (interactive)
  14516. (setq org-display-custom-times (not org-display-custom-times))
  14517. (unless org-display-custom-times
  14518. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14519. (while (setq p (next-single-property-change p 'display))
  14520. (if (and (get-text-property p 'display)
  14521. (eq (get-text-property p 'face) 'org-date))
  14522. (remove-text-properties
  14523. p (setq p (next-single-property-change p 'display))
  14524. '(display t))))
  14525. (set-buffer-modified-p bmp)))
  14526. (if (featurep 'xemacs)
  14527. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14528. (org-restart-font-lock)
  14529. (setq org-table-may-need-update t)
  14530. (if org-display-custom-times
  14531. (message "Time stamps are overlaid with custom format")
  14532. (message "Time stamp overlays removed")))
  14533. (defun org-display-custom-time (beg end)
  14534. "Overlay modified time stamp format over timestamp between BEG and END."
  14535. (let* ((ts (buffer-substring beg end))
  14536. t1 w1 with-hm tf time str w2 (off 0))
  14537. (save-match-data
  14538. (setq t1 (org-parse-time-string ts t))
  14539. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14540. (setq off (- (match-end 0) (match-beginning 0)))))
  14541. (setq end (- end off))
  14542. (setq w1 (- end beg)
  14543. with-hm (and (nth 1 t1) (nth 2 t1))
  14544. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14545. time (org-fix-decoded-time t1)
  14546. str (org-add-props
  14547. (format-time-string
  14548. (substring tf 1 -1) (apply 'encode-time time))
  14549. nil 'mouse-face 'highlight)
  14550. w2 (length str))
  14551. (if (not (= w2 w1))
  14552. (add-text-properties (1+ beg) (+ 2 beg)
  14553. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14554. (if (featurep 'xemacs)
  14555. (progn
  14556. (put-text-property beg end 'invisible t)
  14557. (put-text-property beg end 'end-glyph (make-glyph str)))
  14558. (put-text-property beg end 'display str))))
  14559. (defun org-translate-time (string)
  14560. "Translate all timestamps in STRING to custom format.
  14561. But do this only if the variable `org-display-custom-times' is set."
  14562. (when org-display-custom-times
  14563. (save-match-data
  14564. (let* ((start 0)
  14565. (re org-ts-regexp-both)
  14566. t1 with-hm inactive tf time str beg end)
  14567. (while (setq start (string-match re string start))
  14568. (setq beg (match-beginning 0)
  14569. end (match-end 0)
  14570. t1 (save-match-data
  14571. (org-parse-time-string (substring string beg end) t))
  14572. with-hm (and (nth 1 t1) (nth 2 t1))
  14573. inactive (equal (substring string beg (1+ beg)) "[")
  14574. tf (funcall (if with-hm 'cdr 'car)
  14575. org-time-stamp-custom-formats)
  14576. time (org-fix-decoded-time t1)
  14577. str (format-time-string
  14578. (concat
  14579. (if inactive "[" "<") (substring tf 1 -1)
  14580. (if inactive "]" ">"))
  14581. (apply 'encode-time time))
  14582. string (replace-match str t t string)
  14583. start (+ start (length str)))))))
  14584. string)
  14585. (defun org-fix-decoded-time (time)
  14586. "Set 0 instead of nil for the first 6 elements of time.
  14587. Don't touch the rest."
  14588. (let ((n 0))
  14589. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14590. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14591. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14592. "Difference between TIMESTAMP-STRING and now in days.
  14593. If SECONDS is non-nil, return the difference in seconds."
  14594. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14595. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14596. (funcall fdiff (current-time)))))
  14597. (defun org-deadline-close (timestamp-string &optional ndays)
  14598. "Is the time in TIMESTAMP-STRING close to the current date?"
  14599. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14600. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14601. (not (org-entry-is-done-p))))
  14602. (defun org-get-wdays (ts)
  14603. "Get the deadline lead time appropriate for timestring TS."
  14604. (cond
  14605. ((<= org-deadline-warning-days 0)
  14606. ;; 0 or negative, enforce this value no matter what
  14607. (- org-deadline-warning-days))
  14608. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14609. ;; lead time is specified.
  14610. (floor (* (string-to-number (match-string 1 ts))
  14611. (cdr (assoc (match-string 2 ts)
  14612. '(("d" . 1) ("w" . 7)
  14613. ("m" . 30.4) ("y" . 365.25)))))))
  14614. ;; go for the default.
  14615. (t org-deadline-warning-days)))
  14616. (defun org-calendar-select-mouse (ev)
  14617. "Return to `org-read-date' with the date currently selected.
  14618. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14619. (interactive "e")
  14620. (mouse-set-point ev)
  14621. (when (calendar-cursor-to-date)
  14622. (let* ((date (calendar-cursor-to-date))
  14623. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14624. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14625. (if (active-minibuffer-window) (exit-minibuffer))))
  14626. (defun org-check-deadlines (ndays)
  14627. "Check if there are any deadlines due or past due.
  14628. A deadline is considered due if it happens within `org-deadline-warning-days'
  14629. days from today's date. If the deadline appears in an entry marked DONE,
  14630. it is not shown. The prefix arg NDAYS can be used to test that many
  14631. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14632. (interactive "P")
  14633. (let* ((org-warn-days
  14634. (cond
  14635. ((equal ndays '(4)) 100000)
  14636. (ndays (prefix-numeric-value ndays))
  14637. (t (abs org-deadline-warning-days))))
  14638. (case-fold-search nil)
  14639. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14640. (callback
  14641. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14642. (message "%d deadlines past-due or due within %d days"
  14643. (org-occur regexp nil callback)
  14644. org-warn-days)))
  14645. (defsubst org-re-timestamp (type)
  14646. "Return a regexp for timestamp TYPE.
  14647. Allowed values for TYPE are:
  14648. all: all timestamps
  14649. active: only active timestamps (<...>)
  14650. inactive: only inactive timestamps ([...])
  14651. scheduled: only scheduled timestamps
  14652. deadline: only deadline timestamps
  14653. When TYPE is nil, fall back on returning a regexp that matches
  14654. both scheduled and deadline timestamps."
  14655. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14656. ((eq type 'active) org-ts-regexp)
  14657. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14658. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14659. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14660. ((eq type 'scheduled-or-deadline)
  14661. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14662. (defun org-check-before-date (date)
  14663. "Check if there are deadlines or scheduled entries before DATE."
  14664. (interactive (list (org-read-date)))
  14665. (let ((case-fold-search nil)
  14666. (regexp (org-re-timestamp org-ts-type))
  14667. (callback
  14668. (lambda () (time-less-p
  14669. (org-time-string-to-time (match-string 1))
  14670. (org-time-string-to-time date)))))
  14671. (message "%d entries before %s"
  14672. (org-occur regexp nil callback) date)))
  14673. (defun org-check-after-date (date)
  14674. "Check if there are deadlines or scheduled entries after DATE."
  14675. (interactive (list (org-read-date)))
  14676. (let ((case-fold-search nil)
  14677. (regexp (org-re-timestamp org-ts-type))
  14678. (callback
  14679. (lambda () (not
  14680. (time-less-p
  14681. (org-time-string-to-time (match-string 1))
  14682. (org-time-string-to-time date))))))
  14683. (message "%d entries after %s"
  14684. (org-occur regexp nil callback) date)))
  14685. (defun org-check-dates-range (start-date end-date)
  14686. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14687. (interactive (list (org-read-date nil nil nil "Range starts")
  14688. (org-read-date nil nil nil "Range end")))
  14689. (let ((case-fold-search nil)
  14690. (regexp (org-re-timestamp org-ts-type))
  14691. (callback
  14692. (lambda ()
  14693. (let ((match (match-string 1)))
  14694. (and
  14695. (not (time-less-p
  14696. (org-time-string-to-time match)
  14697. (org-time-string-to-time start-date)))
  14698. (time-less-p
  14699. (org-time-string-to-time match)
  14700. (org-time-string-to-time end-date)))))))
  14701. (message "%d entries between %s and %s"
  14702. (org-occur regexp nil callback) start-date end-date)))
  14703. (defun org-evaluate-time-range (&optional to-buffer)
  14704. "Evaluate a time range by computing the difference between start and end.
  14705. Normally the result is just printed in the echo area, but with prefix arg
  14706. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14707. If the time range is actually in a table, the result is inserted into the
  14708. next column.
  14709. For time difference computation, a year is assumed to be exactly 365
  14710. days in order to avoid rounding problems."
  14711. (interactive "P")
  14712. (or
  14713. (org-clock-update-time-maybe)
  14714. (save-excursion
  14715. (unless (org-at-date-range-p t)
  14716. (goto-char (point-at-bol))
  14717. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14718. (if (not (org-at-date-range-p t))
  14719. (error "Not at a time-stamp range, and none found in current line")))
  14720. (let* ((ts1 (match-string 1))
  14721. (ts2 (match-string 2))
  14722. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14723. (match-end (match-end 0))
  14724. (time1 (org-time-string-to-time ts1))
  14725. (time2 (org-time-string-to-time ts2))
  14726. (t1 (org-float-time time1))
  14727. (t2 (org-float-time time2))
  14728. (diff (abs (- t2 t1)))
  14729. (negative (< (- t2 t1) 0))
  14730. ;; (ys (floor (* 365 24 60 60)))
  14731. (ds (* 24 60 60))
  14732. (hs (* 60 60))
  14733. (fy "%dy %dd %02d:%02d")
  14734. (fy1 "%dy %dd")
  14735. (fd "%dd %02d:%02d")
  14736. (fd1 "%dd")
  14737. (fh "%02d:%02d")
  14738. y d h m align)
  14739. (if havetime
  14740. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14741. y 0
  14742. d (floor (/ diff ds)) diff (mod diff ds)
  14743. h (floor (/ diff hs)) diff (mod diff hs)
  14744. m (floor (/ diff 60)))
  14745. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14746. y 0
  14747. d (floor (+ (/ diff ds) 0.5))
  14748. h 0 m 0))
  14749. (if (not to-buffer)
  14750. (message "%s" (org-make-tdiff-string y d h m))
  14751. (if (org-at-table-p)
  14752. (progn
  14753. (goto-char match-end)
  14754. (setq align t)
  14755. (and (looking-at " *|") (goto-char (match-end 0))))
  14756. (goto-char match-end))
  14757. (if (looking-at
  14758. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14759. (replace-match ""))
  14760. (if negative (insert " -"))
  14761. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14762. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14763. (insert " " (format fh h m))))
  14764. (if align (org-table-align))
  14765. (message "Time difference inserted")))))
  14766. (defun org-make-tdiff-string (y d h m)
  14767. (let ((fmt "")
  14768. (l nil))
  14769. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14770. l (push y l)))
  14771. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14772. l (push d l)))
  14773. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14774. l (push h l)))
  14775. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14776. l (push m l)))
  14777. (apply 'format fmt (nreverse l))))
  14778. (defun org-time-string-to-time (s &optional buffer pos)
  14779. "Convert a timestamp string into internal time."
  14780. (condition-case errdata
  14781. (apply 'encode-time (org-parse-time-string s))
  14782. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14783. s (if (not (and buffer pos))
  14784. ""
  14785. (format " at %d in buffer `%s'" pos buffer))
  14786. (cdr errdata)))))
  14787. (defun org-time-string-to-seconds (s)
  14788. "Convert a timestamp string to a number of seconds."
  14789. (org-float-time (org-time-string-to-time s)))
  14790. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14791. "Convert a time stamp to an absolute day number.
  14792. If there is a specifier for a cyclic time stamp, get the closest date to
  14793. DAYNR.
  14794. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14795. The variable date is bound by the calendar when this is called."
  14796. (cond
  14797. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14798. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14799. daynr
  14800. (+ daynr 1000)))
  14801. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14802. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14803. (time-to-days (current-time))) (match-string 0 s)
  14804. prefer show-all))
  14805. (t (time-to-days
  14806. (condition-case errdata
  14807. (apply 'encode-time (org-parse-time-string s))
  14808. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14809. s (if (not (and buffer pos))
  14810. ""
  14811. (format " at %d in buffer `%s'" pos buffer))
  14812. (cdr errdata))))))))
  14813. (defun org-days-to-iso-week (days)
  14814. "Return the iso week number."
  14815. (require 'cal-iso)
  14816. (car (calendar-iso-from-absolute days)))
  14817. (defun org-small-year-to-year (year)
  14818. "Convert 2-digit years into 4-digit years.
  14819. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14820. The year 2000 cannot be abbreviated. Any year larger than 99
  14821. is returned unchanged."
  14822. (if (< year 38)
  14823. (setq year (+ 2000 year))
  14824. (if (< year 100)
  14825. (setq year (+ 1900 year))))
  14826. year)
  14827. (defun org-time-from-absolute (d)
  14828. "Return the time corresponding to date D.
  14829. D may be an absolute day number, or a calendar-type list (month day year)."
  14830. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14831. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14832. (defun org-calendar-holiday ()
  14833. "List of holidays, for Diary display in Org-mode."
  14834. (require 'holidays)
  14835. (let ((hl (funcall
  14836. (if (fboundp 'calendar-check-holidays)
  14837. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14838. (if hl (mapconcat 'identity hl "; "))))
  14839. (defun org-diary-sexp-entry (sexp entry date)
  14840. "Process a SEXP diary ENTRY for DATE."
  14841. (require 'diary-lib)
  14842. (let ((result (if calendar-debug-sexp
  14843. (let ((stack-trace-on-error t))
  14844. (eval (car (read-from-string sexp))))
  14845. (condition-case nil
  14846. (eval (car (read-from-string sexp)))
  14847. (error
  14848. (beep)
  14849. (message "Bad sexp at line %d in %s: %s"
  14850. (org-current-line)
  14851. (buffer-file-name) sexp)
  14852. (sleep-for 2))))))
  14853. (cond ((stringp result) (split-string result "; "))
  14854. ((and (consp result)
  14855. (not (consp (cdr result)))
  14856. (stringp (cdr result))) (cdr result))
  14857. ((and (consp result)
  14858. (stringp (car result))) result)
  14859. (result entry))))
  14860. (defun org-diary-to-ical-string (frombuf)
  14861. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14862. This uses the icalendar.el library."
  14863. (let* ((tmpdir (if (featurep 'xemacs)
  14864. (temp-directory)
  14865. temporary-file-directory))
  14866. (tmpfile (make-temp-name
  14867. (expand-file-name "orgics" tmpdir)))
  14868. buf rtn b e)
  14869. (with-current-buffer frombuf
  14870. (icalendar-export-region (point-min) (point-max) tmpfile)
  14871. (setq buf (find-buffer-visiting tmpfile))
  14872. (set-buffer buf)
  14873. (goto-char (point-min))
  14874. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14875. (setq b (match-beginning 0)))
  14876. (goto-char (point-max))
  14877. (if (re-search-backward "^END:VEVENT" nil t)
  14878. (setq e (match-end 0)))
  14879. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14880. (kill-buffer buf)
  14881. (delete-file tmpfile)
  14882. rtn))
  14883. (defun org-closest-date (start current change prefer show-all)
  14884. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14885. When PREFER is `past', return a date that is either CURRENT or past.
  14886. When PREFER is `future', return a date that is either CURRENT or future.
  14887. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14888. ;; Make the proper lists from the dates
  14889. (catch 'exit
  14890. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14891. dn dw sday cday n1 n2 n0
  14892. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14893. (setq start (org-date-to-gregorian start)
  14894. current (org-date-to-gregorian
  14895. (if show-all
  14896. current
  14897. (time-to-days (current-time))))
  14898. sday (calendar-absolute-from-gregorian start)
  14899. cday (calendar-absolute-from-gregorian current))
  14900. (if (<= cday sday) (throw 'exit sday))
  14901. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14902. (setq dn (string-to-number (match-string 1 change))
  14903. dw (cdr (assoc (match-string 2 change) a1)))
  14904. (error "Invalid change specifier: %s" change))
  14905. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14906. (cond
  14907. ((eq dw 'day)
  14908. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14909. n2 (+ n1 dn)))
  14910. ((eq dw 'year)
  14911. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14912. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14913. (setq date1 (list m d y1)
  14914. n1 (calendar-absolute-from-gregorian date1)
  14915. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14916. n2 (calendar-absolute-from-gregorian date2)))
  14917. ((eq dw 'month)
  14918. ;; approx number of month between the two dates
  14919. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14920. ;; How often does dn fit in there?
  14921. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14922. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14923. m (+ m nm)
  14924. ny (floor (/ m 12))
  14925. y (+ y ny)
  14926. m (- m (* ny 12)))
  14927. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14928. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14929. (setq m2 (+ m dn) y2 y)
  14930. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14931. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14932. (while (<= n2 cday)
  14933. (setq n1 n2 m m2 y y2)
  14934. (setq m2 (+ m dn) y2 y)
  14935. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14936. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14937. ;; Make sure n1 is the earlier date
  14938. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14939. (if show-all
  14940. (cond
  14941. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14942. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14943. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14944. (cond
  14945. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14946. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14947. (t (if (= cday n1) n1 n2)))))))
  14948. (defun org-date-to-gregorian (date)
  14949. "Turn any specification of DATE into a Gregorian date for the calendar."
  14950. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14951. ((and (listp date) (= (length date) 3)) date)
  14952. ((stringp date)
  14953. (setq date (org-parse-time-string date))
  14954. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14955. ((listp date)
  14956. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14957. (defun org-parse-time-string (s &optional nodefault)
  14958. "Parse the standard Org-mode time string.
  14959. This should be a lot faster than the normal `parse-time-string'.
  14960. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14961. hour and minute fields will be nil if not given."
  14962. (if (string-match org-ts-regexp0 s)
  14963. (list 0
  14964. (if (or (match-beginning 8) (not nodefault))
  14965. (string-to-number (or (match-string 8 s) "0")))
  14966. (if (or (match-beginning 7) (not nodefault))
  14967. (string-to-number (or (match-string 7 s) "0")))
  14968. (string-to-number (match-string 4 s))
  14969. (string-to-number (match-string 3 s))
  14970. (string-to-number (match-string 2 s))
  14971. nil nil nil)
  14972. (error "Not a standard Org-mode time string: %s" s)))
  14973. (defun org-timestamp-up (&optional arg)
  14974. "Increase the date item at the cursor by one.
  14975. If the cursor is on the year, change the year. If it is on the month,
  14976. the day or the time, change that.
  14977. With prefix ARG, change by that many units."
  14978. (interactive "p")
  14979. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14980. (defun org-timestamp-down (&optional arg)
  14981. "Decrease the date item at the cursor by one.
  14982. If the cursor is on the year, change the year. If it is on the month,
  14983. the day or the time, change that.
  14984. With prefix ARG, change by that many units."
  14985. (interactive "p")
  14986. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14987. (defun org-timestamp-up-day (&optional arg)
  14988. "Increase the date in the time stamp by one day.
  14989. With prefix ARG, change that many days."
  14990. (interactive "p")
  14991. (if (and (not (org-at-timestamp-p t))
  14992. (org-at-heading-p))
  14993. (org-todo 'up)
  14994. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14995. (defun org-timestamp-down-day (&optional arg)
  14996. "Decrease the date in the time stamp by one day.
  14997. With prefix ARG, change that many days."
  14998. (interactive "p")
  14999. (if (and (not (org-at-timestamp-p t))
  15000. (org-at-heading-p))
  15001. (org-todo 'down)
  15002. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15003. (defun org-at-timestamp-p (&optional inactive-ok)
  15004. "Determine if the cursor is in or at a timestamp."
  15005. (interactive)
  15006. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15007. (pos (point))
  15008. (ans (or (looking-at tsr)
  15009. (save-excursion
  15010. (skip-chars-backward "^[<\n\r\t")
  15011. (if (> (point) (point-min)) (backward-char 1))
  15012. (and (looking-at tsr)
  15013. (> (- (match-end 0) pos) -1))))))
  15014. (and ans
  15015. (boundp 'org-ts-what)
  15016. (setq org-ts-what
  15017. (cond
  15018. ((= pos (match-beginning 0)) 'bracket)
  15019. ;; Point is considered to be "on the bracket" whether
  15020. ;; it's really on it or right after it.
  15021. ((= pos (1- (match-end 0))) 'bracket)
  15022. ((= pos (match-end 0)) 'after)
  15023. ((org-pos-in-match-range pos 2) 'year)
  15024. ((org-pos-in-match-range pos 3) 'month)
  15025. ((org-pos-in-match-range pos 7) 'hour)
  15026. ((org-pos-in-match-range pos 8) 'minute)
  15027. ((or (org-pos-in-match-range pos 4)
  15028. (org-pos-in-match-range pos 5)) 'day)
  15029. ((and (> pos (or (match-end 8) (match-end 5)))
  15030. (< pos (match-end 0)))
  15031. (- pos (or (match-end 8) (match-end 5))))
  15032. (t 'day))))
  15033. ans))
  15034. (defun org-toggle-timestamp-type ()
  15035. "Toggle the type (<active> or [inactive]) of a time stamp."
  15036. (interactive)
  15037. (when (org-at-timestamp-p t)
  15038. (let ((beg (match-beginning 0)) (end (match-end 0))
  15039. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15040. (save-excursion
  15041. (goto-char beg)
  15042. (while (re-search-forward "[][<>]" end t)
  15043. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15044. t t)))
  15045. (message "Timestamp is now %sactive"
  15046. (if (equal (char-after beg) ?<) "" "in")))))
  15047. (defvar org-clock-history) ; defined in org-clock.el
  15048. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15049. (defun org-timestamp-change (n &optional what updown)
  15050. "Change the date in the time stamp at point.
  15051. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15052. `year', `minute', `second'. If WHAT is not given, the cursor position
  15053. in the timestamp determines what will be changed."
  15054. (let ((origin (point)) origin-cat
  15055. with-hm inactive
  15056. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15057. org-ts-what
  15058. extra rem
  15059. ts time time0 fixnext clrgx)
  15060. (if (not (org-at-timestamp-p t))
  15061. (error "Not at a timestamp"))
  15062. (if (and (not what) (eq org-ts-what 'bracket))
  15063. (org-toggle-timestamp-type)
  15064. ;; Point isn't on brackets. Remember the part of the time-stamp
  15065. ;; the point was in. Indeed, size of time-stamps may change,
  15066. ;; but point must be kept in the same category nonetheless.
  15067. (setq origin-cat org-ts-what)
  15068. (if (and (not what) (not (eq org-ts-what 'day))
  15069. org-display-custom-times
  15070. (get-text-property (point) 'display)
  15071. (not (get-text-property (1- (point)) 'display)))
  15072. (setq org-ts-what 'day))
  15073. (setq org-ts-what (or what org-ts-what)
  15074. inactive (= (char-after (match-beginning 0)) ?\[)
  15075. ts (match-string 0))
  15076. (replace-match "")
  15077. (if (string-match
  15078. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15079. ts)
  15080. (setq extra (match-string 1 ts)))
  15081. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15082. (setq with-hm t))
  15083. (setq time0 (org-parse-time-string ts))
  15084. (when (and updown
  15085. (eq org-ts-what 'minute)
  15086. (not current-prefix-arg))
  15087. ;; This looks like s-up and s-down. Change by one rounding step.
  15088. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15089. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15090. (setcar (cdr time0) (+ (nth 1 time0)
  15091. (if (> n 0) (- rem) (- dm rem))))))
  15092. (setq time
  15093. (encode-time (or (car time0) 0)
  15094. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15095. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15096. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15097. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15098. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15099. (nthcdr 6 time0)))
  15100. (when (and (member org-ts-what '(hour minute))
  15101. extra
  15102. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15103. (setq extra (org-modify-ts-extra
  15104. extra
  15105. (if (eq org-ts-what 'hour) 2 5)
  15106. n dm)))
  15107. (when (integerp org-ts-what)
  15108. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15109. (if (eq what 'calendar)
  15110. (let ((cal-date (org-get-date-from-calendar)))
  15111. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15112. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15113. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15114. (setcar time0 (or (car time0) 0))
  15115. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15116. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15117. (setq time (apply 'encode-time time0))))
  15118. ;; Insert the new time-stamp, and ensure point stays in the same
  15119. ;; category as before (i.e. not after the last position in that
  15120. ;; category).
  15121. (let ((pos (point)))
  15122. ;; Stay before inserted string. `save-excursion' is of no use.
  15123. (setq org-last-changed-timestamp
  15124. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15125. (goto-char pos))
  15126. (save-match-data
  15127. (looking-at org-ts-regexp3)
  15128. (goto-char (cond
  15129. ;; `day' category ends before `hour' if any, or at
  15130. ;; the end of the day name.
  15131. ((eq origin-cat 'day)
  15132. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15133. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15134. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15135. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15136. ;; `year' and `month' have both fixed size: point
  15137. ;; couldn't have moved into another part.
  15138. (t origin))))
  15139. ;; Update clock if on a CLOCK line.
  15140. (org-clock-update-time-maybe)
  15141. ;; Maybe adjust the closest clock in `org-clock-history'
  15142. (when org-clock-adjust-closest
  15143. (if (not (and (org-at-clock-log-p)
  15144. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15145. org-clock-history))))))
  15146. (message "No clock to adjust")
  15147. (cond ((save-excursion ; fix previous clock?
  15148. (re-search-backward org-ts-regexp0 nil t)
  15149. (org-looking-back (concat org-clock-string " \\[")))
  15150. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15151. ((save-excursion ; fix next clock?
  15152. (re-search-backward org-ts-regexp0 nil t)
  15153. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15154. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15155. (save-window-excursion
  15156. ;; Find closest clock to point, adjust the previous/next one in history
  15157. (let* ((p (save-excursion (org-back-to-heading t)))
  15158. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15159. (clfixnth
  15160. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15161. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15162. (if (not clfixpos)
  15163. (message "No clock to adjust")
  15164. (save-excursion
  15165. (org-goto-marker-or-bmk clfixpos)
  15166. (org-show-subtree)
  15167. (when (re-search-forward clrgx nil t)
  15168. (goto-char (match-beginning 1))
  15169. (let (org-clock-adjust-closest)
  15170. (org-timestamp-change n org-ts-what updown))
  15171. (message "Clock adjusted in %s for heading: %s"
  15172. (file-name-nondirectory (buffer-file-name))
  15173. (org-get-heading t t)))))))))
  15174. ;; Try to recenter the calendar window, if any.
  15175. (if (and org-calendar-follow-timestamp-change
  15176. (get-buffer-window "*Calendar*" t)
  15177. (memq org-ts-what '(day month year)))
  15178. (org-recenter-calendar (time-to-days time))))))
  15179. (defun org-modify-ts-extra (s pos n dm)
  15180. "Change the different parts of the lead-time and repeat fields in timestamp."
  15181. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15182. ng h m new rem)
  15183. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15184. (cond
  15185. ((or (org-pos-in-match-range pos 2)
  15186. (org-pos-in-match-range pos 3))
  15187. (setq m (string-to-number (match-string 3 s))
  15188. h (string-to-number (match-string 2 s)))
  15189. (if (org-pos-in-match-range pos 2)
  15190. (setq h (+ h n))
  15191. (setq n (* dm (org-no-warnings (signum n))))
  15192. (when (not (= 0 (setq rem (% m dm))))
  15193. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15194. (setq m (+ m n)))
  15195. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15196. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15197. (setq h (min 24 (max 0 h)))
  15198. (setq ng 1 new (format "-%02d:%02d" h m)))
  15199. ((org-pos-in-match-range pos 6)
  15200. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15201. ((org-pos-in-match-range pos 5)
  15202. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15203. ((org-pos-in-match-range pos 9)
  15204. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15205. ((org-pos-in-match-range pos 8)
  15206. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15207. (when ng
  15208. (setq s (concat
  15209. (substring s 0 (match-beginning ng))
  15210. new
  15211. (substring s (match-end ng))))))
  15212. s))
  15213. (defun org-recenter-calendar (date)
  15214. "If the calendar is visible, recenter it to DATE."
  15215. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15216. (when cwin
  15217. (let ((calendar-move-hook nil))
  15218. (with-selected-window cwin
  15219. (calendar-goto-date (if (listp date) date
  15220. (calendar-gregorian-from-absolute date))))))))
  15221. (defun org-goto-calendar (&optional arg)
  15222. "Go to the Emacs calendar at the current date.
  15223. If there is a time stamp in the current line, go to that date.
  15224. A prefix ARG can be used to force the current date."
  15225. (interactive "P")
  15226. (let ((tsr org-ts-regexp) diff
  15227. (calendar-move-hook nil)
  15228. (calendar-view-holidays-initially-flag nil)
  15229. (calendar-view-diary-initially-flag nil))
  15230. (if (or (org-at-timestamp-p)
  15231. (save-excursion
  15232. (beginning-of-line 1)
  15233. (looking-at (concat ".*" tsr))))
  15234. (let ((d1 (time-to-days (current-time)))
  15235. (d2 (time-to-days
  15236. (org-time-string-to-time (match-string 1)))))
  15237. (setq diff (- d2 d1))))
  15238. (calendar)
  15239. (calendar-goto-today)
  15240. (if (and diff (not arg)) (calendar-forward-day diff))))
  15241. (defun org-get-date-from-calendar ()
  15242. "Return a list (month day year) of date at point in calendar."
  15243. (with-current-buffer "*Calendar*"
  15244. (save-match-data
  15245. (calendar-cursor-to-date))))
  15246. (defun org-date-from-calendar ()
  15247. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15248. If there is already a time stamp at the cursor position, update it."
  15249. (interactive)
  15250. (if (org-at-timestamp-p t)
  15251. (org-timestamp-change 0 'calendar)
  15252. (let ((cal-date (org-get-date-from-calendar)))
  15253. (org-insert-time-stamp
  15254. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15255. (defun org-minutes-to-hh:mm-string (m)
  15256. "Compute H:MM from a number of minutes."
  15257. (let ((h (/ m 60)))
  15258. (setq m (- m (* 60 h)))
  15259. (format org-time-clocksum-format h m)))
  15260. (defun org-hh:mm-string-to-minutes (s)
  15261. "Convert a string H:MM to a number of minutes.
  15262. If the string is just a number, interpret it as minutes.
  15263. In fact, the first hh:mm or number in the string will be taken,
  15264. there can be extra stuff in the string.
  15265. If no number is found, the return value is 0."
  15266. (cond
  15267. ((integerp s) s)
  15268. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15269. (+ (* (string-to-number (match-string 1 s)) 60)
  15270. (string-to-number (match-string 2 s))))
  15271. ((string-match "\\([0-9]+\\)" s)
  15272. (string-to-number (match-string 1 s)))
  15273. (t 0)))
  15274. (defcustom org-effort-durations
  15275. `(("h" . 60)
  15276. ("d" . ,(* 60 8))
  15277. ("w" . ,(* 60 8 5))
  15278. ("m" . ,(* 60 8 5 4))
  15279. ("y" . ,(* 60 8 5 40)))
  15280. "Conversion factor to minutes for an effort modifier.
  15281. Each entry has the form (MODIFIER . MINUTES).
  15282. In an effort string, a number followed by MODIFIER is multiplied
  15283. by the specified number of MINUTES to obtain an effort in
  15284. minutes.
  15285. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15286. effort string \"2hours\" is equivalent to 120 minutes."
  15287. :group 'org-agenda
  15288. :version "24.1"
  15289. :type '(alist :key-type (string :tag "Modifier")
  15290. :value-type (number :tag "Minutes")))
  15291. (defcustom org-image-actual-width t
  15292. "Should we use the actual width of images when inlining them?
  15293. When set to `t', always use the image width.
  15294. When set to a number, use imagemagick (when available) to set
  15295. the image's width to this value.
  15296. When set to a number in a list, try to get the width from the
  15297. #+ATTR.* keyword if it matches a width specification like
  15298. width=\"[0-9]+\", and fall back on that number if none is found.
  15299. When set to nil, try to get the width from an #+ATTR.* keyword
  15300. and fall back on the original width if none is found.
  15301. This requires Emacs >= 24.1, build with imagemagick support."
  15302. :group 'org-appearance
  15303. :version "24.3"
  15304. :type '(choice
  15305. (const :tag "Use the image width" t)
  15306. (integer :tag "Use a number of pixels")
  15307. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15308. (const :tag "Use #+ATTR* or don't resize" nil)))
  15309. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15310. "Convert a duration string S to minutes.
  15311. A bare number is interpreted as minutes, modifiers can be set by
  15312. customizing `org-effort-durations' (which see).
  15313. Entries containing a colon are interpreted as H:MM by
  15314. `org-hh:mm-string-to-minutes'."
  15315. (let ((result 0)
  15316. (re (concat "\\([0-9.]+\\) *\\("
  15317. (regexp-opt (mapcar 'car org-effort-durations))
  15318. "\\)")))
  15319. (while (string-match re s)
  15320. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15321. (string-to-number (match-string 1 s))))
  15322. (setq s (replace-match "" nil t s)))
  15323. (setq result (floor result))
  15324. (incf result (org-hh:mm-string-to-minutes s))
  15325. (if output-to-string (number-to-string result) result)))
  15326. ;;;; Files
  15327. (defun org-save-all-org-buffers ()
  15328. "Save all Org-mode buffers without user confirmation."
  15329. (interactive)
  15330. (message "Saving all Org-mode buffers...")
  15331. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15332. (when (featurep 'org-id) (org-id-locations-save))
  15333. (message "Saving all Org-mode buffers... done"))
  15334. (defun org-revert-all-org-buffers ()
  15335. "Revert all Org-mode buffers.
  15336. Prompt for confirmation when there are unsaved changes.
  15337. Be sure you know what you are doing before letting this function
  15338. overwrite your changes.
  15339. This function is useful in a setup where one tracks org files
  15340. with a version control system, to revert on one machine after pulling
  15341. changes from another. I believe the procedure must be like this:
  15342. 1. M-x org-save-all-org-buffers
  15343. 2. Pull changes from the other machine, resolve conflicts
  15344. 3. M-x org-revert-all-org-buffers"
  15345. (interactive)
  15346. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15347. (error "Abort"))
  15348. (save-excursion
  15349. (save-window-excursion
  15350. (mapc
  15351. (lambda (b)
  15352. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15353. (with-current-buffer b buffer-file-name))
  15354. (org-pop-to-buffer-same-window b)
  15355. (revert-buffer t 'no-confirm)))
  15356. (buffer-list))
  15357. (when (and (featurep 'org-id) org-id-track-globally)
  15358. (org-id-locations-load)))))
  15359. ;;;; Agenda files
  15360. ;;;###autoload
  15361. (defun org-switchb (&optional arg)
  15362. "Switch between Org buffers.
  15363. With one prefix argument, restrict available buffers to files.
  15364. With two prefix arguments, restrict available buffers to agenda files.
  15365. Defaults to `iswitchb' for buffer name completion.
  15366. Set `org-completion-use-ido' to make it use ido instead."
  15367. (interactive "P")
  15368. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15369. ((equal arg '(16)) (org-buffer-list 'agenda))
  15370. (t (org-buffer-list))))
  15371. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15372. (org-completion-use-ido org-completion-use-ido))
  15373. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15374. (setq org-completion-use-iswitchb t))
  15375. (org-pop-to-buffer-same-window
  15376. (org-icompleting-read "Org buffer: "
  15377. (mapcar 'list (mapcar 'buffer-name blist))
  15378. nil t))))
  15379. ;;; Define some older names previously used for this functionality
  15380. ;;;###autoload
  15381. (defalias 'org-ido-switchb 'org-switchb)
  15382. ;;;###autoload
  15383. (defalias 'org-iswitchb 'org-switchb)
  15384. (defun org-buffer-list (&optional predicate exclude-tmp)
  15385. "Return a list of Org buffers.
  15386. PREDICATE can be `export', `files' or `agenda'.
  15387. export restrict the list to Export buffers.
  15388. files restrict the list to buffers visiting Org files.
  15389. agenda restrict the list to buffers visiting agenda files.
  15390. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15391. (let* ((bfn nil)
  15392. (agenda-files (and (eq predicate 'agenda)
  15393. (mapcar 'file-truename (org-agenda-files t))))
  15394. (filter
  15395. (cond
  15396. ((eq predicate 'files)
  15397. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15398. ((eq predicate 'export)
  15399. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15400. ((eq predicate 'agenda)
  15401. (lambda (b)
  15402. (with-current-buffer b
  15403. (and (derived-mode-p 'org-mode)
  15404. (setq bfn (buffer-file-name b))
  15405. (member (file-truename bfn) agenda-files)))))
  15406. (t (lambda (b) (with-current-buffer b
  15407. (or (derived-mode-p 'org-mode)
  15408. (string-match "\*Org .*Export"
  15409. (buffer-name b)))))))))
  15410. (delq nil
  15411. (mapcar
  15412. (lambda(b)
  15413. (if (and (funcall filter b)
  15414. (or (not exclude-tmp)
  15415. (not (string-match "tmp" (buffer-name b)))))
  15416. b
  15417. nil))
  15418. (buffer-list)))))
  15419. (defun org-agenda-files (&optional unrestricted archives)
  15420. "Get the list of agenda files.
  15421. Optional UNRESTRICTED means return the full list even if a restriction
  15422. is currently in place.
  15423. When ARCHIVES is t, include all archive files that are really being
  15424. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15425. `org-agenda-archives-mode' is t."
  15426. (let ((files
  15427. (cond
  15428. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15429. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15430. ((listp org-agenda-files) org-agenda-files)
  15431. (t (error "Invalid value of `org-agenda-files'")))))
  15432. (setq files (apply 'append
  15433. (mapcar (lambda (f)
  15434. (if (file-directory-p f)
  15435. (directory-files
  15436. f t org-agenda-file-regexp)
  15437. (list f)))
  15438. files)))
  15439. (when org-agenda-skip-unavailable-files
  15440. (setq files (delq nil
  15441. (mapcar (function
  15442. (lambda (file)
  15443. (and (file-readable-p file) file)))
  15444. files))))
  15445. (when (or (eq archives t)
  15446. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15447. (setq files (org-add-archive-files files)))
  15448. files))
  15449. (defun org-agenda-file-p (&optional file)
  15450. "Return non-nil, if FILE is an agenda file.
  15451. If FILE is omitted, use the file associated with the current
  15452. buffer."
  15453. (member (or file (buffer-file-name))
  15454. (org-agenda-files t)))
  15455. (defun org-edit-agenda-file-list ()
  15456. "Edit the list of agenda files.
  15457. Depending on setup, this either uses customize to edit the variable
  15458. `org-agenda-files', or it visits the file that is holding the list. In the
  15459. latter case, the buffer is set up in a way that saving it automatically kills
  15460. the buffer and restores the previous window configuration."
  15461. (interactive)
  15462. (if (stringp org-agenda-files)
  15463. (let ((cw (current-window-configuration)))
  15464. (find-file org-agenda-files)
  15465. (org-set-local 'org-window-configuration cw)
  15466. (org-add-hook 'after-save-hook
  15467. (lambda ()
  15468. (set-window-configuration
  15469. (prog1 org-window-configuration
  15470. (kill-buffer (current-buffer))))
  15471. (org-install-agenda-files-menu)
  15472. (message "New agenda file list installed"))
  15473. nil 'local)
  15474. (message "%s" (substitute-command-keys
  15475. "Edit list and finish with \\[save-buffer]")))
  15476. (customize-variable 'org-agenda-files)))
  15477. (defun org-store-new-agenda-file-list (list)
  15478. "Set new value for the agenda file list and save it correctly."
  15479. (if (stringp org-agenda-files)
  15480. (let ((fe (org-read-agenda-file-list t)) b u)
  15481. (while (setq b (find-buffer-visiting org-agenda-files))
  15482. (kill-buffer b))
  15483. (with-temp-file org-agenda-files
  15484. (insert
  15485. (mapconcat
  15486. (lambda (f) ;; Keep un-expanded entries.
  15487. (if (setq u (assoc f fe))
  15488. (cdr u)
  15489. f))
  15490. list "\n")
  15491. "\n")))
  15492. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15493. (org-insert-mode-line-in-empty-file nil))
  15494. (setq org-agenda-files list)
  15495. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15496. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15497. "Read the list of agenda files from a file.
  15498. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15499. filenames, used by `org-store-new-agenda-file-list' to write back
  15500. un-expanded file names."
  15501. (when (file-directory-p org-agenda-files)
  15502. (error "`org-agenda-files' cannot be a single directory"))
  15503. (when (stringp org-agenda-files)
  15504. (with-temp-buffer
  15505. (insert-file-contents org-agenda-files)
  15506. (mapcar
  15507. (lambda (f)
  15508. (let ((e (expand-file-name (substitute-in-file-name f)
  15509. org-directory)))
  15510. (if pair-with-expansion
  15511. (cons e f)
  15512. e)))
  15513. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15514. ;;;###autoload
  15515. (defun org-cycle-agenda-files ()
  15516. "Cycle through the files in `org-agenda-files'.
  15517. If the current buffer visits an agenda file, find the next one in the list.
  15518. If the current buffer does not, find the first agenda file."
  15519. (interactive)
  15520. (let* ((fs (org-agenda-files t))
  15521. (files (append fs (list (car fs))))
  15522. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15523. file)
  15524. (unless files (error "No agenda files"))
  15525. (catch 'exit
  15526. (while (setq file (pop files))
  15527. (if (equal (file-truename file) tcf)
  15528. (when (car files)
  15529. (find-file (car files))
  15530. (throw 'exit t))))
  15531. (find-file (car fs)))
  15532. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15533. (defun org-agenda-file-to-front (&optional to-end)
  15534. "Move/add the current file to the top of the agenda file list.
  15535. If the file is not present in the list, it is added to the front. If it is
  15536. present, it is moved there. With optional argument TO-END, add/move to the
  15537. end of the list."
  15538. (interactive "P")
  15539. (let ((org-agenda-skip-unavailable-files nil)
  15540. (file-alist (mapcar (lambda (x)
  15541. (cons (file-truename x) x))
  15542. (org-agenda-files t)))
  15543. (ctf (file-truename buffer-file-name))
  15544. x had)
  15545. (setq x (assoc ctf file-alist) had x)
  15546. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15547. (if to-end
  15548. (setq file-alist (append (delq x file-alist) (list x)))
  15549. (setq file-alist (cons x (delq x file-alist))))
  15550. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15551. (org-install-agenda-files-menu)
  15552. (message "File %s to %s of agenda file list"
  15553. (if had "moved" "added") (if to-end "end" "front"))))
  15554. (defun org-remove-file (&optional file)
  15555. "Remove current file from the list of files in variable `org-agenda-files'.
  15556. These are the files which are being checked for agenda entries.
  15557. Optional argument FILE means use this file instead of the current."
  15558. (interactive)
  15559. (let* ((org-agenda-skip-unavailable-files nil)
  15560. (file (or file buffer-file-name))
  15561. (true-file (file-truename file))
  15562. (afile (abbreviate-file-name file))
  15563. (files (delq nil (mapcar
  15564. (lambda (x)
  15565. (if (equal true-file
  15566. (file-truename x))
  15567. nil x))
  15568. (org-agenda-files t)))))
  15569. (if (not (= (length files) (length (org-agenda-files t))))
  15570. (progn
  15571. (org-store-new-agenda-file-list files)
  15572. (org-install-agenda-files-menu)
  15573. (message "Removed file: %s" afile))
  15574. (message "File was not in list: %s (not removed)" afile))))
  15575. (defun org-file-menu-entry (file)
  15576. (vector file (list 'find-file file) t))
  15577. (defun org-check-agenda-file (file)
  15578. "Make sure FILE exists. If not, ask user what to do."
  15579. (when (not (file-exists-p file))
  15580. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15581. (abbreviate-file-name file))
  15582. (let ((r (downcase (read-char-exclusive))))
  15583. (cond
  15584. ((equal r ?r)
  15585. (org-remove-file file)
  15586. (throw 'nextfile t))
  15587. (t (error "Abort"))))))
  15588. (defun org-get-agenda-file-buffer (file)
  15589. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15590. it to the list of buffers which might be released later."
  15591. (let ((buf (org-find-base-buffer-visiting file)))
  15592. (if buf
  15593. buf ; just return it
  15594. ;; Make a new buffer and remember it
  15595. (setq buf (find-file-noselect file))
  15596. (if buf (push buf org-agenda-new-buffers))
  15597. buf)))
  15598. (defun org-release-buffers (blist)
  15599. "Release all buffers in list, asking the user for confirmation when needed.
  15600. When a buffer is unmodified, it is just killed. When modified, it is saved
  15601. \(if the user agrees) and then killed."
  15602. (let (buf file)
  15603. (while (setq buf (pop blist))
  15604. (setq file (buffer-file-name buf))
  15605. (when (and (buffer-modified-p buf)
  15606. file
  15607. (y-or-n-p (format "Save file %s? " file)))
  15608. (with-current-buffer buf (save-buffer)))
  15609. (kill-buffer buf))))
  15610. (defun org-agenda-prepare-buffers (files)
  15611. "Create buffers for all agenda files, protect archived trees and comments."
  15612. (interactive)
  15613. (let ((pa '(:org-archived t))
  15614. (pc '(:org-comment t))
  15615. (pall '(:org-archived t :org-comment t))
  15616. (inhibit-read-only t)
  15617. (rea (concat ":" org-archive-tag ":"))
  15618. bmp file re)
  15619. (save-excursion
  15620. (save-restriction
  15621. (while (setq file (pop files))
  15622. (catch 'nextfile
  15623. (if (bufferp file)
  15624. (set-buffer file)
  15625. (org-check-agenda-file file)
  15626. (set-buffer (org-get-agenda-file-buffer file)))
  15627. (widen)
  15628. (setq bmp (buffer-modified-p))
  15629. (org-refresh-category-properties)
  15630. (setq org-todo-keywords-for-agenda
  15631. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15632. (setq org-done-keywords-for-agenda
  15633. (append org-done-keywords-for-agenda org-done-keywords))
  15634. (setq org-todo-keyword-alist-for-agenda
  15635. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15636. (setq org-drawers-for-agenda
  15637. (append org-drawers-for-agenda org-drawers))
  15638. (setq org-tag-alist-for-agenda
  15639. (append org-tag-alist-for-agenda org-tag-alist))
  15640. (save-excursion
  15641. (remove-text-properties (point-min) (point-max) pall)
  15642. (when org-agenda-skip-archived-trees
  15643. (goto-char (point-min))
  15644. (while (re-search-forward rea nil t)
  15645. (if (org-at-heading-p t)
  15646. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15647. (goto-char (point-min))
  15648. (setq re (format org-heading-keyword-regexp-format
  15649. org-comment-string))
  15650. (while (re-search-forward re nil t)
  15651. (add-text-properties
  15652. (match-beginning 0) (org-end-of-subtree t) pc)))
  15653. (set-buffer-modified-p bmp)))))
  15654. (setq org-todo-keywords-for-agenda
  15655. (org-uniquify org-todo-keywords-for-agenda))
  15656. (setq org-todo-keyword-alist-for-agenda
  15657. (org-uniquify org-todo-keyword-alist-for-agenda)
  15658. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15659. ;;;; Embedded LaTeX
  15660. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15661. "Keymap for the minor `org-cdlatex-mode'.")
  15662. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15663. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15664. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15665. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15666. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15667. (defvar org-cdlatex-texmathp-advice-is-done nil
  15668. "Flag remembering if we have applied the advice to texmathp already.")
  15669. (define-minor-mode org-cdlatex-mode
  15670. "Toggle the minor `org-cdlatex-mode'.
  15671. This mode supports entering LaTeX environment and math in LaTeX fragments
  15672. in Org-mode.
  15673. \\{org-cdlatex-mode-map}"
  15674. nil " OCDL" nil
  15675. (when org-cdlatex-mode
  15676. (require 'cdlatex)
  15677. (run-hooks 'cdlatex-mode-hook)
  15678. (cdlatex-compute-tables))
  15679. (unless org-cdlatex-texmathp-advice-is-done
  15680. (setq org-cdlatex-texmathp-advice-is-done t)
  15681. (defadvice texmathp (around org-math-always-on activate)
  15682. "Always return t in org-mode buffers.
  15683. This is because we want to insert math symbols without dollars even outside
  15684. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15685. an embedded LaTeX fragment, let texmathp do its job.
  15686. \\[org-cdlatex-mode-map]"
  15687. (interactive)
  15688. (let (p)
  15689. (cond
  15690. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15691. ((eq this-command 'cdlatex-math-symbol)
  15692. (setq ad-return-value t
  15693. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15694. (t
  15695. (let ((p (org-inside-LaTeX-fragment-p)))
  15696. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15697. (setq ad-return-value t
  15698. texmathp-why '("Org-mode embedded math" . 0))
  15699. (if p ad-do-it)))))))))
  15700. (defun turn-on-org-cdlatex ()
  15701. "Unconditionally turn on `org-cdlatex-mode'."
  15702. (org-cdlatex-mode 1))
  15703. (defun org-inside-LaTeX-fragment-p ()
  15704. "Test if point is inside a LaTeX fragment.
  15705. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15706. sequence appearing also before point.
  15707. Even though the matchers for math are configurable, this function assumes
  15708. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15709. delimiters are skipped when they have been removed by customization.
  15710. The return value is nil, or a cons cell with the delimiter and the
  15711. position of this delimiter.
  15712. This function does a reasonably good job, but can locally be fooled by
  15713. for example currency specifications. For example it will assume being in
  15714. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15715. fragments that are properly closed, but during editing, we have to live
  15716. with the uncertainty caused by missing closing delimiters. This function
  15717. looks only before point, not after."
  15718. (catch 'exit
  15719. (let ((pos (point))
  15720. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15721. (lim (progn
  15722. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15723. (point)))
  15724. dd-on str (start 0) m re)
  15725. (goto-char pos)
  15726. (when dodollar
  15727. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15728. re (nth 1 (assoc "$" org-latex-regexps)))
  15729. (while (string-match re str start)
  15730. (cond
  15731. ((= (match-end 0) (length str))
  15732. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15733. ((= (match-end 0) (- (length str) 5))
  15734. (throw 'exit nil))
  15735. (t (setq start (match-end 0))))))
  15736. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15737. (goto-char pos)
  15738. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15739. (and (match-beginning 2) (throw 'exit nil))
  15740. ;; count $$
  15741. (while (re-search-backward "\\$\\$" lim t)
  15742. (setq dd-on (not dd-on)))
  15743. (goto-char pos)
  15744. (if dd-on (cons "$$" m))))))
  15745. (defun org-inside-latex-macro-p ()
  15746. "Is point inside a LaTeX macro or its arguments?"
  15747. (save-match-data
  15748. (org-in-regexp
  15749. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15750. (defun org-try-cdlatex-tab ()
  15751. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15752. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15753. - inside a LaTeX fragment, or
  15754. - after the first word in a line, where an abbreviation expansion could
  15755. insert a LaTeX environment."
  15756. (when org-cdlatex-mode
  15757. (cond
  15758. ;; Before any word on the line: No expansion possible.
  15759. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15760. ;; Just after first word on the line: Expand it. Make sure it
  15761. ;; cannot happen on headlines, though.
  15762. ((save-excursion
  15763. (skip-chars-backward "a-zA-Z0-9*")
  15764. (skip-chars-backward " \t")
  15765. (and (bolp) (not (org-at-heading-p))))
  15766. (cdlatex-tab) t)
  15767. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15768. (defun org-cdlatex-underscore-caret (&optional arg)
  15769. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15770. Revert to the normal definition outside of these fragments."
  15771. (interactive "P")
  15772. (if (org-inside-LaTeX-fragment-p)
  15773. (call-interactively 'cdlatex-sub-superscript)
  15774. (let (org-cdlatex-mode)
  15775. (call-interactively (key-binding (vector last-input-event))))))
  15776. (defun org-cdlatex-math-modify (&optional arg)
  15777. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15778. Revert to the normal definition outside of these fragments."
  15779. (interactive "P")
  15780. (if (org-inside-LaTeX-fragment-p)
  15781. (call-interactively 'cdlatex-math-modify)
  15782. (let (org-cdlatex-mode)
  15783. (call-interactively (key-binding (vector last-input-event))))))
  15784. (defvar org-latex-fragment-image-overlays nil
  15785. "List of overlays carrying the images of latex fragments.")
  15786. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15787. (defun org-remove-latex-fragment-image-overlays ()
  15788. "Remove all overlays with LaTeX fragment images in current buffer."
  15789. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15790. (setq org-latex-fragment-image-overlays nil))
  15791. (defun org-preview-latex-fragment (&optional subtree)
  15792. "Preview the LaTeX fragment at point, or all locally or globally.
  15793. If the cursor is in a LaTeX fragment, create the image and overlay
  15794. it over the source code. If there is no fragment at point, display
  15795. all fragments in the current text, from one headline to the next. With
  15796. prefix SUBTREE, display all fragments in the current subtree. With a
  15797. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15798. the cursor is before the first headline,
  15799. display all fragments in the buffer.
  15800. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15801. (interactive "P")
  15802. (unless buffer-file-name
  15803. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15804. (org-remove-latex-fragment-image-overlays)
  15805. (save-excursion
  15806. (save-restriction
  15807. (let (beg end at msg)
  15808. (cond
  15809. ((or (equal subtree '(16))
  15810. (not (save-excursion
  15811. (re-search-backward org-outline-regexp-bol nil t))))
  15812. (setq beg (point-min) end (point-max)
  15813. msg "Creating images for buffer...%s"))
  15814. ((equal subtree '(4))
  15815. (org-back-to-heading)
  15816. (setq beg (point) end (org-end-of-subtree t)
  15817. msg "Creating images for subtree...%s"))
  15818. (t
  15819. (if (setq at (org-inside-LaTeX-fragment-p))
  15820. (goto-char (max (point-min) (- (cdr at) 2)))
  15821. (org-back-to-heading))
  15822. (setq beg (point) end (progn (outline-next-heading) (point))
  15823. msg (if at "Creating image...%s"
  15824. "Creating images for entry...%s"))))
  15825. (message msg "")
  15826. (narrow-to-region beg end)
  15827. (goto-char beg)
  15828. (org-format-latex
  15829. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15830. (file-name-nondirectory
  15831. buffer-file-name)))
  15832. default-directory 'overlays msg at 'forbuffer
  15833. org-latex-create-formula-image-program)
  15834. (message msg "done. Use `C-c C-c' to remove images.")))))
  15835. (defvar org-latex-regexps
  15836. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15837. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15838. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15839. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15840. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15841. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15842. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15843. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15844. "Regular expressions for matching embedded LaTeX.")
  15845. (defvar org-export-have-math nil) ;; dynamic scoping
  15846. (defun org-format-latex (prefix &optional dir overlays msg at
  15847. forbuffer processing-type)
  15848. "Replace LaTeX fragments with links to an image, and produce images.
  15849. Some of the options can be changed using the variable
  15850. `org-format-latex-options'."
  15851. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15852. (let* ((prefixnodir (file-name-nondirectory prefix))
  15853. (absprefix (expand-file-name prefix dir))
  15854. (todir (file-name-directory absprefix))
  15855. (opt org-format-latex-options)
  15856. (optnew org-format-latex-options)
  15857. (matchers (plist-get opt :matchers))
  15858. (re-list org-latex-regexps)
  15859. (org-format-latex-header-extra
  15860. (plist-get (org-infile-export-plist) :latex-header-extra))
  15861. (cnt 0) txt hash link beg end re e checkdir
  15862. string
  15863. m n block-type block linkfile movefile ov)
  15864. ;; Check the different regular expressions
  15865. (while (setq e (pop re-list))
  15866. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15867. block (if block-type "\n\n" ""))
  15868. (when (member m matchers)
  15869. (goto-char (point-min))
  15870. (while (re-search-forward re nil t)
  15871. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15872. (not (get-text-property (match-beginning n)
  15873. 'org-protected))
  15874. (or (not overlays)
  15875. (not (eq (get-char-property (match-beginning n)
  15876. 'org-overlay-type)
  15877. 'org-latex-overlay))))
  15878. (setq org-export-have-math t)
  15879. (cond
  15880. ((eq processing-type 'verbatim)
  15881. ;; Leave the text verbatim, just protect it
  15882. (add-text-properties (match-beginning n) (match-end n)
  15883. '(org-protected t)))
  15884. ((eq processing-type 'mathjax)
  15885. ;; Prepare for MathJax processing
  15886. (setq string (match-string n))
  15887. (if (member m '("$" "$1"))
  15888. (save-excursion
  15889. (delete-region (match-beginning n) (match-end n))
  15890. (goto-char (match-beginning n))
  15891. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15892. "\\)")
  15893. '(org-protected t))))
  15894. (add-text-properties (match-beginning n) (match-end n)
  15895. '(org-protected t))))
  15896. ((or (eq processing-type 'dvipng)
  15897. (eq processing-type 'imagemagick))
  15898. ;; Process to an image
  15899. (setq txt (match-string n)
  15900. beg (match-beginning n) end (match-end n)
  15901. cnt (1+ cnt))
  15902. (let ((face (face-at-point))
  15903. (fg (plist-get opt :foreground))
  15904. (bg (plist-get opt :background))
  15905. print-length print-level) ; make sure full list is printed
  15906. (when forbuffer
  15907. ; Get the colors from the face at point
  15908. (goto-char beg)
  15909. (when (eq fg 'auto)
  15910. (setq fg (face-attribute face :foreground nil 'default)))
  15911. (when (eq bg 'auto)
  15912. (setq bg (face-attribute face :background nil 'default)))
  15913. (setq optnew (copy-sequence opt))
  15914. (plist-put optnew :foreground fg)
  15915. (plist-put optnew :background bg))
  15916. (setq hash (sha1 (prin1-to-string
  15917. (list org-format-latex-header
  15918. org-format-latex-header-extra
  15919. org-export-latex-default-packages-alist
  15920. org-export-latex-packages-alist
  15921. org-format-latex-options
  15922. forbuffer txt fg bg)))
  15923. linkfile (format "%s_%s.png" prefix hash)
  15924. movefile (format "%s_%s.png" absprefix hash)))
  15925. (setq link (concat block "[[file:" linkfile "]]" block))
  15926. (if msg (message msg cnt))
  15927. (goto-char beg)
  15928. (unless checkdir ; make sure the directory exists
  15929. (setq checkdir t)
  15930. (or (file-directory-p todir) (make-directory todir t)))
  15931. (org-create-formula-image
  15932. txt movefile optnew forbuffer processing-type)
  15933. (if overlays
  15934. (progn
  15935. (mapc (lambda (o)
  15936. (if (eq (overlay-get o 'org-overlay-type)
  15937. 'org-latex-overlay)
  15938. (delete-overlay o)))
  15939. (overlays-in beg end))
  15940. (setq ov (make-overlay beg end))
  15941. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15942. (if (featurep 'xemacs)
  15943. (progn
  15944. (overlay-put ov 'invisible t)
  15945. (overlay-put
  15946. ov 'end-glyph
  15947. (make-glyph (vector 'png :file movefile))))
  15948. (overlay-put
  15949. ov 'display
  15950. (list 'image :type 'png :file movefile :ascent 'center)))
  15951. (push ov org-latex-fragment-image-overlays)
  15952. (goto-char end))
  15953. (delete-region beg end)
  15954. (insert (org-add-props link
  15955. (list 'org-latex-src
  15956. (replace-regexp-in-string
  15957. "\"" "" txt)
  15958. 'org-latex-src-embed-type
  15959. (if block-type 'paragraph 'character))))))
  15960. ((eq processing-type 'mathml)
  15961. ;; Process to MathML
  15962. (unless (save-match-data (org-format-latex-mathml-available-p))
  15963. (error "LaTeX to MathML converter not configured"))
  15964. (setq txt (match-string n)
  15965. beg (match-beginning n) end (match-end n)
  15966. cnt (1+ cnt))
  15967. (if msg (message msg cnt))
  15968. (goto-char beg)
  15969. (delete-region beg end)
  15970. (insert (org-format-latex-as-mathml
  15971. txt block-type prefix dir)))
  15972. (t
  15973. (error "Unknown conversion type %s for latex fragments"
  15974. processing-type)))))))))
  15975. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15976. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15977. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15978. sucessful, return the portion between \"<math...> </math>\"
  15979. elements otherwise return nil. When MATHML-FILE is specified,
  15980. write the results in to that file. When invoked as an
  15981. interactive command, prompt for LATEX-FRAG, with initial value
  15982. set to the current active region and echo the results for user
  15983. inspection."
  15984. (interactive (list (let ((frag (when (org-region-active-p)
  15985. (buffer-substring-no-properties
  15986. (region-beginning) (region-end)))))
  15987. (read-string "LaTeX Fragment: " frag nil frag))))
  15988. (unless latex-frag (error "Invalid latex-frag"))
  15989. (let* ((tmp-in-file (file-relative-name
  15990. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15991. (ignore (write-region latex-frag nil tmp-in-file))
  15992. (tmp-out-file (file-relative-name
  15993. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15994. (cmd (format-spec
  15995. org-latex-to-mathml-convert-command
  15996. `((?j . ,(shell-quote-argument
  15997. (expand-file-name org-latex-to-mathml-jar-file)))
  15998. (?I . ,(shell-quote-argument tmp-in-file))
  15999. (?o . ,(shell-quote-argument tmp-out-file)))))
  16000. mathml shell-command-output)
  16001. (when (org-called-interactively-p 'any)
  16002. (unless (org-format-latex-mathml-available-p)
  16003. (error "LaTeX to MathML converter not configured")))
  16004. (message "Running %s" cmd)
  16005. (setq shell-command-output (shell-command-to-string cmd))
  16006. (setq mathml
  16007. (when (file-readable-p tmp-out-file)
  16008. (with-current-buffer (find-file-noselect tmp-out-file t)
  16009. (goto-char (point-min))
  16010. (when (re-search-forward
  16011. (concat
  16012. (regexp-quote
  16013. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16014. "\\(.\\|\n\\)*"
  16015. (regexp-quote "</math>")) nil t)
  16016. (prog1 (match-string 0) (kill-buffer))))))
  16017. (cond
  16018. (mathml
  16019. (setq mathml
  16020. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16021. (when mathml-file
  16022. (write-region mathml nil mathml-file))
  16023. (when (org-called-interactively-p 'any)
  16024. (message mathml)))
  16025. ((message "LaTeX to MathML conversion failed")
  16026. (message shell-command-output)))
  16027. (delete-file tmp-in-file)
  16028. (when (file-exists-p tmp-out-file)
  16029. (delete-file tmp-out-file))
  16030. mathml))
  16031. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16032. prefix &optional dir)
  16033. "Use `org-create-math-formula' but check local cache first."
  16034. (let* ((absprefix (expand-file-name prefix dir))
  16035. (print-length nil) (print-level nil)
  16036. (formula-id (concat
  16037. "formula-"
  16038. (sha1
  16039. (prin1-to-string
  16040. (list latex-frag
  16041. org-latex-to-mathml-convert-command)))))
  16042. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16043. (formula-cache-dir (file-name-directory formula-cache)))
  16044. (unless (file-directory-p formula-cache-dir)
  16045. (make-directory formula-cache-dir t))
  16046. (unless (file-exists-p formula-cache)
  16047. (org-create-math-formula latex-frag formula-cache))
  16048. (if (file-exists-p formula-cache)
  16049. ;; Successful conversion. Return the link to MathML file.
  16050. (org-add-props
  16051. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16052. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16053. 'org-latex-src-embed-type (if latex-frag-type
  16054. 'paragraph 'character)))
  16055. ;; Failed conversion. Return the LaTeX fragment verbatim
  16056. (add-text-properties
  16057. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16058. latex-frag)))
  16059. (defun org-create-formula-image (string tofile options buffer &optional type)
  16060. "Create an image from LaTeX source using dvipng or convert.
  16061. This function calls either `org-create-formula-image-with-dvipng'
  16062. or `org-create-formula-image-with-imagemagick' depending on the
  16063. value of `org-latex-create-formula-image-program' or on the value
  16064. of the optional TYPE variable.
  16065. Note: ultimately these two function should be combined as they
  16066. share a good deal of logic."
  16067. (org-check-external-command
  16068. "latex" "needed to convert LaTeX fragments to images")
  16069. (funcall
  16070. (case (or type org-latex-create-formula-image-program)
  16071. ('dvipng
  16072. (org-check-external-command
  16073. "dvipng" "needed to convert LaTeX fragments to images")
  16074. #'org-create-formula-image-with-dvipng)
  16075. ('imagemagick
  16076. (org-check-external-command
  16077. "convert" "you need to install imagemagick")
  16078. #'org-create-formula-image-with-imagemagick)
  16079. (t (error
  16080. "invalid value of `org-latex-create-formula-image-program'")))
  16081. string tofile options buffer))
  16082. ;; This function borrows from Ganesh Swami's latex2png.el
  16083. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16084. "This calls dvipng."
  16085. (require 'org-latex)
  16086. (let* ((tmpdir (if (featurep 'xemacs)
  16087. (temp-directory)
  16088. temporary-file-directory))
  16089. (texfilebase (make-temp-name
  16090. (expand-file-name "orgtex" tmpdir)))
  16091. (texfile (concat texfilebase ".tex"))
  16092. (dvifile (concat texfilebase ".dvi"))
  16093. (pngfile (concat texfilebase ".png"))
  16094. (fnh (if (featurep 'xemacs)
  16095. (font-height (face-font 'default))
  16096. (face-attribute 'default :height nil)))
  16097. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16098. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16099. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16100. "Black"))
  16101. (bg (or (plist-get options (if buffer :background :html-background))
  16102. "Transparent")))
  16103. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16104. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16105. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16106. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16107. (with-temp-file texfile
  16108. (insert (org-splice-latex-header
  16109. org-format-latex-header
  16110. org-export-latex-default-packages-alist
  16111. org-export-latex-packages-alist t
  16112. org-format-latex-header-extra))
  16113. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16114. (require 'org-latex)
  16115. (org-export-latex-fix-inputenc))
  16116. (let ((dir default-directory))
  16117. (condition-case nil
  16118. (progn
  16119. (cd tmpdir)
  16120. (call-process "latex" nil nil nil texfile))
  16121. (error nil))
  16122. (cd dir))
  16123. (if (not (file-exists-p dvifile))
  16124. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16125. (condition-case nil
  16126. (if (featurep 'xemacs)
  16127. (call-process "dvipng" nil nil nil
  16128. "-fg" fg "-bg" bg
  16129. "-T" "tight"
  16130. "-o" pngfile
  16131. dvifile)
  16132. (call-process "dvipng" nil nil nil
  16133. "-fg" fg "-bg" bg
  16134. "-D" dpi
  16135. ;;"-x" scale "-y" scale
  16136. "-T" "tight"
  16137. "-o" pngfile
  16138. dvifile))
  16139. (error nil))
  16140. (if (not (file-exists-p pngfile))
  16141. (if org-format-latex-signal-error
  16142. (error "Failed to create png file from %s" texfile)
  16143. (message "Failed to create png file from %s" texfile)
  16144. nil)
  16145. ;; Use the requested file name and clean up
  16146. (copy-file pngfile tofile 'replace)
  16147. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16148. (if (file-exists-p (concat texfilebase e))
  16149. (delete-file (concat texfilebase e))))
  16150. pngfile))))
  16151. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16152. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16153. "This calls convert, which is included into imagemagick."
  16154. (require 'org-latex)
  16155. (let* ((tmpdir (if (featurep 'xemacs)
  16156. (temp-directory)
  16157. temporary-file-directory))
  16158. (texfilebase (make-temp-name
  16159. (expand-file-name "orgtex" tmpdir)))
  16160. (texfile (concat texfilebase ".tex"))
  16161. (pdffile (concat texfilebase ".pdf"))
  16162. (pngfile (concat texfilebase ".png"))
  16163. (fnh (if (featurep 'xemacs)
  16164. (font-height (face-font 'default))
  16165. (face-attribute 'default :height nil)))
  16166. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16167. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16168. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16169. "black"))
  16170. (bg (or (plist-get options (if buffer :background :html-background))
  16171. "white")))
  16172. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16173. (setq fg (org-latex-color-format fg)))
  16174. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16175. (setq bg (org-latex-color-format
  16176. (if (string= bg "Transparent") "white" bg))))
  16177. (with-temp-file texfile
  16178. (insert (org-splice-latex-header
  16179. org-format-latex-header
  16180. org-export-latex-default-packages-alist
  16181. org-export-latex-packages-alist t
  16182. org-format-latex-header-extra))
  16183. (insert "\n\\begin{document}\n"
  16184. "\\definecolor{fg}{rgb}{" fg "}\n"
  16185. "\\definecolor{bg}{rgb}{" bg "}\n"
  16186. "\n\\pagecolor{bg}\n"
  16187. "\n{\\color{fg}\n"
  16188. string
  16189. "\n}\n"
  16190. "\n\\end{document}\n" )
  16191. (require 'org-latex)
  16192. (org-export-latex-fix-inputenc))
  16193. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16194. (condition-case nil
  16195. (progn
  16196. (cd tmpdir)
  16197. (setq cmds org-latex-to-pdf-process)
  16198. (while cmds
  16199. (setq latex-frags-cmds (pop cmds))
  16200. (if (listp latex-frags-cmds)
  16201. (setq cmds nil)
  16202. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16203. (while latex-frags-cmds
  16204. (setq cmd (pop latex-frags-cmds))
  16205. (while (string-match "%b" cmd)
  16206. (setq cmd (replace-match
  16207. (save-match-data
  16208. (shell-quote-argument texfile))
  16209. t t cmd)))
  16210. (while (string-match "%f" cmd)
  16211. (setq cmd (replace-match
  16212. (save-match-data
  16213. (shell-quote-argument (file-name-nondirectory texfile)))
  16214. t t cmd)))
  16215. (while (string-match "%o" cmd)
  16216. (setq cmd (replace-match
  16217. (save-match-data
  16218. (shell-quote-argument (file-name-directory texfile)))
  16219. t t cmd)))
  16220. (setq cmd (split-string cmd))
  16221. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16222. (error nil))
  16223. (cd dir))
  16224. (if (not (file-exists-p pdffile))
  16225. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16226. (condition-case nil
  16227. (if (featurep 'xemacs)
  16228. (call-process "convert" nil nil nil
  16229. "-density" "96"
  16230. "-trim"
  16231. "-antialias"
  16232. pdffile
  16233. "-quality" "100"
  16234. ;; "-sharpen" "0x1.0"
  16235. pngfile)
  16236. (call-process "convert" nil nil nil
  16237. "-density" dpi
  16238. "-trim"
  16239. "-antialias"
  16240. pdffile
  16241. "-quality" "100"
  16242. ; "-sharpen" "0x1.0"
  16243. pngfile))
  16244. (error nil))
  16245. (if (not (file-exists-p pngfile))
  16246. (if org-format-latex-signal-error
  16247. (error "Failed to create png file from %s" texfile)
  16248. (message "Failed to create png file from %s" texfile)
  16249. nil)
  16250. ;; Use the requested file name and clean up
  16251. (copy-file pngfile tofile 'replace)
  16252. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16253. (if (file-exists-p (concat texfilebase e))
  16254. (delete-file (concat texfilebase e))))
  16255. pngfile))))
  16256. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16257. "Fill a LaTeX header template TPL.
  16258. In the template, the following place holders will be recognized:
  16259. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16260. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16261. [PACKAGES] \\usepackage statements for PKG
  16262. [NO-PACKAGES] do not include PKG
  16263. [EXTRA] the string EXTRA
  16264. [NO-EXTRA] do not include EXTRA
  16265. For backward compatibility, if both the positive and the negative place
  16266. holder is missing, the positive one (without the \"NO-\") will be
  16267. assumed to be present at the end of the template.
  16268. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16269. EXTRA is a string.
  16270. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16271. (let (rpl (end ""))
  16272. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16273. (setq rpl (if (or (match-end 1) (not def-pkg))
  16274. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16275. tpl (replace-match rpl t t tpl))
  16276. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16277. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16278. (setq rpl (if (or (match-end 1) (not pkg))
  16279. "" (org-latex-packages-to-string pkg snippets-p t))
  16280. tpl (replace-match rpl t t tpl))
  16281. (if pkg (setq end
  16282. (concat end "\n"
  16283. (org-latex-packages-to-string pkg snippets-p)))))
  16284. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16285. (setq rpl (if (or (match-end 1) (not extra))
  16286. "" (concat extra "\n"))
  16287. tpl (replace-match rpl t t tpl))
  16288. (if (and extra (string-match "\\S-" extra))
  16289. (setq end (concat end "\n" extra))))
  16290. (if (string-match "\\S-" end)
  16291. (concat tpl "\n" end)
  16292. tpl)))
  16293. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16294. "Turn an alist of packages into a string with the \\usepackage macros."
  16295. (setq pkg (mapconcat (lambda(p)
  16296. (cond
  16297. ((stringp p) p)
  16298. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16299. (format "%% Package %s omitted" (cadr p)))
  16300. ((equal "" (car p))
  16301. (format "\\usepackage{%s}" (cadr p)))
  16302. (t
  16303. (format "\\usepackage[%s]{%s}"
  16304. (car p) (cadr p)))))
  16305. pkg
  16306. "\n"))
  16307. (if newline (concat pkg "\n") pkg))
  16308. (defun org-dvipng-color (attr)
  16309. "Return a RGB color specification for dvipng."
  16310. (apply 'format "rgb %s %s %s"
  16311. (mapcar 'org-normalize-color
  16312. (if (featurep 'xemacs)
  16313. (color-rgb-components
  16314. (face-property 'default
  16315. (cond ((eq attr :foreground) 'foreground)
  16316. ((eq attr :background) 'background))))
  16317. (color-values (face-attribute 'default attr nil))))))
  16318. (defun org-dvipng-color-format (color-name)
  16319. "Convert COLOR-NAME to a RGB color value for dvipng."
  16320. (apply 'format "rgb %s %s %s"
  16321. (mapcar 'org-normalize-color
  16322. (color-values color-name))))
  16323. (defun org-latex-color (attr)
  16324. "Return a RGB color for the LaTeX color package."
  16325. (apply 'format "%s,%s,%s"
  16326. (mapcar 'org-normalize-color
  16327. (if (featurep 'xemacs)
  16328. (color-rgb-components
  16329. (face-property 'default
  16330. (cond ((eq attr :foreground) 'foreground)
  16331. ((eq attr :background) 'background))))
  16332. (color-values (face-attribute 'default attr nil))))))
  16333. (defun org-latex-color-format (color-name)
  16334. "Convert COLOR-NAME to a RGB color value."
  16335. (apply 'format "%s,%s,%s"
  16336. (mapcar 'org-normalize-color
  16337. (color-values color-name))))
  16338. (defun org-normalize-color (value)
  16339. "Return string to be used as color value for an RGB component."
  16340. (format "%g" (/ value 65535.0)))
  16341. ;; Image display
  16342. (defvar org-inline-image-overlays nil)
  16343. (make-variable-buffer-local 'org-inline-image-overlays)
  16344. (defun org-toggle-inline-images (&optional include-linked)
  16345. "Toggle the display of inline images.
  16346. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16347. (interactive "P")
  16348. (if org-inline-image-overlays
  16349. (progn
  16350. (org-remove-inline-images)
  16351. (message "Inline image display turned off"))
  16352. (org-display-inline-images include-linked)
  16353. (if (and (org-called-interactively-p)
  16354. org-inline-image-overlays)
  16355. (message "%d images displayed inline"
  16356. (length org-inline-image-overlays))
  16357. (message "No images to display inline"))))
  16358. (defun org-redisplay-inline-images ()
  16359. "Refresh the display of inline images."
  16360. (interactive)
  16361. (if (not org-inline-image-overlays)
  16362. (org-toggle-inline-images)
  16363. (org-toggle-inline-images)
  16364. (org-toggle-inline-images)))
  16365. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16366. "Display inline images.
  16367. Normally only links without a description part are inlined, because this
  16368. is how it will work for export. When INCLUDE-LINKED is set, also links
  16369. with a description part will be inlined. This can be nice for a quick
  16370. look at those images, but it does not reflect what exported files will look
  16371. like.
  16372. When REFRESH is set, refresh existing images between BEG and END.
  16373. This will create new image displays only if necessary.
  16374. BEG and END default to the buffer boundaries."
  16375. (interactive "P")
  16376. (unless refresh
  16377. (org-remove-inline-images)
  16378. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16379. (save-excursion
  16380. (save-restriction
  16381. (widen)
  16382. (setq beg (or beg (point-min)) end (or end (point-max)))
  16383. (goto-char beg)
  16384. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16385. (substring (org-image-file-name-regexp) 0 -2)
  16386. "\\)\\]" (if include-linked "" "\\]")))
  16387. old file ov img type attrwidth width)
  16388. (while (re-search-forward re end t)
  16389. (setq old (get-char-property-and-overlay (match-beginning 1)
  16390. 'org-image-overlay)
  16391. file (expand-file-name
  16392. (concat (or (match-string 3) "") (match-string 4))))
  16393. (when (image-type-available-p 'imagemagick)
  16394. (setq attrwidth (if (or (listp org-image-actual-width)
  16395. (null org-image-actual-width))
  16396. (save-excursion
  16397. (save-match-data
  16398. (when (re-search-backward
  16399. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16400. (save-excursion
  16401. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16402. (string-to-number (match-string 1))))))
  16403. width (cond ((eq org-image-actual-width t) nil)
  16404. ((null org-image-actual-width) attrwidth)
  16405. ((numberp org-image-actual-width)
  16406. org-image-actual-width)
  16407. ((listp org-image-actual-width)
  16408. (or attrwidth (car org-image-actual-width))))
  16409. type (if width 'imagemagick)))
  16410. (when (file-exists-p file)
  16411. (if (and (car-safe old) refresh)
  16412. (image-refresh (overlay-get (cdr old) 'display))
  16413. (setq img (save-match-data (create-image file type nil :width width)))
  16414. (when img
  16415. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16416. (overlay-put ov 'display img)
  16417. (overlay-put ov 'face 'default)
  16418. (overlay-put ov 'org-image-overlay t)
  16419. (overlay-put ov 'modification-hooks
  16420. (list 'org-display-inline-remove-overlay))
  16421. (push ov org-inline-image-overlays)))))))))
  16422. (define-obsolete-function-alias
  16423. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16424. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16425. "Remove inline-display overlay if a corresponding region is modified."
  16426. (let ((inhibit-modification-hooks t))
  16427. (when (and ov after)
  16428. (delete ov org-inline-image-overlays)
  16429. (delete-overlay ov))))
  16430. (defun org-remove-inline-images ()
  16431. "Remove inline display of images."
  16432. (interactive)
  16433. (mapc 'delete-overlay org-inline-image-overlays)
  16434. (setq org-inline-image-overlays nil))
  16435. ;;;; Key bindings
  16436. ;; Outline functions from `outline-mode-prefix-map'
  16437. ;; that can be remapped in Org:
  16438. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16439. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16440. (define-key org-mode-map [remap outline-forward-same-level]
  16441. 'org-forward-heading-same-level)
  16442. (define-key org-mode-map [remap outline-backward-same-level]
  16443. 'org-backward-heading-same-level)
  16444. (define-key org-mode-map [remap show-branches]
  16445. 'org-kill-note-or-show-branches)
  16446. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16447. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16448. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16449. ;; Outline functions from `outline-mode-prefix-map' that can not
  16450. ;; be remapped in Org:
  16451. ;;
  16452. ;; - the column "key binding" shows whether the Outline function is still
  16453. ;; available in Org mode on the same key that it has been bound to in
  16454. ;; Outline mode:
  16455. ;; - "overridden": key used for a different functionality in Org mode
  16456. ;; - else: key still bound to the same Outline function in Org mode
  16457. ;;
  16458. ;; | Outline function | key binding | Org replacement |
  16459. ;; |------------------------------------+-------------+-----------------------|
  16460. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16461. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16462. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16463. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16464. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16465. ;; | `show-entry' | overridden | no replacement |
  16466. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16467. ;; | `show-branches' | `C-c C-k' | still same function |
  16468. ;; | `show-subtree' | overridden | visibility cycling |
  16469. ;; | `show-all' | overridden | no replacement |
  16470. ;; | `hide-subtree' | overridden | visibility cycling |
  16471. ;; | `hide-body' | overridden | no replacement |
  16472. ;; | `hide-entry' | overridden | visibility cycling |
  16473. ;; | `hide-leaves' | overridden | no replacement |
  16474. ;; | `hide-sublevels' | overridden | no replacement |
  16475. ;; | `hide-other' | overridden | no replacement |
  16476. ;; Make `C-c C-x' a prefix key
  16477. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16478. ;; TAB key with modifiers
  16479. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16480. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16481. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16482. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16483. ;; The following line is necessary under Suse GNU/Linux
  16484. (unless (featurep 'xemacs)
  16485. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16486. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16487. (define-key org-mode-map [backtab] 'org-shifttab)
  16488. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16489. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16490. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16491. ;; Cursor keys with modifiers
  16492. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16493. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16494. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16495. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16496. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16497. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16498. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16499. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16500. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16501. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16502. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16503. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16504. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16505. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16506. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16507. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16508. ;; Babel keys
  16509. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16510. (mapc (lambda (pair)
  16511. (define-key org-babel-map (car pair) (cdr pair)))
  16512. org-babel-key-bindings)
  16513. ;;; Extra keys for tty access.
  16514. ;; We only set them when really needed because otherwise the
  16515. ;; menus don't show the simple keys
  16516. (when (or org-use-extra-keys
  16517. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16518. (not window-system))
  16519. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16520. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16521. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16522. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16523. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16524. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16525. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16526. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16527. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16528. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16529. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16530. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16531. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16532. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16533. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16534. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16535. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16536. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16537. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16538. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16539. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16540. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16541. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16542. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16543. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16544. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16545. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16546. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16547. ;; All the other keys
  16548. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16549. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16550. (if (boundp 'narrow-map)
  16551. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16552. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16553. (if (boundp 'narrow-map)
  16554. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16555. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16556. (if (boundp 'narrow-map)
  16557. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16558. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16559. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16560. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16561. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16562. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16563. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16564. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16565. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16566. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16567. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16568. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16569. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16570. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16571. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16572. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16573. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16574. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16575. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16576. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16577. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16578. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16579. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16580. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16581. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16582. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16583. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16584. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16585. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16586. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16587. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16588. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16589. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16590. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16591. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16592. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16593. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16594. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16595. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16596. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16597. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16598. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16599. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16600. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16601. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16602. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16603. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16604. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16605. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16606. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16607. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16608. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16609. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16610. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16611. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16612. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16613. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16614. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16615. (org-defkey org-mode-map "\C-m" 'org-return)
  16616. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16617. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16618. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16619. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16620. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16621. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16622. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16623. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16624. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16625. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16626. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16627. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16628. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16629. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16630. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16631. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16632. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16633. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16634. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16635. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16636. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16637. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16638. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16639. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16640. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16641. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16642. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16643. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16644. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16645. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16646. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16647. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16648. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16649. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16650. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16651. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16652. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16653. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16654. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16655. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16656. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16657. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16658. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16659. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16660. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16661. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16662. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16663. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16664. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16665. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16666. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16667. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16668. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16669. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16670. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16671. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16672. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16673. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16674. (when (featurep 'xemacs)
  16675. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16676. (defconst org-speed-commands-default
  16677. '(
  16678. ("Outline Navigation")
  16679. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16680. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16681. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16682. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16683. ("u" . (org-speed-move-safe 'outline-up-heading))
  16684. ("j" . org-goto)
  16685. ("g" . (org-refile t))
  16686. ("Outline Visibility")
  16687. ("c" . org-cycle)
  16688. ("C" . org-shifttab)
  16689. (" " . org-display-outline-path)
  16690. (":" . org-columns)
  16691. ("Outline Structure Editing")
  16692. ("U" . org-shiftmetaup)
  16693. ("D" . org-shiftmetadown)
  16694. ("r" . org-metaright)
  16695. ("l" . org-metaleft)
  16696. ("R" . org-shiftmetaright)
  16697. ("L" . org-shiftmetaleft)
  16698. ("i" . (progn (forward-char 1) (call-interactively
  16699. 'org-insert-heading-respect-content)))
  16700. ("^" . org-sort)
  16701. ("w" . org-refile)
  16702. ("a" . org-archive-subtree-default-with-confirmation)
  16703. ("." . org-mark-subtree)
  16704. ("#" . org-toggle-comment)
  16705. ("Clock Commands")
  16706. ("I" . org-clock-in)
  16707. ("O" . org-clock-out)
  16708. ("Meta Data Editing")
  16709. ("t" . org-todo)
  16710. ("," . (org-priority))
  16711. ("0" . (org-priority ?\ ))
  16712. ("1" . (org-priority ?A))
  16713. ("2" . (org-priority ?B))
  16714. ("3" . (org-priority ?C))
  16715. (";" . org-set-tags-command)
  16716. ("e" . org-set-effort)
  16717. ("E" . org-inc-effort)
  16718. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16719. (org-entry-put (point) "APPT_WARNTIME" m)))
  16720. ("Agenda Views etc")
  16721. ("v" . org-agenda)
  16722. ("/" . org-sparse-tree)
  16723. ("Misc")
  16724. ("o" . org-open-at-point)
  16725. ("?" . org-speed-command-help)
  16726. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16727. (">" . (org-agenda-remove-restriction-lock))
  16728. )
  16729. "The default speed commands.")
  16730. (defun org-print-speed-command (e)
  16731. (if (> (length (car e)) 1)
  16732. (progn
  16733. (princ "\n")
  16734. (princ (car e))
  16735. (princ "\n")
  16736. (princ (make-string (length (car e)) ?-))
  16737. (princ "\n"))
  16738. (princ (car e))
  16739. (princ " ")
  16740. (if (symbolp (cdr e))
  16741. (princ (symbol-name (cdr e)))
  16742. (prin1 (cdr e)))
  16743. (princ "\n")))
  16744. (defun org-speed-command-help ()
  16745. "Show the available speed commands."
  16746. (interactive)
  16747. (if (not org-use-speed-commands)
  16748. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16749. (with-output-to-temp-buffer "*Help*"
  16750. (princ "User-defined Speed commands\n===========================\n")
  16751. (mapc 'org-print-speed-command org-speed-commands-user)
  16752. (princ "\n")
  16753. (princ "Built-in Speed commands\n=======================\n")
  16754. (mapc 'org-print-speed-command org-speed-commands-default))
  16755. (with-current-buffer "*Help*"
  16756. (setq truncate-lines t))))
  16757. (defun org-speed-move-safe (cmd)
  16758. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16759. If not, return to the original position and throw an error."
  16760. (interactive)
  16761. (let ((pos (point)))
  16762. (call-interactively cmd)
  16763. (unless (and (bolp) (org-at-heading-p))
  16764. (goto-char pos)
  16765. (error "Boundary reached while executing %s" cmd))))
  16766. (defvar org-self-insert-command-undo-counter 0)
  16767. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16768. (defvar org-speed-command nil)
  16769. (define-obsolete-function-alias
  16770. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  16771. (defun org-speed-command-activate (keys)
  16772. "Hook for activating single-letter speed commands.
  16773. `org-speed-commands-default' specifies a minimal command set.
  16774. Use `org-speed-commands-user' for further customization."
  16775. (when (or (and (bolp) (looking-at org-outline-regexp))
  16776. (and (functionp org-use-speed-commands)
  16777. (funcall org-use-speed-commands)))
  16778. (cdr (assoc keys (append org-speed-commands-user
  16779. org-speed-commands-default)))))
  16780. (define-obsolete-function-alias
  16781. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  16782. (defun org-babel-speed-command-activate (keys)
  16783. "Hook for activating single-letter code block commands."
  16784. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16785. (cdr (assoc keys org-babel-key-bindings))))
  16786. (defcustom org-speed-command-hook
  16787. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16788. "Hook for activating speed commands at strategic locations.
  16789. Hook functions are called in sequence until a valid handler is
  16790. found.
  16791. Each hook takes a single argument, a user-pressed command key
  16792. which is also a `self-insert-command' from the global map.
  16793. Within the hook, examine the cursor position and the command key
  16794. and return nil or a valid handler as appropriate. Handler could
  16795. be one of an interactive command, a function, or a form.
  16796. Set `org-use-speed-commands' to non-nil value to enable this
  16797. hook. The default setting is `org-speed-command-activate'."
  16798. :group 'org-structure
  16799. :version "24.1"
  16800. :type 'hook)
  16801. (defun org-self-insert-command (N)
  16802. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16803. If the cursor is in a table looking at whitespace, the whitespace is
  16804. overwritten, and the table is not marked as requiring realignment."
  16805. (interactive "p")
  16806. (org-check-before-invisible-edit 'insert)
  16807. (cond
  16808. ((and org-use-speed-commands
  16809. (setq org-speed-command
  16810. (run-hook-with-args-until-success
  16811. 'org-speed-command-hook (this-command-keys))))
  16812. (cond
  16813. ((commandp org-speed-command)
  16814. (setq this-command org-speed-command)
  16815. (call-interactively org-speed-command))
  16816. ((functionp org-speed-command)
  16817. (funcall org-speed-command))
  16818. ((and org-speed-command (listp org-speed-command))
  16819. (eval org-speed-command))
  16820. (t (let (org-use-speed-commands)
  16821. (call-interactively 'org-self-insert-command)))))
  16822. ((and
  16823. (org-table-p)
  16824. (progn
  16825. ;; check if we blank the field, and if that triggers align
  16826. (and (featurep 'org-table) org-table-auto-blank-field
  16827. (member last-command
  16828. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16829. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16830. ;; got extra space, this field does not determine column width
  16831. (let (org-table-may-need-update) (org-table-blank-field))
  16832. ;; no extra space, this field may determine column width
  16833. (org-table-blank-field)))
  16834. t)
  16835. (eq N 1)
  16836. (looking-at "[^|\n]* |"))
  16837. (let (org-table-may-need-update)
  16838. (goto-char (1- (match-end 0)))
  16839. (backward-delete-char 1)
  16840. (goto-char (match-beginning 0))
  16841. (self-insert-command N)))
  16842. (t
  16843. (setq org-table-may-need-update t)
  16844. (self-insert-command N)
  16845. (org-fix-tags-on-the-fly)
  16846. (if org-self-insert-cluster-for-undo
  16847. (if (not (eq last-command 'org-self-insert-command))
  16848. (setq org-self-insert-command-undo-counter 1)
  16849. (if (>= org-self-insert-command-undo-counter 20)
  16850. (setq org-self-insert-command-undo-counter 1)
  16851. (and (> org-self-insert-command-undo-counter 0)
  16852. buffer-undo-list (listp buffer-undo-list)
  16853. (not (cadr buffer-undo-list)) ; remove nil entry
  16854. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16855. (setq org-self-insert-command-undo-counter
  16856. (1+ org-self-insert-command-undo-counter))))))))
  16857. (defun org-check-before-invisible-edit (kind)
  16858. "Check is editing if kind KIND would be dangerous with invisible text around.
  16859. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16860. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16861. (if (and org-catch-invisible-edits
  16862. (or (not (boundp 'visible-mode)) (not visible-mode))
  16863. (or (get-char-property (point) 'invisible)
  16864. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16865. ;; OK, we need to take a closer look
  16866. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16867. (invisible-before-point (if (bobp) nil (get-char-property
  16868. (1- (point)) 'invisible)))
  16869. (border-and-ok-direction
  16870. (or
  16871. ;; Check if we are acting predictably before invisible text
  16872. (and invisible-at-point (not invisible-before-point)
  16873. (memq kind '(insert delete-backward)))
  16874. ;; Check if we are acting predictably after invisible text
  16875. ;; This works not well, and I have turned it off. It seems
  16876. ;; better to always show and stop after invisible text.
  16877. ;; (and (not invisible-at-point) invisible-before-point
  16878. ;; (memq kind '(insert delete)))
  16879. )))
  16880. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16881. (memq invisible-before-point '(outline org-hide-block t)))
  16882. (if (eq org-catch-invisible-edits 'error)
  16883. (error "Editing in invisible areas is prohibited - make visible first"))
  16884. (if (and org-custom-properties-overlays
  16885. (y-or-n-p "Display invisible properties in this buffer? "))
  16886. (org-toggle-custom-properties-visibility)
  16887. ;; Make the area visible
  16888. (save-excursion
  16889. (if invisible-before-point
  16890. (goto-char (previous-single-char-property-change
  16891. (point) 'invisible)))
  16892. (org-cycle))
  16893. (cond
  16894. ((eq org-catch-invisible-edits 'show)
  16895. ;; That's it, we do the edit after showing
  16896. (message
  16897. "Unfolding invisible region around point before editing")
  16898. (sit-for 1))
  16899. ((and (eq org-catch-invisible-edits 'smart)
  16900. border-and-ok-direction)
  16901. (message "Unfolding invisible region around point before editing"))
  16902. (t
  16903. ;; Don't do the edit, make the user repeat it in full visibility
  16904. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  16905. (defun org-fix-tags-on-the-fly ()
  16906. (when (and (equal (char-after (point-at-bol)) ?*)
  16907. (org-at-heading-p))
  16908. (org-align-tags-here org-tags-column)))
  16909. (defun org-delete-backward-char (N)
  16910. "Like `delete-backward-char', insert whitespace at field end in tables.
  16911. When deleting backwards, in tables this function will insert whitespace in
  16912. front of the next \"|\" separator, to keep the table aligned. The table will
  16913. still be marked for re-alignment if the field did fill the entire column,
  16914. because, in this case the deletion might narrow the column."
  16915. (interactive "p")
  16916. (org-check-before-invisible-edit 'delete-backward)
  16917. (if (and (org-table-p)
  16918. (eq N 1)
  16919. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16920. (looking-at ".*?|"))
  16921. (let ((pos (point))
  16922. (noalign (looking-at "[^|\n\r]* |"))
  16923. (c org-table-may-need-update))
  16924. (backward-delete-char N)
  16925. (if (not overwrite-mode)
  16926. (progn
  16927. (skip-chars-forward "^|")
  16928. (insert " ")
  16929. (goto-char (1- pos))))
  16930. ;; noalign: if there were two spaces at the end, this field
  16931. ;; does not determine the width of the column.
  16932. (if noalign (setq org-table-may-need-update c)))
  16933. (backward-delete-char N)
  16934. (org-fix-tags-on-the-fly)))
  16935. (defun org-delete-char (N)
  16936. "Like `delete-char', but insert whitespace at field end in tables.
  16937. When deleting characters, in tables this function will insert whitespace in
  16938. front of the next \"|\" separator, to keep the table aligned. The table will
  16939. still be marked for re-alignment if the field did fill the entire column,
  16940. because, in this case the deletion might narrow the column."
  16941. (interactive "p")
  16942. (org-check-before-invisible-edit 'delete)
  16943. (if (and (org-table-p)
  16944. (not (bolp))
  16945. (not (= (char-after) ?|))
  16946. (eq N 1))
  16947. (if (looking-at ".*?|")
  16948. (let ((pos (point))
  16949. (noalign (looking-at "[^|\n\r]* |"))
  16950. (c org-table-may-need-update))
  16951. (replace-match (concat
  16952. (substring (match-string 0) 1 -1)
  16953. " |"))
  16954. (goto-char pos)
  16955. ;; noalign: if there were two spaces at the end, this field
  16956. ;; does not determine the width of the column.
  16957. (if noalign (setq org-table-may-need-update c)))
  16958. (delete-char N))
  16959. (delete-char N)
  16960. (org-fix-tags-on-the-fly)))
  16961. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16962. (put 'org-self-insert-command 'delete-selection t)
  16963. (put 'orgtbl-self-insert-command 'delete-selection t)
  16964. (put 'org-delete-char 'delete-selection 'supersede)
  16965. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16966. (put 'org-yank 'delete-selection 'yank)
  16967. ;; Make `flyspell-mode' delay after some commands
  16968. (put 'org-self-insert-command 'flyspell-delayed t)
  16969. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16970. (put 'org-delete-char 'flyspell-delayed t)
  16971. (put 'org-delete-backward-char 'flyspell-delayed t)
  16972. ;; Make pabbrev-mode expand after org-mode commands
  16973. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16974. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16975. ;; How to do this: Measure non-white length of current string
  16976. ;; If equal to column width, we should realign.
  16977. (defun org-remap (map &rest commands)
  16978. "In MAP, remap the functions given in COMMANDS.
  16979. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16980. (let (new old)
  16981. (while commands
  16982. (setq old (pop commands) new (pop commands))
  16983. (if (fboundp 'command-remapping)
  16984. (org-defkey map (vector 'remap old) new)
  16985. (substitute-key-definition old new map global-map)))))
  16986. (when (eq org-enable-table-editor 'optimized)
  16987. ;; If the user wants maximum table support, we need to hijack
  16988. ;; some standard editing functions
  16989. (org-remap org-mode-map
  16990. 'self-insert-command 'org-self-insert-command
  16991. 'delete-char 'org-delete-char
  16992. 'delete-backward-char 'org-delete-backward-char)
  16993. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16994. (defvar org-ctrl-c-ctrl-c-hook nil
  16995. "Hook for functions attaching themselves to `C-c C-c'.
  16996. This can be used to add additional functionality to the C-c C-c
  16997. key which executes context-dependent commands. This hook is run
  16998. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16999. run after the last test.
  17000. Each function will be called with no arguments. The function
  17001. must check if the context is appropriate for it to act. If yes,
  17002. it should do its thing and then return a non-nil value. If the
  17003. context is wrong, just do nothing and return nil.")
  17004. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17005. "Hook for functions attaching themselves to `C-c C-c'.
  17006. This can be used to add additional functionality to the C-c C-c
  17007. key which executes context-dependent commands. This hook is run
  17008. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17009. before the first test.
  17010. Each function will be called with no arguments. The function
  17011. must check if the context is appropriate for it to act. If yes,
  17012. it should do its thing and then return a non-nil value. If the
  17013. context is wrong, just do nothing and return nil.")
  17014. (defvar org-tab-first-hook nil
  17015. "Hook for functions to attach themselves to TAB.
  17016. See `org-ctrl-c-ctrl-c-hook' for more information.
  17017. This hook runs as the first action when TAB is pressed, even before
  17018. `org-cycle' messes around with the `outline-regexp' to cater for
  17019. inline tasks and plain list item folding.
  17020. If any function in this hook returns t, any other actions that
  17021. would have been caused by TAB (such as table field motion or visibility
  17022. cycling) will not occur.")
  17023. (defvar org-tab-after-check-for-table-hook nil
  17024. "Hook for functions to attach themselves to TAB.
  17025. See `org-ctrl-c-ctrl-c-hook' for more information.
  17026. This hook runs after it has been established that the cursor is not in a
  17027. table, but before checking if the cursor is in a headline or if global cycling
  17028. should be done.
  17029. If any function in this hook returns t, not other actions like visibility
  17030. cycling will be done.")
  17031. (defvar org-tab-after-check-for-cycling-hook nil
  17032. "Hook for functions to attach themselves to TAB.
  17033. See `org-ctrl-c-ctrl-c-hook' for more information.
  17034. This hook runs after it has been established that not table field motion and
  17035. not visibility should be done because of current context. This is probably
  17036. the place where a package like yasnippets can hook in.")
  17037. (defvar org-tab-before-tab-emulation-hook nil
  17038. "Hook for functions to attach themselves to TAB.
  17039. See `org-ctrl-c-ctrl-c-hook' for more information.
  17040. This hook runs after every other options for TAB have been exhausted, but
  17041. before indentation and \t insertion takes place.")
  17042. (defvar org-metaleft-hook nil
  17043. "Hook for functions attaching themselves to `M-left'.
  17044. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17045. (defvar org-metaright-hook nil
  17046. "Hook for functions attaching themselves to `M-right'.
  17047. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17048. (defvar org-metaup-hook nil
  17049. "Hook for functions attaching themselves to `M-up'.
  17050. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17051. (defvar org-metadown-hook nil
  17052. "Hook for functions attaching themselves to `M-down'.
  17053. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17054. (defvar org-shiftmetaleft-hook nil
  17055. "Hook for functions attaching themselves to `M-S-left'.
  17056. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17057. (defvar org-shiftmetaright-hook nil
  17058. "Hook for functions attaching themselves to `M-S-right'.
  17059. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17060. (defvar org-shiftmetaup-hook nil
  17061. "Hook for functions attaching themselves to `M-S-up'.
  17062. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17063. (defvar org-shiftmetadown-hook nil
  17064. "Hook for functions attaching themselves to `M-S-down'.
  17065. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17066. (defvar org-metareturn-hook nil
  17067. "Hook for functions attaching themselves to `M-RET'.
  17068. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17069. (defvar org-shiftup-hook nil
  17070. "Hook for functions attaching themselves to `S-up'.
  17071. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17072. (defvar org-shiftup-final-hook nil
  17073. "Hook for functions attaching themselves to `S-up'.
  17074. This one runs after all other options except shift-select have been excluded.
  17075. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17076. (defvar org-shiftdown-hook nil
  17077. "Hook for functions attaching themselves to `S-down'.
  17078. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17079. (defvar org-shiftdown-final-hook nil
  17080. "Hook for functions attaching themselves to `S-down'.
  17081. This one runs after all other options except shift-select have been excluded.
  17082. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17083. (defvar org-shiftleft-hook nil
  17084. "Hook for functions attaching themselves to `S-left'.
  17085. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17086. (defvar org-shiftleft-final-hook nil
  17087. "Hook for functions attaching themselves to `S-left'.
  17088. This one runs after all other options except shift-select have been excluded.
  17089. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17090. (defvar org-shiftright-hook nil
  17091. "Hook for functions attaching themselves to `S-right'.
  17092. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17093. (defvar org-shiftright-final-hook nil
  17094. "Hook for functions attaching themselves to `S-right'.
  17095. This one runs after all other options except shift-select have been excluded.
  17096. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17097. (defun org-modifier-cursor-error ()
  17098. "Throw an error, a modified cursor command was applied in wrong context."
  17099. (error "This command is active in special context like tables, headlines or items"))
  17100. (defun org-shiftselect-error ()
  17101. "Throw an error because Shift-Cursor command was applied in wrong context."
  17102. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17103. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17104. (error "This command works only in special context like headlines or timestamps")))
  17105. (defun org-call-for-shift-select (cmd)
  17106. (let ((this-command-keys-shift-translated t))
  17107. (call-interactively cmd)))
  17108. (defun org-shifttab (&optional arg)
  17109. "Global visibility cycling or move to previous table field.
  17110. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17111. on context.
  17112. See the individual commands for more information."
  17113. (interactive "P")
  17114. (cond
  17115. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17116. ((integerp arg)
  17117. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17118. (message "Content view to level: %d" arg)
  17119. (org-content (prefix-numeric-value arg2))
  17120. (setq org-cycle-global-status 'overview)))
  17121. (t (call-interactively 'org-global-cycle))))
  17122. (defun org-shiftmetaleft ()
  17123. "Promote subtree or delete table column.
  17124. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17125. `org-table-delete-column', depending on context. See the
  17126. individual commands for more information."
  17127. (interactive)
  17128. (cond
  17129. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17130. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17131. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17132. ((if (not (org-region-active-p)) (org-at-item-p)
  17133. (save-excursion (goto-char (region-beginning))
  17134. (org-at-item-p)))
  17135. (call-interactively 'org-outdent-item-tree))
  17136. (t (org-modifier-cursor-error))))
  17137. (defun org-shiftmetaright ()
  17138. "Demote subtree or insert table column.
  17139. Calls `org-demote-subtree', `org-indent-item-tree', or
  17140. `org-table-insert-column', depending on context. See the
  17141. individual commands for more information."
  17142. (interactive)
  17143. (cond
  17144. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17145. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17146. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17147. ((if (not (org-region-active-p)) (org-at-item-p)
  17148. (save-excursion (goto-char (region-beginning))
  17149. (org-at-item-p)))
  17150. (call-interactively 'org-indent-item-tree))
  17151. (t (org-modifier-cursor-error))))
  17152. (defun org-shiftmetaup (&optional arg)
  17153. "Move subtree up or kill table row.
  17154. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17155. `org-move-item-up' or `org-timestamp-up', depending on context.
  17156. See the individual commands for more information."
  17157. (interactive "P")
  17158. (cond
  17159. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17160. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17161. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17162. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17163. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17164. (call-interactively 'org-timestamp-up)))
  17165. (t (org-modifier-cursor-error))))
  17166. (defun org-shiftmetadown (&optional arg)
  17167. "Move subtree down or insert table row.
  17168. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17169. `org-move-item-down' or `org-timestamp-up', depending on context.
  17170. See the individual commands for more information."
  17171. (interactive "P")
  17172. (cond
  17173. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17174. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17175. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17176. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17177. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17178. (call-interactively 'org-timestamp-down)))
  17179. (t (org-modifier-cursor-error))))
  17180. (defsubst org-hidden-tree-error ()
  17181. (error
  17182. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17183. (defun org-metaleft (&optional arg)
  17184. "Promote heading or move table column to left.
  17185. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17186. With no specific context, calls the Emacs default `backward-word'.
  17187. See the individual commands for more information."
  17188. (interactive "P")
  17189. (cond
  17190. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17191. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17192. ((org-with-limited-levels
  17193. (or (org-at-heading-p)
  17194. (and (org-region-active-p)
  17195. (save-excursion
  17196. (goto-char (region-beginning))
  17197. (org-at-heading-p)))))
  17198. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17199. (call-interactively 'org-do-promote))
  17200. ;; At an inline task.
  17201. ((org-at-heading-p)
  17202. (call-interactively 'org-inlinetask-promote))
  17203. ((or (org-at-item-p)
  17204. (and (org-region-active-p)
  17205. (save-excursion
  17206. (goto-char (region-beginning))
  17207. (org-at-item-p))))
  17208. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17209. (call-interactively 'org-outdent-item))
  17210. (t (call-interactively 'backward-word))))
  17211. (defun org-metaright (&optional arg)
  17212. "Demote a subtree, a list item or move table column to right.
  17213. In front of a drawer or a block keyword, indent it correctly.
  17214. With no specific context, calls the Emacs default `forward-word'.
  17215. See the individual commands for more information."
  17216. (interactive "P")
  17217. (cond
  17218. ((run-hook-with-args-until-success 'org-metaright-hook))
  17219. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17220. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17221. ((org-at-block-p) (call-interactively 'org-indent-block))
  17222. ((org-with-limited-levels
  17223. (or (org-at-heading-p)
  17224. (and (org-region-active-p)
  17225. (save-excursion
  17226. (goto-char (region-beginning))
  17227. (org-at-heading-p)))))
  17228. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17229. (call-interactively 'org-do-demote))
  17230. ;; At an inline task.
  17231. ((org-at-heading-p)
  17232. (call-interactively 'org-inlinetask-demote))
  17233. ((or (org-at-item-p)
  17234. (and (org-region-active-p)
  17235. (save-excursion
  17236. (goto-char (region-beginning))
  17237. (org-at-item-p))))
  17238. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17239. (call-interactively 'org-indent-item))
  17240. (t (call-interactively 'forward-word))))
  17241. (defun org-check-for-hidden (what)
  17242. "Check if there are hidden headlines/items in the current visual line.
  17243. WHAT can be either `headlines' or `items'. If the current line is
  17244. an outline or item heading and it has a folded subtree below it,
  17245. this function returns t, nil otherwise."
  17246. (let ((re (cond
  17247. ((eq what 'headlines) org-outline-regexp-bol)
  17248. ((eq what 'items) (org-item-beginning-re))
  17249. (t (error "This should not happen"))))
  17250. beg end)
  17251. (save-excursion
  17252. (catch 'exit
  17253. (unless (org-region-active-p)
  17254. (setq beg (point-at-bol))
  17255. (beginning-of-line 2)
  17256. (while (and (not (eobp)) ;; this is like `next-line'
  17257. (get-char-property (1- (point)) 'invisible))
  17258. (beginning-of-line 2))
  17259. (setq end (point))
  17260. (goto-char beg)
  17261. (goto-char (point-at-eol))
  17262. (setq end (max end (point)))
  17263. (while (re-search-forward re end t)
  17264. (if (get-char-property (match-beginning 0) 'invisible)
  17265. (throw 'exit t))))
  17266. nil))))
  17267. (defun org-metaup (&optional arg)
  17268. "Move subtree up or move table row up.
  17269. Calls `org-move-subtree-up' or `org-table-move-row' or
  17270. `org-move-item-up', depending on context. See the individual commands
  17271. for more information."
  17272. (interactive "P")
  17273. (cond
  17274. ((run-hook-with-args-until-success 'org-metaup-hook))
  17275. ((org-region-active-p)
  17276. (let* ((a (min (region-beginning) (region-end)))
  17277. (b (1- (max (region-beginning) (region-end))))
  17278. (c (save-excursion (goto-char a)
  17279. (move-beginning-of-line 0)))
  17280. (d (save-excursion (goto-char a)
  17281. (move-end-of-line 0) (point))))
  17282. (transpose-regions a b c d)
  17283. (goto-char c)))
  17284. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17285. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17286. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17287. (t (org-drag-element-backward))))
  17288. (defun org-metadown (&optional arg)
  17289. "Move subtree down or move table row down.
  17290. Calls `org-move-subtree-down' or `org-table-move-row' or
  17291. `org-move-item-down', depending on context. See the individual
  17292. commands for more information."
  17293. (interactive "P")
  17294. (cond
  17295. ((run-hook-with-args-until-success 'org-metadown-hook))
  17296. ((org-region-active-p)
  17297. (let* ((a (min (region-beginning) (region-end)))
  17298. (b (max (region-beginning) (region-end)))
  17299. (c (save-excursion (goto-char b)
  17300. (move-beginning-of-line 1)))
  17301. (d (save-excursion (goto-char b)
  17302. (move-end-of-line 1) (1+ (point)))))
  17303. (transpose-regions a b c d)
  17304. (goto-char d)))
  17305. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17306. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17307. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17308. (t (org-drag-element-forward))))
  17309. (defun org-shiftup (&optional arg)
  17310. "Increase item in timestamp or increase priority of current headline.
  17311. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17312. depending on context. See the individual commands for more information."
  17313. (interactive "P")
  17314. (cond
  17315. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17316. ((and org-support-shift-select (org-region-active-p))
  17317. (org-call-for-shift-select 'previous-line))
  17318. ((org-at-timestamp-p t)
  17319. (call-interactively (if org-edit-timestamp-down-means-later
  17320. 'org-timestamp-down 'org-timestamp-up)))
  17321. ((and (not (eq org-support-shift-select 'always))
  17322. org-enable-priority-commands
  17323. (org-at-heading-p))
  17324. (call-interactively 'org-priority-up))
  17325. ((and (not org-support-shift-select) (org-at-item-p))
  17326. (call-interactively 'org-previous-item))
  17327. ((org-clocktable-try-shift 'up arg))
  17328. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17329. (org-support-shift-select
  17330. (org-call-for-shift-select 'previous-line))
  17331. (t (org-shiftselect-error))))
  17332. (defun org-shiftdown (&optional arg)
  17333. "Decrease item in timestamp or decrease priority of current headline.
  17334. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17335. depending on context. See the individual commands for more information."
  17336. (interactive "P")
  17337. (cond
  17338. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17339. ((and org-support-shift-select (org-region-active-p))
  17340. (org-call-for-shift-select 'next-line))
  17341. ((org-at-timestamp-p t)
  17342. (call-interactively (if org-edit-timestamp-down-means-later
  17343. 'org-timestamp-up 'org-timestamp-down)))
  17344. ((and (not (eq org-support-shift-select 'always))
  17345. org-enable-priority-commands
  17346. (org-at-heading-p))
  17347. (call-interactively 'org-priority-down))
  17348. ((and (not org-support-shift-select) (org-at-item-p))
  17349. (call-interactively 'org-next-item))
  17350. ((org-clocktable-try-shift 'down arg))
  17351. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17352. (org-support-shift-select
  17353. (org-call-for-shift-select 'next-line))
  17354. (t (org-shiftselect-error))))
  17355. (defun org-shiftright (&optional arg)
  17356. "Cycle the thing at point or in the current line, depending on context.
  17357. Depending on context, this does one of the following:
  17358. - switch a timestamp at point one day into the future
  17359. - on a headline, switch to the next TODO keyword.
  17360. - on an item, switch entire list to the next bullet type
  17361. - on a property line, switch to the next allowed value
  17362. - on a clocktable definition line, move time block into the future"
  17363. (interactive "P")
  17364. (cond
  17365. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17366. ((and org-support-shift-select (org-region-active-p))
  17367. (org-call-for-shift-select 'forward-char))
  17368. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17369. ((and (not (eq org-support-shift-select 'always))
  17370. (org-at-heading-p))
  17371. (let ((org-inhibit-logging
  17372. (not org-treat-S-cursor-todo-selection-as-state-change))
  17373. (org-inhibit-blocking
  17374. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17375. (org-call-with-arg 'org-todo 'right)))
  17376. ((or (and org-support-shift-select
  17377. (not (eq org-support-shift-select 'always))
  17378. (org-at-item-bullet-p))
  17379. (and (not org-support-shift-select) (org-at-item-p)))
  17380. (org-call-with-arg 'org-cycle-list-bullet nil))
  17381. ((and (not (eq org-support-shift-select 'always))
  17382. (org-at-property-p))
  17383. (call-interactively 'org-property-next-allowed-value))
  17384. ((org-clocktable-try-shift 'right arg))
  17385. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17386. (org-support-shift-select
  17387. (org-call-for-shift-select 'forward-char))
  17388. (t (org-shiftselect-error))))
  17389. (defun org-shiftleft (&optional arg)
  17390. "Cycle the thing at point or in the current line, depending on context.
  17391. Depending on context, this does one of the following:
  17392. - switch a timestamp at point one day into the past
  17393. - on a headline, switch to the previous TODO keyword.
  17394. - on an item, switch entire list to the previous bullet type
  17395. - on a property line, switch to the previous allowed value
  17396. - on a clocktable definition line, move time block into the past"
  17397. (interactive "P")
  17398. (cond
  17399. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17400. ((and org-support-shift-select (org-region-active-p))
  17401. (org-call-for-shift-select 'backward-char))
  17402. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17403. ((and (not (eq org-support-shift-select 'always))
  17404. (org-at-heading-p))
  17405. (let ((org-inhibit-logging
  17406. (not org-treat-S-cursor-todo-selection-as-state-change))
  17407. (org-inhibit-blocking
  17408. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17409. (org-call-with-arg 'org-todo 'left)))
  17410. ((or (and org-support-shift-select
  17411. (not (eq org-support-shift-select 'always))
  17412. (org-at-item-bullet-p))
  17413. (and (not org-support-shift-select) (org-at-item-p)))
  17414. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17415. ((and (not (eq org-support-shift-select 'always))
  17416. (org-at-property-p))
  17417. (call-interactively 'org-property-previous-allowed-value))
  17418. ((org-clocktable-try-shift 'left arg))
  17419. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17420. (org-support-shift-select
  17421. (org-call-for-shift-select 'backward-char))
  17422. (t (org-shiftselect-error))))
  17423. (defun org-shiftcontrolright ()
  17424. "Switch to next TODO set."
  17425. (interactive)
  17426. (cond
  17427. ((and org-support-shift-select (org-region-active-p))
  17428. (org-call-for-shift-select 'forward-word))
  17429. ((and (not (eq org-support-shift-select 'always))
  17430. (org-at-heading-p))
  17431. (org-call-with-arg 'org-todo 'nextset))
  17432. (org-support-shift-select
  17433. (org-call-for-shift-select 'forward-word))
  17434. (t (org-shiftselect-error))))
  17435. (defun org-shiftcontrolleft ()
  17436. "Switch to previous TODO set."
  17437. (interactive)
  17438. (cond
  17439. ((and org-support-shift-select (org-region-active-p))
  17440. (org-call-for-shift-select 'backward-word))
  17441. ((and (not (eq org-support-shift-select 'always))
  17442. (org-at-heading-p))
  17443. (org-call-with-arg 'org-todo 'previousset))
  17444. (org-support-shift-select
  17445. (org-call-for-shift-select 'backward-word))
  17446. (t (org-shiftselect-error))))
  17447. (defun org-shiftcontrolup ()
  17448. "Change timestamps synchronously up in CLOCK log lines."
  17449. (interactive)
  17450. (cond ((and (not org-support-shift-select)
  17451. (org-at-clock-log-p)
  17452. (org-at-timestamp-p t))
  17453. (org-clock-timestamps-up))
  17454. (t (org-shiftselect-error))))
  17455. (defun org-shiftcontroldown ()
  17456. "Change timestamps synchronously down in CLOCK log lines."
  17457. (interactive)
  17458. (cond ((and (not org-support-shift-select)
  17459. (org-at-clock-log-p)
  17460. (org-at-timestamp-p t))
  17461. (org-clock-timestamps-down))
  17462. (t (org-shiftselect-error))))
  17463. (defun org-ctrl-c-ret ()
  17464. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17465. (interactive)
  17466. (cond
  17467. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17468. (t (call-interactively 'org-insert-heading))))
  17469. (defun org-find-visible ()
  17470. (let ((s (point)))
  17471. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17472. (get-char-property s 'invisible)))
  17473. s))
  17474. (defun org-find-invisible ()
  17475. (let ((s (point)))
  17476. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17477. (not (get-char-property s 'invisible))))
  17478. s))
  17479. (defun org-copy-visible (beg end)
  17480. "Copy the visible parts of the region."
  17481. (interactive "r")
  17482. (let (snippets s)
  17483. (save-excursion
  17484. (save-restriction
  17485. (narrow-to-region beg end)
  17486. (setq s (goto-char (point-min)))
  17487. (while (not (= (point) (point-max)))
  17488. (goto-char (org-find-invisible))
  17489. (push (buffer-substring s (point)) snippets)
  17490. (setq s (goto-char (org-find-visible))))))
  17491. (kill-new (apply 'concat (nreverse snippets)))))
  17492. (defun org-copy-special ()
  17493. "Copy region in table or copy current subtree.
  17494. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17495. See the individual commands for more information."
  17496. (interactive)
  17497. (call-interactively
  17498. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17499. (defun org-cut-special ()
  17500. "Cut region in table or cut current subtree.
  17501. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17502. See the individual commands for more information."
  17503. (interactive)
  17504. (call-interactively
  17505. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17506. (defun org-paste-special (arg)
  17507. "Paste rectangular region into table, or past subtree relative to level.
  17508. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17509. See the individual commands for more information."
  17510. (interactive "P")
  17511. (if (org-at-table-p)
  17512. (org-table-paste-rectangle)
  17513. (org-paste-subtree arg)))
  17514. (defun org-edit-special (&optional arg)
  17515. "Call a special editor for the stuff at point.
  17516. When at a table, call the formula editor with `org-table-edit-formulas'.
  17517. When at the first line of an src example, call `org-edit-src-code'.
  17518. When in an #+include line, visit the include file. Otherwise call
  17519. `ffap' to visit the file at point."
  17520. (interactive)
  17521. ;; possibly prep session before editing source
  17522. (when arg
  17523. (let* ((info (org-babel-get-src-block-info))
  17524. (lang (nth 0 info))
  17525. (params (nth 2 info))
  17526. (session (cdr (assoc :session params))))
  17527. (when (and info session) ;; we are in a source-code block with a session
  17528. (funcall
  17529. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17530. (cond ;; proceed with `org-edit-special'
  17531. ((save-excursion
  17532. (beginning-of-line 1)
  17533. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17534. (find-file (org-trim (match-string 1))))
  17535. ((org-edit-src-code))
  17536. ((org-edit-fixed-width-region))
  17537. ((org-at-table.el-p)
  17538. (org-edit-src-code))
  17539. ((or (org-at-table-p)
  17540. (save-excursion
  17541. (beginning-of-line 1)
  17542. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17543. (call-interactively 'org-table-edit-formulas))
  17544. (t (call-interactively 'ffap))))
  17545. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17546. (defun org-ctrl-c-ctrl-c (&optional arg)
  17547. "Set tags in headline, or update according to changed information at point.
  17548. This command does many different things, depending on context:
  17549. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17550. this is what we do.
  17551. - If the cursor is on a statistics cookie, update it.
  17552. - If the cursor is in a headline, prompt for tags and insert them
  17553. into the current line, aligned to `org-tags-column'. When called
  17554. with prefix arg, realign all tags in the current buffer.
  17555. - If the cursor is in one of the special #+KEYWORD lines, this
  17556. triggers scanning the buffer for these lines and updating the
  17557. information.
  17558. - If the cursor is inside a table, realign the table. This command
  17559. works even if the automatic table editor has been turned off.
  17560. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17561. the entire table.
  17562. - If the cursor is at a footnote reference or definition, jump to
  17563. the corresponding definition or references, respectively.
  17564. - If the cursor is a the beginning of a dynamic block, update it.
  17565. - If the current buffer is a capture buffer, close note and file it.
  17566. - If the cursor is on a <<<target>>>, update radio targets and
  17567. corresponding links in this buffer.
  17568. - If the cursor is on a numbered item in a plain list, renumber the
  17569. ordered list.
  17570. - If the cursor is on a checkbox, toggle it.
  17571. - If the cursor is on a code block, evaluate it. The variable
  17572. `org-confirm-babel-evaluate' can be used to control prompting
  17573. before code block evaluation, by default every code block
  17574. evaluation requires confirmation. Code block evaluation can be
  17575. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17576. (interactive "P")
  17577. (let ((org-enable-table-editor t))
  17578. (cond
  17579. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17580. org-occur-highlights
  17581. org-latex-fragment-image-overlays)
  17582. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17583. (org-remove-occur-highlights)
  17584. (org-remove-latex-fragment-image-overlays)
  17585. (message "Temporary highlights/overlays removed from current buffer"))
  17586. ((and (local-variable-p 'org-finish-function (current-buffer))
  17587. (fboundp org-finish-function))
  17588. (funcall org-finish-function))
  17589. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17590. ((org-in-regexp org-ts-regexp-both)
  17591. (org-timestamp-change 0 'day))
  17592. ((or (looking-at org-property-start-re)
  17593. (org-at-property-p))
  17594. (call-interactively 'org-property-action))
  17595. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17596. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17597. (or (org-at-heading-p) (org-at-item-p)))
  17598. (call-interactively 'org-update-statistics-cookies))
  17599. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17600. ((org-at-table.el-p)
  17601. (message "Use C-c ' to edit table.el tables"))
  17602. ((org-at-table-p)
  17603. (org-table-maybe-eval-formula)
  17604. (if arg
  17605. (call-interactively 'org-table-recalculate)
  17606. (org-table-maybe-recalculate-line))
  17607. (call-interactively 'org-table-align)
  17608. (orgtbl-send-table 'maybe))
  17609. ((or (org-footnote-at-reference-p)
  17610. (org-footnote-at-definition-p))
  17611. (call-interactively 'org-footnote-action))
  17612. ((org-at-item-checkbox-p)
  17613. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17614. ;; list only if at top item.
  17615. (let* ((cbox (match-string 1))
  17616. (struct (org-list-struct))
  17617. (old-struct (copy-tree struct))
  17618. (parents (org-list-parents-alist struct))
  17619. (orderedp (org-entry-get nil "ORDERED"))
  17620. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17621. block-item)
  17622. ;; Use a light version of `org-toggle-checkbox' to avoid
  17623. ;; computing list structure twice.
  17624. (let ((new-box (cond
  17625. ((equal arg '(16)) "[-]")
  17626. ((equal arg '(4)) nil)
  17627. ((equal "[X]" cbox) "[ ]")
  17628. (t "[X]"))))
  17629. (if (and firstp arg)
  17630. ;; If at first item of sub-list, remove check-box from
  17631. ;; every item at the same level.
  17632. (mapc
  17633. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17634. (org-list-get-all-items
  17635. (point-at-bol) struct (org-list-prevs-alist struct)))
  17636. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17637. ;; Replicate `org-list-write-struct', while grabbing a return
  17638. ;; value from `org-list-struct-fix-box'.
  17639. (org-list-struct-fix-ind struct parents 2)
  17640. (org-list-struct-fix-item-end struct)
  17641. (let ((prevs (org-list-prevs-alist struct)))
  17642. (org-list-struct-fix-bul struct prevs)
  17643. (org-list-struct-fix-ind struct parents)
  17644. (setq block-item
  17645. (org-list-struct-fix-box struct parents prevs orderedp)))
  17646. (org-list-struct-apply-struct struct old-struct)
  17647. (org-update-checkbox-count-maybe)
  17648. (when block-item
  17649. (message
  17650. "Checkboxes were removed due to unchecked box at line %d"
  17651. (org-current-line block-item)))
  17652. (when firstp (org-list-send-list 'maybe))))
  17653. ((org-at-item-p)
  17654. ;; Cursor at an item: repair list. Do checkbox related actions
  17655. ;; only if function was called with an argument. Send list only
  17656. ;; if at top item.
  17657. (let* ((struct (org-list-struct))
  17658. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17659. old-struct)
  17660. (when arg
  17661. (setq old-struct (copy-tree struct))
  17662. (if firstp
  17663. ;; If at first item of sub-list, add check-box to every
  17664. ;; item at the same level.
  17665. (mapc
  17666. (lambda (pos)
  17667. (unless (org-list-get-checkbox pos struct)
  17668. (org-list-set-checkbox pos struct "[ ]")))
  17669. (org-list-get-all-items
  17670. (point-at-bol) struct (org-list-prevs-alist struct)))
  17671. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17672. (org-list-write-struct
  17673. struct (org-list-parents-alist struct) old-struct)
  17674. (when arg (org-update-checkbox-count-maybe))
  17675. (when firstp (org-list-send-list 'maybe))))
  17676. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17677. ;; Dynamic block
  17678. (beginning-of-line 1)
  17679. (save-excursion (org-update-dblock)))
  17680. ((save-excursion
  17681. (let ((case-fold-search t))
  17682. (beginning-of-line 1)
  17683. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17684. (cond
  17685. ((or (equal (match-string 1) "TBLFM")
  17686. (equal (match-string 1) "tblfm"))
  17687. ;; Recalculate the table before this line
  17688. (save-excursion
  17689. (beginning-of-line 1)
  17690. (skip-chars-backward " \r\n\t")
  17691. (if (org-at-table-p)
  17692. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17693. (t
  17694. (let ((org-inhibit-startup-visibility-stuff t)
  17695. (org-startup-align-all-tables nil))
  17696. (when (boundp 'org-table-coordinate-overlays)
  17697. (mapc 'delete-overlay org-table-coordinate-overlays)
  17698. (setq org-table-coordinate-overlays nil))
  17699. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17700. (message "Local setup has been refreshed"))))
  17701. ((org-clock-update-time-maybe))
  17702. (t
  17703. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17704. (error "C-c C-c can do nothing useful at this location"))))))
  17705. (defun org-mode-restart ()
  17706. "Restart Org-mode, to scan again for special lines.
  17707. Also updates the keyword regular expressions."
  17708. (interactive)
  17709. (org-mode)
  17710. (message "Org-mode restarted"))
  17711. (defun org-kill-note-or-show-branches ()
  17712. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17713. (interactive)
  17714. (if (not org-finish-function)
  17715. (progn
  17716. (hide-subtree)
  17717. (call-interactively 'show-branches))
  17718. (let ((org-note-abort t))
  17719. (funcall org-finish-function))))
  17720. (defun org-return (&optional indent)
  17721. "Goto next table row or insert a newline.
  17722. Calls `org-table-next-row' or `newline', depending on context.
  17723. See the individual commands for more information."
  17724. (interactive)
  17725. (let (org-ts-what)
  17726. (cond
  17727. ((or (bobp) (org-in-src-block-p))
  17728. (if indent (newline-and-indent) (newline)))
  17729. ((org-at-table-p)
  17730. (org-table-justify-field-maybe)
  17731. (call-interactively 'org-table-next-row))
  17732. ;; when `newline-and-indent' is called within a list, make sure
  17733. ;; text moved stays inside the item.
  17734. ((and (org-in-item-p) indent)
  17735. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17736. (progn
  17737. (save-match-data (newline))
  17738. (org-indent-line-to (length (match-string 0))))
  17739. (let ((ind (org-get-indentation)))
  17740. (newline)
  17741. (if (org-looking-back org-list-end-re)
  17742. (org-indent-line)
  17743. (org-indent-line-to ind)))))
  17744. ((and org-return-follows-link
  17745. (org-at-timestamp-p t)
  17746. (not (eq org-ts-what 'after)))
  17747. (org-follow-timestamp-link))
  17748. ((and org-return-follows-link
  17749. (let ((tprop (get-text-property (point) 'face)))
  17750. (or (eq tprop 'org-link)
  17751. (and (listp tprop) (memq 'org-link tprop)))))
  17752. (call-interactively 'org-open-at-point))
  17753. ((and (org-at-heading-p)
  17754. (looking-at
  17755. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17756. (org-show-entry)
  17757. (end-of-line 1)
  17758. (newline))
  17759. (t (if indent (newline-and-indent) (newline))))))
  17760. (defun org-return-indent ()
  17761. "Goto next table row or insert a newline and indent.
  17762. Calls `org-table-next-row' or `newline-and-indent', depending on
  17763. context. See the individual commands for more information."
  17764. (interactive)
  17765. (org-return t))
  17766. (defun org-ctrl-c-star ()
  17767. "Compute table, or change heading status of lines.
  17768. Calls `org-table-recalculate' or `org-toggle-heading',
  17769. depending on context."
  17770. (interactive)
  17771. (cond
  17772. ((org-at-table-p)
  17773. (call-interactively 'org-table-recalculate))
  17774. (t
  17775. ;; Convert all lines in region to list items
  17776. (call-interactively 'org-toggle-heading))))
  17777. (defun org-ctrl-c-minus ()
  17778. "Insert separator line in table or modify bullet status of line.
  17779. Also turns a plain line or a region of lines into list items.
  17780. Calls `org-table-insert-hline', `org-toggle-item', or
  17781. `org-cycle-list-bullet', depending on context."
  17782. (interactive)
  17783. (cond
  17784. ((org-at-table-p)
  17785. (call-interactively 'org-table-insert-hline))
  17786. ((org-region-active-p)
  17787. (call-interactively 'org-toggle-item))
  17788. ((org-in-item-p)
  17789. (call-interactively 'org-cycle-list-bullet))
  17790. (t
  17791. (call-interactively 'org-toggle-item))))
  17792. (defun org-toggle-item (arg)
  17793. "Convert headings or normal lines to items, items to normal lines.
  17794. If there is no active region, only the current line is considered.
  17795. If the first non blank line in the region is an headline, convert
  17796. all headlines to items, shifting text accordingly.
  17797. If it is an item, convert all items to normal lines.
  17798. If it is normal text, change region into an item. With a prefix
  17799. argument ARG, change each line in region into an item."
  17800. (interactive "P")
  17801. (let ((shift-text
  17802. (function
  17803. ;; Shift text in current section to IND, from point to END.
  17804. ;; The function leaves point to END line.
  17805. (lambda (ind end)
  17806. (let ((min-i 1000) (end (copy-marker end)))
  17807. ;; First determine the minimum indentation (MIN-I) of
  17808. ;; the text.
  17809. (save-excursion
  17810. (catch 'exit
  17811. (while (< (point) end)
  17812. (let ((i (org-get-indentation)))
  17813. (cond
  17814. ;; Skip blank lines and inline tasks.
  17815. ((looking-at "^[ \t]*$"))
  17816. ((looking-at org-outline-regexp-bol))
  17817. ;; We can't find less than 0 indentation.
  17818. ((zerop i) (throw 'exit (setq min-i 0)))
  17819. ((< i min-i) (setq min-i i))))
  17820. (forward-line))))
  17821. ;; Then indent each line so that a line indented to
  17822. ;; MIN-I becomes indented to IND. Ignore blank lines
  17823. ;; and inline tasks in the process.
  17824. (let ((delta (- ind min-i)))
  17825. (while (< (point) end)
  17826. (unless (or (looking-at "^[ \t]*$")
  17827. (looking-at org-outline-regexp-bol))
  17828. (org-indent-line-to (+ (org-get-indentation) delta)))
  17829. (forward-line)))))))
  17830. (skip-blanks
  17831. (function
  17832. ;; Return beginning of first non-blank line, starting from
  17833. ;; line at POS.
  17834. (lambda (pos)
  17835. (save-excursion
  17836. (goto-char pos)
  17837. (skip-chars-forward " \r\t\n")
  17838. (point-at-bol)))))
  17839. beg end)
  17840. ;; Determine boundaries of changes.
  17841. (if (org-region-active-p)
  17842. (setq beg (funcall skip-blanks (region-beginning))
  17843. end (copy-marker (region-end)))
  17844. (setq beg (funcall skip-blanks (point-at-bol))
  17845. end (copy-marker (point-at-eol))))
  17846. ;; Depending on the starting line, choose an action on the text
  17847. ;; between BEG and END.
  17848. (org-with-limited-levels
  17849. (save-excursion
  17850. (goto-char beg)
  17851. (cond
  17852. ;; Case 1. Start at an item: de-itemize. Note that it only
  17853. ;; happens when a region is active: `org-ctrl-c-minus'
  17854. ;; would call `org-cycle-list-bullet' otherwise.
  17855. ((org-at-item-p)
  17856. (while (< (point) end)
  17857. (when (org-at-item-p)
  17858. (skip-chars-forward " \t")
  17859. (delete-region (point) (match-end 0)))
  17860. (forward-line)))
  17861. ;; Case 2. Start at an heading: convert to items.
  17862. ((org-at-heading-p)
  17863. (let* ((bul (org-list-bullet-string "-"))
  17864. (bul-len (length bul))
  17865. ;; Indentation of the first heading. It should be
  17866. ;; relative to the indentation of its parent, if any.
  17867. (start-ind (save-excursion
  17868. (cond
  17869. ((not org-adapt-indentation) 0)
  17870. ((not (outline-previous-heading)) 0)
  17871. (t (length (match-string 0))))))
  17872. ;; Level of first heading. Further headings will be
  17873. ;; compared to it to determine hierarchy in the list.
  17874. (ref-level (org-reduced-level (org-outline-level))))
  17875. (while (< (point) end)
  17876. (let* ((level (org-reduced-level (org-outline-level)))
  17877. (delta (max 0 (- level ref-level))))
  17878. ;; If current headline is less indented than the first
  17879. ;; one, set it as reference, in order to preserve
  17880. ;; subtrees.
  17881. (when (< level ref-level) (setq ref-level level))
  17882. (replace-match bul t t)
  17883. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17884. ;; Ensure all text down to END (or SECTION-END) belongs
  17885. ;; to the newly created item.
  17886. (let ((section-end (save-excursion
  17887. (or (outline-next-heading) (point)))))
  17888. (forward-line)
  17889. (funcall shift-text
  17890. (+ start-ind (* (1+ delta) bul-len))
  17891. (min end section-end)))))))
  17892. ;; Case 3. Normal line with ARG: turn each non-item line into
  17893. ;; an item.
  17894. (arg
  17895. (while (< (point) end)
  17896. (unless (or (org-at-heading-p) (org-at-item-p))
  17897. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17898. (replace-match
  17899. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17900. (forward-line)))
  17901. ;; Case 4. Normal line without ARG: make the first line of
  17902. ;; region an item, and shift indentation of others
  17903. ;; lines to set them as item's body.
  17904. (t (let* ((bul (org-list-bullet-string "-"))
  17905. (bul-len (length bul))
  17906. (ref-ind (org-get-indentation)))
  17907. (skip-chars-forward " \t")
  17908. (insert bul)
  17909. (forward-line)
  17910. (while (< (point) end)
  17911. ;; Ensure that lines less indented than first one
  17912. ;; still get included in item body.
  17913. (funcall shift-text
  17914. (+ ref-ind bul-len)
  17915. (min end (save-excursion (or (outline-next-heading)
  17916. (point)))))
  17917. (forward-line)))))))))
  17918. (defun org-toggle-heading (&optional nstars)
  17919. "Convert headings to normal text, or items or text to headings.
  17920. If there is no active region, only the current line is considered.
  17921. With a \\[universal-argument] prefix, convert the whole list at
  17922. point into heading.
  17923. In a region:
  17924. - If the first non blank line is an headline, remove the stars
  17925. from all headlines in the region.
  17926. - If it is a normal line turn each and every normal line (i.e. not an
  17927. heading or an item) in the region into a heading.
  17928. - If it is a plain list item, turn all plain list items into headings.
  17929. When converting a line into a heading, the number of stars is chosen
  17930. such that the lines become children of the current entry. However,
  17931. when a prefix argument is given, its value determines the number of
  17932. stars to add."
  17933. (interactive "P")
  17934. (let ((skip-blanks
  17935. (function
  17936. ;; Return beginning of first non-blank line, starting from
  17937. ;; line at POS.
  17938. (lambda (pos)
  17939. (save-excursion
  17940. (goto-char pos)
  17941. (while (org-at-comment-p) (forward-line))
  17942. (skip-chars-forward " \r\t\n")
  17943. (point-at-bol)))))
  17944. beg end toggled)
  17945. ;; Determine boundaries of changes. If a universal prefix has
  17946. ;; been given, put the list in a region. If region ends at a bol,
  17947. ;; do not consider the last line to be in the region.
  17948. (when (and current-prefix-arg (org-at-item-p))
  17949. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17950. (org-mark-element))
  17951. (if (org-region-active-p)
  17952. (setq beg (funcall skip-blanks (region-beginning))
  17953. end (copy-marker (save-excursion
  17954. (goto-char (region-end))
  17955. (if (bolp) (point) (point-at-eol)))))
  17956. (setq beg (funcall skip-blanks (point-at-bol))
  17957. end (copy-marker (point-at-eol))))
  17958. ;; Ensure inline tasks don't count as headings.
  17959. (org-with-limited-levels
  17960. (save-excursion
  17961. (goto-char beg)
  17962. (cond
  17963. ;; Case 1. Started at an heading: de-star headings.
  17964. ((org-at-heading-p)
  17965. (while (< (point) end)
  17966. (when (org-at-heading-p t)
  17967. (looking-at org-outline-regexp) (replace-match "")
  17968. (setq toggled t))
  17969. (forward-line)))
  17970. ;; Case 2. Started at an item: change items into headlines.
  17971. ;; One star will be added by `org-list-to-subtree'.
  17972. ((org-at-item-p)
  17973. (let* ((stars (make-string
  17974. (if nstars
  17975. ;; subtract the star that will be added again by
  17976. ;; `org-list-to-subtree'
  17977. (1- (prefix-numeric-value current-prefix-arg))
  17978. (or (org-current-level) 0))
  17979. ?*))
  17980. (add-stars
  17981. (cond (nstars "") ; stars from prefix only
  17982. ((equal stars "") "") ; before first heading
  17983. (org-odd-levels-only "*") ; inside heading, odd
  17984. (t "")))) ; inside heading, oddeven
  17985. (while (< (point) end)
  17986. (when (org-at-item-p)
  17987. ;; Pay attention to cases when region ends before list.
  17988. (let* ((struct (org-list-struct))
  17989. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17990. (save-restriction
  17991. (narrow-to-region (point) list-end)
  17992. (insert
  17993. (org-list-to-subtree
  17994. (org-list-parse-list t)
  17995. '(:istart (concat stars add-stars (funcall get-stars depth))
  17996. :icount (concat stars add-stars (funcall get-stars depth)))))))
  17997. (setq toggled t))
  17998. (forward-line))))
  17999. ;; Case 3. Started at normal text: make every line an heading,
  18000. ;; skipping headlines and items.
  18001. (t (let* ((stars (make-string
  18002. (if nstars
  18003. (prefix-numeric-value current-prefix-arg)
  18004. (or (org-current-level) 0))
  18005. ?*))
  18006. (add-stars
  18007. (cond (nstars "") ; stars from prefix only
  18008. ((equal stars "") "*") ; before first heading
  18009. (org-odd-levels-only "**") ; inside heading, odd
  18010. (t "*"))) ; inside heading, oddeven
  18011. (rpl (concat stars add-stars " ")))
  18012. (while (< (point) end)
  18013. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18014. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18015. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18016. (forward-line)))))))
  18017. (unless toggled (message "Cannot toggle heading from here"))))
  18018. (defun org-meta-return (&optional arg)
  18019. "Insert a new heading or wrap a region in a table.
  18020. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18021. See the individual commands for more information."
  18022. (interactive "P")
  18023. (cond
  18024. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18025. ((or (org-at-drawer-p) (org-at-property-p))
  18026. (newline-and-indent))
  18027. ((org-at-table-p)
  18028. (call-interactively 'org-table-wrap-region))
  18029. (t (call-interactively 'org-insert-heading))))
  18030. ;;; Menu entries
  18031. (defsubst org-in-subtree-not-table-p ()
  18032. "Are we in a subtree and not in a table?"
  18033. (and (not (org-before-first-heading-p))
  18034. (not (org-at-table-p))))
  18035. ;; Define the Org-mode menus
  18036. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18037. '("Tbl"
  18038. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18039. ["Next Field" org-cycle (org-at-table-p)]
  18040. ["Previous Field" org-shifttab (org-at-table-p)]
  18041. ["Next Row" org-return (org-at-table-p)]
  18042. "--"
  18043. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18044. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18045. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18046. "--"
  18047. ("Column"
  18048. ["Move Column Left" org-metaleft (org-at-table-p)]
  18049. ["Move Column Right" org-metaright (org-at-table-p)]
  18050. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18051. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18052. ("Row"
  18053. ["Move Row Up" org-metaup (org-at-table-p)]
  18054. ["Move Row Down" org-metadown (org-at-table-p)]
  18055. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18056. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18057. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18058. "--"
  18059. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18060. ("Rectangle"
  18061. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18062. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18063. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18064. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18065. "--"
  18066. ("Calculate"
  18067. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18068. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18069. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18070. "--"
  18071. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18072. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18073. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18074. "--"
  18075. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18076. "--"
  18077. ["Sum Column/Rectangle" org-table-sum
  18078. (or (org-at-table-p) (org-region-active-p))]
  18079. ["Which Column?" org-table-current-column (org-at-table-p)])
  18080. ["Debug Formulas"
  18081. org-table-toggle-formula-debugger
  18082. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18083. ["Show Col/Row Numbers"
  18084. org-table-toggle-coordinate-overlays
  18085. :style toggle
  18086. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18087. "--"
  18088. ["Create" org-table-create (and (not (org-at-table-p))
  18089. org-enable-table-editor)]
  18090. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18091. ["Import from File" org-table-import (not (org-at-table-p))]
  18092. ["Export to File" org-table-export (org-at-table-p)]
  18093. "--"
  18094. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18095. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18096. '("Org"
  18097. ("Show/Hide"
  18098. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18099. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18100. ["Sparse Tree..." org-sparse-tree t]
  18101. ["Reveal Context" org-reveal t]
  18102. ["Show All" show-all t]
  18103. "--"
  18104. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18105. "--"
  18106. ["New Heading" org-insert-heading t]
  18107. ("Navigate Headings"
  18108. ["Up" outline-up-heading t]
  18109. ["Next" outline-next-visible-heading t]
  18110. ["Previous" outline-previous-visible-heading t]
  18111. ["Next Same Level" outline-forward-same-level t]
  18112. ["Previous Same Level" outline-backward-same-level t]
  18113. "--"
  18114. ["Jump" org-goto t])
  18115. ("Edit Structure"
  18116. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18117. "--"
  18118. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18119. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18120. "--"
  18121. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18122. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18123. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18124. "--"
  18125. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18126. "--"
  18127. ["Copy visible text" org-copy-visible t]
  18128. "--"
  18129. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18130. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18131. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18132. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18133. "--"
  18134. ["Sort Region/Children" org-sort t]
  18135. "--"
  18136. ["Convert to odd levels" org-convert-to-odd-levels t]
  18137. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18138. ("Editing"
  18139. ["Emphasis..." org-emphasize t]
  18140. ["Edit Source Example" org-edit-special t]
  18141. "--"
  18142. ["Footnote new/jump" org-footnote-action t]
  18143. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18144. ("Archive"
  18145. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18146. "--"
  18147. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18148. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18149. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18150. )
  18151. "--"
  18152. ("Hyperlinks"
  18153. ["Store Link (Global)" org-store-link t]
  18154. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18155. ["Insert Link" org-insert-link t]
  18156. ["Follow Link" org-open-at-point t]
  18157. "--"
  18158. ["Next link" org-next-link t]
  18159. ["Previous link" org-previous-link t]
  18160. "--"
  18161. ["Descriptive Links"
  18162. org-toggle-link-display
  18163. :style radio
  18164. :selected org-descriptive-links
  18165. ]
  18166. ["Literal Links"
  18167. org-toggle-link-display
  18168. :style radio
  18169. :selected (not org-descriptive-links)])
  18170. "--"
  18171. ("TODO Lists"
  18172. ["TODO/DONE/-" org-todo t]
  18173. ("Select keyword"
  18174. ["Next keyword" org-shiftright (org-at-heading-p)]
  18175. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18176. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18177. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18178. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18179. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18180. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18181. "--"
  18182. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18183. :selected org-enforce-todo-dependencies :style toggle :active t]
  18184. "Settings for tree at point"
  18185. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18186. :selected (org-entry-get nil "ORDERED")
  18187. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18188. ["Do Children parallel" org-toggle-ordered-property :style radio
  18189. :selected (not (org-entry-get nil "ORDERED"))
  18190. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18191. "--"
  18192. ["Set Priority" org-priority t]
  18193. ["Priority Up" org-shiftup t]
  18194. ["Priority Down" org-shiftdown t]
  18195. "--"
  18196. ["Get news from all feeds" org-feed-update-all t]
  18197. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18198. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18199. ("TAGS and Properties"
  18200. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18201. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18202. "--"
  18203. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18204. ["Column view of properties" org-columns t]
  18205. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18206. ("Dates and Scheduling"
  18207. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18208. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18209. ("Change Date"
  18210. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18211. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18212. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18213. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18214. ["Compute Time Range" org-evaluate-time-range t]
  18215. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18216. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18217. "--"
  18218. ["Custom time format" org-toggle-time-stamp-overlays
  18219. :style radio :selected org-display-custom-times]
  18220. "--"
  18221. ["Goto Calendar" org-goto-calendar t]
  18222. ["Date from Calendar" org-date-from-calendar t]
  18223. "--"
  18224. ["Start/Restart Timer" org-timer-start t]
  18225. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18226. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18227. ["Insert Timer String" org-timer t]
  18228. ["Insert Timer Item" org-timer-item t])
  18229. ("Logging work"
  18230. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18231. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18232. ["Clock out" org-clock-out t]
  18233. ["Clock cancel" org-clock-cancel t]
  18234. "--"
  18235. ["Mark as default task" org-clock-mark-default-task t]
  18236. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18237. ["Goto running clock" org-clock-goto t]
  18238. "--"
  18239. ["Display times" org-clock-display t]
  18240. ["Create clock table" org-clock-report t]
  18241. "--"
  18242. ["Record DONE time"
  18243. (progn (setq org-log-done (not org-log-done))
  18244. (message "Switching to %s will %s record a timestamp"
  18245. (car org-done-keywords)
  18246. (if org-log-done "automatically" "not")))
  18247. :style toggle :selected org-log-done])
  18248. "--"
  18249. ["Agenda Command..." org-agenda t]
  18250. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18251. ("File List for Agenda")
  18252. ("Special views current file"
  18253. ["TODO Tree" org-show-todo-tree t]
  18254. ["Check Deadlines" org-check-deadlines t]
  18255. ["Timeline" org-timeline t]
  18256. ["Tags/Property tree" org-match-sparse-tree t])
  18257. "--"
  18258. ["Export/Publish..." org-export t]
  18259. ("LaTeX"
  18260. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18261. :selected org-cdlatex-mode]
  18262. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18263. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18264. ["Modify math symbol" org-cdlatex-math-modify
  18265. (org-inside-LaTeX-fragment-p)]
  18266. ["Insert citation" org-reftex-citation t]
  18267. "--"
  18268. ["Template for BEAMER" (progn (require 'org-beamer)
  18269. (org-insert-beamer-options-template)) t])
  18270. "--"
  18271. ("MobileOrg"
  18272. ["Push Files and Views" org-mobile-push t]
  18273. ["Get Captured and Flagged" org-mobile-pull t]
  18274. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18275. "--"
  18276. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18277. "--"
  18278. ("Documentation"
  18279. ["Show Version" org-version t]
  18280. ["Info Documentation" org-info t])
  18281. ("Customize"
  18282. ["Browse Org Group" org-customize t]
  18283. "--"
  18284. ["Expand This Menu" org-create-customize-menu
  18285. (fboundp 'customize-menu-create)])
  18286. ["Send bug report" org-submit-bug-report t]
  18287. "--"
  18288. ("Refresh/Reload"
  18289. ["Refresh setup current buffer" org-mode-restart t]
  18290. ["Reload Org (after update)" org-reload t]
  18291. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18292. ))
  18293. (defun org-info (&optional node)
  18294. "Read documentation for Org-mode in the info system.
  18295. With optional NODE, go directly to that node."
  18296. (interactive)
  18297. (info (format "(org)%s" (or node ""))))
  18298. ;;;###autoload
  18299. (defun org-submit-bug-report ()
  18300. "Submit a bug report on Org-mode via mail.
  18301. Don't hesitate to report any problems or inaccurate documentation.
  18302. If you don't have setup sending mail from (X)Emacs, please copy the
  18303. output buffer into your mail program, as it gives us important
  18304. information about your Org-mode version and configuration."
  18305. (interactive)
  18306. (require 'reporter)
  18307. (org-load-modules-maybe)
  18308. (org-require-autoloaded-modules)
  18309. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18310. (reporter-submit-bug-report
  18311. "emacs-orgmode@gnu.org"
  18312. (org-version nil 'full)
  18313. (let (list)
  18314. (save-window-excursion
  18315. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18316. (delete-other-windows)
  18317. (erase-buffer)
  18318. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18319. We would like to add your full Org-mode and Outline configuration to the
  18320. bug report. This greatly simplifies the work of the maintainer and
  18321. other experts on the mailing list.
  18322. HOWEVER, some variables you have customized may contain private
  18323. information. The names of customers, colleagues, or friends, might
  18324. appear in the form of file names, tags, todo states, or search strings.
  18325. If you answer yes to the prompt, you might want to check and remove
  18326. such private information before sending the email.")
  18327. (add-text-properties (point-min) (point-max) '(face org-warning))
  18328. (when (yes-or-no-p "Include your Org-mode configuration ")
  18329. (mapatoms
  18330. (lambda (v)
  18331. (and (boundp v)
  18332. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18333. (or (and (symbol-value v)
  18334. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18335. (and
  18336. (get v 'custom-type) (get v 'standard-value)
  18337. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18338. (push v list)))))
  18339. (kill-buffer (get-buffer "*Warn about privacy*"))
  18340. list))
  18341. nil nil
  18342. "Remember to cover the basics, that is, what you expected to happen and
  18343. what in fact did happen. You don't know how to make a good report? See
  18344. http://orgmode.org/manual/Feedback.html#Feedback
  18345. Your bug report will be posted to the Org-mode mailing list.
  18346. ------------------------------------------------------------------------")
  18347. (save-excursion
  18348. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18349. (replace-match "\\1Bug: \\3 [\\2]")))))
  18350. (defun org-install-agenda-files-menu ()
  18351. (let ((bl (buffer-list)))
  18352. (save-excursion
  18353. (while bl
  18354. (set-buffer (pop bl))
  18355. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18356. (when (derived-mode-p 'org-mode)
  18357. (easy-menu-change
  18358. '("Org") "File List for Agenda"
  18359. (append
  18360. (list
  18361. ["Edit File List" (org-edit-agenda-file-list) t]
  18362. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18363. ["Remove Current File from List" org-remove-file t]
  18364. ["Cycle through agenda files" org-cycle-agenda-files t]
  18365. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18366. "--")
  18367. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18368. ;;;; Documentation
  18369. (defun org-require-autoloaded-modules ()
  18370. (interactive)
  18371. (mapc 'require
  18372. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18373. org-docbook org-exp org-html org-icalendar
  18374. org-id org-latex
  18375. org-publish org-remember org-table
  18376. org-timer org-xoxo)))
  18377. ;;;###autoload
  18378. (defun org-reload (&optional uncompiled)
  18379. "Reload all org lisp files.
  18380. With prefix arg UNCOMPILED, load the uncompiled versions."
  18381. (interactive "P")
  18382. (require 'loadhist)
  18383. (let* ((org-dir (org-find-library-dir "org"))
  18384. (babel-dir (or (org-find-library-dir "ob") org-dir))
  18385. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18386. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18387. (remove-re (mapconcat 'identity
  18388. (mapcar (lambda (f) (concat "^" f "$"))
  18389. (list (if (featurep 'xemacs)
  18390. "org-colview"
  18391. "org-colview-xemacs")
  18392. "org" "org-loaddefs" "org-version"))
  18393. "\\|"))
  18394. (feats (delete-dups
  18395. (mapcar 'file-name-sans-extension
  18396. (mapcar 'file-name-nondirectory
  18397. (delq nil
  18398. (mapcar 'feature-file
  18399. features))))))
  18400. (lfeat (append
  18401. (sort
  18402. (setq feats
  18403. (delq nil (mapcar
  18404. (lambda (f)
  18405. (if (and (string-match feature-re f)
  18406. (not (string-match remove-re f)))
  18407. f nil))
  18408. feats)))
  18409. 'string-lessp)
  18410. (list "org-version" "org")))
  18411. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18412. (load-misses ()))
  18413. (setq load-misses
  18414. (delq 't
  18415. (mapcar (lambda (f)
  18416. (or
  18417. (load (concat org-dir f) 'noerror nil nil 'mustsuffix)
  18418. (unless (string= org-dir babel-dir)
  18419. (load (concat babel-dir f) 'noerror nil nil 'mustsuffix))
  18420. (unless (string= org-dir contrib-dir)
  18421. (load (concat contrib-dir f) 'noerror nil nil 'mustsuffix))
  18422. (and (load (concat (org-find-library-dir f) f) 'noerror nil nil 'mustsuffix)
  18423. ;; fallback to load-path, report as a possible error
  18424. (message "Had to fall back onto load-path, something is not quite right...")
  18425. f)))
  18426. lfeat)))
  18427. (if (not load-misses)
  18428. (message "Successfully reloaded Org\n%s" (org-version nil 'full))
  18429. (message "Some error occured while reloading Org features\n%s\nPlease check *Messages*!\n%s"
  18430. load-misses (org-version nil 'full)))))
  18431. ;;;###autoload
  18432. (defun org-customize ()
  18433. "Call the customize function with org as argument."
  18434. (interactive)
  18435. (org-load-modules-maybe)
  18436. (org-require-autoloaded-modules)
  18437. (customize-browse 'org))
  18438. (defun org-create-customize-menu ()
  18439. "Create a full customization menu for Org-mode, insert it into the menu."
  18440. (interactive)
  18441. (org-load-modules-maybe)
  18442. (org-require-autoloaded-modules)
  18443. (if (fboundp 'customize-menu-create)
  18444. (progn
  18445. (easy-menu-change
  18446. '("Org") "Customize"
  18447. `(["Browse Org group" org-customize t]
  18448. "--"
  18449. ,(customize-menu-create 'org)
  18450. ["Set" Custom-set t]
  18451. ["Save" Custom-save t]
  18452. ["Reset to Current" Custom-reset-current t]
  18453. ["Reset to Saved" Custom-reset-saved t]
  18454. ["Reset to Standard Settings" Custom-reset-standard t]))
  18455. (message "\"Org\"-menu now contains full customization menu"))
  18456. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18457. ;;;; Miscellaneous stuff
  18458. ;;; Generally useful functions
  18459. (defun org-get-at-bol (property)
  18460. "Get text property PROPERTY at beginning of line."
  18461. (get-text-property (point-at-bol) property))
  18462. (defun org-find-text-property-in-string (prop s)
  18463. "Return the first non-nil value of property PROP in string S."
  18464. (or (get-text-property 0 prop s)
  18465. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18466. prop s)))
  18467. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18468. "Display the given MESSAGE as a warning."
  18469. (if (fboundp 'display-warning)
  18470. (display-warning 'org message
  18471. (if (featurep 'xemacs) 'warning :warning))
  18472. (let ((buf (get-buffer-create "*Org warnings*")))
  18473. (with-current-buffer buf
  18474. (goto-char (point-max))
  18475. (insert "Warning (Org): " message)
  18476. (unless (bolp)
  18477. (newline)))
  18478. (display-buffer buf)
  18479. (sit-for 0))))
  18480. (defun org-eval (form)
  18481. "Eval FORM and return result."
  18482. (condition-case error
  18483. (eval form)
  18484. (error (format "%%![Error: %s]" error))))
  18485. (defun org-in-clocktable-p ()
  18486. "Check if the cursor is in a clocktable."
  18487. (let ((pos (point)) start)
  18488. (save-excursion
  18489. (end-of-line 1)
  18490. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18491. (setq start (match-beginning 0))
  18492. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18493. (>= (match-end 0) pos)
  18494. start))))
  18495. (defun org-in-commented-line ()
  18496. "Is point in a line starting with `#'?"
  18497. (equal (char-after (point-at-bol)) ?#))
  18498. (defun org-in-indented-comment-line ()
  18499. "Is point in a line starting with `#' after some white space?"
  18500. (save-excursion
  18501. (save-match-data
  18502. (goto-char (point-at-bol))
  18503. (looking-at "[ \t]*#"))))
  18504. (defun org-in-verbatim-emphasis ()
  18505. (save-match-data
  18506. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18507. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18508. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18509. (if (and marker (marker-buffer marker)
  18510. (buffer-live-p (marker-buffer marker)))
  18511. (progn
  18512. (org-pop-to-buffer-same-window (marker-buffer marker))
  18513. (if (or (> marker (point-max)) (< marker (point-min)))
  18514. (widen))
  18515. (goto-char marker)
  18516. (org-show-context 'org-goto))
  18517. (if bookmark
  18518. (bookmark-jump bookmark)
  18519. (error "Cannot find location"))))
  18520. (defun org-quote-csv-field (s)
  18521. "Quote field for inclusion in CSV material."
  18522. (if (string-match "[\",]" s)
  18523. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18524. s))
  18525. (defun org-force-self-insert (N)
  18526. "Needed to enforce self-insert under remapping."
  18527. (interactive "p")
  18528. (self-insert-command N))
  18529. (defun org-string-width (s)
  18530. "Compute width of string, ignoring invisible characters.
  18531. This ignores character with invisibility property `org-link', and also
  18532. characters with property `org-cwidth', because these will become invisible
  18533. upon the next fontification round."
  18534. (let (b l)
  18535. (when (or (eq t buffer-invisibility-spec)
  18536. (assq 'org-link buffer-invisibility-spec))
  18537. (while (setq b (text-property-any 0 (length s)
  18538. 'invisible 'org-link s))
  18539. (setq s (concat (substring s 0 b)
  18540. (substring s (or (next-single-property-change
  18541. b 'invisible s) (length s)))))))
  18542. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18543. (setq s (concat (substring s 0 b)
  18544. (substring s (or (next-single-property-change
  18545. b 'org-cwidth s) (length s))))))
  18546. (setq l (string-width s) b -1)
  18547. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18548. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18549. l))
  18550. (defun org-shorten-string (s maxlength)
  18551. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18552. If the string is shorter or has length MAXLENGTH, just return the
  18553. original string. If it is longer, the functions finds a space in the
  18554. string, breaks this string off at that locations and adds three dots
  18555. as ellipsis. Including the ellipsis, the string will not be longer
  18556. than MAXLENGTH. If finding a good breaking point in the string does
  18557. not work, the string is just chopped off in the middle of a word
  18558. if necessary."
  18559. (if (<= (length s) maxlength)
  18560. s
  18561. (let* ((n (max (- maxlength 4) 1))
  18562. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18563. (if (string-match re s)
  18564. (concat (match-string 1 s) "...")
  18565. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18566. (defun org-get-indentation (&optional line)
  18567. "Get the indentation of the current line, interpreting tabs.
  18568. When LINE is given, assume it represents a line and compute its indentation."
  18569. (if line
  18570. (if (string-match "^ *" (org-remove-tabs line))
  18571. (match-end 0))
  18572. (save-excursion
  18573. (beginning-of-line 1)
  18574. (skip-chars-forward " \t")
  18575. (current-column))))
  18576. (defun org-get-string-indentation (s)
  18577. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18578. (let ((n -1) (i 0) (w tab-width) c)
  18579. (catch 'exit
  18580. (while (< (setq n (1+ n)) (length s))
  18581. (setq c (aref s n))
  18582. (cond ((= c ?\ ) (setq i (1+ i)))
  18583. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18584. (t (throw 'exit t)))))
  18585. i))
  18586. (defun org-remove-tabs (s &optional width)
  18587. "Replace tabulators in S with spaces.
  18588. Assumes that s is a single line, starting in column 0."
  18589. (setq width (or width tab-width))
  18590. (while (string-match "\t" s)
  18591. (setq s (replace-match
  18592. (make-string
  18593. (- (* width (/ (+ (match-beginning 0) width) width))
  18594. (match-beginning 0)) ?\ )
  18595. t t s)))
  18596. s)
  18597. (defun org-fix-indentation (line ind)
  18598. "Fix indentation in LINE.
  18599. IND is a cons cell with target and minimum indentation.
  18600. If the current indentation in LINE is smaller than the minimum,
  18601. leave it alone. If it is larger than ind, set it to the target."
  18602. (let* ((l (org-remove-tabs line))
  18603. (i (org-get-indentation l))
  18604. (i1 (car ind)) (i2 (cdr ind)))
  18605. (if (>= i i2) (setq l (substring line i2)))
  18606. (if (> i1 0)
  18607. (concat (make-string i1 ?\ ) l)
  18608. l)))
  18609. (defun org-remove-indentation (code &optional n)
  18610. "Remove the maximum common indentation from the lines in CODE.
  18611. N may optionally be the number of spaces to remove."
  18612. (with-temp-buffer
  18613. (insert code)
  18614. (org-do-remove-indentation n)
  18615. (buffer-string)))
  18616. (defun org-do-remove-indentation (&optional n)
  18617. "Remove the maximum common indentation from the buffer."
  18618. (untabify (point-min) (point-max))
  18619. (let ((min 10000) re)
  18620. (if n
  18621. (setq min n)
  18622. (goto-char (point-min))
  18623. (while (re-search-forward "^ *[^ \n]" nil t)
  18624. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18625. (unless (or (= min 0) (= min 10000))
  18626. (setq re (format "^ \\{%d\\}" min))
  18627. (goto-char (point-min))
  18628. (while (re-search-forward re nil t)
  18629. (replace-match "")
  18630. (end-of-line 1))
  18631. min)))
  18632. (defun org-fill-template (template alist)
  18633. "Find each %key of ALIST in TEMPLATE and replace it."
  18634. (let ((case-fold-search nil)
  18635. entry key value)
  18636. (setq alist (sort (copy-sequence alist)
  18637. (lambda (a b) (< (length (car a)) (length (car b))))))
  18638. (while (setq entry (pop alist))
  18639. (setq template
  18640. (replace-regexp-in-string
  18641. (concat "%" (regexp-quote (car entry)))
  18642. (or (cdr entry) "") template t t)))
  18643. template))
  18644. (defun org-base-buffer (buffer)
  18645. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18646. (if (not buffer)
  18647. buffer
  18648. (or (buffer-base-buffer buffer)
  18649. buffer)))
  18650. (defun org-trim (s)
  18651. "Remove whitespace at beginning and end of string."
  18652. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18653. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18654. s)
  18655. (defun org-wrap (string &optional width lines)
  18656. "Wrap string to either a number of lines, or a width in characters.
  18657. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18658. that costs. If there is a word longer than WIDTH, the text is actually
  18659. wrapped to the length of that word.
  18660. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18661. many lines, whatever width that takes.
  18662. The return value is a list of lines, without newlines at the end."
  18663. (let* ((words (org-split-string string "[ \t\n]+"))
  18664. (maxword (apply 'max (mapcar 'org-string-width words)))
  18665. w ll)
  18666. (cond (width
  18667. (org-do-wrap words (max maxword width)))
  18668. (lines
  18669. (setq w maxword)
  18670. (setq ll (org-do-wrap words maxword))
  18671. (if (<= (length ll) lines)
  18672. ll
  18673. (setq ll words)
  18674. (while (> (length ll) lines)
  18675. (setq w (1+ w))
  18676. (setq ll (org-do-wrap words w)))
  18677. ll))
  18678. (t (error "Cannot wrap this")))))
  18679. (defun org-do-wrap (words width)
  18680. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18681. (let (lines line)
  18682. (while words
  18683. (setq line (pop words))
  18684. (while (and words (< (+ (length line) (length (car words))) width))
  18685. (setq line (concat line " " (pop words))))
  18686. (setq lines (push line lines)))
  18687. (nreverse lines)))
  18688. (defun org-split-string (string &optional separators)
  18689. "Splits STRING into substrings at SEPARATORS.
  18690. No empty strings are returned if there are matches at the beginning
  18691. and end of string."
  18692. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18693. (start 0)
  18694. notfirst
  18695. (list nil))
  18696. (while (and (string-match rexp string
  18697. (if (and notfirst
  18698. (= start (match-beginning 0))
  18699. (< start (length string)))
  18700. (1+ start) start))
  18701. (< (match-beginning 0) (length string)))
  18702. (setq notfirst t)
  18703. (or (eq (match-beginning 0) 0)
  18704. (and (eq (match-beginning 0) (match-end 0))
  18705. (eq (match-beginning 0) start))
  18706. (setq list
  18707. (cons (substring string start (match-beginning 0))
  18708. list)))
  18709. (setq start (match-end 0)))
  18710. (or (eq start (length string))
  18711. (setq list
  18712. (cons (substring string start)
  18713. list)))
  18714. (nreverse list)))
  18715. (defun org-quote-vert (s)
  18716. "Replace \"|\" with \"\\vert\"."
  18717. (while (string-match "|" s)
  18718. (setq s (replace-match "\\vert" t t s)))
  18719. s)
  18720. (defun org-uuidgen-p (s)
  18721. "Is S an ID created by UUIDGEN?"
  18722. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18723. (defun org-in-src-block-p nil
  18724. "Whether point is in a code source block."
  18725. (let (ov)
  18726. (when (setq ov (overlays-at (point)))
  18727. (memq 'org-block-background
  18728. (overlay-properties
  18729. (car ov))))))
  18730. (defun org-context ()
  18731. "Return a list of contexts of the current cursor position.
  18732. If several contexts apply, all are returned.
  18733. Each context entry is a list with a symbol naming the context, and
  18734. two positions indicating start and end of the context. Possible
  18735. contexts are:
  18736. :headline anywhere in a headline
  18737. :headline-stars on the leading stars in a headline
  18738. :todo-keyword on a TODO keyword (including DONE) in a headline
  18739. :tags on the TAGS in a headline
  18740. :priority on the priority cookie in a headline
  18741. :item on the first line of a plain list item
  18742. :item-bullet on the bullet/number of a plain list item
  18743. :checkbox on the checkbox in a plain list item
  18744. :table in an org-mode table
  18745. :table-special on a special filed in a table
  18746. :table-table in a table.el table
  18747. :clocktable in a clocktable
  18748. :src-block in a source block
  18749. :link on a hyperlink
  18750. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18751. :target on a <<target>>
  18752. :radio-target on a <<<radio-target>>>
  18753. :latex-fragment on a LaTeX fragment
  18754. :latex-preview on a LaTeX fragment with overlaid preview image
  18755. This function expects the position to be visible because it uses font-lock
  18756. faces as a help to recognize the following contexts: :table-special, :link,
  18757. and :keyword."
  18758. (let* ((f (get-text-property (point) 'face))
  18759. (faces (if (listp f) f (list f)))
  18760. (case-fold-search t)
  18761. (p (point)) clist o)
  18762. ;; First the large context
  18763. (cond
  18764. ((org-at-heading-p t)
  18765. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18766. (when (progn
  18767. (beginning-of-line 1)
  18768. (looking-at org-todo-line-tags-regexp))
  18769. (push (org-point-in-group p 1 :headline-stars) clist)
  18770. (push (org-point-in-group p 2 :todo-keyword) clist)
  18771. (push (org-point-in-group p 4 :tags) clist))
  18772. (goto-char p)
  18773. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18774. (if (looking-at "\\[#[A-Z0-9]\\]")
  18775. (push (org-point-in-group p 0 :priority) clist)))
  18776. ((org-at-item-p)
  18777. (push (org-point-in-group p 2 :item-bullet) clist)
  18778. (push (list :item (point-at-bol)
  18779. (save-excursion (org-end-of-item) (point)))
  18780. clist)
  18781. (and (org-at-item-checkbox-p)
  18782. (push (org-point-in-group p 0 :checkbox) clist)))
  18783. ((org-at-table-p)
  18784. (push (list :table (org-table-begin) (org-table-end)) clist)
  18785. (if (memq 'org-formula faces)
  18786. (push (list :table-special
  18787. (previous-single-property-change p 'face)
  18788. (next-single-property-change p 'face)) clist)))
  18789. ((org-at-table-p 'any)
  18790. (push (list :table-table) clist)))
  18791. (goto-char p)
  18792. (let ((case-fold-search t))
  18793. ;; New the "medium" contexts: clocktables, source blocks
  18794. (cond ((org-in-clocktable-p)
  18795. (push (list :clocktable
  18796. (and (or (looking-at "#\\+BEGIN: clocktable")
  18797. (search-backward "#+BEGIN: clocktable" nil t))
  18798. (match-beginning 0))
  18799. (and (re-search-forward "#\\+END:?" nil t)
  18800. (match-end 0))) clist))
  18801. ((org-in-src-block-p)
  18802. (push (list :src-block
  18803. (and (or (looking-at "#\\+BEGIN_SRC")
  18804. (search-backward "#+BEGIN_SRC" nil t))
  18805. (match-beginning 0))
  18806. (and (search-forward "#+END_SRC" nil t)
  18807. (match-beginning 0))) clist))))
  18808. (goto-char p)
  18809. ;; Now the small context
  18810. (cond
  18811. ((org-at-timestamp-p)
  18812. (push (org-point-in-group p 0 :timestamp) clist))
  18813. ((memq 'org-link faces)
  18814. (push (list :link
  18815. (previous-single-property-change p 'face)
  18816. (next-single-property-change p 'face)) clist))
  18817. ((memq 'org-special-keyword faces)
  18818. (push (list :keyword
  18819. (previous-single-property-change p 'face)
  18820. (next-single-property-change p 'face)) clist))
  18821. ((org-at-target-p)
  18822. (push (org-point-in-group p 0 :target) clist)
  18823. (goto-char (1- (match-beginning 0)))
  18824. (if (looking-at org-radio-target-regexp)
  18825. (push (org-point-in-group p 0 :radio-target) clist))
  18826. (goto-char p))
  18827. ((setq o (car (delq nil
  18828. (mapcar
  18829. (lambda (x)
  18830. (if (memq x org-latex-fragment-image-overlays) x))
  18831. (overlays-at (point))))))
  18832. (push (list :latex-fragment
  18833. (overlay-start o) (overlay-end o)) clist)
  18834. (push (list :latex-preview
  18835. (overlay-start o) (overlay-end o)) clist))
  18836. ((org-inside-LaTeX-fragment-p)
  18837. ;; FIXME: positions wrong.
  18838. (push (list :latex-fragment (point) (point)) clist)))
  18839. (setq clist (nreverse (delq nil clist)))
  18840. clist))
  18841. ;; FIXME: Compare with at-regexp-p Do we need both?
  18842. (defun org-in-regexp (re &optional nlines visually)
  18843. "Check if point is inside a match of regexp.
  18844. Normally only the current line is checked, but you can include NLINES extra
  18845. lines both before and after point into the search.
  18846. If VISUALLY is set, require that the cursor is not after the match but
  18847. really on, so that the block visually is on the match."
  18848. (catch 'exit
  18849. (let ((pos (point))
  18850. (eol (point-at-eol (+ 1 (or nlines 0))))
  18851. (inc (if visually 1 0)))
  18852. (save-excursion
  18853. (beginning-of-line (- 1 (or nlines 0)))
  18854. (while (re-search-forward re eol t)
  18855. (if (and (<= (match-beginning 0) pos)
  18856. (>= (+ inc (match-end 0)) pos))
  18857. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18858. (defun org-at-regexp-p (regexp)
  18859. "Is point inside a match of REGEXP in the current line?"
  18860. (catch 'exit
  18861. (save-excursion
  18862. (let ((pos (point)) (end (point-at-eol)))
  18863. (beginning-of-line 1)
  18864. (while (re-search-forward regexp end t)
  18865. (if (and (<= (match-beginning 0) pos)
  18866. (>= (match-end 0) pos))
  18867. (throw 'exit t)))
  18868. nil))))
  18869. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18870. "Non-nil when point is between matches of START-RE and END-RE.
  18871. Also return a non-nil value when point is on one of the matches.
  18872. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18873. buffer positions. Default values are the positions of headlines
  18874. surrounding the point.
  18875. The functions returns a cons cell whose car (resp. cdr) is the
  18876. position before START-RE (resp. after END-RE)."
  18877. (save-match-data
  18878. (let ((pos (point))
  18879. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18880. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18881. beg end)
  18882. (save-excursion
  18883. ;; Point is on a block when on START-RE or if START-RE can be
  18884. ;; found before it...
  18885. (and (or (org-at-regexp-p start-re)
  18886. (re-search-backward start-re limit-up t))
  18887. (setq beg (match-beginning 0))
  18888. ;; ... and END-RE after it...
  18889. (goto-char (match-end 0))
  18890. (re-search-forward end-re limit-down t)
  18891. (> (setq end (match-end 0)) pos)
  18892. ;; ... without another START-RE in-between.
  18893. (goto-char (match-beginning 0))
  18894. (not (re-search-backward start-re (1+ beg) t))
  18895. ;; Return value.
  18896. (cons beg end))))))
  18897. (defun org-in-block-p (names)
  18898. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18899. NAMES is a list of strings containing names of blocks.
  18900. Return first block name matched, or nil. Beware that in case of
  18901. nested blocks, the returned name may not belong to the closest
  18902. block from point."
  18903. (save-match-data
  18904. (catch 'exit
  18905. (let ((case-fold-search t)
  18906. (lim-up (save-excursion (outline-previous-heading)))
  18907. (lim-down (save-excursion (outline-next-heading))))
  18908. (mapc (lambda (name)
  18909. (let ((n (regexp-quote name)))
  18910. (when (org-between-regexps-p
  18911. (concat "^[ \t]*#\\+begin_" n)
  18912. (concat "^[ \t]*#\\+end_" n)
  18913. lim-up lim-down)
  18914. (throw 'exit n))))
  18915. names))
  18916. nil)))
  18917. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18918. "Call `multi-occur' with buffers for all agenda files."
  18919. (interactive "sOrg-files matching: \np")
  18920. (let* ((files (org-agenda-files))
  18921. (tnames (mapcar 'file-truename files))
  18922. (extra org-agenda-text-search-extra-files)
  18923. f)
  18924. (when (eq (car extra) 'agenda-archives)
  18925. (setq extra (cdr extra))
  18926. (setq files (org-add-archive-files files)))
  18927. (while (setq f (pop extra))
  18928. (unless (member (file-truename f) tnames)
  18929. (add-to-list 'files f 'append)
  18930. (add-to-list 'tnames (file-truename f) 'append)))
  18931. (multi-occur
  18932. (mapcar (lambda (x)
  18933. (with-current-buffer
  18934. (or (get-file-buffer x) (find-file-noselect x))
  18935. (widen)
  18936. (current-buffer)))
  18937. files)
  18938. regexp)))
  18939. (if (boundp 'occur-mode-find-occurrence-hook)
  18940. ;; Emacs 23
  18941. (add-hook 'occur-mode-find-occurrence-hook
  18942. (lambda ()
  18943. (when (derived-mode-p 'org-mode)
  18944. (org-reveal))))
  18945. ;; Emacs 22
  18946. (defadvice occur-mode-goto-occurrence
  18947. (after org-occur-reveal activate)
  18948. (and (derived-mode-p 'org-mode) (org-reveal)))
  18949. (defadvice occur-mode-goto-occurrence-other-window
  18950. (after org-occur-reveal activate)
  18951. (and (derived-mode-p 'org-mode) (org-reveal)))
  18952. (defadvice occur-mode-display-occurrence
  18953. (after org-occur-reveal activate)
  18954. (when (derived-mode-p 'org-mode)
  18955. (let ((pos (occur-mode-find-occurrence)))
  18956. (with-current-buffer (marker-buffer pos)
  18957. (save-excursion
  18958. (goto-char pos)
  18959. (org-reveal)))))))
  18960. (defun org-occur-link-in-agenda-files ()
  18961. "Create a link and search for it in the agendas.
  18962. The link is not stored in `org-stored-links', it is just created
  18963. for the search purpose."
  18964. (interactive)
  18965. (let ((link (condition-case nil
  18966. (org-store-link nil)
  18967. (error "Unable to create a link to here"))))
  18968. (org-occur-in-agenda-files (regexp-quote link))))
  18969. (defun org-uniquify (list)
  18970. "Remove duplicate elements from LIST."
  18971. (let (res)
  18972. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18973. res))
  18974. (defun org-delete-all (elts list)
  18975. "Remove all elements in ELTS from LIST."
  18976. (while elts
  18977. (setq list (delete (pop elts) list)))
  18978. list)
  18979. (defun org-count (cl-item cl-seq)
  18980. "Count the number of occurrences of ITEM in SEQ.
  18981. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18982. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18983. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18984. (while (< cl-start cl-end)
  18985. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18986. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18987. (setq cl-start (1+ cl-start)))
  18988. cl-count))
  18989. (defun org-remove-if (predicate seq)
  18990. "Remove everything from SEQ that fulfills PREDICATE."
  18991. (let (res e)
  18992. (while seq
  18993. (setq e (pop seq))
  18994. (if (not (funcall predicate e)) (push e res)))
  18995. (nreverse res)))
  18996. (defun org-remove-if-not (predicate seq)
  18997. "Remove everything from SEQ that does not fulfill PREDICATE."
  18998. (let (res e)
  18999. (while seq
  19000. (setq e (pop seq))
  19001. (if (funcall predicate e) (push e res)))
  19002. (nreverse res)))
  19003. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19004. "Reduce two-argument FUNCTION across SEQ.
  19005. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19006. \":initial-value\" removed."
  19007. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19008. (cadr (memq :initial-value cl-keys)))
  19009. (cl-seq (pop cl-seq))
  19010. (t (funcall cl-func)))))
  19011. (while cl-seq
  19012. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19013. cl-accum))
  19014. (defun org-back-over-empty-lines ()
  19015. "Move backwards over whitespace, to the beginning of the first empty line.
  19016. Returns the number of empty lines passed."
  19017. (let ((pos (point)))
  19018. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19019. (skip-chars-backward " \t\n\r")
  19020. (unless (eobp)
  19021. (forward-line -1)))
  19022. (beginning-of-line 2)
  19023. (goto-char (min (point) pos))
  19024. (count-lines (point) pos)))
  19025. (defun org-skip-whitespace ()
  19026. (skip-chars-forward " \t\n\r"))
  19027. (defun org-point-in-group (point group &optional context)
  19028. "Check if POINT is in match-group GROUP.
  19029. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19030. match. If the match group does not exist or point is not inside it,
  19031. return nil."
  19032. (and (match-beginning group)
  19033. (>= point (match-beginning group))
  19034. (<= point (match-end group))
  19035. (if context
  19036. (list context (match-beginning group) (match-end group))
  19037. t)))
  19038. (defun org-switch-to-buffer-other-window (&rest args)
  19039. "Switch to buffer in a second window on the current frame.
  19040. In particular, do not allow pop-up frames.
  19041. Returns the newly created buffer."
  19042. (let (pop-up-frames special-display-buffer-names special-display-regexps
  19043. special-display-function)
  19044. (apply 'switch-to-buffer-other-window args)))
  19045. (defun org-combine-plists (&rest plists)
  19046. "Create a single property list from all plists in PLISTS.
  19047. The process starts by copying the first list, and then setting properties
  19048. from the other lists. Settings in the last list are the most significant
  19049. ones and overrule settings in the other lists."
  19050. (let ((rtn (copy-sequence (pop plists)))
  19051. p v ls)
  19052. (while plists
  19053. (setq ls (pop plists))
  19054. (while ls
  19055. (setq p (pop ls) v (pop ls))
  19056. (setq rtn (plist-put rtn p v))))
  19057. rtn))
  19058. (defun org-replace-escapes (string table)
  19059. "Replace %-escapes in STRING with values in TABLE.
  19060. TABLE is an association list with keys like \"%a\" and string values.
  19061. The sequences in STRING may contain normal field width and padding information,
  19062. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19063. so values can contain further %-escapes if they are define later in TABLE."
  19064. (let ((tbl (copy-alist table))
  19065. (case-fold-search nil)
  19066. (pchg 0)
  19067. e re rpl)
  19068. (while (setq e (pop tbl))
  19069. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19070. (when (and (cdr e) (string-match re (cdr e)))
  19071. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19072. (safe "SREF"))
  19073. (add-text-properties 0 3 (list 'sref sref) safe)
  19074. (setcdr e (replace-match safe t t (cdr e)))))
  19075. (while (string-match re string)
  19076. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19077. (cdr e)))
  19078. (setq string (replace-match rpl t t string))))
  19079. (while (setq pchg (next-property-change pchg string))
  19080. (let ((sref (get-text-property pchg 'sref string)))
  19081. (when (and sref (string-match "SREF" string pchg))
  19082. (setq string (replace-match sref t t string)))))
  19083. string))
  19084. (defun org-sublist (list start end)
  19085. "Return a section of LIST, from START to END.
  19086. Counting starts at 1."
  19087. (let (rtn (c start))
  19088. (setq list (nthcdr (1- start) list))
  19089. (while (and list (<= c end))
  19090. (push (pop list) rtn)
  19091. (setq c (1+ c)))
  19092. (nreverse rtn)))
  19093. (defun org-find-base-buffer-visiting (file)
  19094. "Like `find-buffer-visiting' but always return the base buffer and
  19095. not an indirect buffer."
  19096. (let ((buf (or (get-file-buffer file)
  19097. (find-buffer-visiting file))))
  19098. (if buf
  19099. (or (buffer-base-buffer buf) buf)
  19100. nil)))
  19101. (defun org-image-file-name-regexp (&optional extensions)
  19102. "Return regexp matching the file names of images.
  19103. If EXTENSIONS is given, only match these."
  19104. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19105. (image-file-name-regexp)
  19106. (let ((image-file-name-extensions
  19107. (or extensions
  19108. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19109. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19110. (concat "\\."
  19111. (regexp-opt (nconc (mapcar 'upcase
  19112. image-file-name-extensions)
  19113. image-file-name-extensions)
  19114. t)
  19115. "\\'"))))
  19116. (defun org-file-image-p (file &optional extensions)
  19117. "Return non-nil if FILE is an image."
  19118. (save-match-data
  19119. (string-match (org-image-file-name-regexp extensions) file)))
  19120. (defun org-get-cursor-date ()
  19121. "Return the date at cursor in as a time.
  19122. This works in the calendar and in the agenda, anywhere else it just
  19123. returns the current time."
  19124. (let (date day defd)
  19125. (cond
  19126. ((eq major-mode 'calendar-mode)
  19127. (setq date (calendar-cursor-to-date)
  19128. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19129. ((eq major-mode 'org-agenda-mode)
  19130. (setq day (get-text-property (point) 'day))
  19131. (if day
  19132. (setq date (calendar-gregorian-from-absolute day)
  19133. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19134. (nth 2 date))))))
  19135. (or defd (current-time))))
  19136. (defun org-mark-subtree (&optional up)
  19137. "Mark the current subtree.
  19138. This puts point at the start of the current subtree, and mark at
  19139. the end. If a numeric prefix UP is given, move up into the
  19140. hierarchy of headlines by UP levels before marking the subtree."
  19141. (interactive "P")
  19142. (org-with-limited-levels
  19143. (cond ((org-at-heading-p) (beginning-of-line))
  19144. ((org-before-first-heading-p) (error "Not in a subtree"))
  19145. (t (outline-previous-visible-heading 1))))
  19146. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19147. (if (org-called-interactively-p 'any)
  19148. (call-interactively 'org-mark-element)
  19149. (org-mark-element)))
  19150. ;;; Macros
  19151. ;; Macros are expanded with `org-macro-replace-all', which relies
  19152. ;; internally on `org-macro-expand'.
  19153. ;; Default templates for expansion are stored in the buffer-local
  19154. ;; variable `org-macro-templates'. This variable is updated by
  19155. ;; `org-macro-initialize-templates'.
  19156. ;; Along with macros defined through #+MACRO: keyword, default
  19157. ;; templates include the following hard-coded macros:
  19158. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19159. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19160. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19161. ;; {{{title}}} will also be provided.
  19162. (defvar org-macro-templates nil
  19163. "Alist containing all macro templates in current buffer.
  19164. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19165. stands for macro's name and template for its replacement value,
  19166. both as strings. This is an internal variable. Do not set it
  19167. directly, use instead:
  19168. #+MACRO: name template")
  19169. (make-variable-buffer-local 'org-macro-templates)
  19170. (defun org-macro-expand (macro templates)
  19171. "Return expanded MACRO, as a string.
  19172. MACRO is an object, obtained, for example, with
  19173. `org-element-context'. TEMPLATES is an alist of templates used
  19174. for expansion. See `org-macro-templates' for a buffer-local
  19175. default value. Return nil if no template was found."
  19176. (let ((template
  19177. ;; Macro names are case-insensitive.
  19178. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19179. (when template
  19180. (let ((value (replace-regexp-in-string
  19181. "\\$[0-9]+"
  19182. (lambda (arg)
  19183. (or (nth (1- (string-to-number (substring arg 1)))
  19184. (org-element-property :args macro))
  19185. ;; No argument provided: remove
  19186. ;; place-holder.
  19187. ""))
  19188. template)))
  19189. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19190. (when (string-match "\\`(eval\\>" value)
  19191. (setq value (eval (read value))))
  19192. ;; Return string.
  19193. (format "%s" (or value ""))))))
  19194. (defun org-macro-replace-all (templates)
  19195. "Replace all macros in current buffer by their expansion.
  19196. TEMPLATES is an alist of templates used for expansion. See
  19197. `org-macro-templates' for a buffer-local default value."
  19198. (save-excursion
  19199. (goto-char (point-min))
  19200. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19201. (let ((object (org-element-context)))
  19202. (when (eq (org-element-type object) 'macro)
  19203. (let ((value (org-macro-expand object templates)))
  19204. (when value
  19205. (delete-region
  19206. (org-element-property :begin object)
  19207. ;; Preserve white spaces after the macro.
  19208. (progn (goto-char (org-element-property :end object))
  19209. (skip-chars-backward " \t")
  19210. (point)))
  19211. ;; Leave point before replacement in case of recursive
  19212. ;; expansions.
  19213. (save-excursion (insert value)))))))))
  19214. (defun org-macro-initialize-templates ()
  19215. "Collect macro templates defined in current buffer.
  19216. Templates are stored in buffer-local variable
  19217. `org-macro-templates'. In addition to buffer-defined macros, the
  19218. function installs the following ones: \"property\", \"date\",
  19219. \"time\". and, if appropriate, \"input-file\" and
  19220. \"modification-time\"."
  19221. (let ((case-fold-search t)
  19222. (set-template
  19223. (lambda (cell)
  19224. ;; Add CELL to `org-macro-templates' if there's no
  19225. ;; association matching its name already. Otherwise,
  19226. ;; replace old association with the new one in that
  19227. ;; variable.
  19228. (let ((old-template (assoc (car cell) org-macro-templates)))
  19229. (if old-template (setcdr old-template (cdr cell))
  19230. (push cell org-macro-templates))))))
  19231. ;; Install buffer-local macros.
  19232. (org-with-wide-buffer
  19233. (goto-char (point-min))
  19234. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19235. (let ((element (org-element-at-point)))
  19236. (when (eq (org-element-type element) 'keyword)
  19237. (let ((value (org-element-property :value element)))
  19238. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19239. (funcall set-template
  19240. (cons (match-string 1 value)
  19241. (or (match-string 2 value) "")))))))))
  19242. ;; Install hard-coded macros.
  19243. (mapc (lambda (cell) (funcall set-template cell))
  19244. (list
  19245. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19246. (cons "time" "(eval (format-time-string \"$1\"))")))
  19247. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19248. (when (and visited-file (file-exists-p visited-file))
  19249. (mapc (lambda (cell) (funcall set-template cell))
  19250. (list
  19251. (cons "input-file" (file-name-nondirectory visited-file))
  19252. (cons "modification-time"
  19253. (format "(eval (format-time-string \"$1\" '%s))"
  19254. (prin1-to-string
  19255. (nth 5 (file-attributes visited-file)))))))))))
  19256. ;;; Indentation
  19257. (defun org-indent-line ()
  19258. "Indent line depending on context."
  19259. (interactive)
  19260. (let* ((pos (point))
  19261. (itemp (org-at-item-p))
  19262. (case-fold-search t)
  19263. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19264. (inline-task-p (and (featurep 'org-inlinetask)
  19265. (org-inlinetask-in-task-p)))
  19266. (inline-re (and inline-task-p
  19267. (org-inlinetask-outline-regexp)))
  19268. column)
  19269. (if (and orgstruct-is-++ (eq pos (point)))
  19270. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19271. (indent-according-to-mode))
  19272. (beginning-of-line 1)
  19273. (cond
  19274. ;; Headings
  19275. ((looking-at org-outline-regexp) (setq column 0))
  19276. ;; Included files
  19277. ((looking-at "#\\+include:") (setq column 0))
  19278. ;; Footnote definition
  19279. ((looking-at org-footnote-definition-re) (setq column 0))
  19280. ;; Literal examples
  19281. ((looking-at "[ \t]*:\\( \\|$\\)")
  19282. (setq column (org-get-indentation))) ; do nothing
  19283. ;; Lists
  19284. ((ignore-errors (goto-char (org-in-item-p)))
  19285. (setq column (if itemp
  19286. (org-get-indentation)
  19287. (org-list-item-body-column (point))))
  19288. (goto-char pos))
  19289. ;; Drawers
  19290. ((and (looking-at "[ \t]*:END:")
  19291. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19292. (save-excursion
  19293. (goto-char (1- (match-beginning 1)))
  19294. (setq column (current-column))))
  19295. ;; Special blocks
  19296. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19297. (save-excursion
  19298. (re-search-backward
  19299. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19300. (setq column (org-get-indentation (match-string 0))))
  19301. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19302. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19303. (save-excursion
  19304. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19305. (setq column
  19306. (cond ((equal (downcase (match-string 1)) "src")
  19307. ;; src blocks: let `org-edit-src-exit' handle them
  19308. (org-get-indentation))
  19309. ((equal (downcase (match-string 1)) "example")
  19310. (max (org-get-indentation)
  19311. (org-get-indentation (match-string 0))))
  19312. (t
  19313. (org-get-indentation (match-string 0))))))
  19314. ;; This line has nothing special, look at the previous relevant
  19315. ;; line to compute indentation
  19316. (t
  19317. (beginning-of-line 0)
  19318. (while (and (not (bobp))
  19319. (not (looking-at org-drawer-regexp))
  19320. ;; When point started in an inline task, do not move
  19321. ;; above task starting line.
  19322. (not (and inline-task-p (looking-at inline-re)))
  19323. ;; Skip drawers, blocks, empty lines, verbatim,
  19324. ;; comments, tables, footnotes definitions, lists,
  19325. ;; inline tasks.
  19326. (or (and (looking-at "[ \t]*:END:")
  19327. (re-search-backward org-drawer-regexp nil t))
  19328. (and (looking-at "[ \t]*#\\+end_")
  19329. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19330. (looking-at "[ \t]*[\n:#|]")
  19331. (looking-at org-footnote-definition-re)
  19332. (and (ignore-errors (goto-char (org-in-item-p)))
  19333. (goto-char
  19334. (org-list-get-top-point (org-list-struct))))
  19335. (and (not inline-task-p)
  19336. (featurep 'org-inlinetask)
  19337. (org-inlinetask-in-task-p)
  19338. (or (org-inlinetask-goto-beginning) t))))
  19339. (beginning-of-line 0))
  19340. (cond
  19341. ;; There was an heading above.
  19342. ((looking-at "\\*+[ \t]+")
  19343. (if (not org-adapt-indentation)
  19344. (setq column 0)
  19345. (goto-char (match-end 0))
  19346. (setq column (current-column))))
  19347. ;; A drawer had started and is unfinished
  19348. ((looking-at org-drawer-regexp)
  19349. (goto-char (1- (match-beginning 1)))
  19350. (setq column (current-column)))
  19351. ;; Else, nothing noticeable found: get indentation and go on.
  19352. (t (setq column (org-get-indentation))))))
  19353. ;; Now apply indentation and move cursor accordingly
  19354. (goto-char pos)
  19355. (if (<= (current-column) (current-indentation))
  19356. (org-indent-line-to column)
  19357. (save-excursion (org-indent-line-to column)))
  19358. ;; Special polishing for properties, see `org-property-format'
  19359. (setq column (current-column))
  19360. (beginning-of-line 1)
  19361. (if (looking-at
  19362. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19363. (replace-match (concat (match-string 1)
  19364. (format org-property-format
  19365. (match-string 2) (match-string 3)))
  19366. t t))
  19367. (org-move-to-column column))))
  19368. (defun org-indent-drawer ()
  19369. "Indent the drawer at point."
  19370. (interactive)
  19371. (let ((p (point))
  19372. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19373. (match-end 0)))
  19374. (folded
  19375. (save-excursion
  19376. (end-of-line)
  19377. (when (overlays-at (point))
  19378. (member 'invisible (overlay-properties
  19379. (car (overlays-at (point)))))))))
  19380. (when folded (org-cycle))
  19381. (indent-for-tab-command)
  19382. (while (and (move-beginning-of-line 2) (< (point) e))
  19383. (indent-for-tab-command))
  19384. (goto-char p)
  19385. (when folded (org-cycle)))
  19386. (message "Drawer at point indented"))
  19387. (defun org-indent-block ()
  19388. "Indent the block at point."
  19389. (interactive)
  19390. (let ((p (point))
  19391. (case-fold-search t)
  19392. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19393. (match-end 0)))
  19394. (folded
  19395. (save-excursion
  19396. (end-of-line)
  19397. (when (overlays-at (point))
  19398. (member 'invisible (overlay-properties
  19399. (car (overlays-at (point)))))))))
  19400. (when folded (org-cycle))
  19401. (indent-for-tab-command)
  19402. (while (and (move-beginning-of-line 2) (< (point) e))
  19403. (indent-for-tab-command))
  19404. (goto-char p)
  19405. (when folded (org-cycle)))
  19406. (message "Block at point indented"))
  19407. (defun org-indent-region (start end)
  19408. "Indent region."
  19409. (interactive "r")
  19410. (save-excursion
  19411. (let ((line-end (org-current-line end)))
  19412. (goto-char start)
  19413. (while (< (org-current-line) line-end)
  19414. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19415. (t (call-interactively 'org-indent-line)))
  19416. (move-beginning-of-line 2)))))
  19417. ;;; Filling
  19418. ;; We use our own fill-paragraph and auto-fill functions.
  19419. ;; `org-fill-paragraph' relies on adaptive filling and context
  19420. ;; checking. Appropriate `fill-prefix' is computed with
  19421. ;; `org-adaptive-fill-function'.
  19422. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19423. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19424. ;; `org-adaptive-fill-function' value. Internally,
  19425. ;; `org-comment-line-break-function' breaks the line.
  19426. ;; `org-setup-filling' installs filling and auto-filling related
  19427. ;; variables during `org-mode' initialization.
  19428. (defun org-setup-filling ()
  19429. (interactive)
  19430. ;; Prevent auto-fill from inserting unwanted new items.
  19431. (when (boundp 'fill-nobreak-predicate)
  19432. (org-set-local
  19433. 'fill-nobreak-predicate
  19434. (org-uniquify
  19435. (append fill-nobreak-predicate
  19436. '(org-fill-paragraph-separate-nobreak-p
  19437. org-fill-line-break-nobreak-p)))))
  19438. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19439. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19440. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19441. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19442. (defvar org-element-paragraph-separate) ; org-element.el
  19443. (defun org-fill-paragraph-separate-nobreak-p ()
  19444. "Non-nil when a line break at point would insert a new item."
  19445. (looking-at (substring org-element-paragraph-separate 1)))
  19446. (defun org-fill-line-break-nobreak-p ()
  19447. "Non-nil when a line break at point would create an Org line break."
  19448. (save-excursion
  19449. (skip-chars-backward "[ \t]")
  19450. (skip-chars-backward "\\\\")
  19451. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19452. (declare-function message-in-body-p "message" ())
  19453. (defvar org-element--affiliated-re) ; From org-element.el
  19454. (defun org-adaptive-fill-function ()
  19455. "Compute a fill prefix for the current line.
  19456. Return fill prefix, as a string, or nil if current line isn't
  19457. meant to be filled."
  19458. (org-with-wide-buffer
  19459. (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
  19460. ;; FIXME: This is really the job of orgstruct++-mode
  19461. (let* ((p (line-beginning-position))
  19462. (element (save-excursion (beginning-of-line)
  19463. (org-element-at-point)))
  19464. (type (org-element-type element))
  19465. (post-affiliated
  19466. (save-excursion
  19467. (goto-char (org-element-property :begin element))
  19468. (while (looking-at org-element--affiliated-re) (forward-line))
  19469. (point))))
  19470. (unless (< p post-affiliated)
  19471. (case type
  19472. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19473. (footnote-definition "")
  19474. ((item plain-list)
  19475. (make-string (org-list-item-body-column post-affiliated) ? ))
  19476. (paragraph
  19477. ;; Fill prefix is usually the same as the current line,
  19478. ;; except if the paragraph is at the beginning of an item.
  19479. (let ((parent (org-element-property :parent element)))
  19480. (cond ((eq (org-element-type parent) 'item)
  19481. (make-string (org-list-item-body-column
  19482. (org-element-property :begin parent))
  19483. ? ))
  19484. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19485. (match-string 0))
  19486. (t ""))))
  19487. (comment-block
  19488. ;; Only fill contents if P is within block boundaries.
  19489. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19490. (forward-line)
  19491. (point)))
  19492. (cend (save-excursion
  19493. (goto-char (org-element-property :end element))
  19494. (skip-chars-backward " \r\t\n")
  19495. (line-beginning-position))))
  19496. (when (and (>= p cbeg) (< p cend))
  19497. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19498. (match-string 0)
  19499. ""))))))))))
  19500. (declare-function message-goto-body "message" ())
  19501. (defvar message-cite-prefix-regexp) ; From message.el
  19502. (defvar org-element-all-objects) ; From org-element.el
  19503. (defun org-fill-paragraph (&optional justify)
  19504. "Fill element at point, when applicable.
  19505. This function only applies to comment blocks, comments, example
  19506. blocks and paragraphs. Also, as a special case, re-align table
  19507. when point is at one.
  19508. If JUSTIFY is non-nil (interactively, with prefix argument),
  19509. justify as well. If `sentence-end-double-space' is non-nil, then
  19510. period followed by one space does not end a sentence, so don't
  19511. break a line there. The variable `fill-column' controls the
  19512. width for filling.
  19513. For convenience, when point is at a plain list, an item or
  19514. a footnote definition, try to fill the first paragraph within."
  19515. ;; Falls back on message-fill-paragraph when necessary
  19516. (interactive)
  19517. (if (and (derived-mode-p 'message-mode)
  19518. (or (not (message-in-body-p))
  19519. (save-excursion (move-beginning-of-line 1)
  19520. (looking-at message-cite-prefix-regexp))))
  19521. (let ((fill-paragraph-function
  19522. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19523. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19524. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19525. (paragraph-separate
  19526. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19527. (fill-paragraph nil))
  19528. (save-excursion
  19529. ;; Move to end of line in order to get the first paragraph
  19530. ;; within a plain list or a footnote definition.
  19531. (end-of-line)
  19532. (let ((element (org-element-at-point)))
  19533. ;; First check if point is in a blank line at the beginning of
  19534. ;; the buffer. In that case, ignore filling.
  19535. (if (< (point) (org-element-property :begin element)) t
  19536. (case (org-element-type element)
  19537. ;; Align Org tables, leave table.el tables as-is.
  19538. (table-row (org-table-align) t)
  19539. (table
  19540. (when (eq (org-element-property :type element) 'org)
  19541. (org-table-align))
  19542. t)
  19543. (paragraph
  19544. ;; Paragraphs may contain `line-break' type objects.
  19545. (let ((beg (max (point-min)
  19546. (org-element-property :contents-begin element)))
  19547. (end (min (point-max)
  19548. (org-element-property :contents-end element))))
  19549. ;; Do nothing if point is at an affiliated keyword.
  19550. (if (< (point) beg) t
  19551. (when (derived-mode-p 'message-mode)
  19552. ;; In `message-mode', do not fill following
  19553. ;; citation in current paragraph nor text before
  19554. ;; message body.
  19555. (let ((body-start (save-excursion (message-goto-body))))
  19556. (when body-start (setq beg (max body-start beg))))
  19557. (when (save-excursion
  19558. (re-search-forward
  19559. (concat "^" message-cite-prefix-regexp) end t))
  19560. (setq end (match-beginning 0))))
  19561. ;; Fill paragraph, taking line breaks into
  19562. ;; consideration. For that, slice the paragraph
  19563. ;; using line breaks as separators, and fill the
  19564. ;; parts in reverse order to avoid messing with
  19565. ;; markers.
  19566. (save-excursion
  19567. (goto-char end)
  19568. (mapc
  19569. (lambda (pos)
  19570. (fill-region-as-paragraph pos (point) justify)
  19571. (goto-char pos))
  19572. ;; Find the list of ending positions for line
  19573. ;; breaks in the current paragraph. Add paragraph
  19574. ;; beginning to include first slice.
  19575. (nreverse
  19576. (cons
  19577. beg
  19578. (org-element-map
  19579. (org-element--parse-objects
  19580. beg end nil org-element-all-objects)
  19581. 'line-break
  19582. (lambda (lb) (org-element-property :end lb)))))))
  19583. t)))
  19584. ;; Contents of `comment-block' type elements should be
  19585. ;; filled as plain text, but only if point is within block
  19586. ;; markers.
  19587. (comment-block
  19588. (let* ((case-fold-search t)
  19589. (beg (save-excursion
  19590. (goto-char (org-element-property :begin element))
  19591. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19592. (forward-line)
  19593. (point)))
  19594. (end (save-excursion
  19595. (goto-char (org-element-property :end element))
  19596. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19597. (line-beginning-position))))
  19598. (when (and (>= (point) beg) (< (point) end))
  19599. (fill-region-as-paragraph
  19600. (save-excursion
  19601. (end-of-line)
  19602. (re-search-backward "^[ \t]*$" beg 'move)
  19603. (line-beginning-position))
  19604. (save-excursion
  19605. (beginning-of-line)
  19606. (re-search-forward "^[ \t]*$" end 'move)
  19607. (line-beginning-position))
  19608. justify)))
  19609. t)
  19610. ;; Fill comments.
  19611. (comment (fill-comment-paragraph justify))
  19612. ;; Ignore every other element.
  19613. (otherwise t)))))))
  19614. (defun org-auto-fill-function ()
  19615. "Auto-fill function."
  19616. ;; Check if auto-filling is meaningful.
  19617. (let ((fc (current-fill-column)))
  19618. (when (and fc (> (current-column) fc))
  19619. (let ((fill-prefix (org-adaptive-fill-function)))
  19620. (when fill-prefix (do-auto-fill))))))
  19621. (defun org-comment-line-break-function (&optional soft)
  19622. "Break line at point and indent, continuing comment if within one.
  19623. The inserted newline is marked hard if variable
  19624. `use-hard-newlines' is true, unless optional argument SOFT is
  19625. non-nil."
  19626. (if soft (insert-and-inherit ?\n) (newline 1))
  19627. (save-excursion (forward-char -1) (delete-horizontal-space))
  19628. (delete-horizontal-space)
  19629. (indent-to-left-margin)
  19630. (insert-before-markers-and-inherit fill-prefix))
  19631. ;;; Comments
  19632. ;; Org comments syntax is quite complex. It requires the entire line
  19633. ;; to be just a comment. Also, even with the right syntax at the
  19634. ;; beginning of line, some some elements (i.e. verse-block or
  19635. ;; example-block) don't accept comments. Usual Emacs comment commands
  19636. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19637. ;; Org still relies on `comment-dwim', but cannot trust
  19638. ;; `comment-only-p'. So, `comment-region-function' and
  19639. ;; `uncomment-region-function' both point
  19640. ;; to`org-comment-or-uncomment-region'. Eventually,
  19641. ;; `org-insert-comment' takes care of insertion of comments at the
  19642. ;; beginning of line.
  19643. ;; `org-setup-comments-handling' install comments related variables
  19644. ;; during `org-mode' initialization.
  19645. (defun org-setup-comments-handling ()
  19646. (interactive)
  19647. (org-set-local 'comment-use-syntax nil)
  19648. (org-set-local 'comment-start "# ")
  19649. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19650. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19651. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19652. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19653. (defun org-insert-comment ()
  19654. "Insert an empty comment above current line.
  19655. If the line is empty, insert comment at its beginning."
  19656. (beginning-of-line)
  19657. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19658. (org-indent-line)
  19659. (insert "# "))
  19660. (defvar comment-empty-lines) ; From newcomment.el.
  19661. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19662. "Comment or uncomment each non-blank line in the region.
  19663. Uncomment each non-blank line between BEG and END if it only
  19664. contains commented lines. Otherwise, comment them."
  19665. (save-restriction
  19666. ;; Restrict region
  19667. (narrow-to-region (save-excursion (goto-char beg)
  19668. (skip-chars-forward " \r\t\n" end)
  19669. (line-beginning-position))
  19670. (save-excursion (goto-char end)
  19671. (skip-chars-backward " \r\t\n" beg)
  19672. (line-end-position)))
  19673. (let ((uncommentp
  19674. ;; UNCOMMENTP is non-nil when every non blank line between
  19675. ;; BEG and END is a comment.
  19676. (save-excursion
  19677. (goto-char (point-min))
  19678. (while (and (not (eobp))
  19679. (let ((element (org-element-at-point)))
  19680. (and (eq (org-element-type element) 'comment)
  19681. (goto-char (min (point-max)
  19682. (org-element-property
  19683. :end element)))))))
  19684. (eobp))))
  19685. (if uncommentp
  19686. ;; Only blank lines and comments in region: uncomment it.
  19687. (save-excursion
  19688. (goto-char (point-min))
  19689. (while (not (eobp))
  19690. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19691. (replace-match "" nil nil nil 1))
  19692. (forward-line)))
  19693. ;; Comment each line in region.
  19694. (let ((min-indent (point-max)))
  19695. ;; First find the minimum indentation across all lines.
  19696. (save-excursion
  19697. (goto-char (point-min))
  19698. (while (and (not (eobp)) (not (zerop min-indent)))
  19699. (unless (looking-at "[ \t]*$")
  19700. (setq min-indent (min min-indent (current-indentation))))
  19701. (forward-line)))
  19702. ;; Then loop over all lines.
  19703. (save-excursion
  19704. (goto-char (point-min))
  19705. (while (not (eobp))
  19706. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19707. (org-move-to-column min-indent t)
  19708. (insert comment-start))
  19709. (forward-line))))))))
  19710. ;;; Other stuff.
  19711. (defun org-toggle-fixed-width-section (arg)
  19712. "Toggle the fixed-width export.
  19713. If there is no active region, the QUOTE keyword at the current headline is
  19714. inserted or removed. When present, it causes the text between this headline
  19715. and the next to be exported as fixed-width text, and unmodified.
  19716. If there is an active region, this command adds or removes a colon as the
  19717. first character of this line. If the first character of a line is a colon,
  19718. this line is also exported in fixed-width font."
  19719. (interactive "P")
  19720. (let* ((cc 0)
  19721. (regionp (org-region-active-p))
  19722. (beg (if regionp (region-beginning) (point)))
  19723. (end (if regionp (region-end)))
  19724. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19725. (case-fold-search nil)
  19726. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19727. off)
  19728. (if regionp
  19729. (save-excursion
  19730. (goto-char beg)
  19731. (setq cc (current-column))
  19732. (beginning-of-line 1)
  19733. (setq off (looking-at re))
  19734. (while (> nlines 0)
  19735. (setq nlines (1- nlines))
  19736. (beginning-of-line 1)
  19737. (cond
  19738. (arg
  19739. (org-move-to-column cc t)
  19740. (insert ": \n")
  19741. (forward-line -1))
  19742. ((and off (looking-at re))
  19743. (replace-match "" t t nil 1))
  19744. ((not off) (org-move-to-column cc t) (insert ": ")))
  19745. (forward-line 1)))
  19746. (save-excursion
  19747. (org-back-to-heading)
  19748. (cond
  19749. ((looking-at (format org-heading-keyword-regexp-format
  19750. org-quote-string))
  19751. (goto-char (match-end 1))
  19752. (looking-at (concat " +" org-quote-string))
  19753. (replace-match "" t t)
  19754. (when (eolp) (insert " ")))
  19755. ((looking-at org-outline-regexp)
  19756. (goto-char (match-end 0))
  19757. (insert org-quote-string " ")))))))
  19758. (defun org-reftex-citation ()
  19759. "Use reftex-citation to insert a citation into the buffer.
  19760. This looks for a line like
  19761. #+BIBLIOGRAPHY: foo plain option:-d
  19762. and derives from it that foo.bib is the bibliography file relevant
  19763. for this document. It then installs the necessary environment for RefTeX
  19764. to work in this buffer and calls `reftex-citation' to insert a citation
  19765. into the buffer.
  19766. Export of such citations to both LaTeX and HTML is handled by the contributed
  19767. package org-exp-bibtex by Taru Karttunen."
  19768. (interactive)
  19769. (let ((reftex-docstruct-symbol 'rds)
  19770. (reftex-cite-format "\\cite{%l}")
  19771. rds bib)
  19772. (save-excursion
  19773. (save-restriction
  19774. (widen)
  19775. (let ((case-fold-search t)
  19776. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19777. (if (not (save-excursion
  19778. (or (re-search-forward re nil t)
  19779. (re-search-backward re nil t))))
  19780. (error "No bibliography defined in file")
  19781. (setq bib (concat (match-string 1) ".bib")
  19782. rds (list (list 'bib bib)))))))
  19783. (call-interactively 'reftex-citation)))
  19784. ;;;; Functions extending outline functionality
  19785. (defun org-beginning-of-line (&optional arg)
  19786. "Go to the beginning of the current line. If that is invisible, continue
  19787. to a visible line beginning. This makes the function of C-a more intuitive.
  19788. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19789. first attempt, and only move to after the tags when the cursor is already
  19790. beyond the end of the headline."
  19791. (interactive "P")
  19792. (let ((pos (point))
  19793. (special (if (consp org-special-ctrl-a/e)
  19794. (car org-special-ctrl-a/e)
  19795. org-special-ctrl-a/e))
  19796. refpos)
  19797. (if (org-bound-and-true-p line-move-visual)
  19798. (beginning-of-visual-line 1)
  19799. (beginning-of-line 1))
  19800. (if (and arg (fboundp 'move-beginning-of-line))
  19801. (call-interactively 'move-beginning-of-line)
  19802. (if (bobp)
  19803. nil
  19804. (backward-char 1)
  19805. (if (org-truely-invisible-p)
  19806. (while (and (not (bobp)) (org-truely-invisible-p))
  19807. (backward-char 1)
  19808. (beginning-of-line 1))
  19809. (forward-char 1))))
  19810. (when special
  19811. (cond
  19812. ((and (looking-at org-complex-heading-regexp)
  19813. (= (char-after (match-end 1)) ?\ ))
  19814. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19815. (point-at-eol)))
  19816. (goto-char
  19817. (if (eq special t)
  19818. (cond ((> pos refpos) refpos)
  19819. ((= pos (point)) refpos)
  19820. (t (point)))
  19821. (cond ((> pos (point)) (point))
  19822. ((not (eq last-command this-command)) (point))
  19823. (t refpos)))))
  19824. ((org-at-item-p)
  19825. ;; Being at an item and not looking at an the item means point
  19826. ;; was previously moved to beginning of a visual line, which
  19827. ;; doesn't contain the item. Therefore, do nothing special,
  19828. ;; just stay here.
  19829. (when (looking-at org-list-full-item-re)
  19830. ;; Set special position at first white space character after
  19831. ;; bullet, and check-box, if any.
  19832. (let ((after-bullet
  19833. (let ((box (match-end 3)))
  19834. (if (not box) (match-end 1)
  19835. (let ((after (char-after box)))
  19836. (if (and after (= after ? )) (1+ box) box))))))
  19837. ;; Special case: Move point to special position when
  19838. ;; currently after it or at beginning of line.
  19839. (if (eq special t)
  19840. (when (or (> pos after-bullet) (= (point) pos))
  19841. (goto-char after-bullet))
  19842. ;; Reversed case: Move point to special position when
  19843. ;; point was already at beginning of line and command is
  19844. ;; repeated.
  19845. (when (and (= (point) pos) (eq last-command this-command))
  19846. (goto-char after-bullet))))))))
  19847. (org-no-warnings
  19848. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19849. (defun org-end-of-line (&optional arg)
  19850. "Go to the end of the line.
  19851. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19852. first attempt, and only move to after the tags when the cursor is already
  19853. beyond the end of the headline."
  19854. (interactive "P")
  19855. (let ((special (if (consp org-special-ctrl-a/e)
  19856. (cdr org-special-ctrl-a/e)
  19857. org-special-ctrl-a/e)))
  19858. (cond
  19859. ((or (not special) arg
  19860. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19861. (call-interactively
  19862. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19863. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19864. (t 'end-of-line))))
  19865. ((org-at-heading-p)
  19866. (let ((pos (point)))
  19867. (beginning-of-line 1)
  19868. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19869. (if (eq special t)
  19870. (if (or (< pos (match-beginning 1))
  19871. (= pos (match-end 0)))
  19872. (goto-char (match-beginning 1))
  19873. (goto-char (match-end 0)))
  19874. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19875. (goto-char (match-end 0))
  19876. (goto-char (match-beginning 1))))
  19877. (call-interactively (if (fboundp 'move-end-of-line)
  19878. 'move-end-of-line
  19879. 'end-of-line)))))
  19880. ((org-at-drawer-p)
  19881. (move-end-of-line 1)
  19882. (when (overlays-at (1- (point))) (backward-char 1)))
  19883. ;; At an item: Move before any hidden text.
  19884. (t (call-interactively
  19885. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19886. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19887. (t 'end-of-line)))))
  19888. (org-no-warnings
  19889. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19890. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19891. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19892. (defun org-backward-sentence (&optional arg)
  19893. "Go to beginning of sentence, or beginning of table field.
  19894. This will call `backward-sentence' or `org-table-beginning-of-field',
  19895. depending on context."
  19896. (interactive "P")
  19897. (cond
  19898. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19899. (t (call-interactively 'backward-sentence))))
  19900. (defun org-forward-sentence (&optional arg)
  19901. "Go to end of sentence, or end of table field.
  19902. This will call `forward-sentence' or `org-table-end-of-field',
  19903. depending on context."
  19904. (interactive "P")
  19905. (cond
  19906. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19907. (t (call-interactively 'forward-sentence))))
  19908. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19909. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19910. (defun org-kill-line (&optional arg)
  19911. "Kill line, to tags or end of line."
  19912. (interactive "P")
  19913. (cond
  19914. ((or (not org-special-ctrl-k)
  19915. (bolp)
  19916. (not (org-at-heading-p)))
  19917. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19918. org-ctrl-k-protect-subtree)
  19919. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19920. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19921. (error "C-k aborted - would kill hidden subtree")))
  19922. (call-interactively
  19923. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19924. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19925. (kill-region (point) (match-beginning 1))
  19926. (org-set-tags nil t))
  19927. (t (kill-region (point) (point-at-eol)))))
  19928. (define-key org-mode-map "\C-k" 'org-kill-line)
  19929. (defun org-yank (&optional arg)
  19930. "Yank. If the kill is a subtree, treat it specially.
  19931. This command will look at the current kill and check if is a single
  19932. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19933. cursor is at the beginning of a line or after the stars of a currently
  19934. empty headline, then the yank is handled specially. How exactly depends
  19935. on the value of the following variables, both set by default.
  19936. org-yank-folded-subtrees
  19937. When set, the subtree(s) will be folded after insertion, but only
  19938. if doing so would now swallow text after the yanked text.
  19939. org-yank-adjusted-subtrees
  19940. When set, the subtree will be promoted or demoted in order to
  19941. fit into the local outline tree structure, which means that the level
  19942. will be adjusted so that it becomes the smaller one of the two
  19943. *visible* surrounding headings.
  19944. Any prefix to this command will cause `yank' to be called directly with
  19945. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19946. will just
  19947. plainly yank the text as it is.
  19948. \[1] The test checks if the first non-white line is a heading
  19949. and if there are no other headings with fewer stars."
  19950. (interactive "P")
  19951. (org-yank-generic 'yank arg))
  19952. (defun org-yank-generic (command arg)
  19953. "Perform some yank-like command.
  19954. This function implements the behavior described in the `org-yank'
  19955. documentation. However, it has been generalized to work for any
  19956. interactive command with similar behavior."
  19957. ;; pretend to be command COMMAND
  19958. (setq this-command command)
  19959. (if arg
  19960. (call-interactively command)
  19961. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19962. (and (org-kill-is-subtree-p)
  19963. (or (bolp)
  19964. (and (looking-at "[ \t]*$")
  19965. (string-match
  19966. "\\`\\*+\\'"
  19967. (buffer-substring (point-at-bol) (point)))))))
  19968. swallowp)
  19969. (cond
  19970. ((and subtreep org-yank-folded-subtrees)
  19971. (let ((beg (point))
  19972. end)
  19973. (if (and subtreep org-yank-adjusted-subtrees)
  19974. (org-paste-subtree nil nil 'for-yank)
  19975. (call-interactively command))
  19976. (setq end (point))
  19977. (goto-char beg)
  19978. (when (and (bolp) subtreep
  19979. (not (setq swallowp
  19980. (org-yank-folding-would-swallow-text beg end))))
  19981. (org-with-limited-levels
  19982. (or (looking-at org-outline-regexp)
  19983. (re-search-forward org-outline-regexp-bol end t))
  19984. (while (and (< (point) end) (looking-at org-outline-regexp))
  19985. (hide-subtree)
  19986. (org-cycle-show-empty-lines 'folded)
  19987. (condition-case nil
  19988. (outline-forward-same-level 1)
  19989. (error (goto-char end))))))
  19990. (when swallowp
  19991. (message
  19992. "Inserted text not folded because that would swallow text"))
  19993. (goto-char end)
  19994. (skip-chars-forward " \t\n\r")
  19995. (beginning-of-line 1)
  19996. (push-mark beg 'nomsg)))
  19997. ((and subtreep org-yank-adjusted-subtrees)
  19998. (let ((beg (point-at-bol)))
  19999. (org-paste-subtree nil nil 'for-yank)
  20000. (push-mark beg 'nomsg)))
  20001. (t
  20002. (call-interactively command))))))
  20003. (defun org-yank-folding-would-swallow-text (beg end)
  20004. "Would hide-subtree at BEG swallow any text after END?"
  20005. (let (level)
  20006. (org-with-limited-levels
  20007. (save-excursion
  20008. (goto-char beg)
  20009. (when (or (looking-at org-outline-regexp)
  20010. (re-search-forward org-outline-regexp-bol end t))
  20011. (setq level (org-outline-level)))
  20012. (goto-char end)
  20013. (skip-chars-forward " \t\r\n\v\f")
  20014. (if (or (eobp)
  20015. (and (bolp) (looking-at org-outline-regexp)
  20016. (<= (org-outline-level) level)))
  20017. nil ; Nothing would be swallowed
  20018. t))))) ; something would swallow
  20019. (define-key org-mode-map "\C-y" 'org-yank)
  20020. (defun org-truely-invisible-p ()
  20021. "Check if point is at a character currently not visible.
  20022. This version does not only check the character property, but also
  20023. `visible-mode'."
  20024. ;; Early versions of noutline don't have `outline-invisible-p'.
  20025. (if (org-bound-and-true-p visible-mode)
  20026. nil
  20027. (outline-invisible-p)))
  20028. (defun org-invisible-p2 ()
  20029. "Check if point is at a character currently not visible."
  20030. (save-excursion
  20031. (if (and (eolp) (not (bobp))) (backward-char 1))
  20032. ;; Early versions of noutline don't have `outline-invisible-p'.
  20033. (outline-invisible-p)))
  20034. (defun org-back-to-heading (&optional invisible-ok)
  20035. "Call `outline-back-to-heading', but provide a better error message."
  20036. (condition-case nil
  20037. (outline-back-to-heading invisible-ok)
  20038. (error (error "Before first headline at position %d in buffer %s"
  20039. (point) (current-buffer)))))
  20040. (defun org-before-first-heading-p ()
  20041. "Before first heading?"
  20042. (save-excursion
  20043. (end-of-line)
  20044. (null (re-search-backward org-outline-regexp-bol nil t))))
  20045. (defun org-at-heading-p (&optional ignored)
  20046. (outline-on-heading-p t))
  20047. ;; Compatibility alias with Org versions < 7.8.03
  20048. (defalias 'org-on-heading-p 'org-at-heading-p)
  20049. (defun org-at-comment-p nil
  20050. "Is cursor in a line starting with a # character?"
  20051. (save-excursion
  20052. (beginning-of-line)
  20053. (looking-at "^#")))
  20054. (defun org-at-drawer-p nil
  20055. "Is cursor at a drawer keyword?"
  20056. (save-excursion
  20057. (move-beginning-of-line 1)
  20058. (looking-at org-drawer-regexp)))
  20059. (defun org-at-block-p nil
  20060. "Is cursor at a block keyword?"
  20061. (save-excursion
  20062. (move-beginning-of-line 1)
  20063. (looking-at org-block-regexp)))
  20064. (defun org-point-at-end-of-empty-headline ()
  20065. "If point is at the end of an empty headline, return t, else nil.
  20066. If the heading only contains a TODO keyword, it is still still considered
  20067. empty."
  20068. (and (looking-at "[ \t]*$")
  20069. (when org-todo-line-regexp
  20070. (save-excursion
  20071. (beginning-of-line 1)
  20072. (let ((case-fold-search nil))
  20073. (looking-at org-todo-line-regexp)
  20074. (string= (match-string 3) ""))))))
  20075. (defun org-at-heading-or-item-p ()
  20076. (or (org-at-heading-p) (org-at-item-p)))
  20077. (defun org-at-target-p ()
  20078. (or (org-in-regexp org-radio-target-regexp)
  20079. (org-in-regexp org-target-regexp)))
  20080. ;; Compatibility alias with Org versions < 7.8.03
  20081. (defalias 'org-on-target-p 'org-at-target-p)
  20082. (defun org-up-heading-all (arg)
  20083. "Move to the heading line of which the present line is a subheading.
  20084. This function considers both visible and invisible heading lines.
  20085. With argument, move up ARG levels."
  20086. (if (fboundp 'outline-up-heading-all)
  20087. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20088. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20089. (defun org-up-heading-safe ()
  20090. "Move to the heading line of which the present line is a subheading.
  20091. This version will not throw an error. It will return the level of the
  20092. headline found, or nil if no higher level is found.
  20093. Also, this function will be a lot faster than `outline-up-heading',
  20094. because it relies on stars being the outline starters. This can really
  20095. make a significant difference in outlines with very many siblings."
  20096. (let (start-level re)
  20097. (org-back-to-heading t)
  20098. (setq start-level (funcall outline-level))
  20099. (if (equal start-level 1)
  20100. nil
  20101. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20102. (if (re-search-backward re nil t)
  20103. (funcall outline-level)))))
  20104. (defun org-first-sibling-p ()
  20105. "Is this heading the first child of its parents?"
  20106. (interactive)
  20107. (let ((re org-outline-regexp-bol)
  20108. level l)
  20109. (unless (org-at-heading-p t)
  20110. (error "Not at a heading"))
  20111. (setq level (funcall outline-level))
  20112. (save-excursion
  20113. (if (not (re-search-backward re nil t))
  20114. t
  20115. (setq l (funcall outline-level))
  20116. (< l level)))))
  20117. (defun org-goto-sibling (&optional previous)
  20118. "Goto the next sibling, even if it is invisible.
  20119. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20120. when a sibling was found. When none is found, return nil and don't
  20121. move point."
  20122. (let ((fun (if previous 're-search-backward 're-search-forward))
  20123. (pos (point))
  20124. (re org-outline-regexp-bol)
  20125. level l)
  20126. (when (condition-case nil (org-back-to-heading t) (error nil))
  20127. (setq level (funcall outline-level))
  20128. (catch 'exit
  20129. (or previous (forward-char 1))
  20130. (while (funcall fun re nil t)
  20131. (setq l (funcall outline-level))
  20132. (when (< l level) (goto-char pos) (throw 'exit nil))
  20133. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20134. (goto-char pos)
  20135. nil))))
  20136. (defun org-show-siblings ()
  20137. "Show all siblings of the current headline."
  20138. (save-excursion
  20139. (while (org-goto-sibling) (org-flag-heading nil)))
  20140. (save-excursion
  20141. (while (org-goto-sibling 'previous)
  20142. (org-flag-heading nil))))
  20143. (defun org-goto-first-child ()
  20144. "Goto the first child, even if it is invisible.
  20145. Return t when a child was found. Otherwise don't move point and
  20146. return nil."
  20147. (let (level (pos (point)) (re org-outline-regexp-bol))
  20148. (when (condition-case nil (org-back-to-heading t) (error nil))
  20149. (setq level (outline-level))
  20150. (forward-char 1)
  20151. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20152. (progn (goto-char (match-beginning 0)) t)
  20153. (goto-char pos) nil))))
  20154. (defun org-show-hidden-entry ()
  20155. "Show an entry where even the heading is hidden."
  20156. (save-excursion
  20157. (org-show-entry)))
  20158. (defun org-flag-heading (flag &optional entry)
  20159. "Flag the current heading. FLAG non-nil means make invisible.
  20160. When ENTRY is non-nil, show the entire entry."
  20161. (save-excursion
  20162. (org-back-to-heading t)
  20163. ;; Check if we should show the entire entry
  20164. (if entry
  20165. (progn
  20166. (org-show-entry)
  20167. (save-excursion
  20168. (and (outline-next-heading)
  20169. (org-flag-heading nil))))
  20170. (outline-flag-region (max (point-min) (1- (point)))
  20171. (save-excursion (outline-end-of-heading) (point))
  20172. flag))))
  20173. (defun org-get-next-sibling ()
  20174. "Move to next heading of the same level, and return point.
  20175. If there is no such heading, return nil.
  20176. This is like outline-next-sibling, but invisible headings are ok."
  20177. (let ((level (funcall outline-level)))
  20178. (outline-next-heading)
  20179. (while (and (not (eobp)) (> (funcall outline-level) level))
  20180. (outline-next-heading))
  20181. (if (or (eobp) (< (funcall outline-level) level))
  20182. nil
  20183. (point))))
  20184. (defun org-get-last-sibling ()
  20185. "Move to previous heading of the same level, and return point.
  20186. If there is no such heading, return nil."
  20187. (let ((opoint (point))
  20188. (level (funcall outline-level)))
  20189. (outline-previous-heading)
  20190. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20191. (while (and (> (funcall outline-level) level)
  20192. (not (bobp)))
  20193. (outline-previous-heading))
  20194. (if (< (funcall outline-level) level)
  20195. nil
  20196. (point)))))
  20197. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20198. "Goto to the end of a subtree."
  20199. ;; This contains an exact copy of the original function, but it uses
  20200. ;; `org-back-to-heading', to make it work also in invisible
  20201. ;; trees. And is uses an invisible-ok argument.
  20202. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20203. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20204. ;; with many children and grandchildren etc, this can be much faster
  20205. ;; than the outline version.
  20206. (org-back-to-heading invisible-ok)
  20207. (let ((first t)
  20208. (level (funcall outline-level)))
  20209. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20210. ;; A true heading (not a plain list item), in Org-mode
  20211. ;; This means we can easily find the end by looking
  20212. ;; only for the right number of stars. Using a regexp to do
  20213. ;; this is so much faster than using a Lisp loop.
  20214. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20215. (forward-char 1)
  20216. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20217. ;; something else, do it the slow way
  20218. (while (and (not (eobp))
  20219. (or first (> (funcall outline-level) level)))
  20220. (setq first nil)
  20221. (outline-next-heading)))
  20222. (unless to-heading
  20223. (if (memq (preceding-char) '(?\n ?\^M))
  20224. (progn
  20225. ;; Go to end of line before heading
  20226. (forward-char -1)
  20227. (if (memq (preceding-char) '(?\n ?\^M))
  20228. ;; leave blank line before heading
  20229. (forward-char -1))))))
  20230. (point))
  20231. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20232. "Use Org version in org-mode, for dramatic speed-up."
  20233. (if (derived-mode-p 'org-mode)
  20234. (progn
  20235. (org-end-of-subtree nil t)
  20236. (unless (eobp) (backward-char 1)))
  20237. ad-do-it))
  20238. (defun org-end-of-meta-data-and-drawers ()
  20239. "Jump to the first text after meta data and drawers in the current entry.
  20240. This will move over empty lines, lines with planning time stamps,
  20241. clocking lines, and drawers."
  20242. (org-back-to-heading t)
  20243. (let ((end (save-excursion (outline-next-heading) (point)))
  20244. (re (concat "\\(" org-drawer-regexp "\\)"
  20245. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20246. (forward-line 1)
  20247. (while (re-search-forward re end t)
  20248. (if (not (match-end 1))
  20249. ;; empty or planning line
  20250. (forward-line 1)
  20251. ;; a drawer, find the end
  20252. (re-search-forward "^[ \t]*:END:" end 'move)
  20253. (forward-line 1)))
  20254. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20255. (point)))
  20256. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20257. "Move forward to the arg'th subheading at same level as this one.
  20258. Stop at the first and last subheadings of a superior heading.
  20259. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20260. non-nil it will also look at invisible ones."
  20261. (interactive "p")
  20262. (org-back-to-heading invisible-ok)
  20263. (org-at-heading-p)
  20264. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20265. (re (format "^\\*\\{1,%d\\} " level))
  20266. l)
  20267. (forward-char 1)
  20268. (while (> arg 0)
  20269. (while (and (re-search-forward re nil 'move)
  20270. (setq l (- (match-end 0) (match-beginning 0) 1))
  20271. (= l level)
  20272. (not invisible-ok)
  20273. (progn (backward-char 1) (outline-invisible-p)))
  20274. (if (< l level) (setq arg 1)))
  20275. (setq arg (1- arg)))
  20276. (beginning-of-line 1)))
  20277. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20278. "Move backward to the arg'th subheading at same level as this one.
  20279. Stop at the first and last subheadings of a superior heading."
  20280. (interactive "p")
  20281. (org-back-to-heading)
  20282. (org-at-heading-p)
  20283. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20284. (re (format "^\\*\\{1,%d\\} " level))
  20285. l)
  20286. (while (> arg 0)
  20287. (while (and (re-search-backward re nil 'move)
  20288. (setq l (- (match-end 0) (match-beginning 0) 1))
  20289. (= l level)
  20290. (not invisible-ok)
  20291. (outline-invisible-p))
  20292. (if (< l level) (setq arg 1)))
  20293. (setq arg (1- arg)))))
  20294. (defun org-forward-element ()
  20295. "Move forward by one element.
  20296. Move to the next element at the same level, when possible."
  20297. (interactive)
  20298. (cond ((eobp) (error "Cannot move further down"))
  20299. ((org-with-limited-levels (org-at-heading-p))
  20300. (let ((origin (point)))
  20301. (org-forward-heading-same-level 1)
  20302. (unless (org-with-limited-levels (org-at-heading-p))
  20303. (goto-char origin)
  20304. (error "Cannot move further down"))))
  20305. (t
  20306. (let* ((elem (org-element-at-point))
  20307. (end (org-element-property :end elem))
  20308. (parent (org-element-property :parent elem)))
  20309. (if (and parent (= (org-element-property :contents-end parent) end))
  20310. (goto-char (org-element-property :end parent))
  20311. (goto-char end))))))
  20312. (defun org-backward-element ()
  20313. "Move backward by one element.
  20314. Move to the previous element at the same level, when possible."
  20315. (interactive)
  20316. (cond ((bobp) (error "Cannot move further up"))
  20317. ((org-with-limited-levels (org-at-heading-p))
  20318. ;; At an headline, move to the previous one, if any, or stay
  20319. ;; here.
  20320. (let ((origin (point)))
  20321. (org-backward-heading-same-level 1)
  20322. (unless (org-with-limited-levels (org-at-heading-p))
  20323. (goto-char origin)
  20324. (error "Cannot move further up"))))
  20325. (t
  20326. (let* ((trail (org-element-at-point 'keep-trail))
  20327. (elem (car trail))
  20328. (prev-elem (nth 1 trail))
  20329. (beg (org-element-property :begin elem)))
  20330. (cond
  20331. ;; Move to beginning of current element if point isn't
  20332. ;; there already.
  20333. ((/= (point) beg) (goto-char beg))
  20334. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20335. ((org-before-first-heading-p) (goto-char (point-min)))
  20336. (t (org-back-to-heading)))))))
  20337. (defun org-up-element ()
  20338. "Move to upper element."
  20339. (interactive)
  20340. (if (org-with-limited-levels (org-at-heading-p))
  20341. (unless (org-up-heading-safe) (error "No surrounding element"))
  20342. (let* ((elem (org-element-at-point))
  20343. (parent (org-element-property :parent elem)))
  20344. (if parent (goto-char (org-element-property :begin parent))
  20345. (if (org-with-limited-levels (org-before-first-heading-p))
  20346. (error "No surrounding element")
  20347. (org-with-limited-levels (org-back-to-heading)))))))
  20348. (defvar org-element-greater-elements)
  20349. (defun org-down-element ()
  20350. "Move to inner element."
  20351. (interactive)
  20352. (let ((element (org-element-at-point)))
  20353. (cond
  20354. ((memq (org-element-type element) '(plain-list table))
  20355. (goto-char (org-element-property :contents-begin element))
  20356. (forward-char))
  20357. ((memq (org-element-type element) org-element-greater-elements)
  20358. ;; If contents are hidden, first disclose them.
  20359. (when (org-element-property :hiddenp element) (org-cycle))
  20360. (goto-char (or (org-element-property :contents-begin element)
  20361. (error "No content for this element"))))
  20362. (t (error "No inner element")))))
  20363. (defun org-drag-element-backward ()
  20364. "Move backward element at point."
  20365. (interactive)
  20366. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20367. (let* ((trail (org-element-at-point 'keep-trail))
  20368. (elem (car trail))
  20369. (prev-elem (nth 1 trail)))
  20370. ;; Error out if no previous element or previous element is
  20371. ;; a parent of the current one.
  20372. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20373. (error "Cannot drag element backward")
  20374. (let ((pos (point)))
  20375. (org-element-swap-A-B prev-elem elem)
  20376. (goto-char (+ (org-element-property :begin prev-elem)
  20377. (- pos (org-element-property :begin elem)))))))))
  20378. (defun org-drag-element-forward ()
  20379. "Move forward element at point."
  20380. (interactive)
  20381. (let* ((pos (point))
  20382. (elem (org-element-at-point)))
  20383. (when (= (point-max) (org-element-property :end elem))
  20384. (error "Cannot drag element forward"))
  20385. (goto-char (org-element-property :end elem))
  20386. (let ((next-elem (org-element-at-point)))
  20387. (when (or (org-element-nested-p elem next-elem)
  20388. (and (eq (org-element-type next-elem) 'headline)
  20389. (not (eq (org-element-type elem) 'headline))))
  20390. (goto-char pos)
  20391. (error "Cannot drag element forward"))
  20392. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20393. ;; body's length (without final blanks) and by the length of
  20394. ;; blanks between ELEM and NEXT-ELEM.
  20395. (let ((size-next (- (save-excursion
  20396. (goto-char (org-element-property :end next-elem))
  20397. (skip-chars-backward " \r\t\n")
  20398. (forward-line)
  20399. ;; Small correction if buffer doesn't end
  20400. ;; with a newline character.
  20401. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20402. (org-element-property :begin next-elem)))
  20403. (size-blank (- (org-element-property :end elem)
  20404. (save-excursion
  20405. (goto-char (org-element-property :end elem))
  20406. (skip-chars-backward " \r\t\n")
  20407. (forward-line)
  20408. (point)))))
  20409. (org-element-swap-A-B elem next-elem)
  20410. (goto-char (+ pos size-next size-blank))))))
  20411. (defun org-mark-element ()
  20412. "Put point at beginning of this element, mark at end.
  20413. Interactively, if this command is repeated or (in Transient Mark
  20414. mode) if the mark is active, it marks the next element after the
  20415. ones already marked."
  20416. (interactive)
  20417. (let (deactivate-mark)
  20418. (if (and (org-called-interactively-p 'any)
  20419. (or (and (eq last-command this-command) (mark t))
  20420. (and transient-mark-mode mark-active)))
  20421. (set-mark
  20422. (save-excursion
  20423. (goto-char (mark))
  20424. (goto-char (org-element-property :end (org-element-at-point)))))
  20425. (let ((element (org-element-at-point)))
  20426. (end-of-line)
  20427. (push-mark (org-element-property :end element) t t)
  20428. (goto-char (org-element-property :begin element))))))
  20429. (defun org-narrow-to-element ()
  20430. "Narrow buffer to current element."
  20431. (interactive)
  20432. (let ((elem (org-element-at-point)))
  20433. (cond
  20434. ((eq (car elem) 'headline)
  20435. (narrow-to-region
  20436. (org-element-property :begin elem)
  20437. (org-element-property :end elem)))
  20438. ((memq (car elem) org-element-greater-elements)
  20439. (narrow-to-region
  20440. (org-element-property :contents-begin elem)
  20441. (org-element-property :contents-end elem)))
  20442. (t
  20443. (narrow-to-region
  20444. (org-element-property :begin elem)
  20445. (org-element-property :end elem))))))
  20446. (defun org-transpose-element ()
  20447. "Transpose current and previous elements, keeping blank lines between.
  20448. Point is moved after both elements."
  20449. (interactive)
  20450. (org-skip-whitespace)
  20451. (let ((end (org-element-property :end (org-element-at-point))))
  20452. (org-drag-element-backward)
  20453. (goto-char end)))
  20454. (defun org-unindent-buffer ()
  20455. "Un-indent the visible part of the buffer.
  20456. Relative indentation (between items, inside blocks, etc.) isn't
  20457. modified."
  20458. (interactive)
  20459. (unless (eq major-mode 'org-mode)
  20460. (error "Cannot un-indent a buffer not in Org mode"))
  20461. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20462. unindent-tree ; For byte-compiler.
  20463. (unindent-tree
  20464. (function
  20465. (lambda (contents)
  20466. (mapc
  20467. (lambda (element)
  20468. (if (memq (org-element-type element) '(headline section))
  20469. (funcall unindent-tree (org-element-contents element))
  20470. (save-excursion
  20471. (save-restriction
  20472. (narrow-to-region
  20473. (org-element-property :begin element)
  20474. (org-element-property :end element))
  20475. (org-do-remove-indentation)))))
  20476. (reverse contents))))))
  20477. (funcall unindent-tree (org-element-contents parse-tree))))
  20478. (defun org-show-subtree ()
  20479. "Show everything after this heading at deeper levels."
  20480. (interactive)
  20481. (outline-flag-region
  20482. (point)
  20483. (save-excursion
  20484. (org-end-of-subtree t t))
  20485. nil))
  20486. (defun org-show-entry ()
  20487. "Show the body directly following this heading.
  20488. Show the heading too, if it is currently invisible."
  20489. (interactive)
  20490. (save-excursion
  20491. (condition-case nil
  20492. (progn
  20493. (org-back-to-heading t)
  20494. (outline-flag-region
  20495. (max (point-min) (1- (point)))
  20496. (save-excursion
  20497. (if (re-search-forward
  20498. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20499. (match-beginning 1)
  20500. (point-max)))
  20501. nil)
  20502. (org-cycle-hide-drawers 'children))
  20503. (error nil))))
  20504. (defun org-make-options-regexp (kwds &optional extra)
  20505. "Make a regular expression for keyword lines."
  20506. (concat
  20507. "^#\\+\\("
  20508. (mapconcat 'regexp-quote kwds "\\|")
  20509. (if extra (concat "\\|" extra))
  20510. "\\):[ \t]*\\(.*\\)"))
  20511. ;; Make isearch reveal the necessary context
  20512. (defun org-isearch-end ()
  20513. "Reveal context after isearch exits."
  20514. (when isearch-success ; only if search was successful
  20515. (if (featurep 'xemacs)
  20516. ;; Under XEmacs, the hook is run in the correct place,
  20517. ;; we directly show the context.
  20518. (org-show-context 'isearch)
  20519. ;; In Emacs the hook runs *before* restoring the overlays.
  20520. ;; So we have to use a one-time post-command-hook to do this.
  20521. ;; (Emacs 22 has a special variable, see function `org-mode')
  20522. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20523. isearch-mode-end-hook-quit)
  20524. ;; Only when the isearch was not quitted.
  20525. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20526. 'append 'local)))))
  20527. (defun org-isearch-post-command ()
  20528. "Remove self from hook, and show context."
  20529. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20530. (org-show-context 'isearch))
  20531. ;;;; Integration with and fixes for other packages
  20532. ;;; Imenu support
  20533. (defvar org-imenu-markers nil
  20534. "All markers currently used by Imenu.")
  20535. (make-variable-buffer-local 'org-imenu-markers)
  20536. (defun org-imenu-new-marker (&optional pos)
  20537. "Return a new marker for use by Imenu, and remember the marker."
  20538. (let ((m (make-marker)))
  20539. (move-marker m (or pos (point)))
  20540. (push m org-imenu-markers)
  20541. m))
  20542. (defun org-imenu-get-tree ()
  20543. "Produce the index for Imenu."
  20544. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20545. (setq org-imenu-markers nil)
  20546. (let* ((n org-imenu-depth)
  20547. (re (concat "^" (org-get-limited-outline-regexp)))
  20548. (subs (make-vector (1+ n) nil))
  20549. (last-level 0)
  20550. m level head)
  20551. (save-excursion
  20552. (save-restriction
  20553. (widen)
  20554. (goto-char (point-max))
  20555. (while (re-search-backward re nil t)
  20556. (setq level (org-reduced-level (funcall outline-level)))
  20557. (when (and (<= level n)
  20558. (looking-at org-complex-heading-regexp))
  20559. (setq head (org-link-display-format
  20560. (org-match-string-no-properties 4))
  20561. m (org-imenu-new-marker))
  20562. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20563. (if (>= level last-level)
  20564. (push (cons head m) (aref subs level))
  20565. (push (cons head (aref subs (1+ level))) (aref subs level))
  20566. (loop for i from (1+ level) to n do (aset subs i nil)))
  20567. (setq last-level level)))))
  20568. (aref subs 1)))
  20569. (eval-after-load "imenu"
  20570. '(progn
  20571. (add-hook 'imenu-after-jump-hook
  20572. (lambda ()
  20573. (if (derived-mode-p 'org-mode)
  20574. (org-show-context 'org-goto))))))
  20575. (defun org-link-display-format (link)
  20576. "Replace a link with either the description, or the link target
  20577. if no description is present"
  20578. (save-match-data
  20579. (if (string-match org-bracket-link-analytic-regexp link)
  20580. (replace-match (if (match-end 5)
  20581. (match-string 5 link)
  20582. (concat (match-string 1 link)
  20583. (match-string 3 link)))
  20584. nil t link)
  20585. link)))
  20586. (defun org-toggle-link-display ()
  20587. "Toggle the literal or descriptive display of links."
  20588. (interactive)
  20589. (if org-descriptive-links
  20590. (progn (org-remove-from-invisibility-spec '(org-link))
  20591. (org-restart-font-lock)
  20592. (setq org-descriptive-links nil))
  20593. (progn (add-to-invisibility-spec '(org-link))
  20594. (org-restart-font-lock)
  20595. (setq org-descriptive-links t))))
  20596. ;; Speedbar support
  20597. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20598. "Overlay marking the agenda restriction line in speedbar.")
  20599. (overlay-put org-speedbar-restriction-lock-overlay
  20600. 'face 'org-agenda-restriction-lock)
  20601. (overlay-put org-speedbar-restriction-lock-overlay
  20602. 'help-echo "Agendas are currently limited to this item.")
  20603. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20604. (defun org-speedbar-set-agenda-restriction ()
  20605. "Restrict future agenda commands to the location at point in speedbar.
  20606. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20607. (interactive)
  20608. (require 'org-agenda)
  20609. (let (p m tp np dir txt)
  20610. (cond
  20611. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20612. 'org-imenu t))
  20613. (setq m (get-text-property p 'org-imenu-marker))
  20614. (with-current-buffer (marker-buffer m)
  20615. (goto-char m)
  20616. (org-agenda-set-restriction-lock 'subtree)))
  20617. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20618. 'speedbar-function 'speedbar-find-file))
  20619. (setq tp (previous-single-property-change
  20620. (1+ p) 'speedbar-function)
  20621. np (next-single-property-change
  20622. tp 'speedbar-function)
  20623. dir (speedbar-line-directory)
  20624. txt (buffer-substring-no-properties (or tp (point-min))
  20625. (or np (point-max))))
  20626. (with-current-buffer (find-file-noselect
  20627. (let ((default-directory dir))
  20628. (expand-file-name txt)))
  20629. (unless (derived-mode-p 'org-mode)
  20630. (error "Cannot restrict to non-Org-mode file"))
  20631. (org-agenda-set-restriction-lock 'file)))
  20632. (t (error "Don't know how to restrict Org-mode's agenda")))
  20633. (move-overlay org-speedbar-restriction-lock-overlay
  20634. (point-at-bol) (point-at-eol))
  20635. (setq current-prefix-arg nil)
  20636. (org-agenda-maybe-redo)))
  20637. (eval-after-load "speedbar"
  20638. '(progn
  20639. (speedbar-add-supported-extension ".org")
  20640. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20641. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20642. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20643. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20644. (add-hook 'speedbar-visiting-tag-hook
  20645. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20646. ;;; Fixes and Hacks for problems with other packages
  20647. ;; Make flyspell not check words in links, to not mess up our keymap
  20648. (defun org-mode-flyspell-verify ()
  20649. "Don't let flyspell put overlays at active buttons, or on
  20650. {todo,all-time,additional-option-like}-keywords."
  20651. (let ((pos (max (1- (point)) (point-min)))
  20652. (word (thing-at-point 'word)))
  20653. (and (not (get-text-property pos 'keymap))
  20654. (not (get-text-property pos 'org-no-flyspell))
  20655. (not (member word org-todo-keywords-1))
  20656. (not (member word org-all-time-keywords))
  20657. (not (member word org-options-keywords))
  20658. (not (member word (mapcar 'car org-startup-options)))
  20659. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20660. (defun org-remove-flyspell-overlays-in (beg end)
  20661. "Remove flyspell overlays in region."
  20662. (and (org-bound-and-true-p flyspell-mode)
  20663. (fboundp 'flyspell-delete-region-overlays)
  20664. (flyspell-delete-region-overlays beg end))
  20665. (add-text-properties beg end '(org-no-flyspell t)))
  20666. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20667. (eval-after-load "bookmark"
  20668. '(if (boundp 'bookmark-after-jump-hook)
  20669. ;; We can use the hook
  20670. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20671. ;; Hook not available, use advice
  20672. (defadvice bookmark-jump (after org-make-visible activate)
  20673. "Make the position visible."
  20674. (org-bookmark-jump-unhide))))
  20675. ;; Make sure saveplace shows the location if it was hidden
  20676. (eval-after-load "saveplace"
  20677. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20678. "Make the position visible."
  20679. (org-bookmark-jump-unhide)))
  20680. ;; Make sure ecb shows the location if it was hidden
  20681. (eval-after-load "ecb"
  20682. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20683. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20684. (if (derived-mode-p 'org-mode)
  20685. (org-show-context))))
  20686. (defun org-bookmark-jump-unhide ()
  20687. "Unhide the current position, to show the bookmark location."
  20688. (and (derived-mode-p 'org-mode)
  20689. (or (outline-invisible-p)
  20690. (save-excursion (goto-char (max (point-min) (1- (point))))
  20691. (outline-invisible-p)))
  20692. (org-show-context 'bookmark-jump)))
  20693. ;; Make session.el ignore our circular variable
  20694. (eval-after-load "session"
  20695. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20696. ;;;; Experimental code
  20697. (defun org-closed-in-range ()
  20698. "Sparse tree of items closed in a certain time range.
  20699. Still experimental, may disappear in the future."
  20700. (interactive)
  20701. ;; Get the time interval from the user.
  20702. (let* ((time1 (org-float-time
  20703. (org-read-date nil 'to-time nil "Starting date: ")))
  20704. (time2 (org-float-time
  20705. (org-read-date nil 'to-time nil "End date:")))
  20706. ;; callback function
  20707. (callback (lambda ()
  20708. (let ((time
  20709. (org-float-time
  20710. (apply 'encode-time
  20711. (org-parse-time-string
  20712. (match-string 1))))))
  20713. ;; check if time in interval
  20714. (and (>= time time1) (<= time time2))))))
  20715. ;; make tree, check each match with the callback
  20716. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20717. ;;;; Finish up
  20718. (provide 'org)
  20719. (run-hooks 'org-load-hook)
  20720. ;;; org.el ends here